Repository navigation
turbo5 and turbo6 KV cache types - #381
Conversation
TheTom
left a comment
There was a problem hiding this comment.
Thanks for this, the codec side is careful work. The tq6 table is sorted and symmetric, the tq5 table is exactly antisymmetric, and the CUDA and CPU tables and midpoints match in f32. A 512k-value CPU round trip hit every code with zero mismatches, and the tq5 prmt loader, the tq6 shuffle lookup and the flat turbo4 loader all matched scalar references over 20,000 random chunks. The llama-side wiring is also right on the CPU reference: q8_0 K with tq6_0 V gives 8.12 PPL on Qwen2.5-0.5B against 8.11 for q8_0/q8_0.
A few things need to change before this goes in. I also ran the CUDA side on a GB10, and those numbers are at the bottom.
Before merge
-
tq5 sign bits are wrong on wave64 HIP.
k_set_rows_tq5packs the signs with__ballot_sync, andvendors/hip.h:55maps that to(uint32_t)__ballot(var). On CDNA (wave64), lanes 32 to 63 get the low 32 bits of the ballot. So qh bytes 4-7 and 12-15 come out as copies of bytes 0-3 and 8-11, and half the signs in every block are wrong. The turbo3 writer hit the same trap and uses a width-32 shuffle instead (see the comment at set-rows.cu:393). Reusing that idiom, or shifting the 64-bit ballot bylane & 32, fixes it. The last commit builds the HIP vec instances, so MI210 and MI300 can reach this path. The tq6 writer only uses width-32 shuffles and is fine. -
Keep tq K under the auto-asymmetric rule, or show dense high-GQA data. That rule exists because of Qwen2.5 at GQA 7:1; the comment above it in
llama-kv-cache.cpphas the numbers. Qwen2.5-0.5B has 14 query heads over 2 KV heads, and head dim 64 (padded to 128). Perplexity over 4 x 512 tokens:
| K / V | CPU | CUDA (GB10) |
|---|---|---|
| f16 / f16 | 8.02 | 8.04 |
| q8_0 / q8_0 | 8.11 | 8.09 |
| f16 / tq6_0 | 8.03 | 8.03 |
| q8_0 / tq6_0 | 8.12 | 8.08 |
| tq6_0 / f16 | 21.99 | 22.84 |
| tq6_0 / tq6_0 | 22.35 | 22.44 |
| tq5_0 / tq5_0 | 31.64 | 31.80 |
| q5_1 / q8_0 | 16.10 | not run |
| turbo4 / turbo4, K upgraded to q8_0 by the rule | 8.20 | 8.11 |
turbo4 / turbo4, TURBO_AUTO_ASYMMETRIC=0 |
52.66 | not run |
The error bars are about +/- 0.69 on the rows near 8 and +/- 2.2 to 3.2 on the tq K rows, and CPU and CUDA agree, so this is the codec on this model and not a kernel bug.
tq6_0 as V costs nothing. As K it nearly triples perplexity, and it does worse than q5_1 K at about the same size. This is a small model and a hard case, but it is the case the rule was written for. The Qwen3.8-27B numbers in the description come from a hybrid model where only some layers have attention, which dilutes K error. On Qwen3.6-35B-A3B, a hybrid with GQA 8:1 where 10 of 40 layers are attention, every K type I tried on the GB10 lands inside the error bars over 6 x 2048 tokens: f16/f16 3.356, tq6_0/tq6_0 3.364, tq5_0/tq5_0 3.377, and turbo4 K with the rule turned off 3.378, all +/- 0.09. A hybrid model can't show this problem either way, even for turbo4 K. Until there's a dense GQA 6+ result (Qwen2.5-7B is the one the rule cites), please add tq6_0 and tq5_0 to the rule's K check, and drop "indistinguishable from q8_0" from the docs.
-
Naming. In this fork
turboNmeans a KV cache type and thetqprefix means a weight type (TQ3_1S, TQ4_1S). Upstreamtq1_0andtq2_0are ternary weight types too.tq5_0is also one letter away fromq5_0, and both are valid-ctkvalues. The mix-up already bites:llama-quantize --allow-requantize --tensor-type attn_k=tq6_0writes a GGUF with type-51 weights. That file loads with no warning on CPU (PPL 8998 against 8.02) and aborts on Metal with "Asserting on type 51". Your own comment says the codec is the turbo4 pipeline with a bigger codebook, so please rename toturbo5andturbo6(GGML_TYPE_TURBO5_0andGGML_TYPE_TURBO6_0, keeping enums 51 and 52, listed in ascending order). Renaming now costs nothing, while renaming after merge breaks people's flags. Rejecting KV-only types inllama-quantizeand the model loader is a separate follow-up, because turbo2/3/4 have the same hole. -
Non-CUDA builds abort at startup. On Metal with
-ngl 99,-ctk tq6_0 -ctv tq6_0dies during context creation:pre-allocated tensor (cache_k_l0 (reshaped) (view)) in a buffer (MTL0) that cannot run the operation (SET_ROWS)With
-nkvoit runs. From reading the code, Vulkan and SYCL take the same path. The description says these work "like the other turbo KV types", but turbo2/3/4 have kernels on those backends. At minimum, fail inllama_init_from_modelwith a clear message when the KV cache backend can't run SET_ROWS for the type, and fix the doc. The-ctkhelp also lists the new types on every build. -
The test plan doesn't run the new kernels.
-omatches the op name exactly, so-o SET_ROWSruns the 159 generic cases and none of the SET_ROWS_TQ6 or SET_ROWS_TQ5 cases. There is no tq GET_ROWS test case at all, CUDAsupports_opdoesn't advertise GET_ROWS for tq, and CPU GET_ROWS aborts on tq6_0 ("unsupported GET_ROWS source ... type = 51"). So the "GET_ROWS works" part of the description isn't true yet. The new SET_ROWS cases do pass when you name them; the commands I used are in the GB10 section. -
Rebase. Since #372 landed today, this conflicts with the base in
fattn-vec.cuh. Both sides insert at the same line, so keep your tq declarations above the new#ifdef GGML_USE_HIPblock. The rebase also matters for CUDA 13 users: on this branch's current base, an sm_89 build with nvcc 13.0 still fails ptxas on the old D=512 vec instances (#380), and #372 is the fix. #357 (block KV streaming) is about to land too, and it conflicts in llama-kv-cache.cpp, llama-context.cpp and set-rows.cu. It moves the layer-adaptive logic into helpers that list only turbo2/3/4, so tq needs adding there after the rebase.
Worth settling before merge
- Build cost. Nothing new is behind
GGML_CUDA_FA_ALL_QUANTS.fattn.cugoes from 87 to 135FATTN_VEC_CASEentries, and the 14 turbo MMA instance files go from 42 to 119 cases, with each D=256 file going from 3 to 10. The hostedcudajob took 34 minutes on both of this PR's runs, against 18 to 29 minutes on the last dozen runs from other branches, and that job only builds sm_89. I'd put the speculative cross pairs behind FA_ALL_QUANTS (turbo, f16 or q8_0 K with a tq V; tq K with turbo2 V; tq5 K with tq6 V) and keep the default set to the pairs you measured. - Register spills. I compiled the flash attention instance objects for sm_89 with nvcc 13.0 and
-Xptxas=-v. The fused MMA tq kernels don't spill. 80 of the new vec kernels sit at the 255-register cap and spill. At D=256 with two columns they average 135 bytes and peak at 196, against a 144-byte peak for the existing turbo3-V kernels in the same shape. The existing turbo vec kernels already spill at D=256, so this isn't a blocker, but it's another reason to keep the default set small. - bf16 abort.
is_kv_compatincludes BF16, so tq with bf16 in either order passessupports_op, then hitsGGML_ABORT("fatal error")at decode because there's no vec instance. tq with q4_0, q4_1, q5_0, q5_1 or iq4_nl does the same under FA_ALL_QUANTS. turbo with bf16 already had this hole, and this PR widens it. - Evidence. In your KLD table q5_1/q5_1 ties tq6_0/tq5_0. It would help to see q5_0 and q5_1 K with the same V, a dense-attention model, and plain decode, prefill and VRAM from
llama-bench -dnext to the MTP numbers. Please also include the exact KLD command, text, context length and token count, and raw means with intervals in place of "at the floor".
Smaller things
quantize_f32_tq{6,5}_0_blockin the CUDA code are dead and never writenorm.NL_TQ6,NL_TQ6_VEC,NL_TQ5,NL_TQ5_VEC,QK_TQ6_GROUPandQK_TQ5_GROUPare unused.- The tq5 loader and the flat turbo4 loader hardcode copies of the centroid values, which can drift if a table changes.
- turbo4 is 4.125 bits per value (66 bytes per 128 values), not 4.25. The old docs had that wrong too.
- The description says head dim 64 isn't supported, but the llama side pads 64 up to 128. That padding is how the Qwen2.5-0.5B runs above work.
GB10 (sm_121) on the PR head, test-backend-ops -b CUDA0
| run | result |
|---|---|
-o SET_ROWS_TQ6 |
21/21 |
-o SET_ROWS_TQ5 |
21/21 |
-o CPY -p 'tq[56]_0' |
4/4 |
-o FLASH_ATTN_EXT -p 'tq[56]_0' |
1777/1777 |
same, with GGML_TURBO_MMA_FUSED=0 |
1777/1777 |
-o GET_ROWS -p 'tq[56]_0' |
0/0, no cases exist |
-o FLASH_ATTN_EXT, all cases |
8774/8774 on base, 10551/10551 here |
-o SET_ROWS / CPY / GET_ROWS |
159, 256 and 119 on base; 159, 260 and 119 here; all pass |
So the CUDA kernels pass every test that exists for them on warp32 hardware, and the existing types are unchanged. The wave64 bug in item 1 can't show up on this card. Your SET_ROWS_TQ6/TQ5 cases can run on CUDA because this PR adds the CPY they end in. The turbo ones can't, which is now #385.
5f3fcd9 to
84a6285
Compare
|
Thanks for the thorough review and for running it on the GB10. I rebased onto the current base (after #372) and pushed 84a6285b5 with the fixes. Point by point: Before merge
Worth settling
Smaller things
RTX 3090 Ti (sm_86),
The 4 fewer FA cases than your 1777 / 10551 are the pairs now behind ALL_QUANTS (turbo6/turbo2, turbo5/turbo6, turbo4/turbo5, f16/turbo5 in the test matrix). A |
|
Dense results are in (Qwen2.5-7B-Instruct Q8_0, wikitext-2, 20 x 2048,
As V types they're free; as K types they're not, so turbo5/turbo6 K stay under the auto-asymmetric rule as pushed. The table is also in the description. I've dropped the q5_x K arms and the llama-bench run from this PR's scope. From my side the PR is ready for another look. |
|
GB10 (Spark, sm_121) numbers for 84a6285b5, default build. The op tests all pass: SET_ROWS_TURBO6 and SET_ROWS_TURBO5 are 21/21 each with the new shuffle-based writer, CPY is 4/4, and FLASH_ATTN_EXT turbo5/6 is 1773/1773 both fused and with KL divergence against an f16 KV cache, wikitext-2. Qwen3.6-35B-A3B Q4_K_M (hybrid, GQA 8:1, head dim 256), 8 x 4096:
Qwen2.5-0.5B-Instruct Q8_0 (dense, GQA 7:1), 16 x 2048, rule off. The last column keeps the first 4 and last 4 layers at q8_0 with
Decode on the 35B, llama-bench tg128 in t/s:
Prefill is flat across all of them, 1763 to 1810 t/s for pp512 at depth 32768. Three things from the numbers:
Not a blocker for this PR, but the q8_0 K + turbo5/6 V decode speed is worth a look, either with a fused MMA instance for those two pairs or a faster V lookup on the vec path. How the rule should treat hybrid models is a question for the fork, separate from this PR. #357 has landed, so this needs a rebase and turbo5/turbo6 added to the kv-stream helpers, as you planned. |
84a6285 to
8aaa886
Compare
|
I've been running this PR on top of #392 on two laptops. On one the KV cache spills to host RAM; the other also serves from the CPU and a Radeon 780M iGPU over Vulkan. Getting turbo5/turbo6 usable in those setups took a few more changes, so I'm listing them here to avoid duplicate work. They depend on both this PR and #392, so I'd send them as a small follow-up once both land rather than push them into this one. Branch with the whole stack (this PR merged onto #392, then the commits below): grogroningem-code/llama-cpp-turboquant kv-stream (CUDA)
Vulkan. The description says CPU + CUDA only; this adds Vulkan.
CPU
Radeon 780M (Vulkan, coopmat1), Qwen3.5-4B, llama-bench, t/s:
Zen 4 CPU, 8 threads, t/s, before -> after:
Qwen3.8-27B (hybrid, 16 of 64 layers full attention, GQA 6:1, D=256), RTX 4090 Laptop 16 GB, 180k context spilled to host, t/s:
Decode here is with MTP on a repetitive prompt (acceptance 169/170), so the absolute numbers are optimistic. The order holds. KLD against an f16 cache on the same 27B model, 8k context (4 chunks) and 180k context (2 chunks, last 8192 tokens of each scored):
The 180k corpus is easier than the 8k one (f16 PPL 1.69 against 4.39), so compare within a column, not across. These are hybrid-model numbers and say nothing against your dense results. None of this changes the auto-asymmetric rule.
Still open on my side:
Written with Claude Code (Claude Opus 5.5): code, benchmarks and this write-up. Everything above was run on my own hardware. |
|
#392 just landed (native turbo routing for kv-stream, the faster turbo3/turbo4 to f16 converter, CPU tiled flash attention for quantized K/V, and turbo off the Vulkan coopmat2 path). This branch now conflicts with
Could you rebase onto the current tip? Once it's green I'll rerun the GB10 checks from my last comment on the new head, existing turbo types and speed included, before it goes in. grogroningem-code's turbo5/turbo6 follow-up for kv-stream, Vulkan and CPU can come after this lands. |
|
Status from my side: holding this for more investigation before merge, on top of the rebase above. The kernels themselves look good. What I want to settle first is how turbo5/turbo6 behave once the auto-asymmetric rule gets involved, because that decides whether users ever see the good case. Recap of the GB10 numbers posted earlier (Qwen3.6-35B-A3B, hybrid, GQA 8:1, head dim 256; KLD against an f16 cache, decode tg128 at 32K):
Concerns:
What I want to investigate before deciding: whether the rule should key on the model's attention structure (hybrid vs dense) instead of GQA alone, and whether the q8_0 K + turbo5/6 V decode gap closes with a fused path. I'll post findings here. |
Two TurboQuant PolarQuant KV cache types that sit between turbo4_0 and
q8_0: a WHT rotation over the 128-element head group, then a Lloyd-Max
codebook for N(0, 1/128) plus a per-block corrected L2 norm, the same
pipeline turbo2_0 / turbo3_0 / turbo4_0 already use.
tq6_0: 64 centroids, norm + 4-bit low plane + 2-bit high plane,
98 B per 128 values = 6.125 bpw, per-coordinate rmse 0.025 sigma
tq5_0: 32 antisymmetric centroids stored sign-magnitude (4-bit
magnitude index + sign bit), 82 B per 128 values = 5.125 bpw,
per-coordinate rmse 0.050 sigma (turbo4_0: 0.097 sigma)
Both keep the low nibble plane in the element order of block_turbo4_0.qs
so the existing nibble readers and tile loaders carry over; a dense 6-bit
packing would put element boundaries at bit 6/12/18 and break the
half2-pair extraction the KQ dot and the tile loaders rely on.
This commit adds the type enum entries, the block structs, the reference
codec, the CPU backend traits and the round-trip tests. Like the other
TurboQuant KV types, dequantization stays in the rotated domain; the
inverse WHT is applied to the attention output by GGML_OP_TURBO_WHT.
Device-side codec for the two new KV cache types, mirroring the CPU reference: the Lloyd-Max tables, the nearest-centroid search, the per-block quantizer and the per-element dequantizer. SET_ROWS quantizes an f32 KV row in place with the same warp-cooperative shape the turbo types use: one warp per 128-element rotation group, a warp-level WHT, a warp reduction for the norm and for the norm correction, then a ballot to pack the high plane (tq6) or the sign plane (tq5) in element order. CPY and the to_fp16 / to_fp32 converters give the read-back path used by GET_ROWS and by KV cache defragmentation. test-backend-ops gains SET_ROWS round-trip cases for both types over the index types, head dims and row counts the KV cache uses, plus CPY dequant cases.
Add the tq6_0 / tq5_0 KQ dot products and V dequantizers to the VEC path and tile loaders to the GQA-packed MMA path, so both types can be used as K and as V caches. VEC: every combination of tq6_0 / tq5_0 with f16, q8_0, turbo2_0, turbo3_0, turbo4_0 and with each other is instantiated, for head dims 128 and 256 (a tq6_0 / tq5_0 block holds 128 values, so head dim 64 has no instance). MMA: the matched tq6_0 / tq5_0 pairs and tq6_0 / tq5_0 K over a turbo3_0 V are fused at head dims 128 and 256; turbo4_0 V under a tq6_0 / tq5_0 K and tq5_0 V under a tq6_0 K are fused at head dim 256. The tq6_0 loader keeps the 32 positive centroid magnitudes in one register per lane and looks them up with a single warp shuffle; the tq5_0 and turbo4_0 loaders build a per-block fp16 magnitude table and select from it with PRMT, decoding 32 values per lane and storing four aligned uint4 into the swizzled tile.
Accept tq6_0 and tq5_0 for -ctk/-ctv (and the draft variants) and for llama-bench's -ctk/-ctv, and add them to every KV-cache predicate that already lists the turbo types: flash attention auto-enable, head-dim padding and block-size validation, WHT rotation tensor creation, the Q pre-rotation and the inverse WHT on the attention output. The auto-asymmetric rule deliberately keeps its turbo2/3/4 condition: at 5-6 bits the K error is small enough that a high GQA ratio does not need the upgrade to q8_0, so a tq6_0/tq5_0 K is left as requested. Document both types in docs/KV-cache-quantization.md, including the block layouts, the supported head dims and the pairs that take the fused GQA-packed MMA kernel.
The HIP backend keeps its own list of fattn-vec template instances; the TQ6_0/TQ5_0 instances were only added to the CUDA list, so libggml-hip failed to link (undefined ggml_cuda_flash_attn_ext_vec_case<..., 51/52>).
- Rename tq5_0/tq6_0 to turbo5/turbo6 (GGML_TYPE_TURBO5_0 = 51,
GGML_TYPE_TURBO6_0 = 52, ggml type names and -ctk values turbo5/turbo6).
- k_set_rows_turbo5: pack the sign bits with the width-32 shuffle idiom
instead of __ballot_sync, which truncates to 32 bits on wave64 HIP.
- Auto-asymmetric rule: turbo5 and turbo6 K are upgraded to q8_0 at
GQA >= 6 like turbo2/3/4.
- FA vec: the unmeasured cross pairs (turbo2/3/4 or f16 K with a
turbo5/6 V, turbo5/6 K with turbo2 V, turbo5 K with turbo6 V) move
behind GGML_CUDA_FA_ALL_QUANTS. q8_0 K with turbo5/6 V stays in the
default build since the auto-asymmetric rule produces it.
- supports_op: report no FA kernel for turbo5/6 pairs without a vec
instance (bf16, q4/q5, iq4_nl), and for bf16 mixed with anything but
q8_0 in the default build, instead of aborting at decode.
- KV cache creation fails with a clear error when the device holding a
layer cannot run SET_ROWS for the cache type (Metal, Vulkan, SYCL).
- Remove the dead quantize_f32_turbo{5,6}_0_block and unused NL_/GROUP
defines; the turbo5 and flat turbo4 tile loaders read their centroids
from the tables instead of literal copies.
- Docs: bits per value for turbo2/3/4 (2.125/3.125/4.125), backend
support, head dim padding, supported pairs, the auto-asymmetric rule.
ggml-org#357 (kv-stream phase arena) added a set of helpers that enumerate the KV cache types the streaming runtime understands. turbo5_0/turbo6_0 were added on a branch that predates it, so they were missing from all of them: - fattn.cu: kv_stream_resolve_native_partial{,_for_v} (K switch and V switch plus the k_is_turbo gate), the classified/online_write/decode_f16/ direct_attention capability switches, kv_stream_direct_attention_pair_ supported and ggml_cuda_kv_stream_padded_head_dim (turbo5/turbo6 pad head dims to a multiple of 128 exactly like turbo4). - ggml-cuda.cu: ggml_cuda_kv_stream_staged_set_rows_range must reject turbo5/turbo6 too. set_rows_cuda_turbo5/6 ignore the row-base offset the page-staging path passes, so without this the staged path would have written every dirty row at the wrong offset. - llama-kv-cache.cpp: llama_kv_cache_resolve_stream_type_k and the llama_kv_cache_turbo_layer_adaptive_type_k/_v helpers the streaming bootstrap pre-scan calls. - tests/test-kv-stream-cuda-attn.cpp: turbo5/turbo6 in the turbo direct-attention pair test (HEAD_DIM is 256, one of the two head dims they have vec instances for), and backend_supports_plain_kv_pair now mirrors ggml_cuda_fattn_turbo56_pair_supported so default builds skip pairs with no instance instead of failing. Also fix fattn-vec.cuh: ggml-org#357 made DECL_FATTN_VEC_CASE expand to two explicit instantiations (the ordinary case plus the new partial_case), so the turbo5/turbo6 `extern DECL_FATTN_VEC_CASE(...)` lines made the partial_case an explicit instantiation definition in a header. Use the new EXTERN_DECL_FATTN_VEC_CASE for all 48 of them.
ggml-org#392 lets fully resident (or single-page) streamed runs skip the per-page f16 conversion when a turbo type is on either side. Its turbo lists named only turbo2/3/4; add turbo5/turbo6 so they take the same native path. Pairs without a vec instance in this build still fail the ggml_cuda_flash_attn_ext_supported check and keep the f16 conversion. The resident multi-page test now covers the turbo5/turbo6 pairs that the default build instantiates.
On the vec path, q8_0 K with turbo5/turbo6 V (the pair the auto-asymmetric rule produces from turbo5/turbo5 and turbo6/turbo6 on GQA >= 6 models) dequantized V through the generic per-element helper, which indexes the 32/64-entry centroid table in __constant__ memory. Constant memory serializes a warp over every distinct index, so each V lookup cost up to 32 passes; turbo4 V avoids this with a 16-entry register table. Copy the table to shared memory once per block and decode 4 values per thread from 2 qs bytes plus 1 qh byte, like the turbo4 V path. The products are the same c[idx] * norm as before, so outputs are unchanged. Applies to the f32 V path (CUDA); the half2 path (HIP with V_DOT2_F32_F16) keeps the generic helper.
8aaa886 to
ccd0e40
Compare
|
Rebased onto 6e09de1 (after #392) and pushed. Conflicts were in convert.cu (kept #392's fast turbo4 converter and added turbo5/6 next to it) and llama-kv-cache.cpp (kept the kv-stream rotation context, with turbo5/6 added to its check). #392's native-routing lists in On (3): you were right that it's the V lookup. The generic turbo5/6 V helper indexed the 32/64-entry centroid table in On (1), (2) and (4): agreed that these are rule questions for the fork, and I haven't changed the rule in this PR. One data point for the hybrid question: on Qwen3.8-27B (hybrid, 16 of 64 layers attend, GQA 6:1) turbo6/turbo6 with no K upgrade measured 0.00036 and 0.00074 nats of generated-token KLD on two of our harnesses, against 0.00025 and 0.00043 for q8_0/q8_0 on the same runs, so about 1.4-1.7x the q8_0 floor and nowhere near the dense-model breakdown. If the rule ends up keying on attention structure, I'm happy to run the dense and hybrid arms here. For (4), applying the rule to mismatched pairs would also need a check that the upgraded pair (e.g. q8_0/f16 from turbo K + f16 V) has a default vec instance. |
|
Hi Tom, friendly check-in. This merges cleanly with the current feature branch tip (ca28b4b), no rebase needed. When you get to the hybrid-versus-dense question for the auto-asymmetric rule, I am happy to run whichever hybrid model and context lengths you want and post the numbers. The failing self-hosted checks look like runner timeouts (about 2h57m each); could you re-run them? Thanks for the careful review. |
|
Checked this against the current tip. Hosted jobs on The V table fix is the right call. Staging the centroids in shared memory and decoding 4 per thread matches what turbo4 already does, and q8_0/turbo6 going from 36.5 to 70.7 t/s at 16K on the 3090 with byte-identical greedy output is a clean result. One small ask: say in the description that the speedup is CUDA only, since the HIP path still uses the generic helper. No port needed. The remaining step on this PR is mine: I'll rerun the GB10 rows from my earlier comment on this head. The hybrid vs dense question for the rule is not part of this PR. I split it into #404 and you're welcome to pick it up there. |
787b404
into
TheTom:feature/turboquant-kv-cache
Turbo5/turbo6 (51/52) landed in TheTom#381 with 82/98 byte blocks, so the type tables, the KV cache size table and the backend matrix list all ten fork types. The flag parser accepts turbo5/turbo6; only CPU and CUDA/HIP have kernels, and Metal's three mixed K/V pairs are spelled out. docs/KV-cache-quantization.md is now the source of truth for the env knobs (TQ_NATIVE, FUSE_CHAIN, Q8CACHE and ROT_K_NROT were missing) and the TURBO_LAYER_ADAPTIVE auto default is corrected there. README and AGENTS point at it instead of claiming it is stale.
turbo5 and turbo6 KV cache types
Summary
Two new runtime-only KV cache types that sit between
q8_0andturbo4:turbo5f16norm + 64 B 4-bit magnitudes + 16 B sign bitsturbo6f16norm + 64 B 4-bit low plane + 32 B 2-bit high planeBoth reuse the turbo4 pipeline with a larger codebook: the fixed 128x128 WHT
rotation, a Lloyd-Max codebook for the rotated N(0, 1/128) distribution, and a
per-block corrected L2 norm.
turbo6has 64 centroids (per-coordinate rmse0.025 sigma),
turbo5has 32 antisymmetric centroids (0.050 sigma). Because theturbo5codebook is antisymmetric it is stored sign-magnitude, a 4-bitmagnitude index plus a sign bit, which makes the CUDA decode a single
byte-permute lookup instead of a 32-entry gather.
Motivation
turbo4at 4.125 bits is the current ceiling belowq8_0's 8.5. The two newtypes fill that gap, mainly as V types and as K types on models where the
auto-asymmetric rule does not apply (GQA below 6, or hybrid models). On dense
high-GQA models they fall under the auto-asymmetric rule like turbo2/3/4, see
Evidence.
Design
Codec:
ggml/src/ggml-turbo-quant.cholds the centroid tables and thereference quantize/dequantize, wired into
ggml.ctype traits, the CPUbackend vec_dot and
ggml_quantize_chunk. CUDA gets the same codec inggml-cuda/turbo-quant.cuhplusSET_ROWS,CPYand dequant paths. There isno
GET_ROWSfor these types, as for turbo2/3/4.Flash attention, CUDA:
turbo5/turbo6K with Vof the same type,
turbo3,turbo4,q8_0orf16;turbo6K withturbo5V;q8_0K withturbo5/turbo6V (what the auto-asymmetric ruleproduces).
GGML_CUDA_FA_ALL_QUANTSadds the remaining cross pairs withf16,q8_0andturbo2/3/4.fattn-mma-turbo.cuh):turbo5/turbo5,turbo6/turbo6,turbo5/turbo3,turbo6/turbo3at head dims 128 and256, plus
turbo5/turbo4,turbo6/turbo4andturbo6/turbo5at headdim 256. As with the existing turbo pairs the fused path is used for
Q->ne[1] <= 4and otherwise falls back to vec.turbo6uses a warp shuffle over the 64-entry table;
turbo5uses a 16-entryprmtmagnitude table plus the sign bit. A
turbo4V under aturbo5/turbo6K isdecoded by a flat
prmtvariant of the turbo4 loader;turbo4/turbo4isuntouched. Both loaders build their tables from the centroid arrays.
supports_opreports no kernel for any pair without a vec instance, so anunsupported pairing (bf16, q4/q5, iq4_nl) is rejected up front instead of
aborting at decode. The same check now also covers bf16 mixed with anything
but
q8_0in the default build, which already affected turbo2/3/4.as for the other turbo types, so head dim 64 models work.
llama side:
-ctk/-ctv(and the draft variants) acceptturbo5andturbo6,llama-benchparses them, and both types join every predicate thatalready lists the turbo types (FA auto-enable, head padding, rotation tensors,
Q pre-rotation, inverse WHT on the FA output, the auto-asymmetric rule).
Backends: CPU and CUDA (HIP builds the same kernels; the
turbo5sign packinguses width-32 shuffles so it is wave64-safe, but it is untested on AMD
hardware). Metal, Vulkan and SYCL have no
SET_ROWSfor these types; KV cachecreation now fails with a clear error on such a device instead of aborting in
the scheduler, and
-nkvokeeps the cache in host memory. The check applies toevery cache type, not only the new ones.
No new environment variables.
GGML_TURBO_MMA_FUSEDcovers the new fused pairs.Evidence
Dense model (Qwen2.5-7B)
Qwen2.5-7B-Instruct Q8_0 (28 full-attention layers, GQA 28:4 = 7), wikitext-2
test, 20 x 2048 tokens,
-fa on, RTX 3090 Ti. "rule off" =TURBO_AUTO_ASYMMETRIC=0.As V types both are free on this model. As K types they are not: turbo6 K
costs about 15% perplexity and turbo5 K about 5x, so both stay under the
auto-asymmetric rule, which maps them to q8_0 K here.
Hybrid model (Qwen3.8-27B)
Qwen3.8-27B is a hybrid: 16 of its 64 layers are full attention (GQA 24:4 = 6,
head dim 256), the rest are Gated DeltaNet, so it dilutes K error and cannot
show the high-GQA K problem. These numbers are the V-side and pair evidence.
Protocol: for each prompt depth D a SWE-style prompt of D tokens, then the model
generates at least 6,144 tokens on top of it (temp 1.0).
llama-kld-depth(aposition-resolved KLD tool;
-fit off, KLD stride 1) writes an f16/f16 base perdepth and scores each K/V config against it. Only the generated positions are
scored; n is about 6,140 positions per depth. RTX 3090 Ti.
Mean KLD on generated tokens vs f16/f16 (nats), range over depths 10K, 25K, 51K
and 77K:
Paired per-token K deltas (same positions, same base, SE = sd(diff)/sqrt(n)),
with
turbo6V:turbo6K minusq8_0K is +0.00006 to +0.00013 nats (t 2.6to 9.1), and
turbo5K minusturbo6K is +0.00030 to +0.00043 (t 10 to 16).So on this model
turbo6K is measurably but only slightly worse thanq8_0K.Two f16 runs of the same config differ by about 0.00024, which is the tool's floor.
Test plan
Results on an RTX 3090 Ti (sm_86), CUDA build at the PR head:
-o SET_ROWS_TURBO6-o SET_ROWS_TURBO5-o CPY -p 'turbo[56]'-o FLASH_ATTN_EXT -p 'turbo[56]'GGML_TURBO_MMA_FUSED=0-o FLASH_ATTN_EXT, all cases-o SET_ROWS/CPY/GET_ROWS, all casesThe FA counts are 4 lower than before the review because the cross pairs moved
behind
GGML_CUDA_FA_ALL_QUANTSnow report unsupported in a default build.A
GGML_CUDA_FA_ALL_QUANTS=ONbuild compiles; the wave64 HIP path is untested.test-turbo-quantcovers the reference codec and its round-trip error;test-quantize-fnsskips the new types the same way it skips the otherrotated-domain KV types.
Scope notes
is written to GGUF files. Rejecting KV-only types in
llama-quantizeand themodel loader is left for the follow-up that covers turbo2/3/4 too.
-ctkhelp lists the new types on every build, as it does for turbo2/3/4;non-CUDA KV devices now get a clear error at context creation.
turbo5/turbo6added to itslayer-adaptive helpers once it lands.