okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
5bb697bfc
main
1// src/kernels/scr_move_parity.cu - scr_move.cu against a CPU reference. Needs a CUDA device (a few MB). 2#include "strata/kernels/scr_move.hpp" 3#include "strata/kernels/f16_bits.hpp" 4 5#include <cuda_runtime.h> 6 7#include <cmath> 8#include <cstdio> 9#include <cstring> 10#include <random> 11#include <vector> 12 13using namespace strata::kernels; 14 15namespace { 16int checks = 0, failures = 0; 17void check(bool ok, const char* what) { 18 ++checks; 19 if (!ok) { ++failures; std::fprintf(stderr, "FAIL: %s\n", what); } 20} 21 22constexpr int H = 2, D = 256, PS = 64, R = 64, G = 64; 23constexpr int64_t CELLS = 4096; 24const RopeScaling rope{}; 25 26void cpu_angle(int64_t pos, int pair, float& c, float& s) { 27 const float ts = powf((float) rope.freq_base, -2.0f / R); 28 const RopeKernelArgs k = rope.kernel_args(R); 29 rope_scaled_angle((float) pos * powf(ts, float(pair)), k.freq_scale, k.corr_low, k.corr_high, k.ext_factor, 30 k.attn_factor, pair, c, s); 31} 32int64_t row_of(int64_t pos, int h) { return ((pos / PS) * H + h) * PS + pos % PS; } 33 34struct Host { 35 std::vector<int8_t> k, v; 36 std::vector<uint16_t> ks, vs; 37 std::vector<float> pre; // the unrotated K group the "fresh write" started from, per cell and head 38}; 39 40// quantize 64 values as kv_append_q8 does 41void quant(const float* x, int8_t* q, uint16_t* sc) { 42 float amax = 0; 43 for (int i = 0; i < G; ++i) amax = std::max(amax, std::fabs(x[i])); 44 *sc = f16_from_f32(amax / 127.0f); 45 const float sf = f32_from_f16(*sc); 46 for (int i = 0; i < G; ++i) { 47 int c = sf > 0 ? (int) std::nearbyint(x[i] / sf) : 0; 48 q[i] = (int8_t) std::max(-127, std::min(127, c)); 49 } 50} 51 52Host fresh(std::mt19937& rng) { 53 Host h; 54 h.k.resize(CELLS * H * D); h.v.resize(CELLS * H * D); 55 h.ks.resize(CELLS * H * (D / G)); h.vs.resize(CELLS * H * (D / G)); 56 h.pre.resize(CELLS * H * R); 57 std::normal_distribution<float> n(0.0f, 1.0f); 58 std::vector<float> x(D); 59 for (int64_t p = 0; p < CELLS; ++p) 60 for (int hh = 0; hh < H; ++hh) { 61 const int64_t row = row_of(p, hh); 62 float* pre = &h.pre[(p * H + hh) * R]; 63 for (int i = 0; i < R; ++i) pre[i] = n(rng); 64 for (int i = 0; i < R / 2; ++i) { 65 float c, s; 66 cpu_angle(p, i, c, s); 67 x[i] = pre[i] * c - pre[i + R / 2] * s; 68 x[i + R / 2] = pre[i] * s + pre[i + R / 2] * c; 69 } 70 for (int i = R; i < D; ++i) x[i] = n(rng); 71 for (int g = 0; g < D / G; ++g) quant(&x[g * G], &h.k[row * D + g * G], &h.ks[row * (D / G) + g]); 72 for (int i = 0; i < D; ++i) x[i] = n(rng); 73 for (int g = 0; g < D / G; ++g) quant(&x[g * G], &h.v[row * D + g * G], &h.vs[row * (D / G) + g]); 74 } 75 return h; 76} 77 78struct Dev { 79 ScrKvLayer l; 80 void* scratch = nullptr; 81 int64_t chunk = 300; 82 Dev(const Host& h, bool master) { 83 l.kv_heads = H; l.head_dim = D; l.page_size = PS; l.n_rot = R; l.max_cells = CELLS; 84 cudaMalloc(&l.k_q, h.k.size()); cudaMalloc(&l.v_q, h.v.size()); 85 cudaMalloc(&l.k_scale, h.ks.size() * 2); cudaMalloc(&l.v_scale, h.vs.size() * 2); 86 cudaMemcpy(l.k_q, h.k.data(), h.k.size(), cudaMemcpyHostToDevice); 87 cudaMemcpy(l.v_q, h.v.data(), h.v.size(), cudaMemcpyHostToDevice); 88 cudaMemcpy(l.k_scale, h.ks.data(), h.ks.size() * 2, cudaMemcpyHostToDevice); 89 cudaMemcpy(l.v_scale, h.vs.data(), h.vs.size() * 2, cudaMemcpyHostToDevice); 90 if (master) { 91 cudaMalloc(&l.master, CELLS * H * R * 2); 92 cudaMalloc(&l.master_valid, CELLS); 93 cudaMemset(l.master_valid, 0, CELLS); 94 } 95 cudaMalloc(&scratch, scr_move_kv_scratch_bytes(l, chunk)); 96 } 97 ~Dev() { 98 cudaFree(l.k_q); cudaFree(l.v_q); cudaFree(l.k_scale); cudaFree(l.v_scale); 99 if (l.master) { cudaFree(l.master); cudaFree(l.master_valid); } 100 cudaFree(scratch); 101 } 102 bool move(int64_t src, int64_t n, int64_t shift) { 103 const bool ok = scr_move_kv(l, src, n, shift, rope, scratch, chunk, nullptr); 104 return cudaDeviceSynchronize() == cudaSuccess && ok; 105 } 106 Host get() const { 107 Host h; 108 h.k.resize(CELLS * H * D); h.v.resize(CELLS * H * D); 109 h.ks.resize(CELLS * H * (D / G)); h.vs.resize(CELLS * H * (D / G)); 110 cudaMemcpy(h.k.data(), l.k_q, h.k.size(), cudaMemcpyDeviceToHost); 111 cudaMemcpy(h.v.data(), l.v_q, h.v.size(), cudaMemcpyDeviceToHost); 112 cudaMemcpy(h.ks.data(), l.k_scale, h.ks.size() * 2, cudaMemcpyDeviceToHost); 113 cudaMemcpy(h.vs.data(), l.v_scale, h.vs.size() * 2, cudaMemcpyDeviceToHost); 114 return h; 115 } 116}; 117 118// after moving [src, src+n) by `shift` from `before`: the moved cells' bytes and their rotated group 119void check_move(const Host& before, const Host& after, int64_t src, int64_t n, int64_t shift, const char* label) { 120 bool bytes = true, angle_ok = true; 121 double worst = 0; 122 for (int64_t i = 0; i < n; ++i) 123 for (int hh = 0; hh < H; ++hh) { 124 const int64_t a = row_of(src + i, hh), b = row_of(src + i + shift, hh); 125 if (std::memcmp(&before.v[a * D], &after.v[b * D], D) != 0 || 126 std::memcmp(&before.k[a * D + G], &after.k[b * D + G], D - G) != 0 || 127 std::memcmp(&before.vs[a * (D / G)], &after.vs[b * (D / G)], (D / G) * 2) != 0 || 128 std::memcmp(&before.ks[a * (D / G) + 1], &after.ks[b * (D / G) + 1], (D / G - 1) * 2) != 0) 129 bytes = false; 130 // the moved group against the exact rotation of the original values at the target 131 const float* pre = &before.pre[((src + i) * H + hh) * R]; 132 const float sf = f32_from_f16(after.ks[b * (D / G)]); 133 for (int j = 0; j < R / 2; ++j) { 134 float c, s; 135 cpu_angle(src + i + shift, j, c, s); 136 const float want0 = pre[j] * c - pre[j + R / 2] * s, want1 = pre[j] * s + pre[j + R / 2] * c; 137 const double e0 = std::fabs(after.k[b * D + j] * sf - want0), e1 = std::fabs(after.k[b * D + j + R / 2] * sf - want1); 138 worst = std::max(worst, std::max(e0, e1) / (sf > 0 ? sf : 1)); 139 } 140 } 141 // two quantizations (the stored codes, then the move): at most about 1.5 steps from the exact value 142 if (worst > 1.6) angle_ok = false; 143 char buf[256]; 144 std::snprintf(buf, sizeof buf, "%s: unrotated bytes moved exactly", label); check(bytes, buf); 145 std::snprintf(buf, sizeof buf, "%s: rotated group within 1.6 steps (worst %.3f)", label, worst); check(angle_ok, buf); 146} 147 148void pooled_test(std::mt19937& rng) { 149 constexpr int DIM = 128, BR = 64, BS = 4; 150 constexpr int64_t ROWS = 1024; 151 std::normal_distribution<float> n(0.0f, 1.0f); 152 std::vector<float> pre(ROWS * DIM), rows(ROWS * DIM); 153 for (int64_t b = 0; b < ROWS; ++b) 154 for (int d = 0; d < DIM; ++d) pre[b * DIM + d] = n(rng); 155 auto rot = [&](const float* p, int64_t pos, float* out) { 156 for (int d = 0; d < DIM; ++d) out[d] = p[d]; 157 for (int j = 0; j < BR / 2; ++j) { 158 float c, s; 159 cpu_angle(pos, j, c, s); 160 out[j] = p[j] * c - p[j + BR / 2] * s; 161 out[j + BR / 2] = p[j] * s + p[j + BR / 2] * c; 162 } 163 }; 164 for (int64_t b = 0; b < ROWS; ++b) rot(&pre[b * DIM], BS * b, &rows[b * DIM]); 165 float *d_rows = nullptr, *d_scratch = nullptr; 166 cudaMalloc(&d_rows, rows.size() * 4); 167 cudaMalloc(&d_scratch, 100 * DIM * 4); 168 for (int64_t shift : {37, -29, 3, -500}) { 169 cudaMemcpy(d_rows, rows.data(), rows.size() * 4, cudaMemcpyHostToDevice); 170 const int64_t b0 = shift > 0 ? 100 : 600, cnt = 300; 171 const bool ok = scr_move_pooled(d_rows, ROWS, DIM, BR, BS, b0, cnt, shift, rope, d_scratch, 100, nullptr); 172 cudaDeviceSynchronize(); 173 std::vector<float> got(rows.size()); 174 cudaMemcpy(got.data(), d_rows, got.size() * 4, cudaMemcpyDeviceToHost); 175 double worst = 0; 176 std::vector<float> want(DIM); 177 for (int64_t i = 0; i < cnt; ++i) { 178 rot(&pre[(b0 + i) * DIM], BS * (b0 + i + shift), want.data()); 179 for (int d = 0; d < DIM; ++d) worst = std::max(worst, (double) std::fabs(got[(b0 + i + shift) * DIM + d] - want[d])); 180 } 181 char buf[128]; 182 std::snprintf(buf, sizeof buf, "pooled shift %lld: rows at their new angle (worst %.2e)", (long long) shift, worst); 183 check(ok && worst < 2e-4, buf); 184 } 185 cudaFree(d_rows); 186 cudaFree(d_scratch); 187} 188} // namespace 189 190int main() { 191 std::mt19937 rng(7); 192 const Host h = fresh(rng); 193 // overlapping moves, both directions, several chunks 194 for (int64_t shift : {36, -40, 4, -1000, 2000}) { 195 Dev d(h, false); 196 const int64_t src = shift > 0 ? 100 : 1500, n = 1000; 197 char label[64]; 198 std::snprintf(label, sizeof label, "move %lld", (long long) shift); 199 check(d.move(src, n, shift), label); 200 check_move(h, d.get(), src, n, shift, label); 201 } 202 // the master: two moves equal one move by their sum, byte for byte 203 { 204 Dev once(h, true), twice(h, true); 205 check(once.move(200, 1000, 36), "master: one move"); 206 check(twice.move(200, 1000, 8) && twice.move(208, 1000, 28), "master: two moves"); 207 const Host a = once.get(), b = twice.get(); 208 bool same = true; 209 for (int64_t i = 0; i < 1000; ++i) 210 for (int hh = 0; hh < H; ++hh) { 211 const int64_t r = row_of(236 + i, hh); 212 if (std::memcmp(&a.k[r * D], &b.k[r * D], D) || a.ks[r * (D / G)] != b.ks[r * (D / G)]) same = false; 213 } 214 check(same, "master: two moves are byte-identical to one move by the sum"); 215 // without the master the second move starts from the first one's codes, so they differ 216 Dev plain(h, false); 217 plain.move(200, 1000, 8); 218 plain.move(208, 1000, 28); 219 const Host c = plain.get(); 220 int64_t differ = 0; 221 for (int64_t i = 0; i < 1000; ++i) { 222 const int64_t r = row_of(236 + i, 0); 223 differ += std::memcmp(&a.k[r * D], &c.k[r * D], G) != 0; 224 } 225 std::printf(" without the master %lld of 1000 cells differ after two moves\n", (long long) differ); 226 } 227 // invalid arguments move nothing 228 { 229 Dev d(h, false); 230 check(!scr_move_kv(d.l, 4000, 200, 0, rope, d.scratch, d.chunk, nullptr), "past the end refused"); 231 check(!scr_move_kv(d.l, 10, 100, -20, rope, d.scratch, d.chunk, nullptr), "before zero refused"); 232 } 233 pooled_test(rng); 234 std::printf("scr_move_parity: %d checks, %d failed\n", checks, failures); 235 return failures == 0 ? 0 : 1; 236}