Skip to content

inkling2 metal: routed experts and combine one thread an output, each dot the serial chain - #4894

Merged
ashhart merged 5 commits into
mainfrom
lead/inkling2-metal-expert-grid
Nov 24, 2026
Merged

ashhart merged 5 commits into
mainfrom
lead/inkling2-metal-expert-grid

Conversation

@ashhart

@ashhart ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Inkling 2 Mini Metal: the routed experts and the combine run one thread per output, and the contract checks the new grid byte for byte against main's kernels.

Before: each expert product ran one 32-thread group per route pair, its lanes looping over the pair's outputs (36 outputs a lane for gate-up at n 1152, 72 for down at n 2304). The combine ran one group per token.

After:

  • Launch shape: the four products (NVFP4 and MXFP8 gate-up and down) and the combine launch threadgroups along the outputs, one thread per output. That gives n/32 groups, plus one for a remainder, per route pair or per token.
  • Same arithmetic: each thread runs the same ink2_dot_nv / ink2_dot_mx as before. That is one FP32 chain from column 0 up, with every product rounded before its add (fp contract off), followed by the same BF16 rounding and activation. The combine keeps its ascending-expert loop.
  • Only placement changes: an output moves to a threadgroup of its own and keeps its lane number.
  • Bounds: threads past the row return before any read. Launches stay at 32 threads with no threadgroup memory, so the launch records are unchanged.

Credit: the outputs-by-route-pairs grid follows @sethforprivacy's 2f386ca in #4448 (Co-authored-by in the commit). Its lane-strided partial sums and simd_sum change the FP32 summation order, so they are not taken.

Base: two commits on main 9fa8863. Between 5e39051, where the device cells below ran, and 9fa8863, main changed no Inkling or Metal file.

Contract: the second commit adds a byte-for-byte check against main.

  • The baseline: main's experts.metal at 5e39051, frozen under zig/tests/contract/baselines/, run on main's own launch (one group per route pair, a token for the combine).
  • The cells: one for each of the five changed operations. On identical inputs, with both outputs poisoned the same way, every output byte must equal main's.
    • Shapes: several output blocks with tails (n 63, 64, 65, plus 1, 31, 32 and 33 as edges), Mini's own widths (gate-up k 2304 n 1152, down k 1152 n 2304, combine n 2304), and 64 rows (128 route pairs).
    • The float64 reference now holds dots of 2,304 terms.
  • Planted controls: two grid faults in all five kernels, each of which must go red against main's bytes.
    • Every output block past the first returns before writing.
    • The pair (token) index is halved.
  • Host tests: they check that the source keeps both dots serial with no simd_sum, that the grid covers every output exactly once with no thread past the row reading, and that block counts can't overflow at the largest u32 widths.

Checked on device (thinkingmachines/Inkling-2-Mini at ed40e7da62):

  • Kernel contract, M5 Ultra, this exact tree (c625a2c946), tf-kernel-contract built for apple_m1 in ReleaseSafe:
    • --only 'metal inkling2 experts': 12 kernels pass gates 1-4, and 17 controls go red as planted, 0 missed, 0 limit problems.
    • The five grid cells are bit-equal to main's kernels at every shape: 9 shapes for each product, 8 for the combine.
    • All 10 grid controls go red against main's bytes.
    • --only inkling2: 46 kernels pass, 51 controls go red, 0 limit problems.
  • Kernel contract, M3 Ultra, the same tree and settings:
    • --only 'metal inkling2 experts': 12 kernels pass gates 1-4, and 17 controls go red, 0 missed, 0 limit problems.
    • The five grid cells are bit-equal to main's kernels at every shape, and all 10 grid controls go red.
    • --only inkling2: 46 kernels pass, 51 controls go red, 0 limit problems.
  • Served tokens, M5 Ultra, main 5e39051 against this change on it, builds alternated base-new-new-base, one stream:
    • Requests: 256 tokens each, greedy and seeded-sampled, on a 30-token and a 2,084-token prompt, plus a second turn resumed from the prompt cache and fresh.
    • All 9 requests give the same generated ids on all four runs, and every resumed reply equals its fresh run.
    • Decode: main 0.850 tok/s, this change 13.736 tok/s (median over the cells, 16.2x). Per cell the gain is 14.4x to 18.5x, and no cell is slower.
  • Served tokens, M3 Ultra, the same cells and order: all 9 requests give the same generated ids on all four runs,
    and every resumed reply equals its fresh run.
    • Decode: main 0.607 tok/s, this change 10.346 tok/s (median, 17.0x). Per cell the gain is 15.1x to 19.7x, and no
      cell is slower.
    • Generated ids differ between M5 and M3 for most requests, on main as well. Each device is equal only to itself.
  • Four streams and DSpark, M5 Ultra, main 5e39051 against this change, builds alternated:
    • Prompts: four (math, code, prose, a ~2,100-token prompt) plus a resumed second turn, 96 tokens, greedy.
    • Every digest equals the one-stream serial reference on all four runs: 4 streams with no drafts, 8/8 each; 1 stream with DSpark, 5/5; 4 streams with DSpark, 8/8.
    • Speed:
      • 4 streams, aggregate with prompt time: 1.61 to 4.60 tok/s (2.86x)
      • DSpark at 1 stream: 0.87 to 11.4 tok/s (13.0x)
      • DSpark at 4 streams: 1.40 to 2.80 tok/s (2.00x)
    • No cell is slower.

