Repository navigation
glm metal: vectorize pre-M5 Metal prompt, KDA, and indexer kernels - #3921
Conversation
|
Thank you @gilby. The pre-M5 fast path is scoped to GLM and to Macs without tensor units, with the existing kernels and non-GLM families unchanged. Your M3 Ultra and M5 Ultra receipts and the re-run checks cover what we need; nothing further is required from you here.
|
|
[Joshua's agent] Re-reading this thread end to end in a handover: the 07:28Z review asked nothing further from the author side, and nothing in the thread or the diff since changes that. One state note: the branch now shows DIRTY against current |
|
Thank you @gilby. Our last review asked nothing further, and nothing in the thread or the current head changes that: the fast path stays scoped to GLM on Macs without tensor units, the M5 and non-GLM paths are untouched, and your served receipts on both Macs show no token differences with faster cold prompts (5.17% to 5.68% from 2K to 64K tokens). Main has moved since your branch was cut: the GLM index kernel was rewritten, so the dispatch and key-list changes need re-applying, and the touched files must stay at or below 600 lines. We will do that rebase on our side, keeping your authorship, and check it on a Mac before it lands. If main's rewrite turns out to need a different design for your change, we will say so here.
|
|
I checked For the maintainer rebase, I verified the newer indexer check on current main Igor Lessio - AIFLOW LABS |
3117235 to
e771162
Compare
|
I reran the selector check after the rebase. Head zig build tf-glm-prompt-select -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j3 --summary all
./zig-out/bin/tf-glm-prompt-select43/43 tests passed. The synthetic indexer produced identical scores and selected key lists for all eight positions from 2,048 to 120,000, using 256 rows per position. The earlier M3 Ultra run in the PR description covers pre-M5 hardware on its recorded revision. On the later head On the combined #4279 train at zig build test-flashnext-xpu-host test-qwen27-config-xpu-host test-qwen27-prompt-tail-xpu-host test-xpu-refusal-host test-glm-kernel-ownership-host test-glm-window-contract-host -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j3 --summary all35/35 host tests passed. The selector command above again matched scores and lists at all eight positions, and the benchmark fixture suite passed 10/10. These are combined-train host checks. Device coverage remains separate. The revised head Igor Lessio - AIFLOW LABS |
e771162 to
f92572f
Compare
f92572f to
c4f282c
Compare
c4f282c to
a492e93
Compare
a492e93 to
42f4bf5
Compare
42f4bf5 to
0143fe8
Compare
Optimize GLM-5.3-Flash Metal prompt matmul, KDA prep/scan/post, and indexer kernels on !tensorUnits() Apple Silicon GPUs via GLM-scoped TF_GLM_M3_FAST paths and 32-row affine_mm tiling, keeping M5 tensor-unit paths and output tokens bit-identical. Refs ashhart#3152.
Create only grouped pipelines from the combined library before M5, leaving the affine pipelines owned by the separate library. Exercise the actual loader and destructor on both capability paths without a device.
…fter main's ashhart#3920 Format only: the unabsorb strides go back inline and the limits gate is one line. Nothing else changes.
…alf tiles metal_main.zig grows with every family and sat over 600 lines with main; the nine glm_m3_fast registrations join the list as affine_k.glm_m3, as the half tiles already do. Order and names are unchanged apart from where they sit.
0143fe8 to
61d1c0a
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. |
…r adds #3921 made kernels.zig put `#define TF_GLM_M3_FAST 1` before the KDA prompt passes and glm_index_nax.metal on a GPU without tensor units. The KDA and index contracts still built those texts without it, so on an M3 the loader check refused them ("GLM contract text is not one the loader compiles") and the whole GLM contract stopped before its first case. Both texts now follow the loader, and gate 5's pre-M5 texts carry the define too.
…oes; its record declares 18432 bytes On a GPU without tensor units the case compiles glm_index_nax.metal with TF_GLM_M3_FAST (#3921, #4397), whose threadgroups cover 32 rows with 4 simdgroups, but encode launched it as on M5: scoreRows' 64 or 128 rows a group and 2 x rows threads. Rows past 32 x the groups' count were never written (256 x 128 x 16384: rows 64 on, byte 1065728), so the shapes gate compared unwritten floats against main's scores. The case now launches it as selectKeys does (glm/prompt.zig: 32 rows, 128 threads); rows it wrote matched main's bytes already, and the kernel is unchanged. Before M5 the planted control moves into the pre-M5 branch (both simdgroups of a row pair on the same 32 blocks), since the M5 branch it edited is not compiled there. The pre-M5 kernel stages 128 x 72 bf16 (18432 bytes) of keys; the record declared 17408 (the M5 tile, 64 x 136), so gate 5's limit now declares 18432.
…r adds #3921 made kernels.zig put `#define TF_GLM_M3_FAST 1` before the KDA prompt passes and glm_index_nax.metal on a GPU without tensor units. The KDA and index contracts still built those texts without it, so on an M3 the loader check refused them ("GLM contract text is not one the loader compiles") and the whole GLM contract stopped before its first case. Both texts now follow the loader, and gate 5's pre-M5 texts carry the define too.
…oes; its record declares 18432 bytes On a GPU without tensor units the case compiles glm_index_nax.metal with TF_GLM_M3_FAST (#3921, #4397), whose threadgroups cover 32 rows with 4 simdgroups, but encode launched it as on M5: scoreRows' 64 or 128 rows a group and 2 x rows threads. Rows past 32 x the groups' count were never written (256 x 128 x 16384: rows 64 on, byte 1065728), so the shapes gate compared unwritten floats against main's scores. The case now launches it as selectKeys does (glm/prompt.zig: 32 rows, 128 threads); rows it wrote matched main's bytes already, and the kernel is unchanged. Before M5 the planted control moves into the pre-M5 branch (both simdgroups of a row pair on the same 32 blocks), since the M5 branch it edited is not compiled there. The pre-M5 kernel stages 128 x 72 bf16 (18432 bytes) of keys; the record declared 17408 (the M5 tile, 64 x 136), so gate 5's limit now declares 18432.
…r adds #3921 made kernels.zig put `#define TF_GLM_M3_FAST 1` before the KDA prompt passes and glm_index_nax.metal on a GPU without tensor units. The KDA and index contracts still built those texts without it, so on an M3 the loader check refused them ("GLM contract text is not one the loader compiles") and the whole GLM contract stopped before its first case. Both texts now follow the loader, and gate 5's pre-M5 texts carry the define too.
…oes; its record declares 18432 bytes On a GPU without tensor units the case compiles glm_index_nax.metal with TF_GLM_M3_FAST (#3921, #4397), whose threadgroups cover 32 rows with 4 simdgroups, but encode launched it as on M5: scoreRows' 64 or 128 rows a group and 2 x rows threads. Rows past 32 x the groups' count were never written (256 x 128 x 16384: rows 64 on, byte 1065728), so the shapes gate compared unwritten floats against main's scores. The case now launches it as selectKeys does (glm/prompt.zig: 32 rows, 128 threads); rows it wrote matched main's bytes already, and the kernel is unchanged. Before M5 the planted control moves into the pre-M5 branch (both simdgroups of a row pair on the same 32 blocks), since the M5 branch it edited is not compiled there. The pre-M5 kernel stages 128 x 72 bf16 (18432 bytes) of keys; the record declared 17408 (the M5 tile, 64 x 136), so gate 5's limit now declares 18432.
What this changes
On pre-M5 Apple Silicon Macs without tensor units (
!device.tensorUnits(), including Apple M3 Ultra), GLM-5.3-Flash prompt execution previously fell back toTF_SIMD_FRAGSscalar loops inzig/kernels/metal/nax.handzig/kernels/metal/core/affine_mm.metal(BM=64with scalar 2-byte reads), scalar KDAprep/scan/postmemory loads inzig/kernels/metal/glm_kda_prompt.metal, and scalar indexer key loads inzig/kernels/metal/glm_index_nax.metal. This change adds GLM-scopedTF_GLM_M3_FASTMetal fast paths (<metal_simdgroup_matrix>32x32/32x64 tiling with vectorized global/threadgroup loads andBM=32dense/gather dispatches, vectorizedbfloat2/float2/float4KDA prep/post +SG=4, B=24glm_kda_scan/_tp, and vectorizedbfloat2tileT+mma_16x16indexer scores/decode) enabled only byzig/src/families/glm/{kernels,wide}.zigwhen!device.tensorUnits(), keeping M5 tensor-unit paths and all non-GLM Metal families byte-unchanged and model logits/tokens bit-identical.main(a1781885e921e9a772c641fef26010557e1349da), 1 commit (31172351fe92a71b6e32695bbfea05bb2120554d; full-model served A-B-B-A andtf-glm-runreceipts were collected againstf5765b51254d7ec7ef1295e23cb500f328165600atb42f0ab551b8857b928a20be1248b331a359f25cwith byte-identical kernel, engine,glm_rows_*, andmetal_glm_promptsources — rebasing ontoa1781885e9only merged upstreammetal_limits.zigandmetal_main.zigcontract suite registrations, and all unit, golden, launch-map,tf-glm-rows-check, andtf-kernel-contractchecks were re-executed on31172351feon both M3 Ultra and M5 Ultra).atc overlap --json: Run on31172351fe92a71b6e32695bbfea05bb2120554dagainstorigin/main(a1781885e9);complete: false(myPullRequestsLimit,otherPullRequestsLimit,pullFilesUnavailable,timeLimit). Overlaps only stale PRs#1773,#1774,#1775,#1780,#1787,#1858(shared contract registriesmetal_main.zig/metal_limits.zig); zero source overlap with active GLM PRs#3894,#3903,#3906;git merge-tree --write-tree origin/main HEADsucceeds cleanly (52de2e8223d6743f64bba2a5aebc3057d559a9c7).Checks on this head
31172351fe92a71b6e32695bbfea05bb2120554dpython3 tools/zig/lean_check.py, on the base and on this head:221vs221,0 new(tools/zig/lean_new.pyagainstorigin/maina1781885e9;tools/pr_check.py --base origin/mainpasses with0 error(s), 1 warning(s)pre-PR without--pr NUMBER;CHANGELOG.mdintentionally left unmodified to avoid merge-train conflicts).31172351fe):git diff --check origin/main: passed (0exit).zig fmt --check build.zig zig: passed (0exit) on both M3 Ultra and M5 Ultra.applegpu_g15):zig build test --summary all -Dcpu=apple_m1:Build Summary: 895/895 steps succeeded; 10389/10491 tests passed (102 skipped)(0exit).zig build test-golden -Dcpu=apple_m1:322/335+55/55 equal to server goldens, 0 unexpected differences(0exit).zig build test-metal-launch-map-host -Dcpu=apple_m1:Metal launch map: 862 registered launches, 2 controls, 0 missing(0exit).zig build test-glm-sampling -Dcpu=apple_m1:15/15cases passed (0exit).zig build native dev-tools tf-kernel-contract -Dcpu=apple_m1 -Dversion=1.0.0: passed (0exit)../zig-out/bin/tf-glm-rows-check:PASS 1328072 values within the fp32-sum bound (worst err/bound 0.9839), 10809776 bit-equal pairs(0exit)../zig-out/bin/tf-kernel-contract --only "glm m3",--only affine_mm,--only "metal flint" --no-limits,--only glm --no-limits: all0exit (see Kernel contract section).applegpu_g17):zig build test --summary all -Dcpu=apple_m1:Build Summary: 895/895 steps succeeded; 10383/10491 tests passed (108 skipped)(0exit).zig build test-golden -Dcpu=apple_m1:322/335+55/55 equal to server goldens, 0 unexpected differences(0exit).zig build test-metal-launch-map-host -Dcpu=apple_m1:Metal launch map: 862 registered launches, 2 controls, 0 missing(0exit).zig build test-glm-sampling -Dcpu=apple_m1:15/15cases passed (0exit).zig build native dev-tools tf-kernel-contract -Dcpu=apple_m1 -Dversion=1.0.0: passed (0exit)../zig-out/bin/tf-glm-rows-check:PASS 1328072 values within the fp32-sum bound (worst err/bound 0.9839), 10809776 bit-equal pairs(0exit)../zig-out/bin/tf-kernel-contract --only "glm m3",--only affine_mm,--only glm --no-limits: all0exit (see Kernel contract section).CANCELLED(infrastructure— repo-wide07:00:25Z–07:00:35ZActions queue flush cancelled all queued PR runs before execution, zero test failures):Zig format(44467138925) (fmtCANCELLED) andNo GPU(44467139134) (zig,zig-b,zig-windows,zig-macos-dev-tools,pytest,benchmark-clients (ubuntu-latest),benchmark-clients (windows-latest),release-paths (ubuntu-24.04)CANCELLED;host-nightly-linux,host-nightly-macosSKIPPED). Equivalentzig fmt --check,zig build test test-golden test-metal-launch-map-host test-glm-sampling native dev-tools tf-kernel-contract -Dcpu=apple_m1,tf-glm-rows-check, andtf-kernel-contractchecks passed on31172351feon both M3 Ultra and M5 Ultra above.-Dcpu=apple_m1and Metal launch limits audited against M1–M5), physical two-Mac expert-parallel pair (single-Mac M3 Ultra and M5 Ultra verified), CUDA, HIP, Vulkan, XPU, Windows, desktop/mobile app (Metal-only, GLM-scoped viaTF_GLM_M3_FASTon!device.tensorUnits()).Receipt
discard, base, this, this, base, freshtensorfold-nativeper arm) on Apple M3 Ultra withTensorFold/GLM-5.3-Flash-MLX-4bit-MTP,--parallel 4 --context 73728 --prompt-cache-gib 16(--require-coldassertscached_tokens == 0on all prompt rows),notifyutil -g com.apple.system.thermalpressurelevel==0(Nominal) at start and end of every arm and start of every cell (32kand64kcells warmed to2at cell end and cooled to0before the next cell). Scrubbed raw artifacts and summary: https://gist.github.com/bf81a61e01ab643adc491a4ac7b9af06tools/bench_cache.py --require-cold, cold first-requestttft_sacross[arm1_base, arm4_base]vs[arm2_this, arm3_this]):2K(2048tok,token_sha7221558f1eedacross all 4 arms): base3.741s(3.7407s, 3.7416s,547.4 tok/s) vs candidate3.529s(3.5287s, 3.5287s,580.4 tok/s) —+5.68%(-0.212s)8K(8192tok,token_shad3af62c4c734across all 4 arms): base15.511s(15.5088s, 15.5127s,528.2 tok/s) vs candidate14.670s(14.6647s, 14.6745s,558.4 tok/s) —+5.42%(-0.841s)32K(32768tok,token_sha180c0e33028cacross all 4 arms): base62.644s(62.6371s, 62.6511s,523.1 tok/s) vs candidate59.392s(59.3829s, 59.4008s,551.7 tok/s) —+5.19%(-3.252s)64K(65536tok,token_shab4c387a6fc98across all 4 arms): base126.547s(126.5347s, 126.5597s,517.9 tok/s) vs candidate120.009s(119.9872s, 120.0314s,546.1 tok/s) —+5.17%(-6.538s)tf-glm-runon M3 Ultrab42f0ab551):0/64token diffs andmax |diff| 0.0000(0.00 bf16 steps) acrossfib-short(10tok, depth0,3,f8cc3868c5dc23d8),sys-65(65tok, depth0,3,8a0998c8c6837cee),sys-2048(2048tok, depth0,3,ad4e7d04cc3c9ecb), plusGLM_PROMPT=0vs1(0diffs);GLM_PROMPT_PROFILE=3(2048rows):full 3722.29 -> 3508.75 ms(-213.54 ms,550 -> 583 tok/s),kda 1050.84 -> 926.32 ms(-11.8%).tf-glm-runonb42f0ab551, Nominal0 -> 0):0/64token diffs andmax |diff| 0.0000acrossfib-short(67b08d14da0c1132),sys-65(8a0998c8c6837cee),sys-2048(ad4e7d04cc3c9ecb);sys-2048prompt2015 -> 2017 tok/s, depth 0 decode69.0 -> 69.1 tok/s, depth 3 decode121.1 -> 121.4 tok/s.!device.tensorUnits()(pre-M5 Macs including M1–M4); M5 Macs (device.tensorUnits()) keep their existing tensor-unit paths; non-GLM Metal families (step5,spark25,flint,minimax_m3,lfm2_moe, etc.) keepglm_m3_fast = falseand compile byte-unchanged sources.!device.tensorUnits(). No optimization-ledger profile row needed.origin/maina1781885e921e9a772c641fef26010557e1349da, head31172351fe92a71b6e32695bbfea05bb2120554d(served A-B-B-A measured onb42f0ab551b8857b928a20be1248b331a359f25cvsf5765b51254d7ec7ef1295e23cb500f328165600, identical kernel/engine sources); Zig0.17.0; Apple Metal toolchain32023.922 (metalfe-32023.922.4)(Metal 4); Apple M3 Ultra (80-core GPU, 512 GiB RAM, macOS26.6.225G83) and Apple M5 Ultra (80-core GPU, 512 GiB RAM, macOS27.2.126B131); checkpointTensorFold/GLM-5.3-Flash-MLX-4bit-MTP(rev d2b17f5253440224422b150705c1109a9a3a0368, SHA-256 verified).zig/kernels/metal/{nax.h, core/affine_mm.metal, glm_kda_prompt.metal, glm_index_nax.metal},zig/src/core/affine_mm.zig,zig/src/families/glm/{checks,forward,kernels,prompt,wide}.zig,zig/tests/glm_rows_*.zig, andzig/tests/contract/. Ran on Apple M3 Ultra and Apple M5 Ultra (single-Mac host); M1/M2/M4 on-device, physical two-Mac EP pair, and CUDA/HIP/Vulkan/XPU/Windows/app not run (Metal limits checked M1–M5; built-Dcpu=apple_m1).tools/bench_concurrent.py --alone --serialortoken_shaagainst"draft": false:bench_concurrent.py --levels 1,2,4 --alone --tokens 64 --reps 2 --temperatures 0:unequal = 0,failed = 0across every arm (codeandchatatL=1,2,4:equal = 2, 4, 8respectively per arm).bench_openai.py: identicaltoken_shaacross all 4 arms x 3 reps (f200e0f785fa, e1f620931114, 4f10a4283d0eforfibonacci-rawT=1.0;7d9f8501fedf, f8913e9ac80d, 20b71bc7d40aforgpu-chat-no-thinkT=1.0;d2f13da2874cforfibonacci-rawT=0;f24eca094b5cforgpu-chat-no-thinkT=0).tf-glm-run: depth 3 (--draft) == depth 0 (0/64diffs) and0.0000max logit diff (0.00 bf16 steps) on both M3 Ultra and M5 Ultra;GLM_PROMPT=0==1.tools/bench_openai.py:fibonacci-raw, T=0.0: base69.65(69.715, 69.583) vs candidate69.59(69.686, 69.489) tok/s; TTFT0.112s -> 0.112s.gpu-chat-no-think, T=0.0: base62.87(62.969, 62.762) vs candidate62.92(62.990, 62.851) tok/s; TTFT0.277s -> 0.237s(-14.4%).fibonacci-raw, T=1.0: base61.62(60.231, 63.008) vs candidate60.14(60.258, 60.025) tok/s; TTFT0.113s -> 0.113s.gpu-chat-no-think, T=1.0: base57.94(58.032, 57.840) vs candidate57.85(57.953, 57.750) tok/s; TTFT0.277s -> 0.237s(-14.4%).bench_concurrent.pyaggregate / per-stream (T=0.0):code L=169.70 -> 69.60,code L=296.75 -> 96.85,code L=4100.15 -> 100.15;chat L=163.40 -> 62.75,chat L=277.85 -> 79.85(+2.57%),chat L=480.20 -> 83.35(+3.93%).ttft_sfromtools/bench_cache.py --require-coldimproves across all tested lengths on M3 Ultra (2K:3.741s -> 3.529s,+5.68%;8K:15.511s -> 14.670s,+5.42%;32K:62.644s -> 59.392s,+5.19%;64K:126.547s -> 120.009s,+5.17%).TensorFold/GLM-5.3-Flash-MLX-4bit-MTPcheckpoint, no new weights or drafter).Kernel contract, only when a GPU kernel is new or changed
metal affine_mm glm m3 q4 g64,q4 g64 fp32 out,q2 g64,q5 g64,q6 g64,q8 g64,q4 g32,q4 g128(zig/tests/contract/metal_main.zig,metal_limits.zig): replacesBM=64scalarTF_SIMD_FRAGStf_affine_mmunderTF_GLM_M3_FASTfor!device.tensorUnits().metal affine_mm glm m3 q4 g64 gather,metal affine_mm glm m3 q8 g64 gather scatter(zig/tests/contract/metal_affine_gather.zig): replacesBM=64TF_SIMD_FRAGStf_affine_gather_*underTF_GLM_M3_FAST.metal glm m3 index_scores_nax,metal glm m3 index_decode,metal glm m3 kda_prep,metal glm m3 kda_scan,metal glm m3 kda_scan_tp,metal glm m3 kda_post(zig/tests/contract/metal_glm_prompt.zig): replaces unvectorized pre-M5 GLM index and KDA prompt/scan/post paths (bit-identical to baseline across shapes and non-finite inputs).control: metal affine_mm glm m3 q4 g64, wrong row tile height(red at shapes, as planted)control: metal affine_mm glm m3 gather q4 g64, each expert's last row dropped(red at shapes, as planted)control: metal glm m3 index_scores_nax, wrong row tile height(red at shapes, as planted)control: metal glm m3 index_decode, wrong block tile width(red at shapes, as planted)control: metal glm m3 kda_prep, wrong head tile grid(red at shapes, as planted)control: metal glm m3 kda_scan, half value-column grid(red at shapes, as planted)control: metal glm m3 kda_post, wrong head tile grid(red at shapes, as planted)SUMMARYline and exit status:applegpu_g15,tensor units false):./zig-out/bin/tf-kernel-contract --only "glm m3"(0exit):SUMMARY 16 kernels: 16 pass gates 1-4, 0 fail, 5 known findings; 7 controls: 7 red as planted, 0 missed; 0 limit problems; gates 58671 ms, limits 35 ms./zig-out/bin/tf-kernel-contract --only affine_mm(0exit):SUMMARY 14 kernels: 14 pass gates 1-4, 0 fail, 7 known findings; 2 controls: 2 red as planted, 0 missed; 0 limit problems; gates 76846 ms, limits 35 ms./zig-out/bin/tf-kernel-contract --only "metal flint" --no-limits(0exit):SUMMARY 21 kernels: 21 pass gates 1-4, 0 fail, 0 known findings; 16 controls: 16 red as planted, 0 missed; 0 limit problems; gates 144172 ms, limits 0 ms./zig-out/bin/tf-kernel-contract --only glm --no-limits(0exit):SUMMARY 73 kernels: 73 pass gates 1-4, 0 fail, 5 known findings; 31 controls: 31 red as planted, 0 missed; 0 limit problems; gates 1535777 ms, limits 0 msapplegpu_g17,tensor units true):./zig-out/bin/tf-kernel-contract --only "glm m3"(0exit):SUMMARY 16 kernels: 16 pass gates 1-4, 0 fail, 5 known findings; 7 controls: 7 red as planted, 0 missed; 0 limit problems; gates 48296 ms, limits 33 ms./zig-out/bin/tf-kernel-contract --only affine_mm(0exit):SUMMARY 18 kernels: 18 pass gates 1-4, 0 fail, 9 known findings; 5 controls: 5 red as planted, 0 missed; 0 limit problems; gates 92860 ms, limits 37 ms./zig-out/bin/tf-kernel-contract --only glm --no-limits(0exit):SUMMARY 89 kernels: 89 pass gates 1-4, 0 fail, 13 known findings; 31 controls: 31 red as planted, 0 missed; 0 limit problems; gates 1258714 ms, limits 0 ms-Dcpu=apple_m1, Metal limits verified against M1–M5).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. If the bits change, the description says which sums change and why.
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