Skip to content

inkling2 metal: the router and loop-gate dots a thread an output, bit for bit main's - #4912

Merged
ashhart merged 6 commits into
mainfrom
lead/inkling2-metal-router-split
Nov 24, 2026
Merged

ashhart merged 6 commits into
mainfrom
lead/inkling2-metal-router-split

Conversation

@ashhart

@ashhart ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Inkling 2 Mini Metal: the router's and the loop gates' dots run a thread an output, bit for bit main's.

The old launch shape: main's router computed its 128 expert scores on 128 threads of one 256-thread group, each a serial FP32 dot over 2,304 columns, and thread 0 then picked the top 8. The loop gates' down dot ran on 64 threads, each serial over 4,608 columns. Both ran one launch a row, so a prompt chunk of 64 rows made 64 launches of each.

The new kernels: these keep each output's serial dot and change only the launch shape:

  • tf_inkling2_route_scores: one thread per (expert, row), in 32-expert groups. It computes main's dot += float(w[d]) * float(v[d]) for d ascending, then main's sigmoid.
  • tf_inkling2_route_select: one thread per row. It applies main's top-8 (bias added, ties to the lower index), main's ascending-index insertion, the same 1e-20 denominator and divide(score, den) * 8.
  • tf_inkling2_loop_norm, _down, _apply: these split main's gate kernel into three launches that each take every row of a controller. They use main's two RMS reductions, one thread per (rank, row) for the serial 4,608-column down dot rounded to BF16, and main's forget and gain dots with its four separately rounded steps.

What stays the same: the shaders compile with the engine's safe math and fp contract(off), so the source order is the order of the adds.

When the forward uses them: from two rows on. At one row (one-stream decode), it keeps main's tf_inkling2_router and tf_inkling2_loop_gates, called exactly as main calls them. At one row the split's two router launches and three gate launches cost more than they save: it measured 3.6% slower decode on an M5 Ultra. Both paths give the same bits. A host test pins the switch at two rows. Main's tf_inkling2_router and tf_inkling2_loop_gates stay in the library and in the contract as the baseline. The state adds three scratch buffers (about 624 KiB at 64 rows), which every Mac's state budget counts.

The contract:

  • The cells:
    • a split router cell, which replaces tf_inkling2_router;
    • a split gates cell, which replaces tf_inkling2_loop_gates, run one row at a time as the forward called it.
  • Gate 3: every output byte must equal main's kernel at 1, 33 and 64 rows. That covers several output blocks, row tails and production's m stride, with NaN between rows.
  • Inputs: every dot opens with +B·x0 − B·x0 (B = 2^18), so any other summation order loses the low terms' bits.
  • Planted controls: four, which must go red at gate 3: a two-chain strided score dot, a score stored one expert on, a two-chain strided down dot, and a zeta stored one rank on.
  • Records: five new launch records.

