Skip to content

glm cuda: Flash pieces take full GLM-5.3's shapes (slice (a) of #3063) - #3949

Merged
ashhart merged 8 commits into
ashhart:mainfrom
bjornmonnens:glm-dsa-cuda-a
Nov 21, 2026
Merged

ashhart merged 8 commits into
ashhart:mainfrom
bjornmonnens:glm-dsa-cuda-a

Conversation

@bjornmonnens

@bjornmonnens bjornmonnens commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

GLM-5.3-Flash's CUDA pieces take full GLM-5.3's shapes as parameters, slice (a) of #3063: pages, state and snapshot plans take the latent row width (576) and cache count (79), the DSA indexer takes its pool size (1), whole-history key store and 64-dim index rope, and the latent path takes 576-wide roped rows on the mla image with one new kernel, tf_mla_absorb_q_192. Every GLM-5.3-Flash call passes today's values, so Flash computes the same bytes and its replies are unchanged.

Notes for review, against the 2026-11-17 comment:

  • Head padding. Kept: a TP=3 rank's 21 or 22 heads are padded to 32 rows. chunks_wide<32, false, 64> does not fit, as wide_smem<32, 64>() is 113,408 bytes, past the 99 KiB a block may take on sm_86 to sm_121, so roped rows run as two head blocks of 16 (tf_mla_chunks_r16, the second guarded past the rank's heads) in prompt chunks as in decode. No new head-block shape.
  • Callers. The parameterised kernels keep their Flash callers, which now pass Flash's values. The full-GLM-5.3 values (pool 1, rope 64, 576-wide rows, tf_mla_absorb_q_192) are reached by the GLM family's code when Config.rope / kpool say so, by host tests at both shapes and by the kernel contract; the first checkpoint that sets them loads in (b). If you would rather have tf_mla_absorb_q_192 land with (b), I can move that commit's kernel there.
  • No precision or token changes. No FP4/FP8 cache, no load-time weight conversion, no partitioned attention.

Checks on this head

  • Head commit: 02e234998, on main 579450c2c. The DSA indexer commit also passes its geometry through main's decode scores (TF_GLM_DSCORES: scoresTiles, the decode call in selectTokens, the contract's decode shapes and glm-index-check), at GLM-5.3-Flash's values there.
  • Lean checker, python3 tools/zig/lean_check.py: main 579450c2c 165, this head 165, 0 new. tools/pr_check.py --base 579450c2c --pr 3949: 0 errors, 0 warnings.
  • Tests run:
    • zig build test-cuda-host compile-cuda-tests test-glm-index-emulate test-glm-exl3-prompt-host test-exl3-prompt-host test-glm-index-host test-glm-ring-host -Dnvcc=/usr/local/cuda/bin/nvcc: 657/657 steps, 8183/8226 tests passed, 43 skipped, 0 failed (Zig 0.17.0, GB10). zig build test: passes.
    • zig build -Dgpu=spark -Dnvcc=/usr/local/cuda/bin/nvcc -Doptimize=fast: builds.
    • Kernel contract on GB10 (sm_121): see below.
    • The GPU rows (contract, conformance, receipt) were measured on these 3 commits over main 0900b484e with a 4th commit of ours that read the mixed widths in place of glm cuda: EXL3 mcg experts at any whole-bit width, mixed by projection #2094; GLM-5.3-Flash's 4-bit experts take the same bytes and kernels under either (glm cuda: EXL3 mcg experts at any whole-bit width, mixed by projection #2094 keeps the 4-bit path upstream's), and the kernels this changes are the same code.
    • tools/conformance.py on GLM-5.3-Flash at TP=2 (profile: qwen35-cuda.json's prompts and gates with Flash's serve flags and levels 1 and 2; the cache-off server, --prompt-cache-gib 0, and the cached one, --prompt-cache-gib 2, run one after the other on the same two Sparks): 7 passed, 0 failed, 0 skipped. drafted == plain 10/10 (392 of 749 drafts accepted), repeat 10/10, concurrent == solo 28/28 (drafted and plain, 16 with a long prompt among decoders), resumed == fresh 6/6 (resumed turns at 4,096 cached tokens), refusals 4/4, cancel 4/4, evict 3/3.
  • CI: pending; runs on this pull request.
  • Not run, and why: no sm_80/86/89/120 GPU available (records from nvcc 13.0 for those); --drafter (DFlash2) not run, MTP drafts only.

Receipt

  • Main against branch: GLM-5.3-Flash EXL3 4bpw at TP=2 on two DGX Spark (GB10), greedy, main 0900b484e against this head on the same machines and checkpoint.

  • Boxes it reaches: CUDA GLM-5.3-Flash (unchanged values); full GLM-5.3's paths (no caller yet, see above).

  • New switch? None.

  • Environment: TensorFold c548cc601 (these 3 commits and our own mixed-width loader over main 0900b484e, the tree measured on the GPU); Zig 0.17.0; CUDA 13.0 (nvcc 13.0); 2× DGX Spark, NVIDIA GB10 (sm_121), Linux aarch64; Mia-AiLab/GLM-5.3-Flash-EXL3-4bpw-TensorFold revision 078455ffe6472f9a52fbc1139f58b9db2881b25c.

  • Paths and platforms the change reaches: CUDA only. Ran on sm_121; sm_80/86/89/120 compile with nvcc 13.0 records but were not run.

  • Exactness: 8 of 8 greedy replies identical, main against branch (4 solo, 2 long prompts of about 9k and 22k tokens, 2 paired), and the paired replies equal their solo ones on both; the cold 16,384-token prompt's token_sha is the same (5e6751dc5ed6) alone and at 2 streams. Drafted, resumed and concurrent against plain, fresh and solo: the conformance gates above.

  • Main against this branch, same machine and checkpoint: below.

  • Decode speed before and after, tools/bench_openai.py --tokens 256 --reps 5 --temperatures 0 (median tok/s):

    prompt main branch
    fibonacci-raw 53.9 59.8
    gpu-chat-no-think 26.9 25.8

    Within run-to-run spread: an earlier round of the same commits on e7ff97e13 gave 60.8 against 63.3 and 25.9 against 27.9.

  • Prompt speed before and after, tools/bench_cache.py --require-cold, 16,384-token prompt, cold first request ttft_s: main 13.92 s, branch 14.60 s (earlier round: 15.14 s against 14.21 s).

  • Licence of any new weights or drafter: n/a.

Kernel contract, only when a GPU kernel is new or changed
  • Kernels registered with the contract, and the kernel each one replaces:
    • tf_mla_absorb_q_192 (new; tf_mla::absorb_q<192>, 21 and 22 heads) against float64.
    • Full GLM-5.3's values on existing kernels (zig/tests/cuda/contract_glm_full.zig): tf_glm_scores at pools of 1 with 64 roped query dims against float64; tf_glm_index_scores replaces (bit-equal to) tf_glm_scores there; tf_glm_dsa_tokens at pools of 1; tf_glm_index_write (LayerNorm + 64-dim key rope) against float64.
    • The six changed launches keep their Flash cases (tf_glm_pool_keys, tf_glm_scores, tf_glm_index_scores, tf_glm_select_rows, tf_glm_dsel_merge, tf_glm_dsa_tokens), with nvcc 13.0 records.
  • Planted controls, each failing as planted: tf_mla_absorb_q_192 reading the next head's weights; scores with queries left unroped; scores reading keys as pools of 4; token lists one position late; index write leaving keys unroped.
  • CUDA SUMMARY on GB10 (sm_121): kernel-contract glm GLM 48/48 pass, 23/23 controls red, 4 findings (the existing relu NaN rule, also in the two new scores cases); kernel-contract mla 8/8 pass, 9/9 controls red.
  • Chips I could not run: sm_80, sm_86, sm_89, sm_120.

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. (Measured at --parallel 2 without a drafter.)

bjornmonnens added a commit to bjornmonnens/TensorFold that referenced this pull request Nov 19, 2026
bjornmonnens added a commit to bjornmonnens/TensorFold that referenced this pull request Nov 19, 2026
@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thanks @bjornmonnens for this.

One thing before we review it: three of the commits (0fa780ecd3 and two after it) carry a work email as their committer address. Everything we land keeps those lines in public history, so we only take commits whose author and committer are GitHub noreply addresses. The authors are all fine, so only the committer line needs to change:

  1. Set your noreply address with git config user.email (GitHub lists it under Settings, Emails).
  2. Run git rebase --force-rebase $(git merge-base HEAD origin/main), with origin pointing at this repository. This keeps every commit's author, including the ones by MiaAI-Lab and by us, and rewrites only the committer.
  3. Force-push.

Once the new head is up we will review the change itself.

  • Synthena

bjornmonnens added a commit to bjornmonnens/TensorFold that referenced this pull request Nov 20, 2026
@bjornmonnens

Copy link
Copy Markdown
Contributor Author

Thanks. The committer lines are fixed: the new head 19c043e has noreply author and committer addresses on every commit. Only this PR's own 4 commits were rewritten; #2094's commits keep their hashes so the stack stays on 9e24139. Main's dependent-launch change (7948cc4, c4f29fb) now conflicts with this PR's DSA indexer commit. I'll rebase onto it once #2094 moves onto current main, or right away on top of #2094 if you'd rather review it that way.

@AIFlowML

AIFlowML commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

I reran 19c043ef2 with Zig 0.17.0. On M5 Max, zig build test-glm-exl3-prompt-host test-exl3-prompt-host test-glm-index-host test-glm-ring-host -j5 --summary all passed 28/28 tests. On Linux x86_64, zig build test-glm-index-emulate -Dfixed-cpu -j4 --summary all passed and zig build compile-cuda-tests -Dfixed-cpu -j6 --summary all compiled the Zig runner with empty GPU images.

One detail to add to the repro is the emulator's platform requirement at this head. The stock emulator command on macOS fails at the two new sincosf call sites because that symbol is undeclared. The other four host targets above run on macOS. Linux is a reproducible route for the emulator as written. The parent’s emulator passed on Mac, and an Apple-only shim in the emulator source using std::sin and std::cos also passed the full emulator target in my scratch checkout.

I also tried merging the stack into 1ece6e6db. The same four paths already conflict at #2094's 9e24139ae head. #3949 has nine additional conflicting paths across the CUDA glue, indexer kernels and launch records. After the parent moves, rerunning the emulator alongside the host checks would give a quick read on the indexer rebase.

Igor Lessio - AIFLOW LABS

bjornmonnens added a commit to bjornmonnens/TensorFold that referenced this pull request Nov 20, 2026
@bjornmonnens

Copy link
Copy Markdown
Contributor Author

Thanks for the cross-platform runs. The emulator fix is in 2be617d: on macOS the emulator's cuda_runtime.h stand-in now defines sincosf through std::sin and std::cos (behind __APPLE__, so Linux and the CUDA builds are unchanged). Model and kernel both turn through the same function, so the comparison stays exact.

Checked on Zig 0.17.0:

  • Without the change, zig c++ -target aarch64-macos on emulate.cpp gives the same two errors (emulate.cpp:138 and glm_index_rope.cuh:13, undeclared sincosf); with it, aarch64-macos and x86_64-macos both compile.
  • On Linux (GB10, aarch64), zig build test-glm-index-emulate test-glm-exl3-prompt-host test-exl3-prompt-host test-glm-index-host test-glm-ring-host -j8 --summary all: 16/16 steps, 23/23 tests and the emulator pass.

When #2094 moves onto current main I'll rebase the indexer commit onto the dependent launches and rerun the emulator next to the host checks, as you suggest.

ashhart pushed a commit to bjornmonnens/TensorFold that referenced this pull request Nov 21, 2026
ashhart pushed a commit to bjornmonnens/TensorFold that referenced this pull request Nov 21, 2026
ashhart added a commit to bjornmonnens/TensorFold that referenced this pull request Nov 21, 2026
bjornmonnens added a commit to bjornmonnens/TensorFold that referenced this pull request Nov 21, 2026
bjornmonnens added a commit to bjornmonnens/TensorFold that referenced this pull request Nov 21, 2026
bjornmonnens pushed a commit to bjornmonnens/TensorFold that referenced this pull request Nov 21, 2026
bjornmonnens added a commit to bjornmonnens/TensorFold that referenced this pull request Nov 21, 2026
bjornmonnens added a commit to bjornmonnens/TensorFold that referenced this pull request Nov 21, 2026
bjornmonnens pushed a commit to bjornmonnens/TensorFold that referenced this pull request Nov 21, 2026
bjornmonnens added a commit to bjornmonnens/TensorFold that referenced this pull request Nov 21, 2026
bjornmonnens added a commit to bjornmonnens/TensorFold that referenced this pull request Nov 21, 2026
bjornmonnens pushed a commit to bjornmonnens/TensorFold that referenced this pull request Nov 21, 2026
@bjornmonnens

Copy link
Copy Markdown
Contributor Author

