okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
5bb697bfc
main
1// src/kernels/cuda/scr_move.cu - see include/strata/kernels/scr_move.hpp. 2#include "strata/kernels/scr_move.hpp" 3#include "strata/kernels/f16_bits.hpp" 4 5#include <cuda_runtime.h> 6 7#include <algorithm> 8#include <cmath> 9 10namespace strata::kernels { 11namespace { 12 13constexpr int GROUP = 64; // KV_Q8_GROUP: one FP16 scale per 64 values 14 15__device__ __forceinline__ long long row_of(long long pos, int h, int kv_heads, int page_size) { 16 return ((pos / page_size) * kv_heads + h) * page_size + pos % page_size; 17} 18 19// cos/sin at `pos` for rotated pair `pair`, the fresh K write's angle (native_rope.cu `apply`) 20__device__ __forceinline__ void angle(long long pos, int pair, float theta_scale, const RopeKernelArgs& k, float& c, 21 float& s) { 22 const float theta_extrap = (float) pos * powf(theta_scale, float(pair)); 23 rope_scaled_angle(theta_extrap, k.freq_scale, k.corr_low, k.corr_high, k.ext_factor, k.attn_factor, pair, c, s); 24} 25 26// scratch per cell and head: K codes, V codes (head_dim each), the scales (head_dim / 64 each), the unrotated K group 27// (n_rot FP16) 28struct Scratch { 29 int8_t* k; int8_t* v; uint16_t* ks; uint16_t* vs; uint16_t* pre; 30}; 31__host__ __device__ inline Scratch scratch_of(void* base, int64_t chunk, int kv_heads, int head_dim, int n_rot) { 32 const int64_t cells = chunk * kv_heads, groups = head_dim / GROUP; 33 auto* p = static_cast<uint8_t*>(base); 34 Scratch s; 35 s.k = reinterpret_cast<int8_t*>(p); p += cells * head_dim; 36 s.v = reinterpret_cast<int8_t*>(p); p += cells * head_dim; 37 s.ks = reinterpret_cast<uint16_t*>(p); p += cells * groups * 2; 38 s.vs = reinterpret_cast<uint16_t*>(p); p += cells * groups * 2; 39 s.pre = reinterpret_cast<uint16_t*>(p); 40 return s; 41} 42 43// one block per (cell, head): the cell's codes and scales into scratch, and its K group back at position zero 44__global__ void gather(ScrKvLayer l, long long src, Scratch s, float theta_scale, RopeKernelArgs k) { 45 const long long i = blockIdx.x; // cell within the chunk 46 const int h = blockIdx.y, t = threadIdx.x; 47 const long long pos = src + i; 48 const long long row = row_of(pos, h, l.kv_heads, l.page_size); 49 const long long j = i * l.kv_heads + h; 50 const int groups = l.head_dim / GROUP; 51 for (int d = t; d < l.head_dim; d += blockDim.x) { 52 s.k[j * l.head_dim + d] = l.k_q[row * l.head_dim + d]; 53 s.v[j * l.head_dim + d] = l.v_q[row * l.head_dim + d]; 54 } 55 if (t < groups) { 56 s.ks[j * groups + t] = l.k_scale[row * groups + t]; 57 s.vs[j * groups + t] = l.v_scale[row * groups + t]; 58 } 59 const int half = l.n_rot / 2; 60 if (t >= half) return; 61 uint16_t* pre = s.pre + j * l.n_rot; 62 if (l.master != nullptr && l.master_valid[pos]) { 63 const uint16_t* m = l.master + (pos * l.kv_heads + h) * l.n_rot; 64 pre[t] = m[t]; 65 pre[t + half] = m[t + half]; 66 return; 67 } 68 const float sc = f32_from_f16(l.k_scale[row * groups]); 69 const float a = (float) l.k_q[row * l.head_dim + t] * sc, b = (float) l.k_q[row * l.head_dim + t + half] * sc; 70 float c, sn; 71 angle(pos, t, theta_scale, k, c, sn); 72 // the inverse of (a, b) -> (a c - b s, a s + b c), whose matrix is mscale times a rotation 73 const float m2 = c * c + sn * sn; 74 pre[t] = f16_from_f32((a * c + b * sn) / m2); 75 pre[t + half] = f16_from_f32((b * c - a * sn) / m2); 76} 77 78// one block per (cell, head), GROUP threads: the codes back from scratch at the target, the K group turned to the 79// target's angle and quantized as kv_append_q8 quantizes it 80__global__ void scatter(ScrKvLayer l, long long dst, Scratch s, float theta_scale, RopeKernelArgs k) { 81 const long long i = blockIdx.x; 82 const int h = blockIdx.y, t = threadIdx.x; 83 const long long pos = dst + i; 84 const long long row = row_of(pos, h, l.kv_heads, l.page_size); 85 const long long j = i * l.kv_heads + h; 86 const int groups = l.head_dim / GROUP; 87 for (int d = t; d < l.head_dim; d += blockDim.x) { 88 if (d >= GROUP) l.k_q[row * l.head_dim + d] = s.k[j * l.head_dim + d]; 89 l.v_q[row * l.head_dim + d] = s.v[j * l.head_dim + d]; 90 } 91 if (t < groups) { 92 if (t > 0) l.k_scale[row * groups + t] = s.ks[j * groups + t]; 93 l.v_scale[row * groups + t] = s.vs[j * groups + t]; 94 } 95 // the rotated group: n_rot == GROUP, one value a thread 96 const int half = l.n_rot / 2, pair = t < half ? t : t - half; 97 const uint16_t* pre = s.pre + j * l.n_rot; 98 const float a = f32_from_f16(pre[pair]), b = f32_from_f16(pre[pair + half]); 99 float c, sn; 100 angle(pos, pair, theta_scale, k, c, sn); 101 const float x = t < half ? a * c - b * sn : a * sn + b * c; 102 float m = fabsf(x); 103 for (int o = 16; o > 0; o >>= 1) m = fmaxf(m, __shfl_xor_sync(0xffffffffu, m, o)); 104 __shared__ float warp_max[2]; 105 if ((t & 31) == 0) warp_max[t >> 5] = m; 106 __syncthreads(); 107 const float amax = fmaxf(warp_max[0], warp_max[1]); 108 const uint16_t sbits = f16_from_f32(amax / 127.0f); 109 const float sf = f32_from_f16(sbits); 110 int q = 0; 111 if (sf > 0.0f) { 112 q = __float2int_rn(x / sf); 113 q = q < -127 ? -127 : (q > 127 ? 127 : q); 114 } 115 l.k_q[row * l.head_dim + t] = (int8_t) q; 116 if (t == 0) l.k_scale[row * groups] = sbits; 117 if (l.master != nullptr) { 118 l.master[(pos * l.kv_heads + h) * l.n_rot + t] = pre[t]; 119 if (t == 0 && h == 0) l.master_valid[pos] = 1; 120 } 121} 122 123__global__ void pooled_gather(const float* pooled, int dim, long long b0, float* scratch) { 124 const long long i = blockIdx.x; 125 for (int d = threadIdx.x; d < dim; d += blockDim.x) scratch[i * dim + d] = pooled[(b0 + i) * dim + d]; 126} 127 128__global__ void pooled_scatter(float* pooled, int dim, int n_rot, int block_size, long long b0, long long shift, 129 const float* scratch, float theta_scale, RopeKernelArgs k) { 130 const long long i = blockIdx.x; 131 const int half = n_rot / 2; 132 const long long from = block_size * (b0 + i), to = block_size * (b0 + i + shift); 133 for (int d = threadIdx.x; d < dim; d += blockDim.x) { 134 const float* row = scratch + i * dim; 135 float y = row[d]; 136 if (d < n_rot) { 137 const int pair = d % half; 138 float c0, s0, c1, s1; 139 angle(from, pair, theta_scale, k, c0, s0); 140 angle(to, pair, theta_scale, k, c1, s1); 141 const float m2 = c0 * c0 + s0 * s0; 142 const float a = (row[pair] * c0 + row[pair + half] * s0) / m2; 143 const float z = (row[pair + half] * c0 - row[pair] * s0) / m2; 144 y = d < half ? a * c1 - z * s1 : a * s1 + z * c1; 145 } 146 pooled[(b0 + i + shift) * dim + d] = y; 147 } 148} 149 150__global__ void invalidate(uint8_t* valid, long long from, long long n) { 151 const long long i = blockIdx.x * (long long) blockDim.x + threadIdx.x; 152 if (i < n) valid[from + i] = 0; 153} 154 155// the chunks of a move in an order that never overwrites a source still to be read 156template <class F> 157void chunks(int64_t n, int64_t shift, int64_t chunk, F f) { 158 const int64_t count = (n + chunk - 1) / chunk; 159 for (int64_t c = 0; c < count; ++c) { 160 const int64_t k = shift > 0 ? count - 1 - c : c; 161 const int64_t a = k * chunk; 162 f(a, std::min(chunk, n - a)); 163 } 164} 165 166} // namespace 167 168uint64_t scr_move_kv_scratch_bytes(const ScrKvLayer& l, int64_t chunk) { 169 const uint64_t cells = (uint64_t) chunk * l.kv_heads, groups = (uint64_t) l.head_dim / GROUP; 170 return cells * (2ull * l.head_dim + 4ull * groups + 2ull * l.n_rot); 171} 172 173bool scr_move_kv(const ScrKvLayer& l, int64_t src, int64_t n, int64_t shift, const RopeScaling& rope, void* scratch, 174 int64_t chunk, void* stream) { 175 if (!l.k_q || !l.v_q || !l.k_scale || !l.v_scale || !scratch || chunk < 1 || n < 0 || l.n_rot != GROUP || 176 l.head_dim % GROUP != 0 || l.kv_heads < 1 || l.page_size < 1 || (l.master && !l.master_valid) || src < 0 || 177 src + n > l.max_cells || src + shift < 0 || src + shift + n > l.max_cells || chunk > 2147483647ll || 178 rope_scaling_invalid(rope) != nullptr) 179 return false; 180 if (n == 0 || shift == 0) return true; 181 auto st = static_cast<cudaStream_t>(stream); 182 const float theta_scale = powf((float) rope.freq_base, -2.0f / l.n_rot); 183 const RopeKernelArgs k = rope.kernel_args(l.n_rot); 184 const Scratch s = scratch_of(scratch, chunk, l.kv_heads, l.head_dim, l.n_rot); 185 chunks(n, shift, chunk, [&](int64_t a, int64_t m) { 186 const dim3 grid((unsigned) m, (unsigned) l.kv_heads); 187 gather<<<grid, GROUP, 0, st>>>(l, src + a, s, theta_scale, k); 188 scatter<<<grid, GROUP, 0, st>>>(l, src + a + shift, s, theta_scale, k); 189 }); 190 return cudaGetLastError() == cudaSuccess; 191} 192 193bool scr_move_pooled(float* pooled, int64_t rows, int dim, int n_rot, int block_size, int64_t b0, int64_t n, 194 int64_t shift_blocks, const RopeScaling& rope, float* scratch, int64_t chunk, void* stream) { 195 if (!pooled || !scratch || n < 0 || chunk < 1 || n_rot % 2 || n_rot > dim || b0 < 0 || b0 + n > rows || 196 b0 + shift_blocks < 0 || b0 + shift_blocks + n > rows || rope_scaling_invalid(rope) != nullptr) 197 return false; 198 if (n == 0 || shift_blocks == 0) return true; 199 auto st = static_cast<cudaStream_t>(stream); 200 const float theta_scale = powf((float) rope.freq_base, -2.0f / n_rot); 201 const RopeKernelArgs k = rope.kernel_args(n_rot); 202 chunks(n, shift_blocks, chunk, [&](int64_t a, int64_t m) { 203 pooled_gather<<<(unsigned) m, 128, 0, st>>>(pooled, dim, b0 + a, scratch); 204 pooled_scatter<<<(unsigned) m, 128, 0, st>>>(pooled, dim, n_rot, block_size, b0 + a, shift_blocks, scratch, 205 theta_scale, k); 206 }); 207 return cudaGetLastError() == cudaSuccess; 208} 209 210void scr_master_invalidate(const ScrKvLayer& l, int64_t from, int64_t to, void* stream) { 211 if (!l.master_valid || to <= from) return; 212 from = std::max<int64_t>(from, 0); 213 to = std::min<int64_t>(to, l.max_cells); 214 const long long n = to - from; 215 if (n > 0) invalidate<<<(unsigned) ((n + 255) / 256), 256, 0, static_cast<cudaStream_t>(stream)>>>(l.master_valid, from, n); 216} 217 218} // namespace strata::kernels