Checks:

  • Host: on main fa2358c, lean is 165 (equal to main) and fmt is clean. On Linux, test-inkling2-controls-host, test-kernel-contract and test-inkling2-host pass. On an M5 Ultra build host, test-inkling2-metal-host passes, 79 of 83 with 4 skipped, including the inventory tests (one symbol each in the concatenated glue and loop shaders) and the split launches' record tests. These ran before the third commit, which changes only two .metal lines that no host step reads.
  • Shader compile: no host step compiles the Metal shaders. The library compile is proven only by the contract run below. The first device run caught exactly that: two split kernels mixed a scalar thread position with a uint2 threadgroup position, which Metal rejects. The third commit gives both a uint2 and uses its x, the same thread index.
  • Kernel contract, M3 Ultra (tf-kernel-contract --only "metal inkling2", apple_m1 ReleaseSafe, on the tree before the one-row switch, whose kernels are the same):
    • 43 kernels pass gates 1-4, and 45 controls go red as planted, 0 missed, 0 limit problems.
    • Both split cells are bit-equal to main's kernels at 1, 33 and 64 rows.
    • All four split controls go red.
    • The five new launches are within limits.
  • Kernel contract, M5 Ultra, this change's final kernels and forward, built in the job:
    • 43 kernels pass gates 1-4, and 45 controls go red, 0 missed, 0 limit problems.
    • Both split cells are bit-equal to main's at 1, 33 and 64 rows, and all four split controls go red.
    • The five new launches are within limits.
    • The Inkling controls and Metal host tests pass: 94 of 98, 4 skipped.
  • Served A/B, M5 Ultra (thinkingmachines/Inkling-2-Mini at ed40e7da62). Base: the Inkling expert grid from inkling2 metal: routed experts and combine one thread an output, each dot the serial chain #4894 on main 9fa8863. New: the same plus this change. Builds alternated base, new, new, base.
    • Tokens: every generated digest is equal across builds and to each build's one-stream plain replies, with and without the DSpark drafter. That covers 1, 2 and 4 streams, plus a 64-row raw prompt run five times.
    • One-stream decode: 16.80 to 16.71 tok/s (0.995, inside the base runs' own 0.85% spread).
    • Two rows of decode: +9.7% per stream, +14.9% aggregate.
    • Four streams: +63.7% (plain) and +62.1% (DSpark), aggregate with prompt time.
    • DSpark at one stream: +9.8%.
    • 64-row prompt: 1.189 to 0.682 s to first token (42.6% faster).
    • 2,100-token prompt: 63.2 to 36.5 s (42.2% faster).

Not checked: CUDA (not touched), and Macs other than M5 Ultra and M3 Ultra.

… for bit main's

Main's router computes 128 expert scores in one 256-thread group a row and
then picks on thread 0; the loop gates run one 256-thread group a row, with
64 threads on the down dot, and the forward called them once per row. Both
keep each output's FP32 dot as one serial ascending chain, which is what makes
them exact, and both leave most of the GPU idle.

The split keeps every chain and every expression as main wrote them and only
changes the launch shape:
- router: tf_inkling2_route_scores, one thread an (expert, row) score in
  32-expert groups, then tf_inkling2_route_select, one thread a row with
  main's top-8 and ascending-index combine over the stored scores;
- loop gates: tf_inkling2_loop_norm (main's reduction, its BF16 rows
  stored), tf_inkling2_loop_down (one thread a (rank, row), main's serial
  dot) and tf_inkling2_loop_apply (main's forget and gain), over every row
  of a controller in one call; m's rows are a token's message entries apart.

The forward uses the split by default; main's kernels stay in the library as
the contract's baseline. The contract compares the split's bytes (and the
router's expert ids) with main's kernels at 1, 33 and 64 rows, with inputs
that open each dot with +B x0 - B x0 so any other summation order shows; m
is laid out at production's stride with NaN between rows. A strided two-chain
dot and a one-index-off store are planted for each kernel and must go red.
… without their symbols

The inventory tests count each kernel symbol in the concatenated glue and loop shaders and require one; the
comments made it two.
tf_inkling2_route_scores and tf_inkling2_loop_down took their thread
position as a scalar uint beside a uint2 threadgroup position. Metal
requires a kernel's position inputs to be all scalars or all vectors of one
width, so the glue and loop libraries failed to compile on the device
("expecting input declarations with either all scalar types or all vector
types with the same number of elements") and the engine could not load
them. Both now take uint2 and use its x, the same thread index as before.
No arithmetic change.
On an M5 Ultra the split kernels made one-stream decode 3.6% slower (16.76
to 16.15 tok/s): at one row the router's two launches and the gates' three
cost more than the one launch each they replace, with nothing to run side
by side. From two rows they win (a 64-row prompt chunk 42% faster).

The forward now takes the split router and gates from two rows and main's
tf_inkling2_router and tf_inkling2_loop_gates below that, exactly as main
called them. Both paths give the same bits: the contract checks the split
kernels byte for byte against main's. A host test pins the switch at two
rows.
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