Thanks for rebasing this onto current main overnight. When #2094 moved to 3745f1469, the stack conflicted again, so I've rebased it onto that, keeping your two commits (4b220a28f, 91320a4b6). Head 91320a4b6 is mergeable against current main again.

  • Main's decode scores (TF_GLM_DSCORES, c1bfb28, 16ead26, 76e0c0b). These met the DSA indexer commit. scores now goes through main's scoresTiles, and both take the indexer geometry (pool, rope, inv). selectTokens' decode call passes it too. The contract's Scores keeps main's decode/short fields next to the pool-1 fields, and short counts as a planted control. glm-index-check's new decode section passes GLM-5.3-Flash's geometry to scoreColumns.
  • CHANGELOG. The glm cuda: Flash pieces take full GLM-5.3's shapes (slice (a) of #3063) #3949 line had dropped out of the changelog commit in the overnight rebase. It's back, now at the end of Unreleased so main's new entries at the top don't keep conflicting with it.
  • Re-pins. Every pinned kernel source in gradient-serving.tsv matches its sha256 at this head.

Checked on GB10 with Zig 0.17.0:

  • zig build test-cuda-host compile-cuda-tests test-glm-index-emulate test-glm-exl3-prompt-host test-exl3-prompt-host test-glm-index-host test-glm-ring-host -Dnvcc=/usr/local/cuda/bin/nvcc: 632/632 steps, 7934/7977 tests passed, 43 skipped, 0 failed.
  • zig build test passes, and zig build -Dgpu=spark -Doptimize=fast builds.
  • tools/pr_check.py --base 3745f1469 --pr 3949: 0 errors, 0 warnings. Lean is 167 vs 167.

bjornmonnens added a commit to bjornmonnens/TensorFold that referenced this pull request Nov 21, 2026
bjornmonnens added a commit to bjornmonnens/TensorFold that referenced this pull request Nov 21, 2026
bjornmonnens pushed a commit to bjornmonnens/TensorFold that referenced this pull request Nov 21, 2026
@bjornmonnens

Copy link
Copy Markdown
Contributor Author

Thanks for landing #2094. I've rebased this onto main 676cf8b1b with no conflicts, and the range-diff shows every patch identical to the previous head (your two commits included). The head is now 18c78a32b and it's mergeable against current main.

Checked on GB10 with Zig 0.17.0:

  • zig build test-cuda-host compile-cuda-tests test-glm-index-emulate test-glm-exl3-prompt-host test-exl3-prompt-host test-glm-index-host test-glm-ring-host -Dnvcc=/usr/local/cuda/bin/nvcc: 633/633 steps, 7925/7968 tests passed, 43 skipped, 0 failed.
  • zig build test passes, and zig build -Dgpu=spark -Doptimize=fast builds.
  • tools/pr_check.py --base 676cf8b1b --pr 3949: 0 errors, 0 warnings. Lean is 167 vs 167.
  • Every pinned kernel source in gradient-serving.tsv matches its sha256.

bjornmonnens added a commit to bjornmonnens/TensorFold that referenced this pull request Nov 21, 2026
bjornmonnens added a commit to bjornmonnens/TensorFold that referenced this pull request Nov 21, 2026
bjornmonnens pushed a commit to bjornmonnens/TensorFold that referenced this pull request Nov 21, 2026
@bjornmonnens

Copy link
Copy Markdown
Contributor Author

