Skip to content

CUDA: keep four BF16 weight loads in flight in mul_mat_vec_f - #508

Open
danielhanchen wants to merge 1 commit into
base/upstream-9a811bab3from
pin-mmvf-bf16-ilp
Open

danielhanchen wants to merge 1 commit into
base/upstream-9a811bab3from
pin-mmvf-bf16-ilp

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Dec 6, 2026 •

Copy link
Copy Markdown
Member

Summary

The unslothai/llama.cpp prebuilt is an aged upstream b#### tag plus the pins listed in scripts/unsloth/pr-set.json, merged in order. This pin is one commit on top of upstream b12966 (9a811bab3c4) and needs no other pin. Once upstream has an equivalent change, the pin is dropped.

The problem

The BF16 branch of mul_mat_vec_f (one-column BF16 x F32 products: BF16 GGUFs at decode, and the BF16 tensors that UD quants keep) issues one 4 byte weight load per thread per loop iteration. On a B200 this reaches about 2.9 TB/s and the rate stays flat across a 485x range of matrix sizes, which points at too few loads in flight rather than at bandwidth or occupancy.

The fix

  • mmvf.cu, BF16 (non-HIP) branch: the column loop is strip-mined four ways, so each thread has four weight loads (and four gate loads when the GLU fusion is active) outstanding. A remainder loop handles the last columns.
  • Each thread keeps exactly the same column set and adds into sumf[j] in the same order as before, so the result is bit-identical. (A 128 bit vector load would hand each thread a contiguous chunk instead, which changes the column partition and the summation order, so it is not used.)
  • HIP, F16, F32 and FP8 paths are unchanged.

Evidence

Check Result
Speed (measured on our fork, identical code) Qwen3.6-35B-A3B BF16, one B200, batch-1 decode 196.95 / 194.63 -> 209.64 / 209.81 tok/s (+7.1%); BF16 mul_mat_vec_f kernel time 407.98 -> 363.06 ms
B200 (sm_100a) test-backend-ops -b CUDA0, b12966 + this pin MUL_MAT 2845/2845. MUL_MAT_ID 1128/1135: the 7 failures are the partial-row-block cases added by the row-bound pin, which fail the same way on b12966 without this pin (fixed by that pin, not related to this one)
Greedy md5, B200, b12966 vs + this pin, long prompt, 2 runs each identical on all 8: Qwen3-0.6B BF16 / Q4_K_M, Llama-3.2-1B F16, gemma-3-1b BF16 / Q4_K_M, granite-4.0-h-tiny Q4_K_M, Qwen3-30B-A3B UD-Q2_K_XL, tiny Qwen3-MoE F16
No-break md5 on the pin-set tree (RTX 5090, sm_120a): Qwen4-35B-A3B, Qwen3.8-Flash-Next, Inkling-2-Mini, Qwen3.8-27B, 2 prompts each identical to the pin-set tree
Speed, RTX 5090, pin-set tree vs + this pin, tg128, 3 rounds x 3 reps Qwen3-0.6B BF16 746.4 -> 750.2 t/s (+0.5%, A/A 0.06%); Qwen3-30B-A3B Q4_K_XL 383.2 -> 383.2 (no BF16 mat-vec, unchanged)
BF16 greedy md5 on the pin-set tree (RTX 5090), 2 prompts x 128 tokens identical on Qwen3-0.6B BF16, gemma-3-1b BF16 and granite-4.0-h-tiny BF16 (MoE); 5 launches of the pin on Qwen3-0.6B BF16 give one output
tg128, same box, 3 rounds A / pin / A, median Qwen3-0.6B BF16 778.2 -> 784.5 t/s (+0.8%, A/A 0.18%); gemma-3-1b BF16 541.0 -> 540.4 (-0.1%, A/A 0.07%); granite-4.0-h-tiny BF16 306.6 -> 307.7 (inside A/A 0.6%)
CUDA compile, nvcc 12.8, mmvf.cu for sm_75 / 80 / 89 / 100 / 120 OK
Pin set sim (llama_pinset.py sim, private copy, b13023, 12-06): master pr-set + open candidates #485-#503 (without #495) + #505 + #508-#513 + #516 at their current heads, 74 pins every pin merges (git or additive); merge_checks clean; pin_contract: all pins intact once #491 is listed at its current head e6e34ffd3c2 (the copy listed it at 131d93897cd, whose comment-trimmed line arrives through #505 / #512, a listing artifact). CUDA mmvq.cu / mmvf.cu / mmid-gemm.cu / fusion.cu compile on the merged tree (sm_75 + sm_100a)

Not proven

  • The speed number is from the fork on B200 (sm_100); on the pin-set tree only the RTX 5090 (sm_120a, above) was timed; sm_80 / 89 were not.
  • HIP is untouched (the HIP branch is a separate loop).
  • With candidate CUDA: BF16 x F32 one-column mul_mat for sm_100 #503 in the mix, its kernel takes aligned one-column BF16 x F32 products on sm_100 before mul_mat_vec_f is reached, so there this pin only covers the cases CUDA: BF16 x F32 one-column mul_mat for sm_100 #503 declines. Its speedup is still to be measured with CUDA: BF16 x F32 one-column mul_mat for sm_100 #503 present on both sides (GGML_CUDA_DISABLE_MMV_BF16_F32=0/1).
  • test-backend-ops reaches the new four-way loop through the long-K BF16 MUL_MAT cases (K=10240 / 2560). The MUL_MAT_FUSION cases use K=256 and only reach the remainder loop, so the gate loads are covered only by the greedy md5 runs.

The BF16 GEMV loop issues one 4 byte load per thread per iteration. On a
B200 it reaches 2.90 TB/s and the rate is flat across a 485x size range,
which points at memory-level parallelism rather than bandwidth or
occupancy.

Strip-mine the loop four ways so four loads are outstanding. Each thread
keeps the same column set and accumulates into sumf[j] in the same order,
so the result is bit-identical. A 128 bit vector load would give each
thread a contiguous 4-pair chunk instead, changing the column partition
and the summation order, so it is not used.

Qwen3.6-35B-A3B BF16 on one B200, batch-1 decode: 196.95/194.63 ->
209.64/209.81 tok/s (+7.1%). BF16 mul_mat_vec_f kernel time 407.98 ->
363.06 ms.
@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-06T08:37:07.260047Z 9c5a846 PR opened
ℹ️ 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.

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