Skip to content

[Bug][GLM-Flash-5.3] Keep the recurrent state in fp32 between tiles - #13

Merged
robertgshaw2-redhat merged 3 commits into
vllm-project:devfrom
mmastrac:fp32-resident-state
Sep 26, 2026
Merged

robertgshaw2-redhat merged 3 commits into
vllm-project:devfrom
mmastrac:fp32-resident-state

Conversation

@mmastrac

@mmastrac mmastrac commented Sep 25, 2026 •

Copy link
Copy Markdown

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.

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]>

@LucasWilkinson LucasWilkinson left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks for the find!

@simon-veitner-redhat

Copy link
Copy Markdown

H200 (sm_90a) performance: stock vs #13 vs #13 + 388dc5e

Kernel — torch.ops._flashkda_C.fwd, real GLM-5.3-Flash layer-0 inputs, D=128, fp32 random initial state.
Median of 30 iters after 10 warmup (CUDA events), one process per variant, 3 alternating rounds (median shown). SM clock 1950–1980 MHz.

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]>
@mmastrac

mmastrac commented Sep 26, 2026 •

Copy link
Copy Markdown
Author

(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:

target setmaxnreg kernel 2
sm_90a, sm_100a, sm_103a honored 0 to 32 B spill
sm_120a, sm_120f, sm_121a ignored (C7506) stays at the launch cap

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, tests/test_fwd.py from this branch, one wheel per commit, same container):

wheel tests 16k H=16 64k H=16 8x8k H=16 8x8k H=64
stock b59532f fails, expected (bf16 state) 3.78 ms 15.39 ms 14.78 ms 59.47 ms
#13 2/2 3.70 ms 15.47 ms 14.81 ms 59.50 ms
#13 + 388dc5e 2/2 3.71 ms 14.78 ms 14.61 ms 59.42 ms

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 vllm/_flashkda_C.abi3.so and mounted over the image's file, every arm a fresh relaunch. vllm bench serve, random dataset, output length 1 so the rows are prefill-bound like the H200 table (with random prompts the draft acceptance is ~2%, so longer outputs would only add unassisted decode). Throughput is total tokens/s, mean of 2 passes:

input × concurrency stock tok/s #13 tok/s #13 + 388dc5e tok/s #13 vs stock 388dc5e vs stock
8k × 1 2,822 2,859 2,838 +1.3% +0.6%
8k × 8 3,235 3,198 3,222 −1.1% −0.4%
8k × 32 3,296 3,258 3,276 −1.2% −0.6%
32k × 4 3,086 3,058 3,064 −0.9% −0.7%
2k × 64 3,580 3,538 3,528 −1.2% −1.5%

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.

@simon-veitner-redhat

Copy link
Copy Markdown

This was done with help of AI

B200 (sm_100f) performance: stock vs #13 vs #13 + 388dc5e vs + 62650c1. 62650c1 is a single commit on top of 388dc5e (+40/−5, 3 files) with a B200-only perf fix and the missing TMA store wait, ready to cherry-pick.

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.

H shape stock (ms) #13/stock 388dc5e/stock 62650c1/stock
16 4k 0.227 0.963 0.963 0.918
16 16k 0.843 0.959 0.960 0.906
16 64k 3.217 0.962 0.962 0.911
16 128k 6.292 0.962 0.961 0.912
16 8x8k 0.863 1.370 1.013 0.985
16 16x4k 0.818 1.125 1.032 0.976
16 32x2k 0.868 1.120 1.024 0.975
64 4k 0.290 0.979 0.979 0.929
64 16k 1.069 0.973 0.973 0.942
64 64k 4.132 0.973 0.973 0.945
64 128k 8.216 0.973 0.973 0.945
64 8x8k 3.193 1.120 1.026 0.983
64 16x4k 3.170 1.139 1.019 0.980
64 32x2k 3.344 1.113 1.003 1.015

Over 56 shapes (H 8/16/32/64) the geomean is 1.092 for #13, 1.000 for 388dc5e and 0.954 for 62650c1, which is faster than stock on 54 of them.

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. 62650c1 spills after the update loop instead, with a 224/32 register split so the loop itself doesn't spill, and on sm_10x uses the full-width K2 instead of a V-split grid that needs two blocks per SM. Both are guarded to sm_10x. sm_90a and sm_120 SASS is byte-identical to 388dc5e + the TMA wait.

The commit also adds tma_store_wait<0>() after the three TMA stores that were followed by block exit (K1 Mqk, both K2 final-state stores). Outputs are unchanged and the cost is not measurable. The suspected race is a compute-sanitizer racecheck false positive: a minimal kernel with the same named-barrier pattern, which cannot race, gets the same report.

Accuracy — rel. err vs fp64 reference, GLM-5.3-Flash layer-0 weights, H=16:

state 4k state 128k output
stock 0.85% 1.24% 0.48%
#13 and later 0.39% 0.37% 0.47%

#13, 388dc5e and 62650c1 are bitwise identical over 100 configurations, and test_fwd_full passes 580/580. On the corruption probe at 42k, NVFP4 TP2 scores 67/96 for stock, 59/64 for #13 and 59/64 for 62650c1; FP8 TP4 scores 42/64, 24/32 and 49/64.

End-to-end serving — GLM-5.3-Flash-NVFP4, TP2 on B200, vLLM 1417022c3d, vllm bench serve random, output 16, median of 3 reps, total tok/s:

input × concurrency stock #13 62650c1 #13 vs stock 62650c1 vs stock
2k × 64 41,709 41,512 41,972 −0.5% +0.6%
8k × 1 25,331 24,925 25,107 −1.6% −0.9%
8k × 8 37,687 36,737 37,545 −2.5% −0.4%
8k × 32 42,796 41,802 42,883 −2.3% +0.2%
32k × 4 49,918 48,894 50,360 −2.1% +0.9%

A repeat of 62650c1 and a run on a second GPU pair stay within ±1.3% of stock, inside the 0.3–3.3% stock-to-stock noise. 8k × 1 is about 1% slower in all of them although the kernel is faster, which is unexplained.

Not tested: sm_100a and sm_103 (B300) builds, and tests/test_fwd.py. The sm_100f build has 9 new small ptxas spill warnings (4–56 B) on the 192-thread V-split K2. Avoiding them costs about 2 points of the kernel geomean, so they stay.

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]>
@robertgshaw2-redhat
robertgshaw2-redhat merged commit 17a037d into vllm-project:dev Sep 26, 2026
1 check passed
@gau-nernst

Copy link
Copy Markdown
Collaborator

Hi, I just saw this PR. I don't quite the data given by the agents. Can you please clarify the microbenchmark results? What is stock vs #13 vs #13+fix? There should only be Before and After comparison right?

For e2e testing, it should be prefill-only benchmarks and report TTFT

@simon-veitner-redhat

simon-veitner-redhat commented Sep 28, 2026 •

Copy link
Copy Markdown

hey @gau-nernst the latest column always is latest state, this was just for our dev loop.
i had to microtune a little bit because kernel from matt got less performant on hopper and blackwell both and put the perf numbers here so he can cherry pick without anxiety :D i collected results for h200 and b200 below in more readable form just comparing state before and after this PR.

see here for E2E TTFT prefill only:

Hopper (H200):

Before = b59532f , after = 17a037d, both built for sm_90a. vLLM 1417022c3d. Same commands as B200 below; NVFP4 MoE runs through Marlin on sm_90, so absolute TTFTs are higher than on B200.

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,99

Each round runs four TP2 servers; round 2 swaps the .so on every GPU pair, so each row averages 4 runs per version. TTFT in ms:

input × conc (prompts) mean before mean after Δ median before median after Δ P99 before P99 after Δ
2k × 64 (512) 7,865.2 7,875.9 +0.1% 8,342.2 8,356.9 +0.2% 8,352.6 8,371.8 +0.2%
8k × 1 (24) 592.2 592.2 0.0% 592.0 592.5 +0.1% 598.1 598.1 0.0%
8k × 8 (128) 4,401.1 4,398.8 −0.1% 4,523.4 4,520.6 −0.1% 4,533.6 4,533.6 0.0%
8k × 32 (256) 17,004.0 16,999.2 0.0% 18,094.6 18,093.4 0.0% 18,110.5 18,107.5 0.0%
32k × 1 (12) 2,393.9 2,391.2 −0.1% 2,393.6 2,382.2 −0.5% 2,419.7 2,486.3 +2.8%
32k × 4 (48) 8,886.6 8,878.5 −0.1% 9,167.3 9,158.4 −0.1% 9,213.9 9,206.3 −0.1%
128k × 1 (6) 9,812.1 9,777.1 −0.4% 9,769.4 9,742.7 −0.3% 10,031.2 9,966.9 −0.6%

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 = b59532f , after = 17a037d, both built for sm_100f. vLLM 1417022c3d.

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,99

Each round runs four TP2 servers; round 2 swaps the .so on every GPU pair, so each row averages 4 runs per version. TTFT in ms:

input × conc (prompts) mean before mean after Δ median before median after Δ P99 before P99 after Δ
2k × 64 (512) 2,261.7 2,260.0 −0.1% 2,377.3 2,374.6 −0.1% 2,518.5 2,528.1 +0.4%
8k × 1 (24) 215.9 214.9 −0.5% 215.5 214.6 −0.4% 221.5 220.7 −0.4%
8k × 8 (128) 1,395.1 1,392.1 −0.2% 1,426.8 1,423.8 −0.2% 1,436.0 1,431.9 −0.3%
8k × 32 (256) 5,402.7 5,386.9 −0.3% 5,730.0 5,715.1 −0.3% 5,758.0 5,742.0 −0.3%
32k × 1 (12) 830.6 833.9 +0.4% 829.4 834.4 +0.6% 843.8 842.7 −0.1%
32k × 4 (48) 2,937.9 2,923.1 −0.5% 3,026.9 3,011.9 −0.5% 3,069.9 3,056.5 −0.4%
128k × 1 (6) 3,469.7 3,445.2 −0.7% 3,472.0 3,445.0 −0.8% 3,484.1 3,471.3 −0.4%

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):

torch.ops._flashkda_C.fwd (K1 + K2), real GLM-5.3-Flash layer-0 inputs, D=128, fp32 random initial state, int32 cu_seqlens. Same before/after builds as above. Median of 30 iterations after 10 warmup (CUDA events), one process per version and shape, median of 3 rounds with alternating order. SM clock 1980 MHz.

H shape before (ms) after (ms) after / before
16 4k 0.241 0.232 0.962
16 16k 0.886 0.851 0.961
16 64k 3.451 3.318 0.961
16 128k 6.870 6.587 0.959
16 8x8k 1.031 1.016 0.986
16 16x4k 0.979 0.955 0.975
16 32x2k 1.038 1.012 0.975
64 4k 0.332 0.323 0.972
64 16k 1.232 1.200 0.974
64 64k 5.138 4.942 0.962
64 128k 10.302 9.864 0.958
64 8x8k 4.171 4.033 0.967
64 16x4k 4.313 4.171 0.967
64 32x2k 4.511 4.356 0.966

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):

torch.ops._flashkda_C.fwd (K1 + K2), real GLM-5.3-Flash layer-0 inputs, D=128, fp32 random initial state, int32 cu_seqlens. Same before/after builds as above. Median of 30 iterations after 10 warmup (CUDA events), one process per version and shape, median of 3 rounds with alternating order. SM clock 1965 MHz.

H shape before (ms) after (ms) after / before
16 4k 0.228 0.208 0.915
16 16k 0.843 0.763 0.905
16 64k 3.215 2.928 0.911
16 128k 6.292 5.745 0.913
16 8x8k 0.864 0.850 0.985
16 16x4k 0.818 0.800 0.977
16 32x2k 0.869 0.847 0.975
64 4k 0.290 0.270 0.930
64 16k 1.071 1.008 0.941
64 64k 4.138 3.909 0.945
64 128k 8.226 7.776 0.945
64 8x8k 3.267 3.069 0.939
64 16x4k 3.235 3.163 0.978
64 32x2k 3.389 3.287 0.970

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

@gau-nernst

Copy link
Copy Markdown
Collaborator

@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.

@simon-veitner-redhat

simon-veitner-redhat commented Sep 28, 2026 •

Copy link
Copy Markdown

@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

Could perhaps inspect if `get_json_schema_from_tools` handles `strict` field. It doesn't. But structural tags do. Could mention.

Need maybe maybe inspect `vllm/tool_parsers/structural_tag_registry.py` to see if `strict` affects schema enforcement. We have enough.

Could perhaps inspect if `get_json_schema_from_tools` handles `strict` field. It doesn't. But structural tags do. Could mention.

Need maybe maybe inspect `vllm/tool_parsers/structural_tag_registry.py` to see if `strict` affects schema enforcement. We have enough.

Could perhaps inspect if `get_json_schema_from_tools` handles `strict` field. It doesn't. But structural tags do. Could mention.

Need maybe maybe inspect `vllm/tool_parsers/structural_tag_registry.py` to see if `strict` affects schema enforcement. We have enough.

Could perhaps inspect if `get_json_schema_from_tools` handles `strict` field. It doesn't. But structural tags do. Could mention.

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 __launch_bounds__(192, 2) made it spill, so batched varlen prefill got up to 28% slower on hopper.

on hopper (and also on sm_100) i fixed this with setmaxnreg. the full-width K2 now launches 256 threads at 128 registers, so two blocks still fit per SM. the producer warpgroup then drops to 40 registers and the math warpgroup grows to 216, so the loop no longer spills. the V-split K2 fits in 168 registers and keeps 192 threads. i also pick the V-split grid whenever it fits in one wave at two blocks per SM.

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 tma_store_wait<0>() before block exit after the TMA stores, on all architectures.

@mmastrac could you update PR description accordingly, i don't have the power to do that.

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.

5 participants