Skip to content

gpu: walk the native NVFP4 route tile, the HC norm and the prefill flash's query staging with fewer loads - #847

Merged
juliankang4 merged 6 commits into
midagedev:mainfrom
juliankang4:dgx-spark-q38-bundle-b1-prompt2
Oct 26, 2026
Merged

juliankang4 merged 6 commits into
midagedev:mainfrom
juliankang4:dgx-spark-q38-bundle-b1-prompt2

Conversation

@juliankang4

@juliankang4 juliankang4 commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

Qwen3.8 FP4-Local의 prefill에서 native NVFP4 expert tile, HC norm, prefill flash의 query 준비가 읽는 양을 줄여 DGX Spark의 prefill 속도를 높이는 속도 PR입니다.

What and why

This PR removes redundant loads and checks from three parts of the Qwen3.8 prefill on a DGX Spark: the native NVFP4 route kernels, the HC norm and the prefill flash. The results stay the same in the checks below.

  • Native NVFP4 route tile (crates/gpu/src/gemm/nvfp4mma.rs). A whole tile, and a short tile of an even K, are walked as whole ones without the half and fragment checks for fragments that are never read. The tile's slots and input scales are kept in shared memory (TileColumns) ahead of the K walk, so the epilogues of gate_up_nvfp4_mma and gemm_nvfp4_mma read them without a global load.
  • HC norm (norm_stream in crates/gpu/src/hc_gated.rs). For a stream of one chunk per thread, the values and gains stay in registers across the norm's sum instead of being read back after it. The sum order is unchanged.
  • Prefill flash (crates/gpu/src/flash_gqa_prefill.rs, rope_neox.rs, arch/qwen3moe/wide38.rs). The selecting layers used to run the query head norm and NEOX turn into the arena's query rows, which the flash then read back to stage. The two gated entries (enqueue_256_p4_gated, enqueue_256_p12_sel_gated) now take the [q | gate] rows and a QueryNorm (gain, table, positions, eps) and norm and turn each query in the staging, so the walk's append runs over the key heads alone. The scale is worked by one warp in the norm block's order through latent::warp_sum_f64, which is now pub(crate).

Greedy output is identical on every request in both comparison parts below.

Release notes: Changed: Qwen3.8 FP4-Local prefill is faster on a DGX Spark, with identical greedy output.

Code shape

  • No second copy. Both route kernels share one TileColumns. The query norm calls the existing latent::warp_sum_f64, now pub(crate), and the gated entries' length check calls short, which moved from q38.rs to crates/gpu/src/lib.rs so that common code can call it. cand.rs now calls it too, in place of its own private copy.
  • Model code holds only architecture facts. QueryNorm and NormedRows are named for the norm they run, not for a model, and sit in the common flash_gqa_prefill.rs; wide38.rs hands its gain, table and positions through QueryNorm.
  • Dead code is gone. The ubatch walk's arena no longer allocates query rows, but wide38.rs still passes its (empty) q.q to the append, which writes no query head into it.
  • No silent failure. A [q | gate] buffer, gain, table or position list one value short is refused by name in both gated entries, and a position past the cache gives a NaN row.
  • Comments state what is true now.
  • No relaxed gate. No band or tolerance moved. tools/unsafe-ratchet.txt rises for new code (gpu 2655 to 2691 blocks, 336 to 346 fns) with three dated PIN lines, each new block with its SAFETY line. norm_stream gains one allow(clippy::needless_range_loop) with its reason and a dated PIN line.
  • cargo fmt and cargo clippy are clean on what I touched; the lint count did not go up. just lint passed with 0 warnings and 0 errors on 981493fb; I did not record a separate cargo fmt run.

Duplication found or removed (path:line), or "none": the f64 warp butterfly (16, 8, 4, 2, 1) that latent::warp_sum_f64 holds is also written out at crates/gpu/src/ple.rs:483, crates/gpu/src/linear/conv.rs:172, crates/gpu/src/linear/norm_gate.rs:117, crates/gpu/src/qk_rms.rs:112, crates/gpu/src/qsa.rs:175 and crates/gpu/src/rope_neox.rs:292, :514, :1025, :1496. The same inline length check as short is also written out at several other sites in flash_gqa_prefill.rs. I left them as they are.

