Skip to content

glm(metal): prune inactive DFlash2 rows, gate tap capture, and cut sampled transition cost - #4257

Merged
ashhart merged 2 commits into
ashhart:mainfrom
gilby:glm-metal-dflash2-w2
Nov 21, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
gilby:glm-metal-dflash2-w2

Conversation

@gilby

@gilby gilby commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Prunes inactive DFlash2 rows (metal_dflash_encode.zig), gates tap capture when no stream is actively drawing a DFlash2 chain (Taps.setActive), scatters the four HC streams' mean directly into the tap ring without an intermediate staging buffer (glm_tap_mean_ring in glm_glue.metal), and flattens sampled trees across active prefixes (metal_dflash_rounds.zig + metal_dflash_source.zig).

Checks on this head

  • Head commit: 1d57c823ffb7aa1ae54334d682dbe11abf9ce21e (rebased cleanly with range-diff = from 190e1fe04f9ccd13717c98afc3db5869d57b35d3)
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 168 on origin/main (d7dc085bfc), 168 on head (1d57c823ff), 0 new findings (python3 tools/pr_check.py --base origin/main exits 0; metal_main.zig 595 lines, metal_dflash_rounds.zig 596 lines).
  • Pre-fix check on base: glm_tap_mean_ring, metal_dflash_encode.zig, and Taps.setActive are absent on main, where Taps allocates a stage buffer and scatters every tap layer unconditionally.
  • Tests run on rebased head 1d57c823ff, with the command and pass, fail and skip counts:
    • zig fmt --check build.zig zig — passed (0 exit)
    • git diff --check origin/main..HEAD — passed (0 exit)
    • 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/95 steps succeeded; 340/340 tests passed (test-glm-metal-dflash-host 15/15, test-glm-scheduling-host / test-glm-prefill-widths-host 63/63, test-glm-metal-dflash-synthetic glm dflash2 follow: chains agree with today's pass on 2688 of 2688 drafts, test-kernel-contract 192/192 + sub-suites)
    • ./zig-out/bin/tf-kernel-contract --only "glm tap" on Apple Metal (applegpu_g17, tensor units true) — exited 0: SUMMARY 1 kernels: 1 pass gates 1-4, 0 fail, 0 known findings; 3 controls: 3 red as planted, 0 missed; 0 limit problems; gates 12 ms, limits 49 ms
  • Additional checks on receipted commit 190e1fe04f (git range-diff = to 1d57c823ff):
    • zig build test-golden — 322/335 & 69/69, 0 unexpected differences
    • families.glm.* unit tests — All 220 tests passed.
    • ./zig-out/bin/tf-kernel-contract --no-gates --only "glm tap" --offline ... — passed (PASS limits: 1 selected registered Metal launches against M1-M5, 0 problems; PASS offline glm tap glm_tap_mean_ring: 256 threads; M1 pipelines take 256, M2 256; both limit controls red)
  • CI: Rerunning on 1d57c823ff (was 9/9 green on 190e1fe04f prior to rebase).
  • Not run, and why: live M1/M2/M3/M4 runs (M1/M2 verified via --offline limits); CUDA/ROCm paths (Metal-only DFlash2 change).

Receipt

Full scrubbed receipts (taken at 190e1fe04f, git range-diff = to 1d57c823ff; contract log, bench_concurrent.py, bench_openai.py on MLX-4bit-MTP + --drafter --drafter-bits 4): https://gist.github.com/gilby/212629ac206e8ee2b64b8fb5618e68fd

  • glm metal: draft GLM-5.3-Flash with published DFlash2 behind --drafter #3920 (b9615a115aa) against 190e1fe04f, same Apple M5 Ultra (256 GB) and TensorFold/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 (0 unequal / 0 failed across every alone and serial check; timing_valid=True):
Cell #3920 (b9615a115aa) D1 (190e1fe04f) Diff #3920 steady D1 steady Steady diff
code T=0.0 N=1 161.8 162.0 +0.12% — — —
code T=0.0 N=3 183.8 181.1 -1.47% 189.5 188.8 -0.37%
chat T=0.0 N=1 97.2 97.0 -0.21% — — —
chat T=0.0 N=3 155.3 154.2 -0.71% 163.7 163.4 -0.18%
code T=1.0 N=1 128.5 128.2 -0.23% — — —
code T=1.0 N=3 133.6 133.4 -0.15% 138.6 139.2 +0.43%
chat T=1.0 N=1 87.2 89.1 +2.18% — — —
chat T=1.0 N=3 120.5 121.2 +0.58% 125.7 126.9 +0.95%
Prompt / Temp #3920 (b9615a115aa) D1 (190e1fe04f) Diff
fibonacci-raw T=0.0 161.19 161.73 +0.34%
gpu-chat-no-think T=0.0 97.09 97.14 +0.06%
fibonacci-raw T=1.0 121.08 121.47 +0.32%
gpu-chat-no-think T=1.0 89.17 90.61 +1.62%
  • Boxes it reaches: glm5_next on Metal with --drafter (single Mac, tp == 1).
  • 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: 1d57c823ffb7aa1ae54334d682dbe11abf9ce21e (rebased onto main d7dc085bfc, range-diff = vs receipted 190e1fe04f9ccd13717c98afc3db5869d57b35d3)
    • Toolchain: Zig 0.17.0 (-Dcpu=apple_m1), macOS Metal
    • Machine / GPU: Apple M5 Ultra (applegpu_g17, 256 GB unified memory, tensor units true)
    • Checkpoint / drafter: TensorFold/GLM-5.3-Flash-MLX-4bit-MTP with DFlash2 drafter (--drafter-bits 4)
  • Paths and platforms the change reaches, which I ran and which I could not:
    • Ran: macOS Apple M5 Ultra (256 GB) DFlash2 decode/verify, synthetic DFlash2 tests (2688/2688 drafts), 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:
  • 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 against parent glm metal: draft GLM-5.3-Flash with published DFlash2 behind --drafter #3920 in Gist above.
  • Decode speed before and after: see tables above (4/4 prompts faster in bench_openai.py, chat T=1.0 N=1 +2.18%).
  • Prompt speed before and after: removes Taps.stage allocation and skips tap writes for capped or non-DFlash streams; full prefill benchmark not re-run.
  • Licence of any new weights or drafter, or "n/a": uses existing DFlash2 drafter from glm metal: draft GLM-5.3-Flash with published DFlash2 behind --drafter #3920.
Kernel contract, only when a GPU kernel is new or changed
  • Kernels registered with the contract, and the kernel each one replaces:
    • glm_tap_mean_ring — computes the mean of four HC streams directly into the target ring slot, removing the intermediate Taps.stage copy (glm_stream_mean staging pattern).
    • Registered in zig/src/core/contract/glm_tap_records.zig, zig/src/core/contract/registry_joins.zig, zig/tests/contract/metal_glm_tap_ring.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:
    • glm tap control: writes into input (FAIL repeat)
    • glm tap control: misses ring wrap (FAIL shapes)
    • glm tap control: wrong scale 0.5 (FAIL shapes)
    • Limit controls: tf_row_projection_q4_f32 (2 limit problems), 36 KiB tf_ctl_big_tg (8 limit problems); 1 selected registered Metal launches against M1-M5, 0 problems
  • Each backend's SUMMARY line and exit status:
    • Apple Metal (applegpu_g17, tensor units true): SUMMARY 1 kernels: 1 pass gates 1-4, 0 fail, 0 known findings; 3 controls: 3 red as planted, 0 missed; 0 limit problems; gates 12 ms, limits 49 ms (exit 0)
  • Chips I could not run: Apple M1/M2/M3/M4 silicon (M1/M2 verified 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

@gilby
gilby force-pushed the glm-metal-dflash2-w2 branch from 190e1fe to 1d57c82 Compare November 21, 2026 06:21
@gilby
gilby marked this pull request as ready for review November 21, 2026 06:23
gilby and others added 2 commits November 21, 2026 17:21
…mpled transition cost

Prune inactive DFlash2 sequence rows, skip tap writes when no stream draws a DFlash2 chain (scattering stream means directly via glm_tap_mean_ring without the extra staging copy), and flatten sampled chains across active prefixes on top of PR ashhart#3920.

Stacked on ashhart#3920.
Refs ashhart#3152
@ashhart
ashhart force-pushed the glm-metal-dflash2-w2 branch from 1d57c82 to 0081969 Compare November 21, 2026 17:21
@ashhart
ashhart merged commit 1e9e1d1 into ashhart:main Nov 21, 2026
2 of 12 checks passed
@ashhart

ashhart commented Nov 21, 2026

Copy link
Copy Markdown
Owner

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.

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.

2 participants