Skip to content

Inkling 2 Mini on Metal: a prompt's projections and sliding attention on the tensor units (a 45K prompt 906 to 1,634 tok/s) - #4631

Closed
sethforprivacy wants to merge 9 commits into
ashhart:mainfrom
sethforprivacy:inkling2-mini-metal-prompt
Closed

sethforprivacy wants to merge 9 commits into
ashhart:mainfrom
sethforprivacy:inkling2-mini-metal-prompt

Conversation

@sethforprivacy

@sethforprivacy sethforprivacy commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Before: a prompt's rows run the same kernels as a round's rows, except global attention. A 256-row chunk spends half its time in the seven projections of a visit on the shared matvec, and at any real context a quarter in the sliding layers' scalar attention. After, on a GPU with tensor units, two commits:

  1. 3ebdde760: a prompt's rows take their projections on the tensor units. A 256-row chunk at 2K tokens of context goes from 251 to 163 ms.
  2. 2d7a9c90c: a prompt's rows take the sliding layers' attention on the prompt kernel that the global layers already use, a tile of 64 keys a chunk. That chunk goes to 127 ms.

A 45K-token prompt goes from 906 to 1,634 tok/s. Decode is not touched: a round's rows keep their kernels.

This is a proposal on top of #4387, opened as a draft.

The rule that keeps replies exact. In #4523 I wrote that a kernel may only replace another at every width, because choosing a kernel by a chunk's width made a reply depend on how its prompt was cut. That was too strong. What a reply needs is that every prompt row takes one kernel and every round row takes one kernel; the two may differ. Here the choice is made by the caller, never by the width: the prompt pass sets Options.prompt, and a one-row prompt chunk takes the same kernels as a 256-row one. So:

  • one prompt gives one token_sha at 64, 33, 16, 7 and 1 rows a chunk (measured on this head with a debug switch for the chunk size that is not in the diff), and a 44K-token prompt gives one at 256, 512 and 1,024 rows a chunk in a development tree with the row limits raised;
  • a resumed prompt equals a fresh one, since a kept state holds prompt rows only and what follows it is prompt rows in both;
  • drafted equals plain and concurrent equals solo, since rounds are as they were.

3ebdde760, the projections. tf_ink2_dense_units is #4523's construction for a BF16 weight: a threadgroup is 32 outputs against 16 rows, eight simdgroups each sum an eighth of the columns 16 an op, and the eight partial sums are added in order. It takes 2,304 or 3,072 columns, which are the six input projections and wo. project in forward.zig sends a prompt's rows there and a round's rows to dense, as before.

2d7a9c90c, the sliding layers. tf_ink2_attn_prompt gains a window: a row's keys are those within it, a block skips the chunks before its first row's window, and a row's partial slots count from its own window's first chunk, which is where tf_ink2_merge already reads them for a sliding layer. PromptArgs gains window (0 for a global layer, as every caller so far). On a sliding layer the prompt path uses 64-key chunks, one tile each, so a row has at most 17 slots. The decode path keeps the scalar kernel and its 32-key chunks. GPUs without tensor units keep the scalar kernel for prompts too: I could not measure the pre-M5 prompt kernel on a sliding layer, so the forward does not select it there.

