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

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

sethforprivacy wants to merge 41 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 and others added 30 commits November 21, 2026 16:05
Derive per-visit cache access and exact arithmetic for the 48 visits so CUDA admission and kernels have a reference before serving is enabled.
Decode stored NVFP4 and MXFP8 expert weights to BF16 and launch them beside the grouped expert/vision kernels, with exact tensor baselines and planted faults.
Launch cross-pass messages, rank-64 gates, residual steps and split QKV with FP32 reductions, BF16 rounding controls and per-launch resource records.
Run all 48 visits with per-visit cache state, prompt snapshots and exact rollback. Refuse TP, vision and DSpark loading until those engines exist.
@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.

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