Repository navigation
TurboQuant: pack turbo3 in 3 bits (50 B per 128 values) #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2963,18 +2963,43 @@ __device__ __forceinline__ float tq_group_sum(float cs, int g) { | |
| return warp_sum(t); | ||
| } | ||
| template<int BITS> __device__ __forceinline__ void kv_put_tq(u16* c, size_t e, int hd, float x, float norm, bool active) { | ||
| int q = tq_nearest<BITS>(x / norm); | ||
| unsigned nib = (unsigned)q; | ||
| unsigned pair = nib | (__shfl_xor_sync(0xffffffff, nib, 1) << 4); | ||
| // Centroids are calibrated for N(0, 1/128), the coordinates of a unit-L2 128-value WHT group. A 64-value | ||
| // group has twice that variance, so scale the norm by sqrt(128 / group) before quantizing and store the | ||
| // corrected norm in the same scaled units. Dequant then needs no width-dependent correction. | ||
| int gw = hd >= 128 ? 128 : hd; | ||
| float qnorm = norm * sqrtf(128.0f / (float)gw); | ||
| 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) | ||
| unsigned char* r = (unsigned char*)c + e / hd * (hd / 2 + hd / 16); | ||
| if (active && (we & 1) == 0) r[we / 2] = (unsigned char)pair; | ||
| float recon = sqrtf(tq_group_sum(active ? tq_centroid<BITS>(q) * tq_centroid<BITS>(q) : 0.f, g)); | ||
| // The reference stores norm / ||reconstructed centroids||; without it dequant is systematically low. | ||
| // Inactive lanes of a short head width still join the reduction but contribute no centroid energy. | ||
| float recon = sqrtf(tq_group_sum(active ? tq_centroid<BITS>(q) * tq_centroid<BITS>(q) : 0.f, g)); | ||
| if (active && w == 0) *(u16*)(r + hd / 2 + 2 * g) = f2h(norm / fmaxf(recon, 1e-6f)); | ||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 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 agentReply |
||
| unsigned char* r = (unsigned char*)c + e / hd * (3 * hd / 8 + hd / 64); | ||
| unsigned m4 = mag | (__shfl_xor_sync(0xffffffffu, mag, 1) << 2) | ||
| | (__shfl_xor_sync(0xffffffffu, mag, 2) << 4) | ||
| | (__shfl_xor_sync(0xffffffffu, mag, 3) << 6); | ||
| unsigned s8 = sign | (__shfl_xor_sync(0xffffffffu, sign, 1) << 1) | ||
| | (__shfl_xor_sync(0xffffffffu, sign, 2) << 2) | ||
| | (__shfl_xor_sync(0xffffffffu, sign, 3) << 3) | ||
| | (__shfl_xor_sync(0xffffffffu, sign, 4) << 4) | ||
| | (__shfl_xor_sync(0xffffffffu, sign, 5) << 5) | ||
| | (__shfl_xor_sync(0xffffffffu, sign, 6) << 6) | ||
| | (__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)); | ||
| } else { | ||
| unsigned nib = (unsigned)q; | ||
| unsigned pair = nib | (__shfl_xor_sync(0xffffffffu, nib, 1) << 4); | ||
| unsigned char* r = (unsigned char*)c + e / hd * (hd / 2 + hd / 16); | ||
| if (active && (we & 1) == 0) r[we / 2] = (unsigned char)pair; | ||
| if (active && w == 0) *(u16*)(r + hd / 2 + 2 * g) = f2h(norm / fmaxf(recon, 1e-6f)); | ||
| } | ||
| } | ||
| #endif | ||
| __device__ __forceinline__ float warp_min(float v) { | ||
|
|
@@ -3203,12 +3228,23 @@ template <int M, int PER, int BITS = TQ_KBITS> __device__ __forceinline__ void l | |
| if constexpr (M >= 4) { | ||
| // Dequantize to the rotated domain; Q is rotated in attention, so no inverse here. | ||
| int we = off % hd; | ||
| const unsigned char* r = (const unsigned char*)c + off / hd * (hd / 2 + hd / 16); | ||
| float norm = h2f(*(const u16*)(r + hd / 2 + 2 * (we >> 7))); | ||
| for (int i = 0; i < PER / 2; i++) { | ||
| unsigned b = r[we / 2 + i]; | ||
| int q0 = b & 15, q1 = b >> 4; | ||
| o[i] = (unsigned)f2h(tq_centroid<BITS>(q0) * norm) | ((unsigned)f2h(tq_centroid<BITS>(q1) * norm) << 16); | ||
| if constexpr (BITS == 3) { | ||
| const unsigned char* r = (const unsigned char*)c + off / hd * (3 * hd / 8 + hd / 64); | ||
| float norm = h2f(*(const u16*)(r + 3 * hd / 8 + 2 * (we >> 7))); | ||
| for (int i = 0; i < PER / 2; i++) { | ||
| int a = we + 2 * i, b = a + 1; | ||
| int q0 = ((r[a / 4] >> (2 * (a & 3))) & 3) | (((r[hd / 4 + a / 8] >> (a & 7)) & 1) << 2); | ||
| int q1 = ((r[b / 4] >> (2 * (b & 3))) & 3) | (((r[hd / 4 + b / 8] >> (b & 7)) & 1) << 2); | ||
| o[i] = (unsigned)f2h(tq_centroid<BITS>(q0) * norm) | ((unsigned)f2h(tq_centroid<BITS>(q1) * norm) << 16); | ||
| } | ||
| } else { | ||
| const unsigned char* r = (const unsigned char*)c + off / hd * (hd / 2 + hd / 16); | ||
| float norm = h2f(*(const u16*)(r + hd / 2 + 2 * (we >> 7))); | ||
| for (int i = 0; i < PER / 2; i++) { | ||
| unsigned b = r[we / 2 + i]; | ||
| int q0 = b & 15, q1 = b >> 4; | ||
| o[i] = (unsigned)f2h(tq_centroid<BITS>(q0) * norm) | ((unsigned)f2h(tq_centroid<BITS>(q1) * norm) << 16); | ||
| } | ||
| } | ||
| } else if constexpr (M == 3) { | ||
| int w = off % hd; | ||
|
|
@@ -3304,11 +3340,18 @@ __device__ __forceinline__ float rh(float x) { return h2f(f2h(x)); } | |
| template <int M, int BITS = TQ_KBITS> __device__ __forceinline__ float ldkv1(const u16* c, size_t off, int hd) { | ||
| if constexpr (M >= 4) { | ||
| int we = off % hd; | ||
| const unsigned char* r = (const unsigned char*)c + off / hd * (hd / 2 + hd / 16); | ||
| float norm = h2f(*(const u16*)(r + hd / 2 + 2 * (we >> 7))); | ||
| unsigned b = r[we / 2]; | ||
| int q = (we & 1) == 0 ? (int)(b & 15u) : (int)(b >> 4); | ||
| return rh(tq_centroid<BITS>(q) * norm); | ||
| if constexpr (BITS == 3) { | ||
| const unsigned char* r = (const unsigned char*)c + off / hd * (3 * hd / 8 + hd / 64); | ||
| float norm = h2f(*(const u16*)(r + 3 * hd / 8 + 2 * (we >> 7))); | ||
| int q = ((r[we / 4] >> (2 * (we & 3))) & 3) | (((r[hd / 4 + we / 8] >> (we & 7)) & 1) << 2); | ||
| return rh(tq_centroid<BITS>(q) * norm); | ||
| } else { | ||
| const unsigned char* r = (const unsigned char*)c + off / hd * (hd / 2 + hd / 16); | ||
| float norm = h2f(*(const u16*)(r + hd / 2 + 2 * (we >> 7))); | ||
| unsigned b = r[we / 2]; | ||
| int q = (we & 1) == 0 ? (int)(b & 15u) : (int)(b >> 4); | ||
| return rh(tq_centroid<BITS>(q) * norm); | ||
| } | ||
| } else if constexpr (M == 3) { | ||
| int w = off % hd; | ||
| const unsigned char* r = (const unsigned char*)c + off / hd * (3 * hd / 8); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -204,7 +204,7 @@ pub fn kv_mode() -> u32 { | |
|
|
||
| /// u16 words a cache row of `n` values occupies in mode `m`. | ||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 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 agentReply |
||
|
|
||
| /// `--kv q8`: KV cache rows in ggml q8_0 form (`kv_mode() == 1`). | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -111,25 +111,61 @@ float tq_group_reduce(float cs, uint slot) { | |
| } | ||
| // Store one rotated, quantized value. Active lanes join the reduction; inactive ones contribute no energy and write nothing. | ||
| void tq_put(U16 c, uint e, uint hd, float x, float norm, bool valid, uint bits, uint slot) { | ||
| int q = tq_nearest(bits, fdiv(x, norm)); | ||
| uint nib = uint(q), pair = nib | (subgroupShuffleXor(nib, 1u) << 4); | ||
| // Centroids are calibrated for a 128-value WHT group. A 64-value group has twice the variance, so scale | ||
| // the norm by sqrt(128 / group) on store and keep the corrected norm in the same scaled units. | ||
| uint gw = hd >= 128u ? 128u : hd; | ||
| float qnorm = norm * sqrt_(fdiv(128.0, float(gw))); | ||
| int q = tq_nearest(bits, fdiv(x, qnorm)); | ||
| uint we = e % hd, g = we >> 7u, w = we & 127u; | ||
| uint r = e / hd * (hd / 2u + hd / 16u); | ||
| if (valid && (we & 1u) == 0u) U8(uint64_t(c)).v[r + we / 2u] = uint8_t(pair); | ||
| float cen = tq_centroid(bits, q); | ||
| float recon = sqrt_(tq_group_reduce(valid ? cen * cen : 0.0, slot)); | ||
| if (bits == 3u) { | ||
| // 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 High · Correctness · 3 of 6
Prompt for your AI agentReply |
||
| uint s8 = sign | (subgroupShuffleXor(sign, 1u) << 1) | (subgroupShuffleXor(sign, 2u) << 2) | ||
| | (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); | ||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 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 agentReply |
||
| uint nib = uint(q), pair = nib | (subgroupShuffleXor(nib, 1u) << 4); | ||
| uint r = e / hd * (hd / 2u + hd / 16u); | ||
| if (valid && (we & 1u) == 0u) U8(uint64_t(c)).v[r + we / 2u] = uint8_t(pair); | ||
| if (valid && w == 0u) | ||
| c.v[(r + hd / 2u) / 2u + g] = uint16_t(f2h(fdiv(norm, max(recon, 1e-6)))); | ||
| } | ||
| float tq_ldkv(U16 c, uint off, uint hd, uint bits) { | ||
| uint we = off % hd, r = off / hd * (hd / 2u + hd / 16u); | ||
| uint we = off % hd; | ||
| if (bits == 3u) { | ||
| uint r = off / hd * (3u * hd / 8u + hd / 64u); | ||
| float norm = float(uint16BitsToHalf(c.v[(r + 3u * hd / 8u) / 2u + (we >> 7u)])); | ||
| uint m = (uint(U8(uint64_t(c)).v[r + we / 4u]) >> (2u * (we & 3u))) & 3u; | ||
| uint s = (uint(U8(uint64_t(c)).v[r + hd / 4u + we / 8u]) >> (we & 7u)) & 1u; | ||
| return rh(tq_centroid(3u, int(m | (s << 2u))) * norm); | ||
| } | ||
| uint r = off / hd * (hd / 2u + hd / 16u); | ||
| float norm = float(uint16BitsToHalf(c.v[(r + hd / 2u) / 2u + (we >> 7u)])); | ||
| uint b = uint(U8(uint64_t(c)).v[r + we / 2u]); | ||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 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 agentReply |
||
| uint a0 = off % hd, r = off / hd * (hd / 2u + hd / 16u); | ||
| uint a0 = off % hd; | ||
| if (bits == 3u) { | ||
| uint r = off / hd * (3u * hd / 8u + hd / 64u); | ||
| uint lo = f2h(tq_ldkv(c, off, hd, bits)); | ||
| uint a1 = (off + 1u) % hd; | ||
| float n1 = float(uint16BitsToHalf(c.v[(r + 3u * hd / 8u) / 2u + (a1 >> 7u)])); | ||
| uint m1 = (uint(U8(uint64_t(c)).v[r + a1 / 4u]) >> (2u * (a1 & 3u))) & 3u; | ||
| uint s1 = (uint(U8(uint64_t(c)).v[r + hd / 4u + a1 / 8u]) >> (a1 & 7u)) & 1u; | ||
| return lo | (f2h(tq_centroid(3u, int(m1 | (s1 << 2u))) * n1) << 16); | ||
| } | ||
| uint r = off / hd * (hd / 2u + hd / 16u); | ||
| float n0 = float(uint16BitsToHalf(c.v[(r + hd / 2u) / 2u + (a0 >> 7u)])); | ||
| uint b = uint(U8(uint64_t(c)).v[r + a0 / 2u]); | ||
| uint lo = f2h(tq_centroid(bits, int(b & 15u)) * n0); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 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
Reply
/dismiss(optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.