Skip to content

gemm: BF16 tensor cores for GB10 dense prompt products - #668

Merged
juliankang4 merged 4 commits into
midagedev:mainfrom
juliankang4:dgx-spark-e4m3-bf16-prompt-gemm
Oct 22, 2026
Merged

juliankang4 merged 4 commits into
midagedev:mainfrom
juliankang4:dgx-spark-e4m3-bf16-prompt-gemm

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

GB10에서 BF16, MXFP8 E4M3, FP8_B128 가중치의 prompt 곱을 BF16 tensor core를 쓰는 본체 하나로 계산해 4K prefill을 높입니다.

What and why

On GB10, dense prompt products for BF16, MXFP8 E4M3 and FP8_B128 weights ran on exact F32 FMA tiles. This PR sends those three formats through one BF16 tensor-core body with F32 accumulation, using 64 by 64 output tiles and a permutation of the shared-memory rows that spreads fragment loads across banks. Stored weights are not rewritten, and other devices keep their current dispatch. A block-wide vote finds every tile that saw a non-finite or out-of-range operand (a magnitude above 2^47, or a subnormal) or a partial sum outside the F32 range. Only those tiles are replayed with the existing exact F32 tile reduction, so their output is the same as on the exact path. A product still takes one launch and no new global scratch buffer. I also tried keeping the partial sums in F64 instead of replaying; it needed 154, 148 and 148 registers and one block per SM, and it still failed the case where the exact path's lane-ordered F32 sum overflows to NaN, so it is not used.

On one GB10, a 4096-token prompt prefilled 98.2% faster on the NVFP4 checkpoint and 54.0% faster on the FP4-Local checkpoint (table below).

Part of #565.

Release notes: Changed: on GB10, prompts on BF16, MXFP8 E4M3 and FP8_B128 weights prefill on tensor cores, which roughly doubles 4K prefill speed on the NVFP4 checkpoint measured here.

Code shape

  • No second copy. The BF16, MXFP8 and FP8_B128 wrappers all call bf16_mma_body. The duplication scan below found only typed ABI wrappers and launchers.
  • Model code holds only architecture facts. No model code changes, and the common owners gained no per-model branch.
  • Dead code is gone. The exact F32 tile body is one function that the exact entries and the MMA replay both call, and other devices still dispatch to those entries.
  • No silent failure. gate_bf16::k96_case checks the K=96 refusal.
  • Comments state what is true now.
  • No relaxed gate. No existing band or tolerance moved. The new L2 statistic in crates/gpu-gates/src/rounding.rs is diagnostic only.
  • cargo fmt and cargo clippy are clean on what I touched.

Duplication found or removed (path:line), or "none": none duplicated. The scan hits are crates/gpu/src/gemm/kernels32.rs:1201, crates/gpu/src/gemm/kernels32.rs:1243 and crates/gpu/src/q8f32.rs:1724, which are typed wrappers that call bf16_mma_body, and crates/gpu/src/gemm/f32tile.rs:673, where mma_launch reuses tile_launch (f32tile.rs:643) and checks the same spans before it adjusts the grid. kernels32.rs:1155 and kernels32.rs:1106 are the exact F32 entries that other devices need.

What I ran

Head d9f46a5dc, base main 2a2d2d2fa. The gates below ran on 39ccccb24; the head only regroups a private helper's arguments and the gate fixture's constant packing, with no arithmetic change. The 4K prefill table ran the first two commits on top of each checkpoint's baseline build; a short A/B of the exact replay follows it.

GPU Evidence

On an NVIDIA GB10, with BLOOMERY_BOX_LOCAL=1:

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

The new tensor-core entries against their oracle band and the Q8_0 representation, with eager and graph replay compared bit for bit. The boundary cases cover a cancelling pair inside a fragment, across fragments, across stages, across a reused pipeline slot, at the last lane of a warp, at the first output of the next tile, and a pair whose lane-ordered F32 sum overflows to NaN on the exact path. Each runs for all three formats and is compared with the exact F32 tile output:

just gate-gpu-gemm --case dense_mma
dense_mma bf16 rows=33 K=2560 n=33 probe=0,0 edge=ordinary edge_bits=None columns=33 q8_representable=true worst_err_over_band=4.458155e-2 kl=2.469454e-5 q8_kl=1.128230e-3 error_l2=1.823184e0 q8_error_l2=1.496830e1 PASS
dense_mma bf16 rows=33 K=2560 n=33 probe=0,0 edge=ordinary edge_bits=None range=true past_end_untouched=true eager_graph_bits=true PASS
dense_mma bf16 rows=1 K=128 n=1 probe=0,0 edge=within_fragment edge_bits=Some(7f000000) range=true past_end_untouched=true eager_graph_bits=true PASS
dense_mma bf16 rows=1 K=64 n=1 probe=0,0 edge=between_fragments edge_bits=Some(7f000000) range=true past_end_untouched=true eager_graph_bits=true PASS
dense_mma bf16 rows=1 K=128 n=1 probe=0,0 edge=between_stages edge_bits=Some(7f000000) range=true past_end_untouched=true eager_graph_bits=true PASS
dense_mma bf16 rows=1 K=128 n=1 probe=0,0 edge=pipeline_wrap edge_bits=Some(7f000000) range=true past_end_untouched=true eager_graph_bits=true PASS
dense_mma bf16 rows=65 K=128 n=65 probe=63,63 edge=warp_corner edge_bits=Some(7f000000) range=true past_end_untouched=true eager_graph_bits=true PASS
dense_mma bf16 rows=65 K=128 n=65 probe=64,64 edge=tile_corner edge_bits=Some(7f000000) range=true past_end_untouched=true eager_graph_bits=true PASS
dense_mma bf16 rows=1 K=128 n=1 probe=0,0 edge=butterfly edge_bits=Some(7fc00000) range=true past_end_untouched=true eager_graph_bits=true PASS
dense_mma mxfp8 rows=1 K=128 n=1 probe=0,0 edge=butterfly edge_bits=Some(7fc00000) range=true past_end_untouched=true eager_graph_bits=true PASS
dense_mma fp8_b128 rows=1 K=128 n=1 probe=0,0 edge=butterfly edge_bits=Some(7fc00000) range=true past_end_untouched=true eager_graph_bits=true PASS
dense_mma bf16 rows=1 K=128 n=1 probe=0,0 edge=store_overflow edge_bits=Some(7f800000) range=true past_end_untouched=true eager_graph_bits=true PASS
gpu-gate.sh: gate_gemm ran 10 s (exit 0)

The BF16 site gate, which also runs the stored model shapes through the tensor-core entry:

just gate-gpu-bf16
dense_mma blk.3.attn_q.weight columns=64 q8_representable=true worst_err_over_band=5.565383e-2 kl=1.534471e-4 q8_kl=1.073169e-2 error_l2=9.155672e1 q8_error_l2=4.408394e2 PASS
gpu-gate.sh: gate_bf16 ran 4 s (exit 0)

The MXFP8 and FP8_B128 gate:

just gate-gpu-mxfp8
mxfp8 clause=tile.bits shape=blk.3.attn_q_b rows=16384 k=1536 m=64 equal=1048576/1048576 PASS
fp8_b128 clause=tile.bits shape=blk.3.attn_q_b rows=16384 k=1536 m=64 equal=1048576/1048576 PASS
gpu-gate.sh: gate_mxfp8 ran 15 s (exit 0)

4K prefill timing

Each checkpoint ran as a pair on the same GB10: the baseline build first, then the candidate, each loaded once and sent one 4096-id code-fn request that generated two tokens. Concurrency was 1, drafting was off, the window was [0, 4096], and both arms used identical stored weights. Prefill tok/s is 4096 / TTFT. The NVFP4 baseline is main plus the unmerged branches that checkpoint needs to load. The FP4-Local baseline is main plus #649 and the unmerged checkpoint-folder commits its per-layer embedding reader needs. Each checkpoint compares only with its own baseline.

Checkpoint Arm Build TTFT Prefill
NVFP4 baseline main + loading branches 10.414 s 393.3 tok/s
NVFP4 candidate baseline + this PR 5.253 s 779.7 tok/s
FP4-Local baseline main + #649 + folder commits 8.252 s 496.4 tok/s
FP4-Local candidate baseline + this PR 5.358 s 764.5 tok/s

Prefill rose from 393.3 to 779.7 tok/s on NVFP4 (+98.2%) and from 496.4 to 764.5 tok/s on FP4-Local (+54.0%) [derived]. TTFT fell 49.6% and 35.1% [derived]. Each arm is one request after one server load, so these are single observations with no spread and no significance claim.

To check that the exact replay costs nothing on the normal path, I compared the revision before it with this one on NVFP4 (one load and two 4096-token requests each, same GB10). On the second request the earlier revision prefilled at 755.94 tok/s and this one at 774.78 tok/s, so there is no regression. The first request of the earlier revision was slower than its second, and I do not count that as a gain.

Correctness

  • FAIL-first: with the widened MMA output store replaced by zero, the oracle gate exited 1. With the output buffer left uncleared before graph replay, the gate reports eager_graph_bits=false FAIL and exits 1. On the previous revision, the cancelling-pair cases (between fragments, between stages, across a reused pipeline slot, warp corner, tile corner) and the NaN case read range=false for all three formats and the gate exits 1 (gate_gemm ran 11 s (exit 1)). With the replay every one of them reads range=true past_end_untouched=true eager_graph_bits=true and matches the exact tile bits.
  • Coverage includes ragged rows and columns, 513-column raster tails, graph replay with sentinels, invalid spans, unsupported K, and extreme and subnormal E4M3 scales. The original exact tile checks stay enabled.
  • Softmax KL is measured on kernel outputs against the released Q8_0 weight and int8-activation representation, next to a derived f64 oracle band. These are not whole-model KL results.
  • On sm_121, each of the three entries uses 256 threads and 49,152 bytes of shared memory, with no local depot and zero spill. The BF16, FP8_B128 and MXFP8 entries use 121, 114 and 114 registers and fit two blocks per SM, against 124, 126 and 126 registers before the replay.
  • Not run: GB10 is the only card used. No cc 8.6 hardware ran this, and there was no GLM or TP=2 run. GLM's wide dense FFN reaches this dispatch through site::gated, but its GemmFront supplies only quantized inputs, so the new path would not serve it until that frontend passes the common F32 form.

What I ran into

This does not block the PR. The full just gate-gpu-gemm without --case stops before its first case on this machine because it reads model-derived inputs this machine does not have: gate_gemm: io error: No such file or directory. The cases this PR adds are the dense_mma ones in GPU Evidence.

Hardware

One NVIDIA DGX Spark (GB10, compute capability 12.1, unified memory), driver 595.84, CUDA 13.3. Builds use BLOOMERY_ARCH=sm_121.

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 22, 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 proven behavior or quality defect remains after tracing the changed paths.

Non-blocking

⚠ Whole-model numeric gate: crates/gpu/src/gemm/f32tile.rs:528 changes operand precision and reduction order. The change-class table in AGENTS.md requires gate-gpu-e2e count-pin evidence. Attach its command and output on GB10 for the reviewed head; kernel KL checks do not establish that pin.

⚠ Reproducible timing evidence: docs/cards/dense-bf16-prompt.card:5 defines the experiment, but the PR's timing table identifies builds as main plus unnamed loading branches and checkpoints by quantization label. Add the exact model files, both build hashes for each comparison, and the timing commands with output required by CONTRIBUTING.md.

Checked

Duplicates: 20 new named functions, types and constants checked. The dryer pair at kernels32.rs:1201/1243 contains typed entries calling the same owner; no duplicate implementation. Existing vision GEMM has a different storage and output contract.
Complexity: dense_mma_case is new at 44, spanning fixture preparation, eager validation and graph replay; those are its natural stage boundaries. bf16_mma_body is new at 30, a kernel body reviewed for correctness. site_case and e4m3_case each grew by 6 for MMA validation; site::gemm grew by 5 for device dispatch. gate::run and g32::run grew by 3 and 1 for case routing.
Silent failure: 8 candidate sites followed. Padding clamps reach guarded stores; raster rejection cannot discard a valid launched tile. Q8 diagnostic substitutes and statistic clamps do not bypass the output band or finite-KL check.
Tests: mutation analysis only. Dropping f32tile.rs:569 replay fails dense_mma range/exact checks; zeroing :550 stores fails the oracle band; dropping replay stores fails sentinel equality. Accepting K=96 or malformed E4M3 planes fails the extended refusal checks. Dropping rounding.rs:149 KL finiteness makes the new unit assertion fail. CUDA gates were not rerun locally.
Gates: GB10 site bit checks become band/KL checks with dated PIN reasons; exact entries retain their bit checks. The unsafe ratchet increase has a dated PIN. No other existing threshold moved.
Safety: independent index enumeration covers 4,096 distinct shared slots and 4,096 distinct output slots. Ragged stores, pipeline reuse, block voting and exact replay barriers checked.
Escape catalog: C1–C4 do not match this diff.

Card check: gpu-evidence. This pull request changes crates/gpu-gates/src/bin/gate_bf16.rs, crates/gpu-gates/src/bin/gate_gemm.rs, crates/gpu-gates/src/bin/gate_mxfp8.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 22, 2026
@juliankang4

Copy link
Copy Markdown
Collaborator Author

The review is done and the contribution check passed, so I'm merging this.

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