Files
leleka/crates/cuda-driver/emulation/main.cpp
T
Pavlo RatushnyiandClaude Sonnet 5.5 88401e1663 test(kar): run the real kar_decode_levels.cu on the CPU against the kernel model
nvcc and a GPU are unavailable, and the kernel's only executable evidence was a
Rust transcription of it. This harness compiles the actual .cu with clang
through a small shim (threads for CUDA threads, a barrier for __syncthreads,
atomics for atomicOr) and replays model-generated cases: random valid windows
at levels 2..5, non-finite scales/books, ~1k corrupted launch-parameter sets,
multiply and SwiGLU. 17k cases (ASan build included) agree bit for bit; this
does not cover nvcc codegen or device behaviour.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 16:56:27 +02:00

67 lines
4.3 KiB
C++

#include "../src/kar_decode_levels.cu"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
static std::vector<uint32_t> W;
static size_t pos = 0;
static uint32_t rd() { if (pos >= W.size()) { fprintf(stderr, "truncated case file\n"); exit(2); } return W[pos++]; }
int main(int argc, char** argv) {
FILE* f = fopen(argv[1], "rb"); if (!f) return 2;
fseek(f, 0, SEEK_END); long n = ftell(f); fseek(f, 0, SEEK_SET);
W.resize(n / 4); if (fread(W.data(), 4, W.size(), f) != W.size()) return 2; fclose(f);
uint32_t ncases = rd(); unsigned bad = 0, skipped = 0;
for (uint32_t c = 0; c < ncases; ++c) {
uint32_t kind = rd();
if (kind == 0) {
uint32_t s[12]; for (auto& v : s) v = rd();
uint32_t grid = rd(), block = rd(), packet_len = rd();
uint8_t* packet = (uint8_t*)malloc(packet_len ? packet_len : 1);
for (uint32_t i = 0; i < packet_len; ++i) packet[i] = (W[pos + i / 4] >> (8 * (i % 4))) & 255;
pos += (packet_len + 3) / 4;
uint32_t out_words = rd(), exp_status = rd();
float* out = (float*)malloc(out_words * 4 + 4);
uint32_t* ob = (uint32_t*)out; for (uint32_t i = 0; i < out_words; ++i) ob[i] = 0xdeadbeef;
uint32_t* status = (uint32_t*)malloc(4); *status = 0;
std::vector<uint32_t> expect(out_words); for (auto& v : expect) v = rd();
if (grid == 0) grid = (s[2] + 255) / 256;
if (block == 0) block = 256;
launch(dim3(grid), dim3(block), [&] { leleka_kar_decode_levels_f32(packet, out, status, s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[8], s[9], s[10], s[11]); });
bool ok = *status == exp_status || (exp_status == 1 && packet_len < s[11]);
if (exp_status == 1 && packet_len < s[11]) { ++skipped; free(packet); free(out); free(status); continue; }
for (uint32_t i = 0; i < out_words && ok; ++i) if (ob[i] != expect[i]) { ok = false; fprintf(stderr, "case %u decode element %u: got %08x want %08x\n", c, i, ob[i], expect[i]); }
if (!ok) { ++bad; fprintf(stderr, "case %u decode FAIL status got %u want %u params %u %u %u %u %u %u %u %u %u %u %u %u packet_len %u\n", c, *status, exp_status, s[0],s[1],s[2],s[3],s[4],s[5],s[6],s[7],s[8],s[9],s[10],s[11], packet_len); }
free(packet); free(out); free(status);
} else if (kind == 1) {
uint32_t cols = rd(), rows = rd(), tokens = rd(), gx = rd(), gy = rd();
std::vector<uint32_t> wts(cols * (size_t)rows), inp(cols * (size_t)tokens);
for (auto& v : wts) v = rd(); for (auto& v : inp) v = rd();
uint32_t exp_status = rd(), out_words = rd();
std::vector<uint32_t> expect(out_words); for (auto& v : expect) v = rd();
std::vector<uint32_t> out(out_words, 0xdeadbeef); uint32_t* status = (uint32_t*)malloc(4); *status = 0;
if (gx == 0) gx = rows; if (gy == 0) gy = tokens;
launch(dim3(gx, gy), dim3(256), [&] { leleka_kar_multiply_f32((const float*)wts.data(), (const float*)inp.data(), (float*)out.data(), status, cols, rows); });
bool ok = *status == exp_status;
for (uint32_t i = 0; i < out_words && ok; ++i) if (out[i] != expect[i]) { ok = false; fprintf(stderr, "case %u mul element %u: got %08x want %08x\n", c, i, out[i], expect[i]); }
if (!ok) { ++bad; fprintf(stderr, "case %u multiply FAIL status got %u want %u\n", c, *status, exp_status); }
free(status);
} else {
uint32_t values = rd();
std::vector<uint32_t> g(values), u(values), e(values);
for (auto& v : g) v = rd(); for (auto& v : u) v = rd();
uint32_t exp_status = rd(); for (auto& v : e) v = rd();
std::vector<uint32_t> out(values, 0xdeadbeef); uint32_t* status = (uint32_t*)malloc(4); *status = 0;
launch(dim3((values + 255) / 256), dim3(256), [&] { leleka_kar_swiglu_f32((const float*)g.data(), (const float*)u.data(), (float*)out.data(), status, values); });
bool ok = *status == exp_status;
for (uint32_t i = 0; i < values && ok; ++i) {
float a, b; memcpy(&a, &out[i], 4); memcpy(&b, &e[i], 4);
if (!(std::fabs(a - b) <= 2e-6f * std::fmax(1.0f, std::fabs(b)))) { ok = false; fprintf(stderr, "case %u swiglu element %u: got %g want %g\n", c, i, a, b); }
}
if (!ok) { ++bad; fprintf(stderr, "case %u swiglu FAIL status got %u want %u\n", c, *status, exp_status); }
free(status);
}
}
printf("%u cases, %u failures, %u skipped (packet shorter than claimed)\n", ncases, bad, skipped);
return bad ? 1 : 0;
}