Skip to content

vulkan: indexed-bias flash attention on every FA KV type, and on the coopmat1 path - #26

Open
Oxsilaris06 wants to merge 2 commits into
pwilkin:codex/inkling2-flash-attentionfrom
Oxsilaris06:inkling-indexed-bias-coopmat
Open

Oxsilaris06 wants to merge 2 commits into
pwilkin:codex/inkling2-flash-attentionfrom
Oxsilaris06:inkling-indexed-bias-coopmat

Conversation

@Oxsilaris06

Copy link
Copy Markdown

Follow-up to ggml-org#32740 (compact/indexed bias for FLASH_ATTN_EXT), targeting its branch. Two commits on top of ff91e4f.

The measurements below were taken with ggml-org#32740 merged on top of master together with ggml-org#32735 (SCALE on non-contiguous src) and ggml-org#32669 (.scale tensors). The two commits rebased on ff91e4f alone pass the same indexed-bias test (224 cases). Measured on an RX 9070 XT (RDNA4, RADV, KHR_coopmat),
model IsValorum/Inkling-2-Mini-Abliterated-APEX-I-MiniPlus-V2.1-GGUF
(inkling2_loop, 24 heads, 8 KV heads, head 128, SWA 1024, 48 layer visits).

Problem

Inkling 2 adds a relative position bias to every attention score. Without ggml-org#32740 the
bias is materialized as a dense f32 tensor of n_kv × n_ubatch × n_head, copied twice
(get_rows + cont). At 131k context and ubatch 64 that is about 1.6 GB, so ubatch 512
does not fit in 16 GB and prefill is capped at about 230 tok/s.

ggml-org#32740 fixes the memory side, but on Vulkan it had two limits:

  1. supports_op accepted the indexed bias only with F16 K/V. With a quantized KV cache
    (-ctk q4_0 -ctv q4_0, the common case on 16 GB cards) the graph fell back to the
    dense mask, so ubatch 512 at 131k still needed a 12.7 GB pinned host buffer and
    decode dropped to 21 tok/s.
  2. With a bias, the host forced the scalar FA shader. The scalar kernel tops out at
    about 6 TFLOPS on this GPU, so prefill falls off quickly with context depth
    (1553 tok/s at depth 0, 384 tok/s at depth 98k).

Patch 1: accept every FA KV type

The scalar FA shader already dequantizes K/V generically (dequantize4), including
the _rows variant, so the F16-only check in supports_op was not needed. It is
removed. The test tests/test-flash-attn-indexed-bias.cpp now takes the KV type and
loops over Q4_0, Q4_1, Q5_0, Q5_1, Q8_0, IQ4_NL, BF16 and F32. The expected result is
computed from the quantize → dequantize round trip, so the test checks the kernel and
not the quantization error. On Vulkan the test requires support (it fails if the op
falls back).

Patch 2: coopmat1 path

  • flash_attn_cm1.comp adds the bias while the mask tile is loaded, with the same f16
    rounding as the scalar kernel (packHalf2x16(mask + bias)), so the existing
    all-masked block skip and mask add keep working.
  • A KV_ROWS variant of the coopmat1 shader (flash_attn_f32_f16_rows_cm1, f32 acc)
    reads K/V through the row list. With a row list, K/V are always staged through shared
    memory (no direct coopMatLoad from the buffer), like the sparse path.
  • Bug found by the test and fixed: inactive row-list entries (-1) skipped the mask
    load too, so their score was 0 instead of masked. The mask is now indexed by list
    position, as in the scalar kernel.
  • Host: with a bias, the path comes from get_fa_tuning_params with GQA packing and
    flip disabled. It falls back to scalar when the result is not coopmat1, with f16
    accumulation, or with BF16 K and a row list (the BF16 coopmat1 shader has no
    KV_ROWS variant).

Tests

Test Result
test-flash-attn-indexed-bias (224 cases: 8 KV types × 1/7/65 queries × rows on/off × widths 64/128, Vulkan0 and Vulkan1) all pass, max error 9.8e-5 (threshold 1e-3)
test-backend-ops -o FLASH_ATTN_EXT -b Vulkan0 OK, 0 failures
test-backend-ops SCALE, MUL_MAT_ID (after patch 1) OK

Measurements (RX 9070 XT, KV q4_0, -b 2048 -ub 512)

llama-bench, pp512 @ depth:

Depth Before (scalar) After (coopmat1) Gain
0 1553 1615 +4 %
32768 709 1022 +44 %
98304 384 652 +70 %

GGML_VK_PERF_LOGGER, global-attention FLASH_ATTN_EXT at depth 98k: 102.8 ms per
call at 6.0 TFLOPS before, about 18 TFLOPS after.

llama-server, 184 320 context, one prompt, cache_prompt: false:

Prompt Before After Decode after the prompt
12.8k 1125 tok/s — 43.9 tok/s
70k 745 tok/s 1142 tok/s 39.7 tok/s
141k 513 tok/s 908 tok/s 34.7 tok/s
179k — 816 tok/s 31.6 tok/s

Whole-model comparison against the dense-bias build at 131k, ubatch 64:
prefill 227 → 1112 tok/s at 12.8k tokens, decode unchanged (46.6 → 47.2 tok/s),
compute buffer 1.6 GB → 578 MB, VRAM 15.0 → 14.0 GB. At 184k: VRAM 14.6 GB,
566 MB GTT, KV 1.62 GB, compute buffer 734 MB.

Long-context recall (retrieve line 7 of a 70k / 141k / 179k token list): exact at all
three lengths. Short quality checks (reasoning, Rust code compiled and tested,
French prose, 3-bullet instruction) pass, decode 47.5 tok/s.

Not covered

  • Coopmat2 (NVIDIA) and the scalar f16acc path keep the scalar indexed-bias kernel.
  • BF16 K with a row list stays on scalar.
  • Only RDNA4 + RADV measured; the RDNA3 double_block_rows and AMD proprietary driver
    q_in_regs branches are not exercised by these runs.

Host staging buffer (finding, not changed here)

The ~398 MiB Vulkan_Host compute buffer at 131k (566 MiB at 184k, which is also the GTT usage seen) is the CPU-filled inputs: attn_inp_rel_pos (I32, n_kv × n_ubatch, 268 MB at 131k) plus the f16 mask (134 MB). The bias table has 1025 bins, so I16 indices would halve the first tensor, and deriving the bin from query/key positions on the GPU would remove it.

…q8_0, q4_1, q5_x, iq4_nl, bf16, f32), test covers them
The indexed-bias kernel was limited to the scalar shader, which caps at about
6 TFLOPS on RDNA4 and makes prefill fall off with context depth. The bias is
now added while the mask tile is loaded in flash_attn_cm1.comp, matching the
scalar kernel's f16 rounding, and a KV_ROWS variant of the coopmat1 shader
reads K/V through the row list. Inactive list entries keep the mask so they
stay masked. BF16 K with row lists and f16 accumulation keep the scalar path.
@Oxsilaris06
Oxsilaris06 force-pushed the inkling-indexed-bias-coopmat branch from b680664 to d38e73d Compare November 21, 2026 23:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant