Skip to content

glm metal: fuse MTP prep norms and 4-bit/64 greedy head+top1 - #4254

Open
gilby wants to merge 3 commits into
ashhart:mainfrom
gilby:glm-metal-mtp-opt
Open

gilby wants to merge 3 commits into
ashhart:mainfrom
gilby:glm-metal-mtp-opt

Conversation

@gilby

@gilby gilby commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

What this changes

Fuses the GLM-5.3-Flash MTP preparation step (embedRows + enorm + hnorm) into glm_mtp_prep_4b / glm_mtp_prep_8b and fuses greedy 4-bit/64 vocab projection + top-1 argmax into glm_head_top1_4b_one / glm_head_top1_4b + glm_reduce_top1 on single-Mac (tp == 1) loads (GLM_RANKS=1 preserves full logits via keep_logits). In zig/src/families/glm/slots.zig, single-stream draftAll reuses the already-computed iw and qr/xp slices for the absorbed row (drafting == 1) so mtp.finish skips redundant x_proj/indexWeights work while preserving exact e.sc.qr query norms.

Checks on this head

  • Head commit: 4647eba806b59e088b70a0e17f06c53dfdff6618
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 168 on base (4c616e63d57204cd8395dc3af9b572d9ec49626b), 168 on head (4647eba806b59e088b70a0e17f06c53dfdff6618), 0 new findings (python3 tools/pr_check.py --base origin/main exits 0).
  • Pre-fix check on main (4c616e63d5): zig/kernels/metal/glm_mtp_opt.metal and zig/src/families/glm/mtp_opt_test.zig are absent on main (MTP always dispatches separate embed + rms + rms and qmv + argmax).
  • Tests run on 4647eba806, with the command and pass, fail and skip counts:
    • zig fmt --check build.zig zig — passed (0 exit)
    • git diff --check — passed (0 exit)
    • families.glm.* unit tests (mtp_opt_test.zig Metal bit-parity against embed+rms and qmv+argmax) — All 186 tests passed.
    • zig build test-kernel-contract test-golden test-glm-scheduling-host test-glm-prefill-widths-host check-glm-restore-host — passed (test-golden 322/335 & 69/69, 0 unexpected differences)
    • ./zig-out/bin/tf-kernel-contract --no-gates --only "glm mtp" --offline ... — passed (PASS limits: 5 selected registered Metal launches against M1-M5, 0 problems, M1/M2 offline limits 1024/1024/128/512/1024, both limit controls red)
    • ./zig-out/bin/tf-kernel-contract --only "glm mtp" on Apple M5 Ultra (applegpu_g17, therm=0) — exited 0 (details below)
  • CI: Pending on PR submission.
  • Not run, and why: live M1/M2/M3/M4 runs (M1/M2 verified via --offline limits); CUDA/ROCm paths (Metal-only kernels).

Receipt

Full scrubbed receipts (contract log, bench_concurrent.py, bench_openai.py on MLX-4bit-MTP and oq071): https://gist.github.com/gilby/44ce40c872fb35f27fa275bc74a966ab

  • Main (4c616e63d5) against branch (4647eba806), same Apple M5 Ultra (256 GB) and TensorFold/GLM-5.3-Flash-MLX-4bit-MTP, --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 (0 unequal / 0 failed across every alone and serial check; timing_valid=True; identical tokens_per_chunk across cells):
Cell main (4c616e63d5) S4 (4647eba806) Diff main steady S4 steady Steady diff
code T=0.0 N=1 117.4 117.2 -0.17% — — —
code T=0.0 N=3 175.2 184.9 +5.54% 190.1 189.1 -0.53%
chat T=0.0 N=1 96.2 96.2 +0.00% — — —
chat T=0.0 N=3 156.4 155.6 -0.51% 165.1 165.0 -0.06%
code T=1.0 N=1 128.9 128.6 -0.23% — — —
code T=1.0 N=3 131.7 131.1 -0.46% 139.1 138.3 -0.58%
chat T=1.0 N=1 88.6 88.8 +0.23% — — —
chat T=1.0 N=3 121.5 120.9 -0.49% 126.5 126.0 -0.40%
  • Decode speed before and after, from tools/bench_openai.py --tokens 256 --reps 3 --temperatures 0,1.0 (--prompt-cache-gib 0, median tok/s, 12/12 reply token_sha256s bit-identical to main on both checkpoints):
Prompt / Temp MLX-4bit-MTP main MLX-4bit-MTP S4 Diff oq071 main oq071 S4 Diff
fibonacci-raw T=0.0 116.76 117.29 +0.45% 96.70 97.00 +0.30%
gpu-chat-no-think T=0.0 95.86 96.38 +0.55% 80.88 82.48 +1.97%
fibonacci-raw T=1.0 115.86 116.24 +0.33% 87.12 87.40 +0.32%
gpu-chat-no-think T=1.0 89.21 89.26 +0.06% 77.45 77.70 +0.33%
  • Boxes it reaches: glm5_next on Metal (tp == 1, group == 64; glm_mtp_prep_{4b,8b} when inject == null, glm_head_top1_4b{_one} + glm_reduce_top1 on 4/64 greedy heads when GLM_RANKS is unset, draftAll drafting == 1 cache reuse).
  • New switch? Default, accepted values, profile row: None.
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision:
    • TensorFold commit: 4647eba806b59e088b70a0e17f06c53dfdff6618 atop main 4c616e63d57204cd8395dc3af9b572d9ec49626b
    • Toolchain: Zig 0.17.0 (-Dcpu=apple_m1), macOS Metal
    • Machine / GPU: Apple M5 Ultra (applegpu_g17, 256 GB unified memory, tensor units true)
    • Checkpoints: TensorFold/GLM-5.3-Flash-MLX-4bit-MTP and GLM-5.3-Flash-oQ4e-mtp-oq071
  • Paths and platforms the change reaches, which I ran and which I could not:
    • Ran: macOS Apple M5 Ultra (256 GB) single-Mac MLX-4bit-MTP and oq071, Metal contract + M1/M2 offline limits, unit/golden/host tests.
    • Could not run: live M1/M2/M3/M4 silicon, CUDA/ROCm (Metal-only change).
  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false:
    • bench_concurrent.py --alone --serial on MLX-4bit-MTP: 0 unequal and 0 failed across every alone (concurrent == alone) and serial (drafted == "draft": false) comparison.
    • bench_openai.py: 12/12 reply token_sha256s bit-identical to main on both MLX-4bit-MTP and oq071.
  • Main against this branch, same machine and checkpoint, posted before asking for a run on our machines (or why you can't): Yes — same Apple M5 Ultra (256 GB), paired receipts in Gist above.
  • Decode speed before and after: see tables above.
  • Prompt speed before and after: unchanged (MTP drafting & greedy head path only; prefill untouched).
  • Licence of any new weights or drafter, or "n/a": n/a.
Kernel contract, only when a GPU kernel is new or changed
  • Kernels registered with the contract, and the kernel each one replaces:
    • glm_mtp_prep_4b, glm_mtp_prep_8b — replaces separate embedRows + rms (enorm) + rms (hnorm) in mtp.absorb (bit-equal to embed + 2x glm_rms).
    • glm_head_top1_4b_one, glm_head_top1_4b, glm_reduce_top1 — replaces separate qmv (qmv_head) + argmax (glm_argmax) on single-Mac 4/64 greedy heads (bit-equal top-1 token ID to qmv + glm_argmax).
    • Registered in zig/src/core/contract/glm_mtp_records.zig, zig/src/core/contract/registry_joins.zig, zig/tests/contract/metal_glm_mtp_opt.zig, zig/tests/contract/metal_limits.zig, and zig/tests/contract/metal_main.zig.
  • A planted control for each new kernel, and each one fails as planted:
    • control: glm mtp glm_mtp_prep_4b writes into H (FAIL repeat), control: glm mtp glm_mtp_prep_4b drops tail cols (FAIL shapes)
    • control: glm mtp glm_mtp_prep_8b writes into H (FAIL repeat), control: glm mtp glm_mtp_prep_8b drops tail cols (FAIL shapes)
    • control: glm mtp glm_head_top1_4b writes into X (FAIL repeat), control: glm mtp glm_head_top1_4b drops tail vocab (FAIL shapes)
    • Limit controls: tf_row_projection_q4_f32 (2 limit problems), 36 KiB tf_ctl_big_tg (8 limit problems); 5 selected registered Metal launches against M1-M5, 0 problems
  • Each backend's SUMMARY line and exit status:
    • Apple M5 Ultra (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 7524 ms, limits 5 ms (exit 0)
  • Chips I could not run: Apple M1/M2/M3/M4 silicon (M1/M2 checked offline via --offline; metal_limits.zig checks M1–M5 limits with 0 problems); NVIDIA/AMD untouched (Metal-only change).

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

@AIFlowML

AIFlowML commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

I cross-checked the device receipt on M5 Max with Zig 0.17.0 at 4647eba8. After building tf-kernel-contract with -Dcpu=apple_m1 -Doptimize=ReleaseSafe, I ran ./zig-out/bin/tf-kernel-contract --only 'glm mtp'. It exited 0.

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 7297 ms, limits 7 ms

The head merges cleanly with 46c6ee22 on its own. Trial-merging the current public heads with #4248 and #4249 produces five and four conflicts respectively, in the GLM kernel wiring and contract lists among other files. #4253 conflicts in glm.zig where the head is initialized. If these ship together, please rerun the contract and the model equality checks on the combined SHA, including the synthesized q4 head from #4253. This run covers the selected Metal contract, with no served-model timing.

After the DFlash2 merge, I trial-merged this head with 3025ce9d2. I retained both contract suites and test registrations, and both .taps and .keep_logits in Engine.ctx. The M5 glm mtp filter passed all three kernels and caught all six controls, and all five launch records passed. The glm dflash2 accept filter also passed both kernels and caught its control. Building check-glm-restore-host test-glm-scheduling-host test-glm-metal-dflash-host on the same tree passed 75 tests and the restore compile. Served-model parity and timing remain open for the combined tree.

I combined 4647eba8 and #4249 at 59ffca4b in a scratch trial on 3025ce9d2. In glm/kernels.zig, both patches take fixed job slot 27, so I kept sh8 in 27–32, put the MTP pipeline group in 33, and sized the fixed part to 34. The separate contract records, resolver branches and suite registrations are all retained.

The combined M5 tree passed 82 host tests and the restore compile. The existing glm sh8, glm mtp and glm dflash2 accept filters passed seven kernels, caught all nine controls and reported zero limit problems. tf-glm-route-one exercised the actual kernel loader and matched all 79,872 synthetic logits. That tool reported a pre-existing allocator leak at exit from its unreleased kernel object. A scratch teardown rerun printed the same equality result with no leak report. No checkpoint or served timing was run.

Igor Lessio - AIFLOW LABS

@ashhart

ashhart commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Thanks @gilby. The fused MTP prep norms and the 4-bit/64 greedy head with top-1 read exact: your bench_concurrent.py --alone --serial shows 0 unequal and 12/12 token_sha256 matches, and forward.zig stays at 600 lines.

One question before we run it: in your MLX-4bit-MTP receipt every N=3 steady cell is a little slower than main (-0.06% to -0.58%). For more than one row, glm_head_top1_4b moves about the same bytes as qmv_head plus glm_argmax, so the fused path may not win there. Could you re-run the N=3 cells a few times, alternating main and this branch, and post the spread? If the gap stays, please keep the fused head for one row only, where your numbers show it helps.

The GLM DFlash2 work that landed on main conflicts with this branch in four files. We're rebasing it ourselves, keeping your commit as it is.

  • Synthena

@ashhart
ashhart force-pushed the glm-metal-mtp-opt branch 2 times, most recently from 6c8667a to 44f33e4 Compare November 21, 2026 18:54
gilby and others added 2 commits November 22, 2026 00:03
Fuse MTP embedding gather + RMSNorm + hidden RMSNorm into single-pass glm_mtp_prep_{4b,8b} and fuse 4-bit/64 vocab projection + argmax for greedy decoding into glm_head_top1_4b{_one} + glm_reduce_top1 on single-Mac TP=1 loads. Registers glm mtp contract gates/controls/records and Metal parity tests.

Refs ashhart#3152
@AIFlowML

Copy link
Copy Markdown
Contributor

I rechecked the rebased 0f641719 head on #4467 at 3f9742cf9, whose tree is identical to current main 31b7d54e. The trial merge applied cleanly with no local edits.

On M5 Max with Zig 0.17.0, all 287 host checks passed. The Metal run passed 3 kernels, caught 6/6 planted controls, and reported zero limit problems.

zig build tf-kernel-contract test-kernel-contract -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j4 --summary all
./zig-out/bin/tf-kernel-contract --only 'glm mtp'

The requested alternating N=3 performance comparison is still open. These results cover kernel correctness and limits.

Igor Lessio - AIFLOW LABS

@AIFlowML

Copy link
Copy Markdown
Contributor

I checked the red hosted jobs for 0f641719. Linux and Windows both stopped at the Flash Next environment fixture that expects 36 settings. Pytest stopped during collection because PyYAML was absent. Linux · Windows · Pytest

The same published head trial-merges cleanly into 3a02038db. The merged files retain main's 37-setting fixture and the PyYAML install line byte for byte. On M5 Max, zig build test-environment-host -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j1 --summary all passed 32/32. This verifies the environment failure on the current merge tree. The full hosted jobs and the alternating MTP performance run still have their own gates.

Igor Lessio - AIFLOW LABS

@gilby

gilby commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Re-ran the requested alternating N=3 A/B on idle Apple M5 Ultra (512 GB, applegpu_g17, GLM-5.3-Flash-MLX-4bit-MTP, --parallel 3 --prompt-cache-gib 0 --profile stable --no-update-check, tools/bench_concurrent.py --alone --serial --tokens 256 --reps 3 --temperatures 0,1.0) and confirmed the multi-row steady greedy gap persists, so I gated canHeadTop1 to rows == 1 only and removed the unreachable multi-row glm_head_top1_4b path.

1. 4-pair alternating N=3 A/B (b6701e51d5 base vs 0f641719d9, order base, head, head, base, base, head, head, base)

Zero thermal warnings (pmset -g therm), 0 failed/unmeasured requests, 0 alone/serial unequal checks, and identical greedy token_sha256 across every rep (7cd4ab7084c38db657bfe93edc32462fffad1e0b08efeb6e34fef78e3b7dcdaf on code, 4d09cdba4e48219b05493588d9c4072e0a28b22bfd90ee06bfd1ec71ff4d2ff9 on chat):

Cell Metric b6701e51d5 (4 runs, min..max) Med 0f641719d9 (4 runs, min..max) Med Paired deltas Paired med
code T=0.0 N=3 steady_tps 185.5, 185.1, 185.0, 185.2 185.15 183.9, 183.6, 183.7, 184.0 183.80 -0.86%, -0.81%, -0.70%, -0.65% -0.76%
code T=0.0 N=3 aggregate_tps 181.7, 181.4, 181.3, 181.5 181.45 180.2, 180.0, 180.0, 180.3 180.10 -0.83%, -0.77%, -0.72%, -0.66% -0.74%
chat T=0.0 N=3 steady_tps 163.5, 163.1, 163.2, 163.3 163.25 163.0, 162.8, 162.6, 162.9 162.85 -0.31%, -0.18%, -0.37%, -0.24% -0.28%
chat T=0.0 N=3 aggregate_tps 155.8, 155.4, 155.5, 155.5 155.50 155.3, 155.1, 155.0, 155.2 155.15 -0.32%, -0.19%, -0.32%, -0.19% -0.26%
code T=1.0 N=3 steady_tps 130.6, 130.2, 130.7, 130.7 130.65 129.7, 131.9, 129.7, 129.0 129.70 -0.69%, +1.31%, -0.77%, -1.30% -0.73%
code T=1.0 N=3 aggregate_tps 122.3, 117.6, 123.3, 122.4 122.35 124.6, 122.5, 123.0, 123.8 123.40 +1.88%, +4.17%, -0.24%, +1.14% +1.51%
chat T=1.0 N=3 steady_tps 121.2, 121.3, 121.4, 121.5 121.35 121.6, 121.3, 121.2, 121.7 121.45 +0.33%, +0.00%, -0.16%, +0.16% +0.08%
chat T=1.0 N=3 aggregate_tps 112.0, 111.5, 109.0, 112.1 111.75 112.7, 111.3, 111.8, 111.5 111.65 +0.63%, -0.18%, +2.57%, -0.54% +0.22%

Both greedy N=3 cells are slower across all 4 pairs with non-overlapping spreads, so keeping glm_head_top1_4b for multi-row launches does not pay off.

2. Follow-up commit 5487627791: restrict fused greedy head+top1 to rows == 1

  • zig/src/families/glm/kernels.zig: gated canHeadTop1 to rows == 1, simplified headTop1_4b and HeadTop1Args to dispatch only glm_head_top1_4b_one + glm_reduce_top1, and removed the dead head_top1_4b pipeline compilation.
  • zig/kernels/metal/glm_mtp_opt.metal, zig/src/core/contract/glm_mtp_records.zig, zig/tests/contract/metal_glm_mtp_opt.zig, zig/src/families/glm/mtp_opt_test.zig: dropped the unreachable multi-row glm_head_top1_4b entry point and updated limits, contracts (with Gate 1 .nonfinite stepping per-row over small(..., 5, 8)), and unit/Metal parity tests (glm_mtp_prep_4b + glm_head_top1_4b_one).

3. 2-pair alternating N=1,3 verification with the rows == 1 gate (b6701e51d5 vs gated binary, order base, gated, gated, base)

Run on the same idle Apple M5 Ultra (applegpu_g17, GLM-5.3-Flash-MLX-4bit-MTP, therm=0, 0 failed/unmeasured, 0 alone/serial unequal checks, identical greedy token_sha256 across all runs/reps) against the 1-row-gated build executing the exact same dispatched glm_mtp_prep_4b, glm_head_top1_4b_one, and glm_reduce_top1 kernels before deleting the unreachable multi-row entry point:

Cell Metric b6701e51d5 (v1, v4) Med rows == 1 (v2, v3) Med Paired deltas Paired med
code T=0.0 N=1 aggregate_tps 108.3, 108.1 108.20 108.4, 108.4 108.40 +0.09%, +0.28% +0.18%
code T=0.0 N=3 steady_tps 185.3, 185.2 185.25 185.2, 185.0 185.10 -0.05%, -0.11% -0.08%
chat T=0.0 N=1 aggregate_tps 88.0, 88.1 88.05 87.9, 86.8 87.35 -0.11%, -1.48% -0.79%
chat T=0.0 N=3 steady_tps 163.3, 163.2 163.25 162.9, 163.0 162.95 -0.24%, -0.12% -0.18%
code T=1.0 N=1 aggregate_tps 121.5, 121.5 121.50 121.7, 121.6 121.65 +0.16%, +0.08% +0.12%
code T=1.0 N=3 steady_tps 130.2, 129.2 129.70 131.3, 129.8 130.55 +0.84%, +0.46% +0.65%
chat T=1.0 N=1 aggregate_tps 82.6, 81.7 82.15 83.0, 82.8 82.90 +0.48%, +1.35% +0.92%
chat T=1.0 N=3 steady_tps 121.9, 121.1 121.50 121.3, 121.5 121.40 -0.49%, +0.33% -0.08%
  • code T=0.0 N=3 steady delta dropped from -0.76% (-1.35 tok/s) to -0.08% (-0.15 tok/s, aggregate_tps -0.06%).
  • On chat T=0.0 N=1, v3 dipped to 86.8 (rep_agg=[86.81, 88.21, 86.83] vs v1 [88.02, 88.04, 86.54], v2 [88.18, 87.92, 87.91], v4 [88.18, 88.12, 88.12]) due to the discrete adaptive depth-schedule transition (tokens_per_chunk=1.842, serial.accepted=120 vs 1.766 / 112 on v1/v2/v4), with identical token_sha256 throughout.

Checks on 5487627791

  • ./zig-out/bin/tf-kernel-contract --only "glm mtp": SUMMARY 3 kernels: 3 pass gates 1-4, 0 fail, 0 known findings; 6 controls: 6 red as planted, 0 missed; 0 limit problems; PASS limits: 4 selected registered Metal launches against M1-M5, 0 problems.
  • zig build tf-kernel-contract test-kernel-contract test-glm-kernel-ownership-host test-glm-scheduling-host test-glm-prefill-widths-host test-metal-residency-owners-host test-metal-runtime-source-modes-host --summary all: 101/101 steps succeeded.
  • Compiled engine test suite (tensorfold.zig): 1325 passed; 14 skipped; 1 failed — 0 families.glm.* failures, both families.glm.mtp_opt_test.test.* checks (tryHeadTop1 single-row admission and glm_mtp_prep_4b/glm_head_top1_4b_one Metal parity) passed OK (the lone failure is pre-existing unrelated families.deepseek_v41.metal.vision.test.the release's vision_config parses to the defaults returning error.ZeroHeads).
  • zig build native -Doptimize=ReleaseFast -Dcpu=apple_m1: 66/66 steps succeeded.
  • zig fmt --check build.zig zig, git diff --check origin/main...HEAD, python3 tools/pr_check.py --base origin/main (0 error(s), lean 165 vs 165, 0 new): passed.

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.

3 participants