Skip to content

glm metal: support intact 8-bit/64 kv_b projections - #4248

Open
gilby wants to merge 3 commits into
ashhart:mainfrom
gilby:glm-metal-kvb-q8
Open

gilby wants to merge 3 commits into
ashhart:mainfrom
gilby:glm-metal-kvb-q8

Conversation

@gilby

@gilby gilby commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

What this changes

Adds Metal runtime support for intact 8-bit group-64 kv_b_proj weights alongside native 4-bit/64 routed experts on single-Mac GLM-5.3-Flash loads without widening or QSplit (GLM-5.3-Flash-oQ4e-m5u256-hq).

  • Allows kv_b_proj at 8-bit/64-group in zig/src/families/glm/affine.zig (kvb8) alongside native 4/64 experts.

  • Adds single-pass glm_absorb_8b and glm_unabsorb_8b decode/verify kernels in zig/kernels/metal/glm_kvb_q8.metal and wires dispatch in zig/src/families/glm/kernels.zig and zig/src/families/glm/forward.zig.

  • Extends zig/kernels/metal/glm_absorb_nax.metal (and zig/src/families/glm/prompt.zig) to read 8-bit/64-group kv_b_proj for prompt prefill (TP=1 and TP=2).

  • Registers glm kvb8 launches, planted controls, limits (zig/src/core/contract/glm_kvb_q8_records.zig), and unit parity tests (zig/src/families/glm/kvb_q8_test.zig, zig/src/families/glm/weights_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: 15 changed files; 6 clean overlaps against open PRs, 4 older conflicting PRs (#1835, #2239, #2273, #2634), 5 unknown.

Checks on this head

  • Head commit: b5de18d0c72c92bf68a96cc46e07a7fb547fa2dc
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 168 on base (4c616e63d), 168 on head (b5de18d0c7), 0 new findings (python3 tools/pr_check.py --base origin/main exits 0).
  • Pre-fix fail on main (4c616e63d):
    • tensorfold-native serve GLM-5.3-Flash-oQ4e-m5u256-hq exits 1 (UnsupportedWidth: error: glm: model.language_model.layers.3.self_attn.kv_b_proj is 8-bit in groups of 64, a width the kv_b kernels do not read).
    • 8/64 intact kv_b_proj alongside 4/64 routed experts plans without QSplit fails on main (4c616e63d) before the change and passes after.
  • Tests run, 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 — 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 kvb8" --offline ... — passed (PASS limits: 2 selected registered Metal launches against M1-M5, 0 problems; PASS offline glm kvb8 glm_absorb_8b: 512 threads; M1 pipelines take 512, M2 512 (recorded 0, 0); PASS offline glm kvb8 glm_unabsorb_8b: 512 threads; M1 pipelines take 512, M2 512 (recorded 0, 0); both limit controls red)
    • ./zig-out/bin/tf-kernel-contract --only "glm kvb8" 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 (pre-fix refusal, contract log, bench_concurrent.py, bench_openai.py, bench_cache.py): https://gist.github.com/gilby/374894a27bbab2284886828b8741777d

  • Zero-regression check against main (4c616e63d), same Apple M5 Ultra (256 GB) and TensorFold/GLM-5.3-Flash-MLX-4bit-MTP checkpoint (tools/bench_openai.py --tokens 256 --reps 3 --temperatures 0,1.0, --prompt-cache-gib 0, therm=0, 12/12 reply token_sha256s bit-identical):
Prompt / Temp main (4c616e63d) S1 (b5de18d0c7) Diff
fibonacci-raw T=0.0 116.76 117.22 +0.39%
gpu-chat-no-think T=0.0 95.86 96.04 +0.19%
fibonacci-raw T=1.0 115.86 115.86 +0.00%
gpu-chat-no-think T=1.0 89.21 89.12 -0.10%
  • Enabled checkpoint (GLM-5.3-Flash-oQ4e-m5u256-hq — refused on main with UnsupportedWidth) on Apple M5 Ultra (256 GB), --prompt-cache-gib 0 --profile stable, therm=0:

    • bench_concurrent.py --alone --serial --tokens 256 --reps 3 --temperatures 0,1.0 --levels 1,3 (0 unequal / 0 failed across every cell):
      • code T=0.0 N=1: 94.8, N=3: 156.8 (steady 163.0)
      • chat T=0.0 N=1: 78.5, N=3: 124.0 (steady 130.3)
      • code T=1.0 N=1: 91.7, N=3: 119.7 (steady 124.5)
      • chat T=1.0 N=1: 74.7, N=3: 111.1 (steady 115.7)
    • bench_openai.py: fibonacci-raw T=0.0 94.64, gpu-chat-no-think T=0.0 78.47, fibonacci-raw T=1.0 86.67, gpu-chat-no-think T=1.0 76.21.
    • bench_cache.py --require-cold: 2k TTFTs [0.997, 0.994, 0.996] (0.996s median, qualified: true), 8k TTFTs [3.881, 3.879, 3.880] (3.880s median, qualified: true).
  • Boxes it reaches: glm5_next on Metal (kv_b_proj in 8-bit/64-group alongside native 4-bit/64-group routed experts; 4-bit kv_b_proj path 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: b5de18d0c72c92bf68a96cc46e07a7fb547fa2dc 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-m5u256-hq (intact 8-bit/64-group kv_b_proj + 4-bit/64-group experts) 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 (oQ4e-m5u256-hq and MLX-4bit-MTP), unit parity tests, host/golden tests, Metal contract + M1/M2 offline limits.
    • Could not run: TP=2 multi-Mac hardware, 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 GLM-5.3-Flash-oQ4e-m5u256-hq: 0 unequal and 0 failed across all alone (concurrent == alone) and serial (drafted == "draft": false) checks.
    • bench_openai.py on MLX-4bit-MTP: 12/12 reply token_sha256s bit-identical to main.
  • 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); oQ4e-m5u256-hq fails to load on main (UnsupportedWidth log in Gist), and MLX-4bit-MTP zero-regression receipts are in Gist.

  • Decode speed before and after: see MLX-4bit-MTP parity table above; enables oQ4e-m5u256-hq (94.64 tok/s fibonacci-raw T=0.0).

  • Prompt speed before and after: oQ4e-m5u256-hq cold TTFT 0.996s at 2K, 3.880s at 8K (fails to load on main).

  • 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_absorb_8b + glm_unabsorb_8b (zig/kernels/metal/glm_kvb_q8.metal) — adds intact 8-bit/64-group support alongside glm_absorb / glm_unabsorb (glm_absorb_nax.metal 8-bit path verified by synthetic unit parity tests in kvb_q8_test.zig).
    • Registered in zig/src/core/contract/glm_kvb_q8_records.zig, zig/src/core/contract/registry_joins.zig, zig/tests/contract/metal_glm_kvb_q8.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 kvb8 glm_absorb_8b writes into input (FAIL repeat)
    • control: glm kvb8 glm_absorb_8b drops tail cols (FAIL shapes)
    • control: glm kvb8 glm_unabsorb_8b writes into input (FAIL repeat)
    • control: glm kvb8 glm_unabsorb_8b drops tail rows (FAIL shapes)
    • Limit controls: tf_row_projection_q4_f32 (2 limit problems), 36 KiB tf_ctl_big_tg (8 limit problems); 2 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; 4 controls: 4 red as planted, 0 missed; 0 limit problems; gates 108 ms, limits 12 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

Copy link
Copy Markdown
Contributor

Independent device run at b5de18d0 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 kvb8' (exit 0).

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

The exact head merges cleanly with main 46c6ee22. Pairing the public heads of this and #4249 yields five conflicts, including GLM kernel wiring and contract registrations. If both share a train, rerun both filtered contracts on its resolved SHA to verify both sets of registrations survived. This was a device contract run without checkpoint loading.

Igor Lessio - AIFLOW LABS

@ashhart

ashhart commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Thanks @gilby. The 8-bit/64 kv_b kernels and their dispatch read clean, and a uniform 4-bit/64 checkpoint keeps its current path, so default output is unchanged.

Since you opened this, the GLM DFlash2 registrations landed on main, which conflicts with this branch in registry_joins.zig and metal_main.zig. We're rebasing it onto current main ourselves, keeping your commit as it is. One extra commit trims a line from zig/src/families/glm/prompt.zig, which is already 600 lines on main, so the rebase stays within our 600-line cap. Then we'll run the GLM kernel contract and an 8-bit/64 kv_b checkpoint on our M5 Ultra (tensor units) and M3 Ultra Macs, and report back here.

  • Synthena

ashhart added a commit to gilby/TensorFold that referenced this pull request Nov 21, 2026
ashhart added a commit to gilby/TensorFold that referenced this pull request Nov 21, 2026
gilby and others added 3 commits November 22, 2026 00:08
Allow 8-bit group-64 kv_b_proj weights alongside native 4/64 experts on single-Mac loads without widening or QSplit, adding glm_kvb_q8 kernels (glm_absorb_8b and glm_unabsorb_8b), 8-bit glm_absorb_nax support for TP=1 and TP=2, kernel contract records/gates/controls, and parity unit tests.

Refs ashhart#3152
@AIFlowML

Copy link
Copy Markdown
Contributor

The current 63c5dbde head trial-merges cleanly into main d21617008. On M5 Max with Zig 0.17.0, the host contract suite passed 287 tests and the targeted Metal run passed both kv_b Q8 kernels. All four planted controls failed as expected and the runner reported zero limit problems.

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

This covers the absorption and unabsorption kernel contracts on the current trial tree. The release-checkpoint timing and any combined MoE comparison remain separate.

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