Repository navigation
[Bug][GLM-Flash-5.3] Keep the recurrent state in fp32 between tiles - #13
Conversation
Kernel 2 held the [K, V] state as bf16 C fragments and rounded it after every 16-token tile. On GLM-5.3-Flash prefill (H=16 per rank, T=13824, real activations) that put the final state 0.0118 relative L2 from an fp32 recurrence; an fp32 recurrence with the state rounded to bf16 every 16 tokens reproduces 0.0117. The error concentrates in the slow-decay heads (mean per-step decay above 0.97), which carry the long-range memory, and it compounds across chunked-prefill boundaries. The fragments are now fp32 accumulators. bf16 is produced only where the hardware needs it: a staging fragment before the MOVM transpose into the MMA B operand, and before the STSM copies into the shared-memory tile for the final-state and checkpoint stores. With this the final state is 0.0031 from the fp32 recurrence (FLA's chunk kernel: 0.0020) and the output 0.0047 (was 0.0050). End to end on a 4-way TP GLM-5.3-Flash deployment, a 42k-token tool-call probe went from 5/16 to 12/16 correct with this kernel; the FLA chunk path scores 22/24. Register pressure grows by 32 (V-split) or 64 (full-width) fp32 values per thread. Under __launch_bounds__(192, 2) half the kernel-2 instantiations spill 112 to 136 bytes; kernel time on a GB10 is unchanged at T=16128/4096, H=16/64. Raising minBlocks to 1 leaves 12 instantiations spilling 80 bytes at 255 registers, also with unchanged time. torch_ref mirrors the new arithmetic: fp32 work state, bf16 operand for the two state matmuls, bf16 narrowing once at the store. test_fwd and test_fwd_varlen stay bit-exact. Still bf16 across chunk boundaries: an fp32 initial state is narrowed on load and the final state is written through the bf16 tile, a 0.0016 floor. Loading and storing fp32 states directly is the next step. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Matt Mastracci <[email protected]>
d42ecba to
8b693a2
Compare
H200 (sm_90a) performance: stock vs #13 vs #13 +
|
| H | shape | stock (ms) | #13 (ms) | #13 + fix (ms) | #13 / stock | fix / stock |
|---|---|---|---|---|---|---|
| 16 | 4k | 0.240 | 0.231 | 0.232 | 0.962 | 0.967 |
| 16 | 16k | 0.885 | 0.848 | 0.848 | 0.959 | 0.959 |
| 16 | 64k | 3.449 | 3.306 | 3.318 | 0.959 | 0.962 |
| 16 | 128k | 6.874 | 6.581 | 6.580 | 0.957 | 0.957 |
| 16 | 8x8k | 1.028 | 1.320 | 1.000 | 1.283 | 0.973 |
| 16 | 16x4k | 0.969 | 1.235 | 0.945 | 1.275 | 0.975 |
| 16 | 32x2k | 1.029 | 1.256 | 1.003 | 1.221 | 0.975 |
| 64 | 4k | 0.331 | 0.323 | 0.322 | 0.976 | 0.973 |
| 64 | 16k | 1.234 | 1.198 | 1.198 | 0.971 | 0.971 |
| 64 | 64k | 4.968 | 4.777 | 4.796 | 0.962 | 0.965 |
| 64 | 128k | 10.056 | 9.636 | 9.626 | 0.958 | 0.957 |
| 64 | 8x8k | 4.065 | 4.689 | 3.931 | 1.154 | 0.967 |
| 64 | 16x4k | 4.165 | 4.793 | 4.035 | 1.151 | 0.969 |
| 64 | 32x2k | 4.383 | 4.964 | 4.234 | 1.133 | 0.966 |
Root cause of the #13 batched-varlen regression: fp32 state needs ~210 regs/math thread; __launch_bounds__(192,2) caps K2 at 168 → 152 B spill stack (ncu: 1.28 GB local loads / 1.04 GB stores at 16x4k), K2 546 → 831 µs at 8x8k H=16. With the fix: REG 128 at launch, math warpgroup 216 via setmaxnreg, 2 blocks/SM, K2 582 µs (stock 601, ncu clocks locked).
Accuracy (vs fp64 reference, H=16, real layer-0 inputs): output and final state of #13 + fix are bitwise identical to #13 (100 configs incl. varlen, init/final state, bf16/fp32 state I/O).
| T | stock state / out err | #13 (= #13 + fix) state / out err |
|---|---|---|
| 4k | 0.848% / 0.480% | 0.388% / 0.473% |
| 128k | 1.240% / 0.485% | 0.374% / 0.471% |
End-to-end serving — vLLM main 1417022c3d, RedHatAI/GLM-5.3-Flash-NVFP4, TP2 on H200, default MoE backend (Marlin), vllm bench serve random dataset (±50% input length, 16 output tokens, ignore_eos), total token throughput, mean of 2 runs:
| input × concurrency | stock (tok/s) | #13 | #13 + fix | #13 vs stock | fix vs stock |
|---|---|---|---|---|---|
| 8k × 1 | 10,272 | 10,281 | 10,307 | +0.1% | +0.3% |
| 8k × 8 | 13,156 | 13,004 | 13,199 | −1.2% | +0.3% |
| 8k × 32 | 14,105 | 14,020 | 14,207 | −0.6% | +0.7% |
| 32k × 4 | 13,648 | 13,471 | 13,683 | −1.3% | +0.3% |
| 2k × 64 | 14,341 | 14,253 | 14,332 | −0.6% | −0.1% |
Not tested: sm_100 (B200) and sm_12x (GB10). On sm_12x the fix skips setmaxnreg (ptxas drops it) and launches one block per SM.
…per SM on Hopper Keeping the recurrent state in fp32 needs about 210 registers per math thread on the full-width path. Under __launch_bounds__(192, 2) ptxas caps kernel 2 at 168, so the state spilled (152 B stack) and batched varlen prefill on H200 spent up to 52% more time in kernel 2 than the bf16-state kernel. The full-width kernel now runs 256 threads: one math warpgroup and one warpgroup with the load warp, the store warp and two idle warps. It launches at 128 registers (two blocks per SM). After the prologue the producer warpgroup drops to 40 registers with setmaxnreg, and the MMA branch grows the math warpgroup to 216 before its loop, so ptxas allocates the math loop against 216. The V-split kernel fits in 168 registers and keeps 192 threads without reallocation. The V-split path is now chosen while its grid fits in one wave at the occupancy the V-split kernel reaches (two blocks per SM on H200), instead of only while it fits at one block per SM. launch_fwd takes the SM count and makes that choice. Architectures: sm_90a and sm_100f reallocate. sm_12x lowers the load warp's bulk copies to an extern call, and ptxas ignores setmaxnreg there (C7506). K2 needs about 98 KB of shared memory, so sm_12x fits one block per SM anyway: it launches the 256-thread kernel for one block per SM without reallocation. Outputs are bitwise identical to 8b693a2 (100 configurations). H200, end to end in ms (median of 3 alternating rounds, one process per shape): H shape b59532f 8b693a2 this this/b59532f 16 4k 0.240 0.231 0.232 0.967 16 16k 0.885 0.848 0.848 0.959 16 64k 3.449 3.306 3.318 0.962 16 128k 6.874 6.581 6.580 0.957 16 8x8k 1.028 1.320 1.000 0.973 16 16x4k 0.969 1.235 0.945 0.975 16 32x2k 1.029 1.256 1.003 0.975 64 4k 0.331 0.323 0.322 0.973 64 16k 1.234 1.198 1.198 0.971 64 64k 4.968 4.777 4.796 0.965 64 128k 10.056 9.636 9.626 0.957 64 8x8k 4.065 4.689 3.931 0.967 64 16x4k 4.165 4.793 4.035 0.969 64 32x2k 4.383 4.964 4.234 0.966 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Matt Mastracci <[email protected]>
|
(this comment was generated by Claude after I let it run the benchmarks) Verified 388dc5e on the sm_12x side (GB10 / DGX Spark). Summary: no change on our hardware, as intended. Register reallocation by arch. The same PTX assembled with ptxas 13.0 for each target:
So on sm_12x the commit's fallback is what runs: 256 threads at one block per SM, no reallocation. With that path the GB10 build (sm_120f) has 116 of 128 kernel-2 instantiations at 0 spill bytes and the rest at 132 B, against PR 13 alone where 60 instantiations spilled ~200 B at the 168 cap. The C7506 text mentions an extern call; the PTX has none, and NDEBUG makes no difference, so the message is just how ptxas reports the unsupported instruction there. Kernel level (GB10,
End-to-end serving (GLM-5.3-Flash, NVIDIA ModelOpt NVFP4 checkpoint, 4x GB10, TP4, MoE backend flashinfer_cutlass, vLLM nightly 0961bbae + #58704, DFlash k=7). Each FlashKDA commit built as a drop-in
Pass-to-pass spread on the concurrent rows is up to ±10% on this stack (scheduling, not thermal), so anything under a few percent is noise; the 8k × 1 and 2k × 64 rows are tight (<1% between passes). Two controls, the image's own bundled library mounted over itself and the untouched image relaunched, land on the same numbers (8k × 1: 2,844 and 2,854; 2k × 64: 3,592 and 3,560), so the mount method does not move the result. Absolute numbers are GB10 scale; the point is the three columns match. |
|
This was done with help of AI B200 (sm_100f) performance: stock vs #13 vs #13 + Kernel — fwd (K1 + K2), real GLM-5.3-Flash layer-0 inputs, fp32 state, int32 cu_seqlens, sm_100f build. Ratios are against stock measured in the same sweep.
Over 56 shapes (H 8/16/32/64) the geomean is 1.092 for #13, 1.000 for On sm_10x, ptxas branches around the in-loop state spill in K2 instead of predicating it as on sm_90a, which serializes the state update's MMAs and FMAs. The commit also adds Accuracy — rel. err vs fp64 reference, GLM-5.3-Flash layer-0 weights, H=16:
#13, End-to-end serving — GLM-5.3-Flash-NVFP4, TP2 on B200, vLLM
A repeat of Not tested: sm_100a and sm_103 (B300) builds, and |
K1 and K2 issue TMA bulk stores from shared memory and exit without tma_store_wait<0>(). The copies read shared memory until they complete, and the block's shared memory may be reassigned once it exits, so this is undefined behavior under PTX. Wait before exiting. On B200, 388dc5e was 1-6.5% slower than stock on batched shapes. The SASS shows why: sm_100 ptxas branches around the in-loop final-state spill in K2 (16 BRA.U) instead of predicating it as sm_90a does, which serializes the state update's MMAs and FMAs. On sm_10x only: - spill the state after the update loop, with a 224/32 math/producer register split so that the loop itself does not spill; - in launch_fwd, skip the V-split grid when it needs two blocks per SM, because the full-width kernel is faster there. sm_90a and sm_120 SASS is byte-identical to 388dc5e plus the TMA waits. B200, real GLM-5.3-Flash activations, 56 shapes (H 8-64, 4k-128k, batched varlen): kernel geomean -4.6% vs stock, faster on 54 of 56 shapes (worst +1.6%). End-to-end serving is at parity. Outputs are bitwise identical to 388dc5e, and tests/test_fwd_full.py passed 580/580 on sm_100 in prior validation. compute-sanitizer racecheck still reports two shared-memory hazards in K2. They are false positives and are present in stock too. Co-Authored-By: Claude <[email protected]> Signed-off-by: Matt Mastracci <[email protected]>
|
hey @gau-nernst the latest column always is latest state, this was just for our dev loop. see here for E2E TTFT prefill only: Hopper (H200): Before = vllm serve RedHatAI/GLM-5.3-Flash-NVFP4 --tensor-parallel-size 2 --max-model-len 139264 \
--no-enable-prefix-caching --additional-config '{"kda_prefill_backend":"flashkda"}'
vllm bench serve --dataset-name random --random-input-len $IN --random-output-len 1 \
--random-range-ratio 0 --ignore-eos --num-prompts $N --max-concurrency $CONC --seed $ROUND \
--percentile-metrics ttft --metric-percentiles 50,90,99Each round runs four TP2 servers; round 2 swaps the
TTFT is unchanged within noise. Comparing each GPU pair with itself, after is 0.0–0.2% faster on every pair for 8k × 8, 8k × 32 and 32k × 4, and 0.0–0.7% faster for 128k × 1. 2k × 64 is 0.0–0.4% slower on every pair. The single-request 8k and 32k rows flip sign between pairs, so those are noise; the 32k × 1 P99 comes from one slow request out of 12. Blackwell (B200): Before = vllm serve RedHatAI/GLM-5.3-Flash-NVFP4 --tensor-parallel-size 2 --max-model-len 139264 \
--no-enable-prefix-caching --additional-config '{"kda_prefill_backend":"flashkda"}'
vllm bench serve --dataset-name random --random-input-len $IN --random-output-len 1 \
--random-range-ratio 0 --ignore-eos --num-prompts $N --max-concurrency $CONC --seed $ROUND \
--percentile-metrics ttft --metric-percentiles 50,90,99Each round runs four TP2 servers; round 2 swaps the
TTFT is unchanged within noise. Comparing each GPU pair with itself, after is 0.2–0.6% faster on every pair for 8k × 8, 8k × 32 and 32k × 4, and 0.2–1.0% faster for 128k × 1. The single-request 8k and 32k rows flip sign between pairs, so those are noise. For better clarity microbenchmarks before and after this PR: Hopper (H200):
Geomean after / before: 0.967; after is faster on all 14 shapes. As on B200, the kernel gain barely shows in TTFT because FlashKDA is a small share of prefill time. Blackwell (B200):
Geomean after / before: 0.945; after is faster on all 14 shapes. The kernel gain shrinks to about 0.5% in TTFT because FlashKDA is a small share of prefill time; the MoE and MLA layers dominate. AI Assistance was used to run the benches and prepare the tables in markdown and the writeup around it |
|
@simon-veitner-redhat Thank you for the new results. This should have been clearly displayed in the PR description so others can get better context and understanding of the PR. Crucially, it shows that the PR actually improves the kernel (it was not obvious to me reading through the exchanges of AI agents). It's interesting that the new kernel is faster. Was it because of the register reallocation that relieve register pressure? Again, I would appreciate if the kernel optimization details are clearly explained in the PR. |
|
@gau-nernst note that we did not merge this PR to improve kernel performance (as visible the gains are neglible e2e) but to mitigate some problem for glm 5.3-flash in accuracy that was observed for nvfp4 version both for hopper and blackwell (and @mmastrac seemed to also have observed it on sm120). in practice it lead to some of the infinite loops within reasoning for agentic workloads where many tool calls are made (main reason was vllm-project/vllm#58704 but from here we also measured positive input and less "endless generation") , i.e. something like this can be mitigated by keeping the recurrent state in fp32 between the 16-token tiles in K2. before, it was rounded to bf16 after every tile, so the error grew with context length. against an fp64 reference, the state error drops from 0.85% at 4k and 1.24% at 128k to about 0.37% at both lengths, while the output error stays about the same. the state is still bf16 across prefill chunk boundaries. the main contributions i did was to ensure we don't loose e2e perf. the fp32 state needs about 210 registers per math thread, and the 168-register cap from on hopper (and also on sm_100) i fixed this with on blackwell i additionally write the final state out after the K2 update loop instead of inside it, because on sm_100 the in-loop write serializes the state update. a 224/32 register split keeps the loop from spilling, and the V-split grid is skipped when it needs two blocks per SM. i also added the missing @mmastrac could you update PR description accordingly, i don't have the power to do that. |
Summary
While tracking down corruption in GLM-5.3-Flash tool calling at medium (32k+ token) distances, the FlashKDA kernel was found to be introducing error from bf16 rounding on the edge of every 16-token tile.
To keep this first patch smaller, it is still bf16 across chunk boundaries.
So a 42k prompt is about 2600 tiles inside 4 prefill chunks, which meant we rounded 2600 times before and now down to 4 times (at the boundaries) with this PR.
Validation
Note: this may impact smaller quants like NVFP4 more than bf16. This improves corruption on the nvidia/NVFP4 weights on GB10, but not on the upstream bf16 weights on Hopper.
Prefix test: https://gist.github.com/mmastrac/72b67dd76ff02446ca6ad30e1021e355
Original repro: https://gist.github.com/mmastrac/ff0d09589d5480f8614413daf6b1552b
The prefix test runs a minimum subset of the original repro and uses the next token's logprob to detect corruption.
In this case, the model occasionally decides to generate a portion of one tool call with prompt text sliced into in on failure (e.g.:
pytest -q 2 user asked about stock levels. 2>&1).With the fix, we see a tapped state error drop from 0.0118 to 0.0031, and the number of times that tool call is corrupted drops from 50% of the time to <5% of the time.
Performance: effectively unchanged on GB10. Note that this does increase register pressure per thread, but I was not able to measure a kernel time change.