okami.mom/strata

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

Niko1221Merge f-q36-testpad 9f0056e41 into f-p4-intf2b41fd29

main
19.0 KiB353 linesraw
1// src/prefill/mmq_nvfp4_parity.cpp - the prompt path's NVFP4 expert products (MMQ) against an exact reference.
2//
3//     STRATA_PREFILL_NVFP4=w4a8|w4a4 mmq_nvfp4_parity pack/experts.bin [rows ...]
4//     STRATA_PREFILL_NVFP4=w4a8|w4a4 mmq_nvfp4_parity pack/experts.bin --group
5//     STRATA_PREFILL_NVFP4=w4a8|w4a4 mmq_nvfp4_parity pack/experts.bin --layers [expert]
6//     STRATA_PREFILL_NVFP4=w4a8|w4a4 mmq_nvfp4_parity pack/experts.bin --real moe_input.bin layer
7//
8// --group: the prompt path's launch shape - 20 experts of layer 0 with 0..60 rows each, one activation array in
9// expert order, gate/up in two launches (experts 0-15, then 16-19 through the same absolute bounds, 16 in), and the
10// second group's down from its own rows through relative bounds.
11// --layers: one expert (default 7) of every one of the 48 layers, 64 rows - scales differ by layer.
12// --real: a layer's MoE input from a prompt (STRATA_DUMP_MOE_INPUT) through its 12 most-routed experts, gate/up on
13// the real rows and down on the hidden they produce; also an FP16-activation product (the fp16 path) and the rows'
14// outlier ratio (max |x| / rms x).
15//
16// One real expert (layer 0, expert 0): gate/up [1280, 2560] and down [2560, 640], as the prompt path gathers them.
17// Activations are random rows; the reference dequantizes the weights with ggml's own NVFP4 decode and sums in double.
18// Reported per product: relative Frobenius error and the worst element relative to the row's largest output.
19// 8-bit activations land near 0.5%; FP4 activations near 10% - the difference between the two modes.
20#include "strata/prefill/mmq_ids_margin.hpp"
21#include "strata/prefill/moe_mmq.hpp"
22
23#include "ggml.h"
24
25#include <cuda_runtime.h>
26#include <immintrin.h>
27
28#include <algorithm>
29#include <cmath>
30#include <cstdio>
31#include <cstdlib>
32#include <cstring>
33#include <fstream>
34#include <string>
35#include <random>
36#include <vector>
37
38namespace mmq = strata::prefill::mmq;
39
40namespace {
41
42void ck(cudaError_t e, const char* what) {
43    if (e != cudaSuccess) { std::fprintf(stderr, "%s: %s\n", what, cudaGetErrorString(e)); std::exit(1); }
44}
45
46// one product: x [T, cols] times w [rows, cols]^T
47float half_round(float v) { return _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtps_ph(_mm_set_ss(v), 0))); }
48
49int product(const uint8_t* w_host, int64_t rows, int64_t cols, int T, const char* what, std::mt19937& rng,
50            const std::vector<float>* given = nullptr, double* rel_out = nullptr, double* rel16_out = nullptr,
51            std::vector<double>* ref_out = nullptr) {
52    const size_t wbytes = mmq::matrix_bytes(GGML_TYPE_NVFP4, rows, cols);
53    std::vector<float> wf((size_t) rows * cols);
54    ggml_get_type_traits(GGML_TYPE_NVFP4)->to_float(w_host, wf.data(), rows * cols);
55    std::vector<float> x((size_t) T * cols);
56    if (given) {
57        x = *given;
58    } else {
59        std::normal_distribution<float> nd(0.f, 1.f);
60        std::uniform_real_distribution<float> mag(0.1f, 10.f);
61        for (int t = 0; t < T; ++t) {
62            const float m = mag(rng);                                 // rows of different magnitude, like a residual
63            for (int64_t k = 0; k < cols; ++k) x[(size_t) t * cols + k] = nd(rng) * m;
64        }
65    }
66    std::vector<double> ref((size_t) T * rows);
67    for (int t = 0; t < T; ++t)
68        for (int64_t r = 0; r < rows; ++r) {
69            double s = 0;
70            const float* a = x.data() + (size_t) t * cols;
71            const float* b = wf.data() + (size_t) r * cols;
72            for (int64_t k = 0; k < cols; ++k) s += (double) a[k] * b[k];
73            ref[(size_t) t * rows + r] = s;
74        }
75
76    uint8_t* dw = nullptr; float* dx = nullptr; void* dq = nullptr; float* dys = nullptr; float* dd = nullptr;
77    int32_t* dids = nullptr; int32_t* dbounds = nullptr;
78    ck(cudaMalloc(&dw, wbytes + 4096), "w");
79    ck(cudaMemset(dw, 0, wbytes + 4096), "w");
80    ck(cudaMemcpy(dw, w_host, wbytes, cudaMemcpyHostToDevice), "w");
81    ck(cudaMalloc(&dx, x.size() * sizeof(float)), "x");
82    ck(cudaMemcpy(dx, x.data(), x.size() * sizeof(float), cudaMemcpyHostToDevice), "x");
83    ck(cudaMalloc(&dq, mmq::q8_bytes(T, cols)), "xq");
84    ck(cudaMalloc(&dys, T * sizeof(float)), "yscale");
85    ck(cudaMalloc(&dd, (size_t) T * rows * sizeof(float)), "dst");
86    const size_t ids_rows = (size_t) mmq::mmq_ids_table_rows((uint64_t) T, (uint64_t) T);   // (mul_mat_q reads the row table past its last row: mmq_ids_margin.hpp)
87    ck(cudaMalloc(&dids, ids_rows * sizeof(int32_t)), "ids");
88    ck(cudaMalloc(&dbounds, 2 * sizeof(int32_t)), "bounds");
89    const int32_t bounds[2] = {0, T};
90    ck(cudaMemcpy(dbounds, bounds, sizeof bounds, cudaMemcpyHostToDevice), "bounds");
91    mmq::iota(dids, (int64_t) ids_rows, nullptr);
92    mmq::quantize(dx, nullptr, dq, GGML_TYPE_NVFP4, cols, cols, T, nullptr, dys);
93    mmq::Context ctx;
94    mmq::Product p;
95    p.w = dw; p.type = GGML_TYPE_NVFP4; p.w_rows = rows; p.w_cols = cols; p.expert_bytes = wbytes; p.n = 1;
96    p.xq = dq; p.bounds = dbounds; p.ids = dids; p.total_rows = T; p.max_rows = T; p.dst = dd; p.ld_dst = rows;
97    p.y_scale = mmq::fp4_activations(GGML_TYPE_NVFP4) ? dys : nullptr;
98    ctx.run(p, nullptr);
99    ck(cudaDeviceSynchronize(), "run");
100    std::vector<float> got((size_t) T * rows);
101    ck(cudaMemcpy(got.data(), dd, got.size() * sizeof(float), cudaMemcpyDeviceToHost), "dst");
102    cudaFree(dw); cudaFree(dx); cudaFree(dq); cudaFree(dys); cudaFree(dd); cudaFree(dids); cudaFree(dbounds);
103
104    double num = 0, den = 0, worst = 0;
105    for (int t = 0; t < T; ++t) {
106        double rmax = 0;
107        for (int64_t r = 0; r < rows; ++r) rmax = std::fmax(rmax, std::fabs(ref[(size_t) t * rows + r]));
108        for (int64_t r = 0; r < rows; ++r) {
109            const double e = (double) got[(size_t) t * rows + r] - ref[(size_t) t * rows + r];
110            num += e * e;
111            den += ref[(size_t) t * rows + r] * ref[(size_t) t * rows + r];
112            worst = std::fmax(worst, std::fabs(e) / (rmax > 0 ? rmax : 1));
113        }
114    }
115    const double rel = std::sqrt(num / den);
116    if (rel_out) *rel_out = rel;
117    if (ref_out) *ref_out = ref;
118    if (rel16_out) {                                                  // the fp16 path: activations rounded to FP16
119        double n16 = 0;
120        for (int t = 0; t < T; ++t) {
121            std::vector<float> xh(cols);
122            for (int64_t k = 0; k < cols; ++k) xh[k] = half_round(x[(size_t) t * cols + k]);
123            for (int64_t r = 0; r < rows; ++r) {
124                double s = 0;
125                for (int64_t k = 0; k < cols; ++k) s += (double) xh[k] * wf[(size_t) r * cols + k];
126                const double e = s - ref[(size_t) t * rows + r];
127                n16 += e * e;
128            }
129        }
130        *rel16_out = std::sqrt(n16 / den);
131        return rel > 0.2 ? 1 : 0;
132    }
133    std::printf("%-8s T %4d  [%lld x %lld]  rel. error %.4f%%  worst %.4f%% of the row max%s\n", what, T,
134                (long long) rows, (long long) cols, 100 * rel, 100 * worst, rel > 0.05 ? "  <-- FP4-level or wrong" : "");
135    return rel > 0.2 ? 1 : 0;
136}
137
138double dot_ref(const float* a, const float* b, int64_t n) {
139    double s = 0;
140    for (int64_t k = 0; k < n; ++k) s += (double) a[k] * b[k];
141    return s;
142}
143
144struct Err {
145    double num = 0, den = 0;
146    void add(double got, double ref) { num += (got - ref) * (got - ref); den += ref * ref; }
147    double rel() const { return den > 0 ? std::sqrt(num / den) : 0; }
148};
149
150int group(const char* path) {
151    constexpr int64_t N = 2560, FF = 640;
152    constexpr int E = 20;
153    constexpr size_t STRIDE = 2764816;                                 // one layer-0 blob in experts.bin
154    const size_t gub = mmq::matrix_bytes(GGML_TYPE_NVFP4, 2 * FF, N), db = mmq::matrix_bytes(GGML_TYPE_NVFP4, N, FF);
155    std::vector<uint8_t> all((size_t) E * STRIDE);
156    std::ifstream f(path, std::ios::binary);
157    if (!f.read((char*) all.data(), (std::streamsize) all.size())) { std::fprintf(stderr, "cannot read %s\n", path); return 2; }
158    std::vector<uint8_t> G((size_t) E * gub + 4096, 0), D((size_t) E * db + 4096, 0);
159    for (int e = 0; e < E; ++e) {
160        std::memcpy(G.data() + e * gub, all.data() + e * STRIDE, gub);
161        std::memcpy(D.data() + e * db, all.data() + e * STRIDE + gub, db);
162    }
163    std::mt19937 rng(5);
164    std::uniform_int_distribution<int> cntd(0, 60);
165    std::vector<int32_t> bounds(E + 1, 0);
166    int maxA = 0, maxB = 0;
167    for (int e = 0; e < E; ++e) {
168        const int c = (e % 7 == 3) ? 0 : cntd(rng);
169        bounds[e + 1] = bounds[e] + c;
170        if (e < 16) maxA = std::max(maxA, c); else maxB = std::max(maxB, c);
171    }
172    const int T = bounds[E];
173    std::normal_distribution<float> nd(0.f, 1.f);
174    std::uniform_real_distribution<float> mag(0.1f, 10.f);
175    std::vector<float> x((size_t) T * N);
176    for (int t = 0; t < T; ++t) { const float m = mag(rng); for (int64_t k = 0; k < N; ++k) x[(size_t) t * N + k] = nd(rng) * m; }
177    // group B's down input: its own rows of FF values
178    const int rB = bounds[16], nB = T - rB;
179    std::vector<float> h((size_t) std::max(nB, 1) * FF);
180    for (auto& v : h) v = nd(rng);
181    std::vector<int32_t> relB(5);
182    for (int i = 0; i <= 4; ++i) relB[i] = bounds[16 + i] - rB;
183
184    uint8_t *dG, *dD; float *dx, *dh, *dgu, *ddn, *dys, *dhs; void *dxq, *dhq; int32_t *dids, *dbounds, *drel;
185    ck(cudaMalloc(&dG, G.size()), "G"); ck(cudaMemcpy(dG, G.data(), G.size(), cudaMemcpyHostToDevice), "G");
186    ck(cudaMalloc(&dD, D.size()), "D"); ck(cudaMemcpy(dD, D.data(), D.size(), cudaMemcpyHostToDevice), "D");
187    ck(cudaMalloc(&dx, x.size() * 4), "x"); ck(cudaMemcpy(dx, x.data(), x.size() * 4, cudaMemcpyHostToDevice), "x");
188    ck(cudaMalloc(&dh, h.size() * 4), "h"); ck(cudaMemcpy(dh, h.data(), h.size() * 4, cudaMemcpyHostToDevice), "h");
189    ck(cudaMalloc(&dgu, (size_t) T * 2 * FF * 4), "gu"); ck(cudaMemset(dgu, 0, (size_t) T * 2 * FF * 4), "gu");
190    ck(cudaMalloc(&ddn, (size_t) std::max(nB, 1) * N * 4), "dn");
191    ck(cudaMalloc(&dxq, mmq::q8_bytes(T, N)), "xq"); ck(cudaMalloc(&dhq, mmq::q8_bytes(std::max(nB, 1), FF)), "hq");
192    ck(cudaMalloc(&dys, T * 4), "ys"); ck(cudaMalloc(&dhs, std::max(nB, 1) * 4), "hs");
193    const size_t ids_rows = (size_t) mmq::mmq_ids_table_rows((uint64_t) T, (uint64_t) T);   // (mul_mat_q reads the row table past its last row: mmq_ids_margin.hpp)
194    ck(cudaMalloc(&dids, ids_rows * 4), "ids"); ck(cudaMalloc(&dbounds, (E + 1) * 4), "b"); ck(cudaMalloc(&drel, 5 * 4), "rel");
195    ck(cudaMemcpy(dbounds, bounds.data(), (E + 1) * 4, cudaMemcpyHostToDevice), "b");
196    ck(cudaMemcpy(drel, relB.data(), 5 * 4, cudaMemcpyHostToDevice), "rel");
197    mmq::iota(dids, (int64_t) ids_rows, nullptr);
198    const bool fp4 = mmq::fp4_activations(GGML_TYPE_NVFP4);
199    mmq::quantize(dx, nullptr, dxq, GGML_TYPE_NVFP4, N, N, T, nullptr, dys);
200    mmq::Context ctx;
201    for (int grp = 0; grp < 2; ++grp) {
202        mmq::Product p;
203        p.w = dG + (grp ? 16 * gub : 0); p.type = GGML_TYPE_NVFP4; p.w_rows = 2 * FF; p.w_cols = N; p.expert_bytes = gub;
204        p.n = grp ? 4 : 16; p.xq = dxq; p.bounds = dbounds + (grp ? 16 : 0); p.ids = dids; p.total_rows = T;
205        p.max_rows = grp ? maxB : maxA; p.dst = dgu; p.ld_dst = 2 * FF; p.y_scale = fp4 ? dys : nullptr;
206        ctx.run(p, nullptr);
207    }
208    mmq::quantize(dh, nullptr, dhq, GGML_TYPE_NVFP4, FF, FF, nB, nullptr, dhs);
209    {
210        mmq::Product p;
211        p.w = dD + 16 * db; p.type = GGML_TYPE_NVFP4; p.w_rows = N; p.w_cols = FF; p.expert_bytes = db; p.n = 4;
212        p.xq = dhq; p.bounds = drel; p.ids = dids; p.total_rows = nB; p.max_rows = maxB; p.dst = ddn; p.ld_dst = N;
213        p.y_scale = fp4 ? dhs : nullptr;
214        ctx.run(p, nullptr);
215    }
216    ck(cudaDeviceSynchronize(), "run");
217    std::vector<float> gu((size_t) T * 2 * FF), dn((size_t) std::max(nB, 1) * N);
218    ck(cudaMemcpy(gu.data(), dgu, gu.size() * 4, cudaMemcpyDeviceToHost), "gu");
219    ck(cudaMemcpy(dn.data(), ddn, dn.size() * 4, cudaMemcpyDeviceToHost), "dn");
220    cudaFree(dG); cudaFree(dD); cudaFree(dx); cudaFree(dh); cudaFree(dgu); cudaFree(ddn); cudaFree(dxq); cudaFree(dhq);
221    cudaFree(dys); cudaFree(dhs); cudaFree(dids); cudaFree(dbounds); cudaFree(drel);
222
223    Err ea, eb, ed;
224    std::vector<float> wf((size_t) 2 * FF * N), wd((size_t) N * FF);
225    for (int e = 0; e < E; ++e) {
226        ggml_get_type_traits(GGML_TYPE_NVFP4)->to_float(G.data() + e * gub, wf.data(), 2 * FF * N);
227        for (int t = bounds[e]; t < bounds[e + 1]; ++t)
228            for (int64_t r = 0; r < 2 * FF; ++r)
229                (e < 16 ? ea : eb).add(gu[(size_t) t * 2 * FF + r], dot_ref(x.data() + (size_t) t * N, wf.data() + r * N, N));
230        if (e >= 16) {
231            ggml_get_type_traits(GGML_TYPE_NVFP4)->to_float(D.data() + e * db, wd.data(), N * FF);
232            for (int t = bounds[e]; t < bounds[e + 1]; ++t)
233                for (int64_t r = 0; r < N; ++r)
234                    ed.add(dn[(size_t) (t - rB) * N + r], dot_ref(h.data() + (size_t) (t - rB) * FF, wd.data() + r * FF, FF));
235        }
236    }
237    std::printf("group: %d rows over %d experts (max %d / %d per expert)\n", T, E, maxA, maxB);
238    std::printf("  gate/up, experts 0-15 (bounds from 0)      rel. error %.4f%%\n", 100 * ea.rel());
239    std::printf("  gate/up, experts 16-19 (bounds from 16)    rel. error %.4f%%\n", 100 * eb.rel());
240    std::printf("  down, experts 16-19 (relative bounds)      rel. error %.4f%%\n", 100 * ed.rel());
241    const double worst = std::max(ea.rel(), std::max(eb.rel(), ed.rel()));
242    std::printf("RESULT: %s\n", worst > 0.2 ? "WRONG (error beyond 20%)" : "ok");
243    return worst > 0.2 ? 1 : 0;
244}
245
246int layers(const char* path, int e) {
247    constexpr int64_t N = 2560, FF = 640;
248    constexpr size_t STRIDE = 2764816, LAYER = 512 * STRIDE;
249    const size_t gub = mmq::matrix_bytes(GGML_TYPE_NVFP4, 2 * FF, N), db = mmq::matrix_bytes(GGML_TYPE_NVFP4, N, FF);
250    std::vector<uint8_t> blob(gub + db);
251    std::ifstream f(path, std::ios::binary);
252    std::mt19937 rng(3);
253    int bad = 0;
254    for (int l = 0; l < 48; ++l) {
255        f.seekg((std::streamoff) (l * LAYER + (size_t) e * STRIDE));
256        if (!f.read((char*) blob.data(), (std::streamsize) blob.size())) { std::fprintf(stderr, "cannot read layer %d\n", l); return 2; }
257        char name[32];
258        std::snprintf(name, sizeof name, "L%02d gu", l);
259        bad += product(blob.data(), 2 * FF, N, 64, name, rng);
260        std::snprintf(name, sizeof name, "L%02d dn", l);
261        bad += product(blob.data() + gub, N, FF, 64, name, rng);
262    }
263    std::printf("RESULT: %s\n", bad ? "WRONG (error beyond 20%)" : "ok");
264    return bad ? 1 : 0;
265}
266
267int real(const char* experts_bin, const char* dump, int layer) {
268    std::ifstream d(dump, std::ios::binary);
269    int64_t hdr[3];
270    if (!d.read((char*) hdr, sizeof hdr)) { std::fprintf(stderr, "cannot read %s\n", dump); return 2; }
271    const int64_t T = hdr[0], N = hdr[1], K = hdr[2], FF = 640;
272    std::vector<float> x((size_t) (T * N));
273    std::vector<int32_t> ids((size_t) (T * K));
274    d.read((char*) x.data(), (std::streamsize) (x.size() * 4));
275    d.read((char*) ids.data(), (std::streamsize) (ids.size() * 4));
276    // the rows' outlier ratio
277    std::vector<double> ratio;
278    for (int64_t t = 0; t < T; ++t) {
279        double mx = 0, ss = 0;
280        for (int64_t k = 0; k < N; ++k) { const double v = x[(size_t) (t * N + k)]; mx = std::fmax(mx, std::fabs(v)); ss += v * v; }
281        ratio.push_back(mx / std::sqrt(ss / N + 1e-30));
282    }
283    std::sort(ratio.begin(), ratio.end());
284    std::printf("layer %d: %lld tokens, max|x| / rms: median %.1f, p99 %.1f, max %.1f\n", layer, (long long) T,
285                ratio[ratio.size() / 2], ratio[ratio.size() * 99 / 100], ratio.back());
286    std::vector<int> cnt(512, 0);
287    for (int32_t e : ids) if (e >= 0 && e < 512) ++cnt[e];
288    std::vector<int> order(512);
289    for (int i = 0; i < 512; ++i) order[i] = i;
290    std::sort(order.begin(), order.end(), [&](int a, int b) { return cnt[a] > cnt[b]; });
291    constexpr size_t STRIDE = 2764816, LAYER = 512 * STRIDE;
292    const size_t gub = mmq::matrix_bytes(GGML_TYPE_NVFP4, 2 * FF, N);
293    std::ifstream f(experts_bin, std::ios::binary);
294    std::vector<uint8_t> blob(STRIDE);
295    std::mt19937 rng(1);
296    double s_gu = 0, s_gu16 = 0, s_dn = 0, s_dn16 = 0;
297    int n = 0;
298    std::printf("%6s %5s  %10s %10s  %10s %10s\n", "expert", "rows", "gu mmq", "gu fp16", "down mmq", "down fp16");
299    for (int i = 0; i < 12; ++i) {
300        const int e = order[i];
301        f.seekg((std::streamoff) (layer * LAYER + (size_t) e * STRIDE));
302        f.read((char*) blob.data(), (std::streamsize) blob.size());
303        float tail[4];
304        std::memcpy(tail, blob.data() + STRIDE - 16, sizeof tail);
305        std::vector<float> xe;
306        for (int64_t t = 0; t < T; ++t)
307            for (int64_t k = 0; k < K; ++k)
308                if (ids[(size_t) (t * K + k)] == e) xe.insert(xe.end(), x.begin() + t * N, x.begin() + (t + 1) * N);
309        const int Te = (int) (xe.size() / N);
310        double gu = 0, gu16 = 0, dn = 0, dn16 = 0;
311        std::vector<double> ref;
312        product(blob.data(), 2 * FF, N, Te, "gu", rng, &xe, &gu, &gu16, &ref);
313        std::vector<float> h((size_t) Te * FF);                      // the hidden the exact gate/up gives
314        for (int t = 0; t < Te; ++t)
315            for (int64_t r = 0; r < FF; ++r) {
316                const double g = ref[(size_t) t * 2 * FF + r] * tail[0], u = ref[(size_t) t * 2 * FF + FF + r] * tail[1];
317                h[(size_t) t * FF + r] = (float) (g / (1 + std::exp(-g)) * u);
318            }
319        product(blob.data() + gub, N, FF, Te, "down", rng, &h, &dn, &dn16);
320        std::printf("%6d %5d  %9.3f%% %9.3f%%  %9.3f%% %9.3f%%\n", e, Te, 100 * gu, 100 * gu16, 100 * dn, 100 * dn16);
321        s_gu += gu; s_gu16 += gu16; s_dn += dn; s_dn16 += dn16; ++n;
322    }
323    std::printf("%6s %5s  %9.3f%% %9.3f%%  %9.3f%% %9.3f%%\n", "mean", "", 100 * s_gu / n, 100 * s_gu16 / n,
324                100 * s_dn / n, 100 * s_dn16 / n);
325    return 0;
326}
327
328}  // namespace
329
330int main(int argc, char** argv) {
331    if (argc < 2) { std::fprintf(stderr, "usage: mmq_nvfp4_parity pack/experts.bin [rows ...]\n"); return 2; }
332    constexpr int64_t N = 2560, FF = 640;
333    const size_t gu_bytes = mmq::matrix_bytes(GGML_TYPE_NVFP4, 2 * FF, N), d_bytes = mmq::matrix_bytes(GGML_TYPE_NVFP4, N, FF);
334    std::vector<uint8_t> blob(gu_bytes + d_bytes);
335    std::ifstream f(argv[1], std::ios::binary);
336    if (!f.read((char*) blob.data(), (std::streamsize) blob.size())) { std::fprintf(stderr, "cannot read %s\n", argv[1]); return 2; }
337    if (argc >= 3 && std::string(argv[2]) == "--group") return group(argv[1]);
338    if (argc >= 5 && std::string(argv[2]) == "--real") return real(argv[1], argv[3], std::atoi(argv[4]));
339    if (argc >= 3 && std::string(argv[2]) == "--layers") return layers(argv[1], argc >= 4 ? std::atoi(argv[3]) : 7);
340    const char* mode = std::getenv("STRATA_PREFILL_NVFP4");
341    std::printf("mode %s, FP4 activations: %s\n", mode ? mode : "default", mmq::fp4_activations(GGML_TYPE_NVFP4) ? "yes" : "no");
342    std::vector<int> Ts;
343    for (int i = 2; i < argc; ++i) Ts.push_back(std::atoi(argv[i]));
344    if (Ts.empty()) Ts = {1, 7, 37, 128, 300};
345    std::mt19937 rng(11);
346    int bad = 0;
347    for (int T : Ts) {
348        bad += product(blob.data(), 2 * FF, N, T, "gate/up", rng);
349        bad += product(blob.data() + gu_bytes, N, FF, T, "down", rng);
350    }
351    std::printf("RESULT: %s\n", bad ? "WRONG (error beyond 20%)" : "ok");
352    return bad ? 1 : 0;
353}