Skip to content

gpu: BF16 sites run small batches through the multi-column gemv - #652

Merged
juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-bf16-mcol
Oct 22, 2026
Merged

juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-bf16-mcol

Conversation

@juliankang4

@juliankang4 juliankang4 commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

BF16 가중치의 2~8행 호출을 64열 타일 GEMM 대신 다열 GEMV 한 번으로 처리하게 바꿔, DGX Spark에서 MTP 디코드가 코드 코퍼스 기준 30.33 tok/s에서 36.07 tok/s로 빨라집니다.

Part of #565.

What and why

A BF16 weight (attention and SSM projections, the output head) sees one row per request in plain decode, and 2 to 8 rows per step when several requests decode together or when MTP (multi-token prediction, drafted tokens checked together) verifies a draft. site::gemv sent one row to bf16_gemv but sent 2 to 8 rows to bf16_tile_gemm, the 64-column tile built for prompts, which also needs K to be a multiple of 64.

This PR gives bf16_gemv two output strides, so it writes either row-major or token-major. site::gemv now sends every width from 1 to 8 through it with token-major output, as the Q8_0 sites do with q8_0_gemv_mcol. Wide calls (64 columns) keep the tile. The lane accumulation and the warp butterfly sum are unchanged, so every output bit equals the F32 entry's over the widened values, and the tile's. The change adds no CUDA entry, no output buffer and no transpose. It has no model, node or rank-count branch, and the stride follows each tensor's local rows, views included.

Release notes: Changed: BF16 matrices take one multi-column gemv for batches of 2 to 8 rows, which speeds up MTP and concurrent decode on a DGX Spark.

Code shape

  • No second copy. enqueue_bf16_gemv delegates to the new enqueue_bf16_gemv_mcol; both layouts share the geometry check, the accumulation and the store helper. The BF16 head and MTP head callers keep the row-major call.
  • Model code holds only architecture facts. No model file changes.
  • Dead code is gone. The tile branch of gemv_planes is replaced by the gemv call. bf16_tile_gemm stays: site::gemm still uses it at 64 columns.
  • No silent failure. The geometry refusals of enqueue_bf16_gemv are unchanged and named.
  • Comments state what is true now.
  • No relaxed gate. No band, tolerance or lint level moves.
  • cargo fmt is clean on what I touched.

Duplication found or removed (path:line): none. The one kernel is reused; the output layout is chosen by GemvOut, as in crates/gpu/src/q8f32.rs for Q8_0.

What I ran

Speed A/B

Main is 05b5cbb94fb010ba15eaf569d7435224eb5bfd17, and the measured head 259cd0ba is this change on that main. The PR head is the same change on main f02460d16. Main changed none of q8f32.rs, site.rs and gate_bf16.rs between the two. Against the measured diff, the PR diff only refills the gate's output buffer with NaN before the tile comparison, compares the tile and replay outputs with the shared bits_equal, makes enqueue_bf16_gemv_mcol and Bf16GemvMcolArgs pub(crate) and drops two doc-comment lines, so no kernel or launch differs from what was timed.

The model is nvidia/Qwen3.8-Flash-Next-NVFP4 at revision fc694b54fb0174e0913e6adf86691ef85a4ead47, converted by bloomery-model-hf's prepare --keep-bf16 with BLOOMERY_PREPARE_Q8 unset, so its BF16 matrices stay BF16 (type 30). MTP uses MTP/mtp-Qwen3.8-Flash-Next-shared-Q8_0.gguf from unsloth/Qwen3.8-Flash-Next-GGUF at revision 766911a6b7369840a91dbcd95f9f997acaab6cd6. Both servers run with --parallel 4 --ctx-size 16896. Each round starts a fresh server per arm after a discarded 4,096-token warm-up request. One request means one client at a time, four requests means four clients at once. Prompts are 4,096 tokens from a code corpus and a prose corpus, and each request generates 128 tokens. There were three rounds, the arm order rotated every round, and main and head alternated within one lease. The table gives medians of the three rounds.

