Repository navigation
inkling2 metal: unlock experts and loop-gates MMA on Apple M5 - #5144
Conversation
|
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.
|
|
Completed the current- 1. Threshold-Activating Prefill & Reused-CB Cross-Comparison (
|
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):0mismatches acrosspt=16,29,37,357, and2064.resumed == fresh(--prompt-cache-gib 4Turn 2pt=2125,cached_tokens=2048+77uncached >16-row suffix tokens vs cold--prompt-cache-gib 0 --snapshot-dir none --max-snapshots 0): both produce3795b5c9e36c915e(0mismatches).concurrent == solo(c=4vsc=1):0mismatches across every cell (M3U main,M3U v5-2a,M5U main,M5U v5-2a plain,M5U v5-2a dspark).
|
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.
|
- 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.
8355442 to
4a304a7
Compare
|
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. |
What this changes
Before:
experts.Pipelines.loadandloop.Pipelines.loadgatedexperts_mma(mma_rows = 16) andloop_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_mmainexperts_mma.metalomitted a trailingthreadgroup_barrier(mem_flags::mem_threadgroup)afterink2_mma_run, creating a stage-loop staging race under reused/multi-layer command buffers.After:
!device.tensorUnits()guards inexperts.Pipelines.load,loop.Pipelines.load, and their contract suites so Apple M5 GPUs run the0-ULP bit-exact simdgroup-matrixexperts_mma(>= 16rows) andgates_mma(>= 8rows) kernels alongside pre-M5 GPUs.threadgroup_barrier(mem_flags::mem_threadgroup);afterink2_mma_run(...)inzig/kernels/metal/inkling2/experts_mma.metal(matchingloop_mma.metal) while keeping double-buffered staging.maxThreads()validation inexperts.Pipelines.loadMma(128group /256experts) andloop.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).dense_mmagated behind!device.tensorUnits(), as M5 microbenchmarks showedsp_matvec_*outperformingdense_mmaacross tested widths (0.44x–0.71xon a 7-projection visit).V5-2a).origin/main(8f11b41ba0758821ebfd224fcccf664dcbddd5fc), 1 commit (83554426f1d8ffa2c6743cda833af23c337bc791, 8 changed files,+32/-13).git merge-tree --write-tree origin/main HEAD:762d89e56241654e472f5b36cbd007623b7e9714(clean merge against latestorigin/main).Checks on this head
83554426f1d8ffa2c6743cda833af23c337bc791python3 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/.metalfiles<= 427lines (experts_mma.metal217,experts.zig289,forward.zig427,loop.zig115,metal_inkling2_gates_mma.zig184,metal_inkling2_mma.zig297,metal_inkling2_mma_controls.zig74,metal_mapping_test.zig93).zig fmt --checkandgit diff --check origin/main..HEADexit0.Zig 0.17.0,-Dcpu=apple_m1, Apple M5 Maxapplegpu_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-hostexited0../zig-out/bin/tf-kernel-contract --only "metal inkling2"on Apple M5 Maxapplegpu_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(plus0/200and0/240stress checks on M5 Max). Prior Apple M3 Ultra (applegpu_g15) contract run on83554426f1:experts(17/17,30controls,0limits) andloop(3/3,4controls,0limits).pmset -g thermpre/post every served A-B-B-A leg across all three Macs): nominal throughout.Receipt
origin/main8f11b41ba0758821ebfd224fcccf664dcbddd5fcvs83554426f1d8ffa2c6743cda833af23c337bc791;bench_concurrent.py --alone --serial --levels 1,4 --temperatures 0,1 --tokens 128 --reps 2 --seed 42and coldprefillL=2048,4096;0cross-legtoken_sha256mismatches across all 78 cells on all 3 Macs):--no-drafts --parallel 4):3reps):L=2048(pt=2077)451.4 -> 875.8tok/s (+94.00%, TTFT4.694 -> 2.465s);L=4096(pt=4129)451.4 -> 860.4tok/s (+90.61%, TTFT9.261 -> 4.909s).3reps):L=2048(pt=2077)145.9 -> 308.1tok/s (+111.18%, TTFT14.358 -> 6.877s);L=4096(pt=4129)143.4 -> 300.5tok/s (+109.52%, TTFT28.970 -> 13.958s).2reps):L=2048(pt=2077)580.2 -> 572.3tok/s (-1.37%, TTFT3.698 -> 3.754s);L=4096(pt=4129)568.0 -> 558.1tok/s (-1.73%, TTFT7.431 -> 7.572s) (reflecting the added post-MMA staging barrier inexperts_mma.metal).--no-drafts --parallel 4):-0.18%..+0.44%aggregate decode (34.05..56.30tok/s);chatTTFT-16.71%..-22.20%(c=4:0.639 -> 0.497satT=0,0.635 -> 0.502satT=1).-1.54%..0.00%aggregate decode (c=4-0.51%..0.00%);chatTTFT-26.18%..-29.08%(c=4:1.324 -> 0.939satT=0,1.301 -> 0.940satT=1).-0.38%..+2.29%aggregate decode (c=40.00%..+0.24%).--drafter {TARGET} --parallel 4):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%(chatTTFT-14.36%..-24.04%).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%; onchatc=4,T=0measures-6.78%aggregate (51.65 -> 48.15,e2e 46.04 -> 44.54-3.26%, TTFT1.290 -> 0.942s-27.02%,round_gap 83.40 -> 89.84ms) andT=1-3.30%aggregate (44.00 -> 42.55,e2e 39.91 -> 39.77-0.35%,round_gap 130.35 -> 121.98ms, TTFT1.292 -> 0.936s-27.52%) due to lowershared_costskeepinglanesnear the DSpark probe threshold on 20-core M5 Pro.-1.90%..+0.25%.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.83554426f1d8ffa2c6743cda833af23c337bc791onorigin/main8f11b41ba0758821ebfd224fcccf664dcbddd5fc, Zig0.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.>= 16rows) and loop-gates (>= 8rows) on Apple M5 GPUs, plus theexperts_mma.metalstage-loop barrier across all Metal GPUs; non-Apple GPU backends untouched.0-ULP bit-exact vs scalar references across72kernels (87controls red,0limits) on Apple M5 Max andexperts(17/17) +loop(3/3) on Apple M3 Ultra;0cross-legtoken_sha256mismatches across all78served A-B-B-A cells across all 3 Macs.c=4-0.51%..+0.44%,chatTTFT-16.71%..-29.08%); DSparkc=4+14.17%..+28.81%on M5 Ultra,-6.78%..+2.81%on M5 Pro,-1.90%..+0.25%on M3 Ultra.+94.00%/+90.61%on M5 Ultra,+111.18%/+109.52%on M5 Pro,-1.37%/-1.73%on M3 Ultra.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.