Skip to content

inkling2 metal: unlock experts and loop-gates MMA on Apple M5 - #5144

Merged
ashhart merged 2 commits into
ashhart:mainfrom
gilby:v5-2a-m5-mma
Nov 26, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
gilby:v5-2a-m5-mma

Conversation

@gilby

@gilby gilby commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

What this changes

Before: experts.Pipelines.load and loop.Pipelines.load gated experts_mma (mma_rows = 16) and loop_gates_mma (mma_rows = 8) behind !device.tensorUnits(), forcing Apple M5 GPUs onto the per-row scalar / un-blocked paths even for wide prefill and multi-stream decode batches. Additionally, ink2_mma in experts_mma.metal omitted a trailing threadgroup_barrier(mem_flags::mem_threadgroup) after ink2_mma_run, creating a stage-loop staging race under reused/multi-layer command buffers.

After:

  • Removes the !device.tensorUnits() guards in experts.Pipelines.load, loop.Pipelines.load, and their contract suites so Apple M5 GPUs run the 0-ULP bit-exact simdgroup-matrix experts_mma (>= 16 rows) and gates_mma (>= 8 rows) kernels alongside pre-M5 GPUs.
  • Adds the missing trailing threadgroup_barrier(mem_flags::mem_threadgroup); after ink2_mma_run(...) in zig/kernels/metal/inkling2/experts_mma.metal (matching loop_mma.metal) while keeping double-buffered staging.
  • Adds maxThreads() validation in experts.Pipelines.loadMma (128 group / 256 experts) and loop.Pipelines.loadMma (256), plus launch-map coverage (metal_mapping_test.zig) for all 7 Inkling2 MMA records and a host source-control test (metal_inkling2_mma_controls.zig).
  • Keeps dense_mma gated behind !device.tensorUnits(), as M5 microbenchmarks showed sp_matvec_* outperforming dense_mma across tested widths (0.44x–0.71x on a 7-projection visit).

Checks on this head

  • Head commit: 83554426f1d8ffa2c6743cda833af23c337bc791
  • Lean & structure checker, python3 tools/pr_check.py --base origin/main: 0 error(s), 4 warning(s) (advisory --pr / inherited cycles; lean 164 vs 164, 0 new; import cycle edges 24 vs 24, 0 new). All touched .zig/.metal files <= 427 lines (experts_mma.metal 217, experts.zig 289, forward.zig 427, loop.zig 115, metal_inkling2_gates_mma.zig 184, metal_inkling2_mma.zig 297, metal_inkling2_mma_controls.zig 74, metal_mapping_test.zig 93).
  • Formatting & diff cleanliness: zig fmt --check and git diff --check origin/main..HEAD exit 0.
  • Host & compile checks (Zig 0.17.0, -Dcpu=apple_m1, Apple M5 Max applegpu_g17): zig build test-inkling2-host test-inkling2-metal-host test-inkling2-controls-host test-inkling2-launch-map-host test-kernel-contract compile-inkling2-native-host exited 0.
  • Kernel contract suite (./zig-out/bin/tf-kernel-contract --only "metal inkling2" on Apple M5 Max applegpu_g17): SUMMARY 72 kernels: 72 pass gates 1-4, 0 fail, 0 known findings; 87 controls: 87 red as planted, 0 missed; 0 limit problems; gates 16513 ms, limits 663 ms (plus 0/200 and 0/240 stress checks on M5 Max). Prior Apple M3 Ultra (applegpu_g15) contract run on 83554426f1: experts (17/17, 30 controls, 0 limits) and loop (3/3, 4 controls, 0 limits).
  • Thermals (pmset -g therm pre/post every served A-B-B-A leg across all three Macs): nominal throughout.