Not checked:

  • Macs other than M5 Ultra and M3 Ultra.
  • Prompt-chunk boundaries (63/64/65 rows) as served cells, and cancellation.
  • CUDA, which this change doesn't touch.

ashhart and others added 5 commits November 24, 2026 20:45
… dot the serial chain

Each expert product ran one 32-thread group a route pair, its lanes looping
over the pair's outputs: 36 outputs a lane for GU (n 1152), 72 for down
(n 2304), and eight groups a decode step. Combine ran one group a token.

The four products and the combine now launch threadgroups along the outputs
by route pairs (or tokens), one thread an output. Each thread runs the same
ink2_dot_nv / ink2_dot_mx as before: one FP32 chain from column 0 up, every
product rounded before its add (fp contract off), then the same BF16 rounding
and activation. Combine keeps its ascending-expert loop. Only which thread
computes an output changes, so every output is bit-identical to the serial
reference. Threads past the row return before any read. Launches stay
32 threads, so the existing records hold; the quantize kernels are unchanged.

The grid of outputs by route pairs and launchGrid follow #4448's
2f386ca; its lane-strided partial sums and simd_sum, which change the
FP32 sum order, are not taken.

Tests: test-inkling2-controls-host checks the source keeps both dots serial
with no simd_sum and launches every product and the combine one output a
thread (fails before: five lane loops), and that the grid covers every output
exactly once, with no thread past the row reading. The block count rounds
up as n / 32 plus one for a remainder, so the largest u32 widths (4294967265
through 4294967295) take 134217728 blocks instead of overflowing n + 31; a
test asserts those widths and fails on the n + 31 form.

Co-authored-by: Seth For Privacy <40500387+sethforprivacy@users.noreply.github.com>
… kernels

The contract's expert cells ran products of at most 12 outputs a pair and
combines of at most 12 a token, so only the first output block of the new
grid ever ran, and they compared with a float64 bound, not with main's bytes.

Each of the five operations the grid changed (NVFP4 and MXFP8 GU and down,
and the combine) now has a cell that replaces main's kernel: main's
experts.metal at 5e39051, frozen under baselines/, on main's own launch
(one 32-thread group a route pair, a token for the combine, each lane
looping over its outputs). On the same inputs, every output byte of the new
grid must equal main's. The shapes are several output blocks with tails
(n 65, 64, 63 and the 1, 31, 32, 33 edges), Mini's own widths (GU k 2304 n
1152, down k 1152 n 2304, combine n 2304) and 64 rows (128 route pairs).
The float64 reference now holds dots of 2304 terms.

Two planted grid faults go into all five kernels, each a control at the
shapes gate: every output block past the first returning before it
writes, and the pair (token) index halved so two pairs write one row.
@ashhart
ashhart force-pushed the lead/inkling2-metal-expert-grid branch from 2bd11a5 to 3d8f271 Compare November 24, 2026 20:45
@ashhart
ashhart merged commit 7aadce5 into main Nov 24, 2026
12 checks passed
@ashhart
ashhart deleted the lead/inkling2-metal-expert-grid branch November 24, 2026 20:45
@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner Author

Thank you @sethforprivacy. This is merged into main with your commits under your own name, so you're credited as a TensorFold contributor.

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.

1 participant