okami.mom/strata

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

waveringanaedge0-strata: add the engine for Edge0 145B and Edge0 120Bfd19246dc

main
41.5 KiB737 linesraw
1// Native (GGUF-form) experts: ggml activation quantizers and format-specific CPU row kernels.
2// Default keeps the existing dispatch. WindowInvariant preserves that dispatch's single-token arithmetic
3// while retaining shared weight decoding where the grouped kernel gives the same result.
4#include "strata/kernels/cpu/native_expert.hpp"
5#include "strata/kernels/cpu/expert.hpp"
6#include "strata/kernels/cpu/iq_avx512.hpp"
7#include "strata/kernels/cpu/iq_avx2.hpp"
8#include "strata/kernels/cpu/iq_avx1.hpp"
9#include "strata/kernels/cpu/kq_avx2.hpp"
10#include "strata/kernels/cpu/nvfp4_avx2.hpp"
11#include "strata/kernels/cpu/nvfp4_avx512.hpp"
12#include "strata/kernels/cpu/expert_layout.hpp"
13#include "strata/kernels/cpu/hadamard.hpp"
14#include "strata/kernels/cpu/tq_cpu.hpp"
15
16// The weight types' dot products, the activation quantizers and the row geometry: the Strata quant layer (STRATA_OWN_CPU_QUANT, the default build, no
17// ggml-cpu in the pool) or, as before, ggml-cpu's type traits (pool_quant.hpp).
18#include "strata/kernels/cpu/pool_quant.hpp"
19
20#include <algorithm>
21#include <cmath>
22#include <cstdio>
23#include <cstdlib>
24#include <cstring>
25#include <map>
26#include <mutex>
27#include <string>
28#include <vector>
29
30namespace strata::kernels::cpu {
31namespace {
32
33using namespace pq;   // Tr, traits(), row_size_of(), blck_size_of(), flavour_*(), layer_init(), kTQ8_0 ... (pool_quant.hpp)
34
35// Do the trellis rows take Strata's own kernels (tq_cpu.hpp)?  Always, unless STRATA_NO_TQ_KERNELS=1 asks for ggml-cpu's
36// vec_dot AND this ggml has the type (the agentionai fork; an upstream ggml does not, and must never be asked about it).
37bool tq_rows_own(int type) {
38    if (!tq_supported(type)) return false;
39    if (tq_kernels_enabled()) return true;
40#if defined(STRATA_OWN_CPU_QUANT)
41    return !traits(type).has_dot();   // the layer has no trellis dot: always the trellis kernels' own rows
42#else
43    return type >= (int) GGML_TYPE_COUNT || !traits(type).has_dot();
44#endif
45}
46
47// Window-invariant arithmetic (NativeArithmetic::WindowInvariant): the reference rows below reproduce ggml-cpu's one-token dot bit for bit
48// only if that ggml was built to sum the same way (contraction of the scalar tail into an FMA, for one, depends on the build). So each
49// reference is checked once against the linked ggml on synthetic blocks, odd block counts included, and a mismatch sends the invariant
50// path to ggml's own one-token dot per token (slower, never different).
51bool iq4nl_reference_ok() {
52    static const bool ok = [] {
53        const Tr tr = traits(kTIq4nl);
54        const Tr t8 = traits(kTQ8_0);
55        if (!tr.has_dot() || !t8.has_from_float()) return false;
56        uint32_t seed = 4242u;
57        auto rnd = [&seed] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
58        for (const int n : {64, 96, 160, 640}) {   // even and odd numbers of blocks: the odd one ends in the scalar tail
59            constexpr int rows = 16;
60            const size_t rb = row_size_of(kTIq4nl, (int64_t) n), nb = (size_t) n / 32, bb = rb / nb;
61            std::vector<uint8_t> w((size_t) rows * rb), act(row_size_of(kTQ8_0, (int64_t) n));
62            for (auto& b : w) b = (uint8_t) rnd();
63            for (int r = 0; r < rows; ++r)
64                for (size_t k = 0; k < nb; ++k) { uint8_t* d = &w[(size_t) r * rb + k * bb]; d[0] = 0x66; d[1] = 0x2e; }
65            std::vector<float> x((size_t) n);
66            for (auto& v : x) v = ((float) (rnd() % 2001) - 1000.f) * 0.001f;
67            t8.from_float(x.data(), act.data(), n);
68            std::vector<float> got((size_t) rows), want((size_t) rows);
69            const void* hp[1] = {act.data()};
70            float* op[1] = {got.data()};
71            iq4nl_reference_rows(w.data(), rb, n, hp, 1, op, 0, rows);
72            for (int r = 0; r < rows; ++r) tr.dot(n, &want[(size_t) r], w.data() + (size_t) r * rb, act.data());
73            if (std::memcmp(got.data(), want.data(), got.size() * sizeof(float)) != 0) {
74                std::fprintf(stderr, "strata: the window-invariant IQ4_NL rows differ from this ggml-cpu's dot (n=%d): its dot runs per token instead\n", n);
75                return false;
76            }
77        }
78        return true;
79    }();
80    return ok;
81}
82
83// The same check for the AVX-512 accumulation of IQ3_XXS / IQ3_S / IQ2_S gate/up rows (types 18, 21, 22), SwiGLU included.
84bool iq512_reference_ok(int type) {
85    static std::mutex mu;
86    static std::map<int, bool> memo;
87    std::lock_guard<std::mutex> lk(mu);
88    if (const auto it = memo.find(type); it != memo.end()) return it->second;
89    bool ok = false;
90    const Tr tr = traits(type);
91    const Tr t8 = traits(kTQ8_K);
92    if (tr.has_dot() && t8.has_from_float()) {
93        constexpr int n = 2560, rows = 24;
94        const size_t rb = row_size_of(type, n), nb = (size_t) n / 256, bb = rb / nb;
95        std::vector<uint8_t> w((size_t) 2 * rows * rb), act(row_size_of(kTQ8_K, n));
96        uint32_t seed = 777u;
97        auto rnd = [&seed] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
98        for (auto& b : w) b = (uint8_t) rnd();
99        for (int r = 0; r < 2 * rows; ++r)
100            for (size_t k = 0; k < nb; ++k) { uint8_t* d = &w[(size_t) r * rb + k * bb]; d[0] = 0x66; d[1] = 0x2e; }
101        std::vector<float> x(n);
102        for (auto& v : x) v = ((float) (rnd() % 2001) - 1000.f) * 0.001f;
103        t8.from_float(x.data(), act.data(), n);
104        std::vector<float> got((size_t) rows), want((size_t) rows);
105        const void* ap[1] = {act.data()};
106        float* fp[1] = {got.data()};
107        iq512_reference_gu_rows(type, w.data(), rb, (size_t) rows * rb, n, ap, 1, fp, 0, rows);
108        for (int r = 0; r < rows; ++r) {
109            float g = 0.f, u = 0.f;
110            tr.dot(n, &g, w.data() + (size_t) r * rb, act.data());
111            tr.dot(n, &u, w.data() + (size_t) rows * rb + (size_t) r * rb, act.data());
112            want[(size_t) r] = (g / (1.f + std::exp(-g))) * u;
113        }
114        ok = std::memcmp(got.data(), want.data(), got.size() * sizeof(float)) == 0;
115        if (!ok) std::fprintf(stderr, "strata: the window-invariant 512-bit rows of type %d differ from this ggml-cpu's dot: its dot runs per token instead\n", type);
116    }
117    memo[type] = ok;
118    return ok;
119}
120
121// 0.2.5: the 512-bit exact rows (iq512x_rows) take one token of an IQ3_XXS / IQ3_S expert only if this build's ggml-cpu dot gives
122// the same bits on synthetic blocks of that type (a ggml built for AVX-512 may carry another summation order): checked once a type.
123bool x512_one_ok(int type) {
124    static std::mutex mu;
125    static std::map<int, bool> memo;
126    std::lock_guard<std::mutex> lk(mu);
127    if (const auto it = memo.find(type); it != memo.end()) return it->second;
128    bool ok = false;
129    const Tr tr = traits(type);
130    const Tr t8 = traits(kTQ8_K);
131    if (tr.has_dot() && t8.has_from_float() && iq512x_supported(type)) {
132        constexpr int n = 2560, rows = 24;
133        const size_t rb = row_size_of(type, n), nb = (size_t) n / 256, bb = rb / nb;
134        std::vector<uint8_t> w((size_t) rows * rb), act(row_size_of(kTQ8_K, n));
135        uint32_t seed = 12345u;
136        auto rnd = [&seed] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
137        for (auto& b : w) b = (uint8_t) rnd();
138        for (int r = 0; r < rows; ++r)
139            for (size_t k = 0; k < nb; ++k) { uint8_t* d = &w[(size_t) r * rb + k * bb]; d[0] = 0x66; d[1] = 0x2e; }   // d = ~0.1 (f16)
140        std::vector<float> x(n);
141        for (auto& v : x) v = ((float) (rnd() % 2001) - 1000.f) * 0.001f;
142        t8.from_float(x.data(), act.data(), n);
143        std::vector<float> got((size_t) rows), want((size_t) rows);
144        const void* ap[1] = {act.data()};
145        float* op[1] = {got.data()};
146        iq512x_rows(type, w.data(), rb, n, ap, 1, op, 0, rows);
147        for (int r = 0; r < rows; ++r) tr.dot(n, &want[(size_t) r], w.data() + (size_t) r * rb, act.data());
148        ok = std::memcmp(got.data(), want.data(), got.size() * sizeof(float)) == 0;
149        if (!ok) std::fprintf(stderr, "strata: the 512-bit exact one-token rows of type %d differ from this ggml-cpu's dot: not used\n", type);
150    }
151    memo[type] = ok;
152    return ok;
153}
154
155// 0.2.12: the 512-bit Q5_0 down rows (q50_512_down_rows) take the type only if, on synthetic blocks (even and odd block counts, three tokens), they
156// give the bits of ggml-cpu's dot on this build.
157// The same check for the fused gate/up rows (iq512_fused_gu_rows) of an i-quant type: ggml-cpu's dot, SwiGLU included, bit for bit.
158bool iq512f_ok(int type) {
159    static std::mutex mu;
160    static std::map<int, bool> memo;
161    std::lock_guard<std::mutex> lk(mu);
162    if (const auto it = memo.find(type); it != memo.end()) return it->second;
163    bool ok = false;
164    const Tr tr = traits(type);
165    const Tr t8 = traits(kTQ8_K);
166    if (tr.has_dot() && t8.has_from_float() && iq512f_supported(type)) {
167        constexpr int n = 2560, rows = 24, nt = 3;
168        const size_t rb = row_size_of(type, n), nb = (size_t) n / 256, bb = rb / nb;
169        std::vector<uint8_t> w((size_t) 2 * rows * rb);
170        uint32_t seed = 4711u + (uint32_t) type;
171        auto rnd = [&seed] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
172        for (auto& b : w) b = (uint8_t) rnd();
173        for (int r = 0; r < 2 * rows; ++r)
174            for (size_t k = 0; k < nb; ++k) { uint8_t* d = &w[(size_t) r * rb + k * bb]; d[0] = 0x66; d[1] = 0x2e; }
175        std::vector<std::vector<uint8_t>> act(nt, std::vector<uint8_t>(row_size_of(kTQ8_K, n)));
176        const void* ap[nt];
177        for (int t = 0; t < nt; ++t) {
178            std::vector<float> x(n);
179            for (auto& v : x) v = ((float) (rnd() % 2001) - 1000.f) * 0.001f;
180            t8.from_float(x.data(), act[(size_t) t].data(), n);
181            ap[t] = act[(size_t) t].data();
182        }
183        std::vector<float> got((size_t) nt * rows), want((size_t) nt * rows);
184        float* fp[nt];
185        for (int t = 0; t < nt; ++t) fp[t] = &got[(size_t) t * rows];
186        iq512_fused_gu_rows(type, w.data(), rb, (size_t) rows * rb, n, ap, nt, fp, 0, rows);
187        for (int t = 0; t < nt; ++t)
188            for (int r = 0; r < rows; ++r) {
189                float g = 0.f, u = 0.f;
190                tr.dot(n, &g, w.data() + (size_t) r * rb, ap[t]);
191                tr.dot(n, &u, w.data() + (size_t) rows * rb + (size_t) r * rb, ap[t]);
192                want[(size_t) t * rows + r] = (g / (1.f + std::exp(-g))) * u;
193            }
194        ok = std::memcmp(got.data(), want.data(), got.size() * sizeof(float)) == 0;
195        if (!ok) std::fprintf(stderr, "strata: the fused 512-bit gate/up rows of type %d differ from this ggml-cpu's dot: not used\n", type);
196    }
197    memo[type] = ok;
198    return ok;
199}
200
201bool q50_512_ok() {
202    static const bool ok = [] {
203        const Tr tr = traits(kTQ5_0);
204        const Tr t8 = traits(kTQ8_0);
205        if (!cpu_vnni_ok() || !tr.has_dot() || !t8.has_from_float()) return false;
206        uint32_t seed = 31337u;
207        auto rnd = [&seed] { seed = seed * 1664525u + 1013904223u; return seed >> 8; };
208        for (const int n : {96, 640}) {
209            constexpr int rows = 16, nt = 3;
210            const size_t rb = row_size_of(kTQ5_0, (int64_t) n), nb = (size_t) n / 32, bb = rb / nb;
211            std::vector<uint8_t> w((size_t) rows * rb);
212            for (auto& b : w) b = (uint8_t) rnd();
213            for (int r = 0; r < rows; ++r)
214                for (size_t k = 0; k < nb; ++k) { uint8_t* d = &w[(size_t) r * rb + k * bb]; d[0] = 0x66; d[1] = 0x2e; }
215            std::vector<std::vector<uint8_t>> act(nt, std::vector<uint8_t>(row_size_of(kTQ8_0, n)));
216            const void* hp[nt];
217            for (int t = 0; t < nt; ++t) {
218                std::vector<float> x((size_t) n);
219                for (auto& v : x) v = ((float) (rnd() % 2001) - 1000.f) * 0.001f;
220                t8.from_float(x.data(), act[(size_t) t].data(), n);
221                hp[t] = act[(size_t) t].data();
222            }
223            std::vector<float> got((size_t) nt * rows), want((size_t) nt * rows);
224            float* gp[nt];
225            for (int t = 0; t < nt; ++t) gp[t] = &got[(size_t) t * rows];
226            q50_512_down_rows(w.data(), rb, n, hp, nt, gp, 0, rows);
227            for (int r = 0; r < rows; ++r)
228                for (int t = 0; t < nt; ++t) tr.dot(n, &want[(size_t) t * rows + r], w.data() + (size_t) r * rb, hp[t]);
229            if (std::memcmp(got.data(), want.data(), got.size() * sizeof(float)) != 0) {
230                std::fprintf(stderr, "strata: the 512-bit Q5_0 down rows differ from this ggml-cpu's dot: not used\n");
231                return false;
232            }
233        }
234        return true;
235    }();
236    return ok;
237}
238
239void init_once() {
240    static std::once_flag once;
241    std::call_once(once, [] { layer_init(); });
242}
243
244}  // namespace
245
246bool native_experts_available() noexcept { return true; }
247
248// The "is this format handled by that kernel" questions, answered here and not next to the kernels: iq_avx512.cpp is
249// compiled for AVX-512 and iq_avx2.cpp / kq_avx2.cpp for AVX2, and native_gu_rows asks these on every CPU before it
250// has checked what the CPU can run.  A function in a wide-ISA file may use that ISA anywhere in its body, so an
251// answer that is only a comparison must not come from one (#795).
252bool iq512_supported(int type) noexcept {
253    return type == 16 || type == 17 || type == 18 || type == 21 || type == 22;
254}
255
256bool iq256_supported(int type) noexcept {
257    return type == 16 || type == 17 || type == 18 || type == 21 || type == 22 || type == 23;
258}
259
260bool kq256_supported(int type) noexcept { return type == 12 || type == 7 || type == 8; }
261
262bool native_fmt(int gu_type, int d_type, int64_t n_embd, int64_t n_ff, NativeFmt& f, std::string& err) {
263    init_once();
264    // The trellis types (Gyro, ggml types 144-146) are not in a ggml built from upstream (and an upstream ggml indexes its
265    // type table by the id, so asking it about 144 reads past the table): their geometry and activation format (Q8_0)
266    // are known here, and their rows run on tq_cpu.hpp.
267    const bool tq_g = tq_supported(gu_type), tq_d = tq_supported(d_type);
268    const Tr tg = tq_g ? Tr{} : traits(gu_type);
269    const Tr td = tq_d ? Tr{} : traits(d_type);
270    if ((!tq_g && !tg.has_dot()) || (!tq_d && !td.has_dot())) {
271        err = "native experts: " STRATA_POOL_LIB " has no dot product for type " + std::to_string(!tq_g && !tg.has_dot() ? gu_type : d_type);
272        return false;
273    }
274    const int vdt_g = tq_g ? kTQ8_0 : tg.vec_dot_type;
275    const int vdt_d = tq_d ? kTQ8_0 : td.vec_dot_type;
276    const Tr ag = traits(vdt_g);
277    const Tr ad = traits(vdt_d);
278    if (!ag.has_from_float() || !ad.has_from_float()) {
279        err = "native experts: " STRATA_POOL_LIB " cannot quantize an activation for this layer";
280        return false;
281    }
282    if ((tq_g ? n_embd % kTqBlock : n_embd % blck_size_of(gu_type)) ||
283        (tq_d ? n_ff % kTqBlock : n_ff % blck_size_of(d_type)) ||
284        n_embd % blck_size_of(vdt_g) || n_ff % blck_size_of(vdt_d)) {
285        err = "native experts: expert geometry is not whole blocks";
286        return false;
287    }
288    f.gu_type = gu_type;
289    f.d_type = d_type;
290    f.gu_act = (int) vdt_g;
291    f.d_act = (int) vdt_d;
292    f.n_embd = n_embd;
293    f.n_ff = n_ff;
294    f.gu_row = tq_g ? (size_t) (n_embd / kTqBlock) * tq_block_bytes(gu_type) : row_size_of(gu_type, n_embd);
295    f.d_row = tq_d ? (size_t) (n_ff / kTqBlock) * tq_block_bytes(d_type) : row_size_of(d_type, n_ff);
296    f.up_off = f.gu_row * (size_t) n_ff;
297    f.down_off = 2 * f.up_off;
298    f.bytes = f.down_off + f.d_row * (size_t) n_embd;
299    // NVFP4 in any projection: one scale tail per blob; a mixed expert (e.g. NVFP4 gate/up + Q8_0 down) has 1.0 for
300    // the other projection's scale (tools/iq_pack.py), so every path below multiplies by the tail unconditionally
301    if (gu_type == kNvfp4Type || d_type == kNvfp4Type) {
302        f.tail_off = f.bytes;
303        f.bytes += kNvfp4Tail;
304    }
305    f.act_bytes = row_size_of(vdt_g, n_embd);
306    f.h_bytes = row_size_of(vdt_d, n_ff);
307    if (f.act_bytes > kNativeActBytes || f.h_bytes > kNativeHBytes) {
308        err = "native experts: activation larger than the pool's buffers";
309        return false;
310    }
311    return true;
312}
313
314namespace {
315// Q8_K (the activations of every i-quant row): ggml-cpu's x86 quantizer is the scalar reference, ~3 us per token
316// and layer on the host before the pool can start; q8k_quant_avx2 writes the same bytes.  cpu_avx2_ok() too:
317// iq_avx2.cpp is compiled for AVX2 (an AVX-only CPU, or STRATA_FORCE_ISA=avx, keeps ggml's).  STRATA_NO_Q8K_AVX2=1:
318// ggml's on any CPU.
319bool q8k_avx2(int type) {
320    static const bool on = cpu_avx2_ok() && std::getenv("STRATA_NO_Q8K_AVX2") == nullptr;
321    return on && type == (int) kTQ8_K;
322}
323// the older CPUs' copy (iq_avx1.cpp): byte-identical like the AVX-2 one, and only where AVX2 is absent (same
324// policy as STRATA_NO_IQ128 above; STRATA_NO_Q8K_AVX1=1 keeps ggml's scalar reference on those CPUs).
325bool q8k_avx1(int type) {
326    static const bool on = !cpu_avx2_ok() && cpu_avx1_ok() && std::getenv("STRATA_NO_Q8K_AVX1") == nullptr;
327    return on && type == (int) kTQ8_K;
328}
329}  // namespace
330
331// STRATA_ACT_DUMP=<prefix> (research, off by default): the second moments of the vectors that enter the CPU experts, in the
332// basis the experts use (after the Hadamard rotation of a folded layer): per layer the gate/up input (the token's hidden
333// state, once per token) and the down input (the SwiGLU output, once per routed expert).  STRATA_ACT_DUMP_LAYERS=10,20 picks
334// the layers (default none), STRATA_ACT_DUMP_COV=1 adds the full covariance.  Files <prefix>_L<l>_<gu|dn>.bin: int64 n,
335// int64 count, double sum x^2 [n], (double sum x x^T [n*n]); rewritten every 2000 records and at exit.
336namespace {
337struct ActAcc {
338    int64_t n = 0, count = 0;
339    std::vector<double> sq, cov;
340};
341struct ActDump {
342    bool on = false, cov = false;
343    std::string prefix;
344    std::vector<int> layers;
345    std::map<std::pair<int, int>, ActAcc> acc;
346    std::mutex mu;
347    int64_t records = 0;
348    ActDump() {
349        const char* p = std::getenv("STRATA_ACT_DUMP");
350        if (p == nullptr || *p == 0) return;
351        prefix = p;
352        cov = std::getenv("STRATA_ACT_DUMP_COV") != nullptr && std::atoi(std::getenv("STRATA_ACT_DUMP_COV")) != 0;
353        if (const char* l = std::getenv("STRATA_ACT_DUMP_LAYERS")) {
354            std::string s(l);
355            size_t at = 0;
356            while (at < s.size()) {
357                layers.push_back(std::atoi(s.c_str() + at));
358                const size_t c = s.find(',', at);
359                if (c == std::string::npos) break;
360                at = c + 1;
361            }
362        }
363        on = !layers.empty();
364        if (on) std::atexit([] { act_dump_flush(); });
365    }
366    static void act_dump_flush();
367};
368ActDump& act_dump() {
369    static ActDump* d = new ActDump;   // never destroyed: the atexit flush must still find it
370    return *d;
371}
372void ActDump::act_dump_flush() {
373    ActDump& d = act_dump();
374    std::lock_guard<std::mutex> lk(d.mu);
375    for (auto& kv : d.acc) {
376        const std::string path = d.prefix + "_L" + std::to_string(kv.first.first) + (kv.first.second ? "_dn.bin" : "_gu.bin");
377        FILE* f = std::fopen((path + ".tmp").c_str(), "wb");
378        if (!f) continue;
379        std::fwrite(&kv.second.n, 8, 1, f);
380        std::fwrite(&kv.second.count, 8, 1, f);
381        std::fwrite(kv.second.sq.data(), 8, kv.second.sq.size(), f);
382        if (!kv.second.cov.empty()) std::fwrite(kv.second.cov.data(), 8, kv.second.cov.size(), f);
383        std::fclose(f);
384        std::rename((path + ".tmp").c_str(), path.c_str());
385    }
386}
387void act_dump_record(int layer, int kind, const float* v, int64_t n) {
388    ActDump& d = act_dump();
389    if (std::find(d.layers.begin(), d.layers.end(), layer) == d.layers.end()) return;
390    std::lock_guard<std::mutex> lk(d.mu);
391    ActAcc& a = d.acc[{layer, kind}];
392    if (a.n == 0) {
393        a.n = n;
394        a.sq.assign((size_t) n, 0.0);
395        if (d.cov) a.cov.assign((size_t) n * (size_t) n, 0.0);
396    }
397    for (int64_t i = 0; i < n; ++i) a.sq[(size_t) i] += (double) v[i] * v[i];
398    if (d.cov)
399        for (int64_t i = 0; i < n; ++i) {
400            double* row = a.cov.data() + (size_t) i * (size_t) n;
401            const double vi = v[i];
402            for (int64_t j = 0; j < n; ++j) row[j] += vi * v[j];
403        }
404    ++a.count;
405    if (++d.records % (d.cov ? 400000 : 2000) == 0) {
406        d.mu.unlock();
407        ActDump::act_dump_flush();
408        d.mu.lock();
409    }
410}
411}  // namespace
412
413void native_quant_act(const NativeFmt& f, const float* x, void* dst) {
414    if (f.had_gu) {   // Hadamard-folded gate/up: quantize H (s * x), never x itself (hadamard.hpp).
415        thread_local std::vector<float> tmp;   // Column-scaled packs need no multiply here: the D stage's rows
416        // arrive already scaled (lat_colscale_rows in verify.cpp / prefill.cpp multiplies x by 1/s at the source).
417        tmp.assign(x, x + f.n_embd);
418        hadamard_rotate(tmp.data(), (int) f.n_embd, f.had_block, f.gu_signs);
419        x = tmp.data();
420    }
421    if (act_dump().on) act_dump_record(f.layer, 0, x, f.n_embd);
422    if (q8k_avx2(f.gu_act)) { q8k_quant_avx2(x, dst, f.n_embd); return; }
423    if (q8k_avx1(f.gu_act)) { q8k_quant_avx1(x, dst, f.n_embd); return; }
424    traits(f.gu_act).from_float(x, dst, f.n_embd);
425}
426
427void native_quant_h(const NativeFmt& f, const float* h, void* dst) {
428    if (f.had_d) {
429        thread_local std::vector<float> tmp;
430        tmp.assign(h, h + f.n_ff);
431        hadamard_rotate(tmp.data(), (int) f.n_ff, f.had_block, f.d_signs);
432        h = tmp.data();
433    }
434    if (act_dump().on) act_dump_record(f.layer, 1, h, f.n_ff);
435    if (q8k_avx2(f.d_act)) { q8k_quant_avx2(h, dst, f.n_ff); return; }
436    if (q8k_avx1(f.d_act)) { q8k_quant_avx1(h, dst, f.n_ff); return; }
437    traits(f.d_act).from_float(h, dst, f.n_ff);
438}
439
440int native_gu_mt_min(int gu_type) {
441    // #152: from how many tokens the multi-token kernels run (ggml's vec_dot below that).  The default 2 is the
442    // measured-fastest rule, but a token's expert rows then round differently alone than in a group, so greedy output
443    // can depend on how many drafts a verify window held.  STRATA_IQ_MT_MIN=1 (opt-in, 0.1.30) uses the multi-token
444    // kernels for every group: output independent of the drafting, at a measured -1..-3% decode on IQ3_S (AVX-512).
445    static const char* env = std::getenv("STRATA_IQ_MT_MIN");
446    static const int mt_min = env ? std::atoi(env) : 2;
447    // IQ3_S where the AVX-2 kernel gathers its grid (cpu_gather_fast, unless STRATA_IQ256_GATHER=0) on a CPU without
448    // AVX-512: there that kernel beats ggml's dot for ONE token too (14900KF P-core, an expert's gate/up rows: 0.34 ->
449    // 0.19 ms; its E-cores, which keep the scalar decode: 0.71 -> 0.68), and in decode most CPU experts serve one token
450    // of the window.  So IQ3_S takes it for every group, and those rows no longer depend on the drafting.  A machine-
451    // wide rule, not a per-core one: every core rounds an expert the same.  Every probe here is in expert_layout.cpp
452    // and cpu_avx2_ok() comes first, so no AVX2 code runs before the check.  STRATA_IQ_MT_MIN set keeps its rule.
453    // Opt-in (STRATA_IQ3S_MT1=1): it changes a lone token's IQ3_S rounding on those CPUs, so the default stays 0.1.39's.
454    static const bool iq3s_one = env == nullptr && std::getenv("STRATA_IQ3S_MT1") != nullptr && cpu_avx2_ok() && std::getenv("STRATA_NO_IQ256") == nullptr &&
455                                 !cpu_avx512_ok() && iq256_gather_setting() != 0 && cpu_gather_fast();
456    return iq3s_one && gu_type == 21 ? 1 : mt_min;
457}
458
459template <NativeArithmetic Arithmetic>
460void native_gu_rows_impl(const NativeFmt& f, const uint8_t* blob, const void* const* act, int nt, float* const* ff,
461                    int r0, int r1) {
462    // the multi-token kernels decode the weights once for all tokens: 2.0-2.4x ggml-cpu at three tokens, no faster
463    // at one (all are bound by the codebook lookups, ~5 GB/s per core), measured by native_expert_parity.  AVX-512
464    // first, then the AVX-2 one (Zen 2/3, Intel 12th-14th gen).  STRATA_NO_IQ512 drops an AVX-512 CPU to the
465    // AVX-2 kernel, STRATA_NO_IQ256 drops the AVX-2 kernel; ggml-cpu's single-token vec_dot is reached only with
466    // both set (and on a CPU without AVX-512, STRATA_NO_IQ512 changes nothing).
467    // 0.2.12 (PR 5120): AVX-512 F/DQ/BW/VL + VNNI is what iq_avx512.cpp runs on (its object holds no VBMI instruction); VBMI is not required.
468    static const bool avx512 = cpu_vnni_ok() && std::getenv("STRATA_NO_IQ512") == nullptr;
469    // STRATA_NO_IQ256 drops the AVX-2 kernel.  cpu_avx2_ok() is ALSO required: iq_avx2.cpp and kq_avx2.cpp
470    // are compiled /arch:AVX2 and use AVX2 and FMA3, so calling them on a CPU without either is an
471    // illegal instruction, not a slow path.  The gate sat next to `avx512` above, which does test
472    // cpu_avx512_ok(), and that asymmetry is what let a pre-Haswell CPU reach iq256_gu_rows.
473    // Without it this falls through to ggml-cpu's vec_dot below, which is compiled for whatever
474    // baseline the build selected (AVX1 here) and covers the same types - IQ2_XXS and IQ2_S among
475    // them.  That path loops over tokens itself, so it is correct for any `nt`, not just one.
476    static const bool avx2 = cpu_avx2_ok() && std::getenv("STRATA_NO_IQ256") == nullptr;
477    // The older-CPU tier (STRATA_ISA_FLOOR=avx builds): without it every expert on such a CPU runs on ggml-cpu's
478    // scalar _generic dots.  Only where AVX2 is absent, so a modern CPU's rounding never moves (and
479    // STRATA_NO_IQ256=1 there still means ggml, as before).  iq_avx1.cpp is compiled for AVX and gated on
480    // cpu_avx1_ok().  Measured on an E5-2470 v2 (iq_avx1_parity --bench): 1.2-3.1x ggml's dot from two tokens on,
481    // at one token ggml's auto-vectorized generic loop matches the 128-bit kernel - so the same #152 mt_min rule
482    // as the AVX-2 kernels, and STRATA_IQ_MT_MIN=1 takes the kernel for one token too (its rows are then
483    // independent of the drafting: iq_avx1_parity checks every width bit for bit).
484    static const bool avx1 = !cpu_avx2_ok() && cpu_avx1_ok() && std::getenv("STRATA_NO_IQ128") == nullptr;
485    // The exact rows are ggml-cpu's AVX2 single-token dot bit for bit, so they need ggml-cpu itself built for AVX2 and
486    // not AVX-512 (ggml_cpu_has_*): a STRATA_ISA_FLOOR build runs ggml's AVX1/SSE dots, a native build on an AVX-512 CPU
487    // run with STRATA_FORCE_AVX2=1 runs its AVX-512 one, and both round differently (#2215's guard).
488    static const bool exact_one = [] {
489        const char* v = std::getenv("STRATA_CPU_IQ_ALL_EXACT1");
490        return v && v[0] == '1' && v[1] == 0 && flavour_avx2() && !flavour_avx512();
491    }();
492    // only where ggml's dot runs today (below the #152 multi-token rule): with STRATA_IQ_MT_MIN=1 a lone token keeps the
493    // multi-token kernel, so a token's rows still do not depend on how many tokens share its expert
494    if (exact_one && nt == 1 && nt < native_gu_mt_min(f.gu_type) && avx2 && !cpu_avx512_ok() &&
495        (f.gu_type == 16 || f.gu_type == 17 || f.gu_type == 18 || f.gu_type == 21 || f.gu_type == 22)) {
496        static std::once_flag announced;
497        std::call_once(announced, [] { std::fprintf(stderr, "strata ab: GATE=STRATA_CPU_IQ_ALL_EXACT1 active=1\n"); });
498        iq256_gu_rows_exact_one(f.gu_type, blob, f.gu_row, f.up_off, (int) f.n_embd, act[0], ff[0], r0, r1);
499        return;
500    }
501    // 0.2.5 (AVX-512 CPUs where a gather is cheap: Intel Sapphire Rapids and later): ONE token of an IQ3_XXS / IQ3_S expert on the
502    // 512-bit exact rows, ggml-cpu's single-token AVX2 dot bit for bit with the grid words gathered: 1.3x ggml's dot on a Xeon 8468
503    // (x512_bench), the same output.  Where the multi-token rule (#152) already sends one token to the multi-token kernel
504    // (STRATA_IQ_MT_MIN=1) that stays; STRATA_NO_IQ512X1=1 turns this off; the first use checks the bits against this ggml.
505    static const bool x512_one = avx512 && iq512_gather_on() && std::getenv("STRATA_NO_IQ512X1") == nullptr;
506    if (x512_one && nt == 1 && nt < native_gu_mt_min(f.gu_type) && f.tail_off == 0 && (f.gu_type == 18 || f.gu_type == 21) &&
507        x512_one_ok(f.gu_type)) {
508        const int n1 = (int) f.n_embd;
509        float g[64], u[64];
510        float* gp[1] = {g};
511        float* up[1] = {u};
512        for (int c0 = r0; c0 < r1; c0 += 64) {
513            const int c1 = std::min(r1, c0 + 64);
514            iq512x_rows(f.gu_type, blob + (size_t) c0 * f.gu_row, f.gu_row, n1, act, 1, gp, 0, c1 - c0);
515            iq512x_rows(f.gu_type, blob + f.up_off + (size_t) c0 * f.gu_row, f.gu_row, n1, act, 1, up, 0, c1 - c0);
516            for (int r = c0; r < c1; ++r) ff[0][r] = (g[r - c0] / (1.f + std::exp(-g[r - c0]))) * u[r - c0];
517        }
518        return;
519    }
520    // 0.2.12: IQ4_XS on an AVX-512 CPU used to run on ggml-cpu's dot for every token (the 256-bit kernel below is AVX2-only).  The 512-bit rows sum
521    // as that dot sums (the integer lanes, the f16 scale times y.d, eight float lanes, its hsum), so each token's row is ggml's bit for bit
522    // at any number of tokens, checked once against this ggml.  STRATA_NO_IQ4XS512=1 keeps ggml's dot; STRATA_IQ4XS512_MIN=N: from N tokens on.
523    static const bool iq4xs512 = avx512 && std::getenv("STRATA_NO_IQ4XS512") == nullptr;
524    static const int iq4xs512_min = [] { const char* e = std::getenv("STRATA_IQ4XS512_MIN"); return e ? std::atoi(e) : 1; }();
525    if (iq4xs512 && f.gu_type == 23 && nt >= iq4xs512_min && f.tail_off == 0 && iq512_reference_ok(23)) {
526        iq512_reference_gu_rows(23, blob, f.gu_row, f.up_off, (int) f.n_embd, act, nt, ff, r0, r1);
527        return;
528    }
529    // 0.2.12, OPT-IN (N): STRATA_IQ512_FUSED_MAX=M (default 0 = off) sums the gate and up rows of an IQ3_XXS / IQ3_S / IQ2_S expert (types
530    // STRATA_IQ512_FUSED_TYPES, default 18,21,22) for 2..M tokens in ggml-cpu's order, in one pass over the activations (iq512_fused_gu_rows, checked
531    // once against this ggml).  Each token's rows are then what ggml's one-token dot gives it, whatever the group, instead of the 16-lane sums of the
532    // default rows: different rounding (N), measured 1.27-1.34x the default rows at 2-3 tokens on Zen 4 and a loss at one token (so one token keeps ggml's dot).
533    static const int fused_max = [] { const char* e = std::getenv("STRATA_IQ512_FUSED_MAX"); return e ? std::atoi(e) : 0; }();
534    if (fused_max >= 2 && avx512 && nt >= 2 && nt <= fused_max && f.tail_off == 0 && f.gu_type > 0 && f.gu_type < 32) {
535        static const unsigned fused_types = [] {
536            unsigned m = 0;
537            const char* e = std::getenv("STRATA_IQ512_FUSED_TYPES");
538            const std::string v = e != nullptr && e[0] != '\0' ? e : "18,21,22";
539            size_t at = 0;
540            while (at < v.size()) {
541                const int t = std::atoi(v.c_str() + at);
542                if (t > 0 && t < 32) m |= 1u << t;
543                const size_t c = v.find(',', at);
544                if (c == std::string::npos) break;
545                at = c + 1;
546            }
547            return m;
548        }();
549        if (((fused_types >> f.gu_type) & 1u) && iq512f_supported(f.gu_type) && iq512f_ok(f.gu_type)) {
550            iq512_fused_gu_rows(f.gu_type, blob, f.gu_row, f.up_off, (int) f.n_embd, act, nt, ff, r0, r1);
551            return;
552        }
553    }
554    const int mt_min = native_gu_mt_min(f.gu_type);   // #152
555    const bool singleton_iq = Arithmetic == NativeArithmetic::WindowInvariant && mt_min > 1;
556    // Unsloth UD-Q4_K_XL's Q4_K gate/up: the multi-token kernel is bit-exact against ggml's per-token dot (any group
557    // size, no #152 rule).  Opt-in, STRATA_KQ256=1: measured no faster in the engine (a window's expert groups hold
558    // ~1.4 tokens and the weights stay in L1 across ggml's per-token calls; 1.01-1.13x in native_expert_parity).
559    static const bool kq = [] { const char* v = std::getenv("STRATA_KQ256"); return cpu_avx2_ok() && v != nullptr && std::atoi(v) != 0; }();
560    // KQ matches the ggml AVX2/FMA dot; other builds keep their own singleton arithmetic.
561    if (kq && f.gu_type == 12 && nt >= 2 &&
562        (Arithmetic == NativeArithmetic::Default || (flavour_avx2()))) {   // one token: ggml's own dot below (the same bits, less overhead)
563        kq256_gu_rows(f.gu_type, blob, f.gu_row, f.up_off, (int) f.n_embd, act, nt, ff, r0, r1);
564        return;
565    }
566    // The trellis types (TQ2_T / TQK6 / TQK7): Strata's own kernels (tq_cpu.hpp), for every group size - they decode
567    // a block once for all the tokens and give each token the same bits whatever the group (no #152 rule needed).
568    if (tq_rows_own(f.gu_type)) {
569        tq_gu_rows(tq_isa(), f.gu_type, blob, f.gu_row, f.up_off, (int) f.n_embd, act, nt, ff, r0, r1);
570        return;
571    }
572    // A format with only an AVX-2 kernel (IQ4_XS, #415) takes it on AVX-2 CPUs only: an AVX-512 CPU keeps ggml-cpu for
573    // it, as before (its rows would round differently).  Each kernel only for the formats it implements: falling
574    // through an empty switch would leave ff unwritten instead of falling back to ggml-cpu.
575    static const bool cpu512 = cpu_vnni_ok();
576    if (nt >= mt_min && (iq512_supported(f.gu_type) || (!cpu512 && iq256_supported(f.gu_type)))) {
577        if (singleton_iq) {
578#if defined(__x86_64__) || defined(_M_X64)
579            if (avx512 && flavour_avx2() &&
580                (f.gu_type == 18 || f.gu_type == 21 || f.gu_type == 22) && iq512_reference_ok(f.gu_type)) {
581                iq512_reference_gu_rows(f.gu_type, blob, f.gu_row, f.up_off, (int) f.n_embd, act, nt, ff, r0, r1);
582                return;
583            }
584#endif
585            // Otherwise use the singleton dot below, keeping its row-major weight reuse across tokens.
586        } else {
587            if (avx512 && iq512_supported(f.gu_type)) {
588                iq512_gu_rows(f.gu_type, blob, f.gu_row, f.up_off, (int) f.n_embd, act, nt, ff, r0, r1);
589                return;
590            }
591            if (avx2 && iq256_supported(f.gu_type)) {
592                iq256_gu_rows(f.gu_type, blob, f.gu_row, f.up_off, (int) f.n_embd, act, nt, ff, r0, r1);
593                return;
594            }
595        }
596    }
597    if (!singleton_iq && avx1 && nt >= mt_min && iq128_supported(f.gu_type)) {
598        iq128_gu_rows(f.gu_type, blob, f.gu_row, f.up_off, (int) f.n_embd, act, nt, ff, r0, r1);
599        return;
600    }
601    const int n = (int) f.n_embd;
602    // NVFP4: the per-expert global scales from the blob's tail (1 and 1 otherwise). s_down goes on the down output:
603    // folded into up it left the hidden near 1e-5, whose Q8_0 block scale is an FP16 subnormal (expert error 2-12%)
604    float sg = 1.f, su = 1.f;
605    if (f.tail_off) {
606        float tail[4];
607        std::memcpy(tail, blob + f.tail_off, sizeof tail);
608        sg = tail[0];
609        su = tail[1];
610    }
611    // NVFP4 in 512-bit lanes: ggml's arithmetic, blocks decoded once per window; 1.8x at one token, 3.7x at seven
612    // (nvfp4_avx512_parity). STRATA_NO_NVFP4_512 falls back to ggml-cpu.
613    static const bool nvfp4_512 = cpu_avx512_ok() && std::getenv("STRATA_NO_NVFP4_512") == nullptr;
614    if (f.gu_type == kNvfp4Type && nvfp4_512 && nvfp4_512_fits(n)) {
615        nvfp4_512_gu_rows(blob, f.gu_row, f.up_off, n, act, nt, ff, r0, r1, sg, su);
616        return;
617    }
618    // Without AVX-512: the same rows in 256-bit lanes, bit-equal to ggml-cpu's AVX2 dot below; 2.0-2.4x it at 4-8
619    // tokens, 2x under DRAM load with 6 threads (nvfp4_avx512_parity). STRATA_NO_NVFP4_256 falls back to ggml-cpu.
620    static const bool nvfp4_256 = cpu_avx2_ok() && std::getenv("STRATA_NO_NVFP4_256") == nullptr;
621    if (f.gu_type == kNvfp4Type && nvfp4_256 && nvfp4_256_fits(n)) {
622        nvfp4_256_gu_rows(blob, f.gu_row, f.up_off, n, act, nt, ff, r0, r1, sg, su);
623        return;
624    }
625    const Tr dot = traits(f.gu_type);
626    for (int r = r0; r < r1; ++r) {
627        const uint8_t* gr = blob + (size_t) r * f.gu_row;
628        const uint8_t* ur = blob + f.up_off + (size_t) r * f.gu_row;
629        for (int t = 0; t < nt; ++t) {
630            // g and u are read back after both calls (so the up row's dot starts while the gate row's result is still on its way)
631            // and each with its own 4-byte load: GCC on arm64 put them side by side and read both with one 8-byte ldp, a load
632            // that the two dots' separate 4-byte stores cannot forward to, so every row pair stalled until the stores drained
633            // (OWN-2N2: 2% of the Spark's gate/up time with the quant layer's 4-argument dot).  A volatile access is never
634            // paired.  The same arithmetic, the same bits.
635            float g = 0.f, u = 0.f;
636            dot.dot(n, &g, gr, act[t]);
637            dot.dot(n, &u, ur, act[t]);
638            const float gs = *static_cast<volatile const float*>(&g) * sg;
639            const float us = *static_cast<volatile const float*>(&u) * su;
640            ff[t][r] = (gs / (1.f + std::exp(-gs))) * us;
641        }
642    }
643}
644
645template <NativeArithmetic Arithmetic>
646void native_down_rows_impl(const NativeFmt& f, const uint8_t* blob, const void* const* hq, int nt, float* const* out,
647                      int r0, int r1) {
648    // IQ4_NL down rows: the AVX-2 multi-token kernel decodes the nibbles and absolutises the weights once per
649    // block instead of once per token; ggml-cpu's dot is single-token.  STRATA_NO_IQ4NL falls back to it.
650    static const bool iq4nl_mt = std::getenv("STRATA_NO_IQ4NL") == nullptr;
651    static const int mt_min = [] { const char* e = std::getenv("STRATA_IQ_MT_MIN"); return e ? std::atoi(e) : 2; }();
652    static const bool kq = [] { const char* v = std::getenv("STRATA_KQ256"); return v != nullptr && std::atoi(v) != 0; }();
653    // Both multi-token kernels below are /arch:AVX2 translation units (kq_avx2.cpp and iq_avx2.cpp),
654    // so a CPU without AVX2 has to reach ggml-cpu's vec_dot instead - same reasoning as the gate/up
655    // rows above, where `avx512` tested cpu_avx512_ok() and `avx2` did not.
656    if (tq_rows_own(f.d_type)) {   // the trellis types, as in native_gu_rows
657        tq_rows(tq_isa(), f.d_type, blob + f.down_off, f.d_row, (int) f.n_ff, hq, nt, out, r0, r1);
658        return;
659    }
660    if (cpu_avx2_ok() && kq && nt >= 2 && (f.d_type == 7 || f.d_type == 8) &&
661        (Arithmetic == NativeArithmetic::Default || (flavour_avx2()))) {   // Q5_1 / Q8_0 down: bit-exact, any group size
662        kq256_rows(f.d_type, blob + f.down_off, f.d_row, (int) f.n_ff, hq, nt, out, r0, r1);
663        return;
664    }
665    // 0.2.12: Q5_0 down rows (ggml-cpu's dot, once a token) in 512-bit lanes, the same bits (q50_512_ok checks that on first use): the weights
666    // of a block pair are decoded once for all the tokens.  STRATA_NO_Q50_512=1 keeps ggml's dot; STRATA_Q50_512_MIN=N: from N tokens on (default 2).
667    static const bool q50_512 = cpu_vnni_ok() && std::getenv("STRATA_NO_Q50_512") == nullptr;
668    static const int q50_min = [] { const char* e = std::getenv("STRATA_Q50_512_MIN"); return e ? std::atoi(e) : 2; }();
669    if (q50_512 && f.d_type == 6 && nt >= q50_min && f.tail_off == 0 && q50_512_ok()) {
670        q50_512_down_rows(blob + f.down_off, f.d_row, (int) f.n_ff, hq, nt, out, r0, r1);
671        return;
672    }
673    if (cpu_avx2_ok() && nt >= mt_min && f.d_type == 20 && iq4nl_mt) {   // #152: the same rule as the gate/up rows
674        if (Arithmetic == NativeArithmetic::Default || mt_min <= 1) {
675            iq4nl256_down_rows(blob + f.down_off, f.d_row, (int) f.n_ff, hq, nt, out, r0, r1);
676            return;
677        }
678#if defined(__x86_64__) || defined(_M_X64)
679        if (flavour_avx2() && iq4nl_reference_ok()) {
680            iq4nl_reference_rows(blob + f.down_off, f.d_row, (int) f.n_ff, hq, nt, out, r0, r1);
681            return;
682        }
683#endif
684    }
685    // the older-CPU tier: IQ4_NL and Q2_0 down rows in 128-bit (ggml-cpu's dots for both are scalar here)
686    static const bool avx1 = !cpu_avx2_ok() && cpu_avx1_ok() && std::getenv("STRATA_NO_IQ128") == nullptr;
687    if (avx1 && iq128_down_supported(f.d_type)) {
688        iq128_down_rows(f.d_type, blob + f.down_off, f.d_row, (int) f.n_ff, hq, nt, out, r0, r1);
689        return;
690    }
691    const int n = (int) f.n_ff;
692    float sd = 1.f;                                   // NVFP4: the expert's s_down (see native_gu_rows)
693    if (f.tail_off) std::memcpy(&sd, blob + f.tail_off + 2 * sizeof(float), sizeof sd);
694    static const bool nvfp4_512 = cpu_avx512_ok() && std::getenv("STRATA_NO_NVFP4_512") == nullptr;
695    if (f.d_type == kNvfp4Type && nvfp4_512 && nvfp4_512_fits(n)) {
696        nvfp4_512_rows(blob + f.down_off, f.d_row, n, hq, nt, out, r0, r1, sd);
697        return;
698    }
699    static const bool nvfp4_256 = cpu_avx2_ok() && std::getenv("STRATA_NO_NVFP4_256") == nullptr;   // see above
700    if (f.d_type == kNvfp4Type && nvfp4_256 && nvfp4_256_fits(n)) {
701        nvfp4_256_rows(blob + f.down_off, f.d_row, n, hq, nt, out, r0, r1, sd);
702        return;
703    }
704    const Tr dot = traits(f.d_type);
705    for (int r = r0; r < r1; ++r) {
706        const uint8_t* dr = blob + f.down_off + (size_t) r * f.d_row;
707        for (int t = 0; t < nt; ++t) {
708            float s = 0.f;
709            dot.dot(n, &s, dr, hq[t]);
710            out[t][r] = s * sd;
711        }
712    }
713}
714
715// Keep one dispatch implementation: only width-sensitive IQ kernels need a different arithmetic path.
716// Already invariant kernels (including NVFP4) retain their shared decoding and scale handling.
717void native_gu_rows(const NativeFmt& f, const uint8_t* blob, const void* const* act, int nt,
718                    float* const* ff, int r0, int r1) {
719    native_gu_rows_impl<NativeArithmetic::Default>(f, blob, act, nt, ff, r0, r1);
720}
721
722void native_down_rows(const NativeFmt& f, const uint8_t* blob, const void* const* hq, int nt,
723                      float* const* out, int r0, int r1) {
724    native_down_rows_impl<NativeArithmetic::Default>(f, blob, hq, nt, out, r0, r1);
725}
726
727void native_gu_rows_invariant(const NativeFmt& f, const uint8_t* blob, const void* const* act, int nt,
728                              float* const* ff, int r0, int r1) {
729    native_gu_rows_impl<NativeArithmetic::WindowInvariant>(f, blob, act, nt, ff, r0, r1);
730}
731
732void native_down_rows_invariant(const NativeFmt& f, const uint8_t* blob, const void* const* hq, int nt,
733                                float* const* out, int r0, int r1) {
734    native_down_rows_impl<NativeArithmetic::WindowInvariant>(f, blob, hq, nt, out, r0, r1);
735}
736
737}  // namespace strata::kernels::cpu