Which bits change. For a prompt's rows only: the order of each projection's sum, and on sliding layers the attention's arithmetic, which becomes the prompt kernel's (P rounded to BF16 at each tile's maximum) where it was the scalar kernel's. Replies to a given prompt therefore differ from #4618's; rounds after the prompt compute as before.

Checks on this head

  • Head commit: 2d7a9c90c.
  • Lean checker, on the base and on this head: 167 and 167, 0 new.
  • Tests run:
    • zig build native -Dcpu=apple_m1: compiles.
    • The six Inkling host steps (test-inkling2-metal-host, test-inkling2-host, test-inkling2-controls-host, test-inkling2-launch-map-host, test-inkling2-metal-capabilities, compile-inkling2-native-host): exit 0.
    • tf-kernel-contract --only inkling2 (-Doptimize=safe): SUMMARY 48 kernels: 48 pass gates 1-4, 0 fail, 0 known findings; 48 controls: 48 red as planted, 0 missed; 0 limit problems, exit 0.
    • zig fmt --check build.zig zig and git diff --check: clean.
  • CI: not yet reported when this was written.
  • Not run, and why: the whole zig build test test-golden on this head (the stack's inherited failures are listed in Inkling 2 Mini on Metal: #4387's native build compiles, its norms write their output, and concurrent streams equal solo #4446). M1 to M4, where the forward selects nothing new. CUDA and HIP are not touched.

Receipt

GPU time of one 256-row prompt chunk, by where in a 44,424-token prompt it falls:

Context #4618 3ebdde760 This head
2K 251 ms 163 127
11K 267 177 141
22K 279 188 153
33K 291 202 166
44K 304 214 178
Mean of the prompt's chunks 278.5 188.7 153.2

With the attention left out a chunk takes 94 ms at every context, so what grows is the global layers' attention, and at 44K attention is again nearly half of a chunk.

Served, lane_bench.py and the other clients as in #4618:

#4618 This head
A 45,246-token prompt 906 tok/s (49.9 s to the first token) 1,634 (27.7 s)
A 6,905-token prompt beside a decoding stream 7.5 s 4.2 s
That stream's median gap while the prompt fills 275 ms 149 ms
A 56-token prompt's time to the first token 0.09 s 0.07 s
One stream, plain, code and chat 60.3 and 60.6 tok/s 60.5 and 61.1
One stream, --drafter 123.4 and 91.5 121.9 and 91.1
Four streams, --drafter 281.1 and 223.0 275.8 and 224.8

oMLX 0.8.1 does 2,628 to 2,895 tok/s on the 45K-token prompt on this machine, so the prompt path is at about 60 % of it, from a third.

  • Exactness: bench_concurrent.py --alone --serial, four streams: 12 of 12 replies equal their solo runs, plain and with the drafter, and the serial check passes (181 and 153 drafts accepted). A three-turn chat gives the same hashes with the prompt cache on and off (turns 2 and 3 resumed at 2,560 tokens). The chunk sizes are above.
  • Peak footprint: 21 GB plain, 22 GB with the drafter, as before.
  • What I tried and left out: an expert's weights decoded once a step for several bands of its pairs (slower at every chunk size, as the accumulators no longer stay in the op's registers); a fast exp2 in the prompt kernel (the exponentials are 3 ms of a 178 ms chunk). Chunks of 512 and 1,024 rows are 11 and 18 % faster again with the row limits raised, at the cost of a convolution staging buffer that grows with the chunk for every stream; that is not in this pull request.
  • Licence of any new weights or drafter, or "n/a": n/a.
Kernel contract
  • Kernels registered with the contract, and the kernel each one replaces:
    • metal inkling2 units tf_ink2_dense_units (zig/tests/contract/metal_inkling2_dense_units.zig): no replaces, as its sums take another order than sp_matvec's. It is checked against a float64 dot of the BF16 values at 1, 8 and 20 rows (20 crosses a 16-row band), at 64, 24 and 90 outputs (24 and 90 leave the last tile part full) and at both widths.
    • metal inkling2 prompt m5 sliding tf_ink2_attn_prompt and its pre-M5 twin, in the attention probe: the prompt kernel on a sliding layer of 64-key chunks against the probe's float64 attention, with a window that starts inside a tile (8 rows ending at position 1,099), one that wraps the 2,048-row ring (64 rows ending at 2,048) and one not yet full (64 rows ending at 79). The pre-M5 twin is collected only on a GPU without tensor units; the forward does not take that path there, as said above.
  • A planted control for each: the dense store doubles its sum; the sliding window is half as long. Both red at shapes.
  • Each backend's SUMMARY line and exit status: Metal, tf-kernel-contract --only inkling2, M5 Ultra: the line above, exit 0. The sliding entry's shapes are within 0.884 of the float64 bound at worst.
  • Launch records: tf_ink2_dense_units 256 threads, reserved, 16,384 bytes of threadgroup memory, offline = false. The prompt kernel's record is the one it had.
  • Chips I could not run: M1 to M4.

Checklist

@ashhart
ashhart force-pushed the lead/inkling2-mini branch 3 times, most recently from aefb063 to c41911e Compare November 24, 2026 09:43
@ashhart
ashhart deleted the branch ashhart:lead/inkling2-mini November 24, 2026 09:43
@ashhart ashhart closed this Nov 24, 2026
@ashhart ashhart reopened this Nov 24, 2026
@ashhart
ashhart changed the base branch from lead/inkling2-mini to main November 24, 2026 09:49
@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Sorry for the noise: this was closed automatically when #4387 landed on main and its base branch was removed. It is reopened and now targets main. We will follow up here on what it needs next; nothing is needed from you yet.

  • Synthena

@Spectre056

Copy link
Copy Markdown

I can run M5 Max numbers for this, a narrower M5 tier than your M5 Ultra: Apple M5 Max (40-core GPU), 128 GB, macOS build 26B131, Zig 0.17.0.

Plan: thinkingmachines/Inkling-2-Mini@ed40e7da, prompt throughput at about 4K, 16K and 44K tokens, uncached, without and with this PR, with one token_sha per prompt per arm and decode after the prompt to confirm rounds after it compute as before.

One question: should I run it on main plus this PR, or on the whole stack as your rows did (#4447 through #4649 at their heads with #4435 applied)? I'll post the table with receipts.

@sethforprivacy

Copy link
Copy Markdown
Contributor Author

@Spectre056 thank you, M5 Max rows would be useful, but I would hold off for now, for three reasons.

If the maintainers say there is a form of this they would take, I will post the base to run on here, and your numbers would be the first on an M5 Max.

@Spectre056

Copy link
Copy Markdown

Understood, I'll hold for the rebased head. For reference, I had already run this on the stack at its listed heads before your note: head 2d7a9c90 against the #4618 head ad3d31f9, with #4435's loader fix applied to both, thinkingmachines/Inkling-2-Mini@ed40e7da, uncached prompts, two passes.

prompt prompt tok/s, #4618 prompt tok/s, this head
4,060 381 1,113 (2.92x)
16,241 374 1,079 (2.89x)
44,067 357 955 (2.68x)

Short-prompt decode was level (48.3 against 48.3 tok/s chat, 49.8 against 49.9 code), and each arm gave one token_sha per prompt, different between arms as you describe. I'll rerun on whatever base you post here, and can share the receipts if useful.

@ashhart
ashhart force-pushed the inkling2-mini-metal-prompt branch from 2d7a9c9 to c375a66 Compare December 1, 2026 16:28
@ashhart

ashhart commented Dec 1, 2026

Copy link
Copy Markdown
Owner

@Spectre056, thanks for waiting. You offered on 25 Nov to rerun once this sat on main. It now does: the rebased head is c375a6624a, with #4523, #4608 and #4618 under it in the same order. A rerun on your machine would be welcome, especially with the drafter on and off. Please post the head you ran and your token and timing numbers here.

  • Synthena

@ashhart

ashhart commented Dec 1, 2026

Copy link
Copy Markdown
Owner

Thanks @sethforprivacy. #4631 is #4618 plus two commits, and both replay cleanly onto today's main. Both change what a prompt's rows compute on GPUs with tensor units. Decode rows are unchanged, and so are GPUs without tensor units.

Exactness

  • a prompt's rows take their projections on the tensor units. zig/src/families/inkling2/metal/forward.zig:149 sends every bf16 projection with k of 2,304 or 3,072 (q, k, v, rel, sink, gate and the attention output) to the new tf_ink2_dense_units kernel (experts_units.metal:224, experts.zig:389) whenever the forward runs a prompt chunk on a tensor-unit GPU. Main runs those through the register-tiled path. The new kernel sums each output in tensor-unit fragments, which is a different fp32 order. So on M5:

    • a prompt token's projections differ from main's;
    • they also differ from what the same token gets when it is decoded, since decode keeps main's path.

    That moves the KV cache a prompt writes, and every reply after it. Please put the units projections behind an opt-in setting (listed in docs/environment-names.md), or show on M5 that tf_ink2_dense_units equals the register-tiled path bit for bit.

  • a prompt's rows take the sliding layers' attention on the prompt kernel, a tile of keys a chunk. On tensor-unit GPUs, forward_attention.zig:166-168 (with attention_plan.zig:84) routes a prompt's sliding-layer attention to tf_ink2_attn_prompt with 64-key chunks. Main uses the sliding-prompt or decode kernels, with 256-key chunks. Each chunk restarts the online softmax, and the tiled kernel orders its sums differently, so M5 prompt attention moves too. Same ask: opt-in, or shown bit-equal to main's path.

  • This builds on the chunk sizes Inkling 2 Mini on Metal: routed experts on the M5 tensor units at every width (16 rows 49 to 36 ms, 64 rows 138 to 84 ms) #4523 changed, and on its tensor-unit experts. Those asks stay on Inkling 2 Mini on Metal: routed experts on the M5 tensor units at every width (16 rows 49 to 36 ms, 64 rows 138 to 84 ms) #4523.

Tests

  • metal_inkling2_dense_units.zig checks the projections against an FP64 reference, with a planted control (dense_store).
  • The attention contract gains sliding prompt shapes on both kernel variants, with the window check planted.

Both are good correctness checks, but neither compares with main's bits. Note also that the pre-M5 sliding prompt variant in the contract tests a configuration the server never takes: the tiled route requires tensor units.

Repo checks we ran on the rebased branch:

  • zig fmt and ast-check are clean.
  • The lean check is unchanged.
  • No Zig file is over 600 lines.
  • No new environment variables, and no serving pins touched.

The M5 check we'd like to see, main against this branch at default settings:

  • Greedy tokens and logits after 1K and 8K prompts, compared bit for bit.

  • Prompt tokens per second at those lengths.

  • One prompt filled two ways, in one pass and by decoding it a token at a time: the KV rows each writes should be equal.

  • Synthena

@sethforprivacy

Copy link
Copy Markdown
Contributor Author

Thanks for the rebase and the careful review, @ashhart. I'm closing this one rather than putting it behind a switch.

The tensor-unit sums don't match main's bits, and I measured that already on 23 Nov (issuecomment-7107950724). The experts changed 19 of 24 greedy replies, a prompt's projections and sliding attention changed 20, and the logits changed 9,182 of 51 million BF16 values, though none of the 24 replies. A default-off path that only M5 takes, and whose replies differ from the default, isn't worth the contract and review load it would add to the repo.

What happens to the rest:

@Spectre056: since this PR is closed, a rerun of it won't be needed. Thanks for offering.

@Spectre056

Copy link
Copy Markdown

Thanks, Ash. I reran the requested head c375a6624afc1ad80d02733ff9250c9a34ceae59 against the rebased #4618 baseline cb094c50923774308b73b4e43f8914554ad7a6c3 on an M5 Max with 40 GPU cores, 128 GiB, and macOS build 26B131.

Both builds were unpatched and used thinkingmachines/Inkling-2-Mini@ed40e7da625f594fc9c4990f5f66be24873c22d7 with bundled DSpark, one active stream, --parallel 4, greedy seed 0, and the prompt cache off. Figures are medians of two timed runs per long prompt and three per short prompt, per build and drafting mode. Long runs generated 160 tokens and short runs generated 256.

Prompt Prefill off, baseline → head (tok/s) Head TTFT off / on (s) Head decode off / on (tok/s) Head token SHA-256 prefix, both modes
4,058 tokens 780.50 → 1,056.56 3.842 / 3.951 32.11 / 49.39 2bf89590c39d
16,239 tokens 746.84 → 1,006.12 16.143 / 16.273 28.79 / 48.12 8b43520dc1af
44,065 tokens 672.47 → 877.07 50.244 / 50.508 25.17 / 39.48 52fc608e1728
Short chat — 0.283 / 0.351 35.57 / 39.61 3b9cd4f73912
Short code — 0.288 / 0.359 35.57 / 64.89 2438cdc5e912

At 44K, prefill improved 30.4% with drafting off and 30.3% with drafting on. Baseline TTFT there was 65.535 s off and 65.825 s on. With DSpark, short code wall time on the head went from 7.453 s to 4.288 s.

Draft-on and draft-off emitted identical token arrays in all 12 pairs on each build. Baseline-to-head token arrays differed for all five prompts in both modes, consistent with the prompt arithmetic change described here. Draft-off recorded zero proposals. On the head, the draft-on counters recorded 1,478 accepted out of 3,917 proposals, which can include copy proposals.

An SSH disconnect interrupted the baseline run. I kept its 11 completed measurements and collected the remaining 13 after fresh warm-ups. I have the raw per-request timings, full token hashes, and build provenance available if useful.

@Spectre056

Copy link
Copy Markdown

@sethforprivacy No worries. I didn't see your post and closure before running, so its there anyways for posterity, I suppose. :)

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