Skip to content

gemm: pipeline the dense MMA tile on BF16 stages read by ldmatrix - #696

Merged
juliankang4 merged 2 commits into
midagedev:mainfrom
juliankang4:dgx-spark-q38-bf16-mma-pipeline
Oct 23, 2026
Merged

juliankang4 merged 2 commits into
midagedev:mainfrom
juliankang4:dgx-spark-q38-bf16-mma-pipeline

Conversation

@juliankang4

@juliankang4 juliankang4 commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

Qwen3.8의 dense BF16 프롬프트 tensor-core GEMM을 128x128 tile, 3단 cp.async pipeline, ldmatrix fragment 읽기로 바꿔서 DGX Spark 한 대의 4K prefill을 빠르게 하는 작업입니다.

Part of #565.

What and why

The dense prompt MMA tile (bf16_mma_body) is shared by the BF16, MXFP8 and FP8_B128 prompt products and by the fused hyper-connection (HC) up projection. This PR makes it a 128x128 tile with a three-stage cp.async pipeline. Activations are converted to BF16 once per tile when they are staged, and fragments are read from shared memory with ldmatrix.

On one DGX Spark (GB10), the large dense shapes run at 42 to 45 TFLOP/s where main ran at about 16, a 2.6x to 2.9x kernel speedup. A 4096-token prefill of Qwen3.8-Flash-Next gains +31.45% on NVFP4 and +35.64% on FP4-Local [derived], and the 32 greedy ids are equal on main and head for both checkpoints.

  • crates/gpu/src/gemm/f32tile.rs: the tile is 128x128 with eight warps of 64x32, 32 K values a stage and three stages in the existing 48 KiB allocation. MmaRows (fetch, land, landed_fits) stages each operand: a BF16 weight is copied with cp.async.cg two stages ahead and range-checked after the wait, and an E4M3 weight is loaded two stages ahead, then decoded and stored after the current stage's products. F32 activations are stored as BF16 once per tile (cvt.rn.bf16x2.f32) and never re-converted per fragment.
  • Shared-memory lines are slot-permuted so each ldmatrix.x4 phase is free of bank conflicts. The kernel reads both column fragments first and then one row fragment at a time, and it fetches and lands each stage's two pieces around one MMA step. That keeps every dense MMA entry within 128 registers with no spill.
  • Exceptional operands: the mma_fits range check moves from every fragment to the staging of every value. A refused tile replays through the exact owner as before. The isolating entries (bf16_mma_gemm_columns, HC) flag the refused lines in the stage and mark the cells whose row or column staged one. MmaTile keeps its store, vote and replay, and the replay skips 64x64 sub-tiles past the output.
  • crates/gpu/src/hc_gated.rs: HcUpRows implements MmaRows through its stored-row permutation (stored_at). The fused mix passes the gates through warp scratch in shared memory, half the warp's rows at a time. The order of arithmetic for each output is unchanged.
  • crates/gpu/src/q8f32.rs: enqueue_bf16_mma and the HC launch share bf16_mma_launch, which owns the BF16 weight's word-count and 16-byte alignment checks. The 16-byte copies need that alignment, so a misaligned weight is a named Shape error before any launch. crates/gpu-gates/src/bin/gate_gemm.rs gains a dense_mma bf16 weight_unaligned clause for it.
  • tools/ref/ptx-shapes.tsv, tools/ref/ptx-shapes/sm_121-cc12.1.tsv, tools/ref/ptx-shapes/sm_86-cc12.1.tsv and tools/unsafe-ratchet.txt carry the re-pins listed under "Pins that moved".

Release notes: Changed: Qwen3.8's dense BF16, MXFP8 and FP8_B128 prompt products run on a pipelined 128x128 tensor-core tile, which makes 4K prefill about a third faster on a DGX Spark.

Code shape

  • No second copy. bf16_mma_launch is the one place that checks a BF16 MMA weight, and the HC launch calls it instead of keeping its own copy. The HC row permutation has one owner (stored_at), and the fragment layout has one owner (MmaTile::warp_origin and coordinates).
  • Model code holds only architecture facts. No model file changes, and no common owner gained a per-model branch or flag.
  • Dead code is gone. mma_launch is no longer re-exported from the gemm module, because the BF16 launch moved to bf16_mma_launch, and Bf16Rows and HcUpRows use the MmaRows defaults.
  • No silent failure. A BF16 weight with the wrong word count or without 16-byte alignment is a named Shape error, and the new gate clause shows it.
  • Comments state what is true now.
  • No relaxed gate. No band or tolerance moved. The PTX spill pins and the unsafe ratchet moved with PIN(2026-10-23) lines that give their reasons.
  • cargo fmt is clean on the touched files. I did not run clippy on this head, because the machine I edit on cannot build the device crates.

Duplication found or removed: none.

What I ran

Pins that moved

The head moves five pins, each with a PIN(2026-10-23) line in its file.

Pin Before After Reason
tools/unsafe-ratchet.txt, gpu blocks and fns 2231 and 268 2248 and 277 New unsafe blocks for the asynchronous BF16 copies, ldmatrix reads, staged BF16 stores, isolated line flags, the MmaRows fetch, land and range check, and the HC mix's warp scratch. Each block has its SAFETY line.
hc_gated_up_mma_mix_4 in sm_121-cc12.1.tsv, spill and driver-JIT local bytes 84 and 32 260 and 96 The 128x128 tile gives each thread 64 cells, and the fused mix holds half of them while it mixes the other half.
hc_gated_up_mma_mix_4 in sm_86-cc12.1.tsv, spill and driver-JIT local bytes 24 and 32 920 and 96 Same cause, measured on the sm_86 PTX.
hc_gated_up_mma_mix_4 in ptx-shapes.tsv, ptxas spill and stack frame bytes 24 and 16 920 and 136 Same cause. The cc 8.6 driver JIT was not measured, because no cc 8.6 card was available, so this row uses the measured ptxas stack frame.

The fused HC entry spills more than it did, and I kept it because the 4K prefill A/B below includes its cost and still gains. It runs only on GB10. The four dense MMA entries (bf16_mma_gemm, bf16_mma_gemm_columns, mxfp8_mma_gemm, fp8_b128_mma_gemm) stay at 0 spill and 0 driver-JIT local bytes, their existing pins.

Register counts and spill at sm_121a on GB10 in the gate_gemm binary (ptxas 13.3.73), main then head:

Entry Registers Spill bytes Driver-JIT local bytes
bf16_mma_gemm 112 to 126 0 to 0 0 to 0
bf16_mma_gemm_columns 117 to 127 0 to 0 0 to 0
mxfp8_mma_gemm 116 to 124 0 to 0 0 to 0
fp8_b128_mma_gemm 116 to 125 0 to 0 0 to 0
hc_gated_up_mma_mix_4 128 to 128 104 to 260 48 to 96

Correctness

just gate-gpu-gemm --case dense_mma and just gate-gpu-hc-gated on GB10 at the measured head:

dense_mma bf16 weight_unaligned refused=true PASS
gpu-gate.sh: gate_gemm ran 12 s (exit 0)
gpu-gate.sh: gate_hc_gated ran 1 s (exit 0)

Every format's tails, past-end columns, exceptional tiles, range bands and eager-versus-graph bits pass in dense_mma, and gate_hc_gated reports PASSED, including the BF16 up, down and inject overflow cases and wide[graph] eager = graph bits. The new weight_unaligned clause fails first on the defect: with the clause applied to a launcher that has no alignment check, it prints weight_unaligned refused=false FAIL and the gate exits with rc 1.

dense_mma bf16 weight_unaligned refused=false FAIL
gpu-gate.sh: gate_gemm ran 1 s (exit 1)

Kernel bench

A bench binary built from each side's tree runs bf16_mma_gemm, bf16_mma_gemm_columns and mxfp8_mma_gemm at M=4096 with unit operands. It does three warm-up launches and ten timed launches by CUDA events, in the order main, head, main, head. Every output equals K. The table gives the median of the two rounds, in ms with TFLOP/s in parentheses.

Format N x K Main Head Speedup
bf16 320 x 10240 1.973 (13.61) 0.953 (28.16) 2.07x
bf16, isolated columns 320 x 10240 2.189 (12.26) 0.952 (28.20) 2.30x
bf16 10240 x 320 2.142 (12.53) 1.160 (23.13) 1.85x
bf16 10240 x 2560 13.018 (16.50) 4.983 (43.10) 2.61x
bf16 4096 x 4096 8.532 (16.11) 3.173 (43.31) 2.69x
bf16 2560 x 4096 5.483 (15.69) 1.929 (44.53) 2.84x
mxfp8 10240 x 2560 13.509 (15.90) 4.695 (45.74) 2.88x
mxfp8 4096 x 4096 8.746 (15.71) 3.251 (42.28) 2.69x

Prefill A/B on one DGX Spark

Qwen3.8-Flash-Next in two checkpoints, NVFP4 (nvidia/Qwen3.8-Flash-Next-NVFP4, revision fc694b54) and FP4-Local (nvidia/Qwen3.8-Flash-Next-FP4-Local, revision b9ce18a6). The server runs bloomery-serve --parallel 1 --ctx-size 4224 --cache-ram 0 with the draft off. Each checkpoint loads main once and then head once back to back, and each load takes two uncached 4096-token requests (offset 20480 of the same code corpus, eight generated tokens, temperature 0, top-k 1) and one 32-id greedy request. The GPU clock cap was lifted for the run and restored after it. These are paired observations in a fixed order, not a significance test.

Checkpoint and observation Main prefill tok/s Head prefill tok/s Change
nvfp4, request 1, prefill tok/s 862.121 1130.665 +31.15%
nvfp4, request 2, prefill tok/s 897.375 1182.148 +31.73%
fp4local, request 1, prefill tok/s 828.388 1148.188 +38.61%
fp4local, request 2, prefill tok/s 882.963 1173.096 +32.86%

nvfp4 mean prefill changed by +31.45%; fp4local mean prefill changed by +35.64% [derived]. Time to first token fell from 4758.5 and 4570.2 ms to 3629.8 and 3471.6 ms on NVFP4, and from 4950.5 and 4645.5 ms to 3573.3 and 3497.6 ms on FP4-Local. The 32 greedy ids are equal on main and head for both checkpoints. That compares those ids only, not logits.

Tested head and base

The A/B and the gates ran at 15aff1b67 against a base of 0bfdc7550, the tree of the BF16 prompt prefill change this PR builds on. The PR head 0d577eafd is the same patch rebased onto upstream 093f76516: git range-diff shows the commit unchanged (=). The merged files of the prefill change are identical to 0bfdc7550 in every path this PR touches. The four upstream commits since then change the MTP draft store, nightly tools, the Qwen3 system prompt and the MTP draft default, and none of them touches a kernel this PR changes. The A/B ran with the draft off. I carried the measurement to this head and did not rerun it.

Hardware

One DGX Spark (GB10, compute capability 12.1, sm_121a) in a CUDA 13.3.1 container with ptxas 13.3.73. The PTX pin scans ran on a second DGX Spark with the same driver.

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

The card was quiet for every run. The CPU load stayed under 1 for 30 seconds before timing started, and no other GPU process ran.

What I ran into

None of this blocks the PR. The fused HC entry still spills 260 bytes at sm_121, which the pin above records. I did not run clippy or the other check-*.sh scripts, because the machine I edit on cannot build the device crates, so those are left to the upstream checks. The cc 8.6 driver JIT was not measured for the new ptx-shapes.tsv row, because I had no cc 8.6 card.

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 23, 2026
github-actions[bot]
github-actions Bot previously approved these changes 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 remaining performance findings are advisory.

Non-blocking

⚠ Check short prompt geometry: crates/gpu/src/gemm/f32tile.rs:908.
For rows divisible by 128 and 1 ≤ n ≤ 64, each 32-value K stage issues 256 warp MMA instructions per 128 output rows, versus 128 before: twice as many per token [derived]. The supplied kernel timings cover n=4096.
The occupancy/geometry class requires the owning gate and timed A/B. Add GB10 A/B timings at n=9 and n=64 for the shared BF16, MXFP8 and FP8_B128 path.

Optimization headroom

  • E. Less work: crates/gpu/src/hc_gated.rs:1742. Guard padded cells before computing and storing their gates; the reads at line 1748 exclude them. At n=32, each full 128-row tile could avoid 128 × 96 × 4 / 32 = 1,536 bytes of shared stores per prompt token [derived]. Keep the warp barriers converged.

Checked

Duplicates: dryer reports zero qualifying pairs in crates/gpu; its scan excludes gpu-gates.
Complexity: diagnostics report dense_mma_case rose 44 → 48; the diff adds the alignment refusal clause.
Silent failure: padding clamps select existing operand rows or columns; output stores check live coordinates.
Tests: static mutation analysis confirms that removing the alignment check defeats the refusal clause. Changing the layout's row stride from 16 to 8 duplicates 6,144 cells, failing its uniqueness assertion. GPU gates were not rerun.
Gates: changed spill/stack and unsafe pins have dated reasons.
Escape catalog: no pattern applies to the retained findings.

Notes

The body reports GB10 gates at 15aff1b67 against 0bfdc75 and names 0d577eafd as the rebased head; this run reviews 297f426. Confirm equivalence through this head. The timing evidence also needs the exact A/B commands and their output under CONTRIBUTING.md.

Card check: gpu-evidence. This pull request changes crates/gpu-gates/src/bin/gate_gemm.rs, crates/gpu/src/gemm/f32tile.rs, crates/gpu/src/gemm/mod.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 23, 2026
The dense prompt MMA tile covers 128 rows by 128 columns with eight warps of
64 by 32. Both operands are staged once per block as BF16 lines, three
stages deep: a BF16 weight by 16-byte asynchronous copies two stages ahead,
an E4M3 weight and the F32 activations loaded two stages ahead and rounded
after the current stage's products. Warps read their fragments with
ldmatrix from slot-permuted lines. The operand range check moves from every
fragment to the staging of every value. A tile with a refused operand still
replays through the exact owner, and the isolating entries mark the cells
whose row or column staged one. A BF16 weight must be 16-byte aligned.

The fused HC mix passes the larger fragment's gates through warp scratch in
shared memory, half the warp's rows at a time, instead of warp shuffles.

@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 remaining findings are advisory.

Non-blocking

⚠ Complete the geometry-change evidence: crates/gpu/src/gemm/f32tile.rs:417. The body gives timings but omits runnable kernel-bench and prefill-request commands. Its rebase equivalence also stops at 0d577eafd, before this head. Add the commands and output, and show patch equivalence through 58a278bf5, so the owning-gate and timed A/B evidence is reproducible.

Optimization headroom

  • G, Per-launch and per-sync fixed cost: crates/gpu/src/hc_gated.rs:1765. Keep the barrier between scratch passes, but omit it after the final pass; the kernel returns without reusing scratch. This removes 8 warp syncs per 128×128 output tile [derived: one per warp].

Checked

Duplicates: dryer reported zero pairs in crates/gpu; crates/gpu-gates was outside its search scope.
Complexity: diagnostics report dense_mma_case at 44→48; the added clause checks alignment refusal.
Silent failure: BF16 copies use landed_fits; byte-format landing checks decoded values. Operand refusals feed exceptional-tile replay.
Tests: the body reports FAIL-first output when the alignment check is removed; the new clause includes refusal in its result. Changing mi * 16 to mi * 8 violates layout uniqueness. Device gates were not executed during validation.
Gates: no numeric band moved. Spill and unsafe-ratchet changes have dated PIN reasons.
Escape catalog: C1–C4 do not match.

Card check: gpu-evidence. This pull request changes crates/gpu-gates/src/bin/gate_gemm.rs, crates/gpu/src/gemm/f32tile.rs, crates/gpu/src/gemm/mod.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)

@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 fd3b77b 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 gpu-evidence Verified on the author's card: the pull request carries the gate run and its output 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