Split-K without a compiler: 516.523 to 833.559 tokens/s on an Apple M2 Ultra
A kernel-replay engine that cannot compile a kernel reached Apple's own split-K mechanism through emission instead: the same captured matrix multiply dispatched over slices of the contraction dimension, partials summed by a captured add. Prefill went from 516.523 to 833.559 tokens/s with the whole matmul family admitted. The engine was right about every value it computed and wrong about one range it declared.
A kernel-replay engine we built runs prefill of a 4-bit 4-billion-parameter model on an Apple M2 Ultra by replaying captured GPU kernels: the binaries, uniform pushes, bind offsets, grid geometry and barrier flags recorded from a reference run, dispatched again and again. It cannot compile a kernel.
Prefill throughput went from 516.523 to 833.559 tokens/s (+61.4 %) on a frozen 952-token prompt, with the whole matrix-multiply family now split and the token-id gate matching the reference on both arms — 983 of 984 ids, and the single disagreement is the reference's own known departure. Against a matched comparator, a vendor's Vulkan backend for the same GGUF on the same machine with the same prompt, that is 1.59×.
The interesting part is how it was found. The engine was right about every value it computed and wrong about one range it declared.
What the engine is, and what it may change
AGX is the GPU family in Apple's M2 Ultra; the engine drives it through a replay path rather than a compiler. It takes the captured AGX binary bytes, the uniform push values, the bind set, the geometry and the barrier map, and submits them.
This is the same engine and the same machine as the earlier note on the
barrier drain (/blog/agx-barrier-drain-and-the-accidental-rebuild).
That session was about the decode path and the gap between one replay runner
and macOS Metal, attributed to 471 barrier drains; nothing there touched the
matmul family. What follows is a different lever on a different path — the
prefill pass — and a different kind of defect.
The engine can change which captured kernel it dispatches, the push words, the bind set, the grid, the local size and the barrier order. It cannot change the kernel's instructions, its register allocation, its tile, its use of the matrix-multiply instructions, or the driver's scheduler. Any mechanism that lives inside a compiled kernel is closed to it. Split-K, as MLX implements it, is a launch-shape decision — so it is reachable.
Where the time went
Before changing anything, we measured a 32-row chunk of the prefill pass: 688 operations, of which the matrix multiply family was 249 launches and 52.868 ms of 59.21 ms — 90.2 %.
| shape (rows × contraction) | launches | weight window | µs per launch |
|---|---|---|---|
| 1024 × 2560 | 72 | 1.475 MB | 124.8 |
| 2560 × 4096 | 71 | 5.898 MB | 241.8 |
| 4096 × 2560 | 36 | 5.898 MB | 129.8 |
| 9728 × 2560 | 70 | 14.008 MB | 189.5 |
Two shapes with the same 5.898 MB of weights take 242 µs and 130 µs. That one comparison moved the whole question: the limit is not bytes.
The family streams about 2.0 GB of declared weight windows in 52.5 ms — about 38 GB/s — against a 141.2 GB/s marginal rate measured on an isolated sweep of the same silicon, and 573 GB/s for the head's own matrix multiply, which launches 37,984 threadgroups. The production shapes launch between 16 and 152 threadgroups. A threadgroup is the GPU's scheduling unit (128 threads here, running in 32-wide simdgroups); inside it, the kernel walks the contraction dimension — the k-loop — and that dependent chain, not the byte count, is what a narrow grid cannot hide.
The obvious theory, and the arithmetic that killed it
The obvious explanation for a slow chain is per-launch overhead: a fixed cost paid every time a kernel is issued. We had a candidate number, 0.1344 ms, from an isolated sweep. It does not survive the production shape.
Per compute command — the unit the engine actually submits — the measured cost is 1.43 µs at context 32 and 1.88 µs at context 952. Splitting the same 688 launches into 344 commands changed the total by less than the arm noise (ladder flat over 2 to 344 groups, slope −0.16 µs per group, R² = 0.004). The arithmetic is decisive: 688 launches at 0.1344 ms would be 92.5 ms, inside a 63 ms pass. A model with per-launch cost zero reproduces the measured native pass to 0.1–0.15 %.
What survives is an inference, not a measurement: 0.1344 ms is the latency floor of a small-workgroup matrix-multiply launch, re-exposed once per launch because the production barrier map serialises them — not a fee charged for issuing a launch. The direct test (an all-zero barrier map on the production graph) was attempted and refused by the runner, so it stays an inference.
The mechanism
MLX splits the contraction dimension when a matrix shape is too narrow to fill
the machine. Its rule is split_k = max(1, 512/(n_tiles·m_tiles)), with
n_tiles = ceil(N/32) and m_tiles = ceil(M/32), capped by the contraction
length divided by the group size. The 512 targets roughly 512 threadgroups
over the same weight bytes: more threadgroups, same bytes, shorter dependent
chain per threadgroup.
We reach it at the emission level. The same captured matrix-multiply kernel
is dispatched several times over slices of the contraction dimension
(K' = K/split_k), each with k-offset read binds and its own output slice;
the partials are summed by a tree of the captured f32 add, whose last add
writes the original output bind. No kernel is compiled, and the reduce is a
kernel we already had.
Two expressibility limits come from the artefacts, not from taste: the
contraction length must divide by split_k × 32, and the bind-offset stride
must be 4-byte aligned. For a 2560-long contraction, k = 16 is therefore not
expressible, and the runner refused it in band — "bind slot 0 offset is not
4-byte aligned" — rather than rounding it silently.
In isolation, split plus tree-reduce against the admitted kernel was bit-identical on 12 of 12 arms, and an independent row-sum oracle agreed inside its bound — worst deviation 1.63e-3 on a scale of 3.84, against a 6.5e-3 bound. On the same bytes the split launch ran 2.1–2.6× faster, and the gain ordered by threadgroup count exactly as the mechanism predicts.
| shape (rows × contraction) | grid width | best k | gain over no split |
|---|---|---|---|
| 1024 × 2560 | 16 | 8 | +57.5 % |
| 2560 × 4096 | 40 | 8 | +60.1 % |
| 2560 × 9728 | 40 | 8 | +68.4 % |
| 4096 × 2560 | 64 | 4 | +33.7 % |
| 9728 × 2560 | 152 | 2 | +19.9 % |
The narrowest shape gains most and the widest gains least. One pre-registered prediction was falsified on the way: the reduce is not free. It costs 3.9 to 28.7 % of the baseline, growing with k, and it dominates exactly where the slices gain least — which is why the widest shape peaks at k = 2.
The gate
Correctness is measured against the campaign's pre-registered criterion: the token ids the model emits, not logits. The 984-position gate compares the split arm's id sequence against the reference's.
| rung | result |
|---|---|
| 32-position gate (four shapes, 2,332 ops) | 32 / 32 |
| 984-position gate | control 983 / 984, split 983 / 984, mismatch {899} on both — pass |
| chunk pass, N=5 paired | control 63.1816 ms [63.0606, 63.4682] → split 39.0440 ms [39.0340, 39.1278] — −24.14 ms, −38.2 %, disjoint ranges |
| end-to-end prefill, N=5 | control 516.523 tok/s (1.8431 s) → split 833.559 tok/s (1.1421 s) — +61.4 %, −701 ms |
All four matrix shapes are admitted: 1024 × 2560 at k = 8, 2560 × 4096 at
k = 4, 2560 × 9728 at k = 4, 9728 × 2560 at k = 2 — the whole matmul
family, in a 2,332-operation emission. The per-shape k values are the ones the
isolated measurements established; the four-shape emission was not re-tuned as
a joint problem.
The control reproduces the previously admitted 516.71 tok/s to 0.04 % — the protocol cross-check that says the two arms differ only by the split.
How the bug was found
The symptom was strange and specific: the projections were exact and the graph was wrong. Every arithmetic hypothesis died by measurement, one at a time.
- Slice arithmetic, reduce extent, reduce push words, reduce geometry — all exonerated. The reduce was exact at the operation level on the real bytes at the real addresses, to one fp32 ulp.
- Barrier flags under insertion — excluded: every admitted operation's first derived row carried that operation's own flag, 688 of 688 identical, with zero mismatches, and no split run ended on a slice.
- The seed plan under insertion — excluded for all four implicated regions; the claim map is not index-shifted, and the only difference in seeded slots is 14 new regions.
- A real defect, and not the gate failure — the captured add is exact
through 7680 rows and wrong from 8064 rows on, and at 9728 rows it stops
adding its second input at all (
dst == src0), with the element boundary bracketed between 61,440 and 64,512 float4s. A column-slab fix is exact at the operation level, and a push word copied from another shape explained part of the residue. - A reading hypothesis that died because its form was invalid — the per-op read of the first divergent operation reported total divergence, but that read was taken in a stripped form that produces NaN beyond the first operations. The reading was withdrawn, and with it the ordering story built on it.
What survived was a range. A slice's declared weight bind kept the
captured full length. The last slice of one projection therefore declared a
window that reached past its own tensor, into the next tensor in the pool:
the following norm's 512-byte scale vector. That neighbour was zeroed, the
norm computed x · 0 · rstd = 0, and every later operation diverged — while
every projection stayed exact, because the projection was never wrong. The
symptom matched exactly what had been seen for days, and it had survived
every check, because every check was arithmetic.
The fix is one line in the derivation: a slice beyond the first declares its own weight extent, exactly as the activation bind already did. Slice 0 keeps the captured length, so the single seed that covers the whole tensor still fires.
The instrument that found it
Only reading the neighbour's bytes at its own turn could find it. That read needed an instrument the pinned binary did not have: the existing per-operation readback covers only its own subset of operations, and the end-of-pass dump reads a buffer that every layer rewrites.
So the read was built as a separate diagnostic binary: read-only, refused in fused mode, and with a layout fingerprint identical with and without the flag. Its digest is barred from producing any gate or timing number. Every measurement arm in this post ran on the pinned binary of record, unchanged.
That is the shape of the whole mistake. A replay engine must declare ranges it did not measure, and no amount of arithmetic checking can see a range that is wrong.
The arm that regressed
A negative control belongs to the result. An arm that gave each instance its own scratch region took a 25 % chunk win and regressed end-to-end to 473.51 tokens/s, against a 516.43 control, because per-instance scratch added roughly 93 MB of host re-seed per pass. The seed cost is part of the arm, and a chunk-only metric would have called it a win.
What this does not say
- TTFT is not claimed. The admitted TTFT window is wider than this replay's measurement window, and no TTFT figure is inferred from the 701 ms of prefill work that moved.
- Decode is untouched. Split-K works on the prefill pass only. The decode path does not use these matrix shapes, so the admitted decode rate stands unchanged at 62.74 tok/s, and this post claims nothing about it.
- The roughly 1000 tokens/s figure for this silicon is not a denominator. It comes from a different platform, quantisation and prompt; this arm sits at 0.83× of it and the comparison stays unmatched.
- The matched comparator is a lead, not parity. 1.59× against a vendor's Vulkan backend for the same GGUF, on the same machine, with the same prompt.
- The per-shape k assignment was not re-tuned jointly. It is the one the earlier measurements established.
Method rules earned
- An isolated A/B that dumps only the operation's own bind cannot certify extent coverage. Dump a guard band and assert confinement.
- An end-of-pass dump cannot arbitrate a buffer that every layer rewrites. Compare at the operation's own turn, or make the layer of interest the last writer of a prefix; and never compare a slice against a sum.
- Never copy a template's parameter-dependent push words to a different shape. A suspected term can be inert in one probe and decisive in another.
- A partial win is admitted by the gate; a guessed push is not.
- Use a per-shape admission rule, not a class rule. The earlier class claim — that the reduce is row-count dependent, so any shape other than 2560 rows fails — made two wrong predictions and has no remaining basis: the reduce was proven correct at 1024 and at 9728 rows, and the real defect was a property of the slice, not of the row count.
- A per-instance scratch layout can turn a chunk win into an end-to-end regression.
- A check must be adversarial — two constants that cannot be confused — and the deviation structure must be read, not only its magnitude.
- A passing arm whose evidence lives only in a temporary directory is not a result.
- When two readings of one region disagree, find out which form can answer the question before believing either.
The open frontier
The chain was streaming about 38 GB/s of weights against the 141.2 GB/s marginal rate the silicon reaches in isolation, and splitting the contraction dimension closed part of that gap. What is left is per-shape kernel efficiency, and a replay engine cannot change it: the kernel's instructions are the captured ones.
Two things are open on our side of the line. The four-shape emission was not re-tuned as a joint problem — the k values are the per-shape ones, and a joint search over them is not claimed. And the per-operation readback exists only as a separate diagnostic binary whose digest cannot be used for a gate, so the measurement path that found this class of defect is not yet the path the production arms run on.
The machinery is now in place for the same trick on the remaining shapes and families, and the failure mode it exposes — a wrong range hiding behind exact arithmetic — is the one worth watching for.
Update: the matched Metal bar
After this post was published, the same machine ran the comparison this record was missing: llama.cpp's Metal backend, four replicates, same M2 Ultra, same GGUF, same frozen 952-token prompt, same protocol — prefill 1830.53, 1834.83, 1838.65 and 1841.72 tokens/s (about 1836 at the middle of the four), decode 141.70 to 145.02 tokens/s (about 144.6), with the generated ids and first-step logits byte-identical across all four runs.
That makes the honest picture a two-bar one, not the single bar the post closed on:
| this engine | Vulkan (matched) | Metal (matched) | |
|---|---|---|---|
| prefill tok/s | 833.559 | 523.85 | ~1836 |
| decode tok/s | 123.52† | 48.68 | ~144.6 |
† 62.74 at publication; the launch-grouping arm below landed the same evening.
So the 1.59× claim against the Vulkan backend stands, and it is now the weaker of the two true statements: against Apple's own Metal backend on the same silicon this engine is at 0.45× on prefill — 2.2× behind. Decode was 0.43× at publication and is 0.85× after the same-evening grouping arm (below). The roughly-1000 tokens/s figure this post declined to use as a denominator is superseded by this measured row; the comparison it wanted to make is now matched, and it says the same thing more precisely.
At publication the larger of the two gaps was decode, and it is measured rather than guessed: of the 15.82 ms a decode pass takes on this engine, the normalisation family is 49.4 % (7.8155 ms across 1,513 launches) and the matvec family is 39.0 % — already streaming at 383 GB/s, with the contraction-loop pathology that cost prefill 61 % measured absent at six of seven shapes. Splitting the contraction dimension does not transfer to decode (the graph contains no batched matmul at all, and the arithmetic closes negative). That pre-registered arm ran the same evening. It is a push-only change — the same captured kernels, the same 1,513 workgroups, only the launch granularity moved (1,513 launches to 145) — and it passed the same 984-id gate with the mismatch set identical to the control's: decode went from 62.61 to 123.52 tokens/s (+97.3 %, ranges disjoint), and the 7.877 ms it saved is the whole measured normalisation family. The cost was launch service, not kernel work. Decode now stands at 0.85× the Metal bar, and the larger remaining gap is prefill (0.45×).
