Repository navigation
gpu: run routed NVFP4 prompt GEMM on FP4 tensor cores - #687
Conversation
There was a problem hiding this comment.
Verdict: MUST_FIX: the native fused producer accepts buffers that violate its vector-load alignment.
Must fix
1. Check both input buffers before the vector loads
crates/gpu/src/gemm/nvfp4mma.rs:104 casts the gate input to F32x4; line 106 does the same for the up input. A safe Window::<f32>::of(&parent, 4, 64) over a parent of at least 65 floats passes the public launchers' length checks with n_cols=1, k=64. With native scratch, either window reaches a vector load at an address that is 4 modulo 16, violating its alignment and allowing a CUDA misaligned-address failure.
Checking the shared launcher now closes all three fused entry points before callers depend on this unchecked alignment invariant.
Insert these checks in Kernels::swiglu, before let k = act.k as u32;:
crate::tensor::check_alignment::<cuda_device::vector::F32x4, _>(
"NVFP4 activation", "g", g,
)?;
crate::tensor::check_alignment::<cuda_device::vector::F32x4, _>(
"NVFP4 activation", "u", u,
)?;Add a native-scratch gate case using a byte-offset-4 window for each input. It must return the named alignment error before launching; removing either check must fail its assertion.
Non-blocking
⚠ Validate the existing GEMM paths: crates/gpu/src/gemm/gemm32.rs:683 also changes the Q8/Q5/IQ epilogues, while grouped.rs changes K-quants. A full 128×64 tile now executes 8192 range-checked epilogues per 32-value block [derived]. Attach their owning-gate outputs and a timed A/B for the precision change, with occupancy evidence for the changed kernels. The supplied NVFP4 run does not exercise those formats.
Optimization headroom
- G. Per-launch and per-sync fixed cost: crates/gpu/src/gemm/act32.rs:228, fuse the plain Q8 and FP4 producers as the SwiGLU path already does. With native NVFP4 scratch, this removes one launch per plain quantization call on the routed prompt path.
Checked
Duplicates: the retained findings concern separate defects or opportunities.
Complexity: scaled_sum adds fast-path range checks and an F64 fallback inside existing GEMM epilogues.
Silent failure: the public fused launchers check lengths but not the alignment required by act_quad.
Tests: the native gate compares plain/fused outputs, Q8 bytes, guards and graph replay. It uses allocated input buffers and has no native input-alignment case. CUDA gates were not rerun during validation.
Gates: no existing accuracy band moved; target spill and unsafe rows have dated PIN reasons.
Escape catalog: C1–C4 do not apply to the retained findings. No ledger lessons are in force.
Notes
The GPU evidence omits the tested base/head commit hashes and the timing commands with output. gpu.json records evidence=false.
Card check: needs-gpu. This pull request changes crates/gpu-gates/src/bin/gate_gemm.rs, crates/gpu-gates/src/bin/gate_qwen4exp_nvfp4.rs, crates/gpu-gates/src/gemm32.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)
4fd25d1 to
4b3bd63
Compare
There was a problem hiding this comment.
Since the last review:
- Alignment must-fix: fixed at crates/gpu/src/gemm/nvfp4mma.rs:705. Both inputs are checked in the shared launcher.
- Existing GEMM validation: still open at crates/gpu/src/gemm/gemm32.rs:683. The supplied runs cover NVFP4.
Verdict: CLEAN: the alignment defect is closed, and no new must-fix survives review of the added commit.
Non-blocking
⚠ Validate the existing GEMM paths: crates/gpu/src/gemm/gemm32.rs:683 changes Q8/Q5/IQ epilogues; grouped.rs changes K-quants. A full 128×64 tile executes 8192 range-checked epilogues per 32-value block [derived]. Supply their owning-gate outputs and a timed A/B for the precision change, with occupancy evidence. The NVFP4 runs do not exercise those formats.
Optimization headroom
- G. Per-launch and per-sync fixed cost: crates/gpu/src/gemm/act32.rs:228, fuse the plain Q8 and FP4 producers. With native NVFP4 scratch, this removes one launch per plain routed prompt quantization call.
Checked
Duplicates: 0 new definitions in the interdiff; existing alignment owner reused. Dryer reports 0 pairs. Grep found no duplicate of the added gate cases.
Complexity: nvfp4_native_case gains an alignment-case loop and error matching. Crapper's other six flags concern unchanged commits; no kernel body changed in this round.
Silent failure: 2 new alignment checks followed through all 3 public fused launchers; errors propagate before launch and before updating the filled count.
Tests: removing the check at nvfp4mma.rs:705 or :710 prevents the corresponding named refusal at gate_gemm.rs:6885; the gate fails. This is source inspection of the mutation, not an executed mutation test. CUDA gates were not rerun.
Bounds: native K is a multiple of 64; aligned bases keep every four-value load aligned, including later columns and the selected-column path.
Gates: no band, tolerance or pin changes in the added commit.
Escape catalog: C1–C4 do not match the added commit.
Notes
Dryer did not search crates/gpu-gates; the added gate lines were checked by Grep and Read.
The PR evidence omits the tested base/head commit hashes and timing commands with output. It also has no run output for the new alignment cases.
Card check: needs-gpu. This pull request changes crates/gpu-gates/src/bin/gate_gemm.rs, crates/gpu-gates/src/bin/gate_qwen4exp_nvfp4.rs, crates/gpu-gates/src/gemm32.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)
4b3bd63 to
ae85a99
Compare
There was a problem hiding this comment.
Since the last review:
- Alignment must-fix: remains fixed at crates/gpu/src/gemm/nvfp4mma.rs:705. Both inputs retain their alignment checks.
- Existing GEMM validation: still open at crates/gpu/src/gemm/gemm32.rs:683. The supplied runs cover NVFP4.
Verdict: CLEAN: the interdiff only updates the dated unsafe pin to match the rebased source counts.
Non-blocking
⚠ Validate the existing GEMM paths: crates/gpu/src/gemm/gemm32.rs:683 changes Q8/Q5/IQ epilogues; crates/gpu/src/gemm/grouped.rs:504 changes K-quants. A full 128×64 tile executes 8192 range-checked epilogues per 32-value block [derived]. Supply their owning-gate outputs and a timed A/B for the precision change, with occupancy evidence. The supplied NVFP4 runs do not exercise those formats.
Checked
Duplicates: 0 new definitions in the interdiff. Dryer reports 0 pairs; none to read.
Complexity: all seven crapper flags concern unchanged implementation commits. No function changes in the interdiff.
Silent failure: 0 new sites; no runtime behavior changes.
Tests: no new or changed tests in the interdiff, so no test mutation applies. CUDA gates were not rerun.
Gates: independently recounted GPU unsafe lines using the base check's rules: base 2186/267/10, head 2231/268/10 (blocks/functions/impls). The entire increase is 45 blocks and one function in nvfp4mma.rs. The dated pin matches exactly and adds no unused allowance; numeric bands are unchanged.
Escape catalog: C1–C4 do not match the interdiff.
Notes
Dryer did not search crates/gpu-gates; this interdiff changes no gate source.
The PR evidence still omits the tested base/head commit hashes and timing commands with output.
Card check: needs-gpu. This pull request changes crates/gpu-gates/src/bin/gate_gemm.rs, crates/gpu-gates/src/bin/gate_qwen4exp_nvfp4.rs, crates/gpu-gates/src/gemm32.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)
|
The review is done and the contribution check passed, so I'm merging this. |
Qwen3.8 NVFP4 모델의 routed expert prompt GEMM을 GB10의 FP4 텐서 코어(block-scaled MMA)로 실행해 prefill과 첫 토큰까지의 시간을 줄입니다.
Part of #565.
What and why
On a GB10 the routed expert products of an NVFP4 model run on INT8 tensor cores in the prompt path: each NVFP4 weight is expanded to Q8 values and multiplied with Q8 activations. GB10 has FP4 tensor cores with block scaling, so the 4-bit weights can be multiplied directly.
This PR adds a native routed NVFP4 prompt kernel to the shared
Gemm32Kernelsowner incrates/gpu/src/gemm/nvfp4mma.rs.ldmatrix, and repacks weight nibble pairs with byte permutations. It reuses the existing route list and fault owner, so there is no second expert routing list.GemmAct32::new_routed_forallocates the FP4 representation only when the resident routed format is NVFP4; other formats keep their Q8 storage.scaled_sumincrates/gpu/src/gemm/grouped.rs, applies the weight, activation and expert scales for every NVFP4 product. Ordinary inputs keep the F32 multiply and FMA rounding. When an intermediate product would overflow or underflow, it multiplies the original operands in F64 and narrows after the last scale, so a finite result stays finite. The native kernel's two output stores, the plain and fused Q8 reconstruction, the W4A8 path and the grouped epilogues all call it. The W4A8 NVFP4 accumulator is F64 because the expert scale is applied after the sum.crates/gpu/build.rsenables the kernel for explicitsm_120a/fandsm_121a/fbuilds. Other targets keep the existing path.This is a W4A4 product: the activations round to FP4 where the existing path keeps Q8. The faster path is the default, and the existing oracle bands, which this PR does not change, still pass. KL against the oracle is reported below as a diagnostic.
Release notes: Changed: routed NVFP4 prompt products run on the FP4 tensor cores of a GB10, which speeds up prefill.
Code shape
PINexplanation follow the addedunsafeblocks, each with aSAFETYline.cargo fmtandcargo clippyare clean on what I touched.Duplication found or removed (
path:line), or "none": none.packed_pair(crates/gpu/src/gemm/nvfp4mma.rs:251) permutes FP4 nibbles with integer register operations and the INT8 MMA helper (crates/gpu/src/gemm/gemm32.rs:222) multiplies INT8 fragments, so their PTX operations differ. The all-columns and the selected-columns entries (nvfp4mma.rs:297,nvfp4mma.rs:317) call the samefused_swiglu.What I ran
The base of the comparison is main with the dense BF16 tensor-core prompt GEMM merged.
GPU Evidence
On an NVIDIA GB10:
The native gate has 32 cases: plain and fused producers, one-hot weights with alternating signs, partial tiles, K=20480, tiny inputs, varied inputs, and six extreme-range classes (expert scales from 2^-120 to 2^120, inputs of plus and minus F32 MAX, subnormal scales, and a last multiply that overflows after a finite first one). The extreme classes run the native plain and fused producers and the W4A8 path against an independent F64 expectation. Every case checks sentinel guards and eager against graph replay. All 32 print PASS with
wrong=0; four of them:The output hashes of 12 ordinary-input cases are identical with and without the scale range handling, so ordinary inputs produce the same output as before.
A kernel that writes zeros fails the gate. A fused producer that packs unrounded SiLU values fails
producer_eq:The full-model NVFP4 oracle, with its bands unchanged:
Prefill and decode
One server load per arm, base then head, two requests per load. The prompt is a 4096-token code window (corpus offset 20480), 8 tokens are generated at temperature 0 with top-k 1, with no draft, no RAM cache, one request at a time and
--ctx-size 4224. Modelnvidia/Qwen3.8-Flash-Next-NVFP4atfc694b54fb0174e0913e6adf86691ef85a4ead47, on one GB10 with driver 595.84, CUDA 13.3 and release binaries built withcargo oxide build --arch sm_121a. The first request of each load includes first-use effects.Prefill of the 4096-token prompt rose by 2.25% on the first request and 2.42% on the second, and the first request's time to first token fell by 2.20%.
Both arms generated the same eight greedy token ids on both requests.
A second pair of loads with the scale range handling in place, on the same prompt and settings, gives 777.588 tok/s against 777.944 tok/s for first-request prefill (+0.05%), with the same eight greedy token ids. The decode kernels are unchanged, and eight generated tokens are too few to separate the small decode differences from noise.
Oracle diagnostics, KL against the reference set (the BF16 rows are judged by the oracle, the Q8 row is recorded only):
What I ran into
This does not block the PR. KL against the oracle is higher on the head than on the base for the one-ubatch and position 3000 sets, as expected when activations round to FP4. The oracle's argmax and margin checks pass on every set. The timing is two requests per arm on one prompt, not a long benchmark.
Hardware
One NVIDIA DGX Spark (GB10), driver 595.84, CUDA 13.3, with CUDA 13.3.1 in the build image.
AI help
AI 코딩 에이전트가 코드를 작성한 후 설명을 썼고, 테스트를 마쳤습니다. diff 리뷰와 PR 초안 리뷰는 제가 직접 했습니다.