Skip to content

gemm: loop the copied dense MMA stages apart and widen the BF16 raster - #754

Merged
juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-prompt-dense-mma
Oct 23, 2026
Merged

juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-prompt-dense-mma

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

Qwen3.8 prefill의 dense 행렬곱 커널에서 BF16 weight의 stage 반복을 따로 돌리고 raster group을 넓혀 큰 shape의 커널 시간을 줄이는 작업입니다.

Part of #565.

What and why

The dense prompt GEMMs of Qwen3.8 on a GB10 run through one MMA tile, bf16_mma_tile. The NVFP4 checkpoint's dense weights go through bf16_mma_gemm and FP4-Local's through mxfp8_mma_gemm, and hc_gated_up_mma_mix_4 uses the tile too. This PR changes how the tile runs its stages and lays out its blocks:

  • MmaRows gains two associated consts. COPIED (default true) is set for a weight whose fetch is an asynchronous copy, so a thread holds no piece in registers across the products. ByteRows (MXFP8 and FP8_B128) sets it to false. GROUP is the number of column tiles in one raster group: 8 by default, and ByteRows keeps its current 4.
  • One macro, stage_pass!, holds a stage's products. With COPIED, the stages that fetch (0..stages-2) run as one loop with the fetch always on, and the last two stages as a loop with it off, so no branch surrounds the fetch. A byte weight keeps one loop that picks with s + 2 < stages: it holds its next pieces across the products, and hoisting their loads in a branch-free loop would spill.
  • For a copied BF16 weight, the range check W::landed_fits moved from between the stage wait and the barrier to after the first step's products. No warp overwrites that stage buffer before the next stage's barrier, so the check reads the same data (the SAFETY text is updated).
  • The raster takes W::GROUP: eight column tiles for a BF16 weight (bf16_mma_gemm and HcUpRows), four for a byte weight. A wave of resident blocks reads the weight slabs of its rows and the F32 activations of its columns. A BF16 weight is half an activation's bytes a value, and eight tiles balance the two on GB10's 96 resident blocks.
  • gate_gemm --case dense_mma gains a BF16 row (33, 64, 1100). Its 9 column tiles make a second raster group at group 8. The only earlier BF16 row with two groups was n=513, which at group 8 is one group.

hc_gated.rs is not touched. HcUpRows is a copied BF16 weight, so it takes the new defaults; its kernel time in the profiled run stays within 1.3% of main (275.0 to 271.5 ms on FP4-Local, 271.3 to 274.2 ms on NVFP4).

Release notes: Changed: the dense prompt matrix multiply for BF16 weights on GB10 runs its copied stages as separate loops with a wider raster, which cuts its kernel time in the large Qwen3.8 shapes; MXFP8 and FP8_B128 weights keep their loop and group size.

Code shape

  • No second copy. The body of a stage's products lives once, in stage_pass!, and all three loops use it. The raster group size, a local in the tile before, now has one owner, the GROUP const.
  • Model code holds only architecture facts. No model file changed, and no per-model branch, flag or trait method was added to a common owner.
  • Dead code is gone. The local group_size is removed.
  • No silent failure. No new input path. The refusal of a BF16 weight off a 16-byte boundary and the operand range check keep their gate rows.
  • Comments state what is true now. No numbers or dates in the new comments.
  • No relaxed gate. No band, tolerance or lint moved. The unsafe counts in f32tile.rs are unchanged (49 unsafe { blocks, 20 unsafe fn) and tools/check-unsafe.sh is ok.
  • cargo fmt and cargo clippy are clean on what I touched; the lint count did not go up. cargo fmt and tools/check-comments.sh are clean. I could not run clippy for the GPU crate: the machine I tried has no CUDA toolkit, so bloomery-gpu does not build there.

Duplication found or removed (path:line), or "none": the duplicate scan flagged crates/gpu/src/gemm/f32tile.rs:487 against crates/gpu/src/flash_gqa.rs:189. Both are named GROUP, but one is the query heads per key head and the other is the column tiles of an MMA raster group. I left it.

What I ran

Output is unchanged

The kernel bench below compares the FNV digest of every output between main and head. They are equal on every shape.

just gate-gpu-gemm, just gate-gpu-bf16, just gate-gpu-mxfp8 and just gate-gpu-hc-gated not run as recipes: the node run built each gate binary with cargo oxide build and ran it through bash tools/gpu-gate.sh, the last step of each recipe. The measured commit ran them on a DGX Spark:

HEAD_GATE gate_gemm --case dense_mma rc=0 pass=72 fail=0
HEAD_GATE gate_bf16 rc=0 pass=309 fail=0
HEAD_GATE gate_mxfp8 rc=0 pass=343 fail=0
HEAD_GATE gate_hc_gated rc=0 pass=125 fail=0

Every dense_mma row reports eager_graph_bits=true, so a graph replay gives the eager bits. In the server A/B below, main and head give the same greedy tokens (128 tokens, three prompts) on both checkpoints, and the same two tokens in the profiled request.

FAIL-first

I changed the raster so it covers only its first group of column tiles (n.div_ceil(MMA_COLS).min(W::GROUP)), built that, and ran the same gate. gate_gemm --case dense_mma exits 1 and the only FAIL lines are the two of the dense_mma bf16 rows=33 K=64 n=1100 row, with worst_err_over_band 1.1e31 from the unwritten tile 8. Every other dense_mma row passes.

Speed

Kernel bench on a DGX Spark (GB10, clock cap left on): the production entries enqueue_bf16_mma and enqueue_e4m3_mma with synthetic in-range operands, 4096 columns, 3 warmup and 10 timed launches a shape, main and head alternated three times each, median of three. Shapes are Qwen3.8's dense products and their rank-split shapes. Main here is a commit with the same GPU code as the A/B's base.

format N rows × K main ms head ms change
BF16 1 10240 × 2560 5.170 4.335 -16.1%
BF16 1 6144 × 2560 3.089 2.605 -15.7%
BF16 1 12288 × 2560 6.229 5.270 -15.4%
BF16 1 2560 × 6144 3.074 2.709 -11.9%
BF16 1 2560 × 640 0.405 0.414 +2.1%
BF16 1 640 × 2560 0.381 0.366 -3.9%
BF16 2 5120 × 2560 2.672 2.266 -15.2%
BF16 2 2560 × 3072 1.506 1.330 -11.7%
BF16 4 3072 × 2560 1.499 1.252 -16.5%
BF16 4 2560 × 1536 0.653 0.672 +2.9%
MXFP8 1 10240 × 2560 5.578 5.516 -1.1%
MXFP8 1 2560 × 6144 3.619 3.504 -3.2%
MXFP8 2 5120 × 2560 2.827 2.805 -0.8%
MXFP8 4 2560 × 2560 1.370 1.351 -1.4%
FP8_B128 1 10240 × 2560 5.403 5.281 -2.3%
BF16, every tile replays 1 10240 × 2560 44.825 44.757 -0.2%

Weighted by how often each shape launches in a 4,096-token prompt (N = 1), one prompt's BF16 launches take 608.1 ms on main and 529.5 ms on head (-12.9%, -78.6 ms), and its MXFP8 launches 675.4 ms and 663.5 ms (-1.8%), which is inside the run-to-run spread. The small shapes (a K of 640 or less, or 512 rows or fewer) vary by a few percent either way. Driver JIT registers: bf16_mma_gemm 124 to 128, fp8_b128_mma_gemm 124 to 122, mxfp8_mma_gemm 126 unchanged, hc_gated_up_mma_mix_4 128 with 96 local bytes on both sides, the others 0 local bytes.

Server A/B on a DGX Spark: Qwen3.8 FP4-Local and NVFP4, C = 1, draft off, three 4096-id code windows with 128 greedy tokens, median of three requests. Each checkpoint started with a discarded warm load; FP4-Local ran head then main, NVFP4 main then head. The clock cap was lifted only around each arm's requests. One extra profiled request an arm collected the kernel times.

Metric (one GB10, C@1, draft off) main head change
NVFP4 prefill tok/s, median of three 4096-id prompts 1285.5 1312.7 +2.1%
NVFP4 bf16_mma_gemm GPU time, ms in one 4096-id prompt 672.5 595.1 -11.5%
FP4-Local prefill tok/s, median of three 4096-id prompts 1306.4 1265.3 -3.1%
FP4-Local GPU kernel time, ms in one 4096-id prompt 3131.7 3102.1 -0.9%
BF16 dense kernel bench, ms for one 4096-id prompt's launches 608.1 529.5 -12.9%

Delta (head against main): NVFP4 prefill +2.1% (1285.5 to 1312.7 tok/s) and the BF16 dense kernel -12.9% in the kernel bench (608.1 to 529.5 ms); FP4-Local prefill -3.1% (1306.4 to 1265.3 tok/s) with 29.6 ms less GPU kernel time, from one server run per arm, so a single observation.

On NVFP4, time to first token fell 65 ms (-2.0%), the 423 bf16_mma_gemm launches took 77.4 ms less, close to the kernel bench's 78.6 ms estimate, and all GPU kernels together took 70.0 ms less (-2.2%). On FP4-Local, the GPU kernel sum is 29.6 ms lower (mxfp8_mma_gemm 17.0 ms, the HC up kernel 3.5 ms), yet prefill reads 3.1% lower. The head arm's decode witness was also 2.1% below main's, and decode does not run these kernels (its one-row path is a SIMT kernel), so I read that arm as a different card state and make no claim from its prefill number. I have no second pair for it.

Measured head against the PR head

The measurements ran on commit e24bb8cce over base 2a7005b8a, with the kernel bench's main at 0429f1483, two docs and serve commits above that base with no GPU code change. The PR head is 285e3e7dc on current main 501db04fe. The rebased commit 9318baadf was a pure rebase: range-diff shows 1: e24bb8cce = 1: 9318baadf, and the six upstream commits between the bases (docs, serve, tools) touch none of the paths this commit changes. The PR head differs from 9318baadf only in where one SAFETY comment sits, moved onto the unsafe line it explains so the lint accepts it; no code changed. I did not rerun the gates or the timings on the PR head.

Hardware

DGX Spark (GB10), GPU, driver and CUDA lines from nvidia-smi of the gate job:

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

Models: Qwen3.8 FP4-Local and NVFP4. Build: cargo oxide build --arch sm_121a. The gates and the server A/B ran on one DGX Spark and the kernel bench on a second one of the same kind.

What I ran into

None of this blocks the PR.

  • The FP4-Local server A/B reads slower than main, 3.1% lower prefill, while its GPU kernel time is lower. The head arm's decode witness was off too, as described above, and I did not repeat the pair. Read the NVFP4 pair and the kernel bench as the evidence for the gain.
  • The MXFP8 kernels move by only a few percent in the kernel bench, inside the spread, so this PR makes no MXFP8 speed claim.
  • The ptxas spill pin checks at sm_121 and sm_86 and just ptx-compare were not run. The sm_121a driver JIT lines above show 0 local bytes for the dense entries and 96 for hc_gated_up_mma_mix_4, as before.
  • Clippy for the GPU crate was not run, for the reason given under Code shape.

AI help

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

The dense prompt MMA tile fetched stage s + 2 inside one loop over every
stage, with a branch around the fetches for the last two, and read the
copied BF16 weight's range check between the stage's wait and its
barrier. For a weight copied asynchronously (bf16_mma_gemm and the HC
up product) the stages that fetch now run as their own loop and the last
two as another, and the check reads the landed words after the stage's
products. A byte weight (MXFP8, FP8_B128) holds its pieces in registers
across the products and keeps the one loop: the straight loop let ptxas
hoist the next pieces' loads and spilled.

The tile's raster group is now the weight's: eight column tiles for a
BF16 weight, whose slab is half the bytes of the F32 activation tile it
meets, and the F32 tiles' four for a byte weight. The dense_mma gate
gains a BF16 row of 1,100 columns, a second raster group.
@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 23, 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: The stage split preserves checked bounds, shared-memory lifetimes and MMA accumulation order.

Non-blocking

⚠ Complete the GPU validation evidence: f32tile.rs:999 changes kernel scheduling, and f32tile.rs:850 changes raster geometry under AGENTS.md.
Attach the omitted gate-ptx-spill results for sm_86 and sm_121. Include the exact build/gate and timed A/B commands with output and model file paths; the supplied summaries leave CONTRIBUTING.md’s evidence list incomplete.

Checked

Duplicates: Two new associated constants and one macro checked; no duplicate owners. Dryer reported zero pairs; GPU gates were searched manually.
Complexity: Crapper flagged no functions. The stage products retain one owner in the macro.
Silent failure: Three range-check result sites followed through line flags to exact replay; exceptional operands still select replay.
Tests: The added (33, 64, 1100) row protects f32tile.rs:850. Limiting the raster to its first group leaves tile 8 unwritten; the band check fails. The PR reports this FAIL-first run.
Gates: No bands, tolerances, pins or cases were relaxed or removed.
Adversarial: Minimum K, buffer reuse, delayed range checks and partial raster groups checked.
Escape catalog: No C1–C4 patterns matched.

Notes

GPU gates were not rerun during validation. The FAIL-first GPU result above is reported in the PR body; validation here was static.

Card check: needs-gpu. This pull request changes crates/gpu-gates/src/bin/gate_gemm.rs, crates/gpu/src/gemm/f32tile.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 db397ea into midagedev:main Oct 23, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant