// tests/ling32/ling32_vision_cpu_test.cpp - the Ling-3.2-Air picture path on the CPU against the float64 reference (phaseA-tests/ling32/vision/ling32_vit_ref.py). // // ling32_vision_cpu_test VISION.strata GOLDEN_DIR IMAGE_DIR [--tol 1e-4] [--threads N] [stem ...] // // per stem: the preparation (decode, smart_resize, Pillow bicubic, normalise, patchify) must give the reference's resized RGB and pixel values exactly; the encoder's // output must match the float64 output with a relative L2 error <= tol, no NaN / Inf, and the 448x448 picture must give 196 embeddings. // plus: smart_resize cases, the M-RoPE positions against upstream get_rope_index (GOLDEN_DIR/mrope_cases.json), placeholder expansion. #include "strata/ling32/ling32_json.hpp" #include "strata/ling32/ling32_vision.hpp" #include #include #include #include #include #include #include #include #include using namespace strata::ling32; static int g_fail = 0; #define CHECK(cond, ...) \ do { \ if (!(cond)) { std::printf(" FAIL: " __VA_ARGS__); std::printf("\n"); ++g_fail; } \ } while (0) template static bool read_all(const std::string& path, std::vector& v) { std::ifstream f(path, std::ios::binary | std::ios::ate); if (!f) return false; const size_t n = static_cast(f.tellg()); v.resize(n / sizeof(T)); f.seekg(0); f.read(reinterpret_cast(v.data()), static_cast(v.size() * sizeof(T))); return static_cast(f); } static std::string slurp(const std::string& p) { std::ifstream f(p, std::ios::binary); std::stringstream ss; ss << f.rdbuf(); return ss.str(); } static double now() { return std::chrono::duration(std::chrono::steady_clock::now().time_since_epoch()).count(); } static double rel_l2(const float* a, const double* b, size_t n, double* maxabs = nullptr) { double num = 0, den = 0, mx = 0; for (size_t i = 0; i < n; ++i) { const double d = static_cast(a[i]) - b[i]; num += d * d; den += b[i] * b[i]; mx = std::max(mx, std::fabs(d)); } if (maxabs) *maxabs = mx; return std::sqrt(num / std::max(den, 1e-300)); } static double rel_l2f(const std::vector& a, const std::vector& b) { double num = 0, den = 0; for (size_t i = 0; i < a.size() && i < b.size(); ++i) { const double d = static_cast(a[i]) - b[i]; num += d * d; den += static_cast(b[i]) * b[i]; } return std::sqrt(num / std::max(den, 1e-300)); } int main(int argc, char** argv) { if (argc < 4) { std::printf("usage: %s VISION.strata GOLDEN_DIR IMAGE_DIR [--tol T] [--threads N] [stem...]\n", argv[0]); return 2; } const std::string vis = argv[1], gold = argv[2], imgs = argv[3]; double tol = 1e-4; int threads = 0; std::vector stems; for (int i = 4; i < argc; ++i) { if (!std::strcmp(argv[i], "--tol") && i + 1 < argc) tol = std::atof(argv[++i]); else if (!std::strcmp(argv[i], "--threads") && i + 1 < argc) threads = std::atoi(argv[++i]); else stems.push_back(argv[i]); } if (stems.empty()) stems = {"tiny_100x60", "noise_250x370", "grad_300x500", "shapes_448", "doc_480x640", "v_rgba_130x70", "v_gray_130x70", "v_pal_130x70", "big_1920x1080"}; // the last four: preparation only std::string err; VisionWeights W; double t0 = now(); if (!W.load(vis, err)) { std::printf("load: %s\n", err.c_str()); return 1; } std::printf("weights: %.1f MiB loaded in %.2f s (depth %d, hidden %d)\n", W.bytes() / 1048576.0, now() - t0, W.c.depth, W.c.hidden); // geometry { // expected values from the reference's smart_resize (the model's image processor code) struct { int h, w, oh, ow; } cases[] = {{448, 448, 448, 448}, {300, 500, 288, 512}, {60, 100, 64, 96}, {1, 1000, 0, 0}, {5000, 5000, 2048, 2048}, {2000, 100, 1984, 96}, {64, 64, 64, 64}, {50, 50, 64, 64}, {1080, 1920, 1088, 1920}, {4096, 4096, 2048, 2048}, {33, 2000, 32, 1984}, {31, 31, 64, 64}, {10, 3000, 0, 0}}; for (auto& k : cases) { int oh = 0, ow = 0; const bool ok = vision_smart_resize(k.h, k.w, 32, W.c.min_pixels, W.c.max_pixels, oh, ow); if (k.oh == 0) CHECK(!ok, "smart_resize %dx%d (aspect above 200) should be refused", k.h, k.w); else CHECK(ok && oh == k.oh && ow == k.ow, "smart_resize %dx%d -> %dx%d, expected %dx%d", k.h, k.w, oh, ow, k.oh, k.ow); } std::printf("smart_resize cases checked\n"); } for (const std::string& stem : stems) { std::printf("== %s\n", stem.c_str()); PreparedImage im; if (!vision_prepare_file(W.c, imgs + "/" + stem + ".png", im, err)) { std::printf(" prepare: %s\n", err.c_str()); ++g_fail; continue; } std::vector rgb_ref; std::vector pix_ref; std::vector out_ref, merged_ref; if (!read_all(gold + "/" + stem + ".rgb.u8", rgb_ref) || !read_all(gold + "/" + stem + ".pix.f32", pix_ref)) { std::printf(" golden files missing\n"); ++g_fail; continue; } const bool prep_only = !read_all(gold + "/" + stem + ".out.f64", out_ref) || !read_all(gold + "/" + stem + ".merged.f64", merged_ref); // pictures that only test the preparation CHECK(rgb_ref.size() == im.rgb.size(), "resized rgb size %zu vs reference %zu", im.rgb.size(), rgb_ref.size()); size_t rgb_bad = 0; for (size_t i = 0; i < std::min(rgb_ref.size(), im.rgb.size()); ++i) rgb_bad += rgb_ref[i] != im.rgb[i]; size_t pix_bad = 0; for (size_t i = 0; i < std::min(pix_ref.size(), im.pix.size()); ++i) pix_bad += std::memcmp(&pix_ref[i], &im.pix[i], 4) != 0; CHECK(pix_ref.size() == im.pix.size(), "pixel value count %zu vs %zu", im.pix.size(), pix_ref.size()); std::printf(" prep: %dx%d (grid %dx%d, %d embeddings), resized-rgb mismatches %zu, pixel-value mismatches %zu\n", im.w, im.h, im.gh, im.gw, im.tokens(), rgb_bad, pix_bad); CHECK(rgb_bad == 0 && pix_bad == 0, "preparation differs from the reference"); if (prep_only) continue; std::vector out; VisionCheckpoints ck; t0 = now(); if (!vision_encode_cpu(W, im, out, err, threads, &ck)) { std::printf(" encode: %s\n", err.c_str()); ++g_fail; continue; } const double dt = now() - t0; size_t bad = 0; for (float v : out) bad += !std::isfinite(v); CHECK(bad == 0, "%zu NaN / Inf values in the output", bad); CHECK(out.size() == out_ref.size(), "output size %zu vs %zu", out.size(), out_ref.size()); double mx = 0; const double rel = rel_l2(out.data(), out_ref.data(), std::min(out.size(), out_ref.size()), &mx); const double rel_m = rel_l2(ck.merged.data(), merged_ref.data(), std::min(ck.merged.size(), merged_ref.size())); std::vector e_ref, b0_ref, b13_ref, b26_ref; read_all(gold + "/" + stem + ".ck_embed.f32", e_ref); read_all(gold + "/" + stem + ".ck_blk0.f32", b0_ref); read_all(gold + "/" + stem + ".ck_blk13.f32", b13_ref); read_all(gold + "/" + stem + ".ck_blk26.f32", b26_ref); std::printf(" encode: %zu embeddings x %d in %.2f s; rel L2 vs float64: embed %.2e blk0 %.2e blk13 %.2e blk26 %.2e merged %.2e OUT %.2e (max abs %.2e)\n", out.size() / W.c.out_dim, W.c.out_dim, dt, rel_l2f(ck.embed, e_ref), rel_l2f(ck.blk0, b0_ref), rel_l2f(ck.blk13, b13_ref), rel_l2f(ck.blk26, b26_ref), rel_m, rel, mx); CHECK(rel <= tol, "relative error %.3e above %.1e", rel, tol); if (stem == "shapes_448") CHECK(im.tokens() == 196 && out.size() == 196u * static_cast(W.c.out_dim), "448x448 must give 196 embeddings, got %zu", out.size() / static_cast(W.c.out_dim)); } // M-RoPE positions against upstream get_rope_index, placeholder expansion { const std::string cases = gold + "/mrope_cases.json"; const std::string txt = slurp(cases); if (txt.empty()) { std::printf("== mrope: %s missing\n", cases.c_str()); ++g_fail; } else { const json::JVal j = json::json_parse(txt); int n_ok = 0; for (size_t ci = 0; ci < j.size(); ++ci) { const json::JVal& c = j[ci]; std::vector ids; for (size_t i = 0; i < c.at("ids").size(); ++i) ids.push_back(static_cast(c.at("ids")[i].i())); std::vector> grids; for (size_t i = 0; i < c.at("grids").size(); ++i) grids.push_back({static_cast(c.at("grids")[i][0].i()), static_cast(c.at("grids")[i][1].i())}); MmPositions mp; if (!vision_mrope_positions(W.c, ids, grids, mp, err)) { std::printf(" case %zu: %s\n", ci, err.c_str()); ++g_fail; continue; } size_t diff = 0; for (int a = 0; a < 3; ++a) for (size_t i = 0; i < ids.size(); ++i) diff += mp.p[a][i] != static_cast(c.at("pos")[static_cast(a)][i].i()); const int dref = static_cast(c.at("delta").i()); CHECK(diff == 0 && mp.delta == dref, "mrope case %zu: %zu position differences, delta %d vs %d", ci, diff, mp.delta, dref); n_ok += diff == 0 && mp.delta == dref; } std::printf("== mrope: %d / %zu cases equal upstream get_rope_index\n", n_ok, j.size()); } // expansion const int NL = 198; std::vector ids = {5, 6, W.c.start_token, W.c.image_token, W.c.end_token, 7}, out; CHECK(vision_expand_pads(W.c, ids, {3}, NL, out, err), "expand: %s", err.c_str()); const std::vector want = {5, 6, W.c.start_token, W.c.image_token, W.c.image_token, W.c.image_token, W.c.end_token, NL, 7}; CHECK(out == want, "expansion result differs"); CHECK(!vision_expand_pads(W.c, ids, {3, 4}, NL, out, err), "expand must refuse a count mismatch"); std::printf("== expansion checked\n"); } std::printf("%s (%d failures)\n", g_fail ? "FAIL" : "PASS", g_fail); return g_fail ? 1 : 0; }