Skip to content

metal : banded flash attention for Inkling 2 (carries #404), decode kernels, quantized and mixed K/V caches - #431

Open
danielhanchen wants to merge 217 commits into
base/upstream-0df40737cfrom
inkling2-metal-banded
Open

danielhanchen wants to merge 217 commits into
base/upstream-0df40737cfrom
inkling2-metal-banded

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Metal FLASH_ATTN_EXT_BANDED for Inkling 2, so the prebuilt can pin it. It carries #404 by @skeggsguy with its original commits and authors, and adds decode kernels, quantized and mixed K/V caches on top. All credit for the Metal banded kernels goes to @skeggsguy; #404 stays open as the original PR.

Why one pin and not a mirror of #404

#404 changes four lines that #378 adds to tests/test-backend-ops.cpp (the test_flash_attn_ext_banded constructor, its vars() and the V tensor). A mirror of #404 with its commits unchanged would ship those rewrites, and pin_contract fails #378 in the merged tree (kept 76/80 of the lines this pin adds). A second pin on top that restores them would in turn rewrite the mirror's lines, which the pin model does not allow. So this PR carries #404's commits unchanged and folds the conflict in one commit of its own: the #378 lines stay exactly as #378 has them, and #404's extra test layouts move to a derived test_flash_attn_ext_banded_layout. Test case names are unchanged.

Commits

From #404 (unchanged, authors as in the original):

  • 58d6e4c5b metal : add FLASH_ATTN_EXT_BANDED in both attention kernels (@skeggsguy)
  • 982dee7a2 metal : do not reserve the vec and F16 K/V scratch for FLASH_ATTN_EXT_BANDED
  • 944cd7e55 metal : bound the FA pad copy to the K/V row width
  • 11f7c569f Merge inkling2-attn-banded (6348de2) into metal-banded-pr1
  • b64ef9d63 metal : refuse FLASH_ATTN_EXT_BANDED when K and V have different head counts
  • aa8d61c27 metal : banded FA, require mask width == n_kv and the row layouts the kernels load
  • 675186062, 6807804e0 comment-only
  • 081c9bbe4 metal : fix supports_op so the following don't crash for banded flash attention (@skeggsguy)

New here:

Base: inkling2-attn-banded (#386), the same base as #404. The branch is stacked on #386 at its pinned head ded02bdeb, so the diff here is only this PR's change: 17 files, +834/-120, all under ggml/src/ggml-metal/ plus tests/test-backend-ops.cpp. No CPU, CUDA or model code changes.

What it does

  • metal : add FLASH_ATTN_EXT_BANDED in both attention kernels #404: Metal banded attention in the half8x8 (M1 and later) and tensor (M5) prefill kernels. The relative bias is computed only inside the band, so Inkling 2 stops falling back to dense attention with a full F16 bias on Metal.
  • Decode and small batches: the banded bias in the vec and split-KV kernels.
  • q4_0, q4_1, q5_0, q5_1 and q8_0 K/V caches are dequantized inline by the half8x8 and vec kernels. Before, a quantized cache on Metal took the dense path, and the op reserved an F16 copy of the cache.
  • BF16 relative-bias tables where the device has bfloat.
  • Mixed K/V cache types (-ctk q8_0 -ctv q4_0, -ctk q8_0 -ctv f16 and so on) in the plain and banded FA kernels. On the current pins Metal refuses those and they run on the CPU.
  • supports_op refuses the K/V cell and query positions (src[7], src[8]) that model : banded flash attention for the Inkling 2 relative bias #386's next head (fd9663478) adds for several sequences and a wrapped SWA cache. The kernels take the distance from the cell index, so the model takes the dense path for those ubatches instead of a wrong band.

Evidence

Pin set (local, llama_pinset.py sim merge loop + pin_contract + merge_checks, then verify --cuda):

pin set b12674 b12675
unslothai master (#430) + this PR, #386 at ded02bdeb 45 pins merge, pin_contract 45/45, merge_checks clean same

verify --cuda on this set is running; results will be added here. With #386 at fd9663478 the merge is refused, but that is #386 against #378 (ggml-backend-meta.cpp), the same refusal without this PR, and is being fixed in #386.

GitHub macos-15 runner (Apple M1, virtual), on 235fc5071, before the test fold and before the #386 merge. The runner GPU reports MTLGPUFamilyApple5, so a CI-only, env-gated hook turns on simdgroup matrix and bfloat for the run (never committed):

  • test-backend-ops -o FLASH_ATTN_EXT_BANDED -b MTL0: 147/147 OK, 0 FAIL, 14 refused by design (BF16 K/V or rel tables without bfloat on the runner, V with other heads or length than K, a mask wider than K, 65536 queries).
  • test-backend-ops -o FLASH_ATTN_EXT -b MTL0: 5157/5157 OK. The base pins pass 5126/5126; the 31 new cases are the mixed K/V pairs, which the base refuses.
  • Tiny Inkling 2 (random weights, head size 128, llama-perplexity -c 256 -fa on, 10 K/V settings incl. mixed): KLD against the base pins at the floor (mean 0.000000), per-type KLD against F16 equal to the base pins.
  • Compute buffer at -c 32768 -ub 512, mixed q8_0/q4_0: 854 MiB on the base pins, 160 MiB here.

From #404 on real hardware (@skeggsguy, Mac mini M5 Pro 64 GB, Inkling-2-Mini UD-Q8_K_XL, F16 KV): prompt processing 1.21x at depth 4096 and 2.12x at depth 32768; Metal compute buffer at -c 200000 9291 MiB to 2664 MiB.

Memory

Before this PR, Inkling 2 on Metal took the dense path: a full F16 relative-bias mask per head, and for quantized K/V caches an F16 copy of the cache reserved by the op. Both are gone on the banded path, and quantized and mixed K/V caches are dequantized inline instead of copied.

setting without with this PR
tiny Inkling 2, -c 32768 -ub 512, q8_0 K / q4_0 V, Metal compute buffer (macOS CI) 854 MiB 160 MiB
Inkling-2-Mini UD-Q8_K_XL, F16 KV, -c 200000, Metal compute buffer (Mac mini M5 Pro) 9,291 MiB 2,664 MiB

The KV cache itself is unchanged, so a q8_0 or q4_0 cache now keeps its smaller size on Metal end to end, and mixed K/V settings such as -ctk q8_0 -ctv q4_0 stay on the GPU instead of running attention on the CPU.

Speed (macOS CI)

GitHub macos-15 runner (Apple M1, virtual), tiny Inkling 2 (random weights, head size 128), llama-bench, this PR's pins against the same pins without it (head f44881ab6, base b12675). Report-only, 2 rounds, medians:

K/V cache test without with this PR speedup
f16 pp512 depth 0 15,304 t/s 20,531 t/s 1.34x
f16 pp512 depth 4096 7,634 t/s 13,530 t/s 1.77x
f16 tg128 depth 0 301.8 t/s 307.9 t/s 1.02x
f16 tg128 depth 4096 260.2 t/s 279.2 t/s 1.07x
q8_0 pp512 depth 0 15,622 t/s 19,942 t/s 1.28x
q8_0 pp512 depth 4096 7,433 t/s 11,933 t/s 1.61x
q8_0 tg128 depth 0 283.9 t/s 283.4 t/s 1.00x
q8_0 tg128 depth 4096 240.6 t/s 248.8 t/s 1.03x
q8_0 K / q4_0 V pp512 depth 0 8,848 t/s 20,153 t/s 2.28x
q8_0 K / q4_0 V pp512 depth 4096 1,736 t/s 11,833 t/s 6.82x
q8_0 K / q4_0 V tg128 depth 0 148.7 t/s 283.3 t/s 1.91x
q8_0 K / q4_0 V tg128 depth 4096 144.4 t/s 242.2 t/s 1.68x

Mixed K/V types ran on the CPU before this PR, which is where the largest gains come from. The gated check, plain FLASH_ATTN_EXT decode at 4K/16K/64K for f16 and q8_0 over 7 interleaved rounds, passes: head over base 0.98 to 1.06, inside the base A/A spread. The same run passed FLASH_ATTN_EXT_BANDED 303/303, mixed K/V 45/45 and FLASH_ATTN_EXT 5543/5543 on macos-15 and macos-26, and the tiny-model KLD against the CPU is about 1e-6 for every K/V setting.

Earlier field measurement on a Mac mini M5 Pro (Inkling-2-Mini UD-Q8_K_XL, F16 KV) with the banded prefill kernels: prompt processing 1.21x at depth 4096 and 2.12x at depth 32768; Metal compute buffer at -c 200000 9291 MiB -> 2664 MiB.

Not tested

  • The Metal shaders after the model : banded flash attention for the Inkling 2 relative bias #386 merge (6600e1f7c, upstream ggml-org#32677 derived types) have not been compiled on macOS yet; the macOS numbers above are from 235fc5071. A macOS staging run on this head is pending.
  • The decode, quantized K/V, BF16 rel and mixed K/V kernels added here are untested on real Apple hardware and with the full model. They ran only on the virtual M1 runner, with the forced feature hook, on a tiny random-weight model.
  • The M5 tensor path of these additions has never run (the runner has no tensor API); it only compiles.
  • Decode and prefill numbers above are from the virtual M1 runner and a tiny model; no full-model Mac timing of the decode, quantized and mixed K/V kernels.

Lifetime

This pin is dropped once the work lands upstream (with Inkling 2 support in ggml-org) and the aged base tag the nightly picks contains it.

danielhanchen and others added 30 commits July 18, 2026 08:09
Hybrid attention model: 55 sliding-window plus 11 global layers, banded
content-dependent relative position bias instead of RoPE, per-layer short
convolution state, fine-grained MoE (256 experts top-6 plus 2 shared),
attention log-scaling past 128K, 1M context.

Includes the GGML_OP_FLASH_ATTN_EXT_BANDED operator (CPU and CUDA, fused
into the MMA flash attention kernel with an fp16 accumulator overflow
guard), HF to GGUF conversion, chat template with typed content block
parsing (interleaved thinking, narration and tool calls), mmproj vision
and audio support, and backend op tests at production shapes.
# Conflicts:
#	ggml/src/ggml-cuda/mmq.cuh
#	src/llama-model-saver.cpp
#	src/llama-vocab.h
Upstream 910196f renamed common_chat_params::thinking_end_tag to
thinking_end_tags and changed it from std::string to a vector, so
data.thinking_end_tag = END_MESSAGE no longer compiles.
# Conflicts:
#	src/llama-arch.cpp
# Conflicts:
#	ggml/include/ggml-rpc.h
#	ggml/src/ggml-backend-meta.cpp
#	ggml/src/ggml-cuda/ggml-cuda.cu
#	src/llama-model-saver.cpp
#	tests/test-llama-archs.cpp
# Conflicts:
#	ggml/src/ggml-rpc/ggml-rpc.cpp
Resolves the two conflicts left by upstream ggml-org#27764 (chat parsers split into
common/parsers) and ggml-org#26675 (ggml_prec rework):

- common_chat_params_init_inkling moves to common/parsers/inkling.cpp,
  registered in parsers.h and sources.cmake; chat.cpp keeps only the
  template detection branch.
- GGML_PREC_F32_PEDANTIC joins the new ranked ggml_prec scale at 5, below
  GGML_PREC_F32, since it is the stricter contract; ggml_flash_attn_ext_banded
  keeps its declaration next to the flash attention API.
…C_F32_PEDANTIC

Every caller of the deprecated ggml_mul_mat_set_prec /
ggml_flash_attn_ext_set_prec now uses ggml_prec_set_acc (the macOS prebuilt
leg builds with LLAMA_FATAL_WARNINGS, so the deprecation would be fatal
there), and ggml_prec_set_acc accepts GGML_OP_FLASH_ATTN_EXT_BANDED.

With the enum ranked, the CUDA cuBLAS compute-type gate and the CPU, spacemit
and Vulkan flash-attention accumulator checks compare by rank instead of
equality with GGML_PREC_F32, so a pedantic request never falls through to a
lower-precision path. The CUDA mul_mat_id slice carries the acc and src
precision slots, and ggml_cuda_mul_mat_id_needs_sync applies the same
pedantic gate as ggml_cuda_mul_mat_id so a pedantic F32 expert matmul cannot
trip its assertion.
Squash of the 56 commits since 3870aa4 (merges of master up to 8ca5a80, plus):
- chat : port the Inkling parser to the refactored chat API
- convert : check stacked ModelOpt NVFP4 experts after dequant
- inkling : trim comments
…own lines

Inkling shared its n_embd_r() check, the FALCON_H1 hybrid-memory branch and the
kv-cache context declaration with the line any new hybrid architecture also
edits, so every such PR (Inkling 2 in ggml-org#32217 among them) conflicts with this one.
Move each into its own block; behaviour is unchanged (RWKV never sets
n_embd_r_impl, and the Inkling branch is the FALCON_H1 body).
@danielhanchen
danielhanchen changed the base branch from base/upstream-a38695013 to base/upstream-288115043 November 30, 2026 09:52
… path

Since the FA bias cleanup (b12859) a dense KQ bias is cast to the mask type
and added to the mask, and the mask is F16 whenever flash attention is
enabled. Inkling routes multi-sequence ubatches through this path with an
F32 bias, so rounding it to F16 moved its outputs (Inkling-Small wikitext-2
-c 512: KLD 0.196, top-1 83.5%). Cast the mask to F32 instead for Inkling,
which restores the previous numbers exactly.
From ggml-org b12878 (one thread per row for soft_max on short rows) CUDA
computes rows of up to 8 on a new kernel whose rounding differs from the
block kernel by an ulp. Inkling's router softmax over 6 routed + 2 shared
experts is 8 wide, and on Inkling-Small IQ1_S that ulp grows to KLD 0.17
over the layers. Append a -inf column on CUDA layers so the row stays on the
block kernel, which then returns the same bits as before; other backends
and models are unchanged.
…OCm, MUSA and Vulkan

ggml-org b12916 (ggml-org#33197) gave Vulkan the same one-thread-per-row soft_max for
rows of up to 8 that CUDA got in b12878, and the CUDA kernel is also built for
ROCm and MUSA. Pad the router row on those backends too, so their results stay
the same as before the short-row kernels.
@danielhanchen
danielhanchen changed the base branch from base/upstream-288115043 to base/upstream-aa631e031 December 2, 2026 15:08
danielhanchen added a commit that referenced this pull request Dec 2, 2026
#378, #385, #386 and #431 merge upstream aa631e0 (FA bias API cleanup,
b12859); #378 also keeps the Inkling router soft_max off the new short-row
kernels (CUDA b12878, Vulkan b12916). #448 merges b12922 (few-row MMQ for
NVFP4 adds a dst argument to ggml_cuda_should_use_mmq_few).

feature-checks: add inkling2-expert-scales (MUL_MAT_ID with weight scales).
… b12878

ggml-org#33187 (b12878) also gave Metal a one-thread-per-row soft_max for rows
of up to 8 columns. Before it, Metal ran the 8-wide router row on its float4
kernel, which it only picks when the width is a multiple of 4, so a single
-inf column would move it to the scalar kernel and still change the sum
order. Pad to 12 on Metal (9 when the width is not a multiple of 4); the
other backends keep the single -inf column and an unchanged graph.
@danielhanchen
danielhanchen changed the base branch from base/upstream-aa631e031 to base/upstream-0df40737c December 3, 2026 20:34
danielhanchen added a commit that referenced this pull request Dec 3, 2026
fix #396, pin #469 #470 (#473)

* pr-set : pin #464, default a missing NVFP4 / FP8 weight scale to 1.0

* pr-set : repin #378 #385 #386 #431 #448 for upstream b12859..b12927

#378, #385, #386 and #431 merge upstream aa631e0 (FA bias API cleanup,
b12859); #378 also keeps the Inkling router soft_max off the new short-row
kernels (CUDA b12878, Vulkan b12916). #448 merges b12922 (few-row MMQ for
NVFP4 adds a dst argument to ggml_cuda_should_use_mmq_few).

feature-checks: add inkling2-expert-scales (MUL_MAT_ID with weight scales).

* pr-set : repin #378 to keep the Inkling router soft_max on the pre-b12878 Metal kernel

* pr-set : repin #378 #386 #431 for b12939 (rpc SUPPORTS_OP, BF16 supports_op), comment-only repins of #241 #243

* pr-set : drop #462, upstream took the same short conv fusion in b12942

* pr-set : pin #469 (greedy argmax of the logits row)

* pr-set : repin #418 for the upstream flat CONCAT kernel in b12957

* pr-set : pin #470 (CPU multi-row MUL_MAT_ID for few-row experts)

* pr-set : repin #396 (the model saver writes a shared host weight once)

* feature-checks : keep cuda-short-conv-in-place with owner null, upstream b12942 carries it

* pr-set : pin upstream's Qwen4 model support until the aged base carries it (b12969)
…d add_sinks

add_sinks is a deprecated wrapper that calls set_sinks, so behavior is identical; the
deprecated call fails macOS builds with -Werror -Wdeprecated-declarations.

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.

3 participants