Receipt

  • Main against branch: A-B-B-A served runs (origin/main 8f11b41ba0758821ebfd224fcccf664dcbddd5fc vs 83554426f1d8ffa2c6743cda833af23c337bc791; bench_concurrent.py --alone --serial --levels 1,4 --temperatures 0,1 --tokens 128 --reps 2 --seed 42 and cold prefill L=2048,4096; 0 cross-leg token_sha256 mismatches across all 78 cells on all 3 Macs):
    • Cold Prefill (--no-drafts --parallel 4):
      • Apple M5 Ultra 512 GiB (3 reps): L=2048 (pt=2077) 451.4 -> 875.8 tok/s (+94.00%, TTFT 4.694 -> 2.465s); L=4096 (pt=4129) 451.4 -> 860.4 tok/s (+90.61%, TTFT 9.261 -> 4.909s).
      • Apple M5 Pro 64 GiB (3 reps): L=2048 (pt=2077) 145.9 -> 308.1 tok/s (+111.18%, TTFT 14.358 -> 6.877s); L=4096 (pt=4129) 143.4 -> 300.5 tok/s (+109.52%, TTFT 28.970 -> 13.958s).
      • Apple M3 Ultra 512 GiB (2 reps): L=2048 (pt=2077) 580.2 -> 572.3 tok/s (-1.37%, TTFT 3.698 -> 3.754s); L=4096 (pt=4129) 568.0 -> 558.1 tok/s (-1.73%, TTFT 7.431 -> 7.572s) (reflecting the added post-MMA staging barrier in experts_mma.metal).
    • Plain Decode (--no-drafts --parallel 4):
      • Apple M5 Ultra 512 GiB: -0.18%..+0.44% aggregate decode (34.05..56.30 tok/s); chat TTFT -16.71%..-22.20% (c=4: 0.639 -> 0.497s at T=0, 0.635 -> 0.502s at T=1).
      • Apple M5 Pro 64 GiB: -1.54%..0.00% aggregate decode (c=4 -0.51%..0.00%); chat TTFT -26.18%..-29.08% (c=4: 1.324 -> 0.939s at T=0, 1.301 -> 0.940s at T=1).
      • Apple M3 Ultra 512 GiB: -0.38%..+2.29% aggregate decode (c=4 0.00%..+0.24%).
    • DSpark (--drafter {TARGET} --parallel 4):
      • Apple M5 Ultra 512 GiB: c=4 +28.81% (code T=0, 107.25 -> 138.15), +23.51% (chat T=0, 79.95 -> 98.75, TTFT -17.60%), +14.17% (code T=1, 53.30 -> 60.85), +16.81% (chat T=1, 75.85 -> 88.60, TTFT -22.14%); c=1 -0.92%..+1.30% (chat TTFT -14.36%..-24.04%).
      • Apple M5 Pro 64 GiB: c=1 +0.82%..+2.81% (e2e +1.47%..+2.95%), code T=1 c=4 +1.46% (e2e +1.41%), code T=0 c=4 -0.42%; on chat c=4, T=0 measures -6.78% aggregate (51.65 -> 48.15, e2e 46.04 -> 44.54 -3.26%, TTFT 1.290 -> 0.942s -27.02%, round_gap 83.40 -> 89.84ms) and T=1 -3.30% aggregate (44.00 -> 42.55, e2e 39.91 -> 39.77 -0.35%, round_gap 130.35 -> 121.98ms, TTFT 1.292 -> 0.936s -27.52%) due to lower shared_costs keeping lanes near the DSpark probe threshold on 20-core M5 Pro.
      • Apple M3 Ultra 512 GiB: -1.90%..+0.25%.
  • Boxes it reaches: zig/kernels/metal/inkling2/experts_mma.metal, zig/src/families/inkling2/metal/{experts,forward,loop}.zig, zig/tests/contract/{metal_inkling2_gates_mma,metal_inkling2_mma,metal_inkling2_mma_controls,metal_mapping_test}.zig.
  • New switch? No.
  • Environment: Commit 83554426f1d8ffa2c6743cda833af23c337bc791 on origin/main 8f11b41ba0758821ebfd224fcccf664dcbddd5fc, Zig 0.17.0 -Dcpu=apple_m1 -Doptimize=ReleaseFast, Apple M5 Max (applegpu_g17), Apple M5 Ultra 512 GiB, Apple M5 Pro 64 GiB, Apple M3 Ultra 512 GiB; checkpoint Inkling-2-Mini.
  • Paths and platforms the change reaches: Metal Inkling 2 Mini routed-experts (>= 16 rows) and loop-gates (>= 8 rows) on Apple M5 GPUs, plus the experts_mma.metal stage-loop barrier across all Metal GPUs; non-Apple GPU backends untouched.
  • Exactness: 0-ULP bit-exact vs scalar references across 72 kernels (87 controls red, 0 limits) on Apple M5 Max and experts (17/17) + loop (3/3) on Apple M3 Ultra; 0 cross-leg token_sha256 mismatches across all 78 served A-B-B-A cells across all 3 Macs.
  • Decode speed before and after: Plain decode unchanged on M5 Ultra/M5 Pro/M3 Ultra (c=4 -0.51%..+0.44%, chat TTFT -16.71%..-29.08%); DSpark c=4 +14.17%..+28.81% on M5 Ultra, -6.78%..+2.81% on M5 Pro, -1.90%..+0.25% on M3 Ultra.
  • Prompt speed before and after: Cold prefill +94.00%/+90.61% on M5 Ultra, +111.18%/+109.52% on M5 Pro, -1.37%/-1.73% on M3 Ultra.
  • Licence of any new weights or drafter: n/a.

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.
[✓] 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 still goes through the lane rounds. No serial path, and nothing that needs drafts off.
[✓] Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo.
[✓] No precision traded for speed.
[✓] Decode and prompt processing are no slower than main at one stream and the widest admitted; with --drafter, no slower than without it, greedy and sampled.

