Repository navigation
Conversation
|
The old PR (#121) was stuck: it was opened against a What I actually changed:
|
🔍 Aletheore evidence diffgraph LR
n0["src/cuda/mod.rs"]
n1["kv_turbo.rs"]
n2["src/vulkan/mod.rs"]
n3["src/backend.rs"]
n4["chat.rs"]
n5["src/hip/mod.rs"]
n6["device.rs"]
n7["lib.rs"]
n8["main.rs"]
n9["preflight.rs"]
n10["tests/backend.rs"]
n11["tests/common/mod.rs"]
n12["gpu_model.rs"]
n13["inkling2_attn_kv.rs"]
n14["kv_q8.rs"]
n15["llama_numerics.rs"]
n0 --> n3
n0 --> n4
n0 --> n5
n0 --> n6
n0 --> n7
n0 --> n8
n0 --> n9
n0 --> n11
n0 --> n12
n0 --> n13
n0 --> n14
n0 --> n15
n1 --> n7
n2 --> n3
n2 --> n7
n2 --> n9
n2 --> n10
n2 --> n11
n2 --> n12
n2 --> n14
n2 --> n15
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
class n14 dependent
class n15 dependent
What changed
Aggregate deltas
|
| | (__shfl_xor_sync(0xffffffffu, sign, 7) << 7); | ||
| if (active && (we & 3) == 0) r[we / 4] = (unsigned char)m4; | ||
| if (active && (we & 7) == 0) r[hd / 4 + we / 8] = (unsigned char)s8; | ||
| if (active && w == 0) *(u16*)(r + 3 * hd / 8 + 2 * g) = f2h(norm / fmaxf(recon, 1e-6f)); |
There was a problem hiding this comment.
✅ No longer detected as of 4861885e5088.
🟠 High · Correctness · 1 of 6
3-bit stride is 25 B/row for hd=64, norm overflows and u16 store is misaligned For hd=64, 3*hd/8+hd/64=25 bytes/row but code+sign need 24 B plus a 2 B norm =26 B. The norm store at r+24 writes 1 B past the row into the next row's code plane, the next row starts at odd offset 25 making (u16) misaligned (UB/crash on CUDA), and rows overwrite each other's codes/norms.
Prompt for your AI agent
File: src/cuda/kernels.cu
Line: 4338
Issue: 3-bit stride is 25 B/row for hd=64, norm overflows and u16 store is misaligned For hd=64, 3*hd/8+hd/64=25 bytes/row but code+sign need 24 B plus a 2 B norm =26 B. The norm store at r+24 writes 1 B past the row into the next row's code plane, the next row starts at odd offset 25 making *(u16*) misaligned (UB/crash on CUDA), and rows overwrite each other's codes/norms.
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.
| | (subgroupShuffleXor(sign, 3u) << 3) | (subgroupShuffleXor(sign, 4u) << 4) | ||
| | (subgroupShuffleXor(sign, 5u) << 5) | (subgroupShuffleXor(sign, 6u) << 6) | ||
| | (subgroupShuffleXor(sign, 7u) << 7); | ||
| if (valid && (we & 3u) == 0u) U8(uint64_t(c)).v[r + we / 4u] = uint8_t(m4); |
There was a problem hiding this comment.
✅ No longer detected as of 4861885e5088.
🟠 High · Correctness · 2 of 6
Vulkan 3-bit layout has same 25 B odd stride and truncated u16 norm index for hd<128 tq_put/tq_ldkv use r=e/hd*(3hd/8+hd/64)=25 B/row for hd=64 and c.v[(r+3hd/8)/2+g] for the norm. Odd r truncates on /2, the norm word overlaps the next row, and allocation from kv_words is 1 B/row short, corrupting codes and norms for Inkling hd64 turbo3.
Prompt for your AI agent
File: src/vulkan/shaders/tq.glsl
Line: 131
Issue: Vulkan 3-bit layout has same 25 B odd stride and truncated u16 norm index for hd<128 tq_put/tq_ldkv use r=e/hd*(3*hd/8+hd/64)=25 B/row for hd=64 and c.v[(r+3*hd/8)/2+g] for the norm. Odd r truncates on /2, the norm word overlaps the next row, and allocation from kv_words is 1 B/row short, corrupting codes and norms for Inkling hd64 turbo3.
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.
| 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.
🟠 High · Correctness · 3 of 6
kv_mode_words n25/128 under-allocates for hd=64 (and any n not multiple of 128) For hd=64 each row needs 26 B=13 u16 words, but n25/128 gives 12.5 words/row (truncated, 24-25 B/row total). The buffer is short by ~1 B/row, so the last row's norm write is out of bounds and earlier rows' norms overlap following code planes. Same formula in src/vulkan/mod.rs kv_words/kv_words_v.
Prompt for your AI agent
File: src/cuda/mod.rs
Line: 276
Issue: kv_mode_words n*25/128 under-allocates for hd=64 (and any n not multiple of 128) For hd=64 each row needs 26 B=13 u16 words, but n*25/128 gives 12.5 words/row (truncated, 24-25 B/row total). The buffer is short by ~1 B/row, so the last row's norm write is out of bounds and earlier rows' norms overlap following code planes. Same formula in src/vulkan/mod.rs kv_words/kv_words_v.
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 = tq_nearest<BITS>(x / qnorm); | ||
| int we = e % hd; | ||
| int w = we & 127; // value within this 128-wide WHT group | ||
| int g = we >> 7; // WHT group within the head (0 when hd <= 128) |
There was a problem hiding this comment.
🟡 Medium · Correctness · 4 of 6
gw=hd for hd<128 gives hd/64=0 or 1 norm bytes, insufficient for one fp16 norm For hd=32, hd/64=0 so norm store at 3*hd/8 writes entirely past the row; for hd=64 it reserves 1 B for a 2 B norm. The formula only equals 2 B per 128 values when hd>=128, so any head_dim <128 corrupts the cache even before misalignment is considered.
Prompt for your AI agent
File: src/cuda/kernels.cu
Line: 4317
Issue: gw=hd for hd<128 gives hd/64=0 or 1 norm bytes, insufficient for one fp16 norm For hd=32, hd/64=0 so norm store at 3*hd/8 writes entirely past the row; for hd=64 it reserves 1 B for a 2 B norm. The formula only equals 2 B per 128 values when hd>=128, so any head_dim <128 corrupts the cache even before misalignment is considered.
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.
| eprintln!("turbo3 prefill vs decode {e:.2e}, vs f16 {:.2e}", rel_l2(&dec3, &dec16)); | ||
| assert!(e < 5e-4, "turbo3 prefill vs decode {e}"); | ||
| assert!(rel_l2(&dec3, &dec16) < 1.0, "turbo3 decode drifted from f16"); | ||
| } |
There was a problem hiding this comment.
🔵 Low · Maintainability · 5 of 6
turbo3 vs f16 assertion rel_l2 < 1.0 is vacuous and cannot catch drift/scaling bugs rel_l2 < 1.0 passes for a zero vector or a vector scaled by ~0 (error ~1.0 boundary) and for the sqrt(2) hd64 scaling error. The test therefore passes even if dequantized turbo3 values are systematically wrong, masking the hd64 layout/scaling defects above.
Prompt for your AI agent
File: tests/kv_turbo.rs
Line: 240
Issue: turbo3 vs f16 assertion rel_l2 < 1.0 is vacuous and cannot catch drift/scaling bugs rel_l2 < 1.0 passes for a zero vector or a vector scaled by ~0 (error ~1.0 boundary) and for the sqrt(2) hd64 scaling error. The test therefore passes even if dequantized turbo3 values are systematically wrong, masking the hd64 layout/scaling defects above.
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.
| for i in 0..8 { | ||
| let mag = (qs[i / 4] >> (2 * (i % 4))) & 3; | ||
| let sign = (signs[0] >> i) & 1; | ||
| assert_eq!((mag | (sign << 2)) as usize, nearest(rotated[i], 3)); |
There was a problem hiding this comment.
✅ No longer detected as of 4861885e5088.
🔵 Low · Maintainability · 6 of 6
Rust turbo3 test only verifies packing of first 8 values, leaving 120 values unchecked The loop asserts mag/sign only for i in 0..8 (signs[0] only). A sign-plane or code-plane bug at i>=8 (e.g. wrong byte/bit index in quantize3/dequantize3) would not be caught by the exact check; only the loose mean_err<0.09/cos>0.97 aggregate might catch gross errors.
Prompt for your AI agent
File: src/kv_turbo.rs
Line: 295
Issue: Rust turbo3 test only verifies packing of first 8 values, leaving 120 values unchecked The loop asserts mag/sign only for i in 0..8 (signs[0] only). A sign-plane or code-plane bug at i>=8 (e.g. wrong byte/bit index in quantize3/dequantize3) would not be caught by the exact check; only the loose mean_err<0.09/cos>0.97 aggregate might catch gross errors.
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. The pull request rewrites docs/USAGE.md, reorganizing and condensing documentation for rustingstrata commands, flags, server, MCP, and model loading options. It affects users and operators reading usage documentation for chat, generate, serve, bench, calibrate, stats, and update.
5 finding(s) posted as inline review comment(s). 3 earlier finding(s) from previous pushes still open. 3 earlier finding(s) no longer detected in this push.
Review detailsReviewed 9 file(s); skipped 12. Dismissed 0; outside PR diff 1. Grounding: 14 of 15 proposed finding(s). Fix all with your agentNote: 12 of 21 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: 61 other file(s) depend on what this PR changes (13 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. |
|
Hi, ty, 50 B per 128 values is a nice saving. I merged it onto main here and ran the tests on my GPU (RTX 3060). Some things pls before I can merge:
|
Drop the temporary 'int we = w;' left over from adapting the old off-based ldkv1 to the row/w ldkv1_at signature; the M==3/2/1 branches already use w directly. Behavior-preserving.
…g2 turbo3 decode Addresses the maintainer review of the turbo3 KV layout: - CUDA + Vulkan: a turbo3 row is now 3*hd/8 + 2*ceil(hd/128) bytes (a 2-byte fp16 norm per 128-value group) instead of hd/64, so the norm no longer overflows onto the next row's first code byte. Matches TheTom/llama-cpp-turboquant block_turbo3_0 (QK_TURBO3=128, one norm per fixed 128-value block). At hd=64 the row is 26 B; at hd>=128 the stride is unchanged. - CUDA + Vulkan: gate the sqrt(128/gw) norm rescale inside the 3-bit branch so turbo4/turbo2 keep the unscaled norm. - tests/inkling2_q8_attention.rs: decode turbo3 as 3-bit (was a 4-bit nibble), byte-for-byte like kv_turbo::dequantize3; fixes the index-out-of-bounds. - docs/USAGE.md: 4-bit modes are 72 B per 128 values (36 words), not 66; clarify that the turbo3 half of a composite is 3-bit packed. - Regenerate SPIR-V so spirv_is_fresh holds. hd=64 turbo3 stays guarded (hd % 128 == 0); no sizing change is required.
a0e4bda to
4861885
Compare
|
Thanks for the detailed review — all four points are addressed, and the branch is rebased onto current
New/updated tests: |
| } | ||
| } | ||
| #endif | ||
| __device__ __forceinline__ float warp_min(float v) { |
There was a problem hiding this comment.
🟠 High · Correctness · 1 of 5
Stored turbo3 norm uses unscaled norm, not qnorm, so hd<128 dequant is off by sqrt(128/hd) For hd=64, qnorm=normsqrt(2) is used to pick centroids, but the fp16 stored value is f2h(norm/recon) instead of f2h(qnorm/recon). Dequant multiplies centroidstored_norm with no width correction, so reconstructed K/V are systematically low by sqrt(2) (by 2x for hd=32), corrupting attention for Inkling-2 hd64 heads despite the comment saying the corrected norm is stored in scaled units.
Prompt for your AI agent
File: src/cuda/kernels.cu
Line: 4352
Issue: Stored turbo3 norm uses unscaled norm, not qnorm, so hd<128 dequant is off by sqrt(128/hd) For hd=64, qnorm=norm*sqrt(2) is used to pick centroids, but the fp16 stored value is f2h(norm/recon) instead of f2h(qnorm/recon). Dequant multiplies centroid*stored_norm with no width correction, so reconstructed K/V are systematically low by sqrt(2) (by 2x for hd=32), corrupting attention for Inkling-2 hd64 heads despite the comment saying the corrected norm is stored in scaled units.
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 & 3u) == 0u) U8(uint64_t(c)).v[r + we / 4u] = uint8_t(m4); | ||
| 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 5
turbo3 hd64 stored norm uses unscaled norm instead of qnorm, dequantized values off by sqrt(2) For hd=64, qnorm=normsqrt(128/64)=1.414norm and q=nearest(x/qnorm), so x≈centroidqnorm/recon. Code stores norm/recon, and tq_ldkv returns centroid(norm/recon), underestimating every hd64 turbo3 value by 1.414x. Prefill-vs-decode tests pass because both use same wrong norm, but values drift from f16/CPU.
Prompt for your AI agent
File: src/vulkan/shaders/tq.glsl
Line: 138
Issue: turbo3 hd64 stored norm uses unscaled norm instead of qnorm, dequantized values off by sqrt(2) For hd=64, qnorm=norm*sqrt(128/64)=1.414*norm and q=nearest(x/qnorm), so x≈centroid*qnorm/recon. Code stores norm/recon, and tq_ldkv returns centroid*(norm/recon), underestimating every hd64 turbo3 value by 1.414x. Prefill-vs-decode tests pass because both use same wrong norm, but values drift from f16/CPU.
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.
| match self.0.kv_k_mode { 0 => n, 1 => n * 17 / 32, 2 | 4..=6 => n * 9 / 32, 3 => n * 3 / 16, _ => unreachable!() } | ||
| match self.0.kv_k_mode { 0 => n, 1 => n * 17 / 32, 2 | 4 | 6 => n * 9 / 32, 5 => n * 25 / 128, 3 => n * 3 / 16, _ => unreachable!() } | ||
| } | ||
| fn kv_words_v(&self, n: usize) -> usize { |
There was a problem hiding this comment.
🟡 Medium · Correctness · 3 of 5
kv_words for mode 5 under-allocates hd64
25/128 words vs shader's 13 words per 64 values Shader per-row stride is 364/8+2((64+127)/128)=26 bytes=13 u16 words (one fp16 norm per 64-group). kv_words gives n*25/128 = 12.5 words per 64 values, e.g. 25 words for 2 rows instead of 26. Host allocation is short, shader writes/reads the last row's norm/code past the buffer and rows overlap the allocation end.
Prompt for your AI agent
File: src/vulkan/mod.rs
Line: 1353
Symbol: kv_words_v
Issue: kv_words for mode 5 under-allocates hd64: 25/128 words vs shader's 13 words per 64 values Shader per-row stride is 3*64/8+2*((64+127)/128)=26 bytes=13 u16 words (one fp16 norm per 64-group). kv_words gives n*25/128 = 12.5 words per 64 values, e.g. 25 words for 2 rows instead of 26. Host allocation is short, shader writes/reads the last row's norm/code past the buffer and rows overlap the allocation end.
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.
| 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 · 4 of 5
kv_mode_words for mode 5 has same hd64 under-allocation as Vulkan kv_words With hd=64 (Inkling 2), n=rows64 gives n25/128 words, but the 3-bit layout needs 13 words per row (16B code + 8B sign + 2B norm). Allocation is 0.5 word/row short, so CUDA KV writes overflow the buffer / corrupt following rows, same as Vulkan path.
Prompt for your AI agent
File: src/cuda/mod.rs
Line: 276
Issue: kv_mode_words for mode 5 has same hd64 under-allocation as Vulkan kv_words With hd=64 (Inkling 2), n=rows*64 gives n*25/128 words, but the 3-bit layout needs 13 words per row (16B code + 8B sign + 2B norm). Allocation is 0.5 word/row short, so CUDA KV writes overflow the buffer / corrupt following rows, same as Vulkan path.
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.
| /// reconstruction-corrected norm. Mirrors TheTom's `block_turbo3_0` layout: 50 B per 128 values. | ||
| pub fn quantize3(x: &[f32; D], l2: f32) -> ([u8; D / 4], [u8; D / 8], f32) { | ||
| let mut qs = [0u8; D / 4]; | ||
| let mut signs = [0u8; D / 8]; |
There was a problem hiding this comment.
🟡 Medium · Correctness
CPU quantize3/dequantize3 has no hd64 sqrt(128/group) scaling, mismatching Vulkan tq_put quantize3 quantizes x/l2 with no group scaling and stores l2/recon, while Vulkan quantizes x/(norm*sqrt(128/gw)). For hd64 the CPU reference and GPU produce different codes/norms for same input, so CPU round-trip test (D=128 only) passes but CPU-vs-GPU hd64 comparisons and any CPU fallback decode of GPU-written KV are wrong by sqrt(2).
Prompt for your AI agent
File: src/kv_turbo.rs
Line: 126
Symbol: quantize3
Issue: CPU quantize3/dequantize3 has no hd64 sqrt(128/group) scaling, mismatching Vulkan tq_put quantize3 quantizes x/l2 with no group scaling and stores l2/recon, while Vulkan quantizes x/(norm*sqrt(128/gw)). For hd64 the CPU reference and GPU produce different codes/norms for same input, so CPU round-trip test (D=128 only) passes but CPU-vs-GPU hd64 comparisons and any CPU fallback decode of GPU-written KV are wrong by sqrt(2).
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.
|
Hi, ty for the update. Points 2, 3 and 4 are good now, and the shaders are fresh. All the turbo tests pass here on CUDA and Vulkan. Point 1 is only half done. The kernels and the shader use the right 26 B row at head width 64, but the buffer size still comes from n * 25 / 128 (kv_mode_words in src/cuda/mod.rs, kv_words and kv_words_v in src/vulkan/mod.rs). Inkling 2 sizes its cache with that, so at head width 64 a row gets 24 B and the last rows write past the end. Two ways I would be ok with:
Two small things for the tests:
After that I merge it. |
…hecks Co-authored-by: Andrew Pavlov <100561417+andrew-pavlov-ua@users.noreply.github.com>
|
oh ok, I am not really running the inkling model, just running the tests to see if the quantizations are giving correctly rotated numbers so that might be an oversight from my part. |
Supersedes #121 (which GitHub locked after its head branch was force-pushed during the rebase). This is the same two commits, now rebased cleanly onto
main.What this does
turbo3now uses a real 3-bit layout: a 2-bit code plane (4 values/byte) plus a 1-bit sign plane (8 values/byte) and one fp16 reconstruction-corrected norm per 128 values — 50 B per 128 values instead of the 4-bit modes' 66 B (25 u16 words vs 36).Where
kv_put_tq,ldkv,ldkv1_at(3-bit branch),kv_mode_words(mode 5 → 25/128 words).tq.glsl(tq_put,tq_ldkv,tq_ldkv2),kv_words/kv_words_v, regenerated.spv.quantize3/dequantize3inkv_turbo.rs+ round-trip test.kv_turbo,vulkan_split_kvincl. a turbo3 hd64 case).Verification
main(03cd705); no conflicts remain..spvregenerated withtools/gen_spirv.sh; local glslc reproduces the committed shaders byte-for-byte, so the freshness check holds.