Skip to content

Inkling 2 Mini on Metal: the prompt kernel keeps one set of sums in registers and skips the bias on far tiles (prefill 2,215 to 2,376 tok/s at 45K, same bits) - #4666

Draft
sethforprivacy wants to merge 46 commits into
ashhart:mainfrom
sethforprivacy:inkling2-mini-metal-trims
Draft

sethforprivacy wants to merge 46 commits into
ashhart:mainfrom
sethforprivacy:inkling2-mini-metal-trims

Conversation

@sethforprivacy

@sethforprivacy sethforprivacy commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Three changes to tf_ink2_attn_prompt, none of which changes a sum. A 1,024-row chunk of a 45K-token prompt goes from 442 to 412 ms of GPU time on average, and from 504 to 459 at the prompt's end; the prompt is 2,376 tok/s served where it was 2,215.

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

1. One set of sums in registers (442 to 412 ms with the other two in place; the largest of the three). The kernel kept a chunk's folded sums (out, 64 FP32 values a lane) beside the running ones (acc) through the whole tile loop, though it touches them once every 16 tiles. They now live where they end up, in the chunk's partial slot: the chunk's first fold stores 0 * x + acc * y, a later fold reads the slot, folds and stores, and the copy at the end is gone. A lane writes only its own elements. The peak and the mass (four values) stay in registers, since a row's lanes share them.

2. No bias read and no key test on a far tile (435 to 430). On a global layer, a tile whose every key is 1,024 or more positions before both of a lane's rows has every key live and no bias, so the 32 table reads and the four tests a key are skipped. The score is still (score * scale + 0.0f) * LOG2E.

3. A query row four values a load (442 to 435). On the M5 layout a fragment's eight values are four columns of two rows, so two bfloat4 loads replace eight single ones a step, as #4645 did for the keys and values.

Why I think the registers matter this much. Measured on the way, and left out: loading the query fragments once a chunk instead of once a tile (64 more BF16 values live in a lane) made a chunk 47 ms slower, and hoisting the element loop's per-row arithmetic into two small arrays made it 24 ms slower. Less live state is the direction that helps this kernel; more does not, even when it saves work.

Which bits change. None. Each change keeps the expression it replaces (the first fold multiplies the same zero; the far tile adds the same zero), and every reply below has the hash it had on #4649, including at --context 262144, where a chunk is 4,096 keys and folds four times.

Checks on this head

  • Head commit: 232ee08a3.
  • 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 52 kernels: 52 pass gates 1-4, 0 fail, 0 known findings; 52 controls: 52 red as planted, 0 missed; 0 limit problems, exit 0. The prompt kernel's two M5 entries land where they did on Inkling 2 Mini on Metal: wide prompt chunks take the routed experts on turned tiles, 16 weight rows by 32 of an expert's pairs an op #4649 (worst 0.141 and 0.884 of the bound), and their controls stay red.
    • zig fmt --check build.zig zig and git diff --check: clean.
  • CI: not yet reported when this was written.
  • What the contract reaches and what it does not: its global shape of 1,100 keys has one far tile (keys 0 to 63) and the rest near, so both score paths run. Its chunks are single folds, so the fold into a stored partial (a chunk of more than 1,024 keys, which a window over 65,536 gives) is not reached by it, as the register fold it replaces was not. That path is checked by the served hashes at --context 131072 and 262144 below.
  • 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). CUDA and HIP are not touched.
  • Run by BlivionIaG on an M4 Pro without tensor units (comments below), which I cannot do: changes 1 and 2 do not depend on the fragment layout and so reach the pre-M5 build too; change 3 is M5 only. tf-kernel-contract --only inkling2 there gives 42 of 42 kernels and 42 controls red, with both pre-M5 prompt entries passing all four gates, and four served replies have the same hashes without and with this commit at --context 131072 and 262144, which includes the fold into a stored partial.

Receipt

GPU time of a 1,024-row chunk over a 45K-token prompt, --context 65536 (a development tree that is this stack with timing switches; each row adds one change):

Mean At the prompt's end (44K of context)
#4649 442.3 ms 504
Query rows by four-value loads 435.1 492
and far tiles 429.6 480
and one set of sums 411.8 459

Served, one stream, 160 new tokens after a prompt of the given length, --parallel 1 --context 262144:

Prompt Plain, before Plain, this head --drafter, before --drafter, this head
8,436 tokens 54.2 tok/s 55.1 96.6 98.1
44,436 52.3 53.0 84.9 86.2
92,436 46.0 46.9 82.3 86.7

Served, --parallel 4 --prompt-cache-gib 0, TensorFold's clients (256 tokens, greedy, three reps) and lane_bench.py:

Before This head
A 45,246-token prompt 2,215 tok/s (20.4 s to the first token) 2,376 (19.0 s)
A 6,905-token prompt beside a decoding stream 4.05 s 3.89 s
One stream, plain, code and chat (short prompts) 60.6 and 61.2 tok/s 61.0 and 61.6
One stream, --drafter 124.7 and 93.2 125.3 and 96.8
Four streams, --drafter 278.6 and 227.5 279.6 and 228.6
  • Exactness: the six replies above have the hashes they had before, at --context 262144 and at 131072. 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. The concurrency check's four replies and a three-turn chat with the prompt cache on and off have the hashes they had before.
  • oMLX 0.8.1 does 2,628 to 2,895 tok/s on the 45K-token prompt on this machine.
  • Licence of any new weights or drafter, or "n/a": n/a.

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.
@BlivionIaG

Copy link
Copy Markdown
Contributor

@sethforprivacy the pre-M5 run you asked for on #4387: tf-kernel-contract --only inkling2 on this PR's head 232ee08a39, Apple M4 Pro (applegpu_g16, tensor units false), 48 GiB, macOS 27.2, Zig 0.17.0, -Dcpu=apple_m1 -Doptimize=safe.

SUMMARY 42 kernels: 42 pass gates 1-4, 0 fail, 0 known findings; 42 controls: 42 red as planted, 0 missed; 0 limit problems, exit 0.

The two pre-M5 prompt entries are collected and pass all four gates:

  • inkling2 prompt pre-m5 tf_ink2_attn_prompt: five runs byte-identical, 2 shapes within the float64 bound (worst 0.377 of it), sums within the bound at k 1100.
  • inkling2 prompt pre-m5 sliding tf_ink2_attn_prompt: 3 shapes (worst 0.736 of the bound), sums within the bound at k 2049.
  • Their planted controls are red (first key bias boosted at 673x its bound, sliding window half as long gives nan).

42 kernels rather than your 52 is the count without the M5-only entries on this chip. The tf_ctl_big_tg limit control prints a 36,864-byte threadgroup error and is counted red as planted.

Not run: the served hashes (the cold-prompt and decode rows on this stack are queued behind my other GPU jobs and will follow on #4387). I also did not run the fold-into-stored-partial path your description says the contract does not reach.

@sethforprivacy

Copy link
Copy Markdown
Contributor Author

Thank you, this is the first time the pre-M5 prompt entries have run on a real Mac without tensor units: until now I only had the offline build for M1 to M4. Both entries passing all four gates with their controls red on an M4 Pro at 232ee08a3 covers changes 1 and 2 of this pull request on that build, which the description lists as not run.

The fold into a stored partial is not reached by the contract on any chip; on the M5 Ultra it is checked by the served hashes at --context 131072 and 262144. If your served rows on #4387 include a window over 65,536, their hashes would check it on the pre-M5 build too.

@BlivionIaG

Copy link
Copy Markdown
Contributor

@sethforprivacy the served hashes on the pre-M5 build, as asked. Same Mac as above (M4 Pro, no tensor units), the stack at this PR's head 232ee08a39 (B) against the same tree with prompt_attention.metal at 84ab2c63fa, i.e. without this commit (A); both -Doptimize=ReleaseFast, plus #4435's 1 GiB pread cap applied locally to load the checkpoint, serve --parallel 1, greedy chat, each prompt after a fresh server start.

--context A B
131072 18b0f0cf5cf3 77bf7c99cc66 ee856132f966 47029b25d5c5 identical, all four
262144 18b0f0cf5cf3 77bf7c99cc66 ee856132f966 47029b25d5c5 identical, all four

The prompts: a 28-token question (16 tokens out), a 416-token prompt (16 out), a 1,916-token prompt (16 out) and a 4,200-token prompt (8 out). So changes 1 and 2 reproduce on the pre-M5 build at both windows. What this does not reach: no prompt here is long enough to fill a 4,096-key chunk several times (the longest is about 4,200 tokens), so the multi-fold path at 262,144 is exercised by one chunk boundary at most; a 16K or longer prompt would be the real check, which I have not run.

One observation: the 416-token prompt's hash 77bf7c99cc66 is the same one #4337's Metal path gives for that prompt and 16 tokens ("Explain how a residual stream mixes attention outputs. " repeated 40 times, greedy), so the two independent implementations agree on that reply on this Mac.

@sethforprivacy

Copy link
Copy Markdown
Contributor Author

Thank you. Identical hashes without and with the commit, at both windows, is what I hoped to see on that build.

It reaches more than you credit it with. A chunk is the fewest keys that fit the capacity in 64 slots (globalChunk): 2,048 keys at --context 131072 and 4,096 at 262144. Inside a chunk the kernel folds every 1,024 keys: the first fold stores into the partial slot and each later one folds into what is stored, which is the path change 1 rewrote. So your 1,916-token prompt at 131,072 folds once into a stored partial, and your 4,200-token prompt does it in two chunks there and three times in the first chunk at 262,144. A longer prompt adds chunks, not a new path.

So that path is now checked on the pre-M5 build, which I could not do. I have added your two runs to the description.

Two independent implementations giving 77bf7c99cc66 for the same prompt is a good sign for both.

@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

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