okami.mom/strata

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

Niko1221Merge f-vit-exact: the picture reader follows the float32 reference closer on the CPU and on the GPU60d431e2a

main
22.2 KiB388 linesraw
1// tools/vision/own/test_cpu_kernels.cpp - every kernel tier of the CPU tower (the ones this CPU runs) against double-precision references, odd sizes and tails
2// included, and the fork-join team (every task of every run exactly once).   strata-vision-own-cpu-test
3#include "cpu_kernels.h"
4#include "cpu_team.h"
5#include "vit_cpu.h"
6#include "vit_f16.h"
7
8#include <atomic>
9#include <cmath>
10#include <cstdio>
11#include <cstdlib>
12#include <cstring>
13#include <limits>
14#include <random>
15#include <vector>
16
17using namespace strata_vit;
18
19static int g_fail = 0;
20static void check(bool ok, const char* what, const char* tier, double worst, double tol) {
21    if (!ok) {
22        ++g_fail;
23        std::printf("FAIL %-8s %s: worst %.3g (tolerance %.3g)\n", tier, what, worst, tol);
24    }
25}
26
27static std::mt19937 rng(12345);
28static float rnd(float lo, float hi) { return std::uniform_real_distribution<float>(lo, hi)(rng); }
29
30static void test_gemm(const CpuOps& o) {
31    const int Ms[] = {1, 5, 13, 49, 100}, Ks[] = {1, 7, 72, 256, 300}, Ns[] = {1, 8, 17, 33, 72, 100};
32    double worst = 0, tol = 0;
33    for (int M : Ms)
34        for (int K : Ks)
35            for (int N : Ns)
36                for (int init = 0; init < 3; ++init) {
37                    const int lda = K + 3, ldb = (N + 63) / 64 * 64 + 64, ldc = N + 5;
38                    std::vector<float> A((size_t) M * lda), B((size_t) K * ldb + 128), C((size_t) M * ldc), C0, bias((size_t) N);
39                    for (auto& v : A) v = rnd(-1, 1);
40                    for (auto& v : B) v = rnd(-1, 1);
41                    for (auto& v : C) v = rnd(-1, 1);
42                    for (auto& v : bias) v = rnd(-1, 1);
43                    C0 = C;
44                    o.gemm(A.data(), (size_t) lda, M, B.data(), (size_t) ldb, K, N, C.data(), (size_t) ldc, init, init == 1 ? nullptr : bias.data());
45                    for (int m = 0; m < M; ++m)
46                        for (int n = 0; n < N; ++n) {
47                            double s = init == 0 ? 0.0 : C0[(size_t) m * ldc + n];
48                            if (init != 1) s += bias[(size_t) n];
49                            for (int k = 0; k < K; ++k) s += (double) A[(size_t) m * lda + k] * B[(size_t) k * ldb + n];
50                            worst = std::max(worst, std::fabs(s - C[(size_t) m * ldc + n]));
51                        }
52                    // columns past N and rows past M must be untouched
53                    for (int m = 0; m < M; ++m)
54                        for (int n = N; n < ldc; ++n)
55                            if (C[(size_t) m * ldc + n] != C0[(size_t) m * ldc + n]) worst = 1e9;
56                    tol = std::max(tol, 1e-5 * (K + 2));
57                }
58    check(worst <= tol, "gemm (odd M, K, N; init modes 0, 1, 2)", o.name, worst, tol);
59}
60
61static uint16_t rnd_f16bits() { return (uint16_t) ((rng() & 0x8000) | ((rng() % 28 + 2) << 10) | (rng() & 0x3ff)); }   // a finite normal half
62static uint16_t rnd_bf16bits() { return (uint16_t) ((rng() & 0x8000) | ((rng() % 200 + 20) << 7) | (rng() & 0x7f)); }
63
64static void test_pack_values(const CpuOps& o) {
65    const int ldw = 300, rows = 40, K = 300;
66    for (int kind = 0; kind <= 2; ++kind) {
67        std::vector<uint16_t> w16((size_t) rows * ldw);
68        std::vector<float> w32((size_t) rows * ldw);
69        for (size_t i = 0; i < w16.size(); ++i) {
70            w16[i] = kind == 2 ? rnd_f16bits() : rnd_bf16bits();
71            w32[i] = rnd(-2, 2);
72        }
73        const void* W = kind == 0 ? (const void*) w32.data() : (const void*) w16.data();
74        for (int nvalid : {o.NR, 5}) {
75            const int n0 = 3, k0 = 17, kc = 200;
76            std::vector<float> dst((size_t) kc * o.NR, -7.0f);
77            o.pack_w(W, kind, (size_t) ldw, n0, nvalid, k0, kc, dst.data());
78            double worst = 0;
79            for (int k = 0; k < kc; ++k)
80                for (int c = 0; c < o.NR; ++c) {
81                    float want = 0.0f;
82                    if (c < nvalid) {
83                        const size_t idx = (size_t) (n0 + c) * ldw + (size_t) (k0 + k);
84                        if (kind == 0) want = w32[idx];
85                        else if (kind == 1) want = quant::bf16_to_f32(w16[idx]);
86                        else want = quant::f16_to_f32(w16[idx]);
87                    }
88                    worst = std::max(worst, (double) std::fabs(want - dst[(size_t) k * o.NR + c]));
89                }
90            check(worst == 0.0, kind == 0 ? "pack_w float32" : kind == 1 ? "pack_w BF16" : "pack_w F16", o.name, worst, 0.0);
91        }
92    }
93    (void) K;
94}
95
96static void test_conv(const CpuOps& o) {
97    for (int kind = 1; kind <= 2; ++kind)
98        for (size_t n : {1u, 15u, 32u, 33u, 5000u, 8192u + 7u}) {
99            std::vector<uint16_t> w(n);
100            for (auto& v : w) v = kind == 2 ? rnd_f16bits() : rnd_bf16bits();
101            std::vector<float> d(n, -1.0f);
102            o.conv(w.data(), kind, n, d.data());
103            double worst = 0;
104            for (size_t i = 0; i < n; ++i) worst = std::max(worst, (double) std::fabs(d[i] - (kind == 1 ? quant::bf16_to_f32(w[i]) : quant::f16_to_f32(w[i]))));
105            check(worst == 0.0, kind == 1 ? "conv BF16" : "conv F16", o.name, worst, 0.0);
106        }
107}
108
109static void test_round(const CpuOps& o) {
110    const CpuOps& ref = cpu_ops_scalar();
111    for (int kind = 1; kind <= 2; ++kind)
112        for (size_t n : {1u, 7u, 8u, 17u, 100u, 4099u}) {
113            std::vector<float> x(n), a, b;
114            for (size_t i = 0; i < n; ++i) x[i] = (i % 5 == 0) ? rnd(-1e-4f, 1e-4f) : (i % 7 == 0) ? rnd(-3000, 3000) : rnd(-4, 4);
115            a = x;
116            b = x;
117            o.round16(a.data(), n, kind);
118            ref.round16(b.data(), n, kind);
119            size_t bad = 0;
120            for (size_t i = 0; i < n; ++i) bad += std::memcmp(&a[i], &b[i], 4) != 0;
121            check(bad == 0, kind == 1 ? "round16 BF16 equals the scalar tier bit for bit" : "round16 F16 equals the scalar tier bit for bit", o.name, (double) bad, 0.0);
122        }
123}
124
125static void test_gelu(const CpuOps& o) {
126    const int rows = 3, n = 203, ld = 211;
127    std::vector<float> x((size_t) rows * ld), y;
128    for (auto& v : x) v = rnd(-12, 12);
129    y = x;
130    o.gelu(y.data(), (size_t) ld, rows, n);
131    double worst = 0;
132    for (int r = 0; r < rows; ++r)
133        for (int i = 0; i < n; ++i) {
134            const double v = x[(size_t) r * ld + i];
135            const double want = 0.5 * v * (1.0 + std::tanh(0.7978845608028654 * v * (1.0 + 0.044715 * v * v)));
136            worst = std::max(worst, std::fabs(want - y[(size_t) r * ld + i]) / std::max(1.0, std::fabs(v)));
137        }
138    for (int r = 0; r < rows; ++r)
139        for (int i = n; i < ld; ++i)
140            if (y[(size_t) r * ld + i] != x[(size_t) r * ld + i]) worst = 1e9;
141    check(worst <= 3e-6, "gelu", o.name, worst, 3e-6);
142}
143
144static void test_layernorm(const CpuOps& o) {
145    for (int D : {1152, 7, 72, 4608}) {
146        std::vector<float> x((size_t) D), w((size_t) D), b((size_t) D), y((size_t) D);
147        for (int i = 0; i < D; ++i) { x[(size_t) i] = rnd(-3, 5); w[(size_t) i] = rnd(0.5f, 1.5f); b[(size_t) i] = rnd(-1, 1); }
148        o.layernorm(x.data(), y.data(), D, w.data(), b.data(), 1e-6f);
149        double s = 0, q = 0, worst = 0;
150        for (int i = 0; i < D; ++i) s += x[(size_t) i];
151        const double mean = s / D;
152        for (int i = 0; i < D; ++i) q += (x[(size_t) i] - mean) * (x[(size_t) i] - mean);
153        const double scale = 1.0 / std::sqrt(q / D + 1e-6);
154        for (int i = 0; i < D; ++i) worst = std::max(worst, std::fabs((x[(size_t) i] - mean) * scale * w[(size_t) i] + b[(size_t) i] - y[(size_t) i]));
155        check(worst <= 2e-5, "layernorm", o.name, worst, 2e-5);
156    }
157}
158
159static void test_add(const CpuOps& o) {
160    for (size_t n : {1u, 7u, 8u, 33u, 1000u}) {
161        std::vector<float> x(n), y(n), z;
162        for (size_t i = 0; i < n; ++i) { x[i] = rnd(-1, 1); y[i] = rnd(-1, 1); }
163        z = x;
164        o.add(z.data(), y.data(), n);
165        double worst = 0;
166        for (size_t i = 0; i < n; ++i) worst = std::max(worst, (double) std::fabs(z[i] - (x[i] + y[i])));
167        check(worst == 0.0, "add", o.name, worst, 0.0);
168    }
169}
170
171// the online softmax over two key tiles, then P*V by the product kernel, against a plain softmax in double
172static void test_attention(const CpuOps& o) {
173    const int qr = 13, hd = 72, T1 = 100, T2 = 37, n = T1 + T2, lds = 256, ldo = 80, ldv = 3 * 1152;
174    std::vector<float> S1((size_t) qr * lds), S2((size_t) qr * lds), Sfull((size_t) qr * n), V((size_t) n * ldv + 256), O((size_t) qr * ldo, 0.0f), m((size_t) qr, -3.0e38f), l((size_t) qr, 0.0f);
175    for (int r = 0; r < qr; ++r)
176        for (int j = 0; j < n; ++j) {
177            const float s = rnd(-30, 30);
178            Sfull[(size_t) r * n + j] = s;
179            (j < T1 ? S1[(size_t) r * lds + j] : S2[(size_t) r * lds + (j - T1)]) = s;
180        }
181    for (auto& v : V) v = rnd(-1, 1);
182    o.softmax_update(S1.data(), (size_t) lds, qr, T1, m.data(), l.data(), O.data(), (size_t) ldo, hd);
183    o.gemm(S1.data(), (size_t) lds, qr, V.data(), (size_t) ldv, T1, hd, O.data(), (size_t) ldo, 1, nullptr);
184    o.softmax_update(S2.data(), (size_t) lds, qr, T2, m.data(), l.data(), O.data(), (size_t) ldo, hd);
185    o.gemm(S2.data(), (size_t) lds, qr, V.data() + (size_t) T1 * ldv, (size_t) ldv, T2, hd, O.data(), (size_t) ldo, 1, nullptr);
186    double worst = 0;
187    for (int r = 0; r < qr; ++r) {
188        double mx = -1e300, den = 0;
189        for (int j = 0; j < n; ++j) mx = std::max(mx, (double) Sfull[(size_t) r * n + j]);
190        for (int j = 0; j < n; ++j) den += std::exp(Sfull[(size_t) r * n + j] - mx);
191        for (int d = 0; d < hd; ++d) {
192            double acc = 0;
193            for (int j = 0; j < n; ++j) acc += std::exp(Sfull[(size_t) r * n + j] - mx) / den * V[(size_t) j * ldv + d];
194            worst = std::max(worst, std::fabs(acc - O[(size_t) r * ldo + d] / l[(size_t) r]));
195        }
196    }
197    check(worst <= 2e-5, "online softmax + P*V", o.name, worst, 2e-5);
198}
199
200// the rotary of one token row and the position add against double
201static void test_rope_pos(const CpuOps& o) {
202    const int H = 3, HD = 72, D = H * HD, half = HD / 2, quarter = HD / 4;
203    std::vector<float> row((size_t) 2 * D + 16), c1(quarter), s1(quarter), c2(quarter), s2(quarter);
204    for (auto& v : row) v = rnd(-2, 2);
205    for (int j = 0; j < quarter; ++j) { c1[j] = std::cos(0.3f * j); s1[j] = std::sin(0.3f * j); c2[j] = std::cos(0.7f * j + 1); s2[j] = std::sin(0.7f * j + 1); }
206    std::vector<float> got = row;
207    o.rope_row(got.data(), (size_t) D, H, HD, c1.data(), s1.data(), c2.data(), s2.data());
208    double worst = 0;
209    for (int part = 0; part < 2; ++part)
210        for (int hh = 0; hh < H; ++hh)
211            for (int j = 0; j < half; ++j) {
212                const size_t i0 = (size_t) part * D + (size_t) hh * HD + j, i1 = i0 + half;
213                const double co = j < quarter ? c1[j] : c2[j - quarter], si = j < quarter ? s1[j] : s2[j - quarter];
214                worst = std::max(worst, std::fabs((double) row[i0] * co - (double) row[i1] * si - got[i0]));
215                worst = std::max(worst, std::fabs((double) row[i0] * si + (double) row[i1] * co - got[i1]));
216            }
217    for (size_t i = (size_t) 2 * D; i < row.size(); ++i) if (got[i] != row[i]) worst = 1e9;
218    check(worst <= 2e-6, "rope_row", o.name, worst, 2e-6);
219    const int n = 1152;
220    std::vector<float> x(n), a(n), b(n), c(n), d(n);
221    for (int i = 0; i < n; ++i) { x[i] = rnd(-1, 1); a[i] = rnd(-1, 1); b[i] = rnd(-1, 1); c[i] = rnd(-1, 1); d[i] = rnd(-1, 1); }
222    std::vector<float> y = x;
223    const float dx = 0.37f, dy = 0.81f;
224    o.pos_add(y.data(), a.data(), b.data(), c.data(), d.data(), dx, dy, n);
225    worst = 0;
226    for (int i = 0; i < n; ++i) {
227        const double want = x[i] + a[i] * (1.0 - dx) * (1.0 - dy) + b[i] * (double) dx * (1.0 - dy) + c[i] * (1.0 - dx) * dy + d[i] * (double) dx * dy;
228        worst = std::max(worst, std::fabs(want - y[i]));
229    }
230    check(worst <= 2e-6, "pos_add", o.name, worst, 2e-6);
231}
232// The SSE4.1 tier multiplies then adds in the scalar tier's k order, so on x86 its products equal the scalar tier's bit for bit (cpu_kernels_sse41.cpp)
233static void test_gemm_equals_scalar(const CpuOps& o) {
234    const CpuOps& ref = cpu_ops_scalar();
235    size_t bad = 0;
236    for (int M : {1, 7, 13, 50})
237        for (int K : {1, 72, 300})
238            for (int N : {3, 8, 21, 72})
239                for (int init = 0; init < 3; ++init) {
240                    const int lda = K + 1, ldb = (N + 63) / 64 * 64 + 64, ldc = N + 3;
241                    std::vector<float> A((size_t) M * lda), B((size_t) K * ldb + 128), C((size_t) M * ldc), bias((size_t) N);
242                    for (auto& v : A) v = rnd(-1, 1);
243                    for (auto& v : B) v = rnd(-1, 1);
244                    for (auto& v : C) v = rnd(-1, 1);
245                    for (auto& v : bias) v = rnd(-1, 1);
246                    std::vector<float> C2 = C;
247                    o.gemm(A.data(), (size_t) lda, M, B.data(), (size_t) ldb, K, N, C.data(), (size_t) ldc, init, bias.data());
248                    ref.gemm(A.data(), (size_t) lda, M, B.data(), (size_t) ldb, K, N, C2.data(), (size_t) ldc, init, bias.data());
249                    for (size_t i = 0; i < C.size(); ++i) bad += std::memcmp(&C[i], &C2[i], 4) != 0;
250                }
251    check(bad == 0, "gemm equals the scalar tier bit for bit", o.name, (double) bad, 0.0);
252}
253static void test_team() {
254    for (int nth : {1, 2, 7, 16}) {
255        Team t(nth);
256        bool ok = true;
257        for (int run = 0; run < 300 && ok; ++run) {
258            const int n = 1 + (run * 7) % 90;
259            std::vector<std::atomic<int>> hits((size_t) n);
260            for (auto& h : hits) h.store(0);
261            std::atomic<int> bad_thread{0};
262            t.run(n, [&](int task, int thr) {
263                if (thr < 0 || thr >= nth) bad_thread.fetch_add(1);
264                hits[(size_t) task].fetch_add(1);
265            });
266            if (bad_thread.load()) { ok = false; std::printf("team(%d) run %d: %d tasks saw a thread index out of range\n", nth, run, bad_thread.load()); }
267            for (size_t i = 0; i < hits.size(); ++i)
268                if (hits[i].load() != 1) { ok = false; std::printf("team(%d) run %d of %d tasks: task %zu ran %d times\n", nth, run, n, i, hits[i].load()); }
269            if (run % 50 == 0) std::this_thread::sleep_for(std::chrono::milliseconds(2));      // let the workers go to sleep once in a while
270        }
271        check(ok, "team: every task once, thread index in range", "-", 0, 0);
272    }
273}
274
275// the CPU tower's rounding default: none for every weight type (BF16 included since 11-14: code4217.png token 47 0.627 -> 1.000 against float32);
276// STRATA_VIT_REF_ROUND=1 rounds to the weight's 16-bit type, float32 / Q8_0-expanded never round
277static void test_round_choice() {
278    struct Case { int kind; const char* env; int want; };
279    const Case cs[] = {{1, nullptr, 0}, {2, nullptr, 0}, {0, nullptr, 0}, {1, "0", 0}, {2, "1", 2}, {1, "1", 1}, {2, "0", 0}, {0, "1", 0}, {1, "", 0}, {2, "", 0}};
280    bool ok = true;
281    for (const Case& c : cs) ok = ok && vit_cpu_round_kind(c.kind, c.env) == c.want;
282    check(ok, "rounding default per weight type and its override", "-", 0, 0);
283}
284
285// the GPU reader's F16 kind (vit_f16.h): which BF16 values F16 holds, the error of the others, the activation clamp and STRATA_VIT_BF16_AS_F16
286static float bf16_value(uint16_t b) { const uint32_t u = (uint32_t) b << 16; float f; std::memcpy(&f, &u, 4); return f; }
287static void test_f16_kind() {
288    // named cases: 1.0, -1.0, 2^-14, 2^-17 with all mantissa bits, 2^-18 with the lowest bit (not) and the second lowest (yes), 2^-24, 2^-25,
289    // 65,280 (the largest BF16 value under 65,504), 65,536, 2^20, +-0, inf, -inf, nan, a BF16 subnormal, -(2^-18 + lowest bit)
290    struct Case { uint16_t b; bool exact; };
291    const Case cs[] = {{0x3F80, true}, {0xBF80, true}, {0x3880, true}, {0x377F, true}, {0x3681, false}, {0x3682, true}, {0x3380, true}, {0x3300, false},
292                       {0x477F, true}, {0x4780, false}, {0x4980, false}, {0x0000, true}, {0x8000, true}, {0x7F80, false}, {0xFF80, false}, {0x7FC0, false},
293                       {0x0001, false}, {0xB681, false}};
294    bool ok = true;
295    for (const Case& c : cs) if (bf16_exact_in_f16(c.b) != c.exact) { ok = false; std::printf("  bf16 0x%04x: exact %d, want %d\n", c.b, (int) !c.exact, (int) c.exact); }
296    check(ok, "F16 kind: named BF16 values", "-", 0, 0);
297    // all 65,536 BF16 values against an independent rule in double precision: finite, |v| <= 65504 and a multiple of F16's step at that size
298    // (2^-24 under 2^-14, else 2^(floor(log2 |v|) - 10)); the error of the others in range is the distance to the nearest step multiple (ties to even)
299    int bad = 0, bad_err = 0;
300    double worst_err = 0.0;
301    for (uint32_t b = 0; b < 65536; ++b) {
302        const double v = bf16_value((uint16_t) b), a = std::fabs(v);
303        const bool fin = std::isfinite(v);
304        bool want = false;
305        double want_err = 0.0;
306        if (fin && a <= 65504.0) {
307            int ex = 0;
308            (void) std::frexp(a, &ex);   // a = f * 2^ex, f in [0.5, 1)
309            const double step = a < std::ldexp(1.0, -14) ? std::ldexp(1.0, -24) : std::ldexp(1.0, ex - 1 - 10);
310            const double q = a / step, r = std::nearbyint(q);
311            want = a == 0.0 || q == r;
312            want_err = want ? 0.0 : std::fabs(q - r) * step;
313        }
314        if (bf16_exact_in_f16((uint16_t) b) != want) ++bad;
315        const double got_err = bf16_f16_abs_error((uint16_t) b);
316        if (got_err != want_err) ++bad_err;
317        if (got_err > worst_err) worst_err = got_err;
318    }
319    check(bad == 0, "F16 kind: every BF16 value against the double-precision rule", "-", (double) bad, 0.0);
320    check(bad_err == 0 && worst_err == std::ldexp(1.0, -25), "F16 kind: rounding error of the values under 2^-14 (largest 2^-25)", "-", worst_err, std::ldexp(1.0, -25));
321    // a matrix: counts, and it stays BF16 only for a weight beyond F16's range
322    const uint16_t w1[] = {0x3F80, 0x3681, 0x3300, 0x377F, 0x0001};
323    const uint16_t w2[] = {0x3F80, 0x4780, 0x7FC0, 0x3681};
324    const Bf16F16Count c1 = bf16_count_f16(w1, 5), c2 = bf16_count_f16(w2, 4);
325    check(c1.not_exact == 3 && c1.overflow == 0 && c1.max_abs_error == std::ldexp(1.0, -25) && bf16_matrix_fits_f16(c1.overflow) &&
326          c2.not_exact == 3 && c2.overflow == 2 && !bf16_matrix_fits_f16(c2.overflow), "F16 kind: matrix counts and the BF16 fallback", "-", 0, 0);
327    // the overflow count the load uses (no trace) equals the full count's over all 65,536 values: exponents 143-255, both signs
328    std::vector<uint16_t> all(65536);
329    for (uint32_t b = 0; b < 65536; ++b) all[b] = (uint16_t) b;
330    const Bf16F16Count ca = bf16_count_f16(all.data(), 65536);
331    const long long nb = bf16_count_beyond_f16(all.data(), 65536);
332    check(nb == ca.overflow && nb == 113LL * 256 && !bf16_beyond_f16(0x477F) && bf16_beyond_f16(0x4780) && bf16_beyond_f16(0xFFC0),
333          "F16 kind: the overflow count of the load", "-", (double) nb, (double) ca.overflow);
334    // the activation clamp: F16's finite range, NaN stays NaN
335    const float big = 70000.0f, inf = std::numeric_limits<float>::infinity();
336    check(f16_clamp(big) == 65504.0f && f16_clamp(-big) == -65504.0f && f16_clamp(inf) == 65504.0f && f16_clamp(-inf) == -65504.0f &&
337          f16_clamp(65504.0f) == 65504.0f && f16_clamp(-1.5f) == -1.5f && f16_clamp(0.0f) == 0.0f && std::isnan(f16_clamp(std::nanf(""))),
338          "F16 kind: activation clamp to +-65,504", "-", 0, 0);
339    // STRATA_VIT_BF16_AS_F16: unset = the card's default; 0 / empty / not a number = as stored; 2 = F16 inputs; any other number = the BF16-rounded kind
340    struct M { const char* env; int dflt; int want; };
341    const M ms[] = {{nullptr, BF16_TO_F16, BF16_TO_F16}, {nullptr, BF16_ROUNDED, BF16_ROUNDED}, {"0", BF16_TO_F16, BF16_STORED}, {"0", BF16_ROUNDED, BF16_STORED},
342                    {"", BF16_TO_F16, BF16_STORED}, {"x", BF16_TO_F16, BF16_STORED}, {"1", BF16_TO_F16, BF16_ROUNDED}, {"1", BF16_ROUNDED, BF16_ROUNDED},
343                    {"2", BF16_ROUNDED, BF16_TO_F16}, {"2", BF16_TO_F16, BF16_TO_F16}, {"3", BF16_TO_F16, BF16_ROUNDED}, {"-1", BF16_TO_F16, BF16_ROUNDED},
344                    {" 2", BF16_STORED, BF16_TO_F16}, {"12", BF16_TO_F16, BF16_ROUNDED}};
345    bool mok = true;
346    for (const M& c : ms) if (vit_gpu_bf16_mode(c.env, c.dflt) != c.want) { mok = false; std::printf("  env '%s' default %d: %d, want %d\n", c.env ? c.env : "(unset)", c.dflt, vit_gpu_bf16_mode(c.env, c.dflt), c.want); }
347    check(mok, "F16 kind: STRATA_VIT_BF16_AS_F16 values", "-", 0, 0);
348}
349
350int main(int argc, char** argv) {
351    if (argc > 1 && std::strcmp(argv[1], "team") == 0) {     // only the fork-join team (a loop under ThreadSanitizer)
352        const int reps = argc > 2 ? std::atoi(argv[2]) : 1;
353        for (int i = 0; i < reps; ++i) test_team();
354        std::printf(g_fail ? "%d FAILED\n" : "team: all passed\n", g_fail);
355        return g_fail ? 1 : 0;
356    }
357    const quant::Caps& c = quant::caps();
358    std::printf("cpu: sse41 %d avx %d avx2 %d fma %d f16c %d avx512 %d; best tier %s\n", c.sse41, c.avx, c.avx2, c.fma, c.f16c, c.avx512,
359                quant::isa_name(quant::best_isa()));
360    // every tier this CPU runs, capped by STRATA_FORCE_ISA as in the tower (an emulated older CPU never reaches a wider tier); NEON on Arm
361    const int cap = (int) quant::best_isa();
362    std::vector<const CpuOps*> tiers = {&cpu_ops_scalar()};
363    if (cap >= (int) quant::Isa::Sse41 && cap != (int) quant::Isa::Neon && quant::isa_ok(quant::Isa::Sse41)) tiers.push_back(&cpu_ops_sse41());
364    if (cap >= (int) quant::Isa::Avx && cap != (int) quant::Isa::Neon && quant::isa_ok(quant::Isa::Avx)) tiers.push_back(&cpu_ops_avx());
365    if (cap >= (int) quant::Isa::Avx2 && cap != (int) quant::Isa::Neon && quant::isa_ok(quant::Isa::Avx2)) tiers.push_back(&cpu_ops_avx2());
366    if (cap == (int) quant::Isa::Avx512 && quant::isa_ok(quant::Isa::Avx512)) tiers.push_back(&cpu_ops_avx512());
367    if (quant::isa_ok(quant::Isa::Neon)) tiers.push_back(&cpu_ops_neon());
368    for (const CpuOps* o : tiers) {
369        std::printf("tier %s (tile %d x %d)\n", o->name, o->MR, o->NR);
370        test_gemm(*o);
371        test_pack_values(*o);
372        test_conv(*o);
373        test_round(*o);
374        test_gelu(*o);
375        test_layernorm(*o);
376        test_add(*o);
377        test_attention(*o);
378        test_rope_pos(*o);
379#if defined(__x86_64__) || defined(_M_X64)
380        if (std::strcmp(o->name, "sse41") == 0) test_gemm_equals_scalar(*o);
381#endif
382    }
383    test_team();
384    test_round_choice();
385    test_f16_kind();
386    std::printf(g_fail ? "%d FAILED\n" : "all passed\n", g_fail);
387    return g_fail ? 1 : 0;
388}