Repository navigation
gemm: pipeline the dense MMA tile on BF16 stages read by ldmatrix - #696
Conversation
There was a problem hiding this comment.
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)
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.
297f426 to
58a278b
Compare
There was a problem hiding this comment.
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)
|
The review is done and the contribution check passed, so I'm merging this. |
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-stagecp.asyncpipeline. Activations are converted to BF16 once per tile when they are staged, and fragments are read from shared memory withldmatrix.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 withcp.async.cgtwo 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.ldmatrix.x4phase 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.mma_fitsrange 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.MmaTilekeeps its store, vote and replay, and the replay skips 64x64 sub-tiles past the output.crates/gpu/src/hc_gated.rs:HcUpRowsimplementsMmaRowsthrough 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_mmaand the HC launch sharebf16_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 namedShapeerror before any launch.crates/gpu-gates/src/bin/gate_gemm.rsgains adense_mma bf16 weight_unalignedclause for it.tools/ref/ptx-shapes.tsv,tools/ref/ptx-shapes/sm_121-cc12.1.tsv,tools/ref/ptx-shapes/sm_86-cc12.1.tsvandtools/unsafe-ratchet.txtcarry 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
bf16_mma_launchis 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_originandcoordinates).mma_launchis no longer re-exported from the gemm module, because the BF16 launch moved tobf16_mma_launch, andBf16RowsandHcUpRowsuse theMmaRowsdefaults.Shapeerror, and the new gate clause shows it.PIN(2026-10-23)lines that give their reasons.cargo fmtis 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.tools/unsafe-ratchet.txt, gpu blocks and fnsldmatrixreads, staged BF16 stores, isolated line flags, theMmaRowsfetch, land and range check, and the HC mix's warp scratch. Each block has its SAFETY line.hc_gated_up_mma_mix_4insm_121-cc12.1.tsv, spill and driver-JIT local byteshc_gated_up_mma_mix_4insm_86-cc12.1.tsv, spill and driver-JIT local byteshc_gated_up_mma_mix_4inptx-shapes.tsv, ptxas spill and stack frame bytesThe 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_121aon GB10 in thegate_gemmbinary (ptxas 13.3.73), main then head:bf16_mma_gemmbf16_mma_gemm_columnsmxfp8_mma_gemmfp8_b128_mma_gemmhc_gated_up_mma_mix_4Correctness
just gate-gpu-gemm --case dense_mmaandjust gate-gpu-hc-gatedon GB10 at the measured head:Every format's tails, past-end columns, exceptional tiles, range bands and eager-versus-graph bits pass in
dense_mma, andgate_hc_gatedreports PASSED, including the BF16 up, down and inject overflow cases andwide[graph]eager = graph bits. The newweight_unalignedclause fails first on the defect: with the clause applied to a launcher that has no alignment check, it printsweight_unaligned refused=false FAILand the gate exits with rc 1.Kernel bench
A bench binary built from each side's tree runs
bf16_mma_gemm,bf16_mma_gemm_columnsandmxfp8_mma_gemmat 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.Prefill A/B on one DGX Spark
Qwen3.8-Flash-Next in two checkpoints, NVFP4 (
nvidia/Qwen3.8-Flash-Next-NVFP4, revisionfc694b54) and FP4-Local (nvidia/Qwen3.8-Flash-Next-FP4-Local, revisionb9ce18a6). The server runsbloomery-serve --parallel 1 --ctx-size 4224 --cache-ram 0with 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.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
15aff1b67against a base of0bfdc7550, the tree of the BF16 prompt prefill change this PR builds on. The PR head0d577eafdis the same patch rebased onto upstream093f76516:git range-diffshows the commit unchanged (=). The merged files of the prefill change are identical to0bfdc7550in 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.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 othercheck-*.shscripts, 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 newptx-shapes.tsvrow, because I had no cc 8.6 card.AI help
AI 코딩 에이전트가 코드를 작성한 후 설명을 썼고, 테스트를 마쳤습니다. diff 리뷰와 PR 초안 리뷰는 제가 직접 했습니다.