Skip to content

Inkling 2 Mini on Metal: decode without tensor units 9 to about 21 tok/s, every output bit equal to main's - #4919

Open
BlivionIaG wants to merge 13 commits into
ashhart:mainfrom
BlivionIaG:slice/decode-exact
Open

BlivionIaG wants to merge 13 commits into
ashhart:mainfrom
BlivionIaG:slice/decode-exact

Conversation

@BlivionIaG

@BlivionIaG BlivionIaG commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Inkling 2 Mini Metal decode without tensor units (M1 to M4) reads each routed-expert weight a byte at a time. This reads 16-byte words in the two expert dot products (ink2_dot_nv, ink2_dot_mx), keeping one serial chain from column 0 per output, and gives the router, dynamic convolution, logits and loop gates 16-byte loads and a parallel top-8 pick at one row. Every output bit equals main's. On an M4 Pro, one-stream decode goes from about 9 to about 21 tok/s against main a15c7b0603.

Checks on this head

  • Head commit: 956d1c857e (slice/decode-exact), a merge of main 4c79cfe346.
  • Lean checker: 165 on main and on this head, 0 new. tools/pr_check.py --base 4c79cfe346 --pr 4919: 0 errors, 0 warnings. zig fmt --check build.zig zig: clean.
  • Tests (M4 Pro, -Dcpu=apple_m1): test-inkling2-metal-host 101 passed, 6 skipped; test-inkling2-host 45 passed, 4 skipped (the real-checkpoint tests without TF_INKLING2_CKPT).
  • Kernel contract, tf-kernel-contract --only inkling2: 61 kernels pass gates 1-4, 68 controls red as planted, 0 missed, 0 limit problems; includes the repeated NaN-key check (50 launches, 6 rows) with its two planted controls (no serial fallback; flag taken from expert 0's key only). The check passes on the old two-writer kernel too (20 of 20), so the race never showed; the single writer removes it.
  • CI: not run on this head when written.
  • Not run: CUDA; M1, M2, M3 and M4 Max; drafted speed on the M4 Pro (memory pressure from another job).

Receipt

  • Exactness against main 95591a94f8, M4 Pro, --context 8192 --parallel 1 --no-drafts --prompt-cache-gib 0 --no-thinking, full token ids and SHA-256 equal in all 14 cells: cold 255 863541147578, 256 1f6434a3a13f, 257 f5a8eb75e54b, 512 9d934d0e3802, 513 21583159d990; chat 8/16/24 4c2031fb1535 3a974c335a4e 0e69856242e3; greedy 256 6ce0eb7d34ed; seeded (T 0.7, seed 1234) 12dd740614ad; four concurrent requests equal their solo runs. The first merge (6cf853d06d) gave the same 14 against main a15c7b0603.
  • Speed, M4 Pro, tools/bench_concurrent.py, --parallel 4, 128 tokens, A-B-B-A against main a15c7b0603 (one-row decode runs the same kernels on 95591a94f8): one stream, code 9.3 and 9.2 tok/s on main, 20.9 here (B1's 1.9 was its first-run pipeline compile); chat 9.4 on main, 20.7 and 20.3 here. Four streams: code 10.8 and 11.0 on main, 32.4 and 37.8 here; chat 2.1 on main A1 and 39.9 and 39.5 here. Main's A2 at four streams did not finish because another job put the machine under memory pressure, and a rerun against 95591a94f8 was spoilt by the same pressure (503s), so I give no number from it. --alone on B1: 20 of 20 equal.
  • M5 Ultra, earlier design 7cddab1446 against main 9a38b1f7b3: all 8 replies equal in token ids and SHA-256; one stream 0.9 on main to 36.8 here and four streams 3.1 to 43.9 aggregate (code); --alone 30 of 30 equal. Not re-measured on this head.
  • New switch: none. Boxes it reaches: Metal, Inkling 2 Mini, NVFP4 and MXFP8 expert layers; the tensor-unit prompt path is not in the diff.
  • Environment: Zig 0.17.0, -Dcpu=apple_m1 -Doptimize=ReleaseFast, Inkling-2-Mini ed40e7da625f594fc9c4990f5f66be24873c22d7; Apple M4 Pro 48 GiB (no tensor units), and Apple M5 Ultra 256 GiB for the earlier-design cells.
  • Licence of any new weights or drafter: n/a.

Sum orders

  • Experts: each output is one serial chain over its row's columns in index order from zero, product then add, no fused multiply-add. Weight decode goes through FP16 bit patterns that equal the reference's E2M1/E4M3 values and scales exactly; MXFP8 scales outside the exact range and NaN codes use the reference decode. The products refuse activation or weight offsets that are not 16-byte aligned.
  • Router, dynamic conv, logits, loop gates: the same serial chains with wider loads; the router's top 8 are rank 0..7 by (key descending, index ascending), the serial scan's result, and any NaN key runs the original serial scan. From two rows main's split kernels run, and the contract compares them against these.

Checklist

  • The title and description match the head commit.
  • New tests fail before the change: the contract controls plant each one; no new host tests.
  • Comments are one line. No measurements or history in the source.
  • No personal data or AI attribution lines.
  • Concurrent equals solo; no drafts involved. No precision traded; no bit changes.

…anks routes once

- The four expert dot kernels run one lane to one output weight row: a simdgroup covers 32 consecutive rows, a threadgroup eight simdgroups, and the activation row is read in uniform 16-byte loads. The NVFP4 and MXFP8 weights are decoded from 16-byte words, two elements at a time, through FP16 bit patterns that equal the reference's E2M1 and E4M3 values and scale exactly; the MXFP8 path falls back to the reference decode for scales outside the exact range and for NaN codes.
- Sum order preserved: each output is still one serial chain over its row's columns in index order, starting at zero, adding the BF16 weight times the BF16 activation product one term at a time with the same rounding points and no fused multiply-add, so every output bit equals the previous kernel's.
- tf_ink2_combine launches 256 outputs a threadgroup. A threadgroup ranks the token's routes by (expert, route slot) once, drops experts outside the layer, and every output adds weight times down in that order from zero, which is the ascending-expert, ascending-slot order the old per-output scan visited. Same chain, same rounding.
- Contract: dot kernels and combine at 256 threads; the combine control now plants route-slot order; the gate/up control plants a dropped pair swap.
…byte loads; router picks in parallel

- tf_inkling2_router, tf_inkling2_conv_dynamic and tf_inkling2_logits keep one thread to one dot product but load eight BF16 columns of both rows at a time, four loads ahead of each step.
- Sum order preserved: each dot is the same serial chain over columns 0..2303, product then add, with the rounding to BF16 or softcap after it as before.
- The router ranks its 128 keys (score plus bias) in parallel: rank 0 to 7 by key descending, then expert index ascending, is exactly the set the serial first-greatest scan picks, and the ascending-expert normalization sum is unchanged. A NaN key makes the scan order dependent, so any NaN key runs the original serial scan. The control now plants the dropped bias.
- The zeta chain (64 ranks, 4,608 columns) and the forget and gain chains (64 ranks a channel) load eight BF16 weights at a time and batch the loads ahead of each step.
- Sum order preserved: each zeta, forget and gain value is the same serial chain over its columns in index order, product then add, zeta rounded to BF16 as before, no fused multiply-add.
- Names the exact decode speedup and the unchanged sum orders.
@BlivionIaG BlivionIaG changed the title Inkling 2 Mini on Metal: decode without tensor units 0.7 to about 22 tok/s, every output bit equal to main's Inkling 2 Mini on Metal: decode without tensor units 9 to about 21 tok/s, every output bit equal to main's Nov 25, 2026
@BlivionIaG

Copy link
Copy Markdown
Contributor Author

Main's ff73e7441c (routed experts and combine one thread an output) and aa3422fe09 (the expert grid against main's frozen kernels) changed the same kernels this PR changed, and #4912 split the router and loop gates for two rows or more. I merged main twice (6cf853d06d, then ce117bffd9 on main 95591a94f8) and adapted the PR to main's contract:

Checks on ce117bffd9, M4 Pro, -Dcpu=apple_m1:

  • tf-kernel-contract --only inkling2: 48 kernels pass gates 1-4, 55 controls red as planted, 0 missed, 0 limit problems (main 95591a94f8: the same 48 and 55). The grid cells compare each expert product and the combine byte for byte with main's frozen kernels.
  • test-inkling2-metal-host 84 passed, 4 skipped; test-inkling2-host 45 passed, 4 skipped. Lean 165 and 165, 0 new; pr_check --base 95591a94f8 --pr 4919 0 and 0; zig fmt --check clean.
  • Exactness against main 95591a94f8, --context 8192 --parallel 1 --no-drafts --prompt-cache-gib 0 --no-thinking, full token ids and SHA-256 equal in all 14 cells: cold 255 863541147578, 256 1f6434a3a13f, 257 f5a8eb75e54b, 512 9d934d0e3802, 513 21583159d990; chat 8/16/24 4c2031fb1535 3a974c335a4e 0e69856242e3; greedy 256 6ce0eb7d34ed; seeded (T 0.7, seed 1234) 12dd740614ad; four concurrent requests equal their solo runs. The first merge (6cf853d06d) gave the same 14 against main a15c7b0603.
  • Speed, M4 Pro, bench_concurrent.py, --parallel 4, 128 tokens, A-B-B-A against main a15c7b0603 (one-row decode runs the same kernels on 95591a94f8): one stream, code 9.3 and 9.2 tok/s on main, 20.9 here (B1's 1.9 was its first-run pipeline compile); chat 9.4 on main, 20.7 and 20.3 here. Four streams: code 10.8 and 11.0 on main, 32.4 and 37.8 here; chat 2.1 on main A1 and 39.9 and 39.5 here, and A2 at four streams did not finish because another job put the machine under memory pressure. --alone on B1: 20 of 20 equal. A rerun against 95591a94f8 was spoilt by the same pressure (503s), so I give no number from it.

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks @BlivionIaG. The revised dots keep the serial sum order, and the current M4 checks are useful evidence.

One fix remains in the router: every thread that sees a NaN key writes the same non-atomic any_nan flag. Please make that flag single-writer or atomic, retaining the serial fallback, and add a repeated NaN-key check. The current M5 and draft-enabled paths also remain unmeasured; the earlier M5 numbers cover the previous design.

  • Synthena

# Conflicts:
#	CHANGELOG.md
Every thread scans all 128 keys, so thread 0 alone stores any_nan before the barrier; the serial fallback is unchanged. The contract's router check adds six rows with NaN keys at varied experts, fifty launches each equal to the serial scan and to the first launch's bytes, with two planted controls: no fallback, and a flag from expert 0's key only.
@AIFlowML

Copy link
Copy Markdown
Contributor

I rechecked 0caa787ea2145c5bd31ee9a01ada1bea316c2546 against main 4c79cfe346d3c239ff7dfd7aa694df799755bafa. The trial merge conflicted only in CHANGELOG.md. I kept main's changelog in the scratch tree and ran the merged code on the M5 Max with Zig 0.17.0 and macOS 27.2:

zig build tf-kernel-contract -Dcpu=apple_m1 -Doptimize=ReleaseFast
./zig-out/bin/tf-kernel-contract --only 'metal inkling2'

Exit 0. SUMMARY 61 kernels: 61 pass gates 1-4, 0 fail, 0 known findings; 67 controls: 67 red as planted, 0 missed; 0 limit problems; gates 10894 ms, limits 523 ms. The router's shape check includes the revised repeated NaN-key scan. Both added controls failed on selected IDs as intended, including when only expert 0 contributes to the flag.

This verifies the revised router on M5. The live drafted and undrafted performance comparisons remain separate.

Igor Lessio - AIFLOW LABS

@BlivionIaG

Copy link
Copy Markdown
Contributor Author

Thanks. The router's NaN flag now has one writer and the serial fallback is kept; the repeated NaN-key check (50 launches, 6 rows, NaN keys at varied positions, byte-equal to the serial scan each time) is in the contract with two planted controls, both red: no serial fallback, and the flag taken from expert 0's key only. That check does not go red on the old kernel (20 of 20 passes on the M4), so the two-writer race never showed here; the single writer removes it rather than fixing a failure I saw.

Re-measured on head 956d1c857e (main merged in; the kernels and tests are byte-identical to 0caa787ea2, which the M5 binaries were built from).

M5 Ultra, against main 01fec94955, --parallel 4 --context 8192 --prompt-cache-gib 0 --no-thinking

  • Exactness, --no-drafts: token ids and SHA-256 equal in all 14 cells: cold 255/256/257/512/513, chat 8/16/24, greedy 256, seeded (T 0.7, seed 1234) and four concurrent equal to solo. Cold 255 and 257 give the same repeated-token reply on main and on this head, so those two cells show equality but say little.
  • A/B/B/A, bench_concurrent.py --levels 1,4 --tokens 128 --temperatures 0 --alone --serial, tok/s: code one stream main 16.9 / 16.9, head 31.0 / 31.1; code four streams (aggregate) 35.4 / 35.4 against 51.8 / 51.8; chat one stream 17.0 / 17.0 against 31.0 / 31.0; chat four streams 36.8 / 36.8 against 52.7 / 52.7. --alone and --serial all equal, 0 failed.
  • Drafts on (--drafter, the bundled DSpark): drafted ids equal plain ids on main and on this head in the same 14 cells, greedy and seeded. Drafted bench, temperatures 0 and 0.7, levels 1 and 4, --alone --serial: all equal, 0 failed, "draft": false drafted 0. Accepted drafts are identical on main and head (T0 code 69, chat 54; T0.7 code 235, chat 233). tok/s, one stream / four streams aggregate: T0 code main 25.5 / 25.3, head 36.1 / 44.6; T0 chat 20.6 / 18.5 against 28.8 / 32.4; T0.7 code 19.9 / 20.0 against 27.7 / 35.6; T0.7 chat 22.4 / 19.4 against 31.7 / 34.5.

M4 Pro, against main 4c79cfe346: the same 14 cells are equal with drafts off and on. A/B/B/A tok/s: code one stream main 9.1 / 9.2, head 20.7 / 20.8; code four streams 11.2 / 11.5 against 39.2 / 38.3; chat one stream 9.2 / 9.2 against 20.9 / 20.2; chat four streams 12.4 / 12.5 against 41.0 / 40.4. The drafted speed on the M4 was spoilt by memory pressure from another job, so I give no number.

test-inkling2-metal-host 101 passed, 6 skipped; test-inkling2-host 45 passed, 4 skipped; contract 61 kernels and 68 controls; lean 165 and 165, 0 new; pr_check 0 and 0; fmt clean.

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks, @BlivionIaG. The single-writer flag closes the reported race and keeps the serial fallback. The repeated NaN-key check and new M5 plain/drafted and M4 parity results address the earlier asks. The tested code matches this head. The fixture did not reproduce the old race, so the single-writer source change is the closure. The code is ready to land; changelog integration stays with us.

  • Synthena

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants