Skip to content

gpu: run the routed NVFP4 gate, up and SwiGLU as one native launch - #766

Merged
juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-routed-resident
Oct 24, 2026
Merged

juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-routed-resident

Conversation

@juliankang4

@juliankang4 juliankang4 commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

Qwen3.8 NVFP4와 FP4-Local의 prompt 처리에서 routed expert의 gate, up, SwiGLU를 kernel 하나로 합쳐 중간 F32 쓰기와 읽기를 없애고 prefill을 빠르게 하는 작업입니다.

Part of #565.

What and why

On a native FP4 build (the GB10 default, sm_121a) with both routed stacks in NVFP4 planes, a Qwen3.8 layer computes the card slots' gate GEMM, up GEMM and SwiGLU as three launches. The first two write their products to F32 scratch (g and u), and the SwiGLU reads them back and quantizes its result to Q8 for the down GEMM. At 4,096 tokens with top-10 routing that scratch is large.

This PR runs the three as one kernel, gate_up_nvfp4_mma, in crates/gpu/src/gemm/nvfp4mma.rs. Each block holds 64 rows of the gate stack and the same 64 rows of the up stack for one route tile in shared memory, issues the gate and up mma on the same activation fragment, applies SwiGLU in registers and quantizes each slot's two 32-value blocks from a shared tile. The F32 scratch round trip and one launch per layer go away. quantize_nvfp4_32 then packs the down GEMM's FP4 from that Q8, as it already does for other callers.

  • Gemm32Kernels::enqueue_gate_up_sel32 and GateUp32Args (crates/gpu/src/gemm/gemm32.rs) are the one entry, and wide38.rs is its caller. It takes the fused path only on a native FP4 build with both stacks in NVFP4 planes and the activations in the native form. Every other build and stack runs the existing gate GEMM and enqueue_up_swiglu32, the launches main runs today.
  • The native host launcher takes one argument struct, GateUpArgs, plus the stream, the activations and the fault sink, so it needs no lint allowance. Kernel entries keep the function-level too_many_arguments allowance the kernel-entry rule gives them.
  • The epilogue calls crate::elem::silu_mul, the function the activation path runs for SwiGLU, so the arithmetic has one owner. The kernel takes no activation-rule parameters.
  • After the launch the native path marks the activations filled through GemmAct32::fill, like every other quantizer launch, so the BF16 form is forgotten as well.
  • The K staging walk of gemm_nvfp4_mma and gate_up_nvfp4_mma is one macro, nvfp4_walk!, so the protocol has no second copy.
  • The 32-value Q8 rule keeps one owner: the per-lane core of quant32_group is split out as quant32_lane, and both the old quantizer and the new epilogue call it.
  • The launch inputs are checked once at the entry. The entry also refuses down activations whose K differs from the gate and up rows, by name.
  • Host experts, the down GEMM and the top-k sum order are unchanged.
  • tools/ref/ptx-shapes/sm_121a-cc12.1.tsv gains a pinned row for the new kernel (spill 0, JIT local 0). tools/unsafe-ratchet.txt carries a PIN line for the new kernel's unsafe blocks (2297 to 2312). The module-wide too_many_arguments allowance in nvfp4mma.rs is gone; the remaining function-level allowances carry the kernel-entry reason, and the two exemptions that need a dated pin (the swiglu device-core scope and the nvfp4_pack_q8 range loop) have one.

Release notes: Changed: Qwen3.8 NVFP4 and FP4-Local prompt prefill is about 3% faster on a DGX Spark.

Code shape

  • No second copy. The K walk is nvfp4_walk!, the Q8 rule is quant32_lane and the SwiGLU arithmetic is elem::silu_mul, each shared by all of its users.
  • Model code holds only architecture facts. The common owner gained no per-model branch, flag or trait method.
  • Dead code is gone. The check that the rows are a multiple of 64 is dropped because the native activations already guarantee it, and the kernel and launcher carry no unused parameters.
  • No silent failure. A non-finite gate or up value stores the block as refused and raises QuantColumn, a refused expert id raises ExpertId, and a down K that differs from the gate and up rows is refused by name. The gate's reset and replay faults are asserted, not dropped.
  • Comments state what is true now.
  • No relaxed gate. No band, tolerance or lint moved. The PTX shape table and the unsafe ratchet carry the new kernel's row and a PIN line, and the gate that covers the fused launch now replays over reset buffers (see "What I ran").
  • cargo fmt is clean (tools/mac-check.sh fmt, rc 0). Clippy at sm_121a reports no warning in code this PR adds (see "What I ran into"); the default-target lint count is checked by CI.

Duplication found or removed (path:line), or "none": none left on the head. The duplicate scan matched the new kernel against gemm_nvfp4_mma (the K walk, now the shared macro; the fragment step and the epilogue differ) and against swiglu_nvfp4_32 (it reads F32 g and u from global memory and packs FP4, while the new kernel computes both in registers and stores Q8). Both reuse elem::silu_mul and the shared Q8 owner.

What I ran

All gates and timings ran on a DGX Spark. The first timed sitting ran on the commit before the kernel's argument cleanup; the second, shorter sitting ran on this head. "What I ran into" lists what that changes.

Output is unchanged

nvfp4_gate_up compares the fused launch with the existing split launches on the same route at four shapes (64x64 over 9 tokens, 128x128 over 50 with a refused expert id, 192x256 over 37 with a NaN in token 5, 640x2560 over 131), three card experts, one host place and top-2, and checks the down-K refusal. It checks every card slot's Q8 bytes, the down GEMM's output bits and the expected fault on eager and graph replay. After capture it resets the output to its sentinel and the down activations to the quantization of a zero input, replays the graph, and then reads the Q8 bytes and asserts that the replay's fault equals the eager fault. The fused path leaves g and u at their sentinel.

  • just gate-gpu-gemm --case nvfp4_gate_up, run as the recipe's bash tools/gpu-gate.sh gate_gemm --case nvfp4_gate_up step: GATE nvfp4_gate_up: tools/gpu-gate.sh gate_gemm --case nvfp4_gate_up rc=0 pass=5 fail=0, with q8_off=0 y_eq=true eager_graph_eq=true replay_fault_eq=true on every shape and refused_k=true on the K refusal
  • just gate-gpu-gemm --case nvfp4_native, run as the recipe's bash tools/gpu-gate.sh gate_gemm --case nvfp4_native step: GATE nvfp4_native: tools/gpu-gate.sh gate_gemm --case nvfp4_native rc=0 pass=37 fail=0
  • just gate-gpu-gemm --case nvfp4_swiglu, run as the recipe's bash tools/gpu-gate.sh gate_gemm --case nvfp4_swiglu step: GATE nvfp4_swiglu: tools/gpu-gate.sh gate_gemm --case nvfp4_swiglu rc=0 pass=21 fail=0
  • Greedy 128-token outputs of head and main are equal on both windows in both rounds of the short sitting, and in 12 of 12 window pairs of the first sitting.

These gates ran on the tree this head was rebased from; the rebase changed only the unsafe-ratchet lines.

FAIL-first

  • Replay: the mutant that captures only the down GEMM turns four of five clauses red, with eager_graph_eq=false on all four shapes and replay_fault_eq=false on the refused and poisoned shapes (mutant_red=yes). The mutant that never launches the replay does the same. A gate that replayed over the eager run's activations would pass the first mutant.
  • Output: the mutant let b = 2 * rt + blk to let b = 2 * rt (the fused Q8 block index drops the tile's second block) turns all four shapes red: q8_off=48 y_eq=false on the 64 row shape, and the other three the same, with the gate ending gate_gemm: FAILED.
  • Refusal: the mutant that drops the act.k != rows check from the native launcher turns the new clause red: rows=64 act_k=128 refused_k=false FAIL, and the gate exits rc 1. The other four shapes stay green.
  • PTX shape: before the pin, the spill check reported generate_ds41 gate_up_nvfp4_mma unpinned spill=0 jit_local=0 and exited rc 1. The pinned row (0, 0) fixes it.

PTX

At the native arch (sm_121a, ptxas 13.3.73, JIT cc 12.1), generate_ds41:

  • gate_up_nvfp4_mma: 256 threads, 116 registers, 37,376 B of shared memory, 0 spill, 0 JIT local, two blocks per SM.
  • gemm_nvfp4_mma keeps main's digest, 98 registers and 0 spill. swiglu_nvfp4_32 and swiglu_nvfp4_32_sel use 72 registers and quantize_nvfp4_32 80, all with 0 spill.
  • BLOOMERY_ARCH=sm_121a PTX_SPILL_CACHE=off bash tools/ptx-spill-check.sh tools/ref/ptx-shapes.tsv tools/ref/ptx-shapes/ generate_ds41:mma: ptx-spill bin=generate_ds41 entries=18 pinned=18 with violations=0 PASS.
  • A ptx-compare against main lists six existing entries whose PTX digest moved and whose registers, spill and JIT local did not: gemm_nvfp4p_swiglu, quantize_gemm32, relu_sqr_quant32, swiglu_act_quant32, swiglu_quant32 and swiglu_quant32_sel. They inline quant32_group, whose per-lane core is now quant32_lane: the same arithmetic in a different instruction order. Only quantize_gemm32 is on the Qwen3.8 native prompt path: 11.28 ms on main and 11.32 ms on the measured head in the profiled prompt, 48 launches.

Speed

Kernel bench (no model; gate_gemm --bench-kernels, graph replay mean per layer). Shape: 4,096 tokens, top-10 (40,960 slots), 512 experts, 640 x 2560 NVFP4 gate and up. The Q8 output is checked equal between the two forms in every run.

  • Main's three launches: 10821.3 us. The fused launch: 8711.3 us. Change: -2110.0 us (-19.5%) per layer, so about 100 ms of a 4,096 token prompt over 48 layers.

End to end, FP4-Local and NVFP4 Qwen3.8, one server load per arm, bloomery-serve -m <model> --parallel 1 --ctx-size 16896 with BLOOMERY_DRAFT=off. Plan ubatch=4096, all 24,576 routed experts on the card. Requests: two 4,096-id code windows (offsets 0 and 4096), one request at a time, 128 greedy tokens. Both trees built at the default arch (sm_121a), main at 346bbd7. Arm order: round 0 head then main, round 1 main then head, round 2 head then main, then the same for the second checkpoint. The clock cap was lifted only while an arm's requests ran. Models: nvidia/Qwen3.8-Flash-Next-FP4-Local at b9ce18a6 and nvidia/Qwen3.8-Flash-Next-NVFP4 at fc694b54. Medians of three rounds:

Model, metric (one GB10, C@1, draft off) main head median change
FP4-Local prefill tok/s 1521.6 1572.0 +3.31%
FP4-Local TTFT p95 ms 2717.0 2642.7 -2.74%
FP4-Local decode tok/s 30.47 30.17 -0.98%
NVFP4 prefill tok/s 1582.7 1639.9 +3.61%
NVFP4 TTFT p95 ms 2611.1 2526.1 -3.25%
NVFP4 decode tok/s 22.55 22.61 +0.28%

Delta (head against main): prefill +3.31% on FP4-Local (1521.6 to 1572.0 tok/s) and +3.61% on NVFP4 (1582.7 to 1639.9 tok/s); decode is within the run-to-run spread.

The prefill gain is outside the noise for both models: every head prefill sample is above every main prefill sample. FP4-Local decode reads 0.98% lower on head, but the per-round ranges overlap, so I read it as noise. This change does not touch decode.

Kernel time of one profiled 4,096-id FP4-Local prompt (round 2 of that sitting):

Kernel main ms (launches) head ms (launches)
gemm_nvfp4_mma, gate and up 455.1 (96)
swiglu_nvfp4_32_sel 52.4 (48)
gate_up_nvfp4_mma 412.7 (48)
quantize_nvfp4_32 (all uses) 1.9 (48) 9.9 (96)
gemm_nvfp4_mma, down 287.4 (48) 283.7 (48)
all kernels 2671.0 2571.4

A second, shorter sitting ran this change against main at 608c090, FP4-Local only. Both binaries were warmed first; the arms ran head, main, main, head, each on the same two 4,096-id windows with 128 greedy tokens, BLOOMERY_DRAFT=off bloomery-serve -m <model> --host 127.0.0.1 --port <port> --parallel 4 --ctx-size 16896, both trees at the default sm_121a arch:

arm prefill tok/s TTFT p95 ms decode tok/s
round 0 head 1574.1 2635.6 30.55
round 0 main 1523.5 2733.2 30.67
round 1 main 1525.7 2718.7 30.47
round 1 head 1572.5 2635.0 30.61

Over the two pairs, head against main: prefill 1524.6 to 1573.3 tok/s (+3.2%), TTFT p95 2726.0 to 2635.3 ms (-90.7 ms, -3.3%), decode equal within noise. Greedy tokens are equal between head and main on both windows in both rounds. This agrees with the first sitting (+3.31% prefill, -2.74% TTFT).

GPU evidence

A DGX Spark (GB10, nvidia-smi of the gate run and of the short sitting):

| NVIDIA-SMI 595.84                 Driver Version: 595.84         CUDA Version: 13.3     |
|   0  NVIDIA GB10                    On  |   0000000F:01:00.0 Off |                  N/A |

Builds ran in a CUDA 13.3.1 container at the default arch, sm_121a: cargo oxide build --arch sm_121a -- --locked -p bloomery-gpu-gates --release --bin gate_gemm.

What I ran into

None of this blocks the PR.

  • The first timed sitting ran on the commit before the kernel's argument cleanup. That cleanup removed two unused arguments from gate_up_nvfp4_mma and changed no arithmetic, so that kernel's PTX digest moved (08cd504b) while its registers, shared memory and spill read the same, and every other entry's resource columns are equal. I did not repeat the three-round sitting; the short sitting above ran on this head and matches it. The full ptx-compare of this head against the timed commit did not run: the node's repository had no object for the commit it compares with, so the step stopped. Every gate and the spill check had already passed.
  • Clippy at sm_121a (CUDA_OXIDE_TARGET=sm_121a ./tools/mac-check.sh lint) reports seven warnings, the same seven as main: the swiglu and product host launchers in nvfp4mma.rs, each counted for the lib and the lib test, and gpu-gates/src/bin/shared/q38_combine.rs:111. None is in code this PR adds.
  • The change is measured on one DGX Spark only. No RTX 5090 run and no TP=2 run was done, and the non-native and non-NVFP4 paths keep main's launches without a timing of their own.
  • The fused kernel covers gate, up and SwiGLU, not the down GEMM. It still writes Q8 and packs the down's FP4 with quantize_nvfp4_32, which is a second launch per layer after the fused kernel. Packing the FP4 inside the epilogue needs each column's global scale, which depends on every row tile of the column, so it would need a cross-block reduction or a per-tile scale format. The top-k sum is main's.
  • The profiled kernel table is one window per side, so its kernel times are a single observation.

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 24, 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 findings remain in the changed code.

Non-blocking

⚠ Complete the PTX move proof: crates/gpu/src/gemm/act32.rs:620. The PR reports changed digests for six existing entries after extracting quant32_lane. Attach just canon-check <base> output showing that these entries remain the same program, as required by AGENTS.md's move/split class.

⚠ Identify the tested commits: crates/gpu/src/gemm/nvfp4mma.rs:730. The PR identifies the short sitting as this head, but omits the revisions used for the pre-cleanup timing and pre-rebase gate runs. Add those tested commit hashes to complete CONTRIBUTING.md's evidence list.

Checked

Duplicates: 12 new functions, types and constants, plus one macro checked. No duplicate owner found. Dryer returned zero GPU pairs; GPU-gates was searched manually.
Complexity: nvfp4_gate_up_case (70) and nvfp4_gate_up_arm (31) are new fixture/execution/comparison pipelines; those stages provide the split. Benchmark run (16→18) and g32 run (18→19) add dispatch/case arms, harmless growth.
Silent failure: five sites followed through their consumers: unknown benchmark arm, padded route read, inactive quantizer values, zero scale and non-finite epilogue values. Invalid inputs reach a named error or fault.
Tests: dropping the second Q8 block, replay launch or down-K guard reaches failing assertions; FAIL-first results are reported in the PR. Static enumeration of route lengths 1–64 confirms one writer and one quantizer read per live tile value.
Gates: unsafe ratchet increase and lint exemptions have dated PIN reasons; the new spill row has reported FAIL-first evidence. No numeric band was relaxed.
Escape catalog: C1–C4 do not match the diff. Boundary tiles, refused routes, non-finite values and reset replay were traced against their guards and consumers.

Notes

Dryer did not scan GPU-gates. GPU gates and PTX tools were not run during this source review.

Card check: needs-gpu. This pull request changes crates/gpu-gates/src/bin/gate_gemm.rs, crates/gpu/src/arch/qwen3moe/wide38.rs, crates/gpu/src/gemm/act32.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 0c46340 into midagedev:main Oct 24, 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