Repository navigation
glm(metal): fuse DFlash2 Q4 decode kernels and pack batch heads - #4258
Conversation
f29859f to
002e8a3
Compare
002e8a3 to
07695ce
Compare
|
I verified published head
This checks the current head's synthetic path and the three Metal kernels. Full-checkpoint serving and performance were outside this run. Igor Lessio - AIFLOW LABS |
|
I traced the red pytest check on uv run --no-project --with pytest -- pytest -q tools/test_ci_workflow_conditions.pyAdding Igor Lessio - AIFLOW LABS |
|
Thank you, @gilby. The fused kernels keep the existing rounding steps, and the packed batch rows, replacement tests and planted controls are useful. This head merges cleanly with current main. Before landing, we need full-model checks on that combined tree: drafted versus plain and concurrent versus solo token equality, plus before/after timings and peak memory. Please include the exact target and drafter revisions and cover both tensor-unit Metal and the older Metal fallback, since the draft-head choice changes there too. The current-head synthetic receipt helps; the older full-model receipt does not yet cover this tree.
|
|
Verified the combined Checkpoint & Serving Configuration
Local Unit & Kernel Contract Checks (Combined Tree, Apple M5 Max)
1. Apple M5 Ultra, 512 GiB (
|
Cell (N, T) |
Main agg tok/s (main_1 / main_2) |
PR agg tok/s (pr_1 / pr_2) |
Agg Δ | Main per-stream (mean) |
PR per-stream (mean) |
Per-stream Δ | Paired makespan (main vs PR, s) |
|---|---|---|---|---|---|---|---|
code N=1, T=0 |
152.80 (153.3 / 152.3) |
152.15 (152.2 / 152.1) |
-0.43% |
152.80 |
152.15 |
-0.43% |
1.742 vs 1.750 |
code N=3, T=0 |
183.05 (183.7 / 182.4) |
182.35 (182.6 / 182.1) |
-0.38% |
62.15 |
61.90 |
-0.40% |
4.253 vs 4.269 |
chat N=1, T=0 |
88.55 (88.9 / 88.2) |
89.90 (89.9 / 89.9) |
+1.52% |
88.55 |
89.90 |
+1.52% |
3.013 vs 2.971 |
chat N=3, T=0 |
151.15 (151.4 / 150.9) |
150.90 (150.9 / 150.9) |
-0.17% |
51.75 |
51.70 |
-0.10% |
5.196 vs 5.204 |
code N=1, T=1 |
117.95 (118.1 / 117.8) |
118.10 (118.1 / 118.1) |
+0.13% |
117.95 |
118.10 |
+0.13% |
2.235 vs 2.233 |
code N=3, T=1 |
122.50 (123.0 / 122.0) |
122.35 (122.5 / 122.2) |
-0.12% |
47.95 |
49.75 |
+3.75% |
6.319 vs 6.326 |
chat N=1, T=1 |
81.00 (81.1 / 80.9) |
81.90 (81.9 / 81.9) |
+1.11% |
81.00 |
81.90 |
+1.11% |
3.281 vs 3.247 |
chat N=3, T=1 |
110.85 (111.0 / 110.7) |
110.80 (110.5 / 111.1) |
-0.05% |
38.95 |
39.65 |
+1.80% |
7.034 vs 7.038 |
(Accepted drafts under --serial on M5 Ultra: chat T=0 rose from 119 on main to 139 on main + #4258, code T=1 from 471 to 493/492, code T=0 208/209 vs 208, chat T=1 391 vs 390.)
2. Apple M3 Ultra, 512 GiB (apple-m3, older Metal fallback, tensorUnits() == false) — Paired A-B-B-A (main_1, pr_1, pr_2, main_2)
- Token equality:
0failed,0unmeasured across all4passes ×8cells;--alone0unequal/0failed;--serial0unequal/0failed (plain_drafted = 0); cross-binarymainvsmain + #4258:48/48exacttoken_sha256match (0diffs). - Peak memory (
/usr/bin/time -l):main188.17 GBmax RSS /189.18 GBpeak footprint;main + #4258188.17 GBmax RSS /189.18 GBpeak footprint. - Thermals (
pmset -g thermbefore/after every pass): no thermal or performance warnings recorded (sequential sustained load drifted downward across the 4 passesmain_1 -> pr_1 -> pr_2 -> main_2, reflected in both arms of the A-B-B-A pair).
Cell (N, T) |
Main agg tok/s (main_1 / main_2) |
PR agg tok/s (pr_1 / pr_2) |
Agg Δ | Main per-stream (mean) |
PR per-stream (mean) |
Per-stream Δ | Paired makespan (main vs PR, s) |
|---|---|---|---|---|---|---|---|
code N=1, T=0 |
69.50 (73.5 / 65.5) |
70.90 (73.4 / 68.4) |
+2.01% |
69.50 |
70.90 |
+2.01% |
3.794 vs 3.711 |
code N=3, T=0 |
101.90 (107.7 / 96.1) |
104.25 (107.8 / 100.7) |
+2.31% |
34.30 |
35.10 |
+2.33% |
7.645 vs 7.454 |
chat N=1, T=0 |
63.35 (65.2 / 61.5) |
63.40 (65.1 / 61.7) |
+0.08% |
63.35 |
63.40 |
+0.08% |
4.373 vs 4.350 |
chat N=3, T=0 |
92.35 (96.9 / 87.8) |
93.90 (96.8 / 91.0) |
+1.68% |
32.05 |
32.60 |
+1.72% |
8.631 vs 8.472 |
code N=1, T=1 |
74.75 (78.7 / 70.8) |
74.55 (77.1 / 72.0) |
-0.27% |
74.75 |
74.55 |
-0.27% |
3.541 vs 3.541 |
code N=3, T=1 |
78.25 (82.2 / 74.3) |
77.75 (80.2 / 75.3) |
-0.64% |
30.35 |
30.55 |
+0.66% |
9.911 vs 9.968 |
chat N=1, T=1 |
57.70 (60.6 / 54.8) |
56.30 (58.0 / 54.6) |
-2.43% |
57.70 |
56.30 |
-2.43% |
4.768 vs 4.892 |
chat N=3, T=1 |
70.95 (74.3 / 67.6) |
69.40 (70.9 / 67.9) |
-2.18% |
26.60 |
25.35 |
-4.70% |
11.161 vs 11.388 |
|
Thanks @gilby. The pinned M3 and M5 combined-tree runs answer the remaining model checks: drafted equals plain, concurrent equals solo, the cross-binary hashes match, and the paired timings and peak memory are reported. The changed runtime and shader files match the receipted integration. One thing before it lands: on M3, chat at one stream reads 2.43% slower, and the report notes some downward drift under sustained load. Since a change can't land slower in any cell, could you re-run just that cell with the builds alternated (base, new, new, base, after one warm run) so we can tell drift from a real cost? If it holds, we'd look for where the time goes before landing. The remaining merge work (the source registry and keeping the contract runner within its line cap) is ours.
|
|
Re-ran the isolated Setup
1.
|
|
Thanks @gilby. The isolated alternating M3 runs answer the remaining chat-cell request: the paired results are +0.07% and +0.18%, with matching token hashes and memory in those runs. The changed runtime and shader files also match that receipted integration. This is ready to land through the normal final-tree checks. The one remaining merge conflict, in the kernel source list, is ours to resolve; the contract runner is now within its line cap. No additional device rerun is needed for this unchanged head.
|
07695ce to
17b7fa3
Compare
|
I tested the exact #5013 train tip zig build test-glm-metal-dflash-host test-glm-metal-dflash-synthetic test-glm-keep-seqs-host test-glm-dflash-host tf-kernel-contract -Dfixed-cpu -j2 --summary all
./zig-out/bin/tf-kernel-contract --only 'glm dflash2 opt'All 58 build steps and 35 tests passed, and the synthetic Metal drafter executable succeeded. The device filter passed all three optimized kernels, caught all six planted controls and found zero launch-limit problems. This adds an independent check of the final train source. The earlier model-backed and timing receipts remain separate. Igor Lessio - AIFLOW LABS |
Add fused DFlash2 Q4 Metal decode kernels (glm_dflash_kv_append, glm_dflash_qkv_prep, and glm_dflash_conv_norm) with stacked KV/QKV/ gate+up projections in metal_dflash_opt.zig, pack active batch mask rows before final norm and head projection in metal_dflash_same.zig, route draft_head ahead of target head in metal_dflash_backend.zig, and exclude prefill-only rounds from Shared.step decode timing in metal_dflash_source.zig. Refs ashhart#3152
17b7fa3 to
fe2a37e
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
Stacks on #4257 (after #3920 merged) to fuse the hot GLM-5.3-Flash DFlash2 Q4 decode path on Metal (
glm_dflash_kv_append,glm_dflash_qkv_prep, andglm_dflash_conv_normwith stacked KV/QKV/gate+up lane adapters inmetal_dflash_opt.zig), pack active batch mask rows before final RMSNorm and head projection (metal_dflash_same.zig), prefertarget.w.draft_headwhen present (metal_dflash_backend.zig), and ignore prefill-only rounds (decode_n == 0) inShared.stepdecode timing (metal_dflash_source.zig).1d57c823ffb7aa1ae54334d682dbe11abf9ce21e) onmain(d7dc085bfcb50f5ac21271c22fd58d3de13b8ba1, after glm metal: draft GLM-5.3-Flash with published DFlash2 behind --drafter #3920 merged); rebased cleanly onto rebased glm(metal): prune inactive DFlash2 rows, gate tap capture, and cut sampled transition cost #4257 at002e8a353644a11450d2f055777af3676c1e684d(git range-diffagainst receipted commitf29859f39ebc081e84056a7ed70b7cd1ba5d1d35is=).atc overlap --base 1d57c823ffb7aa1ae54334d682dbe11abf9ce21e --json:17changed files atop glm(metal): prune inactive DFlash2 rows, gate tap capture, and cut sampled transition cost #4257;6clean PR overlaps (#1774,#1775,#1780,#1787,#2522,#2634),4older conflicting PRs (#1835,#2231,#2260,#2273),0unknown.Checks on this head
002e8a353644a11450d2f055777af3676c1e684d(rebased cleanly withrange-diff =fromf29859f39ebc081e84056a7ed70b7cd1ba5d1d35)python3 tools/zig/lean_check.py, on the base and on this head:168on glm(metal): prune inactive DFlash2 rows, gate tap capture, and cut sampled transition cost #4257 (1d57c823ff) andorigin/main(d7dc085bfc),168on head (002e8a3536),0new findings (python3 tools/pr_check.py --base 1d57c823ffandpython3 tools/pr_check.py --base origin/mainboth exit0;metal_main.zig597lines).glm_dflash_opt.metal,metal_dflash_opt.zig,Copies.opt, packed multi-stream mask-row projection inmetal_dflash_same.zig, anddraft_head orelse headinmetal_dflash_backend.zigare absent on glm(metal): prune inactive DFlash2 rows, gate tap capture, and cut sampled transition cost #4257, whileShared.stepsetss.last = twhendecode_n == 0(prefill-only rounds in Shared do not pollute the next decode round's timingfails on glm(metal): prune inactive DFlash2 rows, gate tap capture, and cut sampled transition cost #4257 and passes on head).002e8a3536, with the command and pass, fail and skip counts:zig fmt --check build.zig zig— passed (0exit)git diff --check 1d57c823ff..HEAD— passed (0exit)zig build test-glm-metal-dflash-host test-glm-metal-dflash-synthetic test-glm-scheduling-host test-glm-prefill-widths-host check-glm-restore-host test-kernel-contract tf-kernel-contract --summary all—95/95steps succeeded;340/340tests passed (test-glm-metal-dflash-host15/15,test-glm-scheduling-host/test-glm-prefill-widths-host63/63,test-glm-metal-dflash-syntheticglm dflash2 follow: chains agree with today's pass on 2688 of 2688 drafts,test-kernel-contract192/192+ sub-suites)./zig-out/bin/tf-kernel-contract --only "glm dflash2 opt"on Apple Metal (applegpu_g17,tensor units true) — exited0:SUMMARY 3 kernels: 3 pass gates 1-4, 0 fail, 0 known findings; 6 controls: 6 red as planted, 0 missed; 0 limit problems; gates 112 ms, limits 26 msf29859f39e(git range-diff =to002e8a3536):zig build test-golden—322/335&69/69,0unexpected differences./zig-out/bin/tf-kernel-contract --no-gates --only "glm dflash2 opt" --offline <dir>— exited0(PASS limits: 3 selected registered Metal launches against M1-M5, 0 problems;glm_dflash_kv_append: 32 threads; M1 256, M2 256;glm_dflash_qkv_prep: 32 threads; M1 256, M2 256;glm_dflash_conv_norm: 1024 threads; M1 1024, M2 1024; both offline controls RED)./zig-out/bin/tf-kernel-contract --only "glm dflash2 opt"on Apple M5 Ultra (applegpu_g17,therm=0) — exited0(SUMMARY 3 kernels: 3 pass gates 1-4, 0 fail, 0 known findings; 6 controls: 6 red as planted, 0 missed; 0 limit problems; gates 129 ms, limits 4 ms)002e8a3536(was 8/9 green prior to rebase, with the 1 failure being the pre-existing hosted-Windows timing flake intest_a_healthy_server_proves_both).--offlinelimits); CUDA/ROCm paths (Metal-only DFlash2 change).Receipt
Full scrubbed receipts (taken at
f29859f39e,git range-diff =to002e8a3536; contract log, offline M1–M5 limits log,bench_concurrent.py,bench_openai.pyonMLX-4bit-MTP+--drafter--drafter-bits 4): https://gist.github.com/gilby/7da62c9634c7092c44e1ee36ea6d0e1e190e1fe04f) and glm metal: draft GLM-5.3-Flash with published DFlash2 behind --drafter #3920 (b9615a115aa) against branch (f29859f39e,range-diff =to002e8a3536), same Apple M5 Ultra (256 GB) andTensorFold/GLM-5.3-Flash-MLX-4bit-MTP+--drafter zai-org/GLM-5.3-Flash-DFlash2 --drafter-bits 4,--prompt-cache-gib 0 --profile stable,therm=0,tools/bench_concurrent.py --alone --serial --tokens 256 --reps 3 --temperatures 0,1.0 --levels 1,3(0unequal /0failed across everyaloneandserialcheck;timing_valid=True):#3920(b9615a115aa)#4257(190e1fe04f)D2(f29859f39e)D2vs#4257D2vs#3920#4257steadyD2steady#4257code T=0.0 N=1code T=0.0 N=3chat T=0.0 N=1chat T=0.0 N=3code T=1.0 N=1code T=1.0 N=3chat T=1.0 N=1chat T=1.0 N=3tools/bench_openai.py --tokens 256 --reps 3 --temperatures 0,1.0(--prompt-cache-gib 0, median tok/s,12/12token_shaand12/12output_sha256bit-identical to#4257and#3920):#3920(b9615a115aa)#4257(190e1fe04f)D2(f29859f39e)D2vs#4257D2vs#3920fibonacci-raw T=0.0gpu-chat-no-think T=0.0fibonacci-raw T=1.0gpu-chat-no-think T=1.0glm5_nexton Metal with Q4--drafterwhendevice.tensorUnits()holds (tp == 1).002e8a353644a11450d2f055777af3676c1e684dstacked on#4257(1d57c823ffb7aa1ae54334d682dbe11abf9ce21e) andmain(d7dc085bfc),range-diff =vs receiptedf29859f39ebc081e84056a7ed70b7cd1ba5d1d350.17.0(-Dcpu=apple_m1), macOS Metalapplegpu_g17, 256 GB unified memory, tensor units true)TensorFold/GLM-5.3-Flash-MLX-4bit-MTPwith DFlash2 drafter (--drafter-bits 4)2688/2688 drafts), Metal kernel contract (gates 1–4 + M1–M5 limits + M1/M2 offline limits), host/golden tests.tools/bench_concurrent.py --alone --serialortoken_shaagainst"draft": false:bench_concurrent.py --alone --serial:0unequal and0failed across everyalone(concurrent == alone) andserial(drafted == "draft": false) comparison.bench_openai.py:12/12token_shaand12/12output_sha256bit-identical to#4257and#3920.+0.93%greedy /+1.29%sampled onfibonacci-raw,+1.21%oncode T=0.0 N=3,+1.01%onchat T=1.0 N=1over#4257).glm_dflash_kv_append,glm_dflash_qkv_prep,glm_dflash_conv_norm, packed batch head projection) andShared.stepdecode_n == 0guard change; full long-prompt prefill benchmark not re-run.Kernel contract, only when a GPU kernel is new or changed
glm_dflash_kv_append— replaces per-layerdraft_opsnorm+rope+shared_attention.appendKvduring DFlash2 ahead/catchup absorb.glm_dflash_qkv_prep— replaces per-headdraft_opsq/k_norm+rope+vcopy inside DFlash2 attention layers.glm_dflash_conv_norm— replacesdraft_opsdynamic_conv2+draft_rmsforbranch == 1.zig/src/core/contract/glm_dflash_records.zig,zig/tests/contract/metal_glm_dflash_opt.zig,zig/tests/contract/metal_limits.zig, andzig/tests/contract/metal_main.zig.control: glm dflash2 opt kv_append writes into input(FAIL repeat)control: glm dflash2 opt kv_append drops last RoPE pair(FAIL shapes)control: glm dflash2 opt qkv_prep writes into input(FAIL repeat)control: glm dflash2 opt qkv_prep drops last RoPE pair(FAIL shapes)control: glm dflash2 opt conv_norm writes into input(FAIL repeat)control: glm dflash2 opt conv_norm drops tail cols(FAIL shapes)tf_row_projection_q4_f32(2 limit problems),36 KiB tf_ctl_big_tg(8 limit problems);3 selected registered Metal launches against M1-M5, 0 problems(plus M1/M2 offline limits pass with0problems).SUMMARYline and exit status:applegpu_g17, tensor units true):SUMMARY 3 kernels: 3 pass gates 1-4, 0 fail, 0 known findings; 6 controls: 6 red as planted, 0 missed; 0 limit problems; gates 112 ms, limits 26 mson002e8a3536(gates 129 ms, limits 4 msonf29859f39e) (exit0).--offline;metal_limits.zigchecks M1–M5 limits with0 problems); NVIDIA/AMD untouched (Metal-only change).Checklist
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo.--drafter, no slower than without it, greedy and sampled