// src/kernels/scr_move_parity.cu - scr_move.cu against a CPU reference. Needs a CUDA device (a few MB). #include "strata/kernels/scr_move.hpp" #include "strata/kernels/f16_bits.hpp" #include #include #include #include #include #include using namespace strata::kernels; namespace { int checks = 0, failures = 0; void check(bool ok, const char* what) { ++checks; if (!ok) { ++failures; std::fprintf(stderr, "FAIL: %s\n", what); } } constexpr int H = 2, D = 256, PS = 64, R = 64, G = 64; constexpr int64_t CELLS = 4096; const RopeScaling rope{}; void cpu_angle(int64_t pos, int pair, float& c, float& s) { const float ts = powf((float) rope.freq_base, -2.0f / R); const RopeKernelArgs k = rope.kernel_args(R); rope_scaled_angle((float) pos * powf(ts, float(pair)), k.freq_scale, k.corr_low, k.corr_high, k.ext_factor, k.attn_factor, pair, c, s); } int64_t row_of(int64_t pos, int h) { return ((pos / PS) * H + h) * PS + pos % PS; } struct Host { std::vector k, v; std::vector ks, vs; std::vector pre; // the unrotated K group the "fresh write" started from, per cell and head }; // quantize 64 values as kv_append_q8 does void quant(const float* x, int8_t* q, uint16_t* sc) { float amax = 0; for (int i = 0; i < G; ++i) amax = std::max(amax, std::fabs(x[i])); *sc = f16_from_f32(amax / 127.0f); const float sf = f32_from_f16(*sc); for (int i = 0; i < G; ++i) { int c = sf > 0 ? (int) std::nearbyint(x[i] / sf) : 0; q[i] = (int8_t) std::max(-127, std::min(127, c)); } } Host fresh(std::mt19937& rng) { Host h; h.k.resize(CELLS * H * D); h.v.resize(CELLS * H * D); h.ks.resize(CELLS * H * (D / G)); h.vs.resize(CELLS * H * (D / G)); h.pre.resize(CELLS * H * R); std::normal_distribution n(0.0f, 1.0f); std::vector x(D); for (int64_t p = 0; p < CELLS; ++p) for (int hh = 0; hh < H; ++hh) { const int64_t row = row_of(p, hh); float* pre = &h.pre[(p * H + hh) * R]; for (int i = 0; i < R; ++i) pre[i] = n(rng); for (int i = 0; i < R / 2; ++i) { float c, s; cpu_angle(p, i, c, s); x[i] = pre[i] * c - pre[i + R / 2] * s; x[i + R / 2] = pre[i] * s + pre[i + R / 2] * c; } for (int i = R; i < D; ++i) x[i] = n(rng); for (int g = 0; g < D / G; ++g) quant(&x[g * G], &h.k[row * D + g * G], &h.ks[row * (D / G) + g]); for (int i = 0; i < D; ++i) x[i] = n(rng); for (int g = 0; g < D / G; ++g) quant(&x[g * G], &h.v[row * D + g * G], &h.vs[row * (D / G) + g]); } return h; } struct Dev { ScrKvLayer l; void* scratch = nullptr; int64_t chunk = 300; Dev(const Host& h, bool master) { l.kv_heads = H; l.head_dim = D; l.page_size = PS; l.n_rot = R; l.max_cells = CELLS; cudaMalloc(&l.k_q, h.k.size()); cudaMalloc(&l.v_q, h.v.size()); cudaMalloc(&l.k_scale, h.ks.size() * 2); cudaMalloc(&l.v_scale, h.vs.size() * 2); cudaMemcpy(l.k_q, h.k.data(), h.k.size(), cudaMemcpyHostToDevice); cudaMemcpy(l.v_q, h.v.data(), h.v.size(), cudaMemcpyHostToDevice); cudaMemcpy(l.k_scale, h.ks.data(), h.ks.size() * 2, cudaMemcpyHostToDevice); cudaMemcpy(l.v_scale, h.vs.data(), h.vs.size() * 2, cudaMemcpyHostToDevice); if (master) { cudaMalloc(&l.master, CELLS * H * R * 2); cudaMalloc(&l.master_valid, CELLS); cudaMemset(l.master_valid, 0, CELLS); } cudaMalloc(&scratch, scr_move_kv_scratch_bytes(l, chunk)); } ~Dev() { cudaFree(l.k_q); cudaFree(l.v_q); cudaFree(l.k_scale); cudaFree(l.v_scale); if (l.master) { cudaFree(l.master); cudaFree(l.master_valid); } cudaFree(scratch); } bool move(int64_t src, int64_t n, int64_t shift) { const bool ok = scr_move_kv(l, src, n, shift, rope, scratch, chunk, nullptr); return cudaDeviceSynchronize() == cudaSuccess && ok; } Host get() const { Host h; h.k.resize(CELLS * H * D); h.v.resize(CELLS * H * D); h.ks.resize(CELLS * H * (D / G)); h.vs.resize(CELLS * H * (D / G)); cudaMemcpy(h.k.data(), l.k_q, h.k.size(), cudaMemcpyDeviceToHost); cudaMemcpy(h.v.data(), l.v_q, h.v.size(), cudaMemcpyDeviceToHost); cudaMemcpy(h.ks.data(), l.k_scale, h.ks.size() * 2, cudaMemcpyDeviceToHost); cudaMemcpy(h.vs.data(), l.v_scale, h.vs.size() * 2, cudaMemcpyDeviceToHost); return h; } }; // after moving [src, src+n) by `shift` from `before`: the moved cells' bytes and their rotated group void check_move(const Host& before, const Host& after, int64_t src, int64_t n, int64_t shift, const char* label) { bool bytes = true, angle_ok = true; double worst = 0; for (int64_t i = 0; i < n; ++i) for (int hh = 0; hh < H; ++hh) { const int64_t a = row_of(src + i, hh), b = row_of(src + i + shift, hh); if (std::memcmp(&before.v[a * D], &after.v[b * D], D) != 0 || std::memcmp(&before.k[a * D + G], &after.k[b * D + G], D - G) != 0 || std::memcmp(&before.vs[a * (D / G)], &after.vs[b * (D / G)], (D / G) * 2) != 0 || std::memcmp(&before.ks[a * (D / G) + 1], &after.ks[b * (D / G) + 1], (D / G - 1) * 2) != 0) bytes = false; // the moved group against the exact rotation of the original values at the target const float* pre = &before.pre[((src + i) * H + hh) * R]; const float sf = f32_from_f16(after.ks[b * (D / G)]); for (int j = 0; j < R / 2; ++j) { float c, s; cpu_angle(src + i + shift, j, c, s); const float want0 = pre[j] * c - pre[j + R / 2] * s, want1 = pre[j] * s + pre[j + R / 2] * c; const double e0 = std::fabs(after.k[b * D + j] * sf - want0), e1 = std::fabs(after.k[b * D + j + R / 2] * sf - want1); worst = std::max(worst, std::max(e0, e1) / (sf > 0 ? sf : 1)); } } // two quantizations (the stored codes, then the move): at most about 1.5 steps from the exact value if (worst > 1.6) angle_ok = false; char buf[256]; std::snprintf(buf, sizeof buf, "%s: unrotated bytes moved exactly", label); check(bytes, buf); std::snprintf(buf, sizeof buf, "%s: rotated group within 1.6 steps (worst %.3f)", label, worst); check(angle_ok, buf); } void pooled_test(std::mt19937& rng) { constexpr int DIM = 128, BR = 64, BS = 4; constexpr int64_t ROWS = 1024; std::normal_distribution n(0.0f, 1.0f); std::vector pre(ROWS * DIM), rows(ROWS * DIM); for (int64_t b = 0; b < ROWS; ++b) for (int d = 0; d < DIM; ++d) pre[b * DIM + d] = n(rng); auto rot = [&](const float* p, int64_t pos, float* out) { for (int d = 0; d < DIM; ++d) out[d] = p[d]; for (int j = 0; j < BR / 2; ++j) { float c, s; cpu_angle(pos, j, c, s); out[j] = p[j] * c - p[j + BR / 2] * s; out[j + BR / 2] = p[j] * s + p[j + BR / 2] * c; } }; for (int64_t b = 0; b < ROWS; ++b) rot(&pre[b * DIM], BS * b, &rows[b * DIM]); float *d_rows = nullptr, *d_scratch = nullptr; cudaMalloc(&d_rows, rows.size() * 4); cudaMalloc(&d_scratch, 100 * DIM * 4); for (int64_t shift : {37, -29, 3, -500}) { cudaMemcpy(d_rows, rows.data(), rows.size() * 4, cudaMemcpyHostToDevice); const int64_t b0 = shift > 0 ? 100 : 600, cnt = 300; const bool ok = scr_move_pooled(d_rows, ROWS, DIM, BR, BS, b0, cnt, shift, rope, d_scratch, 100, nullptr); cudaDeviceSynchronize(); std::vector got(rows.size()); cudaMemcpy(got.data(), d_rows, got.size() * 4, cudaMemcpyDeviceToHost); double worst = 0; std::vector want(DIM); for (int64_t i = 0; i < cnt; ++i) { rot(&pre[(b0 + i) * DIM], BS * (b0 + i + shift), want.data()); for (int d = 0; d < DIM; ++d) worst = std::max(worst, (double) std::fabs(got[(b0 + i + shift) * DIM + d] - want[d])); } char buf[128]; std::snprintf(buf, sizeof buf, "pooled shift %lld: rows at their new angle (worst %.2e)", (long long) shift, worst); check(ok && worst < 2e-4, buf); } cudaFree(d_rows); cudaFree(d_scratch); } } // namespace int main() { std::mt19937 rng(7); const Host h = fresh(rng); // overlapping moves, both directions, several chunks for (int64_t shift : {36, -40, 4, -1000, 2000}) { Dev d(h, false); const int64_t src = shift > 0 ? 100 : 1500, n = 1000; char label[64]; std::snprintf(label, sizeof label, "move %lld", (long long) shift); check(d.move(src, n, shift), label); check_move(h, d.get(), src, n, shift, label); } // the master: two moves equal one move by their sum, byte for byte { Dev once(h, true), twice(h, true); check(once.move(200, 1000, 36), "master: one move"); check(twice.move(200, 1000, 8) && twice.move(208, 1000, 28), "master: two moves"); const Host a = once.get(), b = twice.get(); bool same = true; for (int64_t i = 0; i < 1000; ++i) for (int hh = 0; hh < H; ++hh) { const int64_t r = row_of(236 + i, hh); if (std::memcmp(&a.k[r * D], &b.k[r * D], D) || a.ks[r * (D / G)] != b.ks[r * (D / G)]) same = false; } check(same, "master: two moves are byte-identical to one move by the sum"); // without the master the second move starts from the first one's codes, so they differ Dev plain(h, false); plain.move(200, 1000, 8); plain.move(208, 1000, 28); const Host c = plain.get(); int64_t differ = 0; for (int64_t i = 0; i < 1000; ++i) { const int64_t r = row_of(236 + i, 0); differ += std::memcmp(&a.k[r * D], &c.k[r * D], G) != 0; } std::printf(" without the master %lld of 1000 cells differ after two moves\n", (long long) differ); } // invalid arguments move nothing { Dev d(h, false); check(!scr_move_kv(d.l, 4000, 200, 0, rope, d.scratch, d.chunk, nullptr), "past the end refused"); check(!scr_move_kv(d.l, 10, 100, -20, rope, d.scratch, d.chunk, nullptr), "before zero refused"); } pooled_test(rng); std::printf("scr_move_parity: %d checks, %d failed\n", checks, failures); return failures == 0 ? 0 : 1; }