Skip to content

gemm, placement: the prompt GEMM admits NVFP4, its store scaling each slot by its expert's tensor scale - #545

Merged
juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-nvfp4-routed-gemm
Oct 20, 2026
Merged

juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-nvfp4-routed-gemm

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

프롬프트 배치에서 라우팅된 NVFP4 전문가를 기존 gemm_nvfp4p 커널로 계산하도록 합니다.

Part of #493 and #495.

Routed NVFP4 experts now run through the existing gemm_nvfp4p prompt GEMM, because kernels::gemm accepts NVFP4. The prompt GEMM takes routed and dense NVFP4 weights with a scale per expert: its gate_gemm cases pass, its spill does not grow and no measured shape is slower than main.

What and why

Main runs NVFP4 stacks only through the decode gemv. A prompt batch with routed NVFP4 experts has no GEMM entry, although each expert already has its own f32 tensor scale.

  • placement/kernels.rs: gemm(NVFP4) returns GemmEntry::Nvfp4, and the pin test gains the pair.
  • gpu/gemm/gemm32.rs, kernels32.rs: Gemm32Weight::Nvfp4Planes holds an Nvfp4Stack, and gemm_nvfp4p takes one scale per expert. The tile's expert scale is staged in one shared word before the K walk and read once for the stores, so the kernel needs no register for the expert index during the walk. The launcher check for NVFP4 is one call to Nvfp4Stack::experts (made pub(crate) in mxfp4_sel.rs), the owner of the K % 64, plane shape, whole-expert and one-scale-an-expert checks, so the launcher keeps no copy of them.
  • gpu/weights.rs, placement/placement.rs: a dense NVFP4 tensor uploads its scale as a one-element device buffer, which the card segment counts (4 bytes).
  • nemotron_h_moe/prefill.rs: the dense NVFP4 prompt projection hands its planes to the GEMM as a one-expert stack.
  • gate_gemm.rs: g32_nvfp4_routed gives each expert its own scale, compares every slot with dot32_nv under that scale and repeats the launch. g32_refuse gains nvfp4_scales_count.
  • Replaced gate clause: g32_refuse no longer has nvfp4_scale_nan. A NaN scale is refused by name at load, in weights::tensor_scales, and gate_nvfp4 tests that refusal (a dense nan tensor and the expert_nan_* stacks). The launch now owns only the scale count, so the clause became nvfp4_scales_count. The change carries a PIN(2026-10-20) line before the new clause in gate_gemm.rs.
  • Changed expectation: the card_buffers placement test now expects [4*128, 4*16, 4] for a dense NVFP4 tensor, because its one scale is now a counted device buffer. It carries a PIN(2026-10-20) line.

The maintainer's open #344 redesigns gemm_nvfp4p. This PR does not touch the kernel's inner loops and moves only where the scale is read, which keeps the overlap with #344 small.

Release notes: Added: Routed NVFP4 experts run in the prompt batch through gemm_nvfp4p.

Code shape

  • No second copy. The dense and routed NVFP4 prompt paths share one kernel and one scale path, and the launcher reuses Nvfp4Stack::experts for its checks instead of repeating them.
  • Model code holds only architecture facts. The NemotronH change hands planes to the GEMM as a one-expert stack and adds no branch to the common GEMM.
  • Dead code is gone. The single-scale parameter and the finite-scale check are replaced by the per-expert scales, and the launcher's NVFP4 checks come from Nvfp4Stack::experts. The gate's single-use expert_scales helper is gone, with its map written inline at its one call.
  • No silent failure. A wrong scale count is a named refusal (nvfp4_scales_count), and the four NVFP4 g32_refuse cases still refuse, with the error text now coming from Nvfp4Stack::experts, and an id past the stack raises expert_id.
  • Comments state what is true now.
  • No relaxed gate. No band moved. tools/unsafe-ratchet.txt goes from gpu 1980 to gpu 1981: staging writes the shared word and the store reads it, two blocks, each with a SAFETY line.
  • cargo fmt and tools/check-unsafe.sh ran on what I touched, with the new pin value above.

Duplication found or removed: none.

What I ran

The oracle is dot32_nv in crates/gpu-gates/src/gemm32.rs, which gate_gemm compares every slot against, with each expert's own scale. The gate also checks decode_vs_ggml against the Qwen3-30B-A3B Q4_K_M reference model. Commands on the GB10 machine, from the repository root with BLOOMERY_ARCH=sm_121:

./target/release/gate_gemm --case g32_nvfp4
./target/release/gate_gemm --case g32_refuse
./target/release/bench_nvfp4
bash tools/ptx-spill-check.sh tools/ref/ptx-shapes.tsv tools/ref/ptx-shapes/ gate_e2e
Reference Result Difference
gate_gemm g32_nvfp4, GB10 17 dense rows and 15 routed rows PASS, rc 0 0 rows fail
gate_gemm g32_refuse, GB10 PASS, rc 0 0 refusals missed
FAIL-first mutant, every tile reads expert 0's scale 14 routed rows FAIL, 1 PASS the gate fails on the defect
Spill of gemm_nvfp4p, sm_86 main 0, head 0 unchanged
Spill of gemm_nvfp4p, sm_121 on GB10 main 40 B, head 40 B unchanged
Kernel time, 6 Qwen3.8 FP4-Local shapes, GB10 head/main median -0.07% to -0.55% no shape is slower

