leleka-serving-policy gains the Original's host-side vision and steering code: the SVE1 record reader and the (t,h,w) position table (compiled verbatim from generate.cpp for the golden fixture), image sources, Python-compatible base64, the <|image_pad|> expansion of Service.prepare, and the control-vector GGUF loader with its add/project tables (bit-exact against the verbatim load_control_vectors). control-plane accepts engine.control_vector in its config, preflights the files and passes the engine its arguments. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
207 lines
10 KiB
C++
207 lines
10 KiB
C++
// Host oracle for control vectors: `load_control_vectors` of src/program/generate.cpp INCLUDED VERBATIM
|
|
// (extracted by build_oracle.sh) with the Options / ModelGeometry / cvec_upload it needs stubbed, driven over
|
|
// small GGUF control-vector files this harness writes. The tables it would upload (dir, s) are recorded as
|
|
// raw float bits, so the Rust loader can be compared bit for bit (-ffp-contract=off pins the host arithmetic).
|
|
//
|
|
// Usage: gen_cvec_fixtures OUT.json
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <string>
|
|
#include <unistd.h>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#include "strata/artifact/gguf_reader.hpp"
|
|
|
|
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::core { struct ModelGeometry { int64_t n_layers = 0, n_embd = 0, hc = 0; }; }
|
|
namespace strata::kernels {
|
|
struct Captured { std::vector<float> dir, s; int mode = 0, first = 0, last = 0; bool called = false; };
|
|
Captured g_up;
|
|
bool cvec_upload(const std::vector<float>& dir, const std::vector<float>& s, int mode, int first, int last, int64_t, int64_t,
|
|
std::string&) {
|
|
g_up = {dir, s, mode, first, last, true};
|
|
return true;
|
|
}
|
|
} // namespace strata::kernels
|
|
|
|
#include "load_control_vectors.inc"
|
|
|
|
// ---- a minimal GGUF v3 writer ---------------------------------------------------------------------------------
|
|
struct Tensor { std::string name; uint32_t type; std::vector<uint64_t> dims; std::vector<float> data; };
|
|
static void w32(std::string& s, uint32_t v) { s.append((const char*) &v, 4); }
|
|
static void w64(std::string& s, uint64_t v) { s.append((const char*) &v, 8); }
|
|
static void wstr(std::string& s, const std::string& v) { w64(s, v.size()); s += v; }
|
|
static std::string gguf(const std::vector<std::pair<std::string, std::string>>& kv, const std::vector<Tensor>& ts) {
|
|
std::string s = "GGUF";
|
|
w32(s, 3);
|
|
w64(s, ts.size());
|
|
w64(s, kv.size());
|
|
for (const auto& [k, v] : kv) { wstr(s, k); w32(s, 8); wstr(s, v); }
|
|
uint64_t offset = 0;
|
|
std::vector<uint64_t> offsets;
|
|
for (const Tensor& t : ts) {
|
|
wstr(s, t.name);
|
|
w32(s, (uint32_t) t.dims.size());
|
|
for (uint64_t d : t.dims) w64(s, d);
|
|
w32(s, t.type);
|
|
w64(s, offset);
|
|
offsets.push_back(offset);
|
|
offset += (t.data.size() * 4 + 31) / 32 * 32;
|
|
}
|
|
while (s.size() % 32) s += '\0';
|
|
for (const Tensor& t : ts) {
|
|
s.append((const char*) t.data.data(), t.data.size() * 4);
|
|
while (s.size() % 32) s += '\0';
|
|
}
|
|
return s;
|
|
}
|
|
static 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;
|
|
}
|
|
static std::string jstr(const std::string& s) {
|
|
std::string o = "\"";
|
|
for (char c : s) { if (c == '"' || c == '\\') o += '\\'; o += c; }
|
|
return o + "\"";
|
|
}
|
|
static Tensor dir(const std::string& name, int64_t n, float seed, float step = 0.37f) {
|
|
Tensor t{name, 0, {(uint64_t) n}, {}};
|
|
for (int64_t j = 0; j < n; ++j) t.data.push_back(seed + step * (float) j - (j % 3 == 0 ? 0.5f : 0.0f));
|
|
return t;
|
|
}
|
|
|
|
struct Case {
|
|
std::string name;
|
|
std::vector<std::pair<std::string, float>> files; // index into blobs, scale
|
|
int first, last, mode, single;
|
|
};
|
|
|
|
int main(int argc, char** argv) {
|
|
if (argc < 2) return 2;
|
|
constexpr int64_t L = 6, N = 5, HC = 2;
|
|
const std::string base = std::string(std::getenv("TMPDIR") ? std::getenv("TMPDIR") : "/tmp") + "/strata_cvec_oracle_" + std::to_string((long long) getpid());
|
|
const std::vector<std::pair<std::string, std::string>> good = {{"general.architecture", "controlvector"}, {"controlvector.model_hint", "qwen4exp"}};
|
|
std::vector<std::string> blobs = {
|
|
// 0: directions for layers 1, 2, 4 (3 and 5 have none)
|
|
gguf(good, {dir("direction.1", N, 1.0f), dir("direction.2", N, -0.5f), dir("direction.4", N, 2.0f)}),
|
|
// 1: directions for 2, 3
|
|
gguf(good, {dir("direction.2", N, 0.25f, 0.11f), dir("direction.3", N, 3.0f)}),
|
|
// 2: not a control vector
|
|
gguf({{"general.architecture", "llama"}}, {dir("direction.1", N, 1.0f)}),
|
|
// 3: no direction tensors
|
|
gguf(good, {dir("other.1", N, 1.0f)}),
|
|
// 4: wrong size
|
|
gguf(good, {dir("direction.1", N + 1, 1.0f)}),
|
|
// 5: wrong type (F16 id 1)
|
|
gguf(good, {Tensor{"direction.1", 1, {(uint64_t) N}, std::vector<float>(3, 0.0f)}}),
|
|
// 6: layer 0, past the model, a non-number, a padded number: ignored / strtol semantics
|
|
gguf(good, {dir("direction.0", N, 9.0f), dir("direction.6", N, 9.0f), dir("direction.99", N, 9.0f), dir("direction.abc", N, 9.0f),
|
|
dir("direction.03", N, 0.75f)}),
|
|
// 7: all zeros
|
|
gguf(good, {Tensor{"direction.1", 0, {(uint64_t) N}, std::vector<float>(N, 0.0f)}, Tensor{"direction.2", 0, {(uint64_t) N}, std::vector<float>(N, 0.0f)}}),
|
|
// 8: another model's hint (a warning on stderr only)
|
|
gguf({{"general.architecture", "controlvector"}, {"controlvector.model_hint", "llama"}}, {dir("direction.1", N, 1.0f)}),
|
|
// 9: no hint at all
|
|
gguf({{"general.architecture", "controlvector"}}, {dir("direction.5", N, -1.0f)}),
|
|
};
|
|
std::vector<Case> cases = {
|
|
{"add, one file, default range", {{"0", 1.0f}}, -1, -1, 1, -1},
|
|
{"add, two files scaled and summed", {{"0", 0.5f}, {"1", -2.0f}}, -1, -1, 1, -1},
|
|
{"project, one file", {{"0", 1.0f}}, -1, -1, 0, -1},
|
|
{"project, two files", {{"0", 1.5f}, {"1", 1.0f}}, -1, -1, 0, -1},
|
|
{"add, layer range 2..3", {{"0", 1.0f}, {"1", 1.0f}}, 2, 3, 1, -1},
|
|
{"add, range past the model", {{"0", 1.0f}}, 1, 99, 1, -1},
|
|
{"project, range first only", {{"0", 1.0f}}, 3, -1, 0, -1},
|
|
{"project single:2", {{"0", 1.0f}}, -1, -1, 0, 2},
|
|
{"project single:2 over a range", {{"0", 1.0f}, {"1", 1.0f}}, 2, 5, 0, 2},
|
|
{"add ignores single", {{"0", 1.0f}}, -1, -1, 1, 2},
|
|
{"project single without a direction", {{"0", 1.0f}}, -1, -1, 0, 3},
|
|
{"project single past the model", {{"0", 1.0f}}, -1, -1, 0, 6},
|
|
{"scale zero adds nothing but still counts as found", {{"0", 0.0f}}, -1, -1, 1, -1},
|
|
{"scale zero in project mode steers nothing", {{"0", 0.0f}}, -1, -1, 0, -1},
|
|
{"layers 0 / past-model / non-numeric ignored, 03 is layer 3", {{"6", 1.0f}}, -1, -1, 1, -1},
|
|
{"not a control vector", {{"2", 1.0f}}, -1, -1, 1, -1},
|
|
{"no direction tensors", {{"3", 1.0f}}, -1, -1, 1, -1},
|
|
{"wrong tensor size", {{"4", 1.0f}}, -1, -1, 1, -1},
|
|
{"wrong tensor type", {{"5", 1.0f}}, -1, -1, 1, -1},
|
|
{"all-zero directions in project mode", {{"7", 1.0f}}, -1, -1, 0, -1},
|
|
{"all-zero directions in add mode", {{"7", 1.0f}}, -1, -1, 1, -1},
|
|
{"another model's hint still loads", {{"8", 1.0f}}, -1, -1, 1, -1},
|
|
{"no hint", {{"9", 1.0f}}, -1, -1, 1, -1},
|
|
{"second file bad", {{"0", 1.0f}, {"3", 1.0f}}, -1, -1, 1, -1},
|
|
{"no steered layer in the range", {{"0", 1.0f}}, 5, 5, 1, -1},
|
|
};
|
|
std::FILE* f = std::fopen(argv[1], "w");
|
|
if (!f) return 2;
|
|
std::fprintf(f, "{\n \"source\": \"src/program/generate.cpp load_control_vectors (verbatim) over GGUF files written by gen_cvec_fixtures.cpp\",\n"
|
|
" \"n_layers\": %lld, \"n_embd\": %lld, \"hc\": %lld,\n \"blobs\": [", (long long) L, (long long) N, (long long) HC);
|
|
for (size_t i = 0; i < blobs.size(); ++i) std::fprintf(f, "%s\"%s\"", i ? "," : "", hex(blobs[i]).c_str());
|
|
std::fprintf(f, "],\n \"cases\": [");
|
|
bool first = true;
|
|
for (const Case& c : cases) {
|
|
Options o;
|
|
o.cvec_first = c.first;
|
|
o.cvec_last = c.last;
|
|
o.cvec_mode = c.mode;
|
|
o.cvec_single = c.single;
|
|
std::vector<std::pair<int, float>> used;
|
|
for (size_t k = 0; k < c.files.size(); ++k) {
|
|
const int blob = std::atoi(c.files[k].first.c_str());
|
|
const std::string path = base + "_" + std::to_string(blob) + ".gguf";
|
|
std::FILE* w = std::fopen(path.c_str(), "wb");
|
|
std::fwrite(blobs[(size_t) blob].data(), 1, blobs[(size_t) blob].size(), w);
|
|
std::fclose(w);
|
|
o.cvec_files.push_back({path, c.files[k].second});
|
|
used.push_back({blob, c.files[k].second});
|
|
}
|
|
strata::core::ModelGeometry g;
|
|
g.n_layers = L; g.n_embd = N; g.hc = HC;
|
|
strata::kernels::g_up = {};
|
|
std::string summary, err;
|
|
const bool ok = load_control_vectors(o, g, summary, err);
|
|
// the temp paths carry this process's id: name the files by their blob instead
|
|
for (size_t k = 0; k < used.size(); ++k) {
|
|
const std::string path = o.cvec_files[k].first, label = "FILE" + std::to_string(used[k].first);
|
|
for (size_t at = err.find(path); at != std::string::npos; at = err.find(path)) err.replace(at, path.size(), label);
|
|
}
|
|
std::fprintf(f, "%s\n {\"name\": %s, \"files\": [", first ? "" : ",", jstr(c.name).c_str());
|
|
for (size_t k = 0; k < used.size(); ++k) {
|
|
uint32_t bits; std::memcpy(&bits, &used[k].second, 4);
|
|
std::fprintf(f, "%s[%d, %u]", k ? "," : "", used[k].first, bits);
|
|
}
|
|
std::fprintf(f, "], \"first\": %d, \"last\": %d, \"mode\": \"%s\", \"single\": %d, \"ok\": %s, \"err\": %s, \"summary\": %s",
|
|
c.first, c.last, c.mode == 0 ? "project" : "add", c.single, ok ? "true" : "false", jstr(err).c_str(), jstr(summary).c_str());
|
|
if (ok) {
|
|
std::fprintf(f, ", \"up\": {\"mode\": %d, \"first\": %d, \"last\": %d, \"dir_bits\": [", strata::kernels::g_up.mode,
|
|
strata::kernels::g_up.first, strata::kernels::g_up.last);
|
|
for (size_t i = 0; i < strata::kernels::g_up.dir.size(); ++i) {
|
|
uint32_t bits; std::memcpy(&bits, &strata::kernels::g_up.dir[i], 4);
|
|
std::fprintf(f, "%s%u", i ? "," : "", bits);
|
|
}
|
|
std::fprintf(f, "], \"s_bits\": [");
|
|
for (size_t i = 0; i < strata::kernels::g_up.s.size(); ++i) {
|
|
uint32_t bits; std::memcpy(&bits, &strata::kernels::g_up.s[i], 4);
|
|
std::fprintf(f, "%s%u", i ? "," : "", bits);
|
|
}
|
|
std::fprintf(f, "]}");
|
|
}
|
|
std::fprintf(f, "}");
|
|
first = false;
|
|
for (const auto& [p, s] : o.cvec_files) { (void) s; std::remove(p.c_str()); }
|
|
}
|
|
std::fprintf(f, "\n ]\n}\n");
|
|
std::fclose(f);
|
|
return 0;
|
|
}
|