Skip to content

gpu: the fused NVFP4 gate and up writes its SwiGLU in native FP4, and qwen38 counts a folder's NVFP4 rows in that form - #1040

Merged
juliankang4 merged 3 commits into
midagedev:mainfrom
juliankang4:dgx-spark-q38-opt-swiglu-native-act
Oct 31, 2026
Merged

juliankang4 merged 3 commits into
midagedev:mainfrom
juliankang4:dgx-spark-q38-opt-swiglu-native-act

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

Qwen3.8 NVFP4 routed MoE에서 fused gate와 up이 SwiGLU 결과를 FP4로 바로 써서 Q8 plane과 quantize launch를 없애고, 카드 메모리와 prefill 시간을 줄입니다.

What and why

In the Qwen3.8 NVFP4 routed MoE, the fused gate_up_nvfp4_mma writes its SwiGLU result as Q8 planes, and a separate quantize_nvfp4_32 launch per layer turns them into the FP4 input of the down projection. The Q8 planes are 800 bytes per run-token column of scratch on the card.

This PR has two code changes:

  • gate_up_nvfp4_mma writes the SwiGLU result straight into the native FP4 form the down projection reads. The block that arrives last at a route tile settles the column scale and each step's scale byte, so the Q8 planes and the quantize_nvfp4_32 launch are gone. The plan's per-run-token bound for the native route drops from 115,732 to 108,172 bytes.
  • PlanInputs::route_form now reads each routed stack's card format. A checkpoint folder declares its NVFP4 routed stacks as NVFP4_ROWS, which the card reads as NVFP4 planes, but the plan counted that route in the rows form (170,360 bytes per run-token), more than the route uses. The plan now counts it in the native form.

The gpu row of tools/unsafe-ratchet.txt and its PIN lines are recounted for the new blocks.

The FP4 bits differ from the previous path, so a greedy continuation can differ from main's. The gate_qwen4exp_nvfp4 oracle argmax and band checks pass on the NVFP4 folder (below).

Release notes: Changed: the fused NVFP4 gate and up writes its SwiGLU in native FP4, so FP4-Local prefill of a 4096-token prompt is 1.2% faster, the card holds 30,965,760 bytes less, and the plan of an NVFP4 checkpoint folder counts 254,722,048 bytes less.

Code shape

The review reads these first. A box left unticked needs one line of why.

  • No second copy. The FP4 rule has one device owner (fp4_sub, scale_byte, column_scale) and one host transcription (gate_up_fp4) that the gate compares bit for bit. GemmAct32::new_gate_up_output adds only the frames to new_native_input, and the route form has one owner, route_form.
  • Model code holds only architecture facts. CardRoute38::new picks the fused output scratch when every card gate and down is NVFP4. The common owners gained no per-model branch, flag or trait method.
  • Dead code is gone. The Q8 store of the fused epilogue, the route argument of quantize_nvfp4_32 and the routed quantize part of hw_native_activation_planes are removed.
  • No silent failure. A fused launch on a scratch without frames, or on a scratch of another K, is refused by name, and a refused column leaves a NaN column scale.
  • Comments state what is true now. tools/check-comments.sh is ok.
  • No relaxed gate. No band or lint moved to make a check pass. The one gate contract change, comparing the down output with the host down over the fused FP4 instead of with the split path's output, carries PIN(2026-10-31) lines with their reason.
  • cargo fmt and cargo clippy are clean on what I touched; the lint count did not go up.

Duplication found or removed (path:line), or "none": the duplicate scan lists one candidate, different. crates/gpu/src/gemm/nvfp4mma.rs:304 and crates/gpu/src/gemm/nvfp4mma.rs:338 are load_gpu, one relaxed GPU-scope load, and arrive, one acq_rel GPU-scope atomic add. Each wraps its own single PTX instruction and they share only the generic-to-global address conversion.

What I ran

Card conditions of every card run below, from nvidia-smi:

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

Speed

One DGX Spark, the default sm_121a build, bloomery-serve with the draft off. Each request is a 4096-token prompt with 128 greedy tokens. Three rounds per side in alternating order after a warm-up, medians below.

FP4-Local (nvidia/Qwen3.8-Flash-Next-FP4-Local), base is upstream main at 904266cfc:

Metric Base Head Difference Ranges overlap
FP4-Local code prefill (tok/s) 2845.34 2879.52 +1.201% no
FP4-Local code TTFT p95 (ms) 1444.54 1427.9 -1.152% no
FP4-Local code decode (tok/s) 31.4703 31.4305 -0.127% yes

Prefill and TTFT improve with separate ranges and decode ranges overlap, so I claim a gain only for prefill and TTFT.

NVFP4 (nvidia/Qwen3.8-Flash-Next-NVFP4), same base and conditions:

Metric Base Head Difference Ranges overlap
NVFP4 code prefill (tok/s) 2530.71 2564.62 +1.340% no
NVFP4 code TTFT p95 (ms) 1623.34 1602.61 -1.277% no
NVFP4 code decode (tok/s) 22.7434 22.8189 +0.332% yes

The NVFP4 prefill and TTFT differences are smaller than the spread of the same build against itself, so I claim no NVFP4 speed gain.

Card bytes from the server log of the same runs. need bytes is the plan's card bound and resident_bytes is what the load holds. The NVFP4 folder row is from a second A/B with the route-form commit, base 6a9680ae:

Metric (bytes) Base Head Difference
FP4-Local resident_bytes 77570279692 77539313932 -30965760
FP4-Local need bytes 81915558992 81884593232 -30965760
NVFP4 folder resident_bytes 80394940684 80363974924 -30965760
NVFP4 folder need bytes 84490222672 84235500624 -254722048

Both differences match the derived values: 4096 run-tokens times 7,560 bytes (10 columns times the 756 bytes the scratch loses per column) is 30,965,760, and 4096 times (170,360 minus 108,172) is 254,722,048.

Reproduce one round, with base on upstream main and head on this branch:

(cd base && cargo oxide build --arch "$(bash tools/arch.sh)" -- -p bloomery-gpu-gates --features glm5next,clef,vision,mimo2,solar2,minimaxm3,flint,gemma-embedding2,hf,checkpoint --release --bin bloomery-serve)
env BLOOMERY_DRAFT=off BLOOMERY_HOST_ROOM=96G ./base/target/release/bloomery-serve -m "${MODEL_FP4L}" --host 127.0.0.1 --port 18731 --parallel 1 --ctx-size 16896 & server_pid=$!
until curl -fsS http://127.0.0.1:18731/health; do sleep 1; done
kill "$server_pid"; wait "$server_pid"

Checks

Host tests of the plan, including the new NVFP4_ROWS case of a_card_route_is_counted_in_its_files_form:

cargo test -p bloomery-model --release --lib -- arch::qwen35moe
test result: ok. 112 passed; 0 failed; 0 ignored; 0 measured; 493 filtered out; finished in 0.07s
cargo test -p bloomery-model --release --test qwen4exp_mtp_reserve
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

With route_form back on t.ty == GgmlType::NVFP4, that test fails:

test arch::qwen35moe::place::tests::slots::a_card_route_is_counted_in_its_files_form ... FAILED
  left: Rows
 right: Native

The fused gate and up gate on the card. Every slot's FP4 is compared bit for bit with the host transcription, and the down output with the host down over that FP4. Spread and Tiny cross the edges of the FP4 range, and Edge carries a column whose SwiGLU is f32::MAX:

gate_gemm --case nvfp4_gate_up rc=0
gemm32 case=nvfp4_gate_up rows=192 k=256 slots=74 varied=false values=Spread scratch_saved_bytes=130240 fp4_off=0 edge_ok=true y_kind_eq=true y_off=0 fault=None PASS
gemm32 case=nvfp4_gate_up rows=192 k=256 slots=74 varied=false values=Tiny scratch_saved_bytes=130240 fp4_off=0 edge_ok=true y_kind_eq=true y_off=0 fault=None PASS
gemm32 case=nvfp4_gate_up rows=64 k=64 slots=18 varied=false values=Edge scratch_saved_bytes=10512 fp4_off=0 edge_ok=true y_kind_eq=true y_off=0 fault=None PASS
gemm32 case=nvfp4_gate_up rows=640 k=2560 slots=262 varied=false values=Plain scratch_saved_bytes=1539512 fp4_off=0 edge_ok=true y_kind_eq=true y_off=0 fault=None PASS
gpu-gate.sh: gate_gemm ran 19 s (exit 0)

With the kernel's sub-block scale back on the old f32 formula, the same gate fails on Edge:

gemm32 case=nvfp4_gate_up rows=64 k=64 slots=18 varied=false values=Edge scratch_saved_bytes=10512 fp4_off=16 edge_ok=false y_kind_eq=true y_off=0 fault=None FAIL
gpu-gate.sh: gate_gemm ran 0 s (exit 1)

The PTX spill ratchet:

just gate-ptx-spill
gate-ptx-spill rc=0
ptx-spill bin=gate_qwen3vl_tower entries=26 pinned=26 nonzero=none violations=0 PASS

The NVFP4 checkpoint folder against the oracle, with the plan split between the card and the host (the route form commit):

BLOOMERY_CARD_ALL_EXPERTS=1 BLOOMERY_REF_MODEL=<NVFP4 folder> just gate-gpu-qwen4exp-nvfp4
(s) the split load: the plan splits true, the oracle's band held true; last argmax 11751 the all-card run's 11751, logits_rel to it 1.184e-1 (printed) 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 100 s (exit 0)

The A/B above ran the real bloomery-serve of this change on the card.

Hardware (if the change is for a machine)

DGX Spark (GB10), driver 595.84, CUDA 13.3, build sm_121a. The change is the sm_121a kernel gate_up_nvfp4_mma.

AI help

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

…the down's input holds no Q8 planes

On a native FP4 build a Qwen3.8 load whose card gate, up and down are all
NVFP4 runs the gate, the up and the SwiGLU as one launch,
gate_up_nvfp4_mma. It stored the SwiGLU's 32-value Q8 planes, and a second
launch, quantize_nvfp4_32 over the route, packed the down's native FP4 from
them: 800 B of Q8 a column that only that launch read, and one launch a
layer.

