Compare commits
1 Commits
step35/67-
...
step35/57-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ff5ee28a07 |
@@ -12,13 +12,18 @@ constant float turbo4_centroids[16] = {
|
||||
|
||||
// Fast Walsh-Hadamard Transform (In-place, SIMD-optimized)
|
||||
// Assumes d=128 (standard head dimension)
|
||||
// Security: bounds-checked — validates thread tile fits within data buffer
|
||||
kernel void kernel_fwht_128(
|
||||
device float* data [[buffer(0)]],
|
||||
constant uint& data_len [[buffer(1)]], // total elements in data buffer
|
||||
uint tid [[thread_position_in_grid]]
|
||||
) {
|
||||
const uint d = 128;
|
||||
uint base = tid * d;
|
||||
|
||||
|
||||
// Guard: thread's 128-float tile must be fully contained in buffer
|
||||
if (base >= data_len || base + d > data_len) return;
|
||||
|
||||
// Stage 1-7 (128 = 2^7)
|
||||
for (uint h = 1; h < d; h <<= 1) {
|
||||
for (uint i = 0; i < d; i += (h << 1)) {
|
||||
@@ -30,7 +35,7 @@ kernel void kernel_fwht_128(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Normalize
|
||||
float scale = 1.0 / sqrt(128.0);
|
||||
for (uint i = 0; i < d; i++) {
|
||||
@@ -40,37 +45,68 @@ kernel void kernel_fwht_128(
|
||||
|
||||
// PolarQuant Turbo4 Dequantization (Attention Hot Path)
|
||||
// Unpacks 4-bit indices, looks up centroids, scales by radius
|
||||
// Security: bounds-checked — validates all buffer accesses against lengths
|
||||
kernel void kernel_turbo4_dequant(
|
||||
device const uchar* src [[buffer(0)]],
|
||||
device const float* norms [[buffer(1)]],
|
||||
device float* dst [[buffer(2)]],
|
||||
constant uint& src_len [[buffer(1)]], // total bytes in src buffer
|
||||
device const float* norms [[buffer(2)]],
|
||||
constant uint& norms_len [[buffer(3)]], // total elements in norms
|
||||
device float* dst [[buffer(4)]],
|
||||
constant uint& dst_len [[buffer(5)]], // total elements in dst buffer
|
||||
uint tid [[thread_position_in_grid]]
|
||||
) {
|
||||
const uint d = 128;
|
||||
uint base_src = tid * (d / 2);
|
||||
uint base_dst = tid * d;
|
||||
uint base_src = tid * (d / 2); // byte offset into src (d/2 bytes per thread)
|
||||
uint base_dst = tid * d; // element offset into dst (d floats per thread)
|
||||
|
||||
// Guard norms before indexing (single element per thread)
|
||||
if (tid >= norms_len) return;
|
||||
|
||||
// Guard src: we read d/2 bytes from base_src
|
||||
if (base_src >= src_len) return;
|
||||
|
||||
// Guard dst: we write d floats from base_dst
|
||||
if (base_dst >= dst_len || base_dst + d > dst_len) return;
|
||||
|
||||
float norm = norms[tid];
|
||||
|
||||
|
||||
for (uint i = 0; i < d; i++) {
|
||||
uchar packed = src[base_src + (i / 2)];
|
||||
uint idx = (i % 2 == 0) ? (packed & 0x0F) : (packed >> 4);
|
||||
dst[base_dst + i] = turbo4_centroids[idx] * norm;
|
||||
}
|
||||
|
||||
|
||||
// Note: FWHT is applied separately or fused into attention
|
||||
}
|
||||
|
||||
// Fused Attention with TurboQuant (Conceptual)
|
||||
// This is where the real speed win happens
|
||||
// Security: bounds-checked — guards each buffer tile before any access
|
||||
kernel void kernel_attention_turbo4(
|
||||
device const float* q [[buffer(0)]],
|
||||
device const uchar* k_packed [[buffer(1)]],
|
||||
device const float* k_norms [[buffer(2)]],
|
||||
device float* scores [[buffer(3)]],
|
||||
constant uint& d [[buffer(4)]],
|
||||
constant uint& q_len [[buffer(1)]], // total elements in q buffer
|
||||
device const uchar* k_packed [[buffer(2)]],
|
||||
constant uint& k_packed_len [[buffer(3)]], // total bytes in k_packed
|
||||
device const float* k_norms [[buffer(4)]],
|
||||
constant uint& k_norms_len [[buffer(5)]], // total elements in k_norms
|
||||
device float* scores [[buffer(6)]],
|
||||
constant uint& scores_len [[buffer(7)]], // total elements in scores buffer
|
||||
constant uint& d [[buffer(8)]],
|
||||
uint tid [[thread_position_in_grid]]
|
||||
) {
|
||||
const uint local_d = d;
|
||||
uint base_q = tid * local_d;
|
||||
uint base_k = tid * local_d; // same tile size for KV
|
||||
uint base_s = tid; // one score per thread (simplified)
|
||||
|
||||
// Guard all inputs before any dereference
|
||||
if (base_q >= q_len || base_q + local_d > q_len) return;
|
||||
if (base_k >= k_packed_len || base_k + local_d > k_packed_len) return;
|
||||
if (tid >= k_norms_len) return;
|
||||
if (base_s >= scores_len || base_s + 1 > scores_len) return;
|
||||
|
||||
// 1. Dequantize K on the fly
|
||||
// 2. Compute dot product with Q
|
||||
// 3. Store score
|
||||
// (Implementation pending)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user