bench_nvfp4 is a measurement-only harness that is not part of this PR. It runs the Qwen3.8 FP4-Local prompt shapes: 512 experts, top 10, gate 640 by K 2560, down 2560 by K 640 and a dense 2560 by 2560, each at 512 and 2048 tokens. Each arm captures 64 launches in a graph and takes the minimum over 7 rounds. The table gives the median of 4 runs per side, main and head alternating in one sitting. Main was built at 5c5c9355c and the head at fc26e826. The head of this PR is b3f288d5, rebased onto upstream d322507e. The kernel code is unchanged since fc26e826, so the timing is carried and not measured again. The FAIL-first mutant and the sm_86 spill comparison also ran on the fc26e826 tree.

The sm_86 spill comes from ptx-compare against the base, which lists gemm_nvfp4p as the only changed entry with no spill change, and from the 0 pinned in tools/ref/ptx-shapes.tsv. On sm_121 the kernel already spills 40 B on main, and this PR does not add to it.

What I ran into

This does not block the PR. tools/ref/ptx-shapes/sm_121-cc12.1.tsv has no gemm_nvfp4p row, so the 40 B sm_121 spill is not pinned on main or here. I did not add the row because other open PRs edit the pin files. The generate_ds41 and gate_qwen3vl_tower spill scans did not run on the GB10 machine.

Hardware

DGX Spark (GB10), measured at head b3f288d5 against main d322507e.

nvidia-smi reports NVIDIA GB10, 595.84, CUDA 13.3 (name, driver, CUDA; compute capability 12.1, ptxas 13.3.73, BLOOMERY_ARCH=sm_121).

  • just gate-gpu-gemm --case g32_nvfp4 builds and runs gate_gemm. The recipe's container wrapper was not available on this machine, so I ran the same binary directly with --case g32_nvfp4: 32 rows PASS (17 dense, 15 routed), rc 0.
  • --case g32_refuse: PASS, rc 0. The four NVFP4 cases nvfp4_k_not_64, nvfp4_qs_short, nvfp4_e_short and nvfp4_scales_count are each refused.
  • ptx-scan gate_e2e gemm_nvfp4p, sm_121:
Revision regs spill smem scalar loads
main d322507e 128 40 B 33280 16
head b3f288d5 128 40 B 33296 17

The 16 extra bytes of shared memory and the one extra scalar load are the staged scale word. Registers and spill are the same. The sm_86 spill comparison ran on an RTX 5090 with the cc 12.0 JIT.

AI help

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

@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 placement Placement planning: crates/placement labels Oct 20, 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 behavior defect or proven quality defect survives the checks of the changed paths.

Non-blocking

⚠ Make the timed A/B reproducible: crates/gpu/src/gemm/gemm32.rs:809 adds one 4-byte scale read per active NVFP4 GEMM block [derived]; the body reports 16 extra shared-memory bytes per block on sm_121. The occupancy/geometry class requires an owning gate and timed A/B. Gate results are supplied, but the timing evidence gives only a ratio range from bench_nvfp4, whose source is absent. Attach the harness, per-shape alternating-run output, exact model filename and commands identifying the measured commits.

Checked

Duplicates: shape checks reuse Nvfp4Stack::experts. Dryer reported 0 pairs; gpu-gates was excluded from its search.

Complexity: nvfp4_routed_case 42→46 adds uploads and a rerun assertion; nvfp4_dense_case 46→48 and refusals 43→45 add upload error paths. No new model-dispatch branch.

Silent failure: invalid stack shapes return named errors. File NaN scales are refused at load. Refused route slots receive NaN outputs.

Tests: static mutation audit. Reading expert 0's scale at gemm32.rs:472 makes the routed contract assertion red (the body reports 14 failing rows). Dropping the multiplier at :487 fails the dense contract check. Skipping experts at :1144 fails the scale-count refusal. Removing card_buffers' dense scale buffer fails its size assertion; returning None for NVFP4 fails the admission assertion.

Gates: dated PINs cover admission, the dense scale buffer, replacement of the NaN-scale clause and the unsafe ratchet. No numeric band moved.

Adversarial checks: the final 16-row slab still stages its scale outside the active-warp branch. An entirely refused route exits before reading the shared scale. Stack rows and scale count agree with the route's expert count.

Escape catalog: C1–C4 do not match the changed code. No ledger lessons are in force.

Notes

Static review only; no build or GPU gate was run here. The PR supplies GB10 gate results and a FAIL-first scale-index mutant.

Card check: gpu-evidence. This pull request changes crates/gpu-gates/src/bin/gate_gemm.rs, crates/gpu/src/arch/nemotron_h_moe/prefill.rs, crates/gpu/src/gemm/gemm32.rs … and carries a gate run on the author's card, which counts as verified.

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

@github-actions github-actions Bot added gpu-evidence Verified on the author's card: the pull request carries the gate run and its output and removed needs-gpu Changes code a card must check, and carries no gate run on a card labels Oct 20, 2026
@juliankang4

Copy link
Copy Markdown
Collaborator Author

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

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 gpu-evidence Verified on the author's card: the pull request carries the gate run and its output placement Placement planning: crates/placement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant