Skip to content

gpu: sum the F32 tile two lanes a thread and run the routers' prompt logits on it - #763

Merged
juliankang4 merged 4 commits into
midagedev:mainfrom
juliankang4:dgx-spark-q38-prompt-router
Oct 24, 2026
Merged

juliankang4 merged 4 commits into
midagedev:mainfrom
juliankang4:dgx-spark-q38-prompt-router

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

Qwen3.8의 4K prompt에서 router logits kernel이 thread 하나에 합 두 개를 들게 해서 load당 FMA 수를 늘려 빨라지게 하고, router마다 따로 있던 tile 본문 복사본을 하나로 합치는 작업입니다.

Part of #565.

What and why

In a Qwen3.8 prompt, qwen35moe_router_logits_512_bf16 multiplies the 513 router rows (K 2560) with the activations of each ubatch, 96 launches for a 4,096-id prompt. The kernel and the other F32 tile entries ran one body, and the router crates held their own copies of it. The F32 tile is bound by the register file: each output keeps 32 lane sums, so the tile cannot grow. What can change is how many FMAs run per shared load.

This PR changes how the F32 tile body (f32_tile_body in crates/gpu/src/gemm/f32tile.rs) assigns lane sums to threads. The summation order does not change (the row walk's lane sums and the xor 16, 8, 4, 2, 1 butterfly), only which thread holds which lane sum.

  • Before: one warp owns an 8 row by 8 column tile and lane L holds lane sum L of each (row, column) pair. A 32-value chunk costs 16 four-byte shared loads and 64 FMAs a thread. The block has 16 warps (512 threads).
  • Now: thread p holds lane sums 2p and 2p+1 for the 8 by 8 pairs (128 accumulators). 16 threads cover a chunk, and the two halves of a warp take different 8 columns. A chunk costs 16 eight-byte loads and 128 FMAs. The butterfly steps lane xor 16, 8, 4, 2 become thread xor 8, 4, 2, 1, and lane xor 1 becomes the addition of a thread's two sums. The block has 8 warps (256 threads). The tile stays 32 rows by 32 columns with three cp.async stages and 48 KiB of static shared memory.
  • A row group with no live row stages and computes nothing (the last row block of a 513-row input stages 8 rows).
  • qwen3moe_router_logits, qwen35moe_router_logits, qwen35moe_router_logits_512 and qwen35moe_router_logits_512_bf16 each carried a copy of the tile body (logits_body, an inline copy in the qwen3moe entry, store_rows, store_rows_live). They now call f32_tile_body and share tile_launch for the grid and the common shape checks. f32_tile_chunk, fma_rows and TILE in q8f32.rs had no remaining user and are deleted.
  • The other entries on the same body (f32_tile_gemm, mxfp8_tile_gemm, fp8_b128_tile_gemm, bf16_tile_gemm) take the new layout too. Their launch bounds and contract are 256 threads.
  • The MMA replay (MmaTile::replay, the exact recomputation of rows and columns that have an out-of-range operand, used by dense MMA and HC MMA) takes its row count from a per-weight constant, MmaRows::REPLAY_ROWS, next to the existing COPIED and GROUP constants. The default is 1; HcUpRows in crates/gpu/src/hc_gated.rs sets 2. replay runs TILE_ROWS / (ROW_GROUPS * W::REPLAY_ROWS) passes, and an inline const assert refuses any other count. Two rows a pass made bf16_mma_gemm spill 264 bytes on an sm_86 build, where main has none, so the dense weights replay one row a pass (126 registers, no spill). One row everywhere made hc_gated_up_mma_mix_4 spill more on sm_121 (732 bytes against main's 492), so that weight keeps two (see FAIL-first). The replay is a rare path, so the pass count does not affect speed.
  • qwen35moe_router_norm_m was already BELOW its pinned cc 12.1 PTX rows on main (spill 0 against a pin of 32, JIT local 0 against 8). This PR touches the same tables, so it lowers that row to the reading too.

The change is smaller than what it replaces: crates/ goes from 727 deleted lines against 287 added, and the unsafe ratchet falls from 2282 to 2265 blocks and from 285 to 281 functions.

Release notes: Changed: Qwen3.8 prompt router logits run about 31% faster per launch in a kernel profile; end-to-end prefill speed is inside the run-to-run spread.

Code shape

  • No second copy. Four router tile-body copies and the q8f32.rs helpers are gone; every F32 tile entry calls f32_tile_body, and tile_launch is the one place for the launch checks.
  • Model code holds only architecture facts. The common owner gained no per-model branch, flag or trait method.
  • Dead code is gone. f32_tile_chunk, fma_rows and TILE are removed.
  • No silent failure. tile_launch refuses a bad token count, input length, k or input alignment with a named shape error.
  • Comments state what is true now.
  • No relaxed gate. No band, tolerance or lint moved. The PTX shape tables and the ratchet change carry PIN(2026-10-24) lines with the reading they follow.
  • 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 router tile-body copies in crates/gpu/src/arch/qwen3moe/router.rs and the helpers in crates/gpu/src/q8f32.rs are removed. The duplicate scan reports no candidate on the head.

What I ran

Output is unchanged

Prompt logits are bit-identical to the decode F32 gemv. On an RTX 5090 (just gate-gpu-qwen35moe-moe --case shape,wide512, rc 0):

  • wide512 ubatch n=70 ... vs host: 70 tokens with logits_bits=true, ids_exact=true and weights_bits=true.
  • wide512 order ubatch n=70 vs the fused launch per token: 70/70 bit-identical PASS. The order fused m=1, 5, 8 lines match the F32 gemv bit for bit, on inputs where 3485 of 4104 logits differ from an f64 rounding.
  • shape kernel=qwen35moe_router_fused_512_bf16 local_depot=false ld_local=0 st_local=0 PASS, and the same for the route kernel.

The F32 tile against the decode F32 gemv, just gate-gpu-gemm --case g32_f32tile: rc 0 on an RTX 5090 and rc 0 on a GB10, with bits_differ_vs_f32_gemv=0 and past_end_untouched=true on every shape (rows 96, 100, 128 and 512, K 2560, n from 1 to 4095). --case g32_refuse and --case dense_mma are rc 0 on the RTX 5090.

On the final code, on an RTX 5090 (BLOOMERY_BOX_LOCAL=1 just gate-gpu-qwen35moe-moe --case shape,wide512, rc 0): the router kernels read local_depot=false, wide512 ubatch n=70 vs the fused launch per token: 70/70 bit-identical PASS and wide512 bf16 ubatch n=70 vs the f32 weight widened: 70/70 bit-identical PASS.

On one GB10, just gate-gpu-hc-gated is rc 0 on the replay code of this change (bf16_mma_gemm and hc_gated_down_mma_4 read local_depot=false). On the head that goes up, also on one GB10: just gate-gpu-gemm --case dense_mma rc 0 with 72 PASS and 0 FAIL, including the replay cells for BF16, MXFP8 and FP8_B128 (a 130 by 130 output with an out-of-range operand at row 64 or column 129: others_kept_k=true odd_lines_exact=true PASS); just gate-gpu-bf16 rc 0 with 309 PASS; just gate-gpu-mxfp8 rc 0 with 343 PASS.

The g32_f32tile and g32_refuse runs above are from an earlier form of this change. The F32 tile body and the routers' path are the same on the final code; only the MMA replay row count differs.

FAIL-first

  • Output: a mutant that swaps the butterfly's xor 8 and xor 4 steps makes wide512 order ubatch n=70 vs the fused launch per token fail with 0/70 bit-identical, the gate print FAIL, and the run exit 1. The check catches a wrong summation order in the new tile.
  • Spill: with two replay rows a pass over main's split copied-stage loop, bf16_mma_gemm spills 264 bytes on an sm_86 build (ptx-compare against main: spill:0->264). On the same code, one replay row reads spill 0 and 126 registers, four rows reads 164 registers and one block per SM, and a for loop in place of the while loops leaves 264 unchanged, so the loop form is not the cause. One row for every weight made hc_gated_up_mma_mix_4 worse on sm_121 and sm_121a (spill 732 against main's 492), hence the per-weight count. With the final code bf16_mma_gemm and every other gemm entry match their pins on sm_86, sm_121 and sm_121a.

PTX shape

hc_gated_up_mma_mix_4 reads lower than its pins, and the tables carry the new readings: sm_86 at cc 12.1 1352/176 to 484/88 (spill/JIT local), sm_121 and sm_121a at cc 12.1 492/176 to 364/88. In the default tools/ref/ptx-shapes.tsv the spill goes from 1352 to 484; that reading comes from an sm_86 ptxas run on a GB10, and the cc 8.6 JIT and the stack frame were not measured.

just ptx-compare upstream/main gate_gemm (sm_86 build, JIT cc 12.1) lists 332 entries on each side, none added or removed, 13 changed, all on this PR's paths: the F32 tile entries and the routers' prompt logits (reqntid 512 to 256), and the MMA entries whose replay runs the tile body, each at or below main:

  • bf16_mma_gemm registers 128 to 126, spill 0 to 0.
  • fp8_b128_mma_gemm registers 128 to 122.
  • mxfp8_mma_gemm registers 128 to 124.
  • hc_gated_down_mma_4 registers 128 to 124.
  • hc_gated_up_mma_mix_4 spill 1352 to 484, JIT local 176 to 88.

just ptx-compare upstream/main gate_hc_gated lists the same 13. just gate-ptx-spill has one violation on each of sm_86, sm_121 and sm_121a: gqa_flash_fold_mma_256_p12_sel above its pin. It reads the same on main, and this PR does not touch it. The check-ptx-names recipe and just lint (3 warnings, the same count as main) are rc 0 on this code.

Speed

Kernel bench (a synthetic input of the real shape: 513 rows, K 2560, 2048 tokens), one GB10 with the clock cap in place, so only same-run comparisons count:

  • The previous layout runs in 0.866 ms in two passes.
  • The layout in this PR runs in 0.706 and 0.673 ms.
  • Copying alone takes 0.504 and 0.503 ms and FMAs alone 0.510 ms.
  • All variants give 0 differing results against the previous layout on 2048 tokens (0/1050624) and on 70 tokens (0/35910).

End to end on one DGX Spark: FP4-Local Qwen3.8, 4096-id code windows (offsets 0, 4096, 8192), 128 greedy tokens, one request at a time, draft off. Each checkpoint started with a discarded warm load, a host settle and a read probe preceded each arm, and the greedy tokens of head and main are equal per window. Main was 2a7005b8a. Each arm served with BLOOMERY_DRAFT=off bloomery-serve -m <model> --host 127.0.0.1 --port <port> --parallel 4 --ctx-size 16896.

Metric (one GB10, C@1, draft off) main head change
FP4-Local prefill tok/s, median of three 4096-id prompts 1308.4 1303.3 -0.4%
Router logits kernel, median ms a launch (profiler, one 4096-id NVFP4 prompt) 1.022 0.708 -31%

Delta (head against main): FP4-Local prefill -0.4% (1308.4 to 1303.3 tok/s), inside the run-to-run spread; the router logits kernel takes a median of 0.708 ms a launch in place of 1.022 ms (-31%) in a profiler capture of one 4096-id prompt.

The per-window prefill speeds were 1297.2, 1321.7 and 1303.3 tok/s on head against 1298.3, 1314.6 and 1308.4 tok/s on main. I claim no end-to-end prefill gain: the expected saving of 0.8 to 1.0% of the prompt time is inside that spread. Decode on the same arms read 30.20 tok/s on head and 30.56 tok/s on main, a path this change does not touch.

The kernel row is a profiler capture of one extra request on NVFP4 loads (96 launches on each side, 93.4 ms against 69.1 ms in total). The main side of that NVFP4 pair ran in a degraded state: its read probe was the lowest of the sitting and its first request prefilled at 1157.0 tok/s, so I do not use that pair's end-to-end speeds. The kernel launch medians are less sensitive to that state, and the head's median (0.708 ms) is below the main side's fastest launch (0.812 ms).

Timings against the final code

The kernel bench and the end-to-end A/B ran on the first form of this change. The routers' prompt-logits path (f32_tile_body, the full-row tile) has not changed since. What changed afterwards is the MMA replay row count and the PTX tables, which no router timing reads. I did not rerun the timings, and I did not measure dense BF16 or HC MMA speed.

GPU evidence

An RTX 5090 (nvidia-smi of the gate runs):

| NVIDIA-SMI 595.99.02              Driver Version: 595.99.02      CUDA Version: 13.2     |
|   0  NVIDIA GeForce RTX 5090        Off |   00000000:01:00.0 Off |                  N/A |

A DGX Spark (GB10, nvidia-smi of the timed run):

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

Gate commands and output summaries from those runs:

  • RTX 5090: just gate-gpu-qwen35moe-moe --case shape,wide512 rc 0 (final code), just gate-gpu-gemm --case g32_f32tile,g32_refuse rc 0, just gate-gpu-bf16 rc 0, just gate-gpu-mxfp8 rc 0 (earlier form).
  • GB10: just gate-gpu-gemm --case dense_mma rc 0 (72 PASS), just gate-gpu-bf16 rc 0 (309 PASS) and just gate-gpu-mxfp8 rc 0 (343 PASS) on the head that goes up; just gate-gpu-hc-gated rc 0 on the replay code of this change; just gate-ptx-spill and just ptx-compare upstream/main gate_gemm as above, with the build's nvidia-smi above.

Models: Qwen3.8 FP4-Local and NVFP4.

What I ran into

None of this blocks the PR.

  • The end-to-end prefill gain is not shown. The router kernel is faster, but the saving is under 1% of a prompt and the A/B stays inside its spread. The NVFP4 end-to-end pair is not used because its main side was degraded.
  • just gate-gpu-qwen3moe-router did not run: the ref_qwen3moe reference set is not on the GB10 machine, and the RTX 5090 run is in host mode and cannot read it. The ubatch refusals it checks (no tokens, short input, k not 64, misaligned input) are now tile_launch's checks; the gated router's equivalent is covered by wide512 bf16 short activation row: fused and ubatch refused. The 128-row kernel's bit evidence is g32_f32tile at 128 rows.
  • A build of bloomery-gpu prints #[unroll] requested but the loop was not unrolled twice, from counted while loops. The PTX is the same with a for loop, so it is cosmetic.
  • The mutant check was not rerun on the final code; the tile body it targets is unchanged.

AI help

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

…logits on it

The F32 tile kept one lane sum of the row walk per thread, so each 32-value
chunk cost a thread sixteen 4-byte shared loads for 64 multiply-adds. A
thread now keeps the lane sums 2p and 2p + 1 of eight rows by eight
columns: sixteen threads cover a chunk, one 8-byte load per row and column
feeds 128 multiply-adds, and the butterfly runs xor 16, 8, 4, 2 of the
lanes as xor 8, 4, 2, 1 of the threads and xor 1 as the add of the pair.
Every lane sum and the tree are the row walk's, so each output keeps
f32_gemv's bits. A block is eight warps; a row group with no live row is
neither staged nor summed.

The qwen3moe and gated routers' prompt logits carried their own copy of the
tile body. They now call f32_tile_body over their rows and launch through
tile_launch, and q8f32's f32_tile_chunk goes with the copy.
On GB10 (ptxas 13.3.73, driver 595.84) gate-ptx-spill reads
qwen35moe_router_norm_m at 0 spill bytes and 0 driver-JIT local bytes,
under the 32 and 8 its sm_121 and sm_121a rows pin and the 8 its sm_86
row on cc 12.1 pins, so the gate fails BELOW on the base tree as well.
The rows now hold what the scans read.
@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 defect found in the tile layout, replay, or router launch checks.

Non-blocking

⚠ Measure the final MMA paths: crates/gpu/src/gemm/f32tile.rs:1013. Default replay grows from two passes to eight; HC up uses four. Each pass copies all 32 activation columns. For a full 32×32 replay subtile, the default adds (8-2)*32*4*K/32 = 24*K requested activation-copy bytes per token [derived], before weight reads. The occupancy/geometry class calls for a timed A/B on the contributor's card. Attach final-code dense BF16 and HC MMA comparisons, including exceptional operands.

⚠ Complete the run witness: crates/gpu/src/gemm/f32tile.rs:281. The PR names cards and run results but omits the tested head hash and exact model file. Attach both tested commit hashes, the model file, and command output under CONTRIBUTING's evidence list so the results identify the code and workload they verify.

Optimization headroom

  • E. Less work: crates/gpu/src/gemm/f32tile.rs:362. Use the existing column-group coordinates to skip wholly unused groups' computation and copies inside this body. At rows=513, K=2560, n=70, omitting 16 activation lines in the last column block across 17 row blocks saves 16*4*K*17/70 requested activation-copy bytes per token, about 39,790 [derived].

Checked

Duplicates: both dryer pairs are distinct CUDA entries that share the existing tile body.

Complexity: f32_tile_at rises 7→14 through paired reduction loops and tail guards. Its kernel bounds and reduction order were checked without proposing a split.

Silent failure: tail clamps supply padding whose outputs are not stored; marked replay preserves healthy cells, and non-finite router logits raise FaultSite::Router. Launch shape failures return named errors.

Tests: none added or changed. Existing wide512 order compares each token's logits and routing outputs; the PR reports its xor-8/xor-4 swap mutant fails.

Gates: PTX and unsafe pins tighten with dated reasons; no tolerance relaxation or gate removal.

Escape catalog: no C1–C4 pattern matched.

Notes

GPU gates were not run during validation. GPU results and the mutant outcome are author-reported. The run witness is incomplete; gpu.json records evidence: false.

Card check: needs-gpu. This pull request changes crates/gpu/src/arch/qwen3moe/router.rs, crates/gpu/src/gemm/f32tile.rs, crates/gpu/src/gemm/kernels32.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 608c090 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