gate_up_nvfp4_mma now packs the FP4 itself. Each block keeps its tile's Q8
codes and block scales in shared memory and packs its own 64-value step:
every 16-value sub-block takes the scale amax / 6 rounded to three mantissa
bits and each code the nearest E2M1 level of code times d / s. A column's
scale is a power of two, so a step packs alone; its scale bytes go out in
the step's own frame (its largest exponent at UE4M3 field 14), and the
tile's last block to arrive (one arrival word a tile, back to 0 after it)
writes the column scale 2^(top - 7) and moves each step's bytes into the
column's frame. The FP4 is the transcription of the split pair's Q8 codes
(gemm32::gate_up_fp4) and differs from quantize_nvfp4_32's, whose column
scale is the largest block scale times 127/6.

GemmAct32::new_gate_up_output makes that scratch: the native FP4 form
alone, with a frame a step and an arrival word a column, 408 B a column at
K = 640 where the routed scratch held 1,164 B. enqueue_gate_up_sel32 takes
the fused launch only into such a scratch and no longer quantizes after
it; quantize_nvfp4_32 loses its route parameters. CardRoute38 makes the
scratch when every card gate and down is NVFP4, and the plan's native run
token drops from 115,732 B to 108,172 B (127,361,024 B fewer than the rows
form at a run of 2,048 tokens).

gate_gemm's nvfp4_gate_up case holds each card slot's FP4 to the host
transcription bit for bit (levels, scale bytes, column scale) and the down
over it to the host product within 2^-16 of its magnitude.
…e native form

PlanInputs::route_form gave the native form only where every routed stack
of the file's layers is GGUF's NVFP4. A Qwen3.8 NVFP4 checkpoint folder
now declares its routed stacks as NVFP4 rows, which the card holds as
NVFP4's planes and runs through the same fused native launch, so the load
held its card route in the native form while the plan counted the rows
form: 62,188 B a run token held back that no buffer took (254,722,048 B at
U = 4,096 on a GB10).

route_form now reads the card format of each routed stack (card_routed):
native where the card reads every one in NVFP4's planes, a GGUF's NVFP4 or
a folder's NVFP4 rows.
@github-actions github-actions Bot added gpu The card engine: crates/gpu*, crates/app model The CPU engine and model files: crates/model, models, hf, gguf, tokenizer, jinja needs-gpu Changes code a card must check, and carries no gate run on a card labels Oct 31, 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 reachable behavior defect or proven quality defect remains.

Non-blocking

⚠ Complete the launch-fold evidence: crates/gpu/src/gemm/nvfp4mma.rs:1999.
At 640 gate/up rows, the fused epilogue adds 10 GPU-scope atomic arrivals per route tile per layer [derived: 640/64]. The “fold a launch into a neighbour” class requires an owning gate and timed A/B.
The PR supplies gate output and A/B summaries, but its timing reproduction only starts and stops the server. Add the tested head hash and the timed request commands with their output so the prefill result can be reproduced.

Checked

Duplicates: 22 new items and the moved nearest checked; no duplicate owner found. dryer reported 0 pairs. Manual searches covered the GPU and gate code.
Complexity: nvfp4_gate_up_case 72→103 adds fixture, oracle and edge-check branches; CardRoute38::new 28→32 adds load-time format selection; gate_up_nvfp4_mma 16→30 adds packing and publication branches. The kernel is exempt from splitting.
Silent failure: 3 default/fallback sites followed; the fixed-size maximum and absent optional scratch/forms have defined meanings. Non-finite SwiGLU raises QuantColumn and propagates a NaN column scale to the down projection and the host fault reader.
Tests: restoring the old predicate at crates/model/src/arch/qwen35moe/place.rs:559 fails the NVFP4_ROWS assertion. Removing the overflow-safe branch at crates/gpu/src/gemm/nvfp4mma.rs:209 fails Edge (fp4_off=16 in the supplied mutation log). Removing the Q8-scratch refusal makes the native-plane test's expect_err fail by inspection. Restoring the old scratch byte bound fails the updated planning assertions.
Gates: the changed numeric comparison, retired routed-quantizer case and unsafe ratchet have dated PIN reasons. No unpinned relaxation found.
Adversarial: checked refused/all-host routes, partial tiles, NaN and extreme finite columns, and stale frames on replay. Route guards, complete active-step writes and acquire/release arrivals block those failure paths. Frame rescaling also matched direct column framing in an independent arithmetic check.
Escape catalog: C1–C4 did not match.

Notes

Static review only; hardware gates were not rerun. dryer excluded crates/gpu and crates/gpu-gates.
The supplied card runs omit the tested head hash, so 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/gemm32.rs, crates/gpu/src/arch/qwen3moe/wide38.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 f083a62 into midagedev:main Oct 31, 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 model The CPU engine and model files: crates/model, models, hf, gguf, tokenizer, jinja needs-gpu Changes code a card must check, and carries no gate run on a card

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant