Skip to content

glm metal: vectorize pre-M5 Metal prompt, KDA, and indexer kernels - #3921

Merged
ashhart merged 8 commits into
ashhart:mainfrom
gilby:glm-metal-m3-ultra
Nov 21, 2026
Merged

ashhart merged 8 commits into
ashhart:mainfrom
gilby:glm-metal-m3-ultra

Conversation

@gilby

@gilby gilby commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

On pre-M5 Apple Silicon Macs without tensor units (!device.tensorUnits(), including Apple M3 Ultra), GLM-5.3-Flash prompt execution previously fell back to TF_SIMD_FRAGS scalar loops in zig/kernels/metal/nax.h and zig/kernels/metal/core/affine_mm.metal (BM=64 with scalar 2-byte reads), scalar KDA prep/scan/post memory loads in zig/kernels/metal/glm_kda_prompt.metal, and scalar indexer key loads in zig/kernels/metal/glm_index_nax.metal. This change adds GLM-scoped TF_GLM_M3_FAST Metal fast paths (<metal_simdgroup_matrix> 32x32/32x64 tiling with vectorized global/threadgroup loads and BM=32 dense/gather dispatches, vectorized bfloat2/float2/float4 KDA prep/post + SG=4, B=24 glm_kda_scan/_tp, and vectorized bfloat2 tileT + mma_16x16 indexer scores/decode) enabled only by zig/src/families/glm/{kernels,wide}.zig when !device.tensorUnits(), keeping M5 tensor-unit paths and all non-GLM Metal families byte-unchanged and model logits/tokens bit-identical.

  • Issue: Refs GLM-5.3-Flash on Metal: decode and prompt speed on M5 Ultra, token-exact (baseline, items and gate) #3152 (pre-M5 / M3 Ultra Metal prompt, KDA, and indexer optimization).
  • Stacked on: main (a1781885e921e9a772c641fef26010557e1349da), 1 commit (31172351fe92a71b6e32695bbfea05bb2120554d; full-model served A-B-B-A and tf-glm-run receipts were collected against f5765b51254d7ec7ef1295e23cb500f328165600 at b42f0ab551b8857b928a20be1248b331a359f25c with byte-identical kernel, engine, glm_rows_*, and metal_glm_prompt sources — rebasing onto a1781885e9 only merged upstream metal_limits.zig and metal_main.zig contract suite registrations, and all unit, golden, launch-map, tf-glm-rows-check, and tf-kernel-contract checks were re-executed on 31172351fe on both M3 Ultra and M5 Ultra).
  • atc overlap --json: Run on 31172351fe92a71b6e32695bbfea05bb2120554d against origin/main (a1781885e9); complete: false (myPullRequestsLimit, otherPullRequestsLimit, pullFilesUnavailable, timeLimit). Overlaps only stale PRs #1773, #1774, #1775, #1780, #1787, #1858 (shared contract registries metal_main.zig / metal_limits.zig); zero source overlap with active GLM PRs #3894, #3903, #3906; git merge-tree --write-tree origin/main HEAD succeeds cleanly (52de2e8223d6743f64bba2a5aebc3057d559a9c7).

Checks on this head

  • Head commit: 31172351fe92a71b6e32695bbfea05bb2120554d
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 221 vs 221, 0 new (tools/zig/lean_new.py against origin/main a1781885e9; tools/pr_check.py --base origin/main passes with 0 error(s), 1 warning(s) pre-PR without --pr NUMBER; CHANGELOG.md intentionally left unmodified to avoid merge-train conflicts).
  • Tests run, with the command and pass, fail and skip counts (all on 31172351fe):
    • git diff --check origin/main: passed (0 exit).
    • zig fmt --check build.zig zig: passed (0 exit) on both M3 Ultra and M5 Ultra.
    • Apple M3 Ultra (applegpu_g15):
      • zig build test --summary all -Dcpu=apple_m1: Build Summary: 895/895 steps succeeded; 10389/10491 tests passed (102 skipped) (0 exit).
      • zig build test-golden -Dcpu=apple_m1: 322/335 + 55/55 equal to server goldens, 0 unexpected differences (0 exit).
      • zig build test-metal-launch-map-host -Dcpu=apple_m1: Metal launch map: 862 registered launches, 2 controls, 0 missing (0 exit).
      • zig build test-glm-sampling -Dcpu=apple_m1: 15/15 cases passed (0 exit).
      • zig build native dev-tools tf-kernel-contract -Dcpu=apple_m1 -Dversion=1.0.0: passed (0 exit).
      • ./zig-out/bin/tf-glm-rows-check: PASS 1328072 values within the fp32-sum bound (worst err/bound 0.9839), 10809776 bit-equal pairs (0 exit).
      • ./zig-out/bin/tf-kernel-contract --only "glm m3", --only affine_mm, --only "metal flint" --no-limits, --only glm --no-limits: all 0 exit (see Kernel contract section).
    • Apple M5 Ultra (applegpu_g17):
      • zig build test --summary all -Dcpu=apple_m1: Build Summary: 895/895 steps succeeded; 10383/10491 tests passed (108 skipped) (0 exit).
      • zig build test-golden -Dcpu=apple_m1: 322/335 + 55/55 equal to server goldens, 0 unexpected differences (0 exit).
      • zig build test-metal-launch-map-host -Dcpu=apple_m1: Metal launch map: 862 registered launches, 2 controls, 0 missing (0 exit).
      • zig build test-glm-sampling -Dcpu=apple_m1: 15/15 cases passed (0 exit).
      • zig build native dev-tools tf-kernel-contract -Dcpu=apple_m1 -Dversion=1.0.0: passed (0 exit).
      • ./zig-out/bin/tf-glm-rows-check: PASS 1328072 values within the fp32-sum bound (worst err/bound 0.9839), 10809776 bit-equal pairs (0 exit).
      • ./zig-out/bin/tf-kernel-contract --only "glm m3", --only affine_mm, --only glm --no-limits: all 0 exit (see Kernel contract section).
  • CI: CANCELLED (infrastructure — repo-wide 07:00:25Z–07:00:35Z Actions queue flush cancelled all queued PR runs before execution, zero test failures): Zig format (44467138925) (fmt CANCELLED) and No GPU (44467139134) (zig, zig-b, zig-windows, zig-macos-dev-tools, pytest, benchmark-clients (ubuntu-latest), benchmark-clients (windows-latest), release-paths (ubuntu-24.04) CANCELLED; host-nightly-linux, host-nightly-macos SKIPPED). Equivalent zig fmt --check, zig build test test-golden test-metal-launch-map-host test-glm-sampling native dev-tools tf-kernel-contract -Dcpu=apple_m1, tf-glm-rows-check, and tf-kernel-contract checks passed on 31172351fe on both M3 Ultra and M5 Ultra above.
  • Not run, and why: Apple M1/M2/M4 on-device runs (targeted Apple M3 Ultra + Apple M5 Ultra verification; all binaries built -Dcpu=apple_m1 and Metal launch limits audited against M1–M5), physical two-Mac expert-parallel pair (single-Mac M3 Ultra and M5 Ultra verified), CUDA, HIP, Vulkan, XPU, Windows, desktop/mobile app (Metal-only, GLM-scoped via TF_GLM_M3_FAST on !device.tensorUnits()).

Receipt

  • Main against branch: 5-arm symmetric served A-B-B-A (discard, base, this, this, base, fresh tensorfold-native per arm) on Apple M3 Ultra with TensorFold/GLM-5.3-Flash-MLX-4bit-MTP, --parallel 4 --context 73728 --prompt-cache-gib 16 (--require-cold asserts cached_tokens == 0 on all prompt rows), notifyutil -g com.apple.system.thermalpressurelevel == 0 (Nominal) at start and end of every arm and start of every cell (32k and 64k cells warmed to 2 at cell end and cooled to 0 before the next cell). Scrubbed raw artifacts and summary: https://gist.github.com/bf81a61e01ab643adc491a4ac7b9af06
    • Cold prompts (tools/bench_cache.py --require-cold, cold first-request ttft_s across [arm1_base, arm4_base] vs [arm2_this, arm3_this]):
      • 2K (2048 tok, token_sha 7221558f1eed across all 4 arms): base 3.741s (3.7407s, 3.7416s, 547.4 tok/s) vs candidate 3.529s (3.5287s, 3.5287s, 580.4 tok/s) — +5.68% (-0.212s)
      • 8K (8192 tok, token_sha d3af62c4c734 across all 4 arms): base 15.511s (15.5088s, 15.5127s, 528.2 tok/s) vs candidate 14.670s (14.6647s, 14.6745s, 558.4 tok/s) — +5.42% (-0.841s)
      • 32K (32768 tok, token_sha 180c0e33028c across all 4 arms): base 62.644s (62.6371s, 62.6511s, 523.1 tok/s) vs candidate 59.392s (59.3829s, 59.4008s, 551.7 tok/s) — +5.19% (-3.252s)
      • 64K (65536 tok, token_sha b4c387a6fc98 across all 4 arms): base 126.547s (126.5347s, 126.5597s, 517.9 tok/s) vs candidate 120.009s (119.9872s, 120.0314s, 546.1 tok/s) — +5.17% (-6.538s)
    • Direct engine parity & prompt-profile (tf-glm-run on M3 Ultra b42f0ab551): 0/64 token diffs and max |diff| 0.0000 (0.00 bf16 steps) across fib-short (10 tok, depth 0,3, f8cc3868c5dc23d8), sys-65 (65 tok, depth 0,3, 8a0998c8c6837cee), sys-2048 (2048 tok, depth 0,3, ad4e7d04cc3c9ecb), plus GLM_PROMPT=0 vs 1 (0 diffs); GLM_PROMPT_PROFILE=3 (2048 rows): full 3722.29 -> 3508.75 ms (-213.54 ms, 550 -> 583 tok/s), kda 1050.84 -> 926.32 ms (-11.8%).
    • M5 Ultra zero-regression check (tf-glm-run on b42f0ab551, Nominal 0 -> 0): 0/64 token diffs and max |diff| 0.0000 across fib-short (67b08d14da0c1132), sys-65 (8a0998c8c6837cee), sys-2048 (ad4e7d04cc3c9ecb); sys-2048 prompt 2015 -> 2017 tok/s, depth 0 decode 69.0 -> 69.1 tok/s, depth 3 decode 121.1 -> 121.4 tok/s.
  • Boxes it reaches: Apple Silicon Macs running native Metal GLM-5.3-Flash when !device.tensorUnits() (pre-M5 Macs including M1–M4); M5 Macs (device.tensorUnits()) keep their existing tensor-unit paths; non-GLM Metal families (step5, spark25, flint, minimax_m3, lfm2_moe, etc.) keep glm_m3_fast = false and compile byte-unchanged sources.
  • New switch? Default, accepted values, profile row: None. Activated automatically for GLM on Metal devices where !device.tensorUnits(). No optimization-ledger profile row needed.
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: Base origin/main a1781885e921e9a772c641fef26010557e1349da, head 31172351fe92a71b6e32695bbfea05bb2120554d (served A-B-B-A measured on b42f0ab551b8857b928a20be1248b331a359f25c vs f5765b51254d7ec7ef1295e23cb500f328165600, identical kernel/engine sources); Zig 0.17.0; Apple Metal toolchain 32023.922 (metalfe-32023.922.4) (Metal 4); Apple M3 Ultra (80-core GPU, 512 GiB RAM, macOS 26.6.2 25G83) and Apple M5 Ultra (80-core GPU, 512 GiB RAM, macOS 27.2.1 26B131); checkpoint TensorFold/GLM-5.3-Flash-MLX-4bit-MTP (rev d2b17f5253440224422b150705c1109a9a3a0368, SHA-256 verified).
  • Paths and platforms the change reaches, which I ran and which I could not: Reaches zig/kernels/metal/{nax.h, core/affine_mm.metal, glm_kda_prompt.metal, glm_index_nax.metal}, zig/src/core/affine_mm.zig, zig/src/families/glm/{checks,forward,kernels,prompt,wide}.zig, zig/tests/glm_rows_*.zig, and zig/tests/contract/. Ran on Apple M3 Ultra and Apple M5 Ultra (single-Mac host); M1/M2/M4 on-device, physical two-Mac EP pair, and CUDA/HIP/Vulkan/XPU/Windows/app not run (Metal limits checked M1–M5; built -Dcpu=apple_m1).
  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false:
    • bench_concurrent.py --levels 1,2,4 --alone --tokens 64 --reps 2 --temperatures 0: unequal = 0, failed = 0 across every arm (code and chat at L=1,2,4: equal = 2, 4, 8 respectively per arm).
    • bench_openai.py: identical token_sha across all 4 arms x 3 reps (f200e0f785fa, e1f620931114, 4f10a4283d0e for fibonacci-raw T=1.0; 7d9f8501fedf, f8913e9ac80d, 20b71bc7d40a for gpu-chat-no-think T=1.0; d2f13da2874c for fibonacci-raw T=0; f24eca094b5c for gpu-chat-no-think T=0).
    • tf-glm-run: depth 3 (--draft) == depth 0 (0/64 diffs) and 0.0000 max logit diff (0.00 bf16 steps) on both M3 Ultra and M5 Ultra; GLM_PROMPT=0 == 1.
  • Main against this branch, same machine and checkpoint, posted before asking for a run on our machines (or why you can't): Posted above and in https://gist.github.com/bf81a61e01ab643adc491a4ac7b9af06.
  • Decode speed before and after, from tools/bench_openai.py:
    • fibonacci-raw, T=0.0: base 69.65 (69.715, 69.583) vs candidate 69.59 (69.686, 69.489) tok/s; TTFT 0.112s -> 0.112s.
    • gpu-chat-no-think, T=0.0: base 62.87 (62.969, 62.762) vs candidate 62.92 (62.990, 62.851) tok/s; TTFT 0.277s -> 0.237s (-14.4%).
    • fibonacci-raw, T=1.0: base 61.62 (60.231, 63.008) vs candidate 60.14 (60.258, 60.025) tok/s; TTFT 0.113s -> 0.113s.
    • gpu-chat-no-think, T=1.0: base 57.94 (58.032, 57.840) vs candidate 57.85 (57.953, 57.750) tok/s; TTFT 0.277s -> 0.237s (-14.4%).
    • bench_concurrent.py aggregate / per-stream (T=0.0): code L=1 69.70 -> 69.60, code L=2 96.75 -> 96.85, code L=4 100.15 -> 100.15; chat L=1 63.40 -> 62.75, chat L=2 77.85 -> 79.85 (+2.57%), chat L=4 80.20 -> 83.35 (+3.93%).
  • Prompt speed before and after: cold first request ttft_s from tools/bench_cache.py --require-cold improves across all tested lengths on M3 Ultra (2K: 3.741s -> 3.529s, +5.68%; 8K: 15.511s -> 14.670s, +5.42%; 32K: 62.644s -> 59.392s, +5.19%; 64K: 126.547s -> 120.009s, +5.17%).
  • Licence of any new weights or drafter, or "n/a": n/a (uses existing TensorFold/GLM-5.3-Flash-MLX-4bit-MTP checkpoint, no new weights or drafter).
Kernel contract, only when a GPU kernel is new or changed
  • Kernels registered with the contract, and the kernel each one replaces:
    • metal affine_mm glm m3 q4 g64, q4 g64 fp32 out, q2 g64, q5 g64, q6 g64, q8 g64, q4 g32, q4 g128 (zig/tests/contract/metal_main.zig, metal_limits.zig): replaces BM=64 scalar TF_SIMD_FRAGS tf_affine_mm under TF_GLM_M3_FAST for !device.tensorUnits().
    • metal affine_mm glm m3 q4 g64 gather, metal affine_mm glm m3 q8 g64 gather scatter (zig/tests/contract/metal_affine_gather.zig): replaces BM=64 TF_SIMD_FRAGS tf_affine_gather_* under TF_GLM_M3_FAST.
    • metal glm m3 index_scores_nax, metal glm m3 index_decode, metal glm m3 kda_prep, metal glm m3 kda_scan, metal glm m3 kda_scan_tp, metal glm m3 kda_post (zig/tests/contract/metal_glm_prompt.zig): replaces unvectorized pre-M5 GLM index and KDA prompt/scan/post paths (bit-identical to baseline across shapes and non-finite inputs).
  • A planted control for each new kernel, and each one fails as planted:
    • control: metal affine_mm glm m3 q4 g64, wrong row tile height (red at shapes, as planted)
    • control: metal affine_mm glm m3 gather q4 g64, each expert's last row dropped (red at shapes, as planted)
    • control: metal glm m3 index_scores_nax, wrong row tile height (red at shapes, as planted)
    • control: metal glm m3 index_decode, wrong block tile width (red at shapes, as planted)
    • control: metal glm m3 kda_prep, wrong head tile grid (red at shapes, as planted)
    • control: metal glm m3 kda_scan, half value-column grid (red at shapes, as planted)
    • control: metal glm m3 kda_post, wrong head tile grid (red at shapes, as planted)
  • Each backend's SUMMARY line and exit status:
    • Metal (Apple M3 Ultra, applegpu_g15, tensor units false):
      • ./zig-out/bin/tf-kernel-contract --only "glm m3" (0 exit): SUMMARY 16 kernels: 16 pass gates 1-4, 0 fail, 5 known findings; 7 controls: 7 red as planted, 0 missed; 0 limit problems; gates 58671 ms, limits 35 ms
      • ./zig-out/bin/tf-kernel-contract --only affine_mm (0 exit): SUMMARY 14 kernels: 14 pass gates 1-4, 0 fail, 7 known findings; 2 controls: 2 red as planted, 0 missed; 0 limit problems; gates 76846 ms, limits 35 ms
      • ./zig-out/bin/tf-kernel-contract --only "metal flint" --no-limits (0 exit): SUMMARY 21 kernels: 21 pass gates 1-4, 0 fail, 0 known findings; 16 controls: 16 red as planted, 0 missed; 0 limit problems; gates 144172 ms, limits 0 ms
      • ./zig-out/bin/tf-kernel-contract --only glm --no-limits (0 exit): SUMMARY 73 kernels: 73 pass gates 1-4, 0 fail, 5 known findings; 31 controls: 31 red as planted, 0 missed; 0 limit problems; gates 1535777 ms, limits 0 ms
    • Metal (Apple M5 Ultra, applegpu_g17, tensor units true):
      • ./zig-out/bin/tf-kernel-contract --only "glm m3" (0 exit): SUMMARY 16 kernels: 16 pass gates 1-4, 0 fail, 5 known findings; 7 controls: 7 red as planted, 0 missed; 0 limit problems; gates 48296 ms, limits 33 ms
      • ./zig-out/bin/tf-kernel-contract --only affine_mm (0 exit): SUMMARY 18 kernels: 18 pass gates 1-4, 0 fail, 9 known findings; 5 controls: 5 red as planted, 0 missed; 0 limit problems; gates 92860 ms, limits 37 ms
      • ./zig-out/bin/tf-kernel-contract --only glm --no-limits (0 exit): SUMMARY 89 kernels: 89 pass gates 1-4, 0 fail, 13 known findings; 31 controls: 31 red as planted, 0 missed; 0 limit problems; gates 1258714 ms, limits 0 ms
    • CUDA / HIP / Vulkan / XPU: not run (Metal-only change).
  • Chips I could not run: Apple M1, M2, M4 (built -Dcpu=apple_m1, Metal limits verified against M1–M5).

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

@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thank you @gilby. The pre-M5 fast path is scoped to GLM and to Macs without tensor units, with the existing kernels and non-GLM families unchanged. Your M3 Ultra and M5 Ultra receipts and the re-run checks cover what we need; nothing further is required from you here.

  • Synthena

@joshuaswarren

Copy link
Copy Markdown
Contributor

[Joshua's agent] Re-reading this thread end to end in a handover: the 07:28Z review asked nothing further from the author side, and nothing in the thread or the diff since changes that. One state note: the branch now shows DIRTY against current main because main moved after the review — a landing-queue merge or a rebase settles that, and no author-side code change is pending.

@ashhart

ashhart commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Thank you @gilby. Our last review asked nothing further, and nothing in the thread or the current head changes that: the fast path stays scoped to GLM on Macs without tensor units, the M5 and non-GLM paths are untouched, and your served receipts on both Macs show no token differences with faster cold prompts (5.17% to 5.68% from 2K to 64K tokens).

Main has moved since your branch was cut: the GLM index kernel was rewritten, so the dispatch and key-list changes need re-applying, and the touched files must stay at or below 600 lines. We will do that rebase on our side, keeping your authorship, and check it on a Mac before it lands. If main's rewrite turns out to need a different design for your change, we will say so here.

  • Synthena

@AIFlowML

Copy link
Copy Markdown
Contributor

I checked 31172351fe on M5 Max with Zig 0.17.0. zig build test-metal-launch-map-host test-glm-sampling -Dcpu=apple_m1 -j6 --summary all passed, with 862 registered launches and none missing. I also ran zig build tf-kernel-contract -Dcpu=apple_m1 && ./zig-out/bin/tf-kernel-contract --only 'metal glm m3' --no-limits. All six kernels passed and all five planted controls were caught. Despite the filter name, the run reports tensor units true and leaves TF_GLM_M3_FAST off, so this covers the M5 path.

For the maintainer rebase, I verified the newer indexer check on current main 1ece6e6db: zig build tf-glm-prompt-select -Dcpu=apple_m1 && ./zig-out/bin/tf-glm-prompt-select. It reported bit-equal scores and key lists at all eight positions from 2,048 to 120,000. The trial merge conflicts in glm_index_nax.metal and GLM's forward.zig, kernels.zig, and prompt.zig. Repeating that long-position check on both paths after resolving the dispatch makes a useful companion to the M3 Ultra receipt.

Igor Lessio - AIFLOW LABS

@AIFlowML

AIFlowML commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

I reran the selector check after the rebase. Head e77116249b merged cleanly into 523d58cbe3 in a scratch worktree on an M5 Max.

zig build tf-glm-prompt-select -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j3 --summary all
./zig-out/bin/tf-glm-prompt-select

43/43 tests passed. The synthetic indexer produced identical scores and selected key lists for all eight positions from 2,048 to 120,000, using 256 rows per position. The earlier M3 Ultra run in the PR description covers pre-M5 hardware on its recorded revision.

On the later head f92572f94 merged into e3efa80e5, zig build test-glm-kernel-ownership-host test-glm-window-contract-host -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j3 --summary all passed all 21 host tests. That includes both ownership fixtures, which verify that each created pipeline is released exactly once with tensor units on and off.

On the combined #4279 train at efc117ebd, the following checks passed on M5 Max:

zig build test-flashnext-xpu-host test-qwen27-config-xpu-host test-qwen27-prompt-tail-xpu-host test-xpu-refusal-host test-glm-kernel-ownership-host test-glm-window-contract-host -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j3 --summary all

35/35 host tests passed. The selector command above again matched scores and lists at all eight positions, and the benchmark fixture suite passed 10/10. These are combined-train host checks. Device coverage remains separate.

The revised head c4f282c93 trial-merges cleanly with both 3025ce9d2 and the new f0cc0a134. On the first trial, zig build test-gemma4-affine-host test-metal-launch-map-host test-glm-sampling tf-glm-prompt-select -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j3 --summary all passed 206 tests with four skips on M5 Max, including the updated Gemma affine pin. The selector passed all eight bit-equality positions from 2,048 to 120,000. This is an M5 rerun.

Igor Lessio - AIFLOW LABS

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
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 8 commits November 21, 2026 14:48
Optimize GLM-5.3-Flash Metal prompt matmul, KDA prep/scan/post, and indexer kernels on !tensorUnits() Apple Silicon GPUs via GLM-scoped TF_GLM_M3_FAST paths and 32-row affine_mm tiling, keeping M5 tensor-unit paths and output tokens bit-identical. Refs ashhart#3152.
Create only grouped pipelines from the combined library before M5, leaving the affine pipelines owned by the separate library. Exercise the actual loader and destructor on both capability paths without a device.
…fter main's ashhart#3920

Format only: the unabsorb strides go back inline and the limits gate is one line. Nothing else changes.
…alf tiles

metal_main.zig grows with every family and sat over 600 lines with main; the
nine glm_m3_fast registrations join the list as affine_k.glm_m3, as the half
tiles already do. Order and names are unchanged apart from where they sit.
@ashhart
ashhart merged commit d24cb5e into ashhart:main Nov 21, 2026
11 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.

ashhart added a commit that referenced this pull request Nov 21, 2026
…r adds

#3921 made kernels.zig put `#define TF_GLM_M3_FAST 1` before the KDA prompt
passes and glm_index_nax.metal on a GPU without tensor units. The KDA and
index contracts still built those texts without it, so on an M3 the loader
check refused them ("GLM contract text is not one the loader compiles") and
the whole GLM contract stopped before its first case. Both texts now follow
the loader, and gate 5's pre-M5 texts carry the define too.
ashhart added a commit that referenced this pull request Nov 21, 2026
…oes; its record declares 18432 bytes

On a GPU without tensor units the case compiles glm_index_nax.metal with TF_GLM_M3_FAST (#3921, #4397), whose
threadgroups cover 32 rows with 4 simdgroups, but encode launched it as on M5: scoreRows' 64 or 128 rows a group and
2 x rows threads. Rows past 32 x the groups' count were never written (256 x 128 x 16384: rows 64 on, byte 1065728),
so the shapes gate compared unwritten floats against main's scores. The case now launches it as selectKeys does
(glm/prompt.zig: 32 rows, 128 threads); rows it wrote matched main's bytes already, and the kernel is unchanged.
Before M5 the planted control moves into the pre-M5 branch (both simdgroups of a row pair on the same 32 blocks),
since the M5 branch it edited is not compiled there. The pre-M5 kernel stages 128 x 72 bf16 (18432 bytes) of keys;
the record declared 17408 (the M5 tile, 64 x 136), so gate 5's limit now declares 18432.
ashhart added a commit that referenced this pull request Nov 21, 2026
…r adds

#3921 made kernels.zig put `#define TF_GLM_M3_FAST 1` before the KDA prompt
passes and glm_index_nax.metal on a GPU without tensor units. The KDA and
index contracts still built those texts without it, so on an M3 the loader
check refused them ("GLM contract text is not one the loader compiles") and
the whole GLM contract stopped before its first case. Both texts now follow
the loader, and gate 5's pre-M5 texts carry the define too.
ashhart added a commit that referenced this pull request Nov 21, 2026
…oes; its record declares 18432 bytes

On a GPU without tensor units the case compiles glm_index_nax.metal with TF_GLM_M3_FAST (#3921, #4397), whose
threadgroups cover 32 rows with 4 simdgroups, but encode launched it as on M5: scoreRows' 64 or 128 rows a group and
2 x rows threads. Rows past 32 x the groups' count were never written (256 x 128 x 16384: rows 64 on, byte 1065728),
so the shapes gate compared unwritten floats against main's scores. The case now launches it as selectKeys does
(glm/prompt.zig: 32 rows, 128 threads); rows it wrote matched main's bytes already, and the kernel is unchanged.
Before M5 the planted control moves into the pre-M5 branch (both simdgroups of a row pair on the same 32 blocks),
since the M5 branch it edited is not compiled there. The pre-M5 kernel stages 128 x 72 bf16 (18432 bytes) of keys;
the record declared 17408 (the M5 tile, 64 x 136), so gate 5's limit now declares 18432.
ashhart added a commit that referenced this pull request Nov 21, 2026
…r adds

#3921 made kernels.zig put `#define TF_GLM_M3_FAST 1` before the KDA prompt
passes and glm_index_nax.metal on a GPU without tensor units. The KDA and
index contracts still built those texts without it, so on an M3 the loader
check refused them ("GLM contract text is not one the loader compiles") and
the whole GLM contract stopped before its first case. Both texts now follow
the loader, and gate 5's pre-M5 texts carry the define too.
ashhart added a commit that referenced this pull request Nov 21, 2026
…oes; its record declares 18432 bytes

On a GPU without tensor units the case compiles glm_index_nax.metal with TF_GLM_M3_FAST (#3921, #4397), whose
threadgroups cover 32 rows with 4 simdgroups, but encode launched it as on M5: scoreRows' 64 or 128 rows a group and
2 x rows threads. Rows past 32 x the groups' count were never written (256 x 128 x 16384: rows 64 on, byte 1065728),
so the shapes gate compared unwritten floats against main's scores. The case now launches it as selectKeys does
(glm/prompt.zig: 32 rows, 128 threads); rows it wrote matched main's bytes already, and the kernel is unchanged.
Before M5 the planted control moves into the pre-M5 branch (both simdgroups of a row pair on the same 32 blocks),
since the M5 branch it edited is not compiled there. The pre-M5 kernel stages 128 x 72 bf16 (18432 bytes) of keys;
the record declared 17408 (the M5 tile, 64 x 136), so gate 5's limit now declares 18432.
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.

4 participants