Skip to content

CUDA: MMVF and vector flash attention at small batches on NVIDIA GPUs from Turing on - #415

Open
danielhanchen wants to merge 2 commits into
base/upstream-418fe97b5from
inkperf4-up-dc-decode-nv
Open

danielhanchen wants to merge 2 commits into
base/upstream-418fe97b5from
inkperf4-up-dc-decode-nv

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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):

    matrix (rows x K), columns MMF MMVF
    64 x 4608, 8 18.0 us 11.0 us
    384 x 2304, 8 12.4 us 7.8 us
    1024 x 2304, 4 12.4 us 10.6 us
    F32 128 x 2304, 8 18.1 us 9.4 us
    1024 x 2304, 8 (MMVF loses, stays MMF) 12.5 us 14.9 us
    3072 x 2304, 4 (stays MMF) 13.0 us 19.4 us
    3072 x 2304, 8 (stays MMF) 13.2 us 25.1 us
  • 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.

  • At 4 rows the vector kernel takes 7.9 us + 7.8 us combine, against 26.2 us + 2.2 us fixup for the MMA kernel.
  • It loses at 8 rows (16.8 + 17.1 us against 26.0 + 2.6 us), so the rule stops at 4.

GGML_CUDA_DISABLE_BLACKWELL_DC_DECODE_TUNING=1 restores 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):

    file ub mean KLD top-1 PPL ratio (A/A of the previous build)
    UD-Q4_K_XL 4 0.00338 97.76% 0.99957 ± 0.00110 (1.00063)
    UD-Q4_K_XL 8 0.00308 97.71% 1.00272 ± 0.00104 (1.00063)
    UD-Q8_K_XL 4 0.00243 97.95% 1.00133 ± 0.00099 (1.00062)
    UD-Q8_K_XL 8 0.00248 97.92% 1.00186 ± 0.00099 (1.00060)
  • 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):

    • The previous build's own -ub 4 / -ub 8 logits are at KLD 0.0033 / 0.0032 (Q4_K_XL) and 0.0025 / 0.0025
      (Q8_K_XL).
    • This build's are at 0.0029 / 0.0033 and 0.0024 / 0.0027.
    • All PPL ratios are within 1.3 sigma of 1.
    • So the change is the same size as the existing dependence on the ubatch size.
  • 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):

op result
FLASH_ATTN_EXT 4442/4442
MUL_MAT 2865/2865
MUL_MAT_ID 1183/1183
CPY, ADD, GET_ROWS, SOFT_MAX all pass

New cases:

  • 12 FLASH_ATTN_EXT with a mask per head (GQA 3, sinks, 1/2/3/4/5/8 rows, kv 256/1024). There was no per-head mask
    case before.
  • 36 MUL_MAT cases of 64/384/1024 rows at 2-8 columns, F32/F16/BF16.

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%:

file sequences before after ratio
UD-Q8_K_XL 1 78.7 [76.6-79.1] 73.2 [70.8-78.9] noise (ranges overlap)
UD-Q8_K_XL 4 184.9 [183.4-186.3] 223.7 [221.9-225.4] 1.21
UD-Q8_K_XL 8 281.9 [272.1-287.8] 323.5 [320.4-326.6] 1.15
UD-Q4_K_XL 1 78.9 [74.5-80.1] 79.2 [78.5-79.8] 1.00
UD-Q4_K_XL 4 179.0 [175.4-181.9] 242.3 [236.0-243.1] 1.35
UD-Q4_K_XL 8 287.1 [285.1-288.3] 352.7 [352.4-353.8] 1.23

Effect on llama-server output and DSpark

The new kernels also run in two places outside multi-sequence decode:

  • the llama-server prompt tail (with context checkpoints the last 4 prompt tokens are their own ubatch)
  • DSpark verify batches (n_max + 1 rows)

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.

file drafter before: accepted / drafted after: accepted / drafted with the kill switch
UD-Q4_K_XL DSpark n_max 2 807 / 1221 (66.1%) 806 / 1190 (67.7%) 807 / 1221, same text as before
UD-IQ1_S DSpark n_max 2 802 / 1236 (64.9%) 794 / 1246 (63.7%) 802 / 1236, same text as before
Mainline_Support UD-IQ1_S DSpark n_max 3 891 / 1597 (55.8%) 872 / 1634 (53.4%) not run
  • With no drafter, the greedy text also differs from before on all 4 prompts, on both files. Two checks pin this on
    the 4-token prompt tail:
    • With the kill switch set, the text is identical to before.
    • With context checkpoints off (-ctxcp 0, so the prompt is one ubatch), before and after give identical text on
      both files.
      Batch-1 decode itself is unchanged.
  • The sampled request (temperature 1) is fine on every build.
  • These are trajectory changes from rounding, not a quality change: see the KLD numbers above.

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.

GPU (cc) file 4 sequences, kill switch set 4 sequences gain 1 sequence
T4 (7.5) UD-IQ1_S 63.2 64.0 +1.3% [1.1%] tie
L4 (8.9) UD-IQ1_S 124.6 132.5 +6.3% [0.6%] tie
L4 (8.9) UD-Q4_K_XL 96.7 101.8 +5.3% [0.3%] tie
A100 (8.0) UD-IQ1_S 161.7 192.5 +19.1% [0.1%] tie
A100 (8.0) UD-Q4_K_XL 150.8 177.0 +17.4% [0.1%] tie
A100 (8.0) UD-Q8_K_XL 154.6 164.9 +6.7% [0.2%] tie
RTX PRO 6000 (12.0) UD-IQ1_S 291.4 385.8 +32.4% [1.1%] tie
RTX PRO 6000 (12.0) UD-Q4_K_XL 279.0 358.4 +28.5% [0.1%] tie
RTX PRO 6000 (12.0) UD-Q8_K_XL 278.6 314.6 +12.9% [0.0%] tie
  • 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:

    GPU UD-IQ1_S UD-Q4_K_XL UD-Q8_K_XL
    T4 0.0035 (top-1 98.0%)
    L4 0.0030 (97.3%) 0.0026 (97.2%)
    A100 0.0030 (97.5%) 0.0028 (97.5%) 0.0027 (97.8%)
    RTX PRO 6000 0.0028 (97.8%) 0.0037 (97.0%) 0.0017 (97.8%)

    The same size as on B200. At -ub 1 and at -ub 512 it is bit-identical to before on every GPU, and with the kill
    switch 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.

…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.
@chatgpt-codex-connector

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-11-23T02:26:30.254534Z a7c4e14 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