Files
leleka/crates/experimental-ref/oracle/gen_cvec_golden.cpp
T
Pavlo RatushnyiandClaude Sonnet 5.5 7bd31abdb2 feat(experimental-ref): keyless routing, control vectors and attention references; emulated kernel scenarios
keyless.rs: SKLROUT1 artifact admission (layout cross-read against prepare-keyless-routing.py),
fmaf-order query routing, positional key reconstruction. cvec.rs: GGUF v3 parsing and the
load_control_vectors table builder (19 golden cases incl. the Original's error texts,
bit-exact, from the Original function extracted verbatim) and a reduction-tree twin of
cvec_kernel. attn.rs: scratch arithmetic and an f64 GQA reference. The emulated kernel
text reproduces routing and cvec bit for bit and attention modes 2/3/4 (incl. M-RoPE) to
1.6e-7 relative L2.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 15:31:44 +02:00

162 lines
8.6 KiB
C++

// Golden fixtures for control-vector loading: `load_control_vectors` of engine/strata/src/program/generate.cpp,
// included VERBATIM (extracted by gen_cvec_golden.sh with awk) and run on synthetic control-vector GGUF files
// written by this harness. `cvec_upload` (CUDA) is replaced by a recorder; the function is otherwise unmodified.
#include "strata/artifact/gguf_reader.hpp"
#include "strata/core/layout.hpp"
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <string>
#include <utility>
#include <vector>
// ---- stand-ins for what the extracted function touches
struct Options {
std::vector<std::pair<std::string, float>> cvec_files;
int cvec_first = -1, cvec_last = -1;
int cvec_mode = 1;
int cvec_single = -1;
};
namespace strata::kernels {
struct Recorded { std::vector<float> dir, s; int mode = -1, first = 0, last = 0; int64_t n_embd = 0, hc = 0; bool called = false; };
inline Recorded g_recorded;
inline bool cvec_upload(const std::vector<float>& dir, const std::vector<float>& s, int mode, int first, int last,
int64_t n_embd, int64_t hc, std::string&) {
g_recorded = {dir, s, mode, first, last, n_embd, hc, true};
return true;
}
} // namespace strata::kernels
#include "fragment_load_control_vectors.inc"
namespace {
uint64_t lcg_state = 77;
uint32_t next32() { lcg_state = lcg_state * 6364136223846793005ull + 1442695040888963407ull; return (uint32_t) (lcg_state >> 32); }
float value() { return (float) ((int32_t) (next32() >> 8) - (1 << 23)) / 8388608.0f * 2.0f; }
uint32_t bits(float f) { uint32_t u; std::memcpy(&u, &f, 4); return u; }
void put_u32(std::string& b, uint32_t v) { b.append((const char*) &v, 4); }
void put_u64(std::string& b, uint64_t v) { b.append((const char*) &v, 8); }
void put_str(std::string& b, const std::string& s) { put_u64(b, s.size()); b += s; }
struct T { std::string name; std::vector<float> data; uint32_t type = 0; };
std::string gguf(const std::string& arch, const std::vector<T>& ts, bool hint = false) {
std::string b;
put_u32(b, 0x46554747u); put_u32(b, 3); put_u64(b, ts.size()); put_u64(b, 1 + (hint ? 1 : 0));
put_str(b, "general.architecture"); put_u32(b, 8); put_str(b, arch);
if (hint) { put_str(b, "controlvector.model_hint"); put_u32(b, 8); put_str(b, "qwen4exp"); }
uint64_t off = 0;
for (const T& t : ts) {
put_str(b, t.name); put_u32(b, 1); put_u64(b, t.data.size()); put_u32(b, t.type); put_u64(b, off);
off += (t.data.size() * 4 + 31) / 32 * 32;
}
while (b.size() % 32) b.push_back(0);
for (const T& t : ts) {
b.append((const char*) t.data.data(), t.data.size() * 4);
while (b.size() % 32) b.push_back(0);
}
return b;
}
std::string hex(const std::string& s) {
static const char* d = "0123456789abcdef";
std::string o;
for (unsigned char c : s) { o += d[c >> 4]; o += d[c & 15]; }
return o;
}
std::vector<float> vec(int n) { std::vector<float> v(n); for (auto& x : v) x = value(); return v; }
struct Case { std::string name; std::vector<std::pair<std::string, float>> files; std::vector<std::string> contents; Options o; };
} // namespace
int main() {
const int L = 6, N = 40, HC = 4;
strata::core::ModelGeometry g;
g.n_layers = L; g.n_embd = N; g.hc = HC;
const std::string dir = std::string(getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp") + "/leleka_cvec_golden";
std::system(("mkdir -p " + dir).c_str());
std::vector<std::string> A = {gguf("controlvector", {{"direction.0", vec(N)}, {"direction.2", vec(N)}, {"direction.3", vec(N)}, {"direction.9", vec(N)}}, true),
gguf("controlvector", {{"direction.2", vec(N)}, {"direction.4", vec(N)}})};
const std::string wrong_arch = gguf("llama", {{"direction.2", vec(N)}});
const std::string wrong_shape = gguf("controlvector", {{"direction.2", vec(N + 1)}});
const std::string wrong_type = gguf("controlvector", {{"direction.2", vec(N), 1}});
const std::string no_dirs = gguf("controlvector", {{"other.2", vec(N)}});
const std::string only_layer0 = gguf("controlvector", {{"direction.0", vec(N)}});
const std::string zero_dir = gguf("controlvector", {{"direction.2", std::vector<float>(N, 0.0f)}});
const std::string truncated = A[0].substr(0, 60);
std::string bad_magic = A[0]; bad_magic[0] = 'X';
std::string v2 = A[0]; v2[4] = 2;
std::vector<Case> cases;
auto mk = [&](const std::string& name, std::vector<std::pair<std::string, float>> files, std::vector<std::string> contents, Options o) {
cases.push_back({name, std::move(files), std::move(contents), o});
};
Options add; add.cvec_mode = 1;
Options proj; proj.cvec_mode = 0;
Options range = add; range.cvec_first = 2; range.cvec_last = 4;
Options proj_range = proj; proj_range.cvec_first = 2; proj_range.cvec_last = 5;
Options single = proj; single.cvec_single = 3;
Options single_missing = proj; single_missing.cvec_single = 5;
Options single_ignored_in_add = add; single_ignored_in_add.cvec_single = 5;
Options out_of_range = add; out_of_range.cvec_first = 5; out_of_range.cvec_last = 5;
Options wide = add; wide.cvec_last = 100;
mk("add_two_files", {{"a.gguf", 1.5f}, {"b.gguf", -0.5f}}, {A[0], A[1]}, add);
mk("add_range", {{"a.gguf", 1.0f}, {"b.gguf", 2.0f}}, {A[0], A[1]}, range);
mk("add_last_beyond_model", {{"a.gguf", 0.25f}}, {A[0]}, wide);
mk("project_two_files", {{"a.gguf", 1.0f}, {"b.gguf", 1.0f}}, {A[0], A[1]}, proj);
mk("project_range", {{"a.gguf", 0.75f}, {"b.gguf", 0.5f}}, {A[0], A[1]}, proj_range);
mk("project_single_layer", {{"a.gguf", 1.0f}}, {A[0]}, single);
mk("project_single_missing", {{"a.gguf", 1.0f}}, {A[0]}, single_missing);
mk("add_ignores_single", {{"a.gguf", 1.0f}}, {A[0]}, single_ignored_in_add);
mk("nothing_in_range", {{"a.gguf", 1.0f}}, {A[0]}, out_of_range);
mk("zero_scale_project", {{"a.gguf", 0.0f}}, {A[0]}, proj);
mk("zero_direction_project", {{"z.gguf", 1.0f}}, {zero_dir}, proj);
mk("wrong_architecture", {{"w.gguf", 1.0f}}, {wrong_arch}, add);
mk("wrong_shape", {{"s.gguf", 1.0f}}, {wrong_shape}, add);
mk("wrong_type", {{"t.gguf", 1.0f}}, {wrong_type}, add);
mk("no_direction_tensors", {{"n.gguf", 1.0f}}, {no_dirs}, add);
mk("only_layer_zero", {{"o.gguf", 1.0f}}, {only_layer0}, add);
mk("truncated", {{"x.gguf", 1.0f}}, {truncated}, add);
mk("bad_magic", {{"m.gguf", 1.0f}}, {bad_magic}, add);
mk("version_2", {{"v.gguf", 1.0f}}, {v2}, add);
std::printf("{\"n_layers\":%d,\"n_embd\":%d,\"hc\":%d,\"cases\":[", L, N, HC);
for (size_t ci = 0; ci < cases.size(); ++ci) {
Case& c = cases[ci];
Options o = c.o;
for (size_t i = 0; i < c.files.size(); ++i) {
const std::string path = dir + "/" + std::to_string(ci) + "_" + c.files[i].first;
std::ofstream(path, std::ios::binary).write(c.contents[i].data(), (std::streamsize) c.contents[i].size());
o.cvec_files.push_back({path, c.files[i].second});
}
strata::kernels::g_recorded = {};
std::string summary, err;
const bool ok = load_control_vectors(o, g, summary, err);
// the error text names the temp path; record it with that prefix replaced by the file label
for (size_t i = 0; i < c.files.size(); ++i) {
const std::string path = dir + "/" + std::to_string(ci) + "_" + c.files[i].first;
for (size_t at; (at = err.find(path)) != std::string::npos;) err.replace(at, path.size(), c.files[i].first);
}
std::printf("%s\n{\"name\":\"%s\",\"options\":{\"first\":%d,\"last\":%d,\"mode\":%d,\"single\":%d},\"files\":[", ci ? "," : "", c.name.c_str(),
o.cvec_first, o.cvec_last, o.cvec_mode, o.cvec_single);
for (size_t i = 0; i < c.files.size(); ++i)
std::printf("%s{\"label\":\"%s\",\"scale\":%u,\"bytes\":\"%s\"}", i ? "," : "", c.files[i].first.c_str(), bits(c.files[i].second), hex(c.contents[i]).c_str());
std::printf("],\"ok\":%d,\"error\":\"%s\"", ok ? 1 : 0, ok ? "" : err.c_str());
if (ok) {
const auto& r = strata::kernels::g_recorded;
std::printf(",\"summary\":\"%s\",\"mode\":%d,\"first\":%d,\"last\":%d,\"dir\":[", summary.c_str(), r.mode, r.first, r.last);
for (size_t i = 0; i < r.dir.size(); ++i) std::printf("%s%u", i ? "," : "", bits(r.dir[i]));
std::printf("],\"s\":[");
for (size_t i = 0; i < r.s.size(); ++i) std::printf("%s%u", i ? "," : "", bits(r.s[i]));
std::printf("]");
}
std::printf("}");
}
std::printf("]}\n");
std::system(("rm -rf " + dir).c_str());
return 0;
}