@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thanks, @gilby. This looks correct and merges cleanly on current main. The M5 experts and loop-gates gates now reach the intended row thresholds, the launch limits match the kernels, dense MMA stays gated, and the new post-MMA barrier protects the next staging iteration. The arithmetic controls and barrier check can fail, and dense behavior is unchanged.

The smallest remaining check is on the pinned Inkling-2-Mini checkpoint: a full 16-row, multi-layer M5 prefill under reused command buffers, covering both NVFP4 and MXFP8 expert MMA and loop-gates MMA. Compare output bytes with the M3/reference path, then confirm drafted equals plain, resumed equals fresh and concurrent equals solo on the threshold-activating cases.

  • Synthena.

@gilby

gilby commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Completed the current-main (67ae36c4dc) multi-layer Apple M5 vs Apple M3 Ultra exactness receipt for PR #5144 (83554426f1) using pinned Inkling-2-Mini-ed40e7da (NVFP4 layers 0..19 + MXFP8 layers 20..23 + loop gates) and its bundled DSpark drafter under reused command buffers (T=0.0, seed=42, max_tokens=64):

1. Threshold-Activating Prefill & Reused-CB Cross-Comparison (M5U v5-2a vs M5U main vs M3U main)

Prompt Slice (prompt_tokens) Mode Apple M3 Ultra main (67ae36c4dc) Apple M3 Ultra v5-2a Apple M5 Ultra main (67ae36c4dc) Apple M5 Ultra v5-2a (plain & dspark) c=4 == solo
Exact 16 tokens (pt=16, /v1/completions, reused CB) plain / dspark cb9fa3fef528 — cb9fa3fef528 cb9fa3fef528 4/4 equal
16 raw / 29 chat-templated tokens (pt=29) plain / dspark 992f7be00dda — 992f7be00dda 992f7be00dda 4/4 equal
Multi-layer 37-token prompt (pt=37, chat) plain / dspark 40f87062f812 40f87062f812 40f87062f812 40f87062f812 4/4 equal
Code-edit 357-token prompt (pt=357, chat) plain / dspark 2cabdc5daa4e 2cabdc5daa4e (0.7277s TTFT) 2cabdc5daa4e (0.9079s TTFT) 2cabdc5daa4e (0.4940s plain, 0.4917s dspark) 4/4 equal
Long prefill (pt=2064, chat, >1K global prompt attn) plain / dspark d3bd50850f44 (561.7 tok/s) d3bd50850f44 (556.6 tok/s) 7fc59835c87e (440.1 tok/s, 4.719s TTFT) 7fc59835c87e (844.4 tok/s, 2.460s TTFT, +91.9%) 4/4 equal

(Note: at pt=2064 > 1024, M5 Ultra main and M5 Ultra v5-2a match each other bit-for-bit at 7fc59835c87e; the difference from M3 Ultra main at > 1024 tokens is already present in origin/main 67ae36c4dc from M5 NAX vs M3 TF_SIMD_FRAGS global tf_ink2_attn_prompt, not introduced by #5144. All threshold-activating >= 16-row cases below the 1024 global prompt-attention cutoff — pt=16, 29, 37, 357 — match Apple M3 Ultra bit-for-bit.)

2. Drafted == Plain, Resumed == Fresh, Concurrent == Solo (Apple M5 Ultra, v5-2a)

  • drafted == plain (--drafter {TARGET} vs --no-drafts): 0 mismatches across pt=16, 29, 37, 357, and 2064.
  • resumed == fresh (--prompt-cache-gib 4 Turn 2 pt=2125, cached_tokens=2048 + 77 uncached >16-row suffix tokens vs cold --prompt-cache-gib 0 --snapshot-dir none --max-snapshots 0): both produce 3795b5c9e36c915e (0 mismatches).
  • concurrent == solo (c=4 vs c=1): 0 mismatches across every cell (M3U main, M3U v5-2a, M5U main, M5U v5-2a plain, M5U v5-2a dspark).

@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thank you, @gilby. Our M5 Ultra A/B on this head (Inkling 2 Mini, base main, base-new-new-base) is green: every prompt token is equal across runs, 2,048-token prompts go from 451 to 894 tok/s at offsets 0, 5,000 and 10,000, and 1- and 4-stream decode is level (-0.2% to +0.5%, within repeat noise) on chat and code at temperatures 0 and 1. It is in a landing train now.

  • Synthena

gilby and others added 2 commits November 26, 2026 12:26
- Remove the !device.tensorUnits() load guards in experts.Pipelines.load
  and loop.Pipelines.load and their contract suites so Apple M5 GPUs use
  the 0-ULP bit-exact simdgroup-matrix routed-experts (from 16 rows) and
  loop-gates (from 8 rows) kernels alongside pre-M5 GPUs.
- Fix a stage-loop threadgroup staging race in ink2_mma (experts_mma.metal)
  by adding a trailing threadgroup_barrier(mem_flags::mem_threadgroup) after
  ink2_mma_run, matching loop_mma.metal's post-MMA barrier invariant and
  eliminating intermittent corruption on Apple M5 under reused/multi-layer
  command buffers while keeping double-buffered staging (faster than single-
  buffered on Apple M5 Max mixed 24-layer visits).
- Keep dense_mma gated to !device.tensorUnits(): Apple M5 Max microbenchmarks
  showed sp_matvec_* outperforms dense_mma across all tested widths (0.44x-0.71x
  on a full 7-projection visit), while experts_mma delivers 3.28x (NVFP4) and
  5.11x (MXFP8) at 256 rows and gates_mma delivers 1.74x at 8 rows and 2.62x
  at 256 rows.
- Add maxThreads() load guards to experts.Pipelines.loadMma and
  loop.Pipelines.loadMma matching each kernel's reserved thread count.
- Add Metal launch map coverage for all seven Inkling2 MMA records and a
  host control test checking the post-MMA barrier in experts_mma.metal.
@ashhart
ashhart merged commit e209630 into ashhart:main Nov 26, 2026
2 of 12 checks passed
@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thank you @gilby. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

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.

2 participants