Skip to content

glm cuda: an uneven KDA stack padded to 16-byte rows keeps its bytes; cold first tokens 10-11% sooner at TP=3 - #3697

Merged
ashhart merged 4 commits into
ashhart:mainfrom
ultradaoto:glm-kda-aligned-rows
Nov 20, 2026
Merged

ashhart merged 4 commits into
ashhart:mainfrom
ultradaoto:glm-kda-aligned-rows

Conversation

@ultradaoto

Copy link
Copy Markdown
Contributor

For #3516, as invited when #3519 was closed: the pad's first-token gain with the unpadded stack's exact bytes. No switch and no new kernel.

What this changes

At worlds where a rank's fused KDA input is not eight columns wide (3, 5, 6 and 7 among the first eight), the stack's rows are not 16-byte aligned. So b16.matmul sends its prompt windows to tf_glm_bmm_rows, the unstaged kernel, and never to the staged tiles. The staged tiles give that kernel's bytes: the same mma chain per K slice, slices added in order (glm-ref checks it for aligned shapes today).

Now the engine always sets the stack's row width (kb.pad_to):

  • Unset (the default): each rank's stack is padded at load to its own width rounded up to eight columns (kb.alignedWidth: at TP=3, 8,726 → 8,728 on rank 0 and 8,341 → 8,344 on ranks 1 and 2; at worlds 1, 2, 4 and 8 it is the stack's width and nothing moves). The padded stack stays row-major bf16 on the B16 kernels. Prompt windows from 128 rows take tf_glm_bmm_tile (the stack) and tf_glm_bmm_tile1 (the f_b and g_b views, whose input rows are now 16-byte aligned). Decode windows keep tf_glm_bmm_decode for the stack; the views take Triton's staged tile where decode_tiles has their shape.
  • TF_GLM_KDA_PAD=1: as on main. The stack is padded to the common width and tiled for glm_mm, and packed under TF_GLM_DENSE_BITS. kb.tile_pad carries the switch, and Layer.tiledInput() is true for an even stack or a padded one with the switch.
  • tile.padKdaInputs moves from tileLinears to the engine's load step, so the pad also runs with TF_GLM_MM=0.

No kernel changes, no new switch, no fingerprint change. The pad is two zero rows a layer on TP=3's rank 0 and three on ranks 1 and 2.

Tests

  • tf-cuda-test glm-ref on a GB10, new check paddedStack: both of TP=3's shapes, the stack [8726, 4096] padded to 8,728 and [8341, 4096] padded to 8,344, and the f_b view [2816, 128] / [2688, 128] read from the projected rows at the uneven and at the padded stride. 144 row counts each: every count from 1 to 130, then prompt passes up to 2,048 rows. Every byte of the first n columns equals the uneven rows' call, and every pad column is zero. glm-ref: 27 PASS, 0 FAIL. glm-kda-pad-split: PASS.
  • cuda_kda_tp3_test.zig, new host test: at worlds 3 and 5 with the width set as Engine.open sets it, which kernel each of the three projections takes at 1, 17, 127, 128 and 2,066 rows, unset and with the switch; at worlds 1, 2, 4 and 8 the width is the stack's own and glm_mm reads it as before. The old test (width unset) is untouched.
  • zig build test-cuda-host on a437511 plus this diff: one failure, cli_config_test's serve --config: expand reads the file ..., which fails the same way on main on this host (6,830 of 6,864 passed and 33 skipped on the branch's first full run). zig fmt --check clean. pr_check --base a437511: 0 errors, 0 new lean findings. git merge-tree is clean against main 9489c38 and against glm cuda: decode kernels as dependent launches, +6.8% decode at TP=2, same tokens #3673's head 7017c23.

Receipts