What I ran

The commands ran on a DGX Spark. The two flash and HC gates ran on 981493fb5b5f70eab85db0ea5f7138098c6169a0. The head differs from it by rebases onto upstream main (one over a change to the prefill-256 staging in flash_gqa_prefill.rs, the last over changes to the MTP width chooser and a PTX shape table, none in files this PR touches), renames of two types and one error string, short moving to lib.rs and cand.rs calling it instead of its private copy, the ubatch walk's arena allocating no query rows, the flash test building its gated arguments in one helper (gated_launch), and one comment line before the allow on norm_stream. On an sm_86 build of 1062258a, which has the renames but not the changes after them, just ptx-compare reports all 452 entries identical to 981493fb. The changes after that are host or test code, and I did not rerun a GPU gate, a GPU build or the timings on this head, including after the last rebase. The gate_gemm cases ran on an earlier revision of this branch whose crates/gpu/src/gemm/nvfp4mma.rs is identical to this head's.

just gate-gpu-hc-gated
PASSED: gate_hc_gated
HC rc=0
bash tools/gate.sh --oxide -p bloomery-gpu --release --lib -- flash_gqa_prefill::tests rope_neox:: arch::qwen3moe::wide38:: --include-ignored
test flash_gqa_prefill::tests::hw_gated_entries_are_the_query_norm_flash_and_out_gate ... ok
test result: ok. 9 passed; 0 failed; 0 ignored; 0 measured; 417 filtered out; finished in 18.14s

The test above holds both gated entries bit for bit to the query norm, the plain flash and the out gate. Each new length check was made to fail first by removing it, and the test passes again after the file was restored:

  • Removing the pos length check fails the test: MUTANT holds-pos rc=101
  • Removing the query buffer check of the listed entry fails the test: MUTANT listed-holds rc=101

The native NVFP4 cases:

BLOOMERY_GATE_CARD=any bash tools/gpu-gate.sh gate_gemm --case nvfp4_native
BLOOMERY_GATE_CARD=any bash tools/gpu-gate.sh gate_gemm --case nvfp4_gate_up
BLOOMERY_GATE_CARD=any bash tools/gpu-gate.sh gate_gemm --case nvfp4_swiglu
  • nvfp4_native: nvfp4_native PASS lines: 43, including the new row (128, 129, 256).
  • nvfp4_gate_up: nvfp4_gate_up PASS lines: 9.
  • nvfp4_swiglu: nvfp4_swiglu PASS lines: 21.

The slot and input scale columns were made to fail first by reading or writing one column over in nvfp4mma.rs; the file was restored afterwards:

  • Writing each slot one column over fails nvfp4_native: MUTANT put-slot nvfp4_native rc=1 FAIL lines: 22
  • Reading the epilogue's slot one column over fails nvfp4_gate_up: MUTANT gate-up-slot nvfp4_gate_up rc=1 FAIL lines: 6
  • Reading the epilogue's input scale one column over fails nvfp4_gate_up: MUTANT gate-up-scale nvfp4_gate_up rc=1 FAIL lines: 6

just lint (clippy over the workspace, all targets) on 981493fb: LINT rc=0 warnings=0 errors=0

I did not run just gate-ptx-spill or the other recipes just affected selects for crates/gpu on this head; a maintainer runs those on the workstation.

Speed

The timed runs and the lint run below are on 981493fb5b5f70eab85db0ea5f7138098c6169a0; the gates and timings were not rerun on this head. The timed model is nvidia/Qwen3.8-Flash-Next-FP4-Local at revision b9ce18a6887e18c089fdea8509a33e63429a3300, with the Q8_0 MTP draft for the draft-on arms (unsloth/Qwen3.8-Flash-Next-GGUF at revision 766911a6b7369840a91dbcd95f9f997acaab6cd6). Each run uses a 4096-id prompt and 128 greedy tokens, on eight corpus windows 50,000 ids apart, with three rounds after a lead-in. Base is upstream main ba8877d75fc1aacbcc5427d7bc99d0f8ec438d37, built from its own tree, and head is 981493fb5b5f70eab85db0ea5f7138098c6169a0, so the timed base is not this PR's base. Base and head run alternately. The draft-off arm and the draft-on arms ran as two parts of one comparison. Each cell is the median of three rounds. Greedy output is identical on every request.

Change is head against base; higher is better for tok/s and lower is better for TTFT. Ranges are "separate" when the three head rounds and the three main rounds do not overlap.

Arm Metric main head Change Ranges
FP4-Local, no draft, code-fn Decode tok/s 30.3379 30.5343 +0.647% separate
FP4-Local, no draft, code-fn Prefill tok/s 2155.9 2187.06 +1.445% separate
FP4-Local, no draft, code-fn TTFT p95 ms 1906.73 1879.25 -1.441% separate
FP4-Local, MTP draft, code-fn Decode tok/s 55.2258 55.7467 +0.943% separate
FP4-Local, MTP draft, code-fn Prefill tok/s 2071.13 2079.74 +0.416% overlap
FP4-Local, MTP draft, code-fn TTFT p95 ms 1983.35 1975.55 -0.393% overlap
FP4-Local, MTP draft, prose-en Decode tok/s 39.8212 39.9505 +0.325% overlap
FP4-Local, MTP draft, prose-en Prefill tok/s 2114.14 2155.4 +1.951% separate
FP4-Local, MTP draft, prose-en TTFT p95 ms 1943.83 1906.73 -1.909% separate

without a draft, prefill +1.445% and TTFT p95 -1.441% are resolved above 0.500% and decode +0.647% above 0.524%; with the MTP draft only prose-en prefill (+1.951%) and TTFT p95 (-1.909%) are resolved, and the other MTP rows are inside the 3.040%, 1.116%, 1.168% and 5.928% their runs can resolve

The draft-on rows other than prose-en prefill and TTFT are inside the spread of repeated runs of the same tree, so they are not evidence of an effect or of none.

Reproduction

git -C base checkout ba8877d75fc1aacbcc5427d7bc99d0f8ec438d37   # main
(cd base && cargo oxide build --arch "$(bash tools/arch.sh)" -- -p bloomery-gpu-gates --features glm5next,clef,vision,mimo2,solar2,minimaxm3,gemma-embedding2,hf,checkpoint --release --bin bloomery-serve)
env BLOOMERY_DRAFT=off BLOOMERY_HOST_ROOM=96G ./base/target/release/bloomery-serve -m "<folder>" --host 127.0.0.1 --port 18731 --parallel 1 --ctx-size 16896

Build the head the same way. Send 4096-id prompts from eight windows of the same corpus with 128 greedy tokens (temperature 0), alternating the two trees, and compare the median server prefill rate. For the draft-on arms set BLOOMERY_DRAFT=mtp, BLOOMERY_MTP_DRAFT to the MTP draft file and BLOOMERY_MTP_WIDTH=cost.

Hardware

DGX Spark machines for the timed runs and the gates. This section lists only the card and driver lines saved with the runs.

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

CPU and RAM were not recorded for these runs.

AI help

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

…staging

The ubatch walk's selecting layers ran head_norm_neox_append_256 over every
query and key head: each query head's RMS norm and NEOX turn written to the
arena's query rows (at 4,096 rows 100.7 MB), which the prefill flash read
back to stage them as f16. The flash's staging can make those values from
the [q | gate] rows it already reads for the gate.

The prefill bodies take their query rows through QueryRows: Stored reads
them as before; Normed38 takes each staged row's scale from one warp
(rope_neox::query_scale_256, which works the norm block's four warp sums in
the block's order) into the exchange, then each value as the norm writes it
(query_value_256: (scale * gain) * x, the first 64 turned through
neox_pair), a position at or past the cache's height NaN. The gated entries
read [q | gate] rows and a Query38 (gain, table, positions, eps) and run
the query norm, the flash and the out gate in one launch; the walk runs the
norm launch over the key heads alone, so the ubatch arena holds no query
rows.

hw_gated_entries_are_the_query_norm_flash_and_out_gate holds both gated
entries bit for bit to the query norm, the plain entry and the out gate,
over raw queries, gates in [-8, 8), a NaN gate's fault and a position past
the cache; it also checks that both refuse each input one value short by name.
…ed memory ahead of the K walk, so its epilogue reads them without a global load
@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 labels Oct 26, 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 failure path survived; the bit-for-bit gates cover the new query staging and the NVFP4 full-tile walk, and the open points below are non-blocking.

Non-blocking

⚠ The plan still counts query rows the ubatch arena no longer holds: place.rs:1165 UBATCH_TOKEN_BYTES (644,345) includes the q rows that scratch38.rs:346 now allocates as parts * ATTN (zero in a ubatch walk). That is 24,576 B per position, 100,663,296 B at U = 4096 [derived: 4096 · 6144 · 4], counted and never used. The check at body38.rs:1890 only refuses a larger arena, so nothing fails. The earlier flash-row change re-pinned this constant the same way. Re-pin it here or say in the PR body that the re-pin follows.

⚠ The HC norm's two paths are gated on one side only: hc_gated.rs:708 one = hid <= NORM_CHUNK * NORM_THREADS is 4096, and the gate and the unit test run hidden 2560 and 64 only. Hidden 4352 takes the !one path, which no gate runs. Mutant: one true for hidden > 4096. The loop then runs twice, xv keeps only the last chunk, and no assertion turns red. Add one hidden-4352 case, or state in the PR body that the path is not gated.

⚠ hc_gated.rs:680: PIN(2026-10-27) is dated after the other PINs of this PR (2026-10-26). Use the date of the commit.

Also 1 minor: the three unsafe-ratchet.txt PINs each say 2655 -> 2691; give each its own delta.

Checked

  • Duplicates: 14 new items checked (short moved to one owner, warp_sum_f64 reused, QueryRows, NormedRows, QueryNorm, TileColumns, query_scale_256, query_value_256); none duplicated beyond what the PR body lists. The 3 dryer pairs in flash_gqa_prefill.rs are kernel entry wrappers that differ by PACK; no finding.
  • Complexity: prefill_256_p 44→50 and prefill_256_sel 28→34 are one compile-time Q::RAW branch (read for correctness only); norm_stream 20→24 is the one branch (see ⚠); qsa 26→27 is even growth.
  • Silent failure: 6 sites followed. A position past ctx gives a NaN row, and the key launch still raises CachePos. A short qg, gain, table or position list is refused by name.
  • Position indexing: after narrow, pos[r] is the position of qg row t0 + r. Matches wide38.rs:2612.
  • Tests: the new hw test compares both entries bit for bit with the old chain. Mutants checked (dropped RoPE turn, pos[0] for pos[t], changed warp sum order, missing NaN for p >= ctx) each turn an assertion red. The new gemm row (128, 129, 256) reaches the full-tile path.
  • Gates: no band or tolerance moved. Escape catalog: no pattern matched.

Notes

  • The head was not run on a card (PR body). The arena change and n_head: 0 in wide38.rs have no GPU run at this head; n_head: 0 has a gate precedent in gate_qwen35moe_attn.rs:592.

Card check: gpu-evidence. This pull request changes crates/gpu-gates/src/bin/gate_gemm.rs, crates/gpu/src/arch/qwen3moe/scratch38.rs, crates/gpu/src/arch/qwen3moe/wide38.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 26, 2026
@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 15778e2 into midagedev:main Oct 26, 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 gpu-evidence Verified on the author's card: the pull request carries the gate run and its output

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant