okami.mom/strata

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

Niko1221Merge branch 'f-138-fixB' into f-138-prs09817da39

main
20.1 KiB424 linesraw
1// src/kernels/decode_cluster_parity.cpp - S19: the decode kernels on thread-block clusters (sm_90+) against the ones
2// they replace, bit for bit (GPU, synthetic, no model).
3//
4//   QSA top-k : qsa_block_topk_cluster vs qsa_block_topk's one-CTA dispatch (the register kernel up to 33,792 blocks,
5//               block_topk_kernel above) and vs qsa_block_topk_ref - every id of every row of the `cap`-wide buffer,
6//               including the entries past the width that neither may write.
7//   argmax    : sample_greedy_cluster vs sample_tokens' one-block greedy kernel.
8//
9// Inputs: random scores like a real indexer's, and adversarial ones - all equal, a few distinct values (ties at the
10// threshold across the CTAs of a cluster), NaN, +-inf, -0.0 against +0.0, denormals, monotone rows, all NaN; contexts
11// from below the selection width (the identity) to 262,144 cells, every tail weight (n_kv mod 4), 1-16 queries; and
12// one CUDA graph that captures both cluster launches, replayed with new inputs.
13//
14//   decode_cluster_parity --selftest     parity (exit 0 = identical everywhere); skipped (exit 0) below sm_90
15//   decode_cluster_parity --bench        per-call times, old vs new, at 32K / 128K / 262K
16//
17// The old paths are reached through the public dispatchers with STRATA_QSA_CLUSTER=0 / STRATA_ARGMAX_MULTI=0, set
18// here before their first call (they read the variables once).
19#include "strata/kernels/qsa.hpp"
20#include "strata/kernels/qsa_select.hpp"
21#include "strata/kernels/sampler.hpp"
22
23#include <cuda_runtime.h>
24
25#include <cmath>
26#include <cstdint>
27#include <cstdio>
28#include <cstdlib>
29#include <cstring>
30#include <limits>
31#include <random>
32#include <string>
33#include <vector>
34
35namespace k = strata::kernels;
36
37namespace {
38
39void ck(cudaError_t e, const char* what) {
40    if (e != cudaSuccess) {
41        std::fprintf(stderr, "%s: %s\n", what, cudaGetErrorString(e));
42        std::exit(2);
43    }
44}
45
46void set_env(const char* name, const char* value) {
47#if defined(_WIN32)
48    _putenv_s(name, value);
49#else
50    setenv(name, value, 1);
51#endif
52}
53
54template <typename T> struct Dev {
55    T* p = nullptr;
56    size_t n = 0;
57    explicit Dev(size_t count) : n(count) { ck(cudaMalloc(&p, count * sizeof(T) + 64), "cudaMalloc"); }
58    ~Dev() { cudaFree(p); }
59    void put(const std::vector<T>& h) {
60        ck(cudaMemcpy(p, h.data(), h.size() * sizeof(T), cudaMemcpyHostToDevice), "up");
61        // #548 #536: a pageable cudaMemcpy can return before its DMA lands, and the graph replays and the timing
62        // cases run on a cudaStreamNonBlocking stream that does not wait for the legacy stream - so wait here
63        ck(cudaDeviceSynchronize(), "up landed");
64    }
65    std::vector<T> get() const {
66        std::vector<T> h(n);
67        ck(cudaMemcpy(h.data(), p, n * sizeof(T), cudaMemcpyDeviceToHost), "down");
68        return h;
69    }
70};
71
72const float kNaN = std::numeric_limits<float>::quiet_NaN(), kInf = std::numeric_limits<float>::infinity();
73
74// ---- QSA top-k fixtures
75enum Pattern { kRandom, kAllEqual, kFewValues, kNaNs, kInfs, kSignedZero, kDenormal, kAscending, kDescending, kAllNaN,
76               kQuantized, kPatterns };
77const char* pattern_name(int p) {
78    static const char* n[] = {"random", "all-equal", "few-values", "nan", "inf", "signed-zero", "denormal", "ascending",
79                              "descending", "all-nan", "quantized"};
80    return n[p];
81}
82
83// one query's row of block scores (blocks 0..n_bid); the tail block gets the +1e9 the score kernel gives it
84void fill_row(float* row, int64_t n_kv, int64_t n_bid, int pattern, std::mt19937& rng) {
85    std::normal_distribution<float> nd(0.f, 1.f);
86    std::uniform_int_distribution<int> pick(0, 99);
87    for (int64_t b = 0; b <= n_bid; ++b) {
88        float v = 0.f;
89        const float g = nd(rng);
90        switch (pattern) {
91            case kRandom: v = std::fmax(0.f, g) * 3.f + std::fmax(0.f, nd(rng)); break;   // relu sums: exact zeros too
92            case kAllEqual: v = 1.f; break;
93            case kFewValues: v = (float) (pick(rng) % 4) * 0.5f; break;
94            case kNaNs: v = pick(rng) < 15 ? kNaN : std::fabs(g); break;
95            case kInfs: {
96                const int r = pick(rng);
97                v = r < 5 ? kInf : r < 15 ? -kInf : r < 30 ? -std::fabs(g) : g;
98                break;
99            }
100            case kSignedZero: v = pick(rng) < 50 ? -0.0f : (pick(rng) < 50 ? 0.0f : -std::fabs(g) * 1e-3f); break;
101            case kDenormal: v = (float) (pick(rng) % 7) * 1e-41f; break;
102            case kAscending: v = (float) b; break;
103            case kDescending: v = (float) (n_bid - b); break;
104            case kAllNaN: v = kNaN; break;
105            case kQuantized: v = std::round(std::fabs(g) * 8.f) / 8.f; break;
106        }
107        row[b] = v;
108    }
109    if (n_kv % 4 != 0 && pattern != kAllNaN && pattern != kAllEqual) row[n_bid] += 1e9f;
110}
111
112struct TopkCase {
113    int64_t ctx;         // the last query's n_kv
114    int64_t capacity;    // --max-context: max_blocks = capacity / 4 + 2 (the engine's rule)
115    int nq;
116    int pattern;
117};
118
119int run_topk_case(const TopkCase& c, std::mt19937& rng, bool verbose) {
120    const k::QsaShapes s = k::qsa_real_shapes();
121    const int64_t max_blocks = c.capacity / 4 + 2, cap = k::qsa_selection_width(k::kTopkMaxCells, s);
122    std::vector<float> sc((size_t) (c.nq * max_blocks), 0.f);
123    std::vector<int32_t> steps((size_t) c.nq * k::kStepCount);
124    for (int i = 0; i < c.nq; ++i) {
125        const int64_t n_kv = c.ctx - (c.nq - 1 - i);   // the window's queries, one cell apart, the last at ctx
126        int32_t* st = steps.data() + (size_t) i * k::kStepCount;
127        st[k::kStepPos] = (int32_t) (n_kv - 1);
128        st[k::kStepNKv] = (int32_t) n_kv;
129        st[k::kStepNBid] = (int32_t) (n_kv / s.idx_block);
130        st[k::kStepWidth] = (int32_t) k::qsa_selection_width(n_kv, s);
131        fill_row(sc.data() + (size_t) i * max_blocks, n_kv, n_kv / s.idx_block, c.pattern, rng);
132    }
133    Dev<float> d_sc(sc.size());
134    const size_t n_ids = (size_t) (c.nq * cap);
135    Dev<int32_t> d_st(steps.size()), d_new(n_ids), d_old(n_ids), d_ref(n_ids);
136    d_sc.put(sc);
137    d_st.put(steps);
138    ck(cudaMemset(d_new.p, 0xA5, d_new.n * 4), "memset");
139    ck(cudaMemset(d_old.p, 0xA5, d_old.n * 4), "memset");
140    ck(cudaMemset(d_ref.p, 0xA5, d_ref.n * 4), "memset");
141    if (!k::qsa_block_topk_cluster(d_sc.p, d_st.p, c.nq, max_blocks, cap, s, d_new.p, nullptr)) {
142        std::printf("FAIL topk ctx %lld cap %lld nq %d: the cluster kernel did not run\n", (long long) c.ctx,
143                    (long long) c.capacity, c.nq);
144        return 1;
145    }
146    k::qsa_block_topk(d_sc.p, d_st.p, c.nq, max_blocks, cap, s, d_old.p, nullptr);
147    k::qsa_block_topk_ref(d_sc.p, d_st.p, c.nq, max_blocks, cap, s, d_ref.p, nullptr);
148    ck(cudaDeviceSynchronize(), "topk sync");
149    const std::vector<int32_t> a = d_new.get(), b = d_old.get(), r = d_ref.get();
150    int64_t diff_old = 0, diff_ref = 0, first = -1;
151    for (size_t i = 0; i < a.size(); ++i) {
152        if (a[i] != b[i]) { ++diff_old; if (first < 0) first = (int64_t) i; }
153        if (a[i] != r[i]) ++diff_ref;
154    }
155    const bool ok = diff_old == 0 && diff_ref == 0;
156    if (!ok || verbose)
157        std::printf("%s topk ctx %7lld cap %7lld nq %2d %-11s: %lld ids differ from the one-CTA dispatch, %lld from "
158                    "the reference%s\n", ok ? "ok  " : "FAIL", (long long) c.ctx, (long long) c.capacity, c.nq,
159                    pattern_name(c.pattern), (long long) diff_old, (long long) diff_ref,
160                    first >= 0 ? (" (first at " + std::to_string(first) + ")").c_str() : "");
161    return ok ? 0 : 1;
162}
163
164// ---- argmax fixtures
165enum AmPattern { aRandom, aTies, aAllEqual, aAllNegInf, aAllNaN, aNaNMax, aPosInf, aSignedZero, aLast, aFirst,
166                 aDenormal, aTiesAcross, aPatterns };
167const char* am_name(int p) {
168    static const char* n[] = {"random", "ties", "all-equal", "all--inf", "all-nan", "nan+max", "+inf", "signed-zero",
169                              "max-last", "max-first", "denormal", "ties-across"};
170    return n[p];
171}
172
173void fill_logits(float* l, int nv, int pattern, std::mt19937& rng) {
174    std::normal_distribution<float> nd(0.f, 3.f);
175    std::uniform_int_distribution<int> at(0, nv - 1);
176    for (int v = 0; v < nv; ++v) l[v] = nd(rng);
177    switch (pattern) {
178        case aRandom: break;
179        case aTies: { const int n = 1 + at(rng) % 9; for (int i = 0; i < n; ++i) l[at(rng)] = 100.f; break; }
180        case aAllEqual: for (int v = 0; v < nv; ++v) l[v] = 0.25f; break;
181        case aAllNegInf: for (int v = 0; v < nv; ++v) l[v] = -kInf; break;
182        case aAllNaN: for (int v = 0; v < nv; ++v) l[v] = kNaN; break;
183        case aNaNMax:
184            for (int v = 0; v < nv; ++v) if (at(rng) % 3 == 0) l[v] = kNaN;
185            l[at(rng)] = 50.f;
186            l[at(rng)] = 50.f;
187            break;
188        case aPosInf: for (int i = 0; i < 4; ++i) l[at(rng)] = kInf; break;
189        case aSignedZero:
190            for (int v = 0; v < nv; ++v) l[v] = -std::fabs(l[v]) - 1.f;
191            for (int i = 0; i < 6; ++i) l[at(rng)] = (i & 1) ? 0.0f : -0.0f;
192            break;
193        case aLast: l[nv - 1] = 1e30f; break;
194        case aFirst: l[0] = 1e30f; if (nv > 1) l[nv - 1] = 1e30f; break;
195        case aDenormal: for (int v = 0; v < nv; ++v) l[v] = (float) (at(rng) % 5) * 1e-42f - 1e-30f * (v & 1); break;
196        case aTiesAcross:   // the same maximum in several CTAs' and threads' ranges (8 x 1024 stride), plus -inf, NaN
197            for (int v = 0; v < nv; v += 1 + at(rng) % 9000) l[v] = 7.f;
198            for (int v = 3; v < nv; v += 11) l[v] = kNaN;
199            for (int v = 5; v < nv; v += 13) l[v] = -kInf;
200            break;
201    }
202}
203
204int run_argmax_case(int nv, int T, int pattern, std::mt19937& rng, bool verbose) {
205    std::vector<float> l((size_t) nv * T);
206    for (int t = 0; t < T; ++t) fill_logits(l.data() + (size_t) t * nv, nv, t % 2 == 0 ? pattern : aRandom, rng);
207    Dev<float> d_l(l.size());
208    Dev<int> d_new((size_t) T), d_old((size_t) T);
209    d_l.put(l);
210    ck(cudaMemset(d_new.p, 0xA5, (size_t) T * 4), "memset");
211    ck(cudaMemset(d_old.p, 0x5A, (size_t) T * 4), "memset");
212    k::SamplerParams sp;
213    sp.greedy = true;
214    sp.temperature = 0.f;
215    if (!k::sample_greedy_cluster(d_l.p, T, nv, d_new.p, nullptr)) {
216        std::printf("FAIL argmax nv %d T %d: the cluster kernel did not run\n", nv, T);
217        return 1;
218    }
219    k::sample_tokens(d_l.p, T, nv, nullptr, 0, sp, d_old.p, nullptr);
220    ck(cudaDeviceSynchronize(), "argmax sync");
221    const std::vector<int> a = d_new.get(), b = d_old.get();
222    // and an independent host answer: the lowest index of the largest non-NaN value above -inf, else 0
223    int bad = 0;
224    for (int t = 0; t < T; ++t) {
225        const float* r = l.data() + (size_t) t * nv;
226        int hb = -1;
227        for (int v = 0; v < nv; ++v)
228            if (r[v] > -kInf && (hb < 0 || r[v] > r[hb])) hb = v;
229        if (hb < 0) hb = 0;
230        if (a[(size_t) t] != b[(size_t) t] || a[(size_t) t] != hb) ++bad;
231    }
232    if (bad || verbose)
233        std::printf("%s argmax nv %6d T %d %-11s: %d rows differ (new %d, old %d)\n", bad ? "FAIL" : "ok  ", nv, T,
234                    am_name(pattern), bad, a[0], b[0]);
235    return bad ? 1 : 0;
236}
237
238// both cluster launches captured in one graph, replayed over new inputs written into the same buffers
239int run_graph_case(std::mt19937& rng) {
240    const k::QsaShapes s = k::qsa_real_shapes();
241    const int nq = 5, nv = 248320;
242    const int64_t capacity = 131072 + 272, max_blocks = capacity / 4 + 2;
243    const int64_t cap = k::qsa_selection_width(k::kTopkMaxCells, s);
244    Dev<float> d_sc((size_t) (nq * max_blocks)), d_l((size_t) nv * nq);
245    Dev<int32_t> d_st((size_t) nq * k::kStepCount), d_new((size_t) (nq * cap)), d_old((size_t) (nq * cap));
246    Dev<int> a_new((size_t) nq), a_old((size_t) nq);
247    ck(cudaMemset(d_new.p, 0, d_new.n * 4), "memset");   // the entries past a row's width: written by neither
248    ck(cudaMemset(d_old.p, 0, d_old.n * 4), "memset");
249    cudaStream_t cs;
250    ck(cudaStreamCreateWithFlags(&cs, cudaStreamNonBlocking), "stream");
251    cudaGraph_t g = nullptr;
252    cudaGraphExec_t ge = nullptr;
253    ck(cudaStreamBeginCapture(cs, cudaStreamCaptureModeThreadLocal), "begin capture");
254    const bool l1 = k::qsa_block_topk_cluster(d_sc.p, d_st.p, nq, max_blocks, cap, s, d_new.p, cs);
255    const bool l2 = k::sample_greedy_cluster(d_l.p, nq, nv, a_new.p, cs);
256    ck(cudaStreamEndCapture(cs, &g), "end capture");
257    if (!l1 && !l2) {   // no cluster code here (a card or a build below sm_90): nothing to compare
258        if (g) cudaGraphDestroy(g);
259        cudaStreamDestroy(cs);
260        return -1;
261    }
262    if (!l1 || !l2) { std::printf("FAIL graph: one cluster kernel did not run\n"); return 1; }
263    ck(cudaGraphInstantiate(&ge, g, 0), "instantiate");
264    int fails = 0;
265    for (int rep = 0; rep < 6; ++rep) {
266        const int64_t ctx = 100000 + rep * 4001 + rep % 4;
267        std::vector<float> sc((size_t) (nq * max_blocks), 0.f), l((size_t) nv * nq);
268        std::vector<int32_t> steps((size_t) nq * k::kStepCount);
269        for (int i = 0; i < nq; ++i) {
270            const int64_t n_kv = ctx - (nq - 1 - i);
271            int32_t* st = steps.data() + (size_t) i * k::kStepCount;
272            st[k::kStepPos] = (int32_t) (n_kv - 1);
273            st[k::kStepNKv] = (int32_t) n_kv;
274            st[k::kStepNBid] = (int32_t) (n_kv / 4);
275            st[k::kStepWidth] = (int32_t) k::qsa_selection_width(n_kv, s);
276            fill_row(sc.data() + (size_t) i * max_blocks, n_kv, n_kv / 4, rep % 2 ? kFewValues : kRandom, rng);
277            fill_logits(l.data() + (size_t) i * nv, nv, rep % 2 ? aTiesAcross : aRandom, rng);
278        }
279        d_sc.put(sc);
280        d_st.put(steps);
281        d_l.put(l);
282        ck(cudaGraphLaunch(ge, cs), "graph launch");
283        k::qsa_block_topk(d_sc.p, d_st.p, nq, max_blocks, cap, s, d_old.p, cs);
284        k::SamplerParams sp;
285        sp.greedy = true;
286        k::sample_tokens(d_l.p, nq, nv, nullptr, 0, sp, a_old.p, cs);
287        ck(cudaStreamSynchronize(cs), "graph sync");
288        const auto x = d_new.get(), y = d_old.get();
289        const auto u = a_new.get(), v = a_old.get();
290        int64_t d1 = 0, d2 = 0;
291        for (size_t i = 0; i < x.size(); ++i) d1 += x[i] != y[i];
292        for (size_t i = 0; i < u.size(); ++i) d2 += u[i] != v[i];
293        if (d1 || d2) ++fails;
294        std::printf("%s graph replay %d (ctx %lld): %lld ids, %lld tokens differ\n", d1 || d2 ? "FAIL" : "ok  ", rep,
295                    (long long) ctx, (long long) d1, (long long) d2);
296    }
297    cudaGraphExecDestroy(ge);
298    cudaGraphDestroy(g);
299    cudaStreamDestroy(cs);
300    return fails;
301}
302
303// back-to-back launches on `cs` (the engine's are too: one stream, inside a graph)
304template <typename F> float time_us(cudaStream_t cs, F&& f, int reps = 300) {
305    cudaEvent_t a, b;
306    cudaEventCreate(&a);
307    cudaEventCreate(&b);
308    for (int i = 0; i < 5; ++i) f();
309    cudaEventRecord(a, cs);
310    for (int i = 0; i < reps; ++i) f();
311    cudaEventRecord(b, cs);
312    ck(cudaEventSynchronize(b), "time");
313    float ms = 0.f;
314    cudaEventElapsedTime(&ms, a, b);
315    cudaEventDestroy(a);
316    cudaEventDestroy(b);
317    return ms * 1000.f / (float) reps;
318}
319
320int bench() {
321    std::mt19937 rng(11);
322    const k::QsaShapes s = k::qsa_real_shapes();
323    const int64_t cap = k::qsa_selection_width(k::kTopkMaxCells, s);
324    cudaStream_t cs;
325    ck(cudaStreamCreateWithFlags(&cs, cudaStreamNonBlocking), "stream");
326    std::printf("QSA top-k, per call (us); capacity = context + 272 as the speed runs set --max-context\n");
327    for (const int64_t ctx : {32768LL, 131072LL, 262144LL - 272}) {
328        for (const int nq : {1, 5}) {
329            const int64_t capacity = ctx + 272, max_blocks = capacity / 4 + 2;
330            std::vector<float> sc((size_t) (nq * max_blocks), 0.f);
331            std::vector<int32_t> steps((size_t) nq * k::kStepCount);
332            for (int i = 0; i < nq; ++i) {
333                const int64_t n_kv = ctx - (nq - 1 - i);
334                int32_t* st = steps.data() + (size_t) i * k::kStepCount;
335                st[k::kStepPos] = (int32_t) (n_kv - 1);
336                st[k::kStepNKv] = (int32_t) n_kv;
337                st[k::kStepNBid] = (int32_t) (n_kv / 4);
338                st[k::kStepWidth] = (int32_t) k::qsa_selection_width(n_kv, s);
339                fill_row(sc.data() + (size_t) i * max_blocks, n_kv, n_kv / 4, kRandom, rng);
340            }
341            Dev<float> d_sc(sc.size());
342            Dev<int32_t> d_st(steps.size()), d_ids((size_t) (nq * cap));
343            d_sc.put(sc);
344            d_st.put(steps);
345            const float* sp = d_sc.p;
346            const int32_t* st = d_st.p;
347            int32_t* ids = d_ids.p;
348            const float t_old = time_us(cs, [&] { k::qsa_block_topk(sp, st, nq, max_blocks, cap, s, ids, cs); });
349            const float t_ref = time_us(cs, [&] { k::qsa_block_topk_ref(sp, st, nq, max_blocks, cap, s, ids, cs); });
350            const float t_new = time_us(cs, [&] { k::qsa_block_topk_cluster(sp, st, nq, max_blocks, cap, s, ids, cs); });
351            std::printf("  ctx %6lld nq %d: one-CTA dispatch %7.1f  (ref %7.1f)  cluster %6.1f  -> %.1fx\n",
352                        (long long) ctx, nq, t_old, t_ref, t_new, t_old / t_new);
353        }
354    }
355    std::printf("argmax, per call (us)\n");
356    for (const int nv : {248320, 32768}) {
357        for (const int T : {1, 5}) {
358            std::vector<float> l((size_t) nv * T);
359            for (int t = 0; t < T; ++t) fill_logits(l.data() + (size_t) t * nv, nv, aRandom, rng);
360            Dev<float> d_l(l.size());
361            Dev<int> d_o((size_t) T);
362            d_l.put(l);
363            k::SamplerParams sp;
364            sp.greedy = true;
365            const float t_old = time_us(cs, [&] { k::sample_tokens(d_l.p, T, nv, nullptr, 0, sp, d_o.p, cs); });
366            const float t_new = time_us(cs, [&] { k::sample_greedy_cluster(d_l.p, T, nv, d_o.p, cs); });
367            std::printf("  n_vocab %6d T %d: one block %6.1f  cluster %5.1f  -> %.1fx\n", nv, T, t_old, t_new,
368                        t_old / t_new);
369        }
370    }
371    cudaStreamDestroy(cs);
372    return 0;
373}
374
375}  // namespace
376
377int main(int argc, char** argv) {
378    set_env("STRATA_QSA_CLUSTER", "0");     // the dispatchers: the one-CTA kernels (the cluster ones called directly)
379    set_env("STRATA_ARGMAX_MULTI", "0");
380    const bool do_bench = argc > 1 && std::strcmp(argv[1], "--bench") == 0;
381    const bool verbose = argc > 2 && std::strcmp(argv[2], "-v") == 0;
382    int dev = 0, major = 0;
383    ck(cudaGetDevice(&dev), "device");
384    ck(cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, dev), "cc");
385    std::mt19937 rng(20261002);
386    // first, so that both kernels' first calls (their per-device checks and opt-ins) happen inside a stream capture,
387    // as in the engine's decode graphs
388    const int gfails = run_graph_case(rng);
389    if (gfails < 0) {
390        std::printf("SKIP: no thread-block clusters on this card / build (compute capability %d)\n", major);
391        return 0;
392    }
393    if (do_bench) return bench();
394    int fails = 0, cases = 0;
395    // contexts: below the width (identity), at it, just past it, odd tails, the register kernel's range, past it
396    // (block_topk_kernel), up to 262,144; capacities: tight (the speed runs) and 262,144 (a long --max-context)
397    const int64_t ctxs[] = {7, 2050, 2051, 2052, 2053, 2054, 2055, 3001, 9000, 32768, 32769, 65538, 100003,
398                            131072, 135170, 135171, 200001, 262142, 262143, 262144};
399    for (const int64_t ctx : ctxs) {
400        for (const int64_t capacity : {ctx + 272, (int64_t) 262144 + 272}) {
401            if (capacity < ctx) continue;
402            for (int p = 0; p < kPatterns; ++p) {
403                const int nq = (p % 3 == 0) ? 1 : (p % 3 == 1 ? 5 : 16);
404                if (nq > ctx) continue;
405                fails += run_topk_case({ctx, capacity, nq, p}, rng, verbose);
406                ++cases;
407            }
408        }
409    }
410    std::printf("QSA top-k: %d cases, %d failed\n", cases, fails);
411    int afails = 0, acases = 0;
412    for (const int nv : {248320, 248321, 248319, 32771, 24576, 8193, 1000, 33, 1}) {
413        for (int p = 0; p < aPatterns; ++p) {
414            for (const int T : {1, 5}) {
415                afails += run_argmax_case(nv, T, p, rng, verbose);
416                ++acases;
417            }
418        }
419    }
420    std::printf("argmax: %d cases, %d failed\n", acases, afails);
421    const int total = fails + afails + gfails;
422    std::printf("%s\n", total == 0 ? "PASS: the cluster kernels are bitwise identical" : "FAIL");
423    return total == 0 ? 0 : 1;
424}