llama : decline the KV precision tail when no device serves it natively - #144
Open
yogthos wants to merge 1 commit into
Open
llama : decline the KV precision tail when no device serves it natively#144yogthos wants to merge 1 commit into
yogthos wants to merge 1 commit into
Conversation
A positive --kv-tail-tokens request is only a win where some device can run the tail with native tail attention. Where none can, planning still succeeds: every layer falls back to the generic tail route, which llama-kv-cache.cpp already logs as "catastrophic generic attention". The result is a large throughput loss for a flag the user set expecting the opposite. The Metal backend exports no tail-attention entry point at all, so every Metal build pays it in full. Measured on an M1 Max, Qwen3.8-27B IQ4_XS, 100k ctx, -ctk q5_0 -ctv q5_0, same command line both times: --kv-tail-tokens 1024, generic route 26.3 tok/s prompt 4.66 tok/s eval --kv-tail-tokens 1024, tail declined 118.5 tok/s prompt 9.28 tok/s eval Decline the tail instead, the way KVarN already declines itself a few lines below when its runtime requirements do not hold. LLAMA_KV_TAIL_ALLOW_GENERIC=1 keeps the old behaviour; an env var rather than a new field so there is no public struct or CLI change. Backends that do implement it (CUDA, Vulkan, CPU) are unaffected: the check only fires when no device backing a KV layer exports any tail-attention entry point.
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.
Overview
--kv-tail-tokens Nis only a win when some device can run the tail with native tail attention. When no device can, planning still succeeds and every layer takes the generic tail route.llama-kv-cache.cppalready logs that route as "catastrophic generic attention", and the run then continues at a large throughput cost for a flag that was set to improve quality.The Metal backend exports no tail-attention entry point at all, so every Metal build takes the generic route whenever a tail is requested.
grep -rl ggml_backend_kv_tail_segmented_attention_supported ggml/src/returns CUDA, Vulkan and CPU, and nothing underggml-metal.This adds a check in
llama_context::llama_contextbefore the cache is built. If no device backing a KV layer exports any tail-attention entry point, the tail is declined and the reason is logged.LLAMA_KV_TAIL_ALLOW_GENERIC=1keeps the previous behaviour. An environment variable was used so there is no change tollama_context_paramsor to the CLI.This follows the KVarN block a few lines below, which already declines itself when its runtime requirements do not hold, and the AGENTS.md invariant that unsupported placements fail closed rather than silently degrade.
Backends that implement tail attention are unaffected. Only the presence of the entry point is checked, so the per-layer planner still makes every finer decision. Loading with
-ngl 0puts the layers on CPU, which exports the entry point, and the new path stays quiet.Additional information
Measurements from the same binary and the same command line. The only difference between the two rows is the environment variable.
Qwen3.8-27B-Q8_0.ggufmax_tokens160--temp 1.0 --top-p 0.95 --top-k 20 --min-p 0.0 --presence-penalty 0.0 --repeat-penalty 1.0LLAMA_KV_TAIL_ALLOW_GENERIC=1With the tail declined the "catastrophic generic attention" warning no longer appears, and the new line reads:
One separate observation found while measuring this, not addressed here. On Metal,
--cache-type-kand--cache-type-vmust be the same type or flash attention is refused, becauseggml-metal-device.mrejectsGGML_OP_FLASH_ATTN_EXTwhensrc[1]->type != src[2]->type. Theq5_0andq4_1pair in the AGENTS.md example is fine on CUDA and silently costs flash attention on Metal. Both problems have to be fixed to reach the numbers above. I can open that separately if it is wanted.Requirements