Skip to content

CUDA: BF16 x F32 one-column mul_mat for sm_100 - #503

Open
danielhanchen wants to merge 3 commits into
base/upstream-7f6ead10dfrom
bf16-mmv-decode
Open

danielhanchen wants to merge 3 commits into
base/upstream-7f6ead10dfrom
bf16-mmv-decode

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Dec 6, 2026 •

Copy link
Copy Markdown
Member

Summary

Faster CUDA decode for BF16 weight tensors on B200 / GB200 (sm_100). On Qwen4-35B-A3B GGUFs that keep the small hyper-connection, ssm_beta_alpha and attention tensors in BF16, tg128 goes from 0.92-0.96x to 1.035-1.038x of the equivalent all-Q8_0 file. Outputs stay the same: the batched path is bit-identical, and decode matches an exact reference.

unslothai/llama.cpp prebuilt = upstream ggml-org aged b#### + scripts/unsloth/pr-set.json pins, merged in order. This PR is a pin: upstream b13019 (7f6ead1) plus this one change, based on base/upstream-7f6ead10d. It is not in pr-set.json yet.

Cause

A BF16 matmul with one src1 column goes through MMVF, which is slow for these shapes:

  • hc_*_up is K=256 x N=8192. MMVF launches 8192 blocks of 128 threads, and each thread makes one 4-byte load. That costs about 10 us per call, 80 calls per token. The same tensor in Q8_0 runs in about 8 us through MMVQ.
  • hc_*_down_inject is K=8192 x N=260. It gets only 260 blocks, and each thread makes 16 sequential 4-byte loads.
  • MMVF waits on the previous kernel (PDL) before it reads any weights.

On a Q6_K_XL-style file this adds about +257 us of kernel time per token.

Change

New kernel mul_mat_vec_bf16_f32 (ggml/src/ggml-cuda/mmv-bf16-f32.cu), dispatched from ggml_cuda_mul_mat ahead of MMVF:

  • Splits each row over 32, 64 or 128 threads with 16-byte loads, and packs several rows per block for short rows.
  • Issues the weight loads before the PDL wait, only when src0 is in a weights buffer and is not a trainable parameter (the same test as mmq_few_is_constant).
  • Accumulates in F64. A BF16 x F32 product is exact in F64, so each output is the dot product rounded once to F32, whatever the thread split.
  • Used only when: one src1 column, BF16 src0, F32 src1/dst, contiguous 2D, K % 8 == 0, K <= 8192, and cc 10.0 (sm_100). sm_103, consumer and older GPUs have a much lower FP64 rate, so they keep MMVF.
  • Kill switch: GGML_CUDA_DISABLE_MMV_BF16_F32=1.
  • test-backend-ops: new one-column BF16 MUL_MAT cases covering short, long, odd-N and K=8192 shapes.

Results (B200, exclusive, llama-bench -t 12, 3 alternating rounds x 3 repeats, medians of 9)

Ratio vs the equivalent all-Q8_0 file (same kernel, measured on an earlier upstream base; the gate-scope check below is on this branch):

file before pp512 / tg128 after pp512 / tg128
Q4_K_XL variant 1 1.068 / 0.950 1.067 / 1.038
Q4_K_XL variant 2 1.018 / 0.954 1.027 / 1.037
Q5_K_XL 0.984 / 0.958 1.019 / 1.038
Q6_K 1.013 / 0.958 1.016 / 1.038
Q6_K_XL 1.057 / 0.916 1.060 / 1.035
  • The kernel alone moves tg128 by 1.094x (Q4_K_XL) and 1.129x (Q6_K_XL); pp512 is unchanged.
  • The currently published Q4_K_XL runs at 1.001 / 0.998 of the unpatched build.
  • Gate scope, checked end to end (exclusive B200, tg128, 3 rounds x 3 repeats): all eligible shapes 1.113x / 1.123x over MMVF (Q6_K_XL / Q4_K_XL variant), K <= 2048 only 1.071x / 1.112x, K <= 512 only 1.080x / 1.112x. Timed alone with warm caches, some long-K shapes (K = 2048 to 8192, few rows) are 4-18% slower than MMVF, but inside the model the prefetch before the PDL wait more than pays for it, so the full gate is kept.
  • Per op, alone (test-backend-ops perf, exclusive): hc_up 8192 x 256 is 9.55 -> 3.22 us, 2048 x 512 is 3.66 -> 1.85 us.
  • An nsys trace confirms the new kernel runs for hc_up, hc_down_inject, ssm_beta_alpha and the attention projections.

Correctness

  • Batched path (llama-perplexity, batch 2048, 20 chunks, patched build vs its kill switch): PPL string-equal, mean KLD 0, same top token 100% on three files. Prompt processing never takes the one-column path.
  • Decode path (batch 1): KLD 0 against an F64-sum reference kernel. MMVF lands at 0.009-0.016 against the same reference. This model amplifies F32 summation-order differences over a 512-token sequence; any F32 reordering lands near 0.014.
  • test-backend-ops on B200 (this branch): MUL_MAT 2927/2927 (three runs), MUL_MAT_ID 1104/1104, the new BF16 one-column cases all pass; with the kill switch MUL_MAT is also 2927/2927.

Recheck on head 91b4f59 (B200)

  • test-backend-ops -o MUL_MAT: 2927/2927 with and without the kill switch (base 2877/2877); MUL_MAT_ID 1104/1104. Negative control: skipping the write of the last row fails 28 of the 185 one-column BF16 cases.
  • Qwen3-0.6B BF16 (every projection BF16), wikitext-2, 8 x 512: batched (-ub 512) head vs base mean KLD 0, same top 100%; decode (-ub 1) kill switch vs base KLD 0; head vs base PPL 21.7323 vs 21.7324, same top 100%. nsys shows mul_mat_vec_bf16_f32 in the decode trace.
  • Exclusive B200, base/head interleaved ABBA x2, llama-bench -t 12 on that file: tg128 732.4 / 735.0 / 732.7 / 723.8 (base) vs 828.4 / 838.2 / 840.1 / 828.7 (head), about 1.14x; pp512 within noise (busy host).

Pin set

Not tested

  • sm_103 (B300), consumer Blackwell, Hopper, Ampere and AMD. The kernel is gated off there, so they keep MMVF.
  • Multi-column decode (MTP or speculative verify with 2+ columns), which still uses MMVF.
  • Inkling / DiffusionGemma numeric gates (the one-column path is never taken by batched perplexity, so they are not expected to move).
  • A full build of the merged pin set: on b13019 it currently fails with or without this PR (GGML_OP_COUNT != 102; two other pins each add one op and both bump the count from 101 to 102), which is also why the 12-05 nightly failed.
  • The pin-set entry (pr-set.json, feature-checks.json) is a follow-up once this is reviewed.

Decode (one src1 column) of BF16 weights went through MMVF, which runs one block per
row sized for the fewest 4-byte loads per thread: K = 256 rows (128 threads x one load,
8192 blocks of 512 bytes) and its weight loads waited for the PDL sync. The new kernel
splits a row over 32/64/128 threads with 16-byte loads, packs several rows per block,
issues the weight loads before the PDL wait, and sums the exact F64 products in F64,
so the result is the dot product rounded once to F32 (independent of the split).
Taken for one src1 column, contiguous 2D operands, K % 8 == 0, K <= 8192, cc 10.0
(sm_103 and consumer GPUs have a much lower FP64 rate and keep MMVF).
Kill switch: GGML_CUDA_DISABLE_MMV_BF16_F32.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 6, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-06T04:59:13.803194Z 91b4f59 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen danielhanchen self-assigned this Dec 6, 2026
Use the mmq_few_is_constant test (view root in a weights buffer, not GGML_TENSOR_FLAG_PARAM),
since the optimizer step writes trainable weights.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 91b4f590bb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant