Skip to content

Inkling 2 Mini on Metal: prompt experts, projections and loop gates on simdgroup matrices without tensor units (stacked on #4618) - #4756

Open
BlivionIaG wants to merge 11 commits into
ashhart:mainfrom
BlivionIaG:port/prefill-on-4618
Open

BlivionIaG wants to merge 11 commits into
ashhart:mainfrom
BlivionIaG:port/prefill-on-4618

Conversation

@BlivionIaG

@BlivionIaG BlivionIaG commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

On a Mac without tensor units (M1 to M4), Inkling 2 Mini's cold prompt runs its routed experts, attention projections and loop gates on 8x8 simdgroup matrices, with every sum in the order of the kernel it replaces, so a cold 2,048-token prompt takes about 9.8 s on an M4 Pro instead of 151 s on main. The head also carries three changes that are not gated on the chip (listed under "Scope"): 256-row prompt chunks, one attention-residual launch for a chunk's rows, and a combine that sorts a token's routes once. No sum changes.

Scope: what reaches M5 and other tensor-unit chips

Chip-gated, loaded only where device.tensorUnits() is false (the pipelines are null otherwise and forward.zig falls back to main's path): dense_mma.metal, experts_mma.metal, loop_mma.metal and their dispatch.

Not gated, so they run on M5 as well, and on decode as well where noted:

  • state.zig: prompt_rows and max_rows 64 to 256, so the prompt chunk, the prefill step and the prompt cache grid (prompt_rows) are 256 on every chip; sliding_ring 1,088 to 1,280. runtime_plan.zig, glue_plan.zig, loop_plan.zig and loop.zig accept up to 256 rows.
  • tf_inkling2_ar (glue.metal) takes a row count and strides; forward.zig launches it once for a chunk's rows (residual, the two prepare roles and the final head). Decode windows (16 rows or fewer) use it too.
  • tf_ink2_combine (experts.metal) sorts a token's 8 routes by expert id once; decode uses it too. experts.zig refuses topk above 8.
  • The tensor-unit kernels on M5 (prompt_attention, the experts and loop kernels of main) are unchanged but now see chunks of up to 256 rows; their 256-row behaviour is measured on an M5 Ultra in the receipt below (token ids equal to main's in every cell, including the 256-row seams).

The only effect of the units that I changed is the memory under "Memory": every chip pays it.

Dense tail guard (review of this pull request)

tf_ink2_dense_mma loaded whole 8-row input tiles, so 45 rows read through row 47. A tile that is not whole now reads the last row for its rows past rows (they only feed outputs that are never stored); whole tiles load as before and every sum is unchanged. The other kernels of the slice do not read past rows: the experts MMA clamps each pair's row (min(xp, live - 1)) and its weights are whole tiles by construction, and the gates MMA zero-fills and bounds-checks its staging.

dense_tail_test.zig (in test-inkling2-metal-host) runs 1, 7, 9, 45 and 255 rows with the input followed by NaN and every tile row stored: the tail rows repeat the last row's bytes and the valid rows equal the old loads. The old loads, kept as the planted control, put NaN in the tail rows (checked in the same test). On the previous head the same probe gives NaN in 112, 16, 112, 48 and 16 tail outputs for 1, 7, 9, 45, 255 rows and 0 on this head. metal_inkling2_dense_guard.zig (zig build test-inkling2-dense-guard, run under Metal's shader validation with abort on fault) ends the projection's input at an unmapped page with no slack, binds it zero-copy and runs 1, 7, 8, 9, 16, 17, 45, 46 and 47 rows: the clamped tail passes and every output byte equals the chunked matvec reference's; with the previous kernel the same test aborts (SIGABRT, invalid load past the buffer). My first version of this paragraph said a read past a buffer does not fault on this Mac; that was wrong for an input that ends at an unmapped page, which is what this test builds.

Checks on this head

  • Head commit: 3d22cd3a91 (merge of main 3e1c4c582e and the admission test's 256-row values).
  • Lean checker on main and on this head: 165 and 165, 0 new. tools/pr_check.py --base 3e1c4c582e --pr 4756: 0 errors, 0 warnings. zig fmt --check build.zig zig clean.
  • Tests (M4 Pro, -Dcpu=apple_m1): test-inkling2-metal-host 102 pass, 6 skip; test-inkling2-host 45 pass, 4 skip; test-inkling2-dense-guard passes; tf-kernel-contract --only inkling2: 67 kernels pass gates 1-4, 74 controls red as planted, 0 missed, 0 limit problems. Exactness on the M4 Pro against main 3e1c4c582e: cold 255/256/257/512/513, chat 8/16/24, greedy 256, seeded 64 and 4 concurrent equal to solo, 14 cells, full token ids and sha256 equal.
  • CI: not run.
  • Not run: M1 to M3; CUDA; decode speed; drafted output; --parallel above 4 (the M5 cells below ran at --parallel 1).

Receipt (M4 Pro 48 GiB, macOS 27.2, Zig 0.17.0, -Dcpu=apple_m1 -Doptimize=ReleaseFast, thinkingmachines/Inkling-2-Mini@ed40e7da625f594fc9c4990f5f66be24873c22d7, --no-drafts --prompt-cache-gib 0 --no-thinking, load average 2 to 5, memory pressure level 1)

Exactness, greedy token_sha (the server's sha=), main 9edf2f89d4 against this head applied on it. Main's chat hashes changed since the earlier receipt (eea0560bb4 sends reasoning_effort "none" to the template, so the first prompt is 26 tokens, not 28). On the old main base the head gives 0b037128ee78 b37d8a0e39c4 146f8038bfc7 870a47079391, the earlier main's.

Cell (--context 8192 unless noted) main head equal
chat 26 tokens 8 out / 26 tokens 64 out / 414 tokens 16 out / 1,036 tokens 24 out 7781f06a3160 33a29a1cc9d2 753dc475f8b6 83614a01d8ae same yes
cold prompts of 255, 256, 257, 511, 513, 1,000 tokens (chunk seams at 64 and 256) 8b9d4d0e236b 598209d1dbf8 4d0198f4905a de3c13467508 b53cd22fd678 598209d1dbf8 same yes
cold 2,048 de3c13467508 de3c13467508 yes
cold 8,192 (--context 16384) dc7ec8d3a016 dc7ec8d3a016 yes
second turn resumed from the prompt cache (--prompt-cache-gib 2), turns 1 and 2 2d70cf5fff15 168a74b163c9 2d70cf5fff15 168a74b163c9 yes

The resumed turn reuses 576 cached tokens on main and 512 on the head: the cache grid is the chunk width, so up to 255 more tokens are prefilled again. No memory pressure: warning in the head's logs; main's first run logged one transient warning, which did not change a greedy hash.

Cold first token, 2,048 tokens, A-B-B-A (A = 8e6b4e5de7, the pull request's earlier head; B = this head's tree, both on the same base): A 22.37 s and 22.33 s, B 9.72 s and 9.82 s. On 9edf2f89d4 main takes 151.4 s at 2,048 tokens and 642.9 s at 8,192; the head applied on it takes 9.75 s and 39.7 s (one run each).

Memory (/usr/bin/time -l on the server after load, no request; main 9edf2f89d4 against the head applied on it):

Run main footprint / max RSS head footprint / max RSS
--context 8192 --parallel 1 20.34 GB / 17.88 GB 20.78 GB / 16.90 GB
--context 8192 --parallel 4 20.34 GB / 15.85 GB 20.78 GB / 16.83 GB
--context 131072 --parallel 1 20.34 GB / 16.44 GB 20.78 GB / 17.14 GB

The footprint grows by 0.45 GB, the scratch, and does not move with the context or the streams, because a stream's cache is allocated when it starts and its new pages are untouched until used (after a cold 8,192 prompt the footprints are 20.64 GB and 21.06 GB). Max RSS is dominated by page cache of the mapped checkpoint and varies by 1 GB between identical runs here, so I give it for reference only. The server's own budget line falls from 15.6 to 15.2 GiB for stream and prompt state at every setting.

Allocated bytes, from state.zig:

  • Model scratch, once: scratchBytes(32) + scratchBytes(prompt_rows), 2,336,868 bytes a row plus 768, of which logits_all is 800,232 a row, part_o 786,432 and messages 442,368. Main 224,340,864 bytes (214 MiB); head 673,019,520 (642 MiB): +448,678,656 bytes (428 MiB).
  • Per stream, cacheBytes(capacity): 40 * sliding_ring * 2 * 2048 + 8 * align64(capacity) * 2 * 2048 + 2 * conv_bytes + 48 * (3 + max_rows) * 13,312 + 4 * 200,058. Growing sliding_ring by 192 adds 31,457,280 bytes and max_rows by 192 adds 122,683,392 in the taps: +154,140,672 bytes (147 MiB) a stream, independent of the context. That is 471 to 618 MiB a stream at 8,192 and 4,311 to 4,458 MiB at 131,072.
  • Added at --parallel 1: 428 + 147 = 575 MiB; at --parallel 4: 428 + 588 = 1,016 MiB; the same at both contexts.

Receipt: M5 Ultra (256 GiB, macOS 27.0, tensor units), main 9a38b1f7b3 against this head 5a614aaf3b, same checkpoint

One cold server per cell, --prompt-cache-gib 0 --parallel 1 --no-drafts --no-thinking, A-B-B-A; the tensor-unit kernels stay, and the shared 256-row chunk, batched residual and combine changes apply.

Exactness, 10 cells: the first token id, its logprob and token_sha are equal on main and on the head.

case token id token_sha first logprob
255 151530 b53cd22fd678 -3.1523818969726562
256 151609 8b9d4d0e236b -3.1544952392578125
257 (head chunks 256 + 1, main 64 x 4 + 1) 151530 b53cd22fd678 -2.86322021484375
512 151609 8b9d4d0e236b -3.0887451171875
513 (head 256 + 256 + 1, main 64 x 8 + 1) 151609 8b9d4d0e236b -3.2803916931152344
2,048 153665 de3c13467508 -3.22320556640625
8,192 (head 32 x 256, main 128 x 64) 189081 8a44904f45d6 -3.114013671875
chat, 8 / 16 / 24 out 4c2031fb1535 3a974c335a4e 0e69856242e3

The M4 and M5 first tokens differ from each other on the same prompt (255: 150019 on the M4 Pro, 151530 on the M5 Ultra): the chips' arithmetic differs, so the comparison is main against head on each chip.

Cold first token (prefill seconds, server ttft), A-B-B-A. Block 1 ran with no other server on the machine; block 2 started after another job's server had been up for a short time. The first head run (B1) includes a one-off Metal pipeline compile of about 5 s that main's cached pipelines did not pay, so read B2 for small prompts.

case main A1 head B1 head B2 main A2
255 7.84 11.41 5.99 7.85
256 7.87 10.56 5.88 7.89
257 9.04 14.31 7.28 9.04
512 15.74 23.54 11.47 15.75
513 16.93 26.22 13.22 16.92
2,048 68.37 41.08 47.66 69.69
8,192 436.59 154.31 171.37 cancelled
chat prefill, 8 / 16 / 24 out 4.10 / 4.21 / 4.13 1.24 / 1.23 / 1.23 1.24 / 1.23 / 1.25

The A2 8,192 run was cancelled when another job's server start-up terminated mine mid-request; it is not used. B2 differs from B1 by 11 % at 8,192.

Memory: peak RSS 18.49 GiB on main and 18.50 GiB on the head (both repeats); peak phys_footprint 19 GB on both. The 575 MiB the scratch and stream caches add at --parallel 1 (above) is below this sampler's resolution on the M5.

Checklist

  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.
  • No precision traded for speed; no sum changes (above).
  • Decode, drafted output and concurrent equal solo: not run on this head.
  • M5 Ultra: token-equal to main in every cell above, memory unchanged within the sampler; decode, drafted output and --parallel above 1 on the M5 not run.

@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

… on simdgroup matrices without tensor units

- Experts, the attention projections and the loop gates of a chunk take simdgroup-matrix kernels where the GPU has no tensor units; each output is bit-equal to the kernel it replaces.
- The contract registers the new kernels with a planted control each.
@BlivionIaG
BlivionIaG marked this pull request as ready for review November 24, 2026 12:02
- A prompt pass reads every expert's and projection's weights once, so 256 rows a pass read them a quarter as often as 64 and fill the expert tiles.
- Every row's sums are the same at every chunk width (the routed experts, projections and gates are bit-equal to the one-row kernels); the sliding ring grows to 1,280 keys.
- The loop-gates contract probe also runs 200 rows.
- Same change as the chunk width of ashhart#4608 (a9fae61), which has no effect on a chip without tensor units until a grouped simdgroup-matrix path like ashhart#4756's is under it.
- Cold 2,048 tokens on an M4 Pro, A-B-B-A: 22.76 and 22.69 s before, 15.53 and 15.52 s after; greedy chat hashes, cold 2,048 (de3c13467508) and 8,192 (dc7ec8d3a016) tokens equal to main's.
- tf_inkling2_ar takes a row count and the strides between rows' blocks, partial sums and outputs, and a threadgroup runs one row's unchanged arithmetic; a visit launched it once a row and role (about 580 launches at 256 rows, 23 us each) and now launches it once a role.
- The contract probe runs a batch of two rows with their own blocks, partial sums and two queries each against the float64 oracle and the planted control.
- Cold 2,048 tokens on an M4 Pro, A-B-B-A: 15.40 and 15.80 s before, 10.84 and 11.45 s after; greedy chat hashes, cold 2,048 and 8,192 tokens equal to main's.
- tf_ink2_combine found each route by scanning every expert index for each output column (1,024 compares a sum); it now sorts the token's eight route slots by expert id once, equal ids in slot order, and adds them in that order, the same sum as before.
- The route-slot-order control now removes the sort.
- Cold 2,048 tokens on an M4 Pro, A-B-B-A: 11.78 and 11.84 s before, 9.90 and 9.46 s after; greedy chat hashes, cold 2,048 and 8,192 tokens equal to main's.
… one entry for ashhart#4756

- One bullet covers the simdgroup-matrix kernels, the 256-row chunks, the one-launch attention residual and the sorted combine.
@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks @BlivionIaG. The scalar replacement checks and cancellation controls are useful, and the current head merges cleanly with main.

The dense-MMA tail needs a read guard or an explicit padded-input contract. It loads whole eight-row tiles: the 45-row case reads through row 47, while its fixture allocates only 45 rows plus guards. Guarding the output store does not guard those reads.

The head also has the later 256-row chunks, batched residuals and combine change. Those shared changes reach M5 too and increase cache/scratch memory, so the older “M5 untouched” receipt needs updating for the actual scope. Please retain exact token and seam comparisons alongside the memory and timing results after the tail fix.

  • Synthena

…ds no row past the input

- A row tile that is not whole read its rows through the next multiple of eight; those rows now read the last row, which only feeds outputs that are never stored, and whole tiles load as before.
- A host test runs 1, 7, 9, 45 and 255 rows with the input followed by NaN: with every tile row stored, the tail rows repeat the last row's bytes, and the old loads (the planted control) put NaN there.
- The kernel contract's dense shapes stay bit-equal to the matvec kernels and its controls stay red.
@BlivionIaG

Copy link
Copy Markdown
Contributor Author

Thank you, @ashhart. The head is now c50166ec60 (one commit on top: the guard), and the body is rewritten for the actual scope.

Tail. tf_ink2_dense_mma now reads the last row for tile rows past rows; whole tiles load as before, so every sum is unchanged. The other two MMA kernels already bound their reads (the experts kernel clamps min(xp, live - 1), the gates kernel zero-fills and checks its staging). A new host test, dense_tail_test.zig, runs 1, 7, 9, 45 and 255 rows with the input allocated exactly that long and NaN after it, stores every tile row, and asserts the tail rows repeat the last row and the valid rows equal the old loads; a planted control with the old loads fails it. On the old kernel a standalone probe gives 112, 16, 112, 48 and 16 NaN tail outputs for those row counts and 0 on the new one. A read past a buffer does not fault on this Mac (also with shader validation on), so the stored tail rows are the witness, not a fault.

Scope. The body now lists what reaches M5: not gated are prompt_rows/max_rows 64 to 256 and sliding_ring 1,280 in state.zig (chunk, prefill step and cache grid are 256 on every chip), the row limits in loop.zig and the three plan files, the batched tf_inkling2_ar and the tf_ink2_combine route sort (both also used by decode). Gated by !tensorUnits(): dense_mma, experts_mma and loop_mma. M5 was not run, so the tensor-unit kernels' behaviour at chunks of up to 256 rows is untested by me.

Memory. Per-model scratch goes from 214 MiB to 642 MiB (+428 MiB); each stream's state grows by 147 MiB (31,457,280 B sliding ring, 122,683,392 B taps), independent of context. Added: 575 MiB at --parallel 1, 1,016 MiB at --parallel 4. Peak footprint after load, /usr/bin/time -l: 20.34 GB on main and 20.78 GB on the head in all three configurations I ran, 20.64 and 21.06 GB after a cold 8,192-token prompt.

Equality. Cold 255, 256, 257, 511, 513, 1,000, 2,048 and 8,192 tokens all equal main's; chat hashes equal main's on both the old and the current base (7781f06a3160 33a29a1cc9d2 753dc475f8b6 83614a01d8ae at 9edf2f89d4). A resumed second turn has equal hashes, and one behaviour change you should know: it restores 512 cached tokens on the head where main restores 576, because the cache grid is now 256, so up to 255 more tokens are prefilled again. Cold 2,048 tokens, A-B-B-A against the 8e6b4e5de7 build: 22.37 and 22.33 s before, 9.72 and 9.82 s after; main 151.4 s and the head 9.75 s at 2,048, 642.9 s and 39.7 s at 8,192 (one run each, load average 2 to 5). Contract: 47 kernels, 49 controls red; pr_check 0 and 0.

…tail

- A focused test ends the projection's input at an unmapped page with no slack, binds it zero-copy, and runs under Metal's shader validation, which is the only layer that reports a read past a buffer's end.
- The old loads abort there at rows=1 (invalid load at offset 16272 of 16384); the clamped tail reads nothing past the row bound at 1, 7, 8, 9, 16, 17, 45, 46 and 47 rows, and every output byte equals the chunked matvec reference's.
- The dense-MMA contract fixture's matvec reference takes `dense`'s exact chunk ladder (32, 16, 8, 4, 2, 1) instead of a 13-row tail in a 16-row tile.
@BlivionIaG

Copy link
Copy Markdown
Contributor Author

A correction to my last comment: I wrote that a GPU read past a buffer does not fault on this Mac, shader validation included. That was wrong for an input that ends at an unmapped page. The head now carries metal_inkling2_dense_guard.zig (zig build test-inkling2-dense-guard, Metal shader validation with abort on fault): the input ends at a guard page with no slack, is bound zero-copy, and 1, 7, 8, 9, 16, 17, 45, 46 and 47 rows run. I ran it on this head on the M4 Pro: 1 of 1 tests pass. With the previous dense_mma.metal swapped back in, the same test aborts (SIGABRT, terminated with signal ABRT, an invalid load past the buffer); I put the new kernel back afterwards. The stored-tail-rows test from before stays. The body says the same.

@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks @BlivionIaG. The partial input tile now reads only valid rows, and the no-slack guard-page test with the old-kernel failure answers the read-safety request. The corrected M5 scope, memory costs and new M4 equality/timing results are credited.

No further tail fix is requested. The remaining qualification covers the shared changes on M5 and current decode, drafting, concurrency and warm-cache behavior, including the coarser cache grid and its extra prefill work. The cold speedup alone does not cover those cells.

  • Synthena

@BlivionIaG

Copy link
Copy Markdown
Contributor Author

Thanks @ashhart. Both points are in 5a614aaf3b (the body is updated).

Dense tail. The guard is the read clamp in tf_ink2_dense_mma (a tile's rows past rows read the last row, so no load leaves the input; in-range rows and their sums are unchanged), and metal_inkling2_dense_guard.zig now proves it with no slack: the input ends at an unmapped page, is bound zero-copy, and runs 1, 7, 8, 9, 16, 17, 45, 46 and 47 rows under Metal's shader validation with abort on fault. The old loads fail that fixture; the guard passes it.

Scope on M5. The "M5 untouched" statement was wrong: the 256-row chunks, the batched residual and the combine's single sort reach every chip. I ran main 9a38b1f7b3 against this head on an M5 Ultra (A-B-B-A, one cold server per cell, --prompt-cache-gib 0 --parallel 1 --no-drafts):

  • Exactness: first token id, logprob and token_sha equal in all ten cells, including the 256-row seams (257, 513, 8,192 = 32 x 256 against main's 128 x 64) and the chat prompts.
  • Prefill: 2,048 tokens 68.4 s to 41.1 s, 8,192 tokens 436.6 s to 154.3 s, 30-token chat 4.1 s to 1.24 s; small prompts are level once the pipeline cache is warm (255 tokens: 7.85 s on main, 5.99 s on the head; the first head run paid a one-off compile).
  • Memory: peak RSS 18.49 GiB on main and 18.50 GiB on the head, phys_footprint 19 GB on both; the +575 MiB computed from state.zig is below the sampler's resolution.
  • One main cell (A2 at 8,192) was cancelled by another job's server start-up and is not used.

Not run on the M5: decode, drafted output, --parallel above 1.

# Conflicts:
#	zig/kernels/metal/inkling2/loop_sources.zig
#	zig/src/families/inkling2/metal/forward.zig
#	zig/src/families/inkling2/metal/loop.zig
#	zig/src/families/inkling2/metal/metal.zig
#	zig/tests/contract/metal_inkling2_main.zig
…atch

The merged admission test measured 64-row scratch; 256-row chunks leave 6.19 and 15.19 GiB beside the weights on 36 and 48 GiB Macs, 10 and 25 streams at an 8,192 window.
@BlivionIaG

Copy link
Copy Markdown
Contributor Author

Merged main 3e1c4c582e (#4912). Up to 7 rows the gates follow main exactly: one launch per row below 2 rows (decode), main's split gates from 2 rows. On chips without tensor units, 8 rows or more take this PR's simdgroup-matrix gates, which give the same bits. The split gates now accept up to 256 rows, so tensor-unit chips can run 256-row chunks; the router takes main's split from 2 rows. Main's new admission test is updated for the 256-row scratch: 6.19 and 15.19 GiB of room on 36 and 48 GiB Macs, 10 and 25 streams at an 8,192 window (it assumed 64-row scratch). On the M4 Pro, main 3e1c4c582e against this merge at --parallel 4 (Inkling-2-Mini@ed40e7da): cold 255/256/257/512/513, chat 8/16/24, greedy 256, seeded 64 and 4 concurrent equal to solo all have equal token ids and sha256. Cold 512 takes 33.0 s on main and 2.05 s here. Tests, kernel contract (67 pass, 74 controls red), lean 0 new, pr_check 0/0 and fmt are clean. Head 3d22cd3a91. The M5 cells in the body were measured before this merge.

@AIFlowML

Copy link
Copy Markdown
Contributor

I tested 3d22cd3a91 on d1ff890729 alone and after #4998 (c011aec4bc) and #5000 (2d955fd616) in that order. Both trial merges were clean.

zig build test-inkling2-host test-inkling2-metal-host test-inkling2-dense-guard tf-kernel-contract -Dfixed-cpu -j2 --summary all
./zig-out/bin/tf-kernel-contract --only 'metal inkling2'

On M5 Max, the standalone tree passed 147 tests with 11 skips and the device filter passed 61 kernels with 65 planted controls caught. The combined tree passed 150 tests with 11 skips, 62 kernels and 66 planted controls. Both reported zero launch-limit problems. The non-tensor-unit dense-guard test skipped on this GPU, so the M4 receipt remains the evidence for that guard. Combined serving comparisons at the 256-row chunk size remain open.

Igor Lessio - AIFLOW LABS

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