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>
146 lines
7.3 KiB
C++
146 lines
7.3 KiB
C++
// Host oracle for the image-embeddings path of `--serve` (GENI): the SVE1 record reader and the M-RoPE
|
|
// position table of src/program/generate.cpp.
|
|
//
|
|
// The block between "if (geni || !mrope_identity) {" and "tr(\"positions built\"" is INCLUDED VERBATIM from the
|
|
// pinned generate.cpp (extracted by build_oracle.sh, see generate_block.inc) - not retyped - and so is `fnv1a`.
|
|
// Everything around it (the variables the block reads, a CUDA-free `put` target) is declared here.
|
|
//
|
|
// Usage: gen_vision_fixtures OUT.json (writes its scratch embedding files to $TMPDIR)
|
|
#include <unistd.h>
|
|
#include <algorithm>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "strata/core/conversation_cache.hpp"
|
|
|
|
using ImgKey = strata::core::ConversationImageKey;
|
|
|
|
#include "fnv1a.inc"
|
|
|
|
struct Geometry { int64_t n_embd; };
|
|
|
|
struct Result {
|
|
std::string ve;
|
|
std::vector<int32_t> table;
|
|
std::vector<int64_t> row_offset; // float offset into img_rows per token, -1 = none
|
|
std::vector<ImgKey> imgs;
|
|
};
|
|
|
|
constexpr int64_t kImagePad = 248056;
|
|
|
|
static Result run(bool geni, const std::vector<int64_t>& ids, const std::string& emb_path, int64_t n_embd, int64_t cells,
|
|
bool mrope_identity) {
|
|
Geometry g{n_embd};
|
|
const int64_t n = (int64_t) ids.size();
|
|
std::vector<ImgKey> req_imgs;
|
|
std::vector<int32_t> mrope_host((size_t) cells * 3);
|
|
for (int64_t c = 0; c < cells; ++c) mrope_host[(size_t) c * 3] = mrope_host[(size_t) c * 3 + 1] = mrope_host[(size_t) c * 3 + 2] = (int32_t) c;
|
|
std::vector<float> img_rows;
|
|
std::vector<const float*> row_ptr;
|
|
Result out;
|
|
{
|
|
// ---------------------------------------------------------------- verbatim from generate.cpp
|
|
#include "generate_block.inc"
|
|
out.ve = ve;
|
|
} // closes the `if (geni || !mrope_identity) {` that the block opens
|
|
}
|
|
out.table = mrope_host;
|
|
out.imgs = req_imgs;
|
|
for (const float* p : row_ptr) out.row_offset.push_back(p ? (int64_t) (p - img_rows.data()) : -1);
|
|
return out;
|
|
}
|
|
|
|
// ---- SVE1 file builders ---------------------------------------------------------------------------------------
|
|
static void put_i32(std::string& s, int32_t v) { s.append((const char*) &v, 4); }
|
|
static std::string image(int32_t n, int32_t nx, int32_t ny, int32_t width, float seed, int32_t magic = 0x31455653) {
|
|
std::string s;
|
|
for (int32_t v : {magic, n, nx, ny, width}) put_i32(s, v);
|
|
for (int32_t i = 0; i < n * width; ++i) {
|
|
const float f = seed + (float) i * 0.5f;
|
|
s.append((const char*) &f, 4);
|
|
}
|
|
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 + "\"";
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
if (argc < 2) return 2;
|
|
const char* tmp = std::getenv("TMPDIR");
|
|
const std::string scratch = std::string(tmp ? tmp : "/tmp") + "/strata_sve_oracle_" + std::to_string((long long) getpid()) + ".sve";
|
|
std::FILE* f = std::fopen(argv[1], "w");
|
|
if (!f) return 2;
|
|
std::fprintf(f, "{\n \"source\": \"src/program/generate.cpp GENI embeddings block + fnv1a (verbatim, host build)\",\n \"cases\": [");
|
|
|
|
struct Case { const char* name; bool geni; std::vector<int64_t> ids; std::string file; int64_t n_embd; int64_t cells; bool identity; };
|
|
const int64_t P = kImagePad;
|
|
const std::string i1 = image(1, 1, 1, 4, 1.0f), i6 = image(6, 3, 2, 4, 10.0f), i8 = image(8, 2, 4, 4, 20.0f), i25 = image(25, 5, 5, 4, 30.0f);
|
|
std::vector<Case> cases = {
|
|
{"text identity after an image request", false, {5, 6, 7}, "", 4, 32, false},
|
|
{"text only with no file", true, {5, 6, 7, 8}, "", 4, 32, true},
|
|
{"one 1x1 image", true, {1, 2, P, 3, 4}, i1, 4, 32, true},
|
|
{"one 3x2 image", true, {1, P, P, P, P, P, P, 9, 10}, i6, 4, 40, true},
|
|
{"one 2x4 image (tall: ny > nx)", true, {7, P, P, P, P, P, P, P, P, 8}, i8, 4, 40, true},
|
|
{"two images in order", true, {1, P, P, P, P, P, P, 2, P, P, P, P, P, P, P, P, 3}, i6 + i8, 4, 64, true},
|
|
{"5x5 image then text", true, {1, 2, 3, P, P, P, P, P, P, P, P, P, P, P, P, P, P, P, P, P, P, P, P, P, P, P, P, P, 4, 5}, i25, 4, 64, true},
|
|
{"adjacent images", true, {P, P, P, P, P, P, P, P, P, P, P, P, P, P, 1}, i6 + i8, 4, 40, true},
|
|
{"image at the start", true, {P, 4}, i1, 4, 16, true},
|
|
{"identity request after (not geni, not identity)", false, {1, 2}, "", 4, 16, false},
|
|
// errors
|
|
{"prompt ends in an image", true, {1, P, P, P, P, P, P}, i6, 4, 32, true},
|
|
{"more images in the prompt than the file", true, {1, P, 2, P, 3}, i1, 4, 32, true},
|
|
{"more images in the file than the prompt", true, {1, P, 2, 3}, i1 + i1, 4, 32, true},
|
|
{"fewer image_pad tokens than rows", true, {1, P, P, 2, 3, 4, 5, 6, 7}, i6, 4, 32, true},
|
|
{"bad magic", true, {1, P, 2}, image(1, 1, 1, 4, 1.0f, 0x12345678), 4, 32, true},
|
|
{"wrong width", true, {1, P, 2}, image(1, 1, 1, 8, 1.0f), 4, 32, true},
|
|
{"grid does not multiply", true, {1, P, 2}, image(6, 3, 3, 4, 1.0f), 4, 32, true},
|
|
{"zero rows", true, {1, P, 2}, image(0, 0, 0, 4, 1.0f), 4, 32, true},
|
|
{"short header", true, {1, P, 2}, i1.substr(0, 12), 4, 32, true},
|
|
{"short data", true, {1, P, 2}, i1.substr(0, i1.size() - 3), 4, 32, true},
|
|
{"second record truncated", true, {1, P, 2, P, 3}, i1 + i1.substr(0, 25), 4, 32, true},
|
|
};
|
|
bool first = true;
|
|
for (const Case& c : cases) {
|
|
if (!c.file.empty() || c.geni) {
|
|
std::FILE* w = std::fopen(scratch.c_str(), "wb");
|
|
std::fwrite(c.file.data(), 1, c.file.size(), w);
|
|
std::fclose(w);
|
|
}
|
|
// "no file": point at a path that does not exist
|
|
const std::string path = c.file.empty() && c.geni ? std::string("/nonexistent/embeddings.sve") : scratch;
|
|
const Result r = run(c.geni, c.ids, path, c.n_embd, c.cells, c.identity);
|
|
std::fprintf(f, "%s\n {\"name\": %s, \"geni\": %s, \"mrope_identity\": %s, \"n_embd\": %lld, \"cells\": %lld, \"file_hex\": \"%s\", \"ids\": [",
|
|
first ? "" : ",", jstr(c.name).c_str(), c.geni ? "true" : "false", c.identity ? "true" : "false",
|
|
(long long) c.n_embd, (long long) c.cells, hex(c.file).c_str());
|
|
for (size_t i = 0; i < c.ids.size(); ++i) std::fprintf(f, "%s%lld", i ? "," : "", (long long) c.ids[i]);
|
|
std::fprintf(f, "], \"ve\": %s, \"table\": [", jstr(r.ve).c_str());
|
|
for (size_t i = 0; i < r.table.size(); ++i) std::fprintf(f, "%s%d", i ? "," : "", r.table[i]);
|
|
std::fprintf(f, "], \"row_offset\": [");
|
|
for (size_t i = 0; i < r.row_offset.size(); ++i) std::fprintf(f, "%s%lld", i ? "," : "", (long long) r.row_offset[i]);
|
|
std::fprintf(f, "], \"imgs\": [");
|
|
for (size_t i = 0; i < r.imgs.size(); ++i)
|
|
std::fprintf(f, "%s[%lld,\"%016llx\"]", i ? "," : "", (long long) r.imgs[i].start, (unsigned long long) r.imgs[i].hash);
|
|
std::fprintf(f, "]}");
|
|
first = false;
|
|
}
|
|
std::remove(scratch.c_str());
|
|
std::fprintf(f, "\n ]\n}\n");
|
|
std::fclose(f);
|
|
return 0;
|
|
}
|