Skip to content

gemm: fold the GEMM epilogues in F32 and redo a tile exactly only when it leaves range - #700

Merged
juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:fix-q38-prefill-regression
Oct 23, 2026
Merged

juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:fix-q38-prefill-regression

Conversation

@juliankang4

@juliankang4 juliankang4 commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

GB10에서 4K prompt prefill이 약 3배 느려진 문제를, GEMM epilogue를 F32로 먼저 걷고 범위를 벗어난 블록만 정확하게 다시 걷게 해서 고칩니다.

Part of #565. Fixes the High item in #699.

What and why

e4364b6cb ("gpu: preserve Q8 rounding in fused NVFP4 activations", part of #687) made every grouped and gemm32 GEMM epilogue step call scaled_sum, which checks each F32 result for leaving the normal range and falls back to F64, and it made the NVFP4 tile accumulators F64. On an sm_121 build the GEMM kernels then spill heavily. A 4,096-id prompt prefill of Qwen3.8 FP4-Local on a DGX Spark ran at 387.6 tok/s on main and runs at 1118.45 tok/s with this change. The commit's PTX pins went only into the sm_121a table, so the sm_121 and sm_86 spill pins did not go red.

This change walks each tile with the F32 epilogue alone and marks any activation block scale under 2^-64. Each warp votes with warp::any whether one of its outputs ended non-finite or a scale was marked. Only a voted block walks the tile again with scaled_sum, one n-tile of four accumulators at a time (F64 for NVFP4), and stores that walk's outputs. When no vote is cast, no step left the F32 normal range, so the F32 bits are the bits scaled_sum produces. The proof is in the "Range" section of the grouped.rs module doc.

  • fold4!, gemm_nts!, range_walks! and exact_vote! in grouped.rs serve both the grouped and the gemm32 families. scaled_sum now shares inner32 with the new sum32.
  • Not touched: nvfp4mma.rs (the native sm_121a gemm_nvfp4_mma), the dense BF16 MMA owners, routing and the activation producers.
  • The sm_121, sm_121a and sm_86 (cc 12.1) spill pin tables hold the new GEMM rows with PIN lines, so the base code's GEMM rows go red against the sm_121 table (violations=54 FAIL, 12 of them GEMM rows). The unsafe ratchet goes from 2248 to 2250 for the two VOTES blocks (write and read), each with its own SAFETY line.

Release notes: Fixed: Qwen3.8 4,096-id prompt prefill on a DGX Spark runs at 1118.45 tok/s instead of 387.6 tok/s on main, with the same greedy tokens.

Code shape

  • No second copy. Both GEMM families go through one range_walks! and one fold4!; scaled_sum reuses inner32 instead of repeating its two operations.
  • Model code holds only architecture facts. The change is inside the grouped and gemm32 GEMM owners and reads no model fact or rank count.
  • Dead code is gone. The per-step range test and the F64-only mode plumbing are removed.
  • No silent failure. A non-finite scale makes a non-finite output, which the vote sends to the exact walk, so the result is the one scaled_sum gave before.
  • Comments state what is true now. The module doc "Range" states the invariant with its real bounds (2^-27, 2^-64, 2^-114).
  • No relaxed gate. No band or tolerance moved. The spill pins moved with PIN lines.
  • cargo fmt is clean. cargo clippy was not run on this head here.

Duplication found or removed (path:line): none.

What I ran

Hardware: a DGX Spark (GB10), sm_121 and sm_121a builds, bloomery-dev:cuda13.3.1 container, models Qwen3.8-Flash-Next FP4-Local rev b9ce18a6 and NVFP4 rev fc694b54.

| NVIDIA-SMI 595.84                 Driver Version: 595.84         CUDA Version: 13.3     |
|   0  NVIDIA GB10

The checks below ran on the kernel source of this change before the final rebase onto main. A later macro squash left the PTX identical (COMPARE_RC sm_121 0), and the commit main gained since touches no GEMM file. I did not rerun the A/B on the rebased head.

Gates (BLOOMERY_BOX_LOCAL=1, gate_gemm):

just gate-gpu-gemm   (run as: bash tools/gpu-gate.sh gate_gemm --case "$c")
PASSED: gate_gemm
  • sm_121, full gate with the Qwen3 Q4_K_M file: 264 PASS, 0 FAIL, then it stopped because the DeepSeek-V4.1 split shard is not on the machine (gate_gemm: split shard ... is missing).
  • sm_121, --case _synth, g32, g38, dense, fault, q5_0p: CASE_RC 0 for every case, 988 PASS, 0 FAIL.
  • sm_121a, --case nvfp4_native (its Range legs run the plain gemm_nvfp4p): 37 PASS, 0 FAIL.
  • ptx-compare against main at sm_121: changed 15 entries, all grouped and gemm32 gemm_*; at sm_121a COMPARE_RC 3 for the same reason.

Spill bytes a thread at sm_121 (ptx-scan, spill and JIT local), main against this change:

Entry Main This change
gemm_q4k 1284 / 160 0 / 0
gemm_nvfp4p 504 / 112 72 / 32
gemm_q6k 596 / 88 532 / 64
gemm_q5k 120 / 64 0 / 0
gemm_q3k 64 / 32 0 / 0

gemm_iq3s goes from 0 / 0 to 24 / 16. In nsys of the 4,096-id prefill, gemm_nvfp4p takes 8.140 s over 288 launches on main and 1.286 s on this change.

FAIL-first:

  • The sm_121 spill check with main's GEMM code against the new pin table is red: gemm_q4k spill=1284 pinned=0 ABOVE, gemm_nvfp4p spill=504 pinned=72 ABOVE, gemm_q6k spill=596 pinned=532 ABOVE, and gemm_iq3s spill=0 pinned=24 BELOW.
  • With the exact walk compiled out, the nvfp4_native Range legs fail: Range(7.523164e-37, 3.4028235e38) with native=false shows wrong=16.

Speed, 4,096-id code prompt, C=1, greedy, sm_121 release builds of main and this change, one server load an arm, two requests an arm, clock cap lifted while the requests run:

Config Main prefill tok/s Head prefill tok/s Main TTFT p95 ms Head TTFT p95 ms Main decode tok/s Head decode tok/s
FP4-Local, MTP on, 16 tokens 386.86 1121.8 10661.9 3689.3 52.16 50.44
FP4-Local, MTP on, 128 tokens 387.6 1118.45 10616.3 3706.3 51.42 51.6
FP4-Local, draft off, 16 tokens 393.93 1136.3 10458.5 3655.8 31.21 31.33
NVFP4, MTP on, 16 tokens 370.43 1137.37 11535.0 3627.6 40.34 41.13

Prefill is 2.9 times main's in every row. Decode at 128 tokens moves by +0.4%. The 16-token decode in the first row (-3.3%) did not repeat at 128 tokens. Greedy tokens are equal per request in every pair.

What I ran into

This does not block review.

  • Not run: the two DeepSeek-V4.1 gate cases v41_gate_q3k and v41_gate_q3k_synth, because that model is not on the machine. Q3_K shares its epilogue with Q6_K, IQ3_S and IQ4_XS, whose synthetic bit cases passed, and sel_fault_Q3K passed.
  • The activation-scale mark has no gate input that reaches it. Dropping it leaves nvfp4_native green, because a constant-input Range leg cannot produce an F32 subnormal intermediate under a normal output. The exact walk itself is covered: compiling it out fails the Range legs.
  • The cc 8.6 spill rows were not re-measured, since there is no RTX 3090 here. The sm_86 table was scanned with ptxas on cc 12.1 only.
  • gate-ptx-spill is red on main already: sm_121 has 42 unpinned non-GEMM entries and 3 vision rows without a pin, and sm_121a has 3 more from other owners. No GEMM row is off its pin on this change.
  • Q3 Q4_K_M prefill on GB10 should have regressed by the same cause (gemm_q4k spills 1284 bytes on main and none here). I did not time it.

AI help

AI 코딩 에이전트가 코드를 작성한 후 설명을 썼고, 테스트를 마쳤습니다. diff 리뷰와 PR 초안 리뷰는 제가 직접 했습니다.

…n it leaves range

A GEMM block walks its tile with the F32 epilogue alone (sum32), marking any
activation block scale under 2^-64, and each warp votes whether one of its
outputs ended non-finite or a scale was marked. Only then does the block walk
the tile again with scaled_sum, one n-tile of four accumulators at a time (F64
for NVFP4), and store that walk's outputs. Without either sign no step left
the F32 normal range, so the F32 walk's bits are scaled_sum's (grouped.rs
module doc, "Range").

scaled_sum at every step and the NVFP4 tile's F64 accumulators kept the
grouped and gemm32 entries in local memory on sm_121: at 4096 prompt ids of
Qwen3.8 FP4 on GB10, gemm_nvfp4p took 28.3 ms a launch, and 4.5 ms with
this change. The PTX spill pins of sm_121, sm_121a and sm_86 on cc 12.1
follow the scans.
@github-actions github-actions Bot added gpu The card engine: crates/gpu*, crates/app needs-gpu Changes code a card must check, and carries no gate run on a card tools Tools, recipes, CI and the toolchain: tools/, justfile, .github/, Cargo labels Oct 23, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verdict: CLEAN: No must-fix defect was found in the changed code.

Non-blocking

⚠ Cover the small-scale retry trigger: crates/gpu/src/gemm/grouped.rs:528. The PR reports that deleting this mark leaves nvfp4_native green, so underflow recovery lacks regression coverage. Add tiny, nonuniform activations with small UE4M3 weight scales and an expert scale that restores a normal output. Check against the existing F64 reference and show that deleting the mark fails.

⚠ Complete the performance evidence: crates/gpu/src/gemm/grouped.rs:1590. The fast path adds one block barrier and 32 shared bytes per GEMM block while changing register use. For the occupancy change class, attach register/shared-memory scan columns and the A/B's measured commit pair, model filenames, and exact timing commands with output. The current tables cannot identify or reproduce the measured builds.

Checked

Scope: both cited production lines are added by the diff; both findings remain advisory.
Tests: no test files changed. The PR reports that removing the exact walk fails and removing the small-scale mark passes; these mutations were not reproduced here.
Gates: spill pins and the unsafe ratchet changed with dated reasons; no numerical tolerance changed.
Range: the shared vote is block-uniform; NVFP4 retries retain F64 accumulation through expert scaling.
Escape catalog: no C1–C4 pattern matched the cited paths.

Notes

CUDA gates were not run during validation.

Card check: needs-gpu. This pull request changes crates/gpu/src/gemm/gemm32.rs, crates/gpu/src/gemm/grouped.rs, crates/gpu/src/gemm/kernels.rs … and carries no gate run on a card (CONTRIBUTING's evidence list); a maintainer adds gpu-verified after one.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

Filed outside this PR: #702

@juliankang4

Copy link
Copy Markdown
Collaborator Author

The review is done and the contribution check passed, so I'm merging this.

@juliankang4
juliankang4 merged commit 7d4d5f6 into midagedev:main Oct 23, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gpu The card engine: crates/gpu*, crates/app needs-gpu Changes code a card must check, and carries no gate run on a card tools Tools, recipes, CI and the toolchain: tools/, justfile, .github/, Cargo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant