okami.mom/strata

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

Niko1221Merge branch 'f-split-int' into f-split-fitfa6367a98

main
20.7 KiB424 linesraw
1// src/kernels/fusions_parity.cpp - the V100 fusion work against its own unfused pipeline, BITWISE, plus
2// the before/after kernel timings (CUDA events).
3//
4//     build/fusions_parity            every bitwise check below; exit 1 on any difference
5//     build/fusions_parity --bench    fused vs unfused kernel times at decode shapes (M=1 and M=8)
6//
7// WHAT IS CHECKED (synthetic data, GPU, no model):
8//   1. `swilu_quantize_q8_0` / `_scaled` against `swiglu` + `quantize_q8_0` / `_scaled` - the S2 expert
9//      path's intermediate.  Products, q8 bytes and the fp32 scales all bitwise.
10//   2. `swilu_quantize_q8_0` / `_q8_K` with the LEGACY silu against `shared_expert`'s swiglu kernel +
11//      `quantize_q8_0` / `quantize_q8_K`.
12//   3. `quantize_act_images` against the three standalone quantizers (q8_0 bytes, q8_K bytes, bf16 bits).
13//   4. `bf16_gemv_pair` against two `bf16_gemv_split(32)` calls (the warp accumulation it replicates).
14#include "strata/kernels/bf16_gemv.hpp"
15#include "strata/kernels/bf16_bits.hpp"
16#include "strata/kernels/elementwise.hpp"
17#include "strata/kernels/f16_bits.hpp"
18#include "strata/kernels/quantize_act.hpp"
19#include "strata/kernels/s_gemv.hpp"
20
21#include <cuda_runtime.h>
22
23#include <cmath>
24#include <cstdint>
25#include <cstdio>
26#include <cstdlib>
27#include <cstring>
28#include <functional>
29#include <random>
30#include <string>
31#include <vector>
32
33namespace k = strata::kernels;
34
35namespace {
36
37int g_fail = 0;
38
39void ck(cudaError_t e, const char* w) {
40    if (e != cudaSuccess) { std::fprintf(stderr, "%s: %s\n", w, cudaGetErrorString(e)); std::exit(2); }
41}
42template <typename T> T* dalloc(size_t n) {
43    T* p = nullptr;
44    ck(cudaMalloc(&p, n * sizeof(T)), "malloc");
45    ck(cudaMemset(p, 0, n * sizeof(T)), "memset");
46    return p;
47}
48template <typename T> void up(T* d, const std::vector<T>& h) {
49    ck(cudaMemcpy(d, h.data(), h.size() * sizeof(T), cudaMemcpyHostToDevice), "h2d");
50}
51template <typename T> std::vector<T> down(const T* d, size_t n) {
52    std::vector<T> h(n);
53    ck(cudaMemcpy(h.data(), d, n * sizeof(T), cudaMemcpyDeviceToHost), "d2h");
54    return h;
55}
56
57void report(const char* name, bool ok) {
58    std::printf("  %-52s %s\n", name, ok ? "bitwise identical" : "DIFFERS");
59    if (!ok) ++g_fail;
60}
61
62// the `gemv` dispatch `shared_expert`'s lambda uses (defined later; declared for `run_bench`)
63void gemv_dispatch(const k::SForm& f, const uint8_t* codes, const float* scales, const float* off,
64                   const uint8_t* x80, const uint8_t* x8k, float* y, long long n_in, long long n_out,
65                   void* stream);
66
67// ---- the unfused SwiGLU references, one thread per element (the shapes the standalone kernels had) ----
68__global__ void swilu_legacy_kernel(const float* g, const float* u, float* out, int n) {
69    const int i = blockIdx.x * blockDim.x + threadIdx.x;
70    if (i >= n) return;
71    const double x = (double) g[i];
72    out[i] = (float) (x / (1.0 + exp(-x))) * u[i];
73}
74__global__ void swilu_fast_kernel(const float* g, const float* u, float* out, int n) {
75    const int i = blockIdx.x * blockDim.x + threadIdx.x;
76    if (i >= n) return;
77    out[i] = (g[i] / (1.0f + __expf(-g[i]))) * u[i];
78}
79__global__ void swilu_native_kernel(const float* g, const float* u, float* out, int n) {
80    const int i = blockIdx.x * blockDim.x + threadIdx.x;
81    if (i >= n) return;
82    out[i] = __fdividef(g[i], 1.0f + __expf(-g[i])) * u[i];
83}
84
85void fill_pairs(std::vector<float>& g, std::vector<float>& u, unsigned seed) {
86    std::mt19937 rng(seed);
87    std::uniform_real_distribution<float> gv(-6.0f, 6.0f), uv(-3.0f, 3.0f);
88    for (size_t i = 0; i < g.size(); ++i) { g[i] = gv(rng); u[i] = uv(rng); }
89}
90
91void check_swilu_quantize(int kind, bool scaled, const char* name) {
92    const long long n = 640 * 4;                       // FF x a few hits, multiple of 256
93    std::vector<float> hg(n), hu(n);
94    fill_pairs(hg, hu, 7u + (unsigned) kind * 13 + (unsigned) scaled);
95    float *dg = dalloc<float>(n), *du = dalloc<float>(n), *dfused = dalloc<float>(n), *dref = dalloc<float>(n);
96    up(dg, hg); up(du, hu);
97    const size_t nb80 = (size_t) (n / 32);
98    uint8_t* db80 = dalloc<uint8_t>(nb80 * 34 + 256);
99    uint8_t* db80r = dalloc<uint8_t>(nb80 * 34 + 256);
100    float* ds = dalloc<float>(nb80 + 16);
101    float* dsr = dalloc<float>(nb80 + 16);
102
103    // fused
104    if (scaled) k::swilu_quantize_q8_0_scaled(dg, du, dfused, n, kind, db80, ds, nullptr);
105    else k::swilu_quantize_q8_0(dg, du, dfused, n, kind, db80, nullptr);
106    // unfused: standalone swilu then quantize
107    const int blocks = (int) ((n + 127) / 128);
108    if (kind == 0) swilu_legacy_kernel<<<blocks, 128>>>(dg, du, dref, (int) n);
109    else swilu_fast_kernel<<<blocks, 128>>>(dg, du, dref, (int) n);
110    ck(cudaGetLastError(), "swilu ref");
111    if (scaled) k::quantize_q8_0_scaled(dref, db80r, dsr, n, nullptr);
112    else k::quantize_q8_0(dref, db80r, n, nullptr);
113    ck(cudaDeviceSynchronize(), "sync");
114
115    const std::vector<float> p1 = down(dfused, n), p2 = down(dref, n);
116    const std::vector<uint8_t> q1 = down(db80, nb80 * 34), q2 = down(db80r, nb80 * 34);
117    bool ok = std::memcmp(p1.data(), p2.data(), n * sizeof(float)) == 0 &&
118              std::memcmp(q1.data(), q2.data(), nb80 * 34) == 0;
119    if (scaled) {
120        const std::vector<float> s1 = down(ds, nb80), s2 = down(dsr, nb80);
121        ok = ok && std::memcmp(s1.data(), s2.data(), nb80 * sizeof(float)) == 0;
122    }
123    report(name, ok);
124    cudaFree(dg); cudaFree(du); cudaFree(dfused); cudaFree(dref); cudaFree(db80); cudaFree(db80r);
125    cudaFree(ds); cudaFree(dsr);
126}
127
128void check_swilu_quantize_q8_K() {
129    const long long n = 256 * 3;
130    std::vector<float> hg(n), hu(n);
131    fill_pairs(hg, hu, 99u);
132    float *dg = dalloc<float>(n), *du = dalloc<float>(n), *df = dalloc<float>(n), *dr = dalloc<float>(n);
133    up(dg, hg); up(du, hu);
134    const size_t nbK = (size_t) (n / 256);
135    uint8_t* dK = dalloc<uint8_t>(nbK * 292 + 256);
136    uint8_t* dKr = dalloc<uint8_t>(nbK * 292 + 256);
137    k::swilu_quantize_q8_K(dg, du, df, n, 0, dK, nullptr);
138    const int blocks = (int) ((n + 127) / 128);
139    swilu_legacy_kernel<<<blocks, 128>>>(dg, du, dr, (int) n);
140    k::quantize_q8_K(dr, dKr, n, nullptr);
141    ck(cudaDeviceSynchronize(), "sync");
142    bool ok = down(df, n) == down(dr, n) && down(dK, nbK * 292) == down(dKr, nbK * 292);
143    report("swilu_quantize_q8_K (legacy silu)", ok);
144    cudaFree(dg); cudaFree(du); cudaFree(df); cudaFree(dr); cudaFree(dK); cudaFree(dKr);
145}
146
147void check_images() {
148    const long long n = 2560;
149    std::vector<float> hx(n);
150    std::mt19937 rng(4u);
151    std::uniform_real_distribution<float> d(-4.0f, 4.0f);
152    for (auto& v : hx) v = d(rng);
153    float* dx = dalloc<float>(n);
154    up(dx, hx);
155    const size_t nb80 = (size_t) (n / 32), nbK = (size_t) (n / 256);
156    uint8_t* i80 = dalloc<uint8_t>(nb80 * 34 + 256);
157    uint8_t* iK = dalloc<uint8_t>(nbK * 292 + 256);
158    uint16_t* i16 = dalloc<uint16_t>(n + 8);
159    uint8_t* r80 = dalloc<uint8_t>(nb80 * 34 + 256);
160    uint8_t* rK = dalloc<uint8_t>(nbK * 292 + 256);
161    k::quantize_act_images(dx, n, i80, iK, i16, nullptr);
162    k::quantize_q8_0(dx, r80, n, nullptr);
163    k::quantize_q8_K(dx, rK, n, nullptr);
164    ck(cudaDeviceSynchronize(), "sync");
165    // the standalone bf16 conversion is `bf16_from_f32` per element (`f32_to_bf16_bulk`)
166    std::vector<uint16_t> hb(n);
167    for (long long i = 0; i < n; ++i) hb[(size_t) i] = k::bf16_from_f32(hx[(size_t) i]);
168    const std::vector<uint16_t> got16 = down(i16, n);
169    report("quantize_act_images (q8_0 + q8_K + bf16)",
170           down(i80, nb80 * 34) == down(r80, nb80 * 34) && down(iK, nbK * 292) == down(rK, nbK * 292) &&
171           std::memcmp(got16.data(), hb.data(), n * sizeof(uint16_t)) == 0);
172    cudaFree(dx); cudaFree(i80); cudaFree(iK); cudaFree(i16); cudaFree(r80); cudaFree(rK);
173}
174
175void check_pair() {
176    const long long n_in = 2560, n1 = 48, n2 = 48;    // ssm_alpha / ssm_beta shapes
177    std::vector<uint16_t> hx(n_in), hw1(n_in * n1), hw2(n_in * n2);
178    std::mt19937 rng(11u);
179    std::uniform_real_distribution<float> d(-2.0f, 2.0f);
180    auto put = [&](std::vector<uint16_t>& v) {
181        for (auto& t : v) t = k::bf16_from_f32(d(rng));
182    };
183    put(hx); put(hw1); put(hw2);
184    uint16_t *dx = dalloc<uint16_t>(n_in), *dw1 = dalloc<uint16_t>(n_in * n1), *dw2 = dalloc<uint16_t>(n_in * n2);
185    up(dx, hx); up(dw1, hw1); up(dw2, hw2);
186    float *dy1 = dalloc<float>(n1 + 8), *dy2 = dalloc<float>(n2 + 8), *dr1 = dalloc<float>(n1 + 8),
187          *dr2 = dalloc<float>(n2 + 8);
188    k::bf16_gemv_pair(dx, dw1, dy1, n1, dw2, dy2, n2, n_in, nullptr);
189    k::bf16_gemv_split(dx, dw1, dr1, n_in, n1, 32, nullptr);
190    k::bf16_gemv_split(dx, dw2, dr2, n_in, n2, 32, nullptr);
191    ck(cudaDeviceSynchronize(), "sync");
192    report("bf16_gemv_pair vs 2x bf16_gemv_split(32)",
193           down(dy1, n1) == down(dr1, n1) && down(dy2, n2) == down(dr2, n2));
194    cudaFree(dx); cudaFree(dw1); cudaFree(dw2); cudaFree(dy1); cudaFree(dy2); cudaFree(dr1); cudaFree(dr2);
195}
196
197// ---- micro bench: fused vs unfused kernel time at decode shapes, CUDA events -------------------------
198//
199// The decode geometry is the model's: n_embd 2560, FF 640 for the experts (H/FF from `cpu/expert.hpp`).
200// M = 1 is the single-token decode; M = 8 runs the M=8 pair shapes (8 columns of the same weights).
201
202float timed(cudaStream_t s, const std::function<void()>& f, int reps) {
203    cudaEvent_t a, b;
204    ck(cudaEventCreate(&a), "ev"); ck(cudaEventCreate(&b), "ev");
205    f(); ck(cudaStreamSynchronize(s), "sync");               // warmup
206    ck(cudaEventRecord(a, s), "rec");
207    for (int i = 0; i < reps; ++i) f();
208    ck(cudaEventRecord(b, s), "rec");
209    ck(cudaEventSynchronize(b), "sync");
210    float ms = 0.0f;
211    ck(cudaEventElapsedTime(&ms, a, b), "elapsed");
212    cudaEventDestroy(a); cudaEventDestroy(b);
213    return ms / reps;
214}
215
216void bench_pair(const char* what, const std::function<void()>& before, const std::function<void()>& after,
217                int reps) {
218    cudaStream_t s = nullptr;
219    const float t0 = timed(s, before, reps), t1 = timed(s, after, reps);
220    std::printf("  %-44s unfused %8.1f us   fused %8.1f us   %+.1f%%\n", what, t0 * 1e3f, t1 * 1e3f,
221                (t1 / t0 - 1.0f) * 100.0f);
222}
223
224void run_bench() {
225    std::printf("\nfusions bench: decode shapes (n_embd 2560, FF 640), CUDA events, %s\n",
226                "stream = default, 200 reps");
227    const int reps = 200;
228
229    for (int m : {1, 8}) {
230        const long long pairs = (long long) 640 * m;             // one expert hit's rows x tokens
231        std::vector<float> hg(pairs), hu(pairs);
232        fill_pairs(hg, hu, 5u + (unsigned) m);
233        float *dg = dalloc<float>(pairs), *du = dalloc<float>(pairs), *dout = dalloc<float>(pairs);
234        up(dg, hg); up(du, hu);
235        const size_t nb = (size_t) (pairs / 32);
236        uint8_t* d80 = dalloc<uint8_t>(nb * 34 + 256);
237        float* ds = dalloc<float>(nb + 16);
238        char label[96];
239        std::snprintf(label, sizeof label, "expert intermediate  M=%d  (swilu + q8_0)", m);
240        bench_pair(label,
241                   [&] {
242                       const int blocks = (int) ((pairs + 127) / 128);
243                       swilu_fast_kernel<<<blocks, 128>>>(dg, du, dout, (int) pairs);
244                       k::quantize_q8_0(dout, d80, pairs, nullptr);
245                   },
246                   [&] { k::swilu_quantize_q8_0(dg, du, dout, pairs, 1, d80, nullptr); }, reps);
247        cudaFree(dg); cudaFree(du); cudaFree(dout); cudaFree(d80); cudaFree(ds);
248    }
249
250    {
251        const long long n = 2560;
252        std::vector<float> hx(n);
253        std::mt19937 rng(21u);
254        std::uniform_real_distribution<float> d(-4.0f, 4.0f);
255        for (auto& v : hx) v = d(rng);
256        float* dx = dalloc<float>(n);
257        up(dx, hx);
258        const size_t nb80 = (size_t) (n / 32), nbK = (size_t) (n / 256);
259        uint8_t* o80 = dalloc<uint8_t>(nb80 * 34 + 256);
260        uint8_t* oK = dalloc<uint8_t>(nbK * 292 + 256);
261        uint16_t* o16 = dalloc<uint16_t>(n + 8);
262        bench_pair("layer activation images  (q8_0 + q8_K + bf16)",
263                   [&] {
264                       k::quantize_q8_0(dx, o80, n, nullptr);
265                       k::quantize_q8_K(dx, oK, n, nullptr);
266                       k::f32_to_bf16_bulk(dx, o16, n, nullptr);
267                   },
268                   [&] { k::quantize_act_images(dx, n, o80, oK, o16, nullptr); }, reps);
269        cudaFree(dx); cudaFree(o80); cudaFree(oK); cudaFree(o16);
270    }
271
272    {
273        // (a) gate/up dual GEMV + SwiGLU: one kernel vs gemv(gate) + gemv(up) + swilu, shared-expert shapes
274        const long long n_in = 2560, n_out = 640;
275        const long long n_groups = n_in / 64;
276        std::vector<uint8_t> hcg((size_t) n_out * n_in / 2), hcu((size_t) n_out * n_in / 2);
277        std::vector<float> hsg((size_t) n_out * n_groups), hsu((size_t) n_out * n_groups);
278        std::vector<float> hog((size_t) n_out * n_groups), hou((size_t) n_out * n_groups);
279        std::vector<float> hx(n_in);
280        std::mt19937 rng(55u);
281        for (auto& v : hcg) v = (uint8_t) rng();
282        for (auto& v : hcu) v = (uint8_t) rng();
283        std::uniform_real_distribution<float> sc(1e-3f, 3e-2f), xv(-4.0f, 4.0f), ov(-1e-2f, 1e-2f);
284        for (auto& v : hsg) v = sc(rng);
285        for (auto& v : hsu) v = sc(rng);
286        for (auto& v : hog) v = ov(rng);
287        for (auto& v : hou) v = ov(rng);
288        for (auto& v : hx) v = xv(rng);
289        float* dx2 = dalloc<float>(n_in);
290        up(dx2, hx);
291        uint8_t* dx80 = dalloc<uint8_t>((size_t) (n_in / 32) * 34 + 256);
292        k::quantize_q8_0(dx2, dx80, n_in, nullptr);
293        uint8_t *dcg = dalloc<uint8_t>(hcg.size()), *dcu = dalloc<uint8_t>(hcu.size());
294        float *dsg = dalloc<float>(hsg.size()), *dsu = dalloc<float>(hsu.size());
295        float *dog = dalloc<float>(hog.size()), *dou = dalloc<float>(hou.size());
296        up(dcg, hcg); up(dcu, hcu); up(dsg, hsg); up(dsu, hsu); up(dog, hog); up(dou, hou);
297        float *d_gate = dalloc<float>(n_out + 8), *d_up = dalloc<float>(n_out + 8), *d_pair = dalloc<float>(n_out + 8);
298        const k::SForm fg{4, -8, 64, k::Codebook::Affine, true, 0};
299        const k::SForm fu{4, -8, 64, k::Codebook::Affine, true, 0};
300        bench_pair("dual GEMV + swilu (gate,up)  FF=640",
301                   [&] {
302                       gemv_dispatch(fg, dcg, dsg, dog, dx80, nullptr, d_gate, n_in, n_out, nullptr);
303                       gemv_dispatch(fu, dcu, dsu, dou, dx80, nullptr, d_up, n_in, n_out, nullptr);
304                       const int bl = (int) ((n_out + 127) / 128);
305                       swilu_legacy_kernel<<<bl, 128>>>(d_gate, d_up, d_pair, (int) n_out);
306                   },
307                   [&] {
308                       k::s_gemv_pair_silu(dx80, nullptr, fg, dcg, dsg, dog, fu, dcu, dsu, dou, d_pair,
309                                           n_in, n_out, 32, 0, nullptr);
310                   }, reps);
311        cudaFree(dx2); cudaFree(dx80); cudaFree(dcg); cudaFree(dcu); cudaFree(dsg); cudaFree(dsu);
312        cudaFree(dog); cudaFree(dou); cudaFree(d_gate); cudaFree(d_up); cudaFree(d_pair);
313    }
314
315    for (int m : {1, 8}) {
316        const long long n_in = 2560, n1 = 48, n2 = 48;
317        std::vector<uint16_t> hx(n_in), hw1(n_in * n1), hw2(n_in * n2);
318        std::mt19937 rng(31u + (unsigned) m);
319        std::uniform_real_distribution<float> d(-2.0f, 2.0f);
320        auto put = [&](std::vector<uint16_t>& v) { for (auto& t : v) t = k::bf16_from_f32(d(rng)); };
321        put(hx); put(hw1); put(hw2);
322        uint16_t *dx = dalloc<uint16_t>(n_in), *dw1 = dalloc<uint16_t>(n_in * n1), *dw2 = dalloc<uint16_t>(n_in * n2);
323        up(dx, hx); up(dw1, hw1); up(dw2, hw2);
324        float *dy1 = dalloc<float>(n1 + 8), *dy2 = dalloc<float>(n2 + 8);
325        char label[96];
326        std::snprintf(label, sizeof label, "bf16 pair (alpha,beta)   M=%d", m);
327        bench_pair(label,
328                   [&] {
329                       k::bf16_gemv_split(dx, dw1, dy1, n_in, n1, 32, nullptr);
330                       k::bf16_gemv_split(dx, dw2, dy2, n_in, n2, 32, nullptr);
331                   },
332                   [&] { k::bf16_gemv_pair(dx, dw1, dy1, n1, dw2, dy2, n2, n_in, nullptr); }, reps);
333        cudaFree(dx); cudaFree(dw1); cudaFree(dw2); cudaFree(dy1); cudaFree(dy2);
334    }
335}
336
337// ---- (a) the gate/up dual GEMV + SwiGLU kernel against its unfused pipeline, bitwise ----------------
338// The unfused pipeline is the one `shared_expert` ran: a standalone GEMV per projection (dispatching the
339// same way its `gemv` lambda does) then the standalone SwiGLU kernel.  Both silu kinds are checked.
340
341void gemv_dispatch(const k::SForm& f, const uint8_t* codes, const float* scales, const float* off,
342                   const uint8_t* x80, const uint8_t* x8k, float* y, long long n_in, long long n_out,
343                   void* stream) {
344    if (f.code_bits == 2) {
345        std::fprintf(stderr, "gemv_dispatch: code_bits 2 goes through s2_gemv_q8 - not exercised here\n");
346        std::exit(2);
347    }
348    if (f.act_kind == 1) k::s_gemv_q8k_split(x8k, codes, scales, off, y, n_in, n_out, f, stream);
349    else k::s_gemv_q8_0_split(x80, codes, scales, off, y, n_in, n_out, f, stream);
350}
351
352void check_dual_gemv(int swilu_kind, const char* name) {
353    const long long n_in = 2560, n_out = 128;
354    const long long n_groups = n_in / 64;
355    std::vector<uint8_t> hcg((size_t) n_out * n_in / 2), hcu((size_t) n_out * n_in / 2);  // S4 codes
356    std::vector<float> hsg((size_t) n_out * n_groups), hsu((size_t) n_out * n_groups);
357    std::vector<float> hog((size_t) n_out * n_groups), hou((size_t) n_out * n_groups);
358    std::vector<float> hx(n_in);
359    std::mt19937 rng(77u + (unsigned) swilu_kind);
360    for (auto& v : hcg) v = (uint8_t) rng();
361    for (auto& v : hcu) v = (uint8_t) rng();
362    std::uniform_real_distribution<float> sc(1e-3f, 3e-2f), xv(-4.0f, 4.0f), ov(-1e-2f, 1e-2f);
363    for (auto& v : hsg) v = sc(rng);
364    for (auto& v : hsu) v = sc(rng);
365    for (auto& v : hog) v = ov(rng);
366    for (auto& v : hou) v = ov(rng);
367    for (auto& v : hx) v = xv(rng);
368
369    // Q8_0 activation image of x (both sides use it)
370    const size_t nb80 = (size_t) (n_in / 32);
371    std::vector<uint8_t> hx80(nb80 * 34 + 256);
372    uint8_t* dx80 = dalloc<uint8_t>(hx80.size());
373    float* dx = dalloc<float>(n_in);
374    up(dx, hx);
375    k::quantize_q8_0(dx, dx80, n_in, nullptr);
376    ck(cudaDeviceSynchronize(), "sync");
377
378    uint8_t *dcg = dalloc<uint8_t>(hcg.size()), *dcu = dalloc<uint8_t>(hcu.size());
379    float *dsg = dalloc<float>(hsg.size()), *dsu = dalloc<float>(hsu.size());
380    float *dog = dalloc<float>(hog.size()), *dou = dalloc<float>(hou.size());
381    up(dcg, hcg); up(dcu, hcu); up(dsg, hsg); up(dsu, hsu); up(dog, hog); up(dou, hou);
382    float* d_pair = dalloc<float>(n_out + 8);
383    float* d_gate = dalloc<float>(n_out + 8), *d_up = dalloc<float>(n_out + 8), *d_ref = dalloc<float>(n_out + 8);
384
385    const k::SForm fg{4, -8, 64, k::Codebook::Affine, true, 0};
386    const k::SForm fu{4, -8, 64, k::Codebook::Affine, true, 0};
387    const bool ok_launch = k::s_gemv_pair_silu(dx80, nullptr, fg, dcg, dsg, dog, fu, dcu, dsu, dou,
388                                               d_pair, n_in, n_out, 32, swilu_kind, nullptr);
389    // unfused: the same two GEMVs the `gemv` lambda would run, then the standalone swilu
390    gemv_dispatch(fg, dcg, dsg, dog, dx80, nullptr, d_gate, n_in, n_out, nullptr);
391    gemv_dispatch(fu, dcu, dsu, dou, dx80, nullptr, d_up, n_in, n_out, nullptr);
392    const int blocks = (int) ((n_out + 127) / 128);
393    if (swilu_kind == 0) swilu_legacy_kernel<<<blocks, 128>>>(d_gate, d_up, d_ref, (int) n_out);
394    else if (swilu_kind == 1) swilu_fast_kernel<<<blocks, 128>>>(d_gate, d_up, d_ref, (int) n_out);
395    else swilu_native_kernel<<<blocks, 128>>>(d_gate, d_up, d_ref, (int) n_out);
396    ck(cudaGetLastError(), "swilu ref");
397    ck(cudaDeviceSynchronize(), "sync");
398    report(name, ok_launch && down(d_pair, n_out) == down(d_ref, n_out));
399
400    cudaFree(dx80); cudaFree(dx); cudaFree(dcg); cudaFree(dcu); cudaFree(dsg); cudaFree(dsu);
401    cudaFree(dog); cudaFree(dou); cudaFree(d_pair); cudaFree(d_gate); cudaFree(d_up); cudaFree(d_ref);
402}
403
404}  // namespace
405
406int main(int argc, char** argv) {
407    bool bench = false;
408    for (int i = 1; i < argc; ++i)
409        if (std::string(argv[i]) == "--bench") bench = true;
410        else { std::fprintf(stderr, "usage: fusions_parity [--bench]\n"); return 2; }
411    std::printf("fusions_parity: V100 fusions vs their unfused pipelines, bitwise\n");
412    check_swilu_quantize(1, false, "swilu_quantize_q8_0 (fast silu, expert path)");
413    check_swilu_quantize(1, true, "swilu_quantize_q8_0_scaled (fast silu, expert path)");
414    check_swilu_quantize(0, false, "swilu_quantize_q8_0 (legacy silu, shared expert)");
415    check_swilu_quantize_q8_K();
416    check_images();
417    check_pair();
418    check_dual_gemv(0, "s_gemv_pair_silu (legacy silu) vs gemv+gemv+swilu");
419    check_dual_gemv(1, "s_gemv_pair_silu (fast silu) vs gemv+gemv+swilu");
420    check_dual_gemv(2, "s_gemv_pair_silu (native silu) vs gemv+gemv+swilu");
421    if (bench) run_bench();
422    std::printf(g_fail ? "fusions_parity: %d failures\n" : "fusions_parity: 0 failures\n", g_fail);
423    return g_fail ? 1 : 0;
424}