// src/kernels/cuda/scr_move.cu - see include/strata/kernels/scr_move.hpp. #include "strata/kernels/scr_move.hpp" #include "strata/kernels/f16_bits.hpp" #include #include #include namespace strata::kernels { namespace { constexpr int GROUP = 64; // KV_Q8_GROUP: one FP16 scale per 64 values __device__ __forceinline__ long long row_of(long long pos, int h, int kv_heads, int page_size) { return ((pos / page_size) * kv_heads + h) * page_size + pos % page_size; } // cos/sin at `pos` for rotated pair `pair`, the fresh K write's angle (native_rope.cu `apply`) __device__ __forceinline__ void angle(long long pos, int pair, float theta_scale, const RopeKernelArgs& k, float& c, float& s) { const float theta_extrap = (float) pos * powf(theta_scale, float(pair)); rope_scaled_angle(theta_extrap, k.freq_scale, k.corr_low, k.corr_high, k.ext_factor, k.attn_factor, pair, c, s); } // scratch per cell and head: K codes, V codes (head_dim each), the scales (head_dim / 64 each), the unrotated K group // (n_rot FP16) struct Scratch { int8_t* k; int8_t* v; uint16_t* ks; uint16_t* vs; uint16_t* pre; }; __host__ __device__ inline Scratch scratch_of(void* base, int64_t chunk, int kv_heads, int head_dim, int n_rot) { const int64_t cells = chunk * kv_heads, groups = head_dim / GROUP; auto* p = static_cast(base); Scratch s; s.k = reinterpret_cast(p); p += cells * head_dim; s.v = reinterpret_cast(p); p += cells * head_dim; s.ks = reinterpret_cast(p); p += cells * groups * 2; s.vs = reinterpret_cast(p); p += cells * groups * 2; s.pre = reinterpret_cast(p); return s; } // one block per (cell, head): the cell's codes and scales into scratch, and its K group back at position zero __global__ void gather(ScrKvLayer l, long long src, Scratch s, float theta_scale, RopeKernelArgs k) { const long long i = blockIdx.x; // cell within the chunk const int h = blockIdx.y, t = threadIdx.x; const long long pos = src + i; const long long row = row_of(pos, h, l.kv_heads, l.page_size); const long long j = i * l.kv_heads + h; const int groups = l.head_dim / GROUP; for (int d = t; d < l.head_dim; d += blockDim.x) { s.k[j * l.head_dim + d] = l.k_q[row * l.head_dim + d]; s.v[j * l.head_dim + d] = l.v_q[row * l.head_dim + d]; } if (t < groups) { s.ks[j * groups + t] = l.k_scale[row * groups + t]; s.vs[j * groups + t] = l.v_scale[row * groups + t]; } const int half = l.n_rot / 2; if (t >= half) return; uint16_t* pre = s.pre + j * l.n_rot; if (l.master != nullptr && l.master_valid[pos]) { const uint16_t* m = l.master + (pos * l.kv_heads + h) * l.n_rot; pre[t] = m[t]; pre[t + half] = m[t + half]; return; } const float sc = f32_from_f16(l.k_scale[row * groups]); 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; float c, sn; angle(pos, t, theta_scale, k, c, sn); // the inverse of (a, b) -> (a c - b s, a s + b c), whose matrix is mscale times a rotation const float m2 = c * c + sn * sn; pre[t] = f16_from_f32((a * c + b * sn) / m2); pre[t + half] = f16_from_f32((b * c - a * sn) / m2); } // one block per (cell, head), GROUP threads: the codes back from scratch at the target, the K group turned to the // target's angle and quantized as kv_append_q8 quantizes it __global__ void scatter(ScrKvLayer l, long long dst, Scratch s, float theta_scale, RopeKernelArgs k) { const long long i = blockIdx.x; const int h = blockIdx.y, t = threadIdx.x; const long long pos = dst + i; const long long row = row_of(pos, h, l.kv_heads, l.page_size); const long long j = i * l.kv_heads + h; const int groups = l.head_dim / GROUP; for (int d = t; d < l.head_dim; d += blockDim.x) { if (d >= GROUP) l.k_q[row * l.head_dim + d] = s.k[j * l.head_dim + d]; l.v_q[row * l.head_dim + d] = s.v[j * l.head_dim + d]; } if (t < groups) { if (t > 0) l.k_scale[row * groups + t] = s.ks[j * groups + t]; l.v_scale[row * groups + t] = s.vs[j * groups + t]; } // the rotated group: n_rot == GROUP, one value a thread const int half = l.n_rot / 2, pair = t < half ? t : t - half; const uint16_t* pre = s.pre + j * l.n_rot; const float a = f32_from_f16(pre[pair]), b = f32_from_f16(pre[pair + half]); float c, sn; angle(pos, pair, theta_scale, k, c, sn); const float x = t < half ? a * c - b * sn : a * sn + b * c; float m = fabsf(x); for (int o = 16; o > 0; o >>= 1) m = fmaxf(m, __shfl_xor_sync(0xffffffffu, m, o)); __shared__ float warp_max[2]; if ((t & 31) == 0) warp_max[t >> 5] = m; __syncthreads(); const float amax = fmaxf(warp_max[0], warp_max[1]); const uint16_t sbits = f16_from_f32(amax / 127.0f); const float sf = f32_from_f16(sbits); int q = 0; if (sf > 0.0f) { q = __float2int_rn(x / sf); q = q < -127 ? -127 : (q > 127 ? 127 : q); } l.k_q[row * l.head_dim + t] = (int8_t) q; if (t == 0) l.k_scale[row * groups] = sbits; if (l.master != nullptr) { l.master[(pos * l.kv_heads + h) * l.n_rot + t] = pre[t]; if (t == 0 && h == 0) l.master_valid[pos] = 1; } } __global__ void pooled_gather(const float* pooled, int dim, long long b0, float* scratch) { const long long i = blockIdx.x; for (int d = threadIdx.x; d < dim; d += blockDim.x) scratch[i * dim + d] = pooled[(b0 + i) * dim + d]; } __global__ void pooled_scatter(float* pooled, int dim, int n_rot, int block_size, long long b0, long long shift, const float* scratch, float theta_scale, RopeKernelArgs k) { const long long i = blockIdx.x; const int half = n_rot / 2; const long long from = block_size * (b0 + i), to = block_size * (b0 + i + shift); for (int d = threadIdx.x; d < dim; d += blockDim.x) { const float* row = scratch + i * dim; float y = row[d]; if (d < n_rot) { const int pair = d % half; float c0, s0, c1, s1; angle(from, pair, theta_scale, k, c0, s0); angle(to, pair, theta_scale, k, c1, s1); const float m2 = c0 * c0 + s0 * s0; const float a = (row[pair] * c0 + row[pair + half] * s0) / m2; const float z = (row[pair + half] * c0 - row[pair] * s0) / m2; y = d < half ? a * c1 - z * s1 : a * s1 + z * c1; } pooled[(b0 + i + shift) * dim + d] = y; } } __global__ void invalidate(uint8_t* valid, long long from, long long n) { const long long i = blockIdx.x * (long long) blockDim.x + threadIdx.x; if (i < n) valid[from + i] = 0; } // the chunks of a move in an order that never overwrites a source still to be read template void chunks(int64_t n, int64_t shift, int64_t chunk, F f) { const int64_t count = (n + chunk - 1) / chunk; for (int64_t c = 0; c < count; ++c) { const int64_t k = shift > 0 ? count - 1 - c : c; const int64_t a = k * chunk; f(a, std::min(chunk, n - a)); } } } // namespace uint64_t scr_move_kv_scratch_bytes(const ScrKvLayer& l, int64_t chunk) { const uint64_t cells = (uint64_t) chunk * l.kv_heads, groups = (uint64_t) l.head_dim / GROUP; return cells * (2ull * l.head_dim + 4ull * groups + 2ull * l.n_rot); } bool scr_move_kv(const ScrKvLayer& l, int64_t src, int64_t n, int64_t shift, const RopeScaling& rope, void* scratch, int64_t chunk, void* stream) { if (!l.k_q || !l.v_q || !l.k_scale || !l.v_scale || !scratch || chunk < 1 || n < 0 || l.n_rot != GROUP || l.head_dim % GROUP != 0 || l.kv_heads < 1 || l.page_size < 1 || (l.master && !l.master_valid) || src < 0 || src + n > l.max_cells || src + shift < 0 || src + shift + n > l.max_cells || chunk > 2147483647ll || rope_scaling_invalid(rope) != nullptr) return false; if (n == 0 || shift == 0) return true; auto st = static_cast(stream); const float theta_scale = powf((float) rope.freq_base, -2.0f / l.n_rot); const RopeKernelArgs k = rope.kernel_args(l.n_rot); const Scratch s = scratch_of(scratch, chunk, l.kv_heads, l.head_dim, l.n_rot); chunks(n, shift, chunk, [&](int64_t a, int64_t m) { const dim3 grid((unsigned) m, (unsigned) l.kv_heads); gather<<>>(l, src + a, s, theta_scale, k); scatter<<>>(l, src + a + shift, s, theta_scale, k); }); return cudaGetLastError() == cudaSuccess; } bool scr_move_pooled(float* pooled, int64_t rows, int dim, int n_rot, int block_size, int64_t b0, int64_t n, int64_t shift_blocks, const RopeScaling& rope, float* scratch, int64_t chunk, void* stream) { if (!pooled || !scratch || n < 0 || chunk < 1 || n_rot % 2 || n_rot > dim || b0 < 0 || b0 + n > rows || b0 + shift_blocks < 0 || b0 + shift_blocks + n > rows || rope_scaling_invalid(rope) != nullptr) return false; if (n == 0 || shift_blocks == 0) return true; auto st = static_cast(stream); const float theta_scale = powf((float) rope.freq_base, -2.0f / n_rot); const RopeKernelArgs k = rope.kernel_args(n_rot); chunks(n, shift_blocks, chunk, [&](int64_t a, int64_t m) { pooled_gather<<<(unsigned) m, 128, 0, st>>>(pooled, dim, b0 + a, scratch); pooled_scatter<<<(unsigned) m, 128, 0, st>>>(pooled, dim, n_rot, block_size, b0 + a, shift_blocks, scratch, theta_scale, k); }); return cudaGetLastError() == cudaSuccess; } void scr_master_invalidate(const ScrKvLayer& l, int64_t from, int64_t to, void* stream) { if (!l.master_valid || to <= from) return; from = std::max(from, 0); to = std::min(to, l.max_cells); const long long n = to - from; if (n > 0) invalidate<<<(unsigned) ((n + 255) / 256), 256, 0, static_cast(stream)>>>(l.master_valid, from, n); } } // namespace strata::kernels