Skip to content

gpu: an NVFP4 run of several tokens dots each selected expert's rows for all its slots in one warp - #808

Merged
juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-verify-pack
Oct 25, 2026
Merged

juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-verify-pack

Conversation

@juliankang4

@juliankang4 juliankang4 commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

MTP 검증 단계에서 한 expert를 고른 여러 slot의 NVFP4 행 dot을 첫 slot의 warp 하나에서 차례로 계산하게 해, GB10에서 draft를 켠 디코드 속도를 높이는 속도 PR입니다.

What and why

An MTP verify step runs the model on two to four tokens at once. Their routes often name the same expert more than once, and the NVFP4 selected-expert kernels gave every slot its own warp that read that expert's weight rows again. A route trace of a 128-token code generation shows how often this happens: the share of selected experts that another row of the same step already selected is 0.183 for 2 rows, 0.275 for 3 rows and 0.335 for 4 rows.

This PR adds two kernel entries to crates/gpu/src/mxfp4_sel.rs, nvfp4_gate_up_sel_cols and nvfp4_gemv_sel32_cols. When a run holds more than one token (n_slots > slots_per_col), the warp of the first slot that names an expert computes that expert's rows for every slot that names it, one slot's dot after the other. The warps of the later slots end before they read any weight, so an expert repeated d times uses R dot warps instead of dR (R rows per expert) with the same launch count. A slot's result is bit for bit what the one-token entry writes for it. Decode with one token still uses the existing entries.

  • names_before answers, for a whole warp, whether an earlier slot of the run chose the same expert. The other slots of an expert are found with warp::ballot, and the row dots go through the shared lane_partials.
  • Nvfp4Down gains a slots_per_col field, which the caller supplies as data (the number of routes one token uses). enqueue_gate_up and enqueue_down pick the _cols entries when n_slots > slots_per_col. A slots_per_col of zero is refused with a named error.
  • crates/gpu/src/card_leg.rs supplies the field, and the wide test hw_the_leg_writes_each_slots_one_token_rows_at_any_m gains a case for the NVFP4 stack (WIDE_PLANES).
  • crates/gpu-gates/src/bin/gate_nvfp4.rs supplies the field in its three Nvfp4Down values.
  • The PTX shape tables pin the two new entries at zero spills and zero local bytes, and the unsafe ratchet carries a dated PIN for the new blocks.

The kernel itself saves little time. In a verify profile the two kernels together gain about 0.37 ms per step, since the GB10's L2 cache already served most repeated reads. Most of the end-to-end gain on code looks like it comes from the draft width chooser: it picks a width from measured step times, and the cheaper verify steps moved it toward deeper drafts. On code, the time per draft window fell from 53.60 ms to 53.14 ms while the tokens emitted per window rose from 2.831 to 2.915.

Release notes: Changed: draft verification of NVFP4 checkpoints computes all slots of a selected expert in one warp, which speeds up decode with the draft on a GB10.

Code shape

  • No second copy. The row dots, the activation rule (act::apply_checked) and the fault sink are the existing owners. The new entries add only the first-slot check and the slot scan.
  • Model code holds only architecture facts. slots_per_col comes from the caller as data. No model name or per-model branch was added to a common owner.
  • Dead code is gone. The from argument of names_before was always 0 and is removed.
  • No silent failure. A slots_per_col of zero is a named error, and an invalid expert id faults as it does in the existing entries.
  • Comments state what is true now.
  • No relaxed gate. No band or tolerance moved. The unsafe ratchet rose with a dated PIN for new code.
  • cargo fmt and cargo clippy are clean on what I touched; the lint count did not go up. cargo clippy ended with exit 0 on this head. I did not record a separate cargo fmt result.

Duplication found or removed (path:line), or "none": crates/gpu/src/mxfp4_sel.rs:1368 against :1253 and :552, and :1615 against :1515 and :1368. These share only the entry head (thread row, id read, fault sink). The body of each new entry is the first-slot check and the slot scan, and the dots use the shared lane_partials. Folding the heads into one device function would change the PTX of the existing entries.

What I ran

GPU Evidence

Timed runs and the model gate ran on one DGX Spark. Its card and driver:

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

The NVFP4 checkpoint gate on that card, on this head:

just gate-gpu-qwen4exp-nvfp4
(l) plan card=GB10 ctx_max=3072 ubatch=3072 host_experts=0 card_experts=24576 layers short of 512 card experts: [] PASS
(rt) the load for a route trace: host views 0 (want 0), trace positions Some(5) (want 5), every slot on the card true, logits the plain load's bit for bit true PASS
gpu-gate.sh: gate_qwen4exp_nvfp4 ran 101 s (exit 0)

In the same gate the last argmax of one pass and of one ubatch equals the reference (11751), and the argmax at position 3000 after 3000 ids equals the reference (423).

Other checks:

  • On an RTX 5090 machine the full cargo check passes, and the three library tests pass 3/3 (the wide test with the NVFP4 stack, the selected-expert arms and the refusal). just gate-gpu-nvfp4 passes with routed_gate_up bits 3200/3200 and routed_down bits 12800/12800. These ran on earlier commits of this branch and were not repeated on the head.
  • The PTX spill gate passes on sm_121a, sm_121 and sm_86 with all 435, 428 and 428 generate_ds41 entries pinned. ptx-compare of main against the branch reports added 2, removed none, changed none.
  • The compiled PTX of the six NVFP4 selected-expert entries is identical on sm_121a and on sm_86 between the commit that was timed and this head, which differ only by the removed argument, one unsafe read split in two and the unsafe PIN.
  • The unsafe ratchet is ok at gpu 2433/312/10.

FAIL-first, each against the wide test or the refusal test:

  • A gate/up entry that writes only its own slot fails the wide test with "a card slot's rows are all zero".
  • A gate/up entry that writes only the first slot (hits = 0) fails the wide test with "rows are all zero".
  • A down entry that writes only the first slot fails the wide test with "holds no finite row the run wrote".
  • Allowing a slots_per_col of zero (if false in place of the check) fails the refusal test.

Speed

The timed model is the FP4-Local checkpoint with the Q8_0 MTP draft, greedy decoding, three rounds over eight windows of each corpus, base and head built from their own trees. Each cell is the median of three rounds. Greedy output is identical on every request. The smallest effect the run can resolve is 3.698% for code-fn decode and 1.804% for prose-en decode.

Corpus Metric main head Change
code-fn Decode tok/s 52.5365 54.7604 +4.233%
code-fn Prefill tok/s 1768.13 1770.48 +0.132%
code-fn TTFT p95 ms 2322.96 2321.09 -0.080%
prose-en Decode tok/s 40.731 41.2748 +1.335%
prose-en Prefill tok/s 1780.00 1783.18 +0.178%
prose-en TTFT p95 ms 2307.69 2302.52 -0.224%

code-fn decode +4.233% (resolved above 3.698%); prose-en decode +1.335% (below the 1.804% the run can resolve, not resolved)

Only the code-fn decode gain is resolved. The other rows are not resolved, so they are not evidence of an effect or of none.

Reproduction

just gate-gpu-nvfp4
just gate-gpu-qwen4exp-nvfp4

The second gate needs the NVFP4 checkpoint and the reference sets. For the timing, serve the FP4-Local checkpoint with the Q8_0 MTP draft from upstream main and from this branch, and send the same greedy requests over the same corpus windows, alternating the two trees.

Hardware

One DGX Spark (GB10, driver 595.84, CUDA 13.3 toolchain) for the timed runs and the model gate; an RTX 5090 machine for the build and the NVFP4 unit gate. This section lists only the card and driver lines saved with the runs.

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 tools Tools, recipes, CI and the toolchain: tools/, justfile, .github/, Cargo labels Oct 25, 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 bug or proven quality defect survived the bounds, ownership and numeric checks.

Non-blocking

⚠ Complete the geometry evidence: crates/gpu/src/mxfp4_sel.rs:1664.
For an expert selected d times with R output rows, dR dot warps become R warps executing d dots serially. This is the occupancy/geometry class in AGENTS.md.
Supply the register/shared-memory counts and occupancy derivation, both timed commit hashes, the checkpoint filename, and exact A/B commands with output. These make the reported gain reproducible before it becomes a baseline.

Optimization headroom

  • D. More at once: crates/gpu/src/mxfp4_sel.rs:1676. Each matching slot calls lane_partials::<1> and traverses the weights again. Reuse lane_partials::<8> with its existing mask and column strides within an eight-slot tile. For d matches in that tile, K weights per row and m tokens per step, this removes (d−1)K/(2m) source-level code-plane load bytes per expert row per token [derived], plus repeated scale loads and decoding. Cache effects determine the memory-traffic saving.

Checked

Duplicates: 4 new functions/constants checked; no duplicate owner found. Existing dot, activation and fault owners are reused. Dryer reported 0 pairs.
Complexity: crapper flagged no changed function.
Silent failure: 2 vector-view exit sites followed; host alignment and shape checks prevent them. Invalid ids reach the token reader as a named fault.
Tests: static mutation analysis finds assertions fail for a wrong gate column (1435), wrong down column (1683), skipped CardLeg down (1179), and removed zero-slot check (1905).
Gates: no tolerance moved; new PTX entries pin zero spills/local bytes; the unsafe increase has PIN(2026-10-25) and its reason.
Adversarial: repeated ids across 32-slot boundaries retain one writer; tail lanes are guarded; invalid ids and non-finite gate/up sums fault. Zero rows/slots are rejected before launch.
Escape catalog: C1–C4 do not match.

Notes

Static review only; no GPU build or gate was run here.
Dryer did not scan gpu-gates; its changed constructors were checked manually.
The PR includes GB10 gate output but lacks both commit hashes required for complete GPU evidence.

Card check: needs-gpu. This pull request changes crates/gpu-gates/src/bin/gate_nvfp4.rs, crates/gpu/src/card_leg.rs, crates/gpu/src/mxfp4_sel.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)

@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 0c768d4 into midagedev:main Oct 25, 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