Skip to content

Metal: experimental Inkling 2 Mini text, vision and bundled DSpark - #4337

Open
BlivionIaG wants to merge 108 commits into
ashhart:mainfrom
BlivionIaG:work/inkling2-mini-metal-4bit
Open

BlivionIaG wants to merge 108 commits into
ashhart:mainfrom
BlivionIaG:work/inkling2-mini-metal-4bit

Conversation

@BlivionIaG

@BlivionIaG BlivionIaG commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Serve the original thinkingmachines/Inkling-2-Mini checkpoint on native TensorFold Metal, with text, PNG/JPEG vision and the bundled DSpark head. Packed NVFP4/MXFP8 expert weights stay packed; kernels decode them to BF16 in registers, use BF16 activations and sum in FP32.

Before: main does not serve inkling2_loop on Metal. After: this branch serves the original checkpoint, including mtp.safetensors, and accepts images. Experimental, M4-class chips; not a release-support claim.

What it contains

  • Text path and bundled DSpark head; vision (PNG, JPEG) through the vision tower; admission by state bytes.
  • Grouped prompt experts, deduplicated verify experts, and MMA tiles for experts, dense prompt projections, loop gates and 8-row prompt attention. Each keeps the one-row chain's order, so bytes are identical (the contract's byte-equality gates, NaN, infinity and all-pairs-one-expert inputs included).
  • One batched draft head pass for all drafting streams, and a plain round runs no head work.
  • Docs: recipe (docs/recipes/inkling-2-mini-metal.md), supported-models row, kernel-contract, vision and known-issues entries, optimization-ledger cells, and the raw receipts in docs/receipts/inkling2-mini-m4pro/.

Shared-core edits (maintainers' call)

  • core/memory_pressure.zig and core/lane_host_pressure.zig: Governor.hold_from, streamsFit and Change.hold_from, with tests. The default for every other family is unchanged. Admission counts the weights, scratch and each stream's state bytes and no longer serializes streams that fit under a warning level.
  • core/contract/registry_joins.zig: four Metal record joins. native/metal.zig and the build files: registration of the family, and metal4 = true (the Metal 4 queue is compiled in). platform/environment.zig: the wording of the existing TF_METAL4 rule now also covers Inkling 2 Mini's prompt chunks.
  • Deferred, not hidden: deepseek_v41/metal/mv*.zig imported family to family, a third vision tower in vision.metal, loop_mix against attn_res, gemm_out against gemm_nax, a third E4M3 decode, core/modelopt* and kv_pool unused, a fourth visit plan (plan.zig). I would do these in a follow-up core PR if the maintainer wants them, or now if the maintainer rules.
  • Open decisions: F1 (the NVFP4 outer factor stays the unrounded FP32 quotient of the two BF16-rounded metadata tensors, as in the current kernels and the public fixture; rounding is the maintainer's call), fma in the expert multiply-add and a changed prompt-chunk sum order (both measured to change bits, so neither is applied).

Checks on this head

  • Head commit: 37f8b61c03.
  • Lean checker, python3 tools/zig/lean_check.py: 165 problems on the base, 165 on this head, 0 new.
  • python3 tools/pr_check.py --base upstream/main --pr 4337: 0 errors, 0 warnings. zig fmt --check build.zig zig: clean.
  • Tests: zig build test-inkling2-host passes on this head; test-inkling2-media-metal 79 passed, 9 skipped (the JPEG tests, for want of the pinned libjpeg-turbo release) at 05b6fdb5c0; zig build test-server-cpu 1841 passed, 15 skipped at 3682b31cfc (it includes the audio-refusal test); zig build dev-tools builds. The one test-kernel-contract failure (registry.zig:456, CUDA inkling2_modelopt entries) fails the same way on the maintainers' stack without this PR.
  • CI: queued on this push when this was written; the previous head e4259ce0e4 had fmt, pytest, zig-macos-dev-tools and the other quick checks green, and I will report each zig job that fails and whether this change caused it.
  • Not run, and why: zig build test test-golden (15-minute timeout locally, not a pass); a task-accuracy quality suite; M1, M2; a 36 GB Mac on this head; the M5 tensor-unit prompt path (not written).

Receipt

Raw results, the prompt-token generator, hashes and the build SHA-256 are in docs/receipts/inkling2-mini-m4pro/; its README says which file each number comes from and at which build. Decode, cold-prompt and footprint numbers were taken on the round-three build (a0935b5279), the concurrency table on the build that became 05b6fdb5c0; the head differs from those by main merges and documentation only.

  • Main against branch: n/a, main does not serve this model on Metal. The same-machine reference is mlx_lm.server (mlx 0.32.4, mlx-lm 0.1.dev1006+g0c4c99192, same checkpoint, no drafter).
  • Boxes it reaches: Apple M4 Pro 48 GiB (here); M4 Max (contract, @akol1); M5 Ultra (contract, plain decode 55.4 and 55.8 tok/s, DSpark 65.3 and 65.3, first token at 6,964 tokens 4.73 s, all at 9a255bb79e, @SashaMIT); M3 Pro 36 GB (earlier head, @jschmied). Linux and CUDA: not touched.
  • New switch? None. The existing TF_METAL4 (default on for DeepSeek-V4.1) now also sends Inkling 2 Mini's prompt chunks of 64 rows or more through a Metal 4 queue on macOS 26 and later (TF_METAL4=0 keeps Metal 3; a Mac without it falls back). TF_INKLING2_MINI_SMOKE is test-only and documented in docs/environment.md. No profile row.
  • Environment: Apple M4 Pro, 48 GiB, macOS 27.2, Zig 0.17.0, Metal; checkpoint ed40e7da625f594fc9c4990f5f66be24873c22d7 plus its mtp.safetensors; -Dcpu=apple_m1 -Doptimize=ReleaseFast. An unrelated CPU job used about 60 % of a core in the decode and cold runs, so read ratios.
  • Exactness: greedy token hashes (server sha=, chat completions at --context 8192) 0b037128ee78, b37d8a0e39c4, 77bf7c99cc66, 538175732cc7, identical across the round-three and round-four builds; bench_concurrent --alone --serial drafted four streams: 30 of 30 replies equal to their solo runs, 4 of 4 equal to "draft": false. Sampled (temperature 0.7, seed 1234): 30 of 30 and 4 of 4 equal, 467 and 447 drafts accepted. On the maintainers' stack at 232ee08a39 and Inkling 2 Mini on Metal: a prompt's experts, projections and loop gates on simdgroup matrices where the GPU has no tensor units, bit for bit #4703's head the same prompts give the same hashes (the 416-token one, 77bf7c99cc66, matches across the two independent implementations).
  • Decode, one stream, bench_openai.py --tokens 256 --reps 3 --temperatures 0: MLX 23.05 / 23.69, native plain 31.12 / 31.07, native DSpark 35.76 / 35.01 tok/s (fibonacci-raw / gpu-chat-no-think).
  • Four streams, bench_concurrent, A-B-B-A, native plain / DSpark aggregate: code 67.0 / 65.1, chat 66.8 / 66.9; one stream 31.1 / 37.7 and 30.9 / 35.8. MLX answers four requests one after another (23.3), so its four-stream row is not a same-width comparison.
  • Sampled decode (temperature 0.7, seed 1234), idle machine, one A-B-B-A session (plain, DSpark, DSpark, plain; the mean of each pair): plain 21.4 / 33.8 (code, 1 / 4 streams) and 21.5 / 33.8 (chat), DSpark 20.7 / 25.9 and 21.3 / 33.2. So the draft head is 1 to 3 % under plain except four code streams, 23 % under. Separately, a sampled plain token costs about 15 ms more than a greedy one here (21 against 31 tok/s at one stream, 34 against 67 at four): the host sampler sorts the whole 201,058-token row. lanes: the host GPU-rule sampler keeps its best 1,024 candidates in a bounded heap, not a full sort #4744 (shared sampler, its own PR, identical tokens) fixes it: plain sampled code 21.2 to 29.2 tok/s at one stream and 33.5 to 56.1 at four on a shared machine; it is not in this branch. Greedy is the case with a receipt for "no slower".
  • Prompt speed, cold first token (/v1/completions, exact ids, cache off), on the Metal 4 build 4418225fb6 with the dense prompt tile, quiet machine (load average 2 to 3), native then MLX, one fresh server and one run per size (not interleaved): MLX 4.32 s (474 tok/s) / 17.04 s (481) / 75.55 s (434) / 169.70 s (386) against native 5.18 s (395) / 21.59 s (380) / 97.05 s (338) / 225.97 s (290) at 2,048 / 8,192 / 32,768 / 65,536 tokens, so native runs at 0.83 / 0.79 / 0.78 / 0.75 of MLX's rate. The round-three session had 0.85 / 0.87 / 0.75 / 0.72 (native 5.49 / 22.53 / 108.27 / 249.21 s against MLX 4.68 / 19.53 / 81.74 / 180.27 s); MLX was slower there and the cause was not isolated. The dense projection tile took the first token from 23.8 s to 22.7 s at 8,192 tokens and from 104.8 s to 102.0 s at 32,768 (A-B-B-A, idle machine, same hashes). Sending the prompt chunks through a Metal 4 queue (same kernels, arguments and order) took it another 10 % off in an A-B-B-A on plain servers: 8,192 tokens 25.2 / 24.9 s to 22.5 / 22.3 s, 32,768 tokens 115.6 / 116.6 s to 105.4 / 102.4 s (an earlier loaded session gave 15 to 17 %; same sha= on all runs, the four chat hashes, DSpark with four streams and a prompt in flight, contract 86 kernels / 133 controls red; with three decode streams running and a cold 4,096-token prompt arriving the Metal 4 queue was not worse for them). A 512-row chunk's GPU time by family is in the recipe: the routed experts are 857 ms of 1,281 at the start of a prompt. The speed bar for prompts is not met.
  • Fidelity against MLX, same checkpoint, seven text prompts (to 31,998 tokens) each continued by MLX's own greedy 256 tokens, forced by restarts: native top-1 equals MLX's at 1,762 of 1,792 positions (98.33 %; lowest prompt 95.7 %, Python source), mean absolute logprob difference 0.005 to 0.028, no drift across the continuation, all 30 disagreements near ties in MLX (top-1 minus top-2 at most 0.386 nats). Control: MLX against itself with a 256-row prefill step diverges at the same near ties on three of the prompts. Per-layer logits were not compared. Built from 57de097873; receipt fidelity/.
  • Images served (--vision, PNG and JPEG): native reads the same text, chart values, shapes and scene as mlx_lm.server. The tower's two fp32 attention dots (q.k^T and p.v) were 89 % of a 12.5 s tower on the 1,561-token invoice (one thread per output); they now run 64 x 64 tiles through threadgroup memory with each output's fma chain over k unchanged (bytes equal: the tower's output hash on 14 image requests, 16 identical replies per image, the four text hashes). First token, A-B-B-A on a quiet M4 Pro: 546-token scene 2.95 to 3.35 s before, 1.94 to 2.33 s after (MLX 1.7 s); 1,561-token invoice 15.7 to 16.2 s before, 6.9 to 7.2 s after (MLX 4.7 to 4.8 s). The small images were not re-measured quiet. The score matrix between q.k^T, the softmax and p.v then moved to bf16 (every value it held was already a bf16 number; the softmax was at 1.06 to 1.10 times its bandwidth floor, so halving its bytes was the lever; same max, exps and sum order, each p.v output still one fma chain): invoice first token 6.94 s to 6.54 s (A-B-B-A, load 2.9, 5.8 %; the scene 1.96 to 1.93 s is inside the noise), tower output hash, every reply and the four text hashes equal, standalone bitwise comparison on 7 ragged and full shapes, contract 86 kernels / 134 controls red / 0 missed (one planted control added). About 2.8 s of the 6.5 s is the tower and about 3.7 s is text prefill.
  • Decode at depth, one stream, plain, mean inter-token latency of 192 streamed greedy tokens after a cold prompt (docs/receipts/inkling2-mini-m4pro/decode_depth.py): native 28.5 / 28.4 / 25.8 / 23.2 tok/s against MLX 23.0 / 22.6 / 20.0 / 17.8 at 2,048 / 8,192 / 32,768 / 65,536 tokens (1.24 to 1.30 times). Native's growth from 2K to 65K is 8.0 ms a token, within the 8 global visits' K and V bandwidth floor (0.131 ms per 1,000 tokens at 250 GB/s); an A-B-B-A pass for 2K to 32K, the 65K rows later; the machine was loaded (load average 4.5 to 6.3).
  • Peak footprint: about 24 GB native, about 23 GB MLX.
  • Licence: n/a, the drafter is the checkpoint's own mtp.safetensors (Apache-2.0, as in docs: a plan for Inkling 2 Mini on CUDA and Metal in claimable slices #4274).
Kernel contract
  • Kernels registered with the contract, and the kernel each replaces: 86 for this family on Metal; the grouped-MMA kernels replace the scalar one-row kernels' prompt and verify paths with identical bytes.
  • Planted controls: 134, each fails as planted.
  • SUMMARY on this head, M4 Pro: SUMMARY 86 kernels: 86 pass gates 1-4, 0 fail, 0 known findings; 134 controls: 134 red as planted, 0 missed; 0 limit problems, exit 0 (the bf16 score matrix of the image tower added one planted control to the earlier 133). On earlier heads: the same 86 kernels and 132 controls (before the dense tile's added control) on an M4 Max at e6cb02e09e (@akol1). An M5 Ultra (@SashaMIT) at 9a255bb79e: 86 pass, 132 controls red, 0 missed, 0 limit problems; it counts one control fewer than the M4 chips because the media suite plants the bf16_nt row control only on chips without tensor units.
  • Chips I could not run: M1, M2 and M3 at this head.

The agreed gate (#4274, docs/design/inkling-2-mini.md), item by item

Gate item Status on this head
Kernels: the full SUMMARY, every planted control red, on the chips named Met. tf-kernel-contract --only inkling2: 86 kernels pass gates 1-4, 134 controls red, 0 missed, 0 limit problems on an M4 Pro (this head); the same kernels with 132 controls on an M4 Max (@akol1) and an M5 Ultra (@SashaMIT, one control fewer by design: the bf16_nt row control is planted only without tensor units).
Same-engine exactness, tiny and released checkpoint, greedy and sampled: drafted equals "draft": false, concurrent equals solo Met on the released checkpoint: bench_concurrent --alone --serial greedy on the DSpark build, 30 replies equal to their runs alone and 4 equal to "draft": false, 0 unequal; sampled (T 0.7, seeded), plain and DSpark, --alone and --serial all equal, 0 unequal (sampled-*.out). I did not run the tiny checkpoint for this table.
A resumed prompt equals a fresh one Met, with and without the drafter. A kept prompt state now also holds the DSpark head's K/V (about 19.7 MiB at 3,000 tokens beside the 273 MiB target snapshot). On 2.9k to 4.5k-token multi-turn chats (--parallel 4, greedy and seeded 0.7, 37 resumed past the 1,024-token window), drafted with the cache equals plain cold in 56 of 56 requests; drafted cold equals plain cold in 28 of 28; bench_concurrent --alone --serial on that server 40 alone and 20 serial equal, 0 unequal (drafted-resume/summary.txt). With the drafter on the default cache budget is now half the room, as on plain servers (it was 0; cacheBudget, one line). Not covered: images with drafts and the cache, and warm-pass-end keeps, which resume with the same tokens and fewer accepted drafts. I could not repeat the served resume on the merged head: the machine reported memory pressure from other jobs and the server then keeps no new states, by design.
Fidelity: teacher-forced histories against the reference, every top-token difference and its margin recorded Partly. Against MLX (mlx-lm) on the original checkpoint: top-1 equal at 1,762 of 1,792 positions (98.33 %), all 30 differences near ties with their margins in the receipt (fidelity/compare.txt). The tower against Transformers 6173a2f6 BF16 eager (83 of 83) and the public fixture (20 completion ids of 20) are carried from the first version. The language model was not compared against huggingface/transformers#51050 here [INFERENCE: a BF16 reference of this size does not fit 48 GiB].
Speed: a served receipt against a standard server on the same machine; the bar is decode no slower than that server, with and without the drafter Met for decode. Plain decode 31.1 tok/s against MLX's 23.05 to 23.69 at one stream; DSpark 35.0 to 35.8; four streams 67 against 23.3; at depth 1.24 to 1.30 times MLX from 2,048 to 65,536 tokens. The receipt also has cold first token (0.75 to 0.83 of MLX's rate: below the standard server, which the bar does not cover, and which this description reports as it is), peak memory (about 24 GB against 23 GB) and one and four streams. Drafted sampled decode at four code streams is 23 % under plain on this head and about 6 % under with #4744 (29.4 against 29.2 at one stream, 52.9 against 56.1 at four).
Refusals: is_varloop, --loops, audio parts, images before the vision slice, each with a test Met (tests in this PR; images are now served under --vision).

Checklist

  • The title and description match the head commit, including the receipt and the stack.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency (the real-checkpoint tests skip without TF_INKLING2_MINI_SMOKE; the JPEG tests skip without the pinned libjpeg-turbo release).
  • 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.
  • Every token goes through the lane rounds. Nothing needs drafts off except kept prompt states (limit stated in the recipe).
  • Resumed prompt equals a fresh one, with the drafter on and off (see the gate table; the drafter's head state is kept beside the lane snapshot).
  • No precision traded for speed. Bits did not change in this PR's optimizations; the two candidates that would change bits are listed above and not applied.
  • Decode no slower than a standard server at one stream and the widest admitted, with and without the drafter (the agreed bar): greedy decode is 1.24 to 1.30 times MLX's. Not met, and stated: cold prompt processing is 0.75 to 0.83 of MLX's rate (the bar does not cover it); sampled decode costs about 15 ms a token more than greedy until lanes: the host GPU-rule sampler keeps its best 1,024 candidates in a bounded heap, not a full sort #4744, and with the drafter at four code streams it is 23 % under plain here (about 6 % with lanes: the host GPU-rule sampler keeps its best 1,024 candidates in a bounded heap, not a full sort #4744).

- Preserve the original release NVFP4 and MXFP8 expert weights and execute the looped text backbone on one Metal device.
- Keep the bundled DSpark head and plain prompt-cache reuse in the native lane backend.
- Refs ashhart#4274. Local M4 Pro evaluation, separate from the upstream full port.
- Keep the original vision tower, PNG/JPEG preparation, and the separate device and operator checks on the full integration branch.
- The padded-image real-weight accuracy gate remains open. This commit is not part of the text-only review branch.
- Match pinned Transformers eager QK and scaling boundaries before FP32 softmax.
- Keep the trained padded-image projector bound unchanged.
- Keep original NVFP4/MXFP8 checkpoint bytes and decode weights in registers, using BF16 activations and FP32 sums.
- Match pinned Transformers conversion and SwiGLU/route precision, with packed-weight and zero-scale controls.
- Refs ashhart#4274. Local original-weight M4 Pro qualification; not an upstream submission.
- Keep the shader and probe comments within the repository lean rules; no arithmetic changes.
@BlivionIaG
BlivionIaG force-pushed the work/inkling2-mini-metal-4bit branch from f3a6a99 to 82e7aa0 Compare November 21, 2026 08:50
@jschmied

Copy link
Copy Markdown
Contributor

Review at 82e7aa0544 (base dcc29d9d62). I read the code at f3a6a992f2 and re-checked the refs below on 82e7aa0544; the rebase changes no line used here. I ran tools/zig/lean_check.py on both heads and their bases; nothing else was built or run. On the M3 Pro (pre-M5), a device run of the contract and the original checkpoint is underway, and I'll post it here.

This covers a lot of the plan: H1, H2 and H4 fully; most of H3, written as a Metal contract rather than a backend-neutral host spec; and M1-M7. M8 (the served receipt) is missing, and so is an M5 prompt path. Visits and taps check out: 5→L5 pass 0, 23→L11 pass 1, 41→L17 pass 2, and 48 after the final norm, with one cache slot per visit. The bank-then-restart order is right, and the SwiGLU sigmoid is taken on the uncapped gate, as in the CUDA code.

Decision needed (@ashhart)

  • F1, the NVFP4 factor. families/inkling2/metal_experts.zig:66-69 computes fp32(bf16(amax) / bf16(2688)) and never rounds the quotient. The kernel rounds once, after applying the factor (experts_dot.metal:91-92). inkling2_expert_math.zig:42 says "rounds operands but not the outer factor". Decision 2 as written says the factor is rounded to bf16 when it loads.
    • The two readings differ by 1 ulp on about a third of the decoded weights (my estimate from simulating both).
    • I confirmed from the bytes that the denominator is 2688 = 6·448 (docs: a plan for Inkling 2 Mini on CUDA and Metal in claimable slices #4274), so bf16(2688) is exact. The only question is whether the quotient is rounded.
    • All three backends need the same answer, which argues for one factor() in core/modelopt_part.zig (F6).

CI

  • F2, lean rules. lean_check.py reports 168 problems on the base dcc29d9d62 and 551 on the head, so this PR adds 383 and no-gpu CI will fail.
    • Of the 382 on f3a6a992f2, there were 339 multi-line comments, 40 comments over 120 columns, 2 block comments, and a direct getenv("TF_INKLING2_MINI_SMOKE") in src/inkling2_metal_checks.zig:15 that should go through platform/environment.zig.
    • native/metal.zig is at exactly 600 lines.

Shared core

  • Reused, good: the lane rounds and taps, prompt_cache, metal_admit, checkpoint_metal, and gemm_nax/vision glue.
    • One catch: the DeepSeek V4.1 bf16 matvec (deepseek_v41/metal/mv.zig, mv_mma.zig) is imported family-to-family, so it belongs in core.
  • Copied:
    • kernels/metal/inkling2/vision.metal is core's qwen_vision.metal glue (:20-144) with a bf16 residual, d^1 rotary pairing and bf16 score rounding. That makes the third Metal tower, after step5's; parameters on the core one would cover it.
    • loop.metal:29 inkling2_loop_mix is this PR's own attn_res.metal:22 without the final norm. One kernel with a norm flag would mean one contract.
    • gemm.metal:6-32 gemm_out is gemm_nax with a different output type, so a template on O would do.
    • The E4M3 decode is the third copy, after core/exlr8_dense.metal:20.
  • Not used: core/modelopt.zig and modelopt_part.zig (the quant check and scale reading at metal_experts.zig:54-73, config.zig:415-427), and core/kv_pool.zig.
    • metal_state.zig:32-33 gives each visit whole private buffers, with the 8 global visits reserving the full context. That means no page sharing between kept prompts; Dory's pages are the looped precedent.
  • Visit plan: plan.zig is the fourth visit plan in the repo, after Dory, qwen3_5 cuda: SenseNova-Loop-9B (sensenova_loop) in BF16, text only #3723 and qwen27 metal: serve SenseNova-Loop-9B natively (BF16 and AWQ g128, 512K YaRN, images) #3859. That is fine under decision 5, but HIP (Inkling 2 Mini on HIP (gfx1151): serve the looped 26.4B checkpoint natively #4265) and CUDA C4 should import it rather than rebuild it. That is the case for a small core/visit_plan.zig, and I asked about it in docs: a plan for Inkling 2 Mini on CUDA and Metal in claimable slices #4274.

Coverage and refusals

  • Kernels without a planted control:
    • Nine kernels run gates without a control of their own: ik2d_alpha, ik2d_embed_block, ik2d_rms, ik2d_add_rms, ik2d_qk_prep, ik2v_embed, ik2v_heads, ik2v_bias_gelu, ik2v_gelu_rows. "80/80 controls" counts controls, not kernels.
    • The expert mutations edit the shared i2x_dot template, so no control isolates the down projections.
  • Refusal tests: --loops is refused (native/inkling2.zig:84) but no test names it. Audio parts get only the generic server refusal.
  • Prompt cache with DSpark: with DSpark on (the default), the prompt cache is 0 and --prompt-cache-gib is refused (native/inkling2.zig:107-113). The served default never resumes, which the plan's gate (resumed equals fresh, drafted) needs. Either put the draft head's state in the snapshot, or state the limit.

Nits

  • The w13 gate/up rows are interleaved (experts_dot.metal:54,69), which deserves a one-line comment.
  • checkpointFileBytes hard-codes the single shard name (native/inkling2.zig:96-101).
  • The state-bytes test uses an unlooped shape, so Mini's 48 buffers, 8 of them global, aren't covered.

- The rebase dropped the alias main uses for slide refusal, so the native server did not compile.
@jschmied

jschmied commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Device run on an M3 Pro (36 GB, no tensor units, so the pre-M5 path) at f3a6a992f2 on main 37fa51d239; f110093bc0 gave the same counts. Since then, 82e7aa0544 and d7dd9c3140 only rebase it: a renamed records file (inkling2_metal_ops_records.zig), one build line and one line in native/metal.zig (learner registry). The kernels and family code these rows exercise are unchanged. Run read-only.

  • Kernel contract (tf-kernel-contract -Dcpu=apple_m1 -Doptimize=safe):
    • --only inkling2: 86/86 gates pass, 80/80 controls red, 0 missed, 0 limit problems.
    • --only "inkling2 experts": 2/2 pass, 9/9 controls red.
  • Host tests, no checkpoint: test-inkling2-host 28/32 (4 skip), test-inkling2-media-metal 74/83 (9 skip), test-golden 324/337 equal (13 known, 0 unexpected).
  • With the original checkpoint (ed40e7da, all 14 files verified against the publisher hashes):
    • test-inkling2-metal 7/7 and test-inkling2-loop-metal 36/36.
    • Media 76/83. The 7 skips are the JPEG tests, because this Mac lacks the pinned libjpeg-turbo; that's a gap on our side, not in the PR.
  • Served, --parallel 1 --context 4096, greedy, plain (--no-drafts) against DSpark:
    • Tokens: the token hashes are equal on all three requests (176, 201 and 256 tokens, the last capped). draft:false on the DSpark server is equal too.
    • Speed: c=1 decode at 256 tokens is 12.0-12.4 tok/s plain and 14.4-14.6 tok/s with DSpark (220 drafted, 124 accepted). Load takes 21.6 s.
    • Memory: peak RSS is 17.49 GB plain and 18.14 GB with DSpark (weights 18.19 GiB resident). On 36 GB, the DSpark server logs memory pressure: warning: one more stream only when none runs.
    • Replies show the raw <|message_model|><|content_thinking|> delimiters, as the CHANGELOG line says.

There's no standard-server baseline on this Mac yet, so these numbers make no speed-bar claim. A llama.cpp row on the same machine is next.

@jschmied

Copy link
Copy Markdown
Contributor

Speed rows from the same M3 Pro (36 GB, pre-M5) at f3a6a992f2, against llama.cpp on the same machine. Every request was greedy and forced to 256 tokens through the chat template. c=1 is two requests after one warm-up; c=4 is four different prompts at once. Runs are in A-B-B-A order.

  • llama.cpp: b12602 (commit 418fe97b5, the official macos-arm64 tarball, sha256 matched to the GitHub digest), -ngl 99 -c 16384 -np 4, no drafting.
  • Its weights: ggml-org/Inkling-2-Mini-GGUF @ db61c5b492, IQ4_NL, sha256 equal to the publisher's.
Run c=1 tok/s c=4 aggregate tok/s (per stream) Peak RSS
A: llama.cpp 18.80 / 18.83 33.19 (8.4-8.9) 19.5 GB
B: this PR, plain 12.6 / 12.4 12.10, run one after another (see below) 19.5 GB
B: this PR, plain 12.6 / 12.5 24.57 (6.3-6.6), all 4 together 18.4 GB
A: llama.cpp 18.64 / 18.20 33.15 (8.7-8.9) 19.4 GB
This PR, DSpark (2 runs) 14.7 / 14.6, 14.4 / 14.7 14.99 / 15.07, run one after another 18.3-18.6 GB
  • Ratios: at c=1, plain decode runs at 0.67× llama.cpp (12.5 vs 18.6 tok/s) and DSpark at 0.78×. At c=4 with all four streams running together, plain decode runs at 0.74× (24.6 vs 33.2 tok/s). On this chip the PR does not yet meet the agreed bar, decode no slower than a standard server, either with or without the drafter.
  • Different weights: llama.cpp runs IQ4_NL plus Q8_0, not the original NVFP4/MXFP8, so this compares speed only, not replies. Per prompt, the token hashes match between plain and DSpark at c=4, and they match c=1.
  • Admission on 36 GB:
    • The server admits streams by OS memory pressure. The first plain run, started right after llama.cpp exited, logged memory pressure: warning: one more stream only when none runs, so its four requests ran one after another.
    • The DSpark server logs the same warning on both runs, even when started with 72 % of memory free. So on a 36 GB Mac, DSpark at c=4 always runs one request at a time.
    • Admission might be worth checking against what the weights and per-visit state actually need. That state is whole buffers per pass; see F7 in the review above.

This chip has no tensor units, so it can't show what M1's M5 prompt path or the planned M5 rows will do.

…-ups

- Comments are one line each again (339 runs collapsed, 40 shortened) and no `/*` text remains, so lean_check.py adds nothing to the base's 168 problems.
- TF_INKLING2_MINI_SMOKE reads through platform/environment.zig: a .metal/.path row, a docs row and a valid case in environment_test.zig.
- The load check sums every model*.safetensors shard, as core/checkpoint.zig lists them, plus mtp.safetensors when drafted.
- The state-bytes test covers Mini's looped shape: 24 physical layers as 48 visits, 8 global, visit 41 at physical 17.
- A new contract control isolates the experts' down projections; --loops and --policy refusals have a test that names them.
- The cacheBudget doc comment and the CHANGELOG state the DSpark prompt-cache limit: kept states need --no-drafts.
- The Inkling 2 contract suites move to metal_inkling2_main.zig, holding their probes for the run, which keeps metal_main.zig under 600 lines.
- The NVFP4 outer factor stays the unrounded quotient, pending the maintainer's F1 decision.
@BlivionIaG

Copy link
Copy Markdown
Contributor Author

Follow-ups from the review are on 3439d56ae0. The description now matches that head.

  • Lean: 168 problems, 0 new against dcc29d9d62.
  • Smoke getenv goes through platform/environment.zig. Shard-byte sum includes every model*.safetensors plus mtp.safetensors when drafted. State-bytes covers the looped Mini shape. Down-projection, --loops, and --policy have named tests. DSpark still refuses prompt-cache resume; that limit is documented, not implemented.
  • F1 is unchanged: the outer factor is the unrounded FP32 quotient. Waiting on the decision before rounding it.
  • M4 Pro served receipt is in the description. Exactness passed. The speed bar did not: MLX plain is 25.26 / 25.43 tok/s, native plain 16.63 / 16.59, native drafted 21.69 / 20.54 on the four-stream server.

…ites

- `zig fmt --check build.zig zig` (what the Zig format workflow runs) is clean; the new suites and loaders had drifted.
- Layout and `@intFromEnum` -> `@backingInt` only: the contract, native, vision, forward and environment checks are rerun green on this tree.
@BlivionIaG

BlivionIaG commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor Author

Point-by-point on the review, head 8606b655d6 (base dcc29d9d62).

F1, the NVFP4 factor — maintainer decision, not chosen here. No code change. metal_experts.zig still computes fp32(bf16(amax) / bf16(2688)) and the kernel still rounds once after applying it, and the comment in inkling2_expert_math.zig still records that the outer factor is unrounded. Whichever way you settle Decision 2 (single rounding at the kernel, or a rounded factor), this is the one line to change; I did not pick for you.

F2, lean rules — cleared. 383 new problems against the base, now 0:

  • 339 multi-line comment runs collapsed to one line each (44 files), keeping the statement, dropping the restatement.
  • 40 comments over 120 columns shortened.
  • 2 block comments (/* … */) turned into one-line // comments.
  • the direct getenv("TF_INKLING2_MINI_SMOKE") in src/inkling2_metal_checks.zig now goes through platform/environment.zig.
  • tests/contract/metal_inkling2_main.zig was 600 lines with the incus constants; the Suite collect and the postLs probe setup moved into metal_inkling2_suite.zig, which leaves it well under the 600-line cap.

Shared core / refactors (deferred, not ours to land here). Moving deepseek_v41/metal/mv.zig into core, merging modelopt.zig/modelopt_part.zig/kv_pool.zig call sites, and collapsing the three kernels' duplicated decode paths are cross-family changes. They would touch DeepSeek V4.1 and Qwen on a Metal-only PR, so they belong to a core-scoped PR; the duplicated scale-ladder code is listed for that one.

Coverage and refusals.

  • --loops is covered by a test: the refusals test in src/native/inkling2.zig sets o.loops = 2, and separately o.policy, and asserts the exact "--policy and --loops are unsupported for Inkling 2 on Metal" message — not just a non-null refusal.
  • Audio parts: still open, and I am not claiming otherwise. The checkpoint's chat template (fixtures/chat_template.jinja:88) raises Inkling 2 does not accept audio input for audio/input_audio/audio_url parts, but nothing on the Metal server path renders through families/inkling2/prompt.zig yet — only host_test.zig/template_test.zig do, which is the same missing prompt path you noted elsewhere — so a served audio part still reaches the generic refusal. A test that names the template's exception would not exercise the served path, so I left the point open instead of implying coverage.
  • Prompt cache with DSpark: stated, not built. The --prompt-cache-gib refusal comment and the CHANGELOG line now say that the draft head's state is not part of the snapshot, so the served default with DSpark does not resume. Putting the draft state in the snapshot is a real change to the snapshot layout and I did not smuggle it in here.

Nits.

  • experts_dot.metal: the w13 gate/up interleave now has the one-line comment you asked for.
  • checkpointFileBytes no longer hard-codes model-00001-of-00002.safetensors; it derives the shard names from the model directory and counts them.
  • The state-bytes test uses a looped Mini shape now, so Mini's 48 buffers (8 global) are covered, not just the unlooped one.

Local results on this head (M-series, -Dcpu=apple_m1):

  • build tf-kernel-contract -Dcpu=apple_m1 then the suite: 86 kernels, 81 controls pass, 0 fail, 0 missed; collate and route tests included.
  • build test-inkling2-host -Dcpu=apple_m1: 29 tests pass.
  • build test-native -Dcpu=apple_m1: 168 pass, 3 skip, 1 crash — the crash is the pre-existing GLM test (draft_sources_metal_test, upstream file at the base, not in this diff).
  • vision suite (-test-filter "Inkling 2 Media"): 74 passed, 9 skipped, 0 failed; the 9 skips need TF_INKLING2_MINI_SMOKE and a real checkpoint.
  • tools/zig/lean_check.py: 168 problems, 168 at the base, 0 new — every remaining one is in a file this PR does not touch.
  • tools/pr_check.py --base dcc29d9d62 --pr 4337: 0 errors, 0 warnings.
  • zig fmt --check build.zig zig: clean. The earlier zig-format.yml failure was real: zig fmt on the changed files. Formatting is now its own commit (8606b655d6), and the whole tree is clean for the command the workflow runs.

CI on 8606b655d6: the hosted run 44893743142 is still running — 9 checks pending (zig, zig-b, zig-windows, zig-macos-dev-tools, pytest, release-paths, benchmark-clients ubuntu and windows, comfyui-moss-installer) and 2 skipped (host-nightly-linux, host-nightly-macos, by design). No hosted check has reported on this head yet, and I am not waiting for it here. The previous head 3439d56ae0 did have a real failure: zig-format.yml → zig fmt --check on the changed files; that is fixed by 8606b655d6, whose tree passes the same command locally. If zig or zig-b fails on this head I will fix it in a follow-up push.

@BlivionIaG

Copy link
Copy Markdown
Contributor Author

Thanks for the device rows and the llama.cpp comparison. Recorded plainly: on the M3 Pro the agreed bar is not met — plain decode 12.5 vs 18.6 tok/s at c=1 (0.67×), DSpark 0.78×, and c=4 plain 0.74× (24.6 vs 33.2) — so this stays a draft and I am not claiming a pass. The admission observation is tracked as open, not fixed: the DSpark server serializes c=4 under memory pressure even with 72% free, and the per-visit state is whole buffers per pass (F7), so the admission check needs to account for that state rather than only OS pressure; I have not changed it here because it needs a 36 GB machine to reproduce and measure. Token equality between plain and DSpark at c=1 and c=4 is noted as the useful part of those runs. F1 remains the maintainer's decision.

@jschmied

Copy link
Copy Markdown
Contributor

@BlivionIaG, a profile of 8606b655d6 on the 36 GB M3 Pro, to go with the speed rows: ReleaseFast, original checkpoint ed40e7da, greedy, 256 tokens, Metal System Trace attached to the server.

Case tok/s GPU busy Command buffers per token CPU encode per token
plain, c=1 12.0 99.4 % 1.00 0.90 ms
DSpark, c=1 13.7 98.0 % 2.06 0.50 ms
DSpark, c=4 14.2 total 98.6 % 0.96 0.22 ms

So it is neither launch- nor CPU-bound. The trace records no per-shader samples on this chip, so a local phase timer split plain c=1 (the token hash was unchanged with the timer in):

Phase Share of GPU time ms per token Weights read per token Achieved
attention block: 9 BF16 linears plus conv, prep and attention, 48 visits 64 % 54.9 2.59 GB 47 GB/s
NVFP4 experts, 44 visits 22 % 19.0 1.58 GB 83 GB/s
MXFP8 experts, 4 visits 5 % 4.1 0.26 GB 64 GB/s
unembedding, BF16 8 % 7.0 0.93 GB 133 GB/s
  • Where the gap is: the attention block's BF16 linears. They are 7 to 9 separate one-row GEMVs per visit, 48 visits per token, at 47 GB/s, where the unembedding reaches 133 GB/s on the same chip. One fused GEMV for the projections that share an input (q, gate, wr, sink; k and v), at the unembedding's rate, would save about 30 ms of the 80 ms per token. The experts are fine.
  • Bytes: the forward reads 5.55 GB per token, so 150 GB/s bounds it at 27 tok/s. llama.cpp's IQ4_NL file keeps the dense weights in Q8_0, about 3.8 GB per token, and both engines run near 70 GB/s. Reaching the bar on this chip needs the attention GEMVs near the bandwidth, since the weights stay as stored.
  • Admission: memory_pressure.zig follows kern.memorystatus_vm_pressure_level. With 18.2 GiB of weights on 36 GB the level sits at the warning edge. Whether c=4 serialises depends on when the warning arrives: in the speed-bar runs it came right after load, before the requests, so they ran one at a time; in this profile it came after admission and all four ran together. A check against the stream's own state bytes, as you suggest, would make this deterministic. This 36 GB machine can test a fix whenever you have one.

Receipts, traces and the phase-timer patch are kept locally.

@SashaMIT

Copy link
Copy Markdown
Contributor

M5 Ultra run of d7dd9c3140 (base dcc29d9d62), the missing tensor-unit chip: Mac Studio Mac17,15, M5 Ultra (80-core GPU, applegpu_g17, tensor units true), 512 GB, macOS 27.2 (26B118), Zig 0.17.0, Command Line Tools only (no offline Metal compiler). Original checkpoint ed40e7da with the bundled mtp.safetensors, publisher hashes verified. Run read-only; nothing in the tree changed.

  • Build: at 82e7aa0544, zig build native stops at native/metal.zig:297: use of undeclared identifier 'learners'. d7dd9c3140 restores that one line and builds. tf-inkling2-forward builds at both.
  • Kernel contract (tf-kernel-contract, -Dcpu=apple_m1 -Doptimize=safe):
    • --only inkling2: 86/86 pass gates 1-4, 80/80 controls red, 0 missed, 0 limit problems.
    • --only "inkling2 experts": 2/2 pass, 9/9 controls red.
    • These are the M4 Pro's and M3 Pro's counts.
  • With the original checkpoint:
    • test-inkling2-metal 7/7: shared rows, rollback/restore, snapshots, images, drafted equals plain.
    • test-inkling2-loop-metal 36/36.
    • test-inkling2-host 30/32 with TF_INKLING2_CKPT. The 2 skips are the tiny checkpoint and Small's 24-shard checkpoint.
  • Media: one failure that only an M5 can show. test-inkling2-media-metal gave 74 pass, 8 skip, 1 fail.
    • The failure is "the tensor-unit GEMMs: every kind, whole and ragged tiles, K past and short of 256", case .f32_nt: got -0.40765, want -0.41413, tolerance 0.00583.
    • On an M5 the tensor units keep 10 mantissa bits of an fp32 operand, rounded toward zero, as metal_mma_operand.zig measures. Earlier chips take nax.h's simdgroup path at full fp32, which is why this passes on the M4 Pro and M3 Pro.
    • The tower uses .f32_nt for the attention scores (metal_vision.zig:359), so on an M5 the vision scores run on cut operands. Two possible fixes: the bound states operand_cut, as core/attention_range.zig does for the other M5 attentions, or that product keeps full fp32, as TF_ATTENTION_PV_FP32=1 does for attention_nax.
    • So far the vision suite's 83/83 and the 20/20 public fixture have been measured only on chips without tensor units.
    • This Mac also lacks the pinned libjpeg-turbo. I have not mapped each of the 8 skips.
  • Served, --parallel 1 --context 16384, greedy, plain (--no-drafts --prompt-cache-gib 0) against DSpark:
    • Method: 16 real chat prompts (55 to 6,964 prompt tokens), max_tokens 512, reasoning_effort low, A-B-B-A order, a fresh server per block.
Run Decode tok/s, median (range) Prefill Drafts accepted
plain 20.58 (18.2-21.0) about 370 tok/s (4,878 tokens in 13.2 s)
DSpark 30.67 (27.9-57.5) same 3,830 / 6,318, 1.99 tokens a round
DSpark 30.69 same the same counts
plain 20.61 same
  • Tokens: all 16 token hashes are equal between plain and DSpark. The two runs of each arm are equal too, acceptance counts included.
  • Timing check: the server's own decode time agrees with the client's (511 rounds in 28.05 s, 18.2 tok/s, on the 4,878-token prompt).
  • Load and memory: load takes 3.8 s. Weights are 18.19 GiB resident, dense plus scratch 18.37 GiB (server line). I did not measure peak footprint.
  • Standard server on the same chip:
    • oMLX 0.8.2 on ed40e7da, same 16 prompts and settings, A-B-B-A, measured earlier today on an identical M5 Ultra: 51.1 tok/s plain and 65.6 with DSpark, prefill 2,100-3,100 tok/s.
    • So on the M5 Ultra this head decodes at 0.40x plain and 0.47x with DSpark, and long prompts take about 7x as long. It does not meet the agreed bar on this chip yet.
    • The prompt gap is what M1's M5 path is for: the Inkling attention, experts and glue here are scalar fp32 kernels with simd_sum trees, so nothing on the text path uses the tensor units.
  • Not run: the tiny-checkpoint comparison against Transformers 6173a2f6, which needs a Transformers install we have not made. The text kernels are fixed-order fp32 with no chip-specific path, so your 129-row trace (SHA256 fbbb16ea…) should reproduce bit for bit here. If you share its token file, I'll run it with tf-inkling2-forward.

@BlivionIaG

Copy link
Copy Markdown
Contributor Author

@SashaMIT, thanks for the M5 Ultra run of d7dd9c3140. Recorded as measured, not as a pass: 20.6 tok/s plain and 30.7 with DSpark, against 51.1 and 65.6 on oMLX, so 0.40× and 0.47×. Token hashes matched. The text path has no tensor-unit kernels, which matches the prompt gap you measured.

The vision failure is real and only an M5 can show it. test-inkling2-media-metal .f32_nt got -0.40765, want -0.41413. The 83/83 vision figure was not measured on tensor units. I am not treating it as an M5 result. The two fixes you named, operand_cut or keeping that product in full fp32, stay on the vision slice. I will not pick one on this draft.

The 129-row token file is not in the branch and I will not invent it. The receipt that was hashed is 129 rows by 200058 FP32 logits, equal across a 128-row chunk and a 32-row chunk, SHA256 fbbb16ea5bf855e5b106ce740c32232ddb9ce7cd4f200792e3376fd4598956e7. The tests in the tree (test-inkling2-metal, test-inkling2-loop-metal, tf-kernel-contract --only inkling2) are the reproducible rows. If I regenerate that token file I will attach it here rather than describe a file I do not have.

@jschmied, the profile is the useful part. Plain c=1 is 99.4% GPU busy, 1.00 command buffer per token, so it is not launch-bound or CPU-bound. The attention block is 64% of GPU time at 47 GB/s, against 133 GB/s on the unembedding. The experts are not the gap. I am not going to rewrite them for speed on this draft. A fused GEMV for the projections that share an input is M1, which I asked for on #4274 and will not open until that claim is confirmed.

Admission: no 36 GB machine here, so I will not land a state-bytes check I cannot measure. Your note that the warning can arrive before or after admission, and that this machine can test a fix, is the reproduction. When a patch exists it goes on the draft for that run, not in ahead of it.

F1 is still @ashhart's decision. No change.

…w projections

- Score GEMMs are a full fp32 dot, off the tensor-unit operand cut, so an M5 matches the product measured on earlier chips.
- One-row projections that share an input use one launch of the existing matvec lane chains. Same checkpoint token hash, about 2 ms off a 57 ms decode token.
@BlivionIaG

Copy link
Copy Markdown
Contributor Author

@SashaMIT, the .f32_nt failure is addressed on 8b0bd3c2f4. Vision score products (f32_nt, f32_nn) are a full fp32 dot, one thread per output, not the tensor-unit op that keeps 10 mantissa bits. On this M4 Pro the GEMM suite and the tower's f64 reference pass. I have not run it on tensor units. Please retest test-inkling2-media-metal on the M5 Ultra. The bound was not widened.

Same head, stage split of one decode token, 22-token context, GPU time, cache off. Token hash 09a78850d723 matched before and after the projection fuse.

Stage ms
input mix 13.6
projections 7.8
attention kernel 2.2
output projection 2.8
post mix and route 14.8
experts 10.5
head 3.6

The attention kernel is not the gap on this chip. Fusing the one-row projections that share an input, with the same lane chains as the existing matvec, saved about 2 ms and did not change the hash. It does not meet the 25.26 bar. The two mix calls are about half the token, and rewriting that kernel's register use did not move them, so that rewrite is not in the tree.

@SashaMIT

Copy link
Copy Markdown
Contributor

M5 Ultra retest of 8b0bd3c2f4. Same Mac model and setup as the last run: Mac Studio Mac17,15, M5 Ultra (80-core GPU, tensor units), 512 GB, macOS 27.2 (26B118), Zig 0.17.0, Command Line Tools only. -Dcpu=apple_m1, original checkpoint ed40e7da. This run was on the second Studio, which is identical to the first. I ran it read-only and changed nothing in the tree.

  • Media: fixed. test-inkling2-media-metal gave 76 pass, 8 skip, 0 fail (84 total). At d7dd9c3140 it gave 74 pass, 8 skip, 1 fail (83 total).
    • "the tensor-unit GEMMs: every kind, whole and ragged tiles, K past and short of 256" now passes on tensor-unit hardware, .f32_nt and .f32_nn included, with the bound unchanged (5e-3 + 2e-3 * |want|). This test skips only when there is no Metal device, so it ran.
    • The 8 skips are the same count as before. I still have not mapped them, and this Mac still lacks the pinned libjpeg-turbo.
  • Kernel contract (tf-kernel-contract --only inkling2, -Doptimize=safe): 86/86 pass gates 1-4, 81/81 controls red (one more control than last time), 0 missed, 0 limit problems.
  • With the original checkpoint:
    • test-inkling2-metal 7/7, including drafted equals plain, with the fused one-row projections in place.
    • test-inkling2-loop-metal 36/36.
    • test-inkling2-host 30/32 with TF_INKLING2_CKPT. The same 2 skips as before.
  • Build: tf-kernel-contract and tf-inkling2-forward build clean at this head.
  • A gap in the contract: the contract's inkling2 media gemm f32_nt / f32_nn shape gates read "worst 0.000 of the bound" at both d7dd9c3140 and 8b0bd3c2f4 on this chip. So they passed even on the cut-operand path, and only the vision unit test caught it. Their inputs seem to fit in 10 mantissa bits. A control that cuts an fp32 operand to 10 bits, or inputs with low mantissa bits set, would let the contract catch this kind of change too.
  • Not run: decode speed, so I have no M5 number for the fused one-row projections (your ~2 ms of 57 ms). I can add it to the next served A-B-B-A block.

- zig fmt on the forward layer, and the score-GEMM note is one line so lean check stays at the base count.
- Drop the blank lines zig fmt --check rejected on the projection fuse.
@BlivionIaG

Copy link
Copy Markdown
Contributor Author

@SashaMIT, recorded. test-inkling2-media-metal 76 pass, 8 skip, 0 fail on the M5 Ultra, including .f32_nt and .f32_nn with the bound unchanged, is the result this fix was for. The 8 skips and the missing libjpeg-turbo stay as you left them.

The contract gap is real. inkling2 media gemm f32_nt / f32_nn reading "worst 0.000 of the bound" on both the cut-operand head and this one means those gates never saw a low mantissa bit. A control whose inputs do not fit in 10 fraction bits belongs on this draft. I will add it rather than treat the unit test as the only catch.

fmt and the lean check failed on 8b0bd3c2f4 (a blank-line drift and a two-line comment). Both are fixed on 65dacc7b78. Decode speed on the M5 is still yours to add to the next A-B-B-A if you run one. I will not treat the 2 ms projection fuse as a pass on that chip.

- f32_nt and f32_nn were filled from bf16 dyadics, so a 10-bit operand cut still matched the float64 product.
- Wide inputs now set the low fraction bits. On this M4 Pro the gates pass at 0.337 and 0.354 of the bound.
# Conflicts:
#	docs/receipts/inkling2-mini-m4pro/SHA256SUMS
…etal 4 prompt queue

- The recipe gains the decode token's bytes against its floor, the two byte-equal attempts that did not pay and the fusion estimate.
- Three decoding streams with a cold 4,096-token prompt arriving, A-B-B-A: the Metal 4 prompt queue is not worse for them, same prompt hash.
- The receipt gains mixed_load.py and its output.
@BlivionIaG

Copy link
Copy Markdown
Contributor Author

Another measured prompt-speed step on this branch (head 4418225fb6, M4 Pro): the prompt chunks of 64 rows or more now go through a Metal 4 queue (the existing TF_METAL4, which DeepSeek-V4.1 already uses; TF_METAL4=0 keeps Metal 3; a Mac without Metal 4 falls back). Same kernels, arguments and order.

  • First token, A-B-B-A, plain servers (A = 5e75f97db2): 8,192 tokens 25.2 / 24.9 s to 22.5 / 22.3 s (-10 %); 32,768 tokens 115.6 / 116.6 s to 105.4 / 102.4 s (-10 %). An earlier, loaded session gave -15 to -17 %.
  • Bytes equal: the four chat hashes, the 8,192-token (419cacb4c297) and 32,768-token (8a44904f45d6) cold hashes, and a DSpark server with four streams (--alone --serial equal) with a cold 4,096-token prompt in flight, on both builds. Contract 86 kernels / 133 controls red / 0 missed / 0 limit problems.
  • With decoding streams running: three greedy streams and a cold 4,096-token prompt arriving at 8 s: the prompt took 13.9 and 15.8 s on Metal 3 and 13.2 and 13.0 s on Metal 4, and the streams' longest gap was 12.8 and 15.6 s against 11.7 and 11.6 s. Decode itself stays on the Metal 3 queue and did not change.
  • Where a 512-row chunk's time goes (a stamp between every two dispatches; the stage spans add to the first token, 21.7 s of 21.9 s at 8,192 tokens): routed experts 857 ms of 1,281 at the start of a prompt (floor 406), dense projections 215 (floor 172), sliding attention 112, loop gates 47, global attention 18 growing to 449 at 31,744 tokens (about 240 ms over its floor). The growth of the prefill cost with prompt length is global attention. I found no byte-equal attention change.
  • Decode token roofline (one stream, 1,560 dispatches, 31.0 ms GPU span): the weights a token reads are 5.49 GB, 21.95 ms at 250 GB/s; the dense matvecs run at 248 to 263 GB/s; routed experts and router are each about 1.7 ms over; the rest is about 1,200 small latency-bound dispatches. Two byte-equal attempts (16-byte-load matvecs, fewer one-group launches) were not faster. Details in the recipe.

The MLX comparison table in the description predates this; I will re-run the sweep and update it.

….75 to 0.83 of its rate

- Quiet machine, native then MLX, one run per size; native 5.18 / 21.59 / 97.05 / 225.97 s, MLX 4.32 / 17.04 / 75.55 / 169.70 s at 2,048 / 8,192 / 32,768 / 65,536 tokens.
- The recipe, the ledger and known-issues carry the new ratio; the receipt gains the raw JSON and server logs.
- q k^T's epilogue stores bf16(bf16(sum) * scale), the softmax reads and writes bf16 with the same max, exps and sum order, p v reads the bf16 probabilities.
- Every value the score matrix held between the kernels was already a bf16 number, so the bytes are the same and the DRAM traffic of the softmax (at its fp32 floor) halves.
- The gemm kinds f32_nt / f32_nn become score_nt / prob_nn; the softmax probe takes bf16 scores; a planted control drops the score scale (134 controls).
- Recipe rows, receipt README and SHA256SUMS, the softmax/bf16-score receipt (bench harness, A-B-B-A, tower hashes, contract totals), CHANGELOG.
…le control, the tower's init kinds

- Completes the previous commit: records for score_nt / prob_nn, the suite registers the control that drops the score scale, the tower loads the renamed kinds.
…logs) and its checksums

- img-sm-* and hash-sm-* files named in the receipt README; SHA256SUMS regenerated (shasum -a 256 -c passes).
@BlivionIaG

Copy link
Copy Markdown
Contributor Author

One more measured step on the image path (head 602bf873c0, M4 Pro): the image tower's score matrix between q.k^T, the softmax and p.v is now stored as bf16 instead of fp32. Every value it held between the three kernels was already a bf16 number, and the softmax was already at 1.06 to 1.10 times its bandwidth floor (read plus write of the 100 MB matrix at 250 GB/s), so the lever was halving its bytes. The max, the exps, the sum order and each p.v output's fma chain over k are unchanged.

  • First token, A-B-B-A (load average 2.9 before and after): the 1,561-token invoice 6.94 / 6.94 s to 6.54 / 6.53 s (-5.8 %), PNG and JPEG alike; the 546-token scene 1.96 to 1.93 s, which is inside the noise. This sits on the earlier tile change (15.7 to 16.2 s down to 6.9 s).
  • Bytes equal: a standalone comparison on 7 ragged and full shapes (scores, probabilities and p.v outputs: 0 differ), the tower's output hash on four sessions over 7 images, all replies including usage, and the four text hashes (0b037128ee78 b37d8a0e39c4 77bf7c99cc66 538175732cc7).
  • Contract: 86 kernels pass, 134 controls red as planted (one new, for the dropped score scale), 0 missed, 0 limit problems.
  • Tried and dropped: a one-simdgroup-per-row softmax (bit-equal, no faster) and fusing the exp into p.v's operand fetch (bit-equal, slower: 2.64 ms against 2.16 ms for the separate pair).

What is left in the invoice's 6.5 s is about 3.7 s of text prefill and about 2.8 s of tower.

…and a byte-equal rewrite that stays a tie

- A visit at 31,744 keys runs at 3.47 TFLOP/s; the score phase's operand preparation is the excess.
- A byte-equal variant ran the visit in 0.82 of the time (about 2.5 % of the 32K first token) and was not applied.
@BlivionIaG
BlivionIaG marked this pull request as ready for review November 23, 2026 21:44
… a drafted request resumes it

- The head has no recurrence, but its K/V at a position come from the target tap rows, which a kept state does not hold.
  So the head context is copied beside the lane snapshot: the global layer for all positions (4 KiB each) and the two
  sliding layers' last 1,024 (8 MiB); about 7 % more per state at 3,000 tokens. The last feature row is not kept.
- A kept state with no head (a "draft": false stream, a warm-pass end) resumes a drafted request with the head starting
  past the prefix: same tokens, fewer accepted drafts.
- --prompt-cache-gib with DSpark is accepted; the default budget with drafts is half the room, as on plain servers.
- Tests: adapter save/restore equals the source head (past the 1,152 ring, pending rows drained); on the real checkpoint
  a resumed drafted stream equals a fresh one in tokens and draft counters, greedy and seeded, and equals draft:false.
  Without the head restore the real-checkpoint test fails (expected 17, found 16 drafted); without the lane copy the
  unit test fails on the drafted tokens.
…ken, decode, concurrency) and clients

- Raw results behind docs/receipts/inkling2-mini-m4pro/drafted-resume/summary.txt, the recipe's Kept prompt states section
  and the first commit's CHANGELOG, Limits and known-issues text.
…metal-4bit

# Conflicts:
#	CHANGELOG.md
#	zig/src/platform/environment.zig
…metal-4bit

# Conflicts:
#	CHANGELOG.md
#	docs/optimization-ledger.md
- The merge of main left the table twice (ours with the ink2 column, main's without); main's row 39 is added to ours.
@BlivionIaG

Copy link
Copy Markdown
Contributor Author

CI result for 7f4d175c3d: all 10 checks passed (fmt, pytest, zig, zig-b, zig-windows, zig-macos-dev-tools, benchmark-clients on Ubuntu and Windows, release-paths, comfyui-moss-installer). main had moved while the run was queued, so the head is now 37f8b61c03 (a merge of main; the conflicts were a changelog bullet and two docs/optimization-ledger.md hunks) and CI is running again on it. Nothing else changed since the passing run except that merge.

@SashaMIT

Copy link
Copy Markdown
Contributor

@BlivionIaG, the rerun with #4744 on the M5 Ultra. Yes, it recovers here too, and DSpark now wins at four code streams.

Base is this branch at 7f4d175c3d; "with" is the same commit plus #4744 at eaa9e41a6a (the only conflict was CHANGELOG.md, both bullets kept). Same flags as before: --temperatures 0.7 --seed 1234 --alone --serial --levels 1,4 --tokens 256 --reps 3, server --parallel 4 --prompt-cache-gib 0, a fresh server per block, quiet Mac. Both PRs have moved on since; happy to rerun at the new heads if that helps.

M5 Ultra, T=0.7 1 stream, decode tok/s: base with #4744 4 streams, aggregate tok/s: base with #4744
plain, code 34.8 57.5 / 57.3 51.0 107.9 / 107.5
plain, chat 35.0 57.2 / 57.1 51.4 107.9 / 107.6
DSpark, code 33.4 62.0 34.5 139.6
DSpark, chat 34.4 65.4 50.0 140.2
  • At four code streams, DSpark goes from 34.5 to 139.6 tok/s (plain with lanes: the host GPU-rule sampler keeps its best 1,024 candidates in a bounded heap, not a full sort #4744: 107.9), and its p95 round gap from 397 ms to 69 ms.
  • One plain sampled stream (57.5) now matches our earlier greedy control (57.7 at 9a255bb79e).
  • --alone and --serial: all equal in every cell, base and with, 0 failed, timings valid. The plain rows' serial check is "vacuous" (no drafts), as before.
  • Plain "with" is two blocks (both shown); plain base and both DSpark columns are one block each.

@BlivionIaG

Copy link
Copy Markdown
Contributor Author

@SashaMIT thank you, that is exactly the check I wanted: independent hardware, a fresh server per block, --alone --serial equal, and the same direction as on the M4 Pro (plain sampled 21.2 to 29.2 at one stream and 33.5 to 56.1 at four; DSpark sampled code 19.9 to 29.4 and 25.9 to 52.9, which there stays about 6 % under plain at four streams, where your M5 Ultra goes past it). I added your table to #4744's description with the commits and flags you gave. No rerun needed: both heads have only moved by merges of main.

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.

4 participants