Files
Strata/engine/tests/ling32/ling32_reader_test.cpp
Niko1221 f60ba0d68e ling32_reader_test: do not throw when the temp file cannot be removed (Windows keeps a file the reader holds open)
On Windows the test died with an uncaught std::filesystem_error from remove() (the reader of the v1.0 file still holds it open), which the runtime turns into a fast-fail abort (exit code 0xC0000409, no output): the reader test had never actually passed on a Windows build; the Linux run was fine.  remove() now takes an error_code.  With it the whole test passes on the MSVC build, including the fixture part (PASS, 0 failed).
2026-12-01 22:14:25 +01:00

223 lines
13 KiB
C++

// tests/ling32/ling32_reader_test.cpp - the `.strata` reader on both header layouts, and the Ling-3.2-Air file view on top of it.
// No GPU. Usage: ling32_reader_test [FIXTURE.strata] (the fixture is made by ling32_pyref.py make-fixture)
//
// 1. a .strata v1.0 file (4 KiB layout, table at 0x10, Hy4-Flash manifest) opens exactly as before: layout, sections, manifest, directory;
// 2. a .strata v1.1 file (16 KiB layout, table at 0x20, tile_layout_id) whose Section 0 is the manifest's zstd frame followed by a
// trailing skippable alignment frame (0x184D2A50) opens, and the manifest is the first frame only;
// 3. the model family of both manifests (arch hy_v4_flash -> Hy4Flash; strata_family bailing_moe_v3 -> Ling32Air);
// 4. the fixture (if given): a real v1.1 Ling-3.2-Air-shaped file: sections, manifest -> Cfg, tensor tables, Section 3 directory
// validated against the manifest and the (rank, layer) order of Section 4, Section 2 rank map, an expert slab read.
#include "strata/ling32/ling32_file.hpp"
#include <zstd.h>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <string>
#include <vector>
using namespace strata;
using namespace strata::artifact;
using namespace strata::ling32;
namespace {
int g_fail = 0;
void check(bool ok, const std::string& what) {
std::printf(" %-100s %s\n", what.c_str(), ok ? "ok" : "FAIL");
if (!ok) ++g_fail;
}
template <class T> void put(std::vector<uint8_t>& b, size_t at, T v) {
if (b.size() < at + sizeof(T)) b.resize(at + sizeof(T), 0);
std::memcpy(b.data() + at, &v, sizeof(T));
}
uint64_t align_up(uint64_t n, uint64_t a) { return (n + a - 1) / a * a; }
// a valid zstd frame holding `text` as one raw block
std::vector<uint8_t> zstd_raw_frame(const std::string& text) {
std::vector<uint8_t> f(4);
const uint32_t magic = 0xFD2FB528u, n = static_cast<uint32_t>(text.size());
std::memcpy(f.data(), &magic, 4);
f.push_back(0xA0);
for (int i = 0; i < 4; ++i) f.push_back(static_cast<uint8_t>(n >> (8 * i)));
const uint32_t bh = 1u | (n << 3);
for (int i = 0; i < 3; ++i) f.push_back(static_cast<uint8_t>(bh >> (8 * i)));
f.insert(f.end(), text.begin(), text.end());
return f;
}
struct Built { std::filesystem::path path; uint64_t off[6] = {}, len[6] = {}; };
// kLayers x kExperts directory entries, one 16 KiB (or 4 KiB) body each. k16: the v1.1 layout with the trailing skippable frame.
constexpr int kLayers = 3, kExperts = 4;
Built build(const char* name, bool k16, const std::string& manifest) {
Built r;
r.path = std::filesystem::temp_directory_path() / name;
const uint64_t page = k16 ? 16384 : 4096;
std::vector<uint8_t> man = zstd_raw_frame(manifest);
uint64_t man_len = man.size();
if (k16) { // pad with a skippable frame to the page, as the real Section 0 is
uint64_t total = align_up(man.size(), page);
if (total - man.size() < 8) total += page;
const uint32_t sk_magic = 0x184D2A50u, sk_size = static_cast<uint32_t>(total - man.size() - 8);
const size_t at = man.size();
man.resize(total, 0);
std::memcpy(man.data() + at, &sk_magic, 4);
std::memcpy(man.data() + at + 4, &sk_size, 4);
man_len = total;
}
uint64_t cur = page;
auto place = [&](int id, uint64_t len) { r.off[id] = cur; r.len[id] = len; cur = align_up(cur + len, page); };
const int n = kLayers * kExperts;
place(1, 100);
place(4, static_cast<uint64_t>(n) * page);
place(3, k16 ? align_up(static_cast<uint64_t>(n) * 32, page) : static_cast<uint64_t>(n) * 32);
place(5, 83);
place(0, man_len);
std::vector<uint8_t> f(cur, 0);
std::memcpy(f.data() + r.off[0], man.data(), man.size());
for (int k = 0; k < n; ++k) {
const size_t e = static_cast<size_t>(r.off[3]) + static_cast<size_t>(k) * 32;
put<uint16_t>(f, e + 0, static_cast<uint16_t>(k % kExperts));
f[e + 2] = 4;
f[e + 3] = 8;
put<uint64_t>(f, e + 8, static_cast<uint64_t>(k) * page);
put<uint32_t>(f, e + 16, static_cast<uint32_t>(page));
put<uint32_t>(f, e + 20, 64);
std::memset(f.data() + r.off[4] + static_cast<uint64_t>(k) * page, 0x30 + k, 64);
}
const uint8_t magic[8] = {'S', 'T', 'R', 'A', 'T', 'A', 0x00, 0x01};
std::memcpy(f.data(), magic, 8);
put<uint32_t>(f, 8, 1);
put<uint16_t>(f, 12, 0);
put<uint16_t>(f, 14, 6);
if (k16) put<uint16_t>(f, 0x10, 1); // tile_layout_id
const size_t tab = k16 ? 0x20 : 0x10;
for (int i = 0; i < 6; ++i) {
const size_t e = tab + static_cast<size_t>(i) * 64;
put<uint64_t>(f, e, r.off[i]);
put<uint64_t>(f, e + 8, r.len[i]);
put<uint32_t>(f, e + 48, static_cast<uint32_t>(i));
}
std::ofstream(r.path, std::ios::binary).write(reinterpret_cast<const char*>(f.data()), static_cast<std::streamsize>(f.size()));
return r;
}
const char* kHy4Manifest =
"{\"format\":\"strata-v1.0\",\"arch\":\"hy_v4_flash\",\"num_physical_layers\":3,\"num_experts_per_layer\":4,\"config\":{}}";
const char* kLingMini =
"{\"format\":\"strata/1.1\",\"strata_family\":\"bailing_moe_v3\",\"n_moe_layers\":3,\"n_experts_per_layer\":4,"
"\"config\":{\"model_type\":\"bailing_moe_v3_vl\",\"text_config\":{\"num_hidden_layers\":5,\"first_k_dense_replace\":2,"
"\"layer_group_size\":3,\"hidden_size\":256,\"num_experts\":4,\"expert_swiglu_limit_list\":[0,0,0,4,4]}},"
"\"trunk_tensors\":[{\"name\":\"model.norm.weight\",\"format\":\"F32\",\"shape\":[256],\"offset\":0,\"bytes\":1024}],"
"\"chat_template\":\"a { b } \\\"c\\\"\"}";
void test_v10() {
std::printf("[.strata v1.0, 4 KiB layout]\n");
Built b = build("ling32_rt_v10.strata", false, kHy4Manifest);
StrataFileReader rd;
std::string err;
{ const bool ok = rd.open(b.path.string(), err); check(ok, "opens: " + err); }
const auto& h = rd.header();
check(h.layout == StrataLayout::Page4K && h.page_size == 4096 && h.section_table_offset == 0x10 && h.tile_layout_id == 0, "4 KiB layout, table at 0x10, no tile id");
bool secs = true;
for (int i = 0; i < 6; ++i) secs = secs && h.sections[i].file_offset == b.off[i] && h.sections[i].byte_length == b.len[i];
check(secs, "six sections as written");
check(rd.manifest_json() == kHy4Manifest, "manifest text");
check(rd.expert_directory().size() == kLayers * kExperts, "12 directory entries (length / 32, exact)");
std::vector<uint8_t> body;
check(rd.read_expert_body(5, body, err) && body.size() == 4096 && body[0] == 0x35, "expert body 5 from Section 4");
check(strata_file_family(rd) == ModelFamily::Hy4Flash, "family from arch: Hy4Flash");
{ std::error_code ec_; std::filesystem::remove(b.path, ec_); } // Windows cannot delete a file the reader still holds open: do not throw
}
void test_v11_skippable() {
std::printf("[.strata v1.1, 16 KiB layout, Section 0 + skippable frame]\n");
Built b = build("ling32_rt_v11.strata", true, kLingMini);
StrataFileReader rd;
std::string err;
{ const bool ok = rd.open(b.path.string(), err); check(ok, "opens: " + err); }
const auto& h = rd.header();
check(h.layout == StrataLayout::Page16K && h.page_size == 16384 && h.section_table_offset == 0x20 && h.tile_layout_id == 1, "16 KiB layout, table at 0x20, tile_layout_id 1");
bool aligned = true;
for (int i = 0; i < 6; ++i) aligned = aligned && h.sections[i].file_offset % 16384 == 0;
check(aligned, "section offsets 16 KiB aligned");
std::vector<uint8_t> s0;
check(rd.read_section(0, s0, err) && s0.size() == b.len[0] && s0.size() % 16384 == 0, "Section 0 is whole pages");
const size_t first = ZSTD_findFrameCompressedSize(s0.data(), s0.size());
uint32_t next = 0;
if (first + 4 <= s0.size()) std::memcpy(&next, s0.data() + first, 4);
check(!ZSTD_isError(first) && first < s0.size() && (next & 0xFFFFFFF0u) == 0x184D2A50u, "Section 0 = zstd frame + trailing skippable frame (0x184D2A5x)");
check(rd.manifest_json() == kLingMini, "manifest text is the first frame only");
check(rd.expert_directory().size() == kLayers * kExperts, "padded Section 3: 12 entries, not 16384 / 32");
std::vector<uint8_t> body;
check(rd.read_expert_body(7, body, err) && body.size() == 16384 && body[0] == 0x37, "expert body 7 from Section 4");
check(strata_file_family(rd) == ModelFamily::Ling32Air, "family from strata_family: Ling32Air");
{ std::error_code ec_; std::filesystem::remove(b.path, ec_); } // Windows cannot delete a file the reader still holds open: do not throw
Ling32File lf;
Built b2 = build("ling32_rt_v11b.strata", true, kLingMini);
{ const bool ok = lf.open(b2.path.string(), err); check(ok, "Ling32File opens the mini v1.1 file: " + err); }
check(lf.cfg().n_layers == 5 && lf.cfg().first_k_dense == 2 && lf.cfg().layer_group_size == 3 && lf.cfg().D == 256, "Cfg read from text_config");
check(lf.cfg().is_mla(2) && lf.cfg().is_mla(5) && !lf.cfg().is_mla(3) && lf.cfg().routed_limit(3) == 4.0f && lf.cfg().routed_limit(2) == 0.0f, "layer kinds and clamp list");
check(lf.man().chat_template == "a { b } \"c\"", "chat_template string unescaped");
check(lf.man().find("model.norm.weight") && lf.man().find("model.norm.weight")->fmt == Fmt::F32, "trunk tensor table");
{ std::error_code ec_; std::filesystem::remove(b2.path, ec_); } // Windows cannot delete a file the reader still holds open: do not throw
Built b3 = build("ling32_rt_v10_as_ling.strata", false, kLingMini); // a 4 KiB file is not a Ling file
Ling32File lf3;
check(!lf3.open(b3.path.string(), err) && err.find("v1.1") != std::string::npos, "a v1.0 file is refused by Ling32File with a reason");
{ std::error_code ec_; std::filesystem::remove(b3.path, ec_); } // Windows cannot delete a file the reader still holds open: do not throw
Built b4 = build("ling32_rt_hy4_16k.strata", true, kHy4Manifest); // a 16 KiB file of another family
check(!lf3.open(b4.path.string(), err) && err.find("not a Ling-3.2-Air") != std::string::npos, "a v1.1 file of another family is refused");
{ std::error_code ec_; std::filesystem::remove(b4.path, ec_); } // Windows cannot delete a file the reader still holds open: do not throw
}
void test_fixture(const std::string& path) {
std::printf("[fixture %s]\n", path.c_str());
Ling32File f;
std::string err;
if (!f.open(path, err)) { check(false, "opens: " + err); return; }
check(true, "opens as Ling32Air");
const Cfg& c = f.cfg();
check(c.n_layers == 6 && c.first_k_dense == 2 && c.D == 256 && c.n_exp == 64 && c.top_k == 8 && c.H == 4 && c.hd == 64, "Cfg from the manifest");
check(!c.is_mla(0) && !c.is_mla(1) && c.is_mla(2) && !c.is_mla(3) && !c.is_mla(4) && c.is_mla(5), "MLA layers 2, 5 (layer_group_size 3)");
check(c.routed_limit(3) == 0.0f && c.routed_limit(4) == 4.0f && c.shared_limit(3) == 3.0f && c.shared_limit(5) == 5.0f, "clamp lists: 0 = unlimited");
const auto& h = f.reader().header();
bool al = true;
for (int i = 0; i < 6; ++i) al = al && h.sections[i].file_offset % 16384 == 0 && h.sections[i].byte_length % 16384 == 0;
check(al, "all six sections 16 KiB aligned");
check(f.man().trunk.size() > 100 && f.man().sec2.size() == 3 + 2 * 2 * 4, "tensor tables (trunk, Section 2: rank map + 2 x 4 lookahead layers x {w, b})");
const TensorInfo* kvb = f.man().find("model.layers.2.attention.kv_b_proj.weight");
check(kvb && kvb->fmt == Fmt::BF16 && kvb->shape.size() == 2 && kvb->shape[0] == 512 && kvb->shape[1] == 128, "kv_b_proj is BF16 [H*(nope+v), kv_lora] as on the real file");
check(f.validate_directory(err), "Section 3 directory vs manifest, 16 KiB slabs, (rank, layer) physical order: " + err);
std::vector<uint16_t> r2e, e2r;
check(f.load_rank_map(r2e, e2r, err), "rank map loads: " + err);
bool inv = r2e.size() == 4 * 64;
for (int m = 0; inv && m < 4; ++m)
for (int r = 0; r < 64; ++r) {
const int eid = r2e[m * 64 + r];
inv = inv && e2r[m * 64 + eid] == r && f.expert(m + 2, eid).aux_offsets[0] == static_cast<uint32_t>(r);
}
check(inv, "rank_to_eid / eid_to_rank invert each other and match the directory's aux_offsets[0]");
std::vector<uint8_t> slab;
check(f.reader().read_expert_body(5, slab, err) && slab.size() % 16384 == 0, "an expert slab reads");
std::vector<float> w;
const TensorInfo* ln = f.man().find("model.norm.weight");
check(ln && f.read_float(*ln, 1, w, err) && w.size() == 256 && w[0] > 0.3f && w[0] < 1.7f, "F32 tensor reads");
const TensorInfo* emb = f.man().find("model.word_embeddings.weight");
check(emb && f.read_float(*emb, 1, w, err) && w.size() == 512 * 256, "S8_K embedding dequantises");
check(kvb && f.read_float(*kvb, 1, w, err) && w.size() == 512 * 128, "BF16 tensor widens to float");
}
} // namespace
int main(int argc, char** argv) {
test_v10();
test_v11_skippable();
if (argc > 1) test_fixture(argv[1]);
std::printf("%s (%d failed)\n", g_fail ? "FAIL" : "PASS", g_fail);
return g_fail ? 1 : 0;
}