okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
44ffa86c5
main
1// src/kernels/cuda/kv_q4.cu - see include/strata/kernels/kv_q4.hpp. Q4_0 KV with Walsh-Hadamard rotation 2// (from PR #21 by code-martin; KV-streaming integration and the deterministic group maximum added on merge). 3#include "strata/kernels/kv_q4.hpp" 4#include "strata/kernels/f16_bits.hpp" 5#include "strata/kernels/kv_stream.hpp" 6 7#include <cuda_fp16.h> 8#include <cuda_runtime.h> 9#include <cstdio> 10#include <cstdlib> 11 12namespace strata::kernels { 13namespace { 14 15void check(const char* what) { 16 const cudaError_t e = cudaGetLastError(); 17 if (e != cudaSuccess) { 18 std::fprintf(stderr, "kv_q4: %s: %s\n", what, cudaGetErrorString(e)); 19 std::exit(1); 20 } 21} 22 23// Fast Walsh-Hadamard Transform for N = 256: one warp per row, 8 values per lane in registers. 24// Orthonormal (scale 1/sqrt(256) = 1/16), so it is its own inverse. 25__global__ void fwht256_kernel(const float* __restrict__ src, float* __restrict__ dst, int64_t n_rows, float scale) { 26 constexpr int warp_size = 32; 27 constexpr int N = 256; 28 constexpr int el_w = N / warp_size; // 8 29 30 const int64_t r = (int64_t) blockIdx.x * blockDim.y + threadIdx.y; 31 if (r >= n_rows) return; 32 33 const float* row_src = src + r * N; 34 float* row_dst = dst + r * N; 35 36 float reg[el_w]; 37 const int lane = threadIdx.x; 38 39#pragma unroll 40 for (int i = 0; i < el_w; ++i) reg[i] = row_src[i * warp_size + lane] * scale; 41 42 // the low 5 index bits live across lanes 43#pragma unroll 44 for (int h = 1; h < warp_size; h *= 2) { 45#pragma unroll 46 for (int j = 0; j < el_w; ++j) { 47 const float val = reg[j]; 48 const float val2 = __shfl_xor_sync(0xffffffffu, val, h, warp_size); 49 reg[j] = (lane & h) == 0 ? val + val2 : val2 - val; 50 } 51 } 52 // the high 3 bits across each lane's registers 53#pragma unroll 54 for (int h = warp_size; h < N; h *= 2) { 55 const int step = h / warp_size; 56#pragma unroll 57 for (int j = 0; j < el_w; j += 2 * step) { 58#pragma unroll 59 for (int k = 0; k < step; ++k) { 60 const float x = reg[j + k]; 61 const float y = reg[j + k + step]; 62 reg[j + k] = x + y; 63 reg[j + k + step] = x - y; 64 } 65 } 66 } 67#pragma unroll 68 for (int i = 0; i < el_w; ++i) row_dst[i * warp_size + lane] = reg[i]; 69} 70 71// One 32-value group in one warp (lane = element), ggml's q4_0: d = (the value of largest |x|) / -8, 72// q = clamp(trunc(x / d + 8.5), 0, 15). Returns the scale bits; `byte` is lane t's packed byte for t < 16 73// (element t in the low nibble, t + 16 in the high one). Ties in |x| resolve to the larger value in EVERY lane, 74// so all lanes agree on d (a plain `a > amax` could leave lanes with opposite signs and one block two scales). 75__device__ __forceinline__ uint16_t q4_group(float x, int lane, uint8_t& byte) { 76 float amax = fabsf(x), mval = x; 77#pragma unroll 78 for (int o = 16; o > 0; o >>= 1) { 79 const float a = __shfl_xor_sync(0xffffffffu, amax, o); 80 const float v = __shfl_xor_sync(0xffffffffu, mval, o); 81 if (a > amax || (a == amax && v > mval)) { amax = a; mval = v; } 82 } 83 const float d = mval / -8.0f; 84 const float id = d != 0.0f ? 1.0f / d : 0.0f; 85 int q = __float2int_rz(x * id + 8.5f); 86 const uint8_t qc = (uint8_t) (q < 0 ? 0 : (q > 15 ? 15 : q)); 87 const uint8_t qhi = __shfl_down_sync(0xffffffffu, qc, 16); 88 byte = (uint8_t) (qc | (qhi << 4)); 89 (void) lane; 90 return f16_from_f32(d); 91} 92 93__device__ __forceinline__ void q4_store(uint8_t* pool, long long row, int b, int lane, uint16_t d, uint8_t byte) { 94 block_q4_0* blk = reinterpret_cast<block_q4_0*>(pool + row * (long long) sizeof(block_q4_0) * 8) + b; 95 if (lane == 0) blk->d = d; 96 if (lane < 16) blk->qs[lane] = byte; 97} 98 99// One block = one 32-value group of one KV head of K (plane 0) or V (plane 1) for token step_idx; 32 threads. 100__global__ void kv_append_q4_kernel(uint8_t* __restrict__ k_q4, uint8_t* __restrict__ v_q4, 101 const int32_t* __restrict__ table, const int32_t* __restrict__ step, 102 int step_stride, int planes, 103 const float* __restrict__ kcur, const float* __restrict__ vcur, 104 int kv_heads, int head_dim, int page_size, KvHostPools host) { 105 const int step_idx = (int) (blockIdx.z / (unsigned) planes); 106 const bool is_v = (blockIdx.z % (unsigned) planes) == 1u; 107 const long long pos = (long long) __ldg(step + (long long) step_idx * step_stride + kStepPos); 108 const int h = blockIdx.x, b = blockIdx.y, t = threadIdx.x; 109 const long long step_off = (long long) step_idx * kv_heads * head_dim; 110 const float x = (is_v ? vcur : kcur)[step_off + h * head_dim + b * QK4_0 + t]; 111 uint8_t byte; 112 const uint16_t d = q4_group(x, t, byte); 113 const long long page = (long long) table[pos / page_size]; 114 if (page >= 0) q4_store(is_v ? v_q4 : k_q4, (page * kv_heads + h) * page_size + (pos % page_size), b, t, d, byte); 115 if (host.k_q4 != nullptr) 116 q4_store(is_v ? host.v_q4 : host.k_q4, ((pos / page_size) * kv_heads + h) * page_size + (pos % page_size), b, t, 117 d, byte); 118} 119 120// The prompt path: grid (T, kv_heads, groups), K then V; also into the staging pool (identity layout) when given. 121__global__ void kv_append_q4_batch_kernel(uint8_t* __restrict__ k_q4, uint8_t* __restrict__ v_q4, 122 const int32_t* __restrict__ table, int64_t pos0, 123 const float* __restrict__ K, const float* __restrict__ V, 124 int kv_heads, int head_dim, int page_size, int is_v_grid, KvHostPools host, 125 KvHostPools stage) { 126 const long long t = blockIdx.x; 127 const long long pos = pos0 + t; 128 const int h = blockIdx.y, b = blockIdx.z, th = threadIdx.x; 129 const bool is_v = is_v_grid != 0; 130 const float x = (is_v ? V : K)[t * (kv_heads * head_dim) + h * head_dim + b * QK4_0 + th]; 131 uint8_t byte; 132 const uint16_t d = q4_group(x, th, byte); 133 const long long page = (long long) table[pos / page_size]; 134 const long long row_id = ((pos / page_size) * kv_heads + h) * page_size + (pos % page_size); 135 if (page >= 0) q4_store(is_v ? v_q4 : k_q4, (page * kv_heads + h) * page_size + (pos % page_size), b, th, d, byte); 136 if (host.k_q4 != nullptr) q4_store(is_v ? host.v_q4 : host.k_q4, row_id, b, th, d, byte); 137 if (stage.k_q4 != nullptr) q4_store(is_v ? stage.v_q4 : stage.k_q4, row_id, b, th, d, byte); 138} 139 140// Gather step[kStepWidth] cells into FP16 scratch (the non-fused attention paths) 141__global__ void kv_gather_q4_kernel(const uint8_t* __restrict__ k_q4, const uint8_t* __restrict__ v_q4, 142 const int32_t* __restrict__ table, const int32_t* __restrict__ ids, 143 const int32_t* __restrict__ step, int kv_heads, int head_dim, int page_size, 144 uint16_t* __restrict__ k_scratch, uint16_t* __restrict__ v_scratch) { 145 const long long n_ids = (long long) __ldg(step + kStepWidth); 146 const int blocks_per_head = head_dim / QK4_0; // 8 147 const int bytes_per_head = blocks_per_head * sizeof(block_q4_0); // 144 148 const long long total_blocks = n_ids * kv_heads * blocks_per_head; 149 150 const long long blk_idx = (long long) blockIdx.x * blockDim.y + threadIdx.y; 151 if (blk_idx >= total_blocks) return; 152 153 const int t = threadIdx.x; 154 const long long id = blk_idx / (kv_heads * blocks_per_head); 155 const int rem = (int) (blk_idx % (kv_heads * blocks_per_head)); 156 const int h = rem / blocks_per_head; 157 const int b = rem % blocks_per_head; 158 159 const int cell = ids[id]; 160 const long long page = (long long) table[cell / page_size]; 161 const long long row = (page * kv_heads + h) * page_size + (cell % page_size); 162 163 const block_q4_0* k_blk = reinterpret_cast<const block_q4_0*>(k_q4 + row * bytes_per_head) + b; 164 const block_q4_0* v_blk = reinterpret_cast<const block_q4_0*>(v_q4 + row * bytes_per_head) + b; 165 const float kd = f32_from_f16(k_blk->d); 166 const float vd = f32_from_f16(v_blk->d); 167 const int j = t < 16 ? t : (t - 16); 168 const uint8_t k_byte = k_blk->qs[j]; 169 const uint8_t v_byte = v_blk->qs[j]; 170 const int kq = (t < 16) ? ((k_byte & 0x0F) - 8) : ((k_byte >> 4) - 8); 171 const int vq = (t < 16) ? ((v_byte & 0x0F) - 8) : ((v_byte >> 4) - 8); 172 const long long dst_offset = ((id * kv_heads + h) * head_dim) + (b * QK4_0 + t); 173 k_scratch[dst_offset] = f16_from_f32((float) kq * kd); 174 v_scratch[dst_offset] = f16_from_f32((float) vq * vd); 175} 176 177void need_256(const QsaShapes& s, const char* what) { 178 if (s.head_dim != 256) { 179 std::fprintf(stderr, "%s: head_dim must be 256 (the Hadamard transform's size)\n", what); 180 std::exit(1); 181 } 182} 183 184} // namespace 185 186void fwht256_cuda(const float* src, float* dst, int64_t n_rows, void* stream) { 187 if (n_rows <= 0) return; 188 const int rows_per_block = 4; 189 const int64_t num_blocks = (n_rows + rows_per_block - 1) / rows_per_block; 190 fwht256_kernel<<<dim3((unsigned) num_blocks), dim3(32, rows_per_block), 0, (cudaStream_t) stream>>>( 191 src, dst, n_rows, 1.0f / 16.0f); 192 check("fwht256 launch"); 193} 194 195void kv_append_q4_steps(uint8_t* k_q4, uint8_t* v_q4, const int32_t* page_table, const int32_t* step, 196 int step_stride, int n_steps, const float* kcur, const float* vcur, const QsaShapes& s, 197 void* stream, const KvHostPools* host) { 198 if (n_steps <= 0) return; 199 need_256(s, "kv_append_q4"); 200 const int planes = (v_q4 == nullptr || (v_q4 == k_q4 && vcur == kcur)) ? 1 : 2; 201 const dim3 grid((unsigned) s.n_head_kv, (unsigned) (s.head_dim / QK4_0), (unsigned) (n_steps * planes)); 202 kv_append_q4_kernel<<<grid, 32, 0, (cudaStream_t) stream>>>( 203 k_q4, v_q4, page_table, step, step_stride, planes, kcur, vcur, (int) s.n_head_kv, (int) s.head_dim, 204 (int) s.page_size, host ? *host : KvHostPools{}); 205 check("kv_append_q4 launch"); 206} 207 208void kv_append_q4_step(uint8_t* k_q4, uint8_t* v_q4, const int32_t* page_table, const int32_t* step, 209 const float* kcur, const float* vcur, const QsaShapes& s, void* stream, 210 const KvHostPools* host) { 211 kv_append_q4_steps(k_q4, v_q4, page_table, step, 0, 1, kcur, vcur, s, stream, host); 212} 213 214void kv_append_q4(uint8_t* k_q4, uint8_t* v_q4, const int32_t* page_table, int64_t pos0, int64_t T, const float* K, 215 const float* V, const QsaShapes& s, void* stream, const KvHostPools* host, const KvHostPools* stage) { 216 if (T <= 0) return; 217 need_256(s, "kv_append_q4"); 218 const dim3 grid((unsigned) T, (unsigned) s.n_head_kv, (unsigned) (s.head_dim / QK4_0)); 219 cudaStream_t cs = (cudaStream_t) stream; 220 const KvHostPools h = host ? *host : KvHostPools{}, st = stage ? *stage : KvHostPools{}; 221 for (int is_v = 0; is_v < 2; ++is_v) 222 kv_append_q4_batch_kernel<<<grid, 32, 0, cs>>>(k_q4, v_q4, page_table, pos0, K, V, (int) s.n_head_kv, 223 (int) s.head_dim, (int) s.page_size, is_v, h, st); 224 check("kv_append_q4 batch launch"); 225} 226 227void kv_gather_q4_step(const uint8_t* k_q4, const uint8_t* v_q4, const int32_t* page_table, const int32_t* ids, 228 const int32_t* step, int64_t max_ids, const QsaShapes& s, uint16_t* k_scratch, 229 uint16_t* v_scratch, void* stream) { 230 if (max_ids <= 0) return; 231 const int blocks_per_head = (int) (s.head_dim / QK4_0); 232 const int64_t total_blocks = max_ids * s.n_head_kv * blocks_per_head; 233 const int rows_per_block = 4; 234 const unsigned num_blocks = (unsigned) ((total_blocks + rows_per_block - 1) / rows_per_block); 235 kv_gather_q4_kernel<<<dim3(num_blocks), dim3(32, rows_per_block), 0, (cudaStream_t) stream>>>( 236 k_q4, v_q4, page_table, ids, step, (int) s.n_head_kv, (int) s.head_dim, (int) s.page_size, k_scratch, 237 v_scratch); 238 check("kv_gather_q4 launch"); 239} 240 241} // namespace strata::kernels