Files
leleka/engine/strata/src/core/keyless.cpp
T
Pavlo Ratushnyi 4013e0c9bd
Desktop foundation / core (ubuntu-latest) (push) Failing after 28s
Desktop foundation / core (macos-latest) (push) Canceled after 0s
Desktop foundation / core (windows-latest) (push) Canceled after 0s
Linux / verify (push) Canceled after 0s
Linux / keyless-cuda (push) Canceled after 0s
Linux / static-cuda-package (push) Canceled after 0s
Linux / hosted-cuda-package (push) Canceled after 0s
Linux / linux-static-cli (push) Canceled after 0s
Linux / linux-cli-container (push) Canceled after 0s
Linux / release-cli (push) Canceled after 0s
Import tracked Strata Next source snapshot
2026-10-05 18:15:08 +02:00

80 lines
4.5 KiB
C++

#include "strata/core/keyless.hpp"
#include <cuda_runtime.h>
#include <array>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <map>
#include <vector>
namespace strata::core {
namespace {
std::map<int64_t, float*> routes;
uint32_t u32(std::istream& f) {
unsigned char b[4]{}; f.read((char*) b, 4);
return (uint32_t)b[0] | (uint32_t)b[1]<<8 | (uint32_t)b[2]<<16 | (uint32_t)b[3]<<24;
}
uint64_t u64(std::istream& f) { const uint64_t a = u32(f); return a | (uint64_t)u32(f)<<32; }
}
const float* keyless_routing(int64_t layer) {
const auto it = routes.find(layer);
return it == routes.end() ? nullptr : it->second;
}
bool keyless_load(const char* path, const WeightTable& weights, const ModelGeometry& g, std::string& error) {
if (!routes.empty()) { error = "keyless routing already loaded"; return false; }
std::ifstream f(path, std::ios::binary);
std::array<char,8> magic{}; f.read(magic.data(), 8);
const uint32_t version = u32(f), layers = u32(f), heads = u32(f), dim = u32(f);
if (!f || std::memcmp(magic.data(), "SKLROUT1", 8) || version != 1 ||
layers != (uint32_t)g.n_qsa_layers() || heads != (uint32_t)g.n_head || dim != (uint32_t)g.head_dim || dim != 256) {
error = "invalid keyless artifact header/geometry"; return false;
}
std::map<int64_t, float*> pending;
auto fail = [&](const std::string& why) {
error = why; for (auto& row : pending) cudaFree(row.second); return false;
};
for (uint32_t ordinal = 0; ordinal < layers; ++ordinal) {
const uint32_t layer = u32(f);
if (!f || layer >= g.n_layers || !is_qsa_layer(g, layer) || pending.count(layer))
return fail("keyless artifact has invalid/duplicate layer");
for (const char* role : {"q", "k", "v"}) {
const uint32_t kind = u32(f); const uint64_t bytes = u64(f), wanted = u64(f);
const auto* w = weights.find("blk." + std::to_string(layer) + ".attn_" + role + ".weight");
// Quantized projection size is recorded by the source decoder. Bound before any device read.
const uint64_t expected_elements = (uint64_t)g.n_embd * g.head_dim *
(std::strcmp(role,"q") == 0 ? g.n_head * 2 : g.n_head_kv);
if (!f || !w || !w->native_data || w->native_type != (int)kind ||
bytes == 0 || bytes > expected_elements * 4 || expected_elements != (uint64_t)w->elements)
return fail("keyless artifact source projection geometry/type mismatch");
// NativeDense owns source GGUF blocks. Verify the artifact was derived from these actual bytes.
// Source byte length must agree with ggml geometry; ne0/ne1 cannot validate a quantized byte span.
const uint64_t block_elements = kind == 42 ? 64 : kind == 8 ? 32 : 256;
const uint64_t block_bytes = kind == 42 ? 18 : kind == 11 ? 110 : kind == 12 ? 144 :
kind == 13 ? 176 : kind == 14 ? 210 : kind == 23 ? 136 : kind == 8 ? 34 : 0;
if (!block_bytes || expected_elements % block_elements || bytes != expected_elements / block_elements * block_bytes)
return fail("keyless artifact unsupported/mismatched native projection encoding");
std::vector<uint8_t> raw((size_t)bytes);
if (cudaMemcpy(raw.data(), w->native_data, raw.size(), cudaMemcpyDeviceToHost) != cudaSuccess)
return fail("keyless source projection readback failed");
uint64_t hash = 0xcbf29ce484222325ull;
for (uint8_t byte : raw) hash = (hash ^ byte) * 0x100000001b3ull;
if (hash != wanted) return fail("keyless artifact source projection fingerprint mismatch");
}
std::vector<float> matrix((size_t)heads * dim * dim);
f.read((char*)matrix.data(), matrix.size()*sizeof(float));
if (!f || !std::all_of(matrix.begin(), matrix.end(), [](float v) { return std::isfinite(v); }))
return fail("truncated/nonfinite keyless query matrix");
float* device = nullptr;
if (cudaMalloc((void**)&device, matrix.size()*sizeof(float)) != cudaSuccess)
return fail("keyless query matrices do not fit in GPU memory");
pending[layer] = device;
if (cudaMemcpy(device, matrix.data(), matrix.size()*sizeof(float), cudaMemcpyHostToDevice) != cudaSuccess)
return fail("keyless query matrix upload failed");
}
if (f.peek() != std::char_traits<char>::eof()) return fail("keyless artifact has trailing data");
routes = std::move(pending);
return true;
}
}