okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-3v (aec86299d) into f-own-int: our own Qwen vision tower (mtmd stays the default)0479b72e9

main
3.7 KiB76 linesraw
1// tools/vision/own/compare_sve.cpp - compares two strata-vision output files (SVE1) of the same picture.
2//
3//   strata-vision-own-compare <a.sve> <b.sve>
4// Prints one line:  n=<tokens> nx= ny= dim= global=<cosine of the two whole files as vectors> tokmin=<smallest per-token cosine>
5//                   tokmean=<mean per-token cosine> maxabs=<largest |a-b|> rel_rms=<rms(a-b)/rms(a)>
6// Exit 0 when both files read and have the same shape; 1 otherwise ("MISMATCH ..." printed).
7#include <algorithm>
8#include <cmath>
9#include <cstdint>
10#include <cstdio>
11#include <cstdlib>
12#include <cstring>
13#include <string>
14#include <vector>
15
16struct Sve {
17    int32_t n = 0, nx = 0, ny = 0, dim = 0;
18    std::vector<float> v;
19};
20
21static bool load(const char* path, Sve& s) {
22    FILE* f = std::fopen(path, "rb");
23    if (!f) { std::printf("MISMATCH cannot open %s\n", path); return false; }
24    int32_t h[5];
25    if (std::fread(h, sizeof h, 1, f) != 1 || h[0] != 0x31455653 || h[1] <= 0 || h[4] <= 0) { std::printf("MISMATCH %s is not an SVE1 file\n", path); std::fclose(f); return false; }
26    s.n = h[1]; s.nx = h[2]; s.ny = h[3]; s.dim = h[4];
27    s.v.resize((size_t) s.n * s.dim);
28    const bool ok = std::fread(s.v.data(), sizeof(float), s.v.size(), f) == s.v.size();
29    std::fclose(f);
30    if (!ok) std::printf("MISMATCH %s is cut short\n", path);
31    return ok;
32}
33
34int main(int argc, char** argv) {
35    int worst = 0;
36    if (argc == 5 && std::strcmp(argv[3], "--worst") == 0) worst = std::atoi(argv[4]);
37    else if (argc != 3) { std::fprintf(stderr, "usage: strata-vision-own-compare a.sve b.sve [--worst K]\n"); return 2; }
38    Sve a, b;
39    if (!load(argv[1], a) || !load(argv[2], b)) return 1;
40    if (a.n != b.n || a.nx != b.nx || a.ny != b.ny || a.dim != b.dim) {
41        std::printf("MISMATCH shapes differ: %d tokens (%dx%d, dim %d) vs %d tokens (%dx%d, dim %d)\n", a.n, a.nx, a.ny, a.dim, b.n, b.nx, b.ny, b.dim);
42        return 1;
43    }
44    double ab = 0, aa = 0, bb = 0, dd = 0, maxabs = 0, tmin = 2.0, tsum = 0;
45    struct Tok { int t; double cos, na, nb, maxd, dn; };
46    std::vector<Tok> toks;
47    for (int t = 0; t < a.n; ++t) {
48        double tab = 0, taa = 0, tbb = 0, tdd = 0, tmax = 0;
49        for (int i = 0; i < a.dim; ++i) {
50            const double x = a.v[(size_t) t * a.dim + i], y = b.v[(size_t) t * a.dim + i];
51            tab += x * y; taa += x * x; tbb += y * y;
52            dd += (x - y) * (x - y);
53            tdd += (x - y) * (x - y);
54            maxabs = std::fmax(maxabs, std::fabs(x - y));
55            tmax = std::fmax(tmax, std::fabs(x - y));
56        }
57        ab += tab; aa += taa; bb += tbb;
58        const double c = taa > 0 && tbb > 0 ? tab / std::sqrt(taa * tbb) : 0.0;
59        tmin = std::fmin(tmin, c);
60        tsum += c;
61        toks.push_back({t, c, std::sqrt(taa), std::sqrt(tbb), tmax, std::sqrt(tdd)});
62    }
63    std::printf("n=%d nx=%d ny=%d dim=%d global=%.6f tokmin=%.6f tokmean=%.6f maxabs=%.5g rel_rms=%.5g\n", a.n, a.nx, a.ny, a.dim,
64                aa > 0 && bb > 0 ? ab / std::sqrt(aa * bb) : 0.0, tmin, tsum / a.n, maxabs, aa > 0 ? std::sqrt(dd / aa) : 0.0);
65    if (worst > 0) {   // the tokens with the lowest cosine: where they sit, how large they are in each file, and how far apart
66        std::sort(toks.begin(), toks.end(), [](const Tok& p, const Tok& q) { return p.cos < q.cos; });
67        double mean_norm = std::sqrt(aa / a.n);
68        std::printf("typical token norm in A: %.4g\n", mean_norm);
69        for (int k = 0; k < worst && k < (int) toks.size(); ++k) {
70            const Tok& w = toks[(size_t) k];
71            std::printf("worst %d: token %d (x=%d y=%d of %dx%d) cos %.6f |A| %.5g |B| %.5g max|A-B| %.5g |A-B| %.5g\n", k + 1, w.t, w.t % a.nx, w.t / a.nx, a.nx,
72                        a.ny, w.cos, w.na, w.nb, w.maxd, w.dn);
73        }
74    }
75    return 0;
76}