Rebased onto main 579450c2c (it conflicted in cuda_forward.zig, where main's cuda_pdl.bind(ctx) now sits next to the roped parameter; kept both). The range-diff is otherwise identical, including your two commits, and the CHANGELOG line moved back under Unreleased now that 1.3.3 is out. Head 02e234998: the CUDA host set 657/657 steps (8183 passed, 43 skipped, 0 failed), zig build test and the Spark build pass, lean 165 vs 165, pr_check 0 errors and 0 warnings. Description updated.

bjornmonnens and others added 8 commits November 21, 2026 21:24
…s, state and snapshot plans (ashhart#3063)

Full GLM-5.3 keeps 79 latent caches (78 DSA layers and the MTP layer) of
576-value rows (kv_lora 512 and qk_rope 64), a pooled key a row and no KDA
layers. config.zig reads qk_rope_head_dim into Config.rope (still refused
unless 0 for GLM-5.3-Flash) and latentRow() is kv_lora + rope. cuda_state's
Shape carries the cache count, the row width, the index key dims and the
pool size (Config.kpool) that pages, a State's arena and onPages lay out
from, and the snapshot plan takes its latent and pooled spans from the same
shape; its parts hold up to 79 caches instead of max_layers' 47. The KV host
gather's row bytes follow latentRow(). GLM-5.3-Flash's 12 caches of 512
values and pools of 4 give the same 13,056 bytes a row and the same plans.
…arameters (ashhart#3063)

Full GLM-5.3 scores every index key (no pooling, top 2,048 over the whole
history) and ropes the first 64 dims of its index keys and queries in
interleaved pairs. cuda_sparse.Geometry carries the pool size, the pools a
row keeps, the roped dims and their fp32 frequencies; dsa_block.Index holds
one, set from Config.kpool and index_topk (GLM-5.3-Flash: 4, 512, none).

The kernels take the pool size where they divided by 4: tf_glm_scores,
tf_glm_index_scores (a page's pools become 512 / pool), tf_glm_select_rows
and its wide form, tf_glm_index_pools, tf_glm_dsel_scan and _merge, and
tf_glm_dsa_tokens (pool K tokens and a tail of pool - 1). tf_glm_pool_keys
keeps its gated softmax for pools of 4; pools of 1 store each LayerNormed
key as written and read neither gates nor ape, so scoring pool 1 is scoring
every key directly. tf_glm_index_write ropes its LayerNormed key below
`rope` before the bf16 store, and both score kernels rope the query
fragments they load (glm_index_rope.cuh; the angle's sincosf out of line).
A State's index key rows come from Shape.keys (index_ring, or maxInt for a
whole sequence) and its rebase keeps whole pools of State.pool. Every
existing call passes GLM-5.3-Flash's values, so its launches compute the
same bytes.

The emulator (test-glm-index-emulate) adds pool-1 scores, paged at 512 keys
a page, with queries roped on 64 dims against a model that ropes them
itself, and pool-1 top-2,048 picks through tf_glm_index_pools and the
decode select. The GPU contract and check launchers pass the new arguments
at GLM-5.3-Flash's values, and nvcc 13.0's records follow the six changed
launches (tf_glm_pool_keys, tf_glm_scores, tf_glm_index_scores,
tf_glm_select_rows, tf_glm_dsel_merge, tf_glm_dsa_tokens).

The contract also runs full GLM-5.3's values on these kernels
(contract_glm_full.zig): tf_glm_scores at pools of 1 with 64 roped query
dims against float64, allowing the roped queries' bf16 rounding;
tf_glm_index_scores bit-equal to it; tf_glm_dsa_tokens at pools of 1; and
tf_glm_index_write's LayerNormed keys roped on 64 dims against float64. Four
planted controls go red: queries or keys left unroped, keys read as pools of
4, and token lists one position late. A score's -inf (a pool the row cannot
see) is checked exactly.
… the mla image (ashhart#3063)

Full GLM-5.3 keeps 64 roped key columns past its 512-value latent and splits
each 256-wide q head into 192 latent-path dims and 64 roped ones. The DSA
block's Layer carries rope (Config.rope, 0 on GLM-5.3-Flash) and its fp32
frequencies; row() is lw + rope and qHead() qk + rope, which size the kv_a
and q_b widths, the latent and absorbed q rows, their scratch and the token
lists' page rows, and the softmax scale is 1 / sqrt(qk + rope) (1 / 16 on both).

A roped row takes cuda.mla's image (loaded only when Config.rope is set): its
GPT-J rope turns k_pe at pos + r into the latent row before the cache write
and each head's q_pe into qa past the absorbed latent; the chunk tiles are
tf_mla_chunks_r16 (chunks_wide<16, false, 64>), now launched on pages and over
token lists through Mla.chunksOn as GLM's own chunks are. Roped rows keep 16
heads a block in prompt chunks too: wide_smem<32, 64>() is 113,408 bytes, past
the 99 KiB a block may take on sm_86 to sm_121. The merge and expand_v stay
GLM's (the values are the latent's). The one new kernel is tf_mla_absorb_q_192
(tf_mla::absorb_q<192>, q rows 256 apart, qa rows 576), under the contract with
a planted control and nvcc 13.0 records (72 registers on every SM).

A page is padded to whole 576-wide rows and whole token bytes (their lcm), so
a token list's page rows stay whole: full GLM-5.3's 79 caches take 4,096 bytes
more a page (49,440 rows). GLM-5.3-Flash's page, buffers, kernels and launch
arguments are unchanged, pinned by host tests at both shapes.
Main 7948cc4 routes GLM launches through cuLaunchKernelEx.
Forward that entry through main's pdl.forwardEx in the new recording fixtures and keep the merged code in zig fmt style.
@ashhart
ashhart merged commit 11b8997 into ashhart:main Nov 21, 2026
ashhart pushed a commit that referenced this pull request Nov 21, 2026
@ashhart

ashhart commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Thank you @bjornmonnens. 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.

3 participants