Repository navigation
CUDA: MMVF and vector flash attention at small batches on NVIDIA GPUs from Turing on - #415
Open
danielhanchen wants to merge 2 commits into
Open
danielhanchen wants to merge 2 commits into
danielhanchen wants to merge 2 commits into
Conversation
…Blackwell At 2-8 tokens (batched decode, speculative verify) on datacenter Blackwell (cc 10.x, B200): - MMF costs a near-constant ~12 us per matrix (bound by its K loop) while MMVF scales with the matrix, so use MMVF for F32/F16/BF16 matrices of 64 or more rows with rows x columns <= 4096. Measured on B200 per call, MMF vs MMVF: 64 x 4608 at 8 columns 18.0 vs 11.0 us, 384 x 2304 at 8 columns 12.4 vs 7.8 us, 1024 x 2304 at 4 columns 12.4 vs 10.6 us, F32 128 x 2304 at 8 columns 18.1 vs 9.4 us; MMVF loses from 1024 x 2304 at 8 columns (12.5 vs 14.9 us). - With a mask that has a head dimension (no GQA packing), the vector flash attention kernel beats the MMA kernel up to 4 rows: 16 us vs 28 us including the combine / fixup kernels. It loses at 8 rows. Other GPUs keep their choice. GGML_CUDA_DISABLE_BLACKWELL_DC_DECODE_TUNING=1 restores the previous choice on cc 10.x. These change the kernel, so the rounding, for those shapes. test-backend-ops: flash attention with a mask per head at 1-8 rows, MUL_MAT of 64-1024 rows at 2-8 columns.
… from Turing on Measured with the 4-row MoE kernel widened the same way (GGML_CUDA_SMALL_BATCH_TUNING_ANY_CC probe), 2 / 4 / 8-sequence decode vs the cc 10.x-only rules: T4 +12 / +13 / +13%, A100 +9 / +21 / +15%, L4 +4 / +11 / +10% (Inkling-2-Mini UD-IQ1_S). Batch-1 decode and prefill unchanged. Same rounding trade at 2-8 tokens as on cc 10.x (KLD 0.0030-0.0035 at -ub 4); bit-identical at -ub 1. GGML_CUDA_DISABLE_BLACKWELL_DC_DECODE_TUNING=1 restores the previous choice. Hopper (cc 9.x) is not measured and keeps the previous choice.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
erokhins
pushed a commit
to JetBrains-Hardware/llama.cpp
that referenced
this pull request
Nov 24, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pin for the prebuilt: upstream b12602 (418fe97) plus these two commits. Base branch base/upstream-418fe97b5.
Batched decode and speculative verify run 2-8 tokens per step. On NVIDIA GPUs two kernel choices for those shapes are
slow. The first commit changes them on datacenter Blackwell (cc 10.x, B200), where they were tuned. The second applies
the same two rules to NVIDIA from Turing on (cc 7.5 and newer), measured on T4, L4, A100 and RTX PRO 6000. Hopper (cc 9.x) is not
measured and keeps its current choice, as do AMD and the other backends.
1. MMVF instead of MMF for small matrices. F32/F16/BF16 matrices with at least 64 rows and rows x columns
<= 4096 at 2-8 columns now use MMVF.
On B200, MMF costs a near-constant ~12 us per matrix at these batch sizes (it is bound by its K loop). MMVF time
scales with the matrix size instead.
Per call, B200, exclusive card (nsys, Inkling-2-Mini decode shapes):
Forcing MMVF for every matrix up to 8 columns was slower than this rule.
2. Vector flash attention up to 4 rows when the mask has a head dimension. A per-head mask (e.g. a relative
position bias) means no GQA packing.
GGML_CUDA_DISABLE_BLACKWELL_DC_DECODE_TUNING=1restores the previous choice on every GPU.Rounding
Both rules change which kernel runs for these shapes. So the results at 2-8 tokens are not bit-identical to before;
the differences are rounding-sized.
Evidence on Inkling-2-Mini, wikitext-2
-c 512, 32 chunks, scored against the previous build:-ub 4/-ub 8(the same kernel shapes as 4 / 8 sequences of batched decode):With the kill switch set, the same build is bit-identical to before at ub 4 / 8 (KLD 0). So all of the difference
comes from these two rules.
Scale: a 1-ulp perturbation of one activation (the q/k clip output) gives mean KLD 0.0031 and top-1 97.8% on the
same setup.
Scored against the previous build at
-ub 1(the single-token decode graphs):-ub 4/-ub 8logits are at KLD 0.0033 / 0.0032 (Q4_K_XL) and 0.0025 / 0.0025(Q8_K_XL).
Prefill (
-ub 512) and batch-1 decode are unchanged: bit-identical to before on UD-IQ1_S, UD-Q4_K_XL,Mainline_Support UD-IQ1_S and UD-Q8_K_XL, on 1 GPU and on 2 GPUs (
-sm layer -ts 1,1).Tests
test-backend-ops on B200 (CUDA0 vs CPU):
New cases:
case before.
test-llama-archs
inkling2.*: 6/6.Speed
B200, one exclusive card,
llama-batched-bench -c 4096 -npp 128 -ntg 64 -kvu, aggregate decode tok/s, ABBA x4,median [min-max]. Before = the previous Inkling 2 stack (pin set e1b2d72ca plus the #386 graph dedupe, #400, #401, #402). Measured with two other changes also applied (a flat concat kernel and 4 rows per block in the MoE vector kernel),
whose share is +5 to +7%:
Effect on llama-server output and DSpark
The new kernels also run in two places outside multi-sequence decode:
So greedy text from llama-server can differ from before after the first token where the rounding flips an argmax.
That holds even without a drafter.
Measured with llama-server: 4 chat prompts x 384 tokens, greedy, one slot, same card.
the 4-token prompt tail:
-ctxcp 0, so the prompt is one ubatch), before and after give identical text onboth files.
Batch-1 decode itself is unchanged.
Other NVIDIA GPUs
Colab T4, L4, A100-40GB and RTX PRO 6000 Blackwell Server, one card each, built from b12628 plus #399 plus the decode
pins, with and without this pin's second commit.
llama-batched-bench -c 4096 -npp 128 -ntg 64 -kvu, aggregate decode tok/s,3 rounds ABBA, median; the spread of #399 over its 3 runs in brackets.
Both columns include 4 rows per block in the MoE vector kernel (a separate pin, bit-exact), so the gain is this
pin's alone.
llama-bench pp512 and tg128 are unchanged on all four (within 0.5%).
Rounding, KLD against pr-set : quantized K/V banded attention for Inkling, the Inkling 2 graph dedupe and three CUDA speed pins #399 on the same card (bit-identical to this build with the kill switch set), wikitext-2
-c 512 -ub 4, 4 chunks:The same size as on B200. At
-ub 1and at-ub 512it is bit-identical to before on every GPU, and with the killswitch set every configuration is bit-identical to before.
test-backend-ops (CUDA0 vs CPU) on all four: FLASH_ATTN_EXT 4543/4543, MUL_MAT 2863/2863, MUL_MAT_ID 1167/1167.
Base: upstream b12602 (418fe97). B300 (cc 10.3) is inside the cc 10.x range, but its crossover point is not
measured. Hopper keeps the previous choice until it is measured.