Three GB10s on a switchless triangle, TP=3, GLM-5.3-Flash EXL3, --parallel 4 --kv-cache fp8 --decode-share 0.5, bf16 (no TF_GLM_DENSE_BITS, no TF_GLM_KDA_PAD), MTP drafts, greedy. Main a437511 and the branch built here the same way (Zig 0.17.0, nvcc 13.0). Starts in one session, in this order: main, (the first version twice, see below), main, branch, branch, (a main start that is not used, see below), main. One number per start, or two where the tool takes two:

main a437511 this branch
cold 1K prompt, first token (s) 0.76, 0.78 / 0.77, 0.78 / 0.78, 0.77 0.71, 0.72 / 0.72, 0.72
cold 7.2K prompt, first token (s) 5.07, 5.07 / 5.05, 5.04 / 5.07, 5.09 4.53, 4.53 / 4.55, 4.56 (-10%)
cold 28.8K prompt, first token (s) 19.32, 19.40 / 19.25, 19.30 / 19.37, 19.44 17.18, 17.19 / 17.16, 17.22 (-11%)
16 varied greedy requests one at a time, aggregate tok/s 40.29 / 40.30 / 40.40 40.56 / 40.40
tools/bench_openai.py --tokens 256 --reps 3 --temperatures 0, code, median tok/s 45.0 / 44.9 / 45.0 44.5 / 45.2
the same, chat with thinking off 35.0 / 35.3 / 34.8 35.0 / 35.6
tools/bench_concurrent.py --tokens 256 --reps 3 --temperatures 0 --levels 1,4 --alone --serial, 4 streams, code 92.0 / 94.9 / 94.3 93.7 / 94.1
the same, chat 73.0 / 74.6 / 73.9 73.9 / 74.3

The cold prompts are a nonce line and Python source, max_tokens 1, thinking off, each length twice a start.

Tokens: 13 greedy replies (8 prompts alone, 4 at once, one prompt of about 8K tokens) hash the same in every start, and so do bench_concurrent's 36 (alone and serial, 1 and 4 streams). One cold 1440x900 image answers the same in every start.

Decode is level. TF_GLM_PROF=1, --mtp-confidence 0.5, the 4-row verify calls, mean ms a call, starts in the order main, (the first version twice), main, branch, branch, main:

main main branch branch main
rank 0 wall 60.97 60.78 60.87 60.85 60.85
rank 0 kda 17.56 17.55 17.47 17.39 17.50
rank 1 wall 60.87 60.70 60.78 60.72 60.77
rank 1 kda 16.43 16.39 16.51 16.31 16.35
rank 2 wall 60.86 60.69 60.77 60.72 60.76
rank 2 kda 16.55 16.45 16.40 16.40 16.46

The switches around it, one start each of main and the branch on the same settings:

  • TF_GLM_DENSE_BITS=8 TF_GLM_HEAD_BITS=8 without the pad switch: both log 188 linears packed, so the stack stays bf16 on the branch as on main. 13 of 13 hashes equal between them. Cold 7.2K 5.02 → 4.54 s, 28.8K 19.18 → 17.16 s; the 16 requests 45.69 → 45.84 tok/s.
  • TF_GLM_MM=0: 13 of 13 hashes equal between them. Cold 7.2K 5.00 → 4.51 s, 28.8K 19.04 → 16.99 s; the 16 requests 39.65 → 39.62 tok/s.
  • TF_GLM_KDA_PAD=1 with the dense bits and a DFlash2 drafter, on the branch: 290 linears packed, and 13 of 13 hashes equal to the 1.2.9 release's on those settings.

Two things about how these were taken:

  • A first version padded every rank to the common width, as TF_GLM_KDA_PAD=1 does. It had the same first tokens, but ranks 1 and 2 then carried a 4.6% wider stack: their kda went 16.4 → 16.9 ms and every rank's wall 60.9 → 61.2 ms. That is why unset pads each rank to its own width.
  • One more main start sat between the branch's two and the last one. A host-test build ran on rank 1's CPU during its cold prompts, so its numbers are not in the table (they read 5.06 and 19.37 s).

Not run: TP=2 and one GPU (the row width there is the stack's own, which the host test walks), and FP4-Local's split stack on a served model (glm-kda-pad-split covers its pad).

One question: I left TF_GLM_KDA_PAD=1 on the common width. If nothing needs one width across the ranks, it could take each rank's own width too and spare ranks 1 and 2 the wider rows. Say so and I will add it here or after.

CI: this fork's runs wait for a maintainer's approval, as on my other PRs.

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

ashhart commented Nov 18, 2026

Copy link
Copy Markdown
Owner

Thanks @ultradaoto. Padding each rank's stack to its own width rounded up to eight columns is a neat way to keep the unpadded bytes and still reach the staged tiles.

  • At worlds 1, 2, 4 and 8 the aligned width is the stack's own, so nothing moves. tiledInput() is unchanged there, since width() equals matWidth(). Your new host test walks this at worlds 3 and 5 and at the even worlds.
  • TF_GLM_KDA_PAD=1 keeps main's common width and its tiling for glm_mm.
  • glm-ref's paddedStack checks every byte of both TP=3 shapes at 144 row counts, and the 13 greedy hashes and bench_concurrent's 36 match across main and the branch.

On your question: please keep TF_GLM_KDA_PAD=1 on the common width in this PR. If you want it to take each rank's own width too, that's better as its own PR, with the same byte check for the tiled path.

The code applies to current main as it is. The only conflict is the CHANGELOG line, which we settle when it lands. Nothing to change. CI on this fork still needs approval to run, and it has to be green at this head before landing.

  • Synthena

@AIFlowML

Copy link
Copy Markdown
Contributor

I ran zig build test-glm-tp3-host test-kda-host test-glm-index-host -Dcpu=apple_m1 -j5 --summary all on 5edf171f with Zig 0.17.0 on M5 Max. All 45 tests passed. The head also trial-merges into 124052c97, including its changelog, and the CI checks that ran on this PR head now report success.

On a scratch merge of that main plus #3697 and #3689, I built compile-cuda-tests with -Dnvcc=/usr/local/cuda/bin/nvcc -Dsm=120 -Dfixed-cpu (nvcc 13.1) and ran tf-cuda-test glm-ref on an RTX 5090 Laptop. It exited zero. The 8,726→8,728 and 8,341→8,344 stacks and their f_b views matched byte for byte across 144 row counts each. This was a single-device fixture for the two TP=3 rank shapes. The multi-rank checkpoint timing remains the author's receipt.

Igor Lessio - AIFLOW LABS

ultradaoto and others added 4 commits November 20, 2026 21:42
…on the staged tiles (for ashhart#3516)

At worlds where a rank's fused KDA input is not eight columns wide (3, 5, 6, 7), its rows are not 16-byte aligned, so
prompt windows ran on tf_glm_bmm_rows and never on the staged tiles, which give the same bytes. The stack is now always
padded at load to its own width rounded up to eight columns and stays row-major bf16: prompt windows take
tf_glm_bmm_tile and tf_glm_bmm_tile1, decode windows keep tf_glm_bmm_decode. TF_GLM_KDA_PAD=1 keeps main's common
width and still tiles the stack for glm_mm. No kernel changes, no new switch.

glm-ref checks both of TP=3's shapes and the f_b view at 144 row counts from 1 to 2,048: every byte as the uneven rows'.
Forward cuLaunchKernelEx to the existing Capture.launch recorder, as the adjacent TP3 test already does. Keep the test assertions and production PDL default unchanged.
@ashhart
ashhart force-pushed the glm-kda-aligned-rows branch from 3ea1454 to f00e5f0 Compare November 20, 2026 21:42
@ashhart
ashhart merged commit 46c6ee2 into ashhart:main Nov 20, 2026
1 check passed
ashhart pushed a commit that referenced this pull request Nov 20, 2026
@ashhart

ashhart commented Nov 20, 2026

Copy link
Copy Markdown
Owner

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