Skip to content

CUDA: quantized GQA-3 dp4a+half2 banded decode & small-batch banded MMA for Inkling 2 - #407

Open
danbunnell wants to merge 1 commit into
unslothai:inkling2-attn-bandedfrom
danbunnell:pr-inkling2-gqa3-banded-512k
Open

danbunnell wants to merge 1 commit into
unslothai:inkling2-attn-bandedfrom
danbunnell:pr-inkling2-gqa3-banded-512k

Conversation

@danbunnell

@danbunnell danbunnell commented Nov 22, 2026 •

Copy link
Copy Markdown

Summary

Builds on #386 (inkling2-attn-banded @ 06322ffb0) specifically for quantized KV caches (Q8_0/Q4_0) at >= 4096 global context, leaving F16/BF16 KV caches (handled by #413) and dense chunking limits (n_kq_bias_max = 1 << 29) completely untouched:

  1. Quantized GQA-3 dp4a + half2 split-K banded decode (fattn-banded.cu):
    • Single-token decode path (q_len == 1) with optional device-side q_pos (src[7] via ggml_flash_attn_ext_banded_add_pos) so CUDA graphs stay valid across positions on padded n_kv.
    • src[7] (q_pos) is now honored across every backend/fallback path: CUDA vec_gqa, CUDA f32_split & f32 scalar fallbacks, and the CPU reference (ggml/src/ggml-cpu/ops.cpp), and validated in ggml_cuda_flash_attn_ext_banded_supported + use_banded().
    • In inkling2.cpp, single-token banded_decode is gated strictly to Q8_0/Q8_0, Q4_0/Q4_0, and Q8_0/Q4_0 global caches (c0 == 0, n_kv >= 4096), leaving F16 and BF16 KV untouched.
  2. Quantized-only small-batch banded MMA (2..32 tokens on >= 4K global KV):
    • Routes 2..32-token batches on contiguous quantized global KV caches (>= 4096) to quantized banded MMA (min_q = 2), preventing multi-GiB dense kq_b spikes on 24 GB GPUs at 256K/512K while leaving F16/BF16 small batches (e.g., DSpark verify) untouched.
    • Restores n_kq_bias_max = 1 << 29 and original chunk padding so multi-stream -kvu graphs never hit the node budget assertion.
  3. O(1) seq_contig_prefix() & exact can_reuse() (llama-kv-cells.h, llama-kv-cache.cpp, llama-graph.cpp):
    • Amortized O(1) check for c0 == 0 sequences, with matching c0_zero and banded_small_long checks in llm_graph_input_mem_hybrid_iswa_rel::can_reuse() so sequences starting at c0 != 0 keep graph reuse.
  4. Kill switch & tests:
    • GGML_CUDA_DISABLE_BANDED_DECODE=1 disables both the inkling2.cpp routing and the CUDA vec_gqa / min_q = 2 dispatch (bit-identical to base).
    • Added 11 test-backend-ops cases for FLASH_ATTN_EXT_BANDED: device q_pos over padded KV (mask == nullptr, tail past q_pos ignored) for Q8_0, Q4_0, Q8_0/Q4_0, F16, BF16, and Q5_1, plus n_q = 2, 8 quantized banded MMA at n_kv = 4096.

Status

  • Rebased cleanly onto inkling2-attn-banded (06322ffb0), merges with zero conflicts onto b12635 + #419 (#411–#418), and compiles cleanly for sm_86 (CUDA 13.3).
  • GPU verification (test-backend-ops, KLD with kill switch on/off, bf16 KV at 8K, -kvu multi-stream check, and #419 vs #419 + #407 benchmarks on RTX 3090) will be posted next.

@danbunnell

Copy link
Copy Markdown
Author

GPU Verification Results (Linux x86_64, RTX 3090 24 GB sm_86, CUDA 13.3)

Tested the combined mix (b12606 + pr-set.json@cfc9ea37 + #407 + #408 + #409, zero merge conflicts, merged tree b301feeb6) on unsloth/Inkling-2-Mini-GGUF (UD-IQ4_XS + CPU mmproj-F16.gguf):

  • test-backend-ops test -b CUDA0 -o FLASH_ATTN_EXT_BANDED: 71/71 PASS
  • KLD parity (llama-perplexity, 16 chunks, -ub 128):
    • q8_0 KV: Mean KLD = 0.003023, Mean PPL(Q) = 3.048365
    • q4_0 KV: Mean KLD = 0.007744, Mean PPL(Q) = 3.054044
  • 256K (-ctk q8_0 -ctv q8_0 -ub 128 -fa on, 100% GPU): 6/6 end-to-end gates (load, chat, reasoning, tool-calling, needle-in-haystack context, speed) PASS — 92.1 tok/s decode, 772 tok/s prefill (vs 57.5 tok/s decode / 638 tok/s prefill on #386@7db4a706), peak VRAM 21,161 MiB, 0 NVRM Xids.
  • 512K (-ctk q4_0 -ctv q4_0 -ub 128 -fa on, 100% GPU, no --n-cpu-moe): 6/6 gates PASS — 93.0 tok/s decode, 627 tok/s prefill (vs 54.5 tok/s / 383 tok/s with --n-cpu-moe 2 -ub 64 on #386@7db4a706), peak VRAM 21,445 MiB, 0 NVRM Xids.

@danielhanchen

Copy link
Copy Markdown
Member

Thanks for this. I tested it on a B200 on top of our pin set (#399 tree, the published Mini GGUFs do not load on #386 alone), base = pin set, head = pin set plus this PR.

Two correctness problems:

  1. bf16 KV gives wrong output at 4096+ cells. On decode the global-layer mask is dropped (src/models/inkling2.cpp:517) and the position is passed as an extra input (:534), but only the new kernel reads it. The kernel takes q8_0/q8_0, q4_0/q4_0, q8_0/q4_0 and f16/f16 with head dim 128 (fattn-banded.cu:1594); anything else falls through (:1695, :1709) to the generic banded kernel, and the CPU op (ops.cpp:9031), which ignore the position and attend over unwritten padded cells. Measured with bf16 KV at 8K: mean KLD 0.0386, max 4.84, top-1 91.9%, PPL 5.549 to 5.731. CPU-resident layers should hit the same path (from reading the code, not run).
  2. Abort at long unified-KV contexts. With the bias cap at 1<<25 (inkling2.cpp:9), multi-sequence batches split into up to 64 chunks per layer and run out of graph nodes: ggml.c:1862: GGML_ASSERT(obj_new) failed at -c 131072 -kvu -npl 32 -npp 4000 and at -c 262144 -kvu -npl 8 -npp 32000. The base runs both (256K x 32 streams peaks at 37.0 GB).

Numerics on the supported KV types: KLD 0.0021 to 0.0031 vs base at 8K and 32K with f16 and q8_0 KV (top-1 about 98%), exact at short contexts. PPL moves both ways, so it is rounding, but it is a change on every decode step past 4096 cells and there is no switch to turn it off.

Speed (exclusive B200, 3 interleaved rounds, median):

base head ratio
tg128 Q4_K_XL f16 d0 / d8K / d32K 77.9 / 81.0 / 77.3 76.1 / 79.8 / 75.7 0.98 / 0.99 / 0.98
tg128 Q4_K_XL q8_0 d8K / d32K 70.5 / 70.6 73.2 / 72.5 1.04 / 1.03
pp8 -ub 8 Q4_K_XL q8_0 d8K 216.4 234.0 1.08
pp8 -ub 8 Q4_K_XL f16 d32K 209.3 150.5 0.72
batched 1 / 4 / 8 streams -kvu 74.8 / 178.9 / 279.7 79.1 / 170.7 / 283.9 within spread

The f16 small-batch case at 32K now runs flash_attn_ext_banded_f32_split, which is the default KV type and the shape of DSpark verify batches. Graph reuse is also lost for sequences that do not start at cell 0 (llama-graph.cpp:1483).

So I would not pin this as is. A version that keeps the position consistent on every fallback path (or keeps the mask when the new kernel does not run), keeps the bias cap from producing more chunks than the graph can hold, adds test-backend-ops cases for the position input and padded KV, and has an env switch would be much easier to take. Note it also rewrites lines owned by #385 and #386, so it would have to land inside #386.

…MA for Inkling 2

Builds on unslothai#386 (inkling2-attn-banded @ 06322ff) for quantized KV caches
(Q8_0/Q4_0) at >= 4096 global context while leaving F16/BF16 KV and dense
chunking limits untouched:

- GQA-3 dp4a+half2 split-K single-token banded decode (fattn-banded.cu) with
  optional device q_pos (src[7]) so CUDA graphs stay valid across positions;
  also honored in CUDA scalar/split fallbacks and CPU reference (ops.cpp).
- Quantized-KV-only routing for single-token banded decode (Q8_0/Q8_0,
  Q4_0/Q4_0, Q8_0/Q4_0) and small-batch banded MMA (2..32 tokens on >= 4K
  global caches), avoiding any F16/BF16 regression or dense kq_b spike.
- O(1) amortized seq_contig_prefix() check for c0 == 0 sequences, with matching
  c0_zero predicate in llm_graph_input_mem_hybrid_iswa_rel::can_reuse().
- Preserves n_kq_bias_max = 1<<29 so multi-stream -kvu graphs do not exceed
  the node budget.
- Adds GGML_CUDA_DISABLE_BANDED_DECODE=1 kill switch (bit-identical to base
  when set) and test-backend-ops cases for q_pos over padded KV (Q8_0, Q4_0,
  Q8_0/Q4_0, F16, BF16, Q5_1) and small-batch quantized banded MMA.
@danbunnell
danbunnell force-pushed the pr-inkling2-gqa3-banded-512k branch from 7e0186a to 888cc4e Compare November 23, 2026 04:38
@danbunnell danbunnell changed the title CUDA: GQA-3 dp4a+half2 banded decode, small-batch banded MMA & 128 MiB dense bias cap CUDA: quantized GQA-3 dp4a+half2 banded decode & small-batch banded MMA for Inkling 2 Nov 23, 2026
@danbunnell

Copy link
Copy Markdown
Author

Thanks for the detailed B200 review and catching every one of those issues. I've rebased onto inkling2-attn-banded (06322ffb0) and pushed an updated commit (888cc4e31) addressing all points:

  1. bf16 KV & CPU fallback correctness: Honored src[7] (q_pos) across all fallback paths (ggml/src/ggml-cpu/ops.cpp, CUDA flash_attn_ext_banded_f32_split, and flash_attn_ext_banded_f32), validated src[7] in ggml_cuda_flash_attn_ext_banded_supported + use_banded(), and gated inkling2.cpp's banded_decode strictly to Q8_0/Q8_0, Q4_0/Q4_0, and Q8_0/Q4_0 (leaving F16 and BF16 KV completely untouched on the #413/dense path).
  2. -kvu multi-stream graph node abort: Restored n_kq_bias_max = 1 << 29 and the original chunk rounding in inkling2.cpp.
  3. f16 pp8 -ub 8 regression: Restricted banded_small_long (2..32 tokens on >= 4K global KV) to quantized KV only, preserving the quantized small-batch speedup and 24 GB 512K VRAM savings without affecting F16 / DSpark verify batches.
  4. Graph reuse when c0 != 0: Matched the c0_zero predicate (and banded_small_long check) in llm_graph_input_mem_hybrid_iswa_rel::can_reuse().
  5. Kill switch & test-backend-ops: Added GGML_CUDA_DISABLE_BANDED_DECODE=1 (restores base graph & dispatch) and 11 new test-backend-ops cases covering q_pos over padded KV (Q8_0, Q4_0, Q8_0/Q4_0, F16, BF16, Q5_1) plus small-batch quantized banded MMA (n_q = 2, 8, n_kv = 4096).

Verified zero-conflict merge + clean CUDA build on top of b12635 + #419 (#411–#418). I'll post GPU test & benchmark results (including test-backend-ops with BF16, kill-switch KLD=0 check, and #419 vs #419 + #407 on RTX 3090) in a follow-up comment once I run them.

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.

2 participants