Decode tok/s is a request's 127 tokens after its first, divided by the time from its first to its last token. Steady total tok/s counts the tokens all four requests produce while all four are decoding, from the latest first token to the earliest last token. Makespan runs from the first request sent to the last token.

Case Metric Main Head Change
code, MTP, one request decode tok/s 30.33 36.07 +18.9%
prose, MTP, one request decode tok/s 19.24 23.28 +21.0%
code, no MTP, one request decode tok/s 20.56 20.58 +0.1%
prose, no MTP, one request decode tok/s 20.52 20.49 -0.1%
code, four requests steady total tok/s 40.75 48.22 +18.3%
code, four requests makespan s 45.75 43.93 -4.0%
code, four requests client decode tok/s, median 10.20 8.42 -17.5%
prose, four requests steady total tok/s 40.81 49.31 +20.8%
prose, four requests client decode tok/s, median 10.27 12.40 +20.8%

Delta (head minus main): MTP decode on code +18.9% and on prose +21.0%; four code requests: steady total +18.3%, makespan -4.0%, client decode median -17.5%.

The one-request no-MTP rows are controls: their ranges overlap, so this run shows no change at one request without MTP. MTP decode on code is faster in every round, +16.7% to +27.4%.

Every one of the 60 main/head response pairs holds the same 128 token ids.

The four-request code row

The client decode median for four code requests falls even though the steady total rises and the makespan shrinks. The fall tracks when the requests start decoding. The steady total shows the decode step itself is faster. The serve worker runs a whole prompt before it steps the other slots, so with four 4,096-token prompts the first tokens arrive in two waves (round 1, seconds from the start):

Build First wave Second wave
main 1 request at 8.40 s 3 requests at 33.41 s
head 2 requests at 16.74 s 2 requests at 33.40 s

On main, three of four requests have their first token only after every prompt is done, so their decode window has no prompt in it. With this change two requests have a first token at 16.74 s and then wait through the other two prompts, and that gap sits inside their 127-token window. The median over four requests therefore moves with how many requests get an early first token. In the steady window all four are decoding, and the total there is 40.75 tok/s on main and 48.22 tok/s with this change. The prose row, which has the same prompt scheduling, rises. Prompt scheduling is not part of this PR.

GPU evidence

Card and driver, from nvidia-smi on the DGX Spark that ran the gate:

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

Card and clock state at the start of the A/B sitting, with the clock cap lifted for the sitting and restored after it (nvidia-smi --query-gpu line, then the first witness):

NVIDIA GB10, 595.84, 305 MHz, 3003 MHz, 49, 5.13 W
    timing-card: NVIDIA GB10, [N/A], 3003 MHz

The gate, on the changed gemv and its site:

BLOOMERY_BOX_LOCAL=1 BLOOMERY_ARCH=sm_121 just gate-gpu-bf16
gpu-gate.sh: gate_bf16 ran 13 s (exit 0)

gate_bf16 now checks every width 1..=8 on four shapes (8,192 x 2,560, 2,560 x 4,096, 1,024 x 128 and a 19-row tail) against the F32 site and the BF16 tile, captures each small call as one graph node and replays it, and adds a K = 96 case the tile cannot take. All 32 tile comparisons and all 32 graph replays with one node pass. The kernel keeps no local memory:

shape kernel=bf16_gemv local_depot=false ld_local=0 st_local=0 PASS

The tile comparison refills the output buffer with NaN before the tile runs, so a tile that skips a write cannot pass on the F32 site's values. With bf16_tile_gemm changed to return at once for n < 32, the gate fails on every small width, and passes again with the kernel restored:

bf16 clause=tile.bits shape=blk.3.attn_q.weight rows=8192 k=2560 call=gemv m=1 equal=false FAIL
bf16 clause=tile.bits shape=blk.3.attn_output.weight rows=2560 k=4096 call=gemv m=1 equal=false FAIL
MUTANT rc=1
bf16 clause=tile.bits shape=blk.3.attn_q.weight rows=8192 k=2560 call=gemv m=1 equal=true PASS
FIXED rc=0

Launch geometry. Widths 2 to 8 used to launch bf16_tile_gemm as ceil(rows / 32) blocks of 512 threads and now launch bf16_gemv as ceil(rows / 8) blocks of 256 threads. From just ptx-scan gate_bf16 bf16 on the GB10 (the jit_regs column is what the driver JIT assigned for compute capability 12.1):

entry                         reqntid  depot  ld.local  st.local    fma  cvt.f16   regs   smem  spill blk/SM(static) jit_regs jit_local ld.global.scalar ld.global.v2 ld.global.v4
bf16_gemv                         256     no         0         0     83        0     48      0      0              5       40         0              103            0            0
bf16_tile_gemm                    512     no         0         0    128        0    110  49152      0              1      109         0                6            0            0

The GB10 has 48 SMs, 65,536 registers, 1,536 threads, 24 blocks and 102,400 B of shared memory per SM. bf16_tile_gemm needs 109 x 512 = 55,808 registers per block, so one block (16 warps) fits on an SM, and its 49,152 B of shared memory would allow two. bf16_gemv needs 40 x 256 = 10,240 registers and no shared memory, so the thread limit binds at six blocks (48 warps). An SM therefore holds 48 rows in flight with the gemv against 32 with the tile, with three times the warps to cover memory latency [derived]. Neither kernel spills.

The same gate with only its own changes applied to main's site.rs fails on the K = 96 case: one column passes, and two columns reach the tile's multiple-of-64 check.

bf16 clause=k96.site.bits rows=8 k=96 m=1 equal=8/8 PASS
gate_bf16: Q8F32Kernels::enqueue_bf16_tile: k must be a positive multiple of 64 (the staged line), got 96
gpu-gate.sh: gate_bf16 ran 11 s (exit 1)

Reproduce

Convert the checkpoint folder, then start one server per arm, main's binary and this branch's in turn:

env -u BLOOMERY_PREPARE_Q8 prepare --keep-bf16 <Qwen3.8-Flash-Next-NVFP4 folder> <out>/nvfp4-asstored
# MTP arms
BLOOMERY_DRAFT=mtp BLOOMERY_MTP_DRAFT=<GGUF repo>/MTP/mtp-Qwen3.8-Flash-Next-shared-Q8_0.gguf \
  bloomery-serve -m <out>/nvfp4-asstored-00001-of-00004.gguf --host 127.0.0.1 --port <port> \
  --parallel 4 --ctx-size 16896
# no-MTP arms
BLOOMERY_DRAFT=off bloomery-serve -m <out>/nvfp4-asstored-00001-of-00004.gguf --host 127.0.0.1 \
  --port <port> --parallel 4 --ctx-size 16896

Each request is a streamed POST /completion with the 4,096 prompt ids, greedy and without the prompt cache:

{"prompt": [<4096 ids>], "n_predict": 128, "cache_prompt": false, "stream": true,
 "timings_per_token": true, "return_tokens": true, "temperature": 0, "top_k": 1}

Hardware

A DGX Spark (NVIDIA GB10, driver 595.84, CUDA 13.3, sm_121 release builds), CPU governor performance. During the sitting the card reported a 3003 MHz maximum SM clock and up to 83 C, with zero thermal-cap counts.

What I ran into

None of this blocks the PR. A standalone read-bandwidth probe at the start and the end of the sitting fell from 252.4 GB/s to 196.1 GB/s, so I draw no absolute bandwidth conclusion. The result above is a main/head comparison taken under alternating arms in one lease. I took no component profile of the small BF16 calls, so the gain is measured end to end only. I also did not instrument why this change admits two requests in the first wave instead of one.

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 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: MUST_FIX: Two new gate comparisons duplicate the existing bit-equality helper.

Must fix

1. Reuse bits_equal for the new comparisons

crates/gpu-gates/src/bin/gate_bf16.rs:371 and crates/gpu-gates/src/bin/gate_bf16.rs:384 copy the bit comparison in crates/gpu-gates/src/lib.rs:719. The public bits_equal helper already checks lengths and bits. This is a kind B duplication defect under R14.
Reusing it removes duplicate logic and leaves one equality contract to maintain, with no new helper or parameter.

Replace the tile comparison at line 371 with:

let same = bloomery_gpu_gates::bits_equal(&tile, &y);

Replace the replay comparison at lines 384–387 with:

let same = bloomery_gpu_gates::bits_equal(&replay, &y);

Checked

Duplicates: both added comparison loops were checked against bits_equal; the replacements use its existing public signature.

Complexity: site_case adds tile-comparison and replay branches; k96_case adds a width loop.

Silent failure: the changed launcher propagates geometry and launch errors; graph capture and replay errors propagate.

Tests, static mutation check: changing the strides at crates/gpu/src/q8f32.rs:2462 breaks site/F32 equality. Returning early from crates/gpu/src/q8f32.rs:1687 breaks the poisoned tile comparison. At crates/gpu/src/site.rs:578, an extra launch fails the graph node assertion; restoring tile dispatch fails K=96.

Gates: no bands or tolerances moved. The PR supplies tile and K=96 FAIL-first output.

Escape catalog: no C1–C4 patterns matched.

Notes

Validation was static. CUDA gates were not rerun here. The PR reports a GB10 gate run and timed A/B results.

Card check: gpu-evidence. This pull request changes crates/gpu-gates/src/bin/gate_bf16.rs, crates/gpu/src/q8f32.rs, crates/gpu/src/site.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)

Comment thread crates/gpu-gates/src/bin/gate_bf16.rs Outdated
)?;
stream.synchronize()?;
let tile = f_dev.to_host_vec(stream)?;
let same = tile.iter().zip(&y).all(|(a, b)| a.to_bits() == b.to_bits());

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.

Reuse the existing public bit-equality helper, which checks lengths and bits (R14).

Suggested change
let same = tile.iter().zip(&y).all(|(a, b)| a.to_bits() == b.to_bits());
let same = bloomery_gpu_gates::bits_equal(&tile, &y);

Comment thread crates/gpu-gates/src/bin/gate_bf16.rs Outdated
Comment on lines +384 to +387
let same = replay
.iter()
.zip(&y)
.all(|(a, b)| a.to_bits() == b.to_bits());

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.

Reuse the existing public bit-equality helper for the replay comparison (R14).

Suggested change
let same = replay
.iter()
.zip(&y)
.all(|(a, b)| a.to_bits() == b.to_bits());
let same = bloomery_gpu_gates::bits_equal(&replay, &y);

@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

@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.

Since the last review: must-fix 1 is fixed. Both comparisons now reuse bits_equal at gate_bf16.rs:371 and gate_bf16.rs:384.

Verdict: CLEAN: The duplication is removed, and the two comparison replacements preserve the gate checks.

Checked

Duplicates: 0 new items in the interdiff; both edited sites reuse the existing owner. Dryer reported 0 pairs in gpu.

Complexity: crapper reports site_case 22 → 34 (tile and graph-check branches) and k96_case 9 → 15 (width loop) for the full PR. The interdiff adds no branches.

Silent failure: 2 comparison results followed through ok to checks_failed and a failing exit status.

Tests, static mutation check: replacing the token-major stride at q8f32.rs:2462 with (m, 1) fails the host-rule bit assertion. Both edited comparisons still reject differing bits and now reject unequal lengths. Matching NaN buffers still fail the existing finite-output check.

Gates: no bands, tolerances or pins moved. No test clauses changed in the interdiff.

Escape catalog: no C1–C4 patterns matched.

Notes

Dryer skipped gpu-gates by design; both edited comparisons were checked manually.
Validation was static. CUDA gates were not rerun here. The PR supplies GB10 gate output and timed A/B evidence.

Card check: gpu-evidence. This pull request changes crates/gpu-gates/src/bin/gate_bf16.rs, crates/gpu/src/q8f32.rs, crates/gpu/src/site.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 4b884ea into midagedev:main Oct 22, 2026
6 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant