Repository navigation
Conversation
🔍 Aletheore evidence diffgraph LR
n0["src/cuda/mod.rs"]
n1["kv_turbo.rs"]
n2["src/vulkan/mod.rs"]
n3["src/backend.rs"]
n4["src/hip/mod.rs"]
n5["device.rs"]
n6["lib.rs"]
n7["preflight.rs"]
n8["tests/backend.rs"]
n9["tests/common/mod.rs"]
n10["gpu_model.rs"]
n11["kernels.rs"]
n12["kv_q8.rs"]
n13["llama_numerics.rs"]
n0 --> n3
n0 --> n4
n0 --> n5
n0 --> n6
n0 --> n7
n0 --> n9
n0 --> n10
n0 --> n12
n0 --> n13
n1 --> n6
n2 --> n3
n2 --> n6
n2 --> n7
n2 --> n8
n2 --> n9
n2 --> n10
n2 --> n11
n2 --> n12
n2 --> n13
classDef changed fill:#d9772c,color:#fff,stroke:#b85f1c;
classDef changedFinding fill:#c0392b,color:#fff,stroke:#922b21;
classDef dependent fill:#f2f0ec,color:#18140f,stroke:#d6d0c5;
class n0 changed
class n1 changed
class n2 changed
class n3 dependent
class n4 dependent
class n5 dependent
class n6 dependent
class n7 dependent
class n8 dependent
class n9 dependent
class n10 dependent
class n11 dependent
class n12 dependent
class n13 dependent
What changed
Aggregate deltas
|
| if constexpr (BITS == 3) { | ||
| // True 3-bit layout: a 2-bit code plane (4 values per byte) and a 1-bit sign plane (8 per byte), | ||
| // followed by one fp16 corrected norm per 128 values. 50 B per group instead of 66. | ||
| unsigned mag = (unsigned)(q & 3), sign = (unsigned)(q >> 2); |
There was a problem hiding this comment.
🟠 High · Correctness · 1 of 6
3-bit head stride reserves hd/64 norm bytes but writes 2 per group, so hd=64 norms overlap the next head For hd=64 the per-head stride is 364/8 + 64/64 = 25 bytes, but the fp16 norm is written at r+24 (2 bytes, to offset 25) and every head has one group. Each head's norm overwrites the first code byte of the following head, and the odd stride misaligns the u16 store. Inkling-2 (hd=64) turbo3 KV then decodes corrupted values. The stride should reserve 2max(1, hd/128) norm bytes, e.g. 3hd/8 + 2max(1,hd/128).
Prompt for your AI agent
File: src/cuda/kernels.cu
Line: 2868
Issue: 3-bit head stride reserves hd/64 norm bytes but writes 2 per group, so hd=64 norms overlap the next head For hd=64 the per-head stride is 3*64/8 + 64/64 = 25 bytes, but the fp16 norm is written at r+24 (2 bytes, to offset 25) and every head has one group. Each head's norm overwrites the first code byte of the following head, and the odd stride misaligns the u16 store. Inkling-2 (hd=64) turbo3 KV then decodes corrupted values. The stride should reserve 2*max(1, hd/128) norm bytes, e.g. 3*hd/8 + 2*max(1,hd/128).
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
| if (valid && (we & 7u) == 0u) U8(uint64_t(c)).v[r + hd / 4u + we / 8u] = uint8_t(s8); | ||
| if (valid && w == 0u) c.v[(r + 3u * hd / 8u) / 2u + g] = uint16_t(f2h(fdiv(norm, max(recon, 1e-6)))); | ||
| return; | ||
| } |
There was a problem hiding this comment.
🟠 High · Correctness · 2 of 6
Vulkan 3-bit stride and norm read share the hd=64 bug, with an odd base offset tq_ldkv and tq_group_reduce use r = off/hd*(3hd/8 + hd/64). For hd=64 that is 25 bytes per head, so r is odd for every head after the first. The norm index (r + 3hd/8)/2 truncates to the wrong u16 and reads the previous head's code bytes as the scale. The Vulkan turbo3 hd=64 path (the new vulkan_inkling_turbo3_hd64 test) decodes wrong values, and the CUDA and Vulkan layouts must match for shared caches.
Prompt for your AI agent
File: src/vulkan/shaders/tq.glsl
Line: 131
Issue: Vulkan 3-bit stride and norm read share the hd=64 bug, with an odd base offset tq_ldkv and tq_group_reduce use r = off/hd*(3*hd/8 + hd/64). For hd=64 that is 25 bytes per head, so r is odd for every head after the first. The norm index (r + 3*hd/8)/2 truncates to the wrong u16 and reads the previous head's code bytes as the scale. The Vulkan turbo3 hd=64 path (the new vulkan_inkling_turbo3_hd64 test) decodes wrong values, and the CUDA and Vulkan layouts must match for shared caches.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
| // 2-bit code plane plus 1-bit sign plane, then one corrected fp16 norm per 128 values: 50 B per group. | ||
| uint mag = uint(q & 3), sign = uint(q >> 2); | ||
| uint r = e / hd * (3u * hd / 8u + hd / 64u); | ||
| uint m4 = mag | (subgroupShuffleXor(mag, 1u) << 2) | (subgroupShuffleXor(mag, 2u) << 4) | (subgroupShuffleXor(mag, 3u) << 6); |
There was a problem hiding this comment.
🟠 High · Correctness · 3 of 6
return; in a float-returning GLSL function does not compile tq_group_reduce is declared to return float, but the new bits==3 branch uses a bare return;. GLSL rejects this, so the Vulkan shader fails to build and every turbo3 path fails. The branch should return a value, such as the reduced recon, or restructure so the function ends in one return.
Prompt for your AI agent
File: src/vulkan/shaders/tq.glsl
Line: 122
Issue: `return;` in a float-returning GLSL function does not compile tq_group_reduce is declared to return float, but the new bits==3 branch uses a bare `return;`. GLSL rejects this, so the Vulkan shader fails to build and every turbo3 path fails. The branch should return a value, such as the reduced recon, or restructure so the function ends in one return.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
| `--kv turbo3` uses 3-bit packing: a 2-bit code plane plus a sign plane and one corrected norm per 128 values, | ||
| or 50 B per 128 values instead of the 4-bit modes' 66 B. `--kv turbo4-3`, `turbo4-2`, `turbo2` and `turbo3-2` | ||
| select narrower centroids with the 4-bit nibble packing. | ||
| - `--k TYPE` and `--v TYPE` set the two cache formats independently (CUDA). Either half may be `f16`, `q8`, |
There was a problem hiding this comment.
🟡 Medium · Correctness · 4 of 6
Doc says 4-bit modes use 66 B per 128 values; the code gives 72 B The 4-bit stride is hd/2 + hd/16 = 72 bytes for hd=128, and the test expects 36 u16 words (n*9/32). The doc's 66 B figure understates the 4-bit footprint, so users sizing memory from the doc will under-allocate and the stated 50-vs-66 comparison is wrong.
Prompt for your AI agent
File: docs/USAGE.md
Line: 151
Issue: Doc says 4-bit modes use 66 B per 128 values; the code gives 72 B The 4-bit stride is hd/2 + hd/16 = 72 bytes for hd=128, and the test expects 36 u16 words (n*9/32). The doc's 66 B figure understates the 4-bit footprint, so users sizing memory from the doc will under-allocate and the stated 50-vs-66 comparison is wrong.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
| fn kv_mode_words(m: u32, n: usize) -> usize { | ||
| match m { 1 => n * 17 / 32, 2 | 4..=6 => n * 9 / 32, 3 => n * 3 / 16, _ => n } | ||
| match m { 1 => n * 17 / 32, 2 | 4 | 6 => n * 9 / 32, 5 => n * 25 / 128, 3 => n * 3 / 16, _ => n } | ||
| } |
There was a problem hiding this comment.
🟡 Medium · Correctness · 5 of 6
Mode 5 changes from nibble packing (9/32) to 3-bit packing (25/128) without visible mapping Previously modes 4..=6 all used n*9/32 words (4-bit nibble layout). This diff makes mode 5 use 25/128, but the diff never shows that mode 5 is turbo3. If mode 5 is one of turbo4-3, turbo4-2, turbo2 or turbo3-2 (which the docs say stay on 4-bit nibble packing), those modes allocate too little and read/write out of bounds. The same change appears in src/vulkan/mod.rs.
Prompt for your AI agent
File: src/cuda/mod.rs
Line: 188
Symbol: kv_mode_words
Issue: Mode 5 changes from nibble packing (9/32) to 3-bit packing (25/128) without visible mapping Previously modes 4..=6 all used n*9/32 words (4-bit nibble layout). This diff makes mode 5 use 25/128, but the diff never shows that mode 5 is turbo3. If mode 5 is one of turbo4-3, turbo4-2, turbo2 or turbo3-2 (which the docs say stay on 4-bit nibble packing), those modes allocate too little and read/write out of bounds. The same change appears in src/vulkan/mod.rs.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
| int q = int((we & 1u) == 0u ? b & 15u : b >> 4); | ||
| return rh(tq_centroid(bits, q) * norm); | ||
| } | ||
| uint tq_ldkv2(U16 c, uint off, uint hd, uint bits) { |
There was a problem hiding this comment.
🔵 Low · Performance · 6 of 6
tq_ldkv2 for 3-bit recomputes the lo half through tq_ldkv, reloading the norm and codes The 3-bit branch of tq_ldkv2 calls tq_ldkv(c, off, ...) for the low half, which repeats the norm load and both code-byte loads already done for the high half. Each dual-element read therefore does redundant memory traffic, which matters in the attention inner loop. The lo element should be computed from the same loaded norm and code bytes as the hi element.
Prompt for your AI agent
File: src/vulkan/shaders/tq.glsl
Line: 153
Issue: tq_ldkv2 for 3-bit recomputes the lo half through tq_ldkv, reloading the norm and codes The 3-bit branch of tq_ldkv2 calls tq_ldkv(c, off, ...) for the low half, which repeats the norm load and both code-byte loads already done for the high half. Each dual-element read therefore does redundant memory traffic, which matters in the attention inner loop. The lo element should be computed from the same loaded norm and code bytes as the hi element.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
Aletheore Flash Lite reviewWhat this PR does. This PR changes the
6 finding(s) posted as inline review comment(s).
Review detailsReviewed 8 file(s); skipped 12. Dismissed 0; outside PR diff 0. Grounding: 6 of 6 proposed finding(s). Fix all with your agentNote: 12 of 20 changed file(s) were not included in this review (the review reads at most 30 files, and skips any file over 100KB). Issues in them would not be found, and citations pointing into them could not be checked against the real file: Blast radius: 59 other file(s) depend on what this PR changes (11 directly, 48 indirectly)Computed from the import graph of this exact commit, not guessed by a model. It sees imports only, so dynamic imports, reflection and non-code references are not counted.
Indirectly affected: The graph is large; this list is capped and not exhaustive. |
d41648c to
9f20ad3
Compare
TurboQuant 3-bit now uses a real 3-bit layout. Previously every turbo mode wrote 4-bit nibbles, so turbo3 cost the same 66 B per 128 values as turbo4. It now stores a 2-bit code plane in 32 bytes, a 1-bit sign plane in 16 bytes and one corrected fp16 norm, for 50 B per 128 values. The 25/36 word ratio is asserted in tests on both CUDA and Vulkan.
The layout follows TheTom's
block_turbo3_0: index = low2 | (sign << 2), with the 2-bit plane packed four values per byte and the sign plane eight values per byte. Reference: https://raw.githubusercontent.com/TheTom/llama-cpp-turboquant/feature/turboquant-kv-cache/ggml/src/ggml-cuda/turbo-quant.cuhdocs/USAGE.mdis updated in place, so the earlier docs-only PR #119 is closed.Correctness
kv_turbo.rs: CPU round trip checks the byte layout (50 B), centroid indices and round-trip cosine.tests/kv_turbo.rs: turbo3 cache is 25 u16 words per 128 values against 36 for turbo4, prefill versus decode relative L2 is 9.33e-5, and decode versus f16 is 1.26e-1.tests/vulkan_split_kv.rs: the Inkling 2 head_dim 64 clip/attention path is checked for turbo3 as well as turbo4, relative L2 2.07e-7.cargo test --releaseis green, including the pinned legacy-mode hashes, kernel name counts and NVRTC compiles for older targets.Quality
A short calibration on Tiel Coder chose chunk 1024, MTP 1 and short 32. Quality used that chunk with MTP off and 2044 scored positions over four chunks of two much larger corpora: project documentation and prose, and Rust source files from this repository.
Text corpus, f16 PPL 8.4625:
Code corpus, f16 PPL 6.5032:
On both corpora turbo4 has higher top-1 and lower mean KLD than q4 at the same cache size. The earlier 127-position Inkling result was noise from a small sample and changing expert placement. turbo3 remains an opt-in memory setting; the mixed setting gives a smaller V half with a measurable quality cost.
The q8-K rescue configuration was measured against the q8 baseline on the same corpora:
Keeping K at q8 gives PPL equal to or better than q8 for turbo4 V on both corpora, but top-1 still sits in the low 90s against the q8 baseline. V quantization changes the output logits enough to flip near-tie argmax tokens even when the probability distribution stays close, which is the same reason top-1 does not reach q8 with symmetric turbo4 K.
Memory and speed
Per 128 values, turbo3 uses 50 B against 66 B for turbo4 and q4, a 24% reduction. Cache word count goes from n9/32 to n25/128.
Long-context bench with the calibrated chunk and MTP, pp 1024 and 2048, tg 32, prefill/decode tok/s:
Needle-in-a-haystack bench with a 2048-token prompt and tg 16, prefill/decode tok/s: f16 503.7/33.1, q8 506.6/36.1, q4 481.5/34.4, turbo4 476.5/33.0, mixed 472.6/33.8. All five modes returned the planted code
Zephyr-4719.This branch also scales the quantizer norm by sqrt(128 / rotation group) on store. The 128-value WHT groups used by the general engine are unchanged. The 64-value Inkling clip path now uses centroids matched to its coordinate variance: the synthetic head_dim 64 prefill-versus-f16 relative L2 drops from 1.07e-1 to 9.67e-2, and the test now pins it under 0.2. The real Inkling 2 Mini GGUF has head_dim 128, so this correction does not alter its measured quality.
No benchmark numbers were added to
docs/. No legacy mode changes: f16, q8, q4 and q2 keep their pinned byte hashes.Rebased onto upstream
3920b2cafter the 1.4.0 batched-serving merge;git merge-treereports no conflicts.