Skip to content

glm metal: fuse 8-bit/64 shared expert with 4-bit/64 routed MoE - #4249

Open
gilby wants to merge 2 commits into
ashhart:mainfrom
gilby:glm-metal-moe-sh8
Open

gilby wants to merge 2 commits into
ashhart:mainfrom
gilby:glm-metal-moe-sh8

Conversation

@gilby

@gilby gilby commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

What this changes

Extends the fused Metal 4/64 MoE gather/down kernels (glm_moe_gateup_0_sh8, glm_moe_down_0_sh8, _one, and _pick) with uint4 8-bit/64-group shared-expert loads so mixed 4/64 routed + 8/64 shared layers stay on the single fused MoE launch (both + combine_0) instead of falling back to unfused wide.moe launches.

  • Adds sh8Source in zig/src/families/glm/kernels.zig (with [[max_total_threads_per_threadgroup(512)]] on the gateup kernels so M1/M2 offline limits match the runtime dispatch max of 512 threads) and loads the 6 sh8 pipelines alongside moe_gateup_0* / moe_down_0*.

  • Adds shared8 / canBoth and fused sh8 dispatch in zig/src/families/glm/moe.zig.

  • Registers glm sh8 contract launches, planted controls, M1/M2 offline records (zig/src/core/contract/glm_sh8_records.zig), and bit-exact parity test zig/src/families/glm/sh8_moe_test.zig.

  • Issue: Refs GLM-5.3-Flash on Metal: decode and prompt speed on M5 Ultra, token-exact (baseline, items and gate) #3152

  • Stacked on: main (4c616e63d57204cd8395dc3af9b572d9ec49626b)

  • atc overlap --base origin/main --json: 9 changed files; 6 clean PR overlaps, 4 older conflicting PRs (#1835, #2239, #2273, #2634), 5 unknown.

Checks on this head

  • Head commit: 59ffca4bb0b6cf3bc70f422f862fa3461d9b88a4
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 168 on base (4c616e63d), 168 on head (59ffca4bb), 0 new findings (python3 tools/pr_check.py --base origin/main exits 0).
  • Pre-fix check on main (4c616e63d): shared8 and sh8_moe_test are absent on main, where 8/64 shared + 4/64 routed experts fall back to unfused wide.moe + moe_combine instead of both (canBoth is false); sh8_moe_test fails on main before the change and passes bit-for-bit after.
  • Tests run on 59ffca4bb, 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 (sh8_moe_test.zig bit-for-bit against wide + moe_2) — All 185 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 sh8" --offline ... — passed (PASS limits: 6 selected registered Metal launches against M1-M5, 0 problems; all 6 M1/M2 offline limits pass: 512/512, 896/896, 512/512, 832/832, 512/512, 896/896; both limit controls red)
    • ./zig-out/bin/tf-kernel-contract --only "glm sh8" on Apple M5 Ultra (applegpu_g17, therm=0) — exited 0 (details below)
  • CI: GitHub Actions runs pending on PR submission.
  • Not run, and why: live M1/M2/M3/M4 runs (M1/M2 checked via --offline limits); CUDA/ROCm paths (Metal-only change).

Receipt

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

  • Main (4c616e63d) against branch (59ffca4bb), same Apple M5 Ultra (256 GB) and GLM-5.3-Flash-oQ4e-mtp-oq071 checkpoint (8/64 shared + 4/64 routed experts), --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 cell):
Cell main (4c616e63d) S3 (59ffca4bb) Diff main steady S3 steady Steady diff
code T=0.0 N=1 96.7 99.0 +2.38% — — —
code T=0.0 N=3 154.8 156.0 +0.78% 159.8 159.5 -0.19%
chat T=0.0 N=1 82.0 85.4 +4.15% — — —
chat T=0.0 N=3 138.7 139.5 +0.58% 145.8 147.2 +0.96%
code T=1.0 N=1 91.3 92.1 +0.88% — — —
code T=1.0 N=3 104.3 106.4 +2.01% 116.9 118.8 +1.63%
chat T=1.0 N=1 78.9 81.3 +3.04% — — —
chat T=1.0 N=3 108.5 107.0 -1.38% 113.4 113.4 +0.00%
  • 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 oq071 main oq071 S3 Diff MLX-4bit-MTP main MLX-4bit-MTP S3 Diff
fibonacci-raw T=0.0 96.70 98.89 +2.26% 116.76 116.83 +0.06%
gpu-chat-no-think T=0.0 80.88 85.22 +5.36% 95.86 95.85 -0.01%
fibonacci-raw T=1.0 87.12 87.58 +0.53% 115.86 115.81 -0.04%
gpu-chat-no-think T=1.0 77.45 79.39 +2.50% 89.21 89.09 -0.13%
  • Boxes it reaches: glm5_next on Metal (4/64 routed + 8/64 shared single-Mac decode/verify path); uniform 4/64 (MLX-4bit-MTP) unchanged.
  • 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: 59ffca4bb0b6cf3bc70f422f862fa3461d9b88a4 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: GLM-5.3-Flash-oQ4e-mtp-oq071 (8/64 shared + 4/64 routed) and TensorFold/GLM-5.3-Flash-MLX-4bit-MTP
  • Paths and platforms the change reaches, which I ran and which I could not:
    • Ran: macOS Apple M5 Ultra (256 GB) single-Mac GLM-5.3-Flash (oq071 and MLX-4bit-MTP), bit-exact parity unit test, host/golden tests, Metal contract + M1/M2 offline limits.
    • 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 oq071: 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 oq071 and MLX-4bit-MTP.
  • 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 (decode/verify single-Mac MoE change).
  • 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_moe_gateup_0_sh8, glm_moe_down_0_sh8, glm_moe_gateup_0_sh8_one, glm_moe_down_0_sh8_one, glm_moe_gateup_0_sh8_pick, glm_moe_down_0_sh8_pick — replaces separate wide.moe (glm_rows_q8) + moe_2 (moe_gateup_2/moe_down_2) for 8/64 shared + 4/64 routed MoE layers (bit-equal output to moe_2_and_wide_q8).
    • Registered in zig/src/core/contract/glm_sh8_records.zig, zig/src/core/contract/registry_joins.zig, zig/tests/contract/metal_glm_sh8.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 sh8 fused MoE takes shared slot one early (FAIL shapes)
    • control: glm sh8 pick takes wrong routed expert (FAIL shapes)
    • Limit controls: tf_row_projection_q4_f32 (2 limit problems), 36 KiB tf_ctl_big_tg (8 limit problems); 6 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 2 kernels: 2 pass gates 1-4, 0 fail, 0 known findings; 2 controls: 2 red as planted, 0 missed; 0 limit problems; gates 1144 ms, limits 40 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

Independent device run at 59ffca4b on M5 Max with Zig 0.17.0. I ran zig build tf-kernel-contract -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j3 --summary all, then ./zig-out/bin/tf-kernel-contract --only 'glm sh8' (exit 0).

SUMMARY 2 kernels: 2 pass gates 1-4, 0 fail, 0 known findings; 2 controls: 2 red as planted, 0 missed; 0 limit problems; gates 1454 ms, limits 18 ms

The exact head merges cleanly with 46c6ee22. The #4248 and #4254 public heads conflict with this one in GLM and the contract lists. The combined-head check should retain all six glm sh8 registered launch limits along with the two gated kernels in the SUMMARY. I verified the device contract here. Served-model timing and equality are covered by the separate checkpoint receipts.

After the DFlash2 merge, this head conflicts with 3025ce9d2 in registry_joins.zig and metal_main.zig. Keeping both the DFlash2 and sh8 entries in each file gave a passing M5 trial. The same glm sh8 command passed both kernels and caught both controls, with all six launch records passing. The separate --only "glm dflash2 accept" run also passed both kernels, caught its control and found zero limit problems. These runs cover the device contracts.

Igor Lessio - AIFLOW LABS

@ashhart

ashhart commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Thanks @gilby. Fusing the 8-bit/64 shared expert into the 4-bit/64 routed MoE reads correct: uniform 4-bit/64 layers keep their pipelines, the mixed layers match wide + moe_2 bit for bit in your tests, and your 12 token_sha256 matches agree.

The GLM DFlash2 work that landed on main conflicts with this branch in two places. We're rebasing it ourselves, keeping your commit as it is, and then running it on our M5 Ultra and M3 Ultra Macs. We'll post the results here.

  • Synthena

@ashhart
ashhart force-pushed the glm-metal-moe-sh8 branch 2 times, most recently from c14a50c to e003903 Compare November 21, 2026 18:52
gilby and others added 2 commits November 22, 2026 00:06
Extend the fused Metal MoE gather/down kernels (moe_gateup_0_sh8, moe_down_0_sh8, _one, and _pick) via uint4 8-bit/64 shared-expert loads so mixed 4/64 routed + 8/64 shared layers stay on the single fused path instead of falling back to unfused wide launches. Includes M1/M2 512-thread reservation, contract gates/controls/records, and bit-exact parity unit test.

Refs ashhart#3152
@AIFlowML

Copy link
Copy Markdown
Contributor

I checked current heads 9d2bcbc83 and #4248 63c5dbde5 against main 2ce240cf0. They trial-merge cleanly on their own. Taking #4248 first and then this PR conflicts in five registration, limits, and kernel files.

In a scratch resolution I kept both families’ registrations and tests, assigned KVB8 jobs to slots 27–29 and SH8 jobs to 30–35, and left the optional M3 job last at 36. On M5 Max with Zig 0.17.0, zig build test-glm-gathers test-glm-metal-dflash-host tf-kernel-contract -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j4 --summary all passed 51/51 steps and 21/21 tests. Both focused device runs exited 0.

  • tf-kernel-contract --only 'glm kvb8' passed two kernels and caught all four planted controls, with zero limit problems.
  • tf-kernel-contract --only 'glm sh8' passed two kernels and caught both planted controls, with zero limit problems.

The combined metal_main.zig is 602 lines and still needs to fit the repository’s 600-line limit before using this resolution. Full-checkpoint serving and M1/M2/M3 device runs were outside this check.

Igor Lessio - AIFLOW LABS

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