okami.mom/strata

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

waveringanaRestore the DeepSeek V4.1 engine and CPU tests removed upstream8ac8ce32e

main
39.4 KiB782 linesraw
1// Copyright (c) 2026 Peasant Smith and the Project Maya contributors (MIT; https://github.com/mw00/project-maya, v1.1.17).
2// Ported into Strata (third_party/project-maya/NOTICE.md, LICENSE); changes are in the git history of this file.
3// src/ds4/ds4_iq_cpu.cpp - a GGUF's low-bit experts on the CPU lane (see ds4_gguf_cpu.hpp): the GLM CPU lane's native
4// experts (src/kernels/cpu/native_expert.cpp), whose arithmetic is ggml-cpu's - and for IQ3_XXS / IQ3_S (Maya-S's
5// down, Maya-M's gate / up / down) rows of our own, the same numbers bit for bit, faster.
6#include "ds4_gguf_cpu.hpp"
7// Strata port: the low-bit (IQ) experts have no CPU lane here (they need Maya's native_expert split-base rows); the stub
8// branch below answers for them.  MXFP4, the source format, has its own CPU rows (ds4_gguf_cpu.cpp).
9#undef STRATA_NATIVE_EXPERTS
10#if defined(STRATA_NATIVE_EXPERTS)
11#include "strata/kernels/cpu/native_expert.hpp"  // (only with STRATA_NATIVE_EXPERTS)
12
13#define GGML_COMMON_DECL_CPP
14#define GGML_COMMON_IMPL_CPP
15#include "ggml-common.h"
16#include "ggml-cpu.h"
17#include "ggml.h"
18#endif
19
20#include <algorithm>
21#include <chrono>
22#include <cmath>
23#include <cstdio>
24#include <cstdlib>
25#include <cstring>
26#include <limits>
27#include <mutex>
28#include <string>
29#include <vector>
30
31#if defined(__x86_64__) || defined(_M_X64)
32#define IQ_X86 1
33#include <immintrin.h>
34#if defined(_MSC_VER)
35#include <intrin.h>
36#else
37#include <cpuid.h>
38#endif
39#endif
40#if defined(IQ_X86) && (defined(__GNUC__) || defined(__clang__))
41#define IQ_AVX2 __attribute__((target("avx2,fma")))
42#else
43#define IQ_AVX2
44#endif
45
46namespace strata::ds4::gguf {
47
48#if defined(STRATA_NATIVE_EXPERTS)
49namespace kc = strata::dsv41k::cpu;
50namespace {
51kc::NativeFmt native(const IqCpu& f) {   // (the fields the rows read; no lookups)
52    kc::NativeFmt n;
53    n.gu_type = f.tg;
54    n.d_type = f.td;
55    n.gu_act = f.gu_act;
56    n.d_act = f.d_act;
57    n.n_embd = f.n_embd;
58    n.n_ff = f.n_ff;
59    n.gu_row = f.gu_row;
60    n.d_row = f.d_row;
61    n.up_off = f.gu_row * (size_t) f.n_ff;
62    n.down_off = 2 * n.up_off;
63    n.bytes = n.down_off + f.d_row * (size_t) f.n_embd;
64    n.act_bytes = f.act_bytes;
65    n.h_bytes = f.h_bytes;
66    return n;
67}
68
69// ---- what this CPU runs: AVX2 + FMA (the OS saving the YMM state), and AVX-512 F / BW + VBMI with BMI2 (the ZMM state)
70struct IqCpuIsa {
71    bool avx2 = false, w512 = false;
72};
73IqCpuIsa iq_isa() {
74    IqCpuIsa c;
75#if defined(IQ_X86)
76    unsigned r[4] = {0, 0, 0, 0};
77    auto cpuid = [&](unsigned leaf, unsigned sub) {
78#if defined(_MSC_VER)
79        int x[4];
80        __cpuidex(x, (int) leaf, (int) sub);
81        for (int i = 0; i < 4; ++i) r[i] = (unsigned) x[i];
82#else
83        __cpuid_count(leaf, sub, r[0], r[1], r[2], r[3]);
84#endif
85    };
86    cpuid(0, 0);
87    if (r[0] < 7) return c;
88    cpuid(1, 0);
89    if (!((r[2] >> 12) & 1u) || !((r[2] >> 27) & 1u) || !((r[2] >> 28) & 1u)) return c;   // FMA, OSXSAVE, AVX
90#if defined(_MSC_VER)
91    const unsigned long long xcr0 = _xgetbv(0);
92#else
93    unsigned lo = 0, hi = 0;
94    __asm__ volatile("xgetbv" : "=a"(lo), "=d"(hi) : "c"(0));
95    const unsigned long long xcr0 = ((unsigned long long) hi << 32) | lo;
96#endif
97    if ((xcr0 & 0x6) != 0x6) return c;
98    cpuid(7, 0);
99    c.avx2 = (r[1] >> 5) & 1u;
100    c.w512 = c.avx2 && (xcr0 & 0xE6) == 0xE6 && ((r[1] >> 16) & 1u) && ((r[1] >> 30) & 1u) && ((r[2] >> 1) & 1u) &&
101             ((r[1] >> 8) & 1u);   // AVX-512 F, BW, VBMI; BMI2
102#endif
103    return c;
104}
105const IqCpuIsa& isa() {
106    static const IqCpuIsa c = iq_isa();
107    return c;
108}
109
110// ---- IQ3_XXS / IQ3_S rows of our own.  ggml-cpu's AVX2 dots build each 32-value chunk's grid magnitudes from eight
111// scalar table loads and inserts, and redo it for every token; here the grid comes in with one vpgatherdd from the
112// same table, the signs are built in registers (the GLM rows' sgn7_vec / sgn_vec), and every token of a call applies
113// the decoded chunk.  The arithmetic is ggml's to the bit: the same exact int32 lanes per block (lane l: the chunks'
114// grid entries l, each times its 2 s + 1), then for each block fma(d_x d_y, lanes, acc) and hsum_float_8 (IQ3_XXS:
115// times 0.25) - checked against ggml-cpu's own dot once per type (iq3_check), else ggml's rows are used.
116inline float h2f(uint16_t h) {   // F16 -> F32, exactly
117    const uint32_t s = (uint32_t) (h & 0x8000u) << 16, e = (h >> 10) & 0x1fu, m = h & 0x3ffu;
118    uint32_t b;
119    if (e == 0) {
120        const float f = (float) m * 5.9604644775390625e-8f;   // m 2^-24 (exact)
121        std::memcpy(&b, &f, 4);
122        b |= s;
123    } else if (e == 31) {
124        b = s | 0x7f800000u | (m << 13);
125    } else {
126        b = s | ((e + 112u) << 23) | (m << 13);
127    }
128    float f;
129    std::memcpy(&f, &b, 4);
130    return f;
131}
132inline uint32_t u32(const uint8_t* p) {
133    uint32_t v;
134    std::memcpy(&v, p, 4);
135    return v;
136}
137
138#if defined(IQ_X86)
139IQ_AVX2 inline float hsum_ggml(__m256 x) {   // ggml's hsum_float_8, the same additions in the same order
140    __m128 r = _mm256_extractf128_ps(x, 1);
141    r = _mm_add_ps(r, _mm256_castps256_ps128(x));
142    r = _mm_add_ps(r, _mm_movehl_ps(r, r));
143    r = _mm_add_ss(r, _mm_movehdup_ps(r));
144    return _mm_cvtss_f32(r);
145}
146// IQ2_S / IQ3_S: 32 explicit sign bits -> 32 bytes of -1 / +1 (ggml's bit selector)
147IQ_AVX2 inline __m256i sgn32(uint32_t m) {
148    const __m256i sh = _mm256_setr_epi8(0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,   //
149                                        2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3);
150    const __m256i sel = _mm256_set1_epi64x((long long) 0x8040201008040201ull);
151    const __m256i b = _mm256_and_si256(_mm256_shuffle_epi8(_mm256_set1_epi32((int) m), sh), sel);
152    return _mm256_or_si256(_mm256_cmpeq_epi8(b, sel), _mm256_set1_epi8(1));
153}
154// table entries into a vector without the shuffle port (ggml's _mm256_set_epi64x / _epi32 of table loads ends up in
155// inserts, one shuffle each; here each entry is a broadcast load and the lanes are put together by dword blends)
156IQ_AVX2 inline __m256i bq(const void* p) { return _mm256_castpd_si256(_mm256_broadcast_sd((const double*) p)); }
157IQ_AVX2 inline __m256i bd(const void* p) { return _mm256_castps_si256(_mm256_broadcast_ss((const float*) p)); }
158IQ_AVX2 inline __m256i tab4(const uint64_t* t, unsigned a, unsigned b, unsigned c, unsigned d) {   // qwords t[a] t[b] t[c] t[d]
159    return _mm256_blend_epi32(_mm256_blend_epi32(bq(t + a), bq(t + b), 0x0C), _mm256_blend_epi32(bq(t + c), bq(t + d), 0xC0),
160                              0xF0);
161}
162IQ_AVX2 inline __m256i tab8(const uint32_t* t, const unsigned* i) {   // dwords t[i[0]] .. t[i[7]]
163    const __m256i a = _mm256_blend_epi32(bd(t + i[0]), bd(t + i[1]), 0x02), b = _mm256_blend_epi32(bd(t + i[2]), bd(t + i[3]), 0x08);
164    const __m256i c = _mm256_blend_epi32(bd(t + i[4]), bd(t + i[5]), 0x20), e = _mm256_blend_epi32(bd(t + i[6]), bd(t + i[7]), 0x80);
165    return _mm256_blend_epi32(_mm256_blend_epi32(a, b, 0x0C), _mm256_blend_epi32(c, e, 0xC0), 0xF0);
166}
167// the 8 sign bytes (-1 / +1) of each 7-bit sign index with its parity bit: ggml's keven_signs_q2xs, as +-1 bytes
168alignas(64) uint64_t g_ks64[128];
169void build_ks64() {
170    for (int i = 0; i < 128; ++i) {
171        int par = 0;
172        for (int b = 0; b < 7; ++b) par ^= (i >> b) & 1;
173        const int s = i | (par << 7);
174        uint64_t v = 0;
175        for (int j = 0; j < 8; ++j) v |= (uint64_t) (((s >> j) & 1) ? 0xFFu : 0x01u) << (8 * j);
176        g_ks64[i] = v;
177    }
178}
179IQ_AVX2 inline __m256i sgn7t(uint32_t w) {   // a chunk's four 7-bit sign indices (bits 0..27 of w)
180    return tab4(g_ks64, w & 127u, (w >> 7) & 127u, (w >> 14) & 127u, (w >> 21) & 127u);
181}
182IQ_AVX2 inline __m256i sc16(int a, int b) {   // int16 lanes 0-7 a (values 0-15), 8-15 b (values 16-31)
183    return _mm256_inserti128_si256(_mm256_castsi128_si256(_mm_set1_epi16((short) a)), _mm_set1_epi16((short) b), 1);
184}
185bool g_gather18 = false, g_gather21 = false;   // the IQ3_XXS / IQ3_S grids by vpgatherdd (own(): if faster here)
186// IQ2_XXS's grid entry with its signs applied, for every grid index g and sign index i, at g << 7 | i (256 KB, built on
187// first use): one lookup instead of two; the products are then (q8 + 128) x signed grid less 128 x the grid's pair sums
188// (vpmaddubsw takes u8 x s8), the same integers as ggml's |grid| x signed q8
189alignas(64) uint64_t g_ct2x[256 * 128];
190bool g_ct16 = false;   // (own(): if faster here)
191void build_ct2x() {
192    for (int g = 0; g < 256; ++g)
193        for (int i = 0; i < 128; ++i) {
194            const uint64_t sg = g_ks64[i];   // (+-1 bytes)
195            uint64_t v = 0;
196            for (int j = 0; j < 8; ++j) {
197                const int gb = (int) ((iq2xxs_grid[g] >> (8 * j)) & 255u);
198                const int sb = ((sg >> (8 * j)) & 255u) == 0xFFu ? -gb : gb;
199                v |= (uint64_t) (uint8_t) (int8_t) sb << (8 * j);
200            }
201            g_ct2x[(size_t) g << 7 | (size_t) i] = v;
202        }
203}
204
205// rows [r0, r1) (out[t][r - r0]) of NT tokens; TY 16 = IQ2_XXS, 22 = IQ2_S, 18 = IQ3_XXS, 21 = IQ3_S.  A pair of
206// 32-value chunks a step: grid magnitudes g1 / g2, sign vectors s1 / s2 (-1 / +1), scales c1 / c2 - decoded once - then
207// every token: q8 times the signs, vpmaddubsw against the grid, vpmaddwd by the scales, into ggml's 8 int32 lanes
208template <int TY, int NT>
209IQ_AVX2 void iq_rows_avx2(const uint8_t* w, size_t rb, int nb, const block_q8_K* const* y, float* const* out, int r0,
210                          int r1) {
211    const __m256i sub = _mm256_setr_epi32(0, 1, 2, 3, 4, 5, 6, 7);
212    const bool gat = TY == 18 ? g_gather18 : TY == 21 ? g_gather21 : false;
213    const bool ct = TY == 16 && g_ct16;
214    const __m256i k80 = _mm256_set1_epi8((char) 0x80);
215    constexpr float K = TY == 18 ? 0.25f : TY == 21 ? 1.0f : 0.125f;
216    constexpr size_t BB = TY == 16 ? sizeof(block_iq2_xxs) : TY == 22 ? sizeof(block_iq2_s) :
217                          TY == 18 ? sizeof(block_iq3_xxs) : sizeof(block_iq3_s);
218    for (int r = r0; r < r1; ++r) {
219        const uint8_t* row = w + (size_t) r * rb;
220        __m256 accf[NT];
221        for (int t = 0; t < NT; ++t) accf[t] = _mm256_setzero_ps();
222        for (int i = 0; i < nb; ++i) {
223            const uint8_t* blk = row + (size_t) i * BB;
224            __m256i sumi[NT];
225            for (int t = 0; t < NT; ++t) sumi[t] = _mm256_setzero_si256();
226            uint16_t dh;
227            std::memcpy(&dh, blk, 2);
228            for (int ib = 0; ib < QK_K / 32; ib += 2) {
229                __m256i g1, g2, s1, s2, c1, c2;
230                if (TY == 16) {   // IQ2_XXS: per chunk a word of 4 grid indices, a word of 4 sign indices + the scale
231                    const uint8_t* q = blk + 2 + 8 * ib;
232                    const uint32_t a0 = u32(q), a1 = u32(q + 4), b0 = u32(q + 8), b1 = u32(q + 12);
233                    if (ct) {   // (g1 / g2 the signed grids; s1 / s2 their 128 x pair sums)
234                        g1 = tab4(g_ct2x, (a0 & 255u) << 7 | (a1 & 127u), ((a0 >> 8) & 255u) << 7 | ((a1 >> 7) & 127u),
235                                  ((a0 >> 16) & 255u) << 7 | ((a1 >> 14) & 127u), (a0 >> 24) << 7 | ((a1 >> 21) & 127u));
236                        g2 = tab4(g_ct2x, (b0 & 255u) << 7 | (b1 & 127u), ((b0 >> 8) & 255u) << 7 | ((b1 >> 7) & 127u),
237                                  ((b0 >> 16) & 255u) << 7 | ((b1 >> 14) & 127u), (b0 >> 24) << 7 | ((b1 >> 21) & 127u));
238                        s1 = _mm256_maddubs_epi16(k80, g1);
239                        s2 = _mm256_maddubs_epi16(k80, g2);
240                    } else {
241                        g1 = tab4(iq2xxs_grid, a0 & 255u, (a0 >> 8) & 255u, (a0 >> 16) & 255u, a0 >> 24);
242                        g2 = tab4(iq2xxs_grid, b0 & 255u, (b0 >> 8) & 255u, (b0 >> 16) & 255u, b0 >> 24);
243                        s1 = sgn7t(a1);
244                        s2 = sgn7t(b1);
245                    }
246                    c1 = _mm256_set1_epi16((short) (2 * (a1 >> 28) + 1));
247                    c2 = _mm256_set1_epi16((short) (2 * (b1 >> 28) + 1));
248                } else if (TY == 22) {   // IQ2_S: 10-bit grid indices (qs | 2 bits of qh), explicit signs, 2 scales a chunk
249                    const uint8_t* qs = blk + 2 + 4 * ib;
250                    const uint8_t* qh = blk + 2 + QK_K / 4;
251                    const uint8_t* sg = blk + 2 + QK_K / 8 + 4 * ib;
252                    const uint8_t* scl = blk + 2 + QK_K / 4 + QK_K / 32;
253                    const unsigned h0 = qh[ib], h1 = qh[ib + 1];
254                    g1 = tab4(iq2s_grid, qs[0] | ((h0 << 8) & 0x300u), qs[1] | ((h0 << 6) & 0x300u), qs[2] | ((h0 << 4) & 0x300u),
255                              qs[3] | ((h0 << 2) & 0x300u));
256                    g2 = tab4(iq2s_grid, qs[4] | ((h1 << 8) & 0x300u), qs[5] | ((h1 << 6) & 0x300u), qs[6] | ((h1 << 4) & 0x300u),
257                              qs[7] | ((h1 << 2) & 0x300u));
258                    s1 = sgn32(u32(sg));
259                    s2 = sgn32(u32(sg + 4));
260                    c1 = sc16(2 * (scl[ib] & 0xf) + 1, 2 * (scl[ib] >> 4) + 1);
261                    c2 = sc16(2 * (scl[ib + 1] & 0xf) + 1, 2 * (scl[ib + 1] >> 4) + 1);
262                } else if (TY == 18) {   // IQ3_XXS: 8 grid indices a chunk, then the chunks' sign / scale words
263                    const uint8_t* q3 = blk + 2 + 8 * ib;
264                    const uint8_t* gas = blk + 2 + QK_K / 4 + 4 * ib;
265                    if (gat) {
266                        g1 = _mm256_i32gather_epi32((const int*) iq3xxs_grid, _mm256_cvtepu8_epi32(_mm_loadl_epi64((const __m128i*) q3)), 4);
267                        g2 = _mm256_i32gather_epi32((const int*) iq3xxs_grid,
268                                                    _mm256_cvtepu8_epi32(_mm_loadl_epi64((const __m128i*) (q3 + 8))), 4);
269                    } else {
270                        unsigned i1[8], i2[8];
271                        for (int k = 0; k < 8; ++k) {
272                            i1[k] = q3[k];
273                            i2[k] = q3[8 + k];
274                        }
275                        g1 = tab8(iq3xxs_grid, i1);
276                        g2 = tab8(iq3xxs_grid, i2);
277                    }
278                    const uint32_t a0 = u32(gas), a1 = u32(gas + 4);
279                    s1 = sgn7t(a0);
280                    s2 = sgn7t(a1);
281                    c1 = _mm256_set1_epi16((short) (2 * (a0 >> 28) + 1));
282                    c2 = _mm256_set1_epi16((short) (2 * (a1 >> 28) + 1));
283                } else {   // IQ3_S: 9-bit grid indices (qs | a bit of qh), explicit signs
284                    const uint8_t* qs = blk + 2 + 8 * ib;
285                    const uint8_t* qh = blk + 2 + QK_K / 4;
286                    const uint8_t* sg = blk + 2 + QK_K / 4 + QK_K / 32 + 4 * ib;
287                    const int sc = blk[2 + QK_K / 4 + QK_K / 32 + QK_K / 8 + ib / 2];
288                    const unsigned h0 = qh[ib], h1 = qh[ib + 1];
289                    if (gat) {
290                        const __m256i hb1 = _mm256_slli_epi32(_mm256_and_si256(_mm256_srlv_epi32(_mm256_set1_epi32((int) h0), sub), _mm256_set1_epi32(1)), 8);
291                        const __m256i hb2 = _mm256_slli_epi32(_mm256_and_si256(_mm256_srlv_epi32(_mm256_set1_epi32((int) h1), sub), _mm256_set1_epi32(1)), 8);
292                        g1 = _mm256_i32gather_epi32((const int*) iq3s_grid,
293                                                    _mm256_or_si256(_mm256_cvtepu8_epi32(_mm_loadl_epi64((const __m128i*) qs)), hb1), 4);
294                        g2 = _mm256_i32gather_epi32((const int*) iq3s_grid,
295                                                    _mm256_or_si256(_mm256_cvtepu8_epi32(_mm_loadl_epi64((const __m128i*) (qs + 8))), hb2), 4);
296                    } else {
297                        unsigned i1[8], i2[8];
298                        for (int k = 0; k < 8; ++k) {
299                            i1[k] = qs[k] | (((h0 >> k) & 1u) << 8);
300                            i2[k] = qs[8 + k] | (((h1 >> k) & 1u) << 8);
301                        }
302                        g1 = tab8(iq3s_grid, i1);
303                        g2 = tab8(iq3s_grid, i2);
304                    }
305                    s1 = sgn32(u32(sg));
306                    s2 = sgn32(u32(sg + 4));
307                    c1 = _mm256_set1_epi16((short) (2 * (sc & 0xf) + 1));
308                    c2 = _mm256_set1_epi16((short) (2 * (sc >> 4) + 1));
309                }
310                if (ct) {
311                    for (int t = 0; t < NT; ++t) {
312                        const int8_t* q8 = y[t][i].qs + 32 * ib;
313                        const __m256i u1 = _mm256_xor_si256(_mm256_loadu_si256((const __m256i*) q8), k80);   // (q8 + 128)
314                        const __m256i u2 = _mm256_xor_si256(_mm256_loadu_si256((const __m256i*) (q8 + 32)), k80);
315                        const __m256i d1 = _mm256_sub_epi16(_mm256_maddubs_epi16(u1, g1), s1);
316                        const __m256i d2 = _mm256_sub_epi16(_mm256_maddubs_epi16(u2, g2), s2);
317                        sumi[t] = _mm256_add_epi32(sumi[t], _mm256_add_epi32(_mm256_madd_epi16(d1, c1), _mm256_madd_epi16(d2, c2)));
318                    }
319                    continue;
320                }
321                for (int t = 0; t < NT; ++t) {
322                    const int8_t* q8 = y[t][i].qs + 32 * ib;
323                    const __m256i d1 = _mm256_maddubs_epi16(g1, _mm256_sign_epi8(_mm256_loadu_si256((const __m256i*) q8), s1));
324                    const __m256i d2 = _mm256_maddubs_epi16(g2, _mm256_sign_epi8(_mm256_loadu_si256((const __m256i*) (q8 + 32)), s2));
325                    sumi[t] = _mm256_add_epi32(sumi[t], _mm256_add_epi32(_mm256_madd_epi16(d1, c1), _mm256_madd_epi16(d2, c2)));
326                }
327            }
328            const float dx = h2f(dh);
329            for (int t = 0; t < NT; ++t)
330                accf[t] = _mm256_fmadd_ps(_mm256_set1_ps(dx * y[t][i].d), _mm256_cvtepi32_ps(sumi[t]), accf[t]);
331        }
332        for (int t = 0; t < NT; ++t) out[t][r - r0] = K * hsum_ggml(accf[t]);
333    }
334}
335
336// ---- the same rows in 512-bit lanes (AVX-512 BW + VBMI, BMI2: Ice Lake and later Intel, Zen 4 / Zen 5), the GLM
337// lane's one-token IQ2_XXS scheme (src/kernels/cpu/iq_avx512_one.cpp) for 3-bit grids: every grid byte is one of 8
338// levels, so an entry is four 3-bit codes (a u16) and the grid's codes fit in registers - IQ3_XXS's 256 in 8 zmm,
339// IQ3_S's 512 in 16; a block's 64 indices are looked up 32 at a time with two-register word permutes and blends, each
340// 64 values' 16 codes spread to their bytes by a word permute, a multishift, an and and a byte shuffle; IQ3_XXS's signs
341// come from pdep and a parity fold, IQ3_S's are a mask register's bits.  Two chunks a register, folded to ggml's 8
342// lanes, then ggml's float steps: the same numbers to the bit (own_check).
343#if defined(__GNUC__) || defined(__clang__)
344#define IQ_512 __attribute__((target("avx512f,avx512bw,avx512vbmi,bmi2,avx2,fma")))
345#else
346#define IQ_512
347#endif
348alignas(64) uint16_t g_code3x[256], g_code3s[512];
349bool g_codes_ok = false;
350
351IQ_512 inline uint32_t signs4(uint32_t w) {   // ksigns_iq2xs for the four 7-bit fields of w: i | parity(i) << 7
352    const uint64_t s = _pdep_u64(w & 0x0FFFFFFFu, 0x7F7F7F7Full);
353    uint64_t p = s ^ ((s >> 4) & 0x0F0F0F0Full);
354    p ^= (p >> 2) & 0x03030303ull;
355    p ^= (p >> 1) & 0x01010101ull;
356    return (uint32_t) (s | ((p & 0x01010101ull) << 7));
357}
358// each grid entry as four 3-bit level codes; false if a byte is not one of the 8 levels (a grid this file does not know)
359bool build_codes3() {
360    static const int lx[8] = {4, 12, 20, 28, 36, 44, 52, 62}, ls[8] = {1, 3, 5, 7, 9, 11, 13, 15};
361    auto code = [](uint32_t e, const int* lv) -> int {
362        int c = 0;
363        for (int k = 0; k < 4; ++k) {
364            const int b = (int) ((e >> (8 * k)) & 255u);
365            int l = -1;
366            for (int j = 0; j < 8; ++j)
367                if (lv[j] == b) l = j;
368            if (l < 0) return -1;
369            c |= l << (3 * k);
370        }
371        return c;
372    };
373    for (int i = 0; i < 256; ++i) {
374        const int c = code(iq3xxs_grid[i], lx);
375        if (c < 0) return false;
376        g_code3x[i] = (uint16_t) c;
377    }
378    for (int i = 0; i < 512; ++i) {
379        const int c = code(iq3s_grid[i], ls);
380        if (c < 0) return false;
381        g_code3s[i] = (uint16_t) c;
382    }
383    return true;
384}
385IQ_512 inline __m256i fold(__m512i a) {   // a block's 16 int32 lanes -> ggml's 8 (lane k + lane k + 8), exact
386    return _mm256_add_epi32(_mm512_castsi512_si256(a), _mm512_extracti64x4_epi64(a, 1));
387}
388IQ_512 inline __m512i scales4(int s0, int s1, int s2, int s3) {   // int16 lanes 0-7 s0, 8-15 s1, 16-23 s2, 24-31 s3
389    const __m512i idx = _mm512_set_epi16(3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1,   //
390                                         0, 0, 0, 0, 0, 0, 0, 0);
391    const uint64_t packed = (uint64_t) (uint16_t) s0 | ((uint64_t) (uint16_t) s1 << 16) |
392                            ((uint64_t) (uint16_t) s2 << 32) | ((uint64_t) (uint16_t) s3 << 48);
393    return _mm512_permutexvar_epi16(idx, _mm512_set1_epi64((long long) packed));
394}
395// the codes of 32 word indices: 64 entries a register pair, NP pairs, blended by the index's bits 6, 7 (and 8)
396template <int NP>
397IQ_512 inline __m512i lookup(const __m512i* T, __m512i idx) {
398    __m512i r[NP];
399    for (int j = 0; j < NP; ++j) r[j] = _mm512_permutex2var_epi16(T[2 * j], idx, T[2 * j + 1]);
400    const __mmask32 m6 = _mm512_test_epi16_mask(idx, _mm512_set1_epi16(64));
401    const __mmask32 m7 = _mm512_test_epi16_mask(idx, _mm512_set1_epi16(128));
402    __m512i a = _mm512_mask_blend_epi16(m7, _mm512_mask_blend_epi16(m6, r[0], r[1]), _mm512_mask_blend_epi16(m6, r[2], r[3]));
403    if (NP == 8) {
404        const __mmask32 m8 = _mm512_test_epi16_mask(idx, _mm512_set1_epi16(256));
405        const __m512i b = _mm512_mask_blend_epi16(m7, _mm512_mask_blend_epi16(m6, r[NP > 4 ? 4 : 0], r[NP > 5 ? 5 : 0]),
406                                                  _mm512_mask_blend_epi16(m6, r[NP > 6 ? 6 : 0], r[NP > 7 ? 7 : 0]));
407        a = _mm512_mask_blend_epi16(m8, a, b);
408    }
409    return a;
410}
411
412template <int TY, int NT>
413IQ_512 void iq3_rows_512(const uint8_t* w, size_t rb, int nb, const block_q8_K* const* y, float* const* out, int r0,
414                         int r1) {
415    constexpr int NTAB = TY == 18 ? 8 : 16;
416    constexpr size_t BB = TY == 18 ? sizeof(block_iq3_xxs) : sizeof(block_iq3_s);
417    __m512i T[NTAB];
418    for (int k = 0; k < NTAB; ++k)
419        T[k] = _mm512_load_si512((const void*) ((TY == 18 ? g_code3x : g_code3s) + 32 * k));
420    // code -> grid byte (IQ3_XXS 4 .. 62, IQ3_S 1 .. 15) in every 128-bit lane; byte b of an output qword <- 3 bits at
421    // [0, 3, 6, 9] (its first entry's code, word 0) or [32, 35, 38, 41] (the second's, word 2)
422    const __m512i lut = TY == 18 ? _mm512_set4_epi32(0, 0, 0x3E342C24, 0x1C140C04) : _mm512_set4_epi32(0, 0, 0x0F0D0B09, 0x07050301);
423    const __m512i ctl = _mm512_set1_epi64(0x2926232009060300LL);
424    const __m512i seven = _mm512_set1_epi8(7), zero = _mm512_setzero_si512();
425    __m512i pidx[2];   // a 64-value group h of 32 codes: output qword m <- entries 16 h + 2 m (word 0), + 1 (word 2)
426    for (int h = 0; h < 2; ++h) {
427        alignas(64) uint16_t p[32];
428        for (int m = 0; m < 8; ++m) {
429            p[4 * m] = p[4 * m + 1] = (uint16_t) (16 * h + 2 * m);
430            p[4 * m + 2] = p[4 * m + 3] = (uint16_t) (16 * h + 2 * m + 1);
431        }
432        pidx[h] = _mm512_load_si512((const void*) p);
433    }
434    for (int r = r0; r < r1; ++r) {
435        const uint8_t* row = w + (size_t) r * rb;
436        __m256 accf[NT];
437        for (int t = 0; t < NT; ++t) accf[t] = _mm256_setzero_ps();
438        for (int i = 0; i < nb; ++i) {
439            const uint8_t* blk = row + (size_t) i * BB;
440            __m512i codes[2];
441            if (TY == 18) {
442                const __m512i q = _mm512_loadu_si512((const void*) (blk + 2));
443                codes[0] = lookup<4>(T, _mm512_cvtepu8_epi16(_mm512_castsi512_si256(q)));
444                codes[1] = lookup<4>(T, _mm512_cvtepu8_epi16(_mm512_extracti64x4_epi64(q, 1)));
445            } else {
446                const uint8_t* qh = blk + 2 + QK_K / 4;
447                const __m512i hi = _mm512_set1_epi16(256);
448                const __m512i ia = _mm512_cvtepu8_epi16(_mm256_loadu_si256((const __m256i*) (blk + 2)));
449                const __m512i ib = _mm512_cvtepu8_epi16(_mm256_loadu_si256((const __m256i*) (blk + 34)));
450                // (index k of chunk c: qs[8 c + k] | bit k of qh[c] << 8 - the 32 bits of qh[0..3] are chunks 0..3's)
451                codes[0] = lookup<8>(T, _mm512_or_si512(ia, _mm512_maskz_mov_epi16((__mmask32) u32(qh), hi)));
452                codes[1] = lookup<8>(T, _mm512_or_si512(ib, _mm512_maskz_mov_epi16((__mmask32) u32(qh + 4), hi)));
453            }
454            __m512i acc[NT];
455            for (int t = 0; t < NT; ++t) acc[t] = _mm512_setzero_si512();
456            for (int p = 0; p < 4; ++p) {
457                const __m512i rep = _mm512_permutexvar_epi16(pidx[p & 1], codes[p >> 1]);
458                const __m512i g = _mm512_shuffle_epi8(lut, _mm512_and_si512(_mm512_multishift_epi64_epi8(ctl, rep), seven));
459                uint64_t sm;
460                int sa, sb;
461                if (TY == 18) {
462                    const uint8_t* sw = blk + 2 + QK_K / 4 + 8 * p;
463                    const uint32_t wa = u32(sw), wb = u32(sw + 4);
464                    sm = (uint64_t) signs4(wa) | ((uint64_t) signs4(wb) << 32);
465                    sa = 2 * (int) (wa >> 28) + 1;
466                    sb = 2 * (int) (wb >> 28) + 1;
467                } else {
468                    std::memcpy(&sm, blk + 2 + QK_K / 4 + QK_K / 32 + 8 * p, 8);
469                    const int sc = blk[2 + QK_K / 4 + QK_K / 32 + QK_K / 8 + p];
470                    sa = 2 * (sc & 0xf) + 1;
471                    sb = 2 * (sc >> 4) + 1;
472                }
473                const __mmask64 m = _cvtu64_mask64(sm);
474                const __m512i S = scales4(sa, sa, sb, sb);
475                for (int t = 0; t < NT; ++t) {
476                    const __m512i yv = _mm512_loadu_si512((const void*) (y[t][i].qs + 64 * p));
477                    const __m512i ys = _mm512_mask_sub_epi8(yv, m, zero, yv);
478                    acc[t] = _mm512_add_epi32(acc[t], _mm512_madd_epi16(_mm512_maddubs_epi16(g, ys), S));
479                }
480            }
481            uint16_t dh;
482            std::memcpy(&dh, blk, 2);
483            const float dx = h2f(dh);
484            for (int t = 0; t < NT; ++t)
485                accf[t] = _mm256_fmadd_ps(_mm256_set1_ps(dx * y[t][i].d), _mm256_cvtepi32_ps(fold(acc[t])), accf[t]);
486        }
487        for (int t = 0; t < NT; ++t) out[t][r - r0] = (TY == 18 ? 0.25f : 1.0f) * hsum_ggml(accf[t]);
488    }
489}
490
491template <int TY>
492void iq_rows_n(const uint8_t* w, size_t rb, int n, const void* const* act, int nt, float* const* out, int r0, int r1) {
493    static const bool w512 = [] {
494        const char* v = std::getenv("DS4_IQ_AVX2");   // (=1: the AVX2 rows on an AVX-512 CPU, to compare)
495        return (TY == 18 || TY == 21) && g_codes_ok && isa().w512 && !(v != nullptr && std::atoi(v) != 0);
496    }();
497    const int nb = n / QK_K;
498    for (int t0 = 0; t0 < nt; t0 += 4) {
499        const block_q8_K* y[4];
500        float* o[4];
501        const int c = nt - t0 < 4 ? nt - t0 : 4;
502        for (int t = 0; t < c; ++t) {
503            y[t] = (const block_q8_K*) act[t0 + t];
504            o[t] = out[t0 + t];
505        }
506        if constexpr (TY == 18 || TY == 21) {
507            if (w512) {
508                switch (c) {
509                    case 1: iq3_rows_512<TY, 1>(w, rb, nb, y, o, r0, r1); break;
510                    case 2: iq3_rows_512<TY, 2>(w, rb, nb, y, o, r0, r1); break;
511                    case 3: iq3_rows_512<TY, 3>(w, rb, nb, y, o, r0, r1); break;
512                    default: iq3_rows_512<TY, 4>(w, rb, nb, y, o, r0, r1); break;
513                }
514                continue;
515            }
516        }
517        switch (c) {
518            case 1: iq_rows_avx2<TY, 1>(w, rb, nb, y, o, r0, r1); break;
519            case 2: iq_rows_avx2<TY, 2>(w, rb, nb, y, o, r0, r1); break;
520            case 3: iq_rows_avx2<TY, 3>(w, rb, nb, y, o, r0, r1); break;
521            default: iq_rows_avx2<TY, 4>(w, rb, nb, y, o, r0, r1); break;
522        }
523    }
524}
525#endif  // IQ_X86
526
527// out[t][r - r0] for the rows of `type` (an own type only)
528void own_rows(int type, const uint8_t* w, size_t rb, int n, const void* const* act, int nt, float* const* out, int r0,
529              int r1) {
530#if defined(IQ_X86)
531    if (type == GGML_TYPE_IQ2_XXS) return iq_rows_n<16>(w, rb, n, act, nt, out, r0, r1);
532    if (type == GGML_TYPE_IQ2_S) return iq_rows_n<22>(w, rb, n, act, nt, out, r0, r1);
533    if (type == GGML_TYPE_IQ3_XXS) return iq_rows_n<18>(w, rb, n, act, nt, out, r0, r1);
534    if (type == GGML_TYPE_IQ3_S) return iq_rows_n<21>(w, rb, n, act, nt, out, r0, r1);
535#endif
536    (void) type, (void) w, (void) rb, (void) n, (void) act, (void) nt, (void) out, (void) r0, (void) r1;
537}
538
539// our rows against ggml-cpu's dot, bit for bit: random blocks of the type (its F16 scale a small normal value), Q8_K
540// activations from ggml's quantizer, 1, 2, 3, 5 and 9 tokens, 13 rows of 8 blocks
541bool own_check(int type) {
542    ggml_cpu_init();
543    const ggml_type t = (ggml_type) type;
544    const int n = 2048, rows = 13;
545    const size_t rb = ggml_row_size(t, n), bb = ggml_type_size(t), ab = ggml_row_size(GGML_TYPE_Q8_K, n);
546    uint64_t st = 0x9E3779B97F4A7C15ull;
547    auto next = [&st] {
548        st ^= st << 13;
549        st ^= st >> 7;
550        st ^= st << 17;
551        return st;
552    };
553    std::vector<uint8_t> w(rb * rows);
554    for (auto& b : w) b = (uint8_t) next();
555    for (size_t o = 0; o < w.size(); o += bb) {
556        const ggml_fp16_t d = ggml_fp32_to_fp16(0.002f + 0.001f * (float) (next() % 16));
557        std::memcpy(&w[o], &d, sizeof d);
558    }
559    const auto* q8 = ggml_get_type_traits_cpu(GGML_TYPE_Q8_K);
560    const auto dot = ggml_get_type_traits_cpu(t)->vec_dot;
561    for (int nt : {1, 2, 3, 5, 9}) {
562        std::vector<uint8_t> a(ab * (size_t) nt);
563        std::vector<float> x((size_t) n), got((size_t) nt * rows), ref((size_t) nt * rows);
564        std::vector<const void*> ap((size_t) nt);
565        std::vector<float*> op((size_t) nt);
566        for (int k = 0; k < nt; ++k) {
567            for (auto& v : x) v = ((float) (next() % 2001) - 1000.0f) * 1e-3f;
568            q8->from_float(x.data(), a.data() + ab * (size_t) k, n);
569            ap[(size_t) k] = a.data() + ab * (size_t) k;
570            op[(size_t) k] = got.data() + (size_t) k * rows;
571        }
572        own_rows(type, w.data(), rb, n, ap.data(), nt, op.data(), 0, rows);
573        for (int k = 0; k < nt; ++k)
574            for (int r = 0; r < rows; ++r) dot(n, &ref[(size_t) k * rows + r], 0, w.data() + (size_t) r * rb, 0, ap[(size_t) k], 0, 1);
575        if (std::memcmp(got.data(), ref.data(), got.size() * sizeof(float)) != 0) return false;
576    }
577    return true;
578}
579
580// the time (us, best of 5 rounds) of 32 rows of 16 blocks of the type against one token: ours with the IQ3 grids as
581// broadcasts + blends (variant 1) or by vpgatherdd (2), or the native lane's rows (0: the GLM lane's / ggml-cpu's)
582double time_variant(int type, int variant) {
583    const ggml_type t = (ggml_type) type;
584    const int n = 4096, rows = 32;
585    const size_t rb = ggml_row_size(t, n), bb = ggml_type_size(t), ab = ggml_row_size(GGML_TYPE_Q8_K, n);
586    std::vector<uint8_t> w(rb * rows), a(ab);
587    uint32_t s = 12345u;
588    for (auto& b : w) b = (uint8_t) ((s = s * 1664525u + 1013904223u) >> 24);
589    for (size_t o = 0; o < w.size(); o += bb) {
590        const ggml_fp16_t d = ggml_fp32_to_fp16(0.003f);
591        std::memcpy(&w[o], &d, sizeof d);
592    }
593    std::vector<float> x((size_t) n), y((size_t) rows);
594    for (auto& v : x) v = (float) ((int) ((s = s * 1664525u + 1013904223u) >> 16) - 32768) / 32768.0f;
595    ggml_get_type_traits_cpu(GGML_TYPE_Q8_K)->from_float(x.data(), a.data(), n);
596    const void* ap[1] = {a.data()};
597    float* yp[1] = {y.data()};
598    kc::NativeFmt nf;   // (the native rows timed as a gate / up pair of the same rows: 2 x rows dots, as ours below)
599    std::string err;
600    if (variant == 0 && !kc::native_fmt(type, type, n, QK_K, nf, err)) return 1e30;
601    bool* gflag = type == GGML_TYPE_IQ3_XXS ? &g_gather18 : type == GGML_TYPE_IQ3_S ? &g_gather21 :
602                  type == GGML_TYPE_IQ2_XXS ? &g_ct16 : nullptr;
603    const bool keep = gflag != nullptr && *gflag;
604    if (gflag != nullptr) *gflag = variant >= 2;
605    double best = 1e30;
606    for (int round = 0; round < 6; ++round) {
607        const auto t0 = std::chrono::steady_clock::now();
608        for (int rep = 0; rep < 4; ++rep) {
609            if (variant == 0) {
610                kc::native_gu_rows_split(nf, w.data(), w.data(), ap, 1, yp, 0, rows, std::numeric_limits<float>::infinity());
611            } else {
612                own_rows(type, w.data(), rb, n, ap, 1, yp, 0, rows);
613                own_rows(type, w.data(), rb, n, ap, 1, yp, 0, rows);
614            }
615        }
616        const double us = std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - t0).count();
617        if (round > 0) best = std::min(best, us);
618    }
619    if (gflag != nullptr) *gflag = keep;
620    return best;
621}
622
623// our rows for this type, here: IQ3_XXS / IQ3_S on AVX2 or AVX-512 VBMI, IQ2_XXS / IQ2_S on AVX2 - checked once
624// against ggml-cpu's dot (each variant), then the fastest of ours and the native lane's measured on this CPU (they
625// give the same bits, so the choice changes no answer).  DS4_IQ_OWN=0: the native lane's rows; =1: ours without
626// measuring (DS4_IQ_GATHER=1: the IQ3 grids by vpgatherdd); DS4_IQ_AVX2=1: our AVX2 rows on an AVX-512 CPU too.
627bool own(int type) {
628#if defined(IQ_X86)
629    int k;
630    switch (type) {
631        case GGML_TYPE_IQ3_XXS: k = 0; break;
632        case GGML_TYPE_IQ3_S: k = 1; break;
633        case GGML_TYPE_IQ2_XXS: k = 2; break;
634        case GGML_TYPE_IQ2_S: k = 3; break;
635        default: return false;
636    }
637    static int mode = -1;   // DS4_IQ_OWN: -1 measured, 0 never, 1 always
638    static bool force_avx2 = false, force_gather = false;
639    static const bool allowed = [] {
640        const char* v = std::getenv("DS4_IQ_OWN");
641        mode = v == nullptr ? -1 : std::atoi(v) != 0 ? 1 : 0;
642        const char* a2 = std::getenv("DS4_IQ_AVX2");
643        force_avx2 = a2 != nullptr && std::atoi(a2) != 0;
644        const char* gt = std::getenv("DS4_IQ_GATHER");
645        force_gather = gt != nullptr && std::atoi(gt) != 0;
646        g_codes_ok = build_codes3();   // (the 512-bit rows' tables; without them those rows are not used)
647        build_ks64();
648        build_ct2x();
649        return mode != 0 && isa().avx2;
650    }();
651    // (IQ2 on an AVX-512 CPU: the GLM lane's AVX-512 rows serve them, unless DS4_IQ_AVX2)
652    if (!allowed || (k >= 2 && isa().w512 && !force_avx2)) return false;
653    static std::once_flag once[4];
654    static bool ok[4];
655    std::call_once(once[k], [&] {
656        bool* gflag = k == 0 ? &g_gather18 : k == 1 ? &g_gather21 : k == 2 ? &g_ct16 : nullptr;   // (the second form)
657        bool good = own_check(type);   // (the blend form; the 512-bit rows where they serve)
658        bool good_g = false;
659        if (gflag != nullptr) {
660            *gflag = true;
661            good_g = own_check(type);
662            *gflag = false;
663        }
664        if (mode == 1) {
665            if (gflag != nullptr) *gflag = force_gather && good_g;
666            ok[k] = good || (force_gather && good_g);
667            return;
668        }
669        // the variants timed in turns, the best of each kept (other work on the machine - the load's read-ahead, the
670        // prompt - then hits them alike), and ours taken only when clearly faster (10 %): the choice changes no answer,
671        // a wrong one costs time (Uranus' Golden Cove: one-shot timings 30-80 % apart run to run, the choice flipping)
672        double tn = 1e30, tb = 1e30, tg = 1e30;
673        for (int it = 0; it < 5; ++it) {
674            tn = std::min(tn, time_variant(type, 0));
675            if (good) tb = std::min(tb, time_variant(type, 1));
676            if (good_g) tg = std::min(tg, time_variant(type, 2));
677        }
678        if (gflag != nullptr) *gflag = tg < tb;
679        ok[k] = std::min(tb, tg) < 0.9 * tn;
680        if (std::getenv("DS4_IQ_VERBOSE") != nullptr)
681            std::fprintf(stderr, "ds4: CPU lane %s rows: native %.0f us, ours %.0f us%s -> %s\n", ggml_type_name((ggml_type) type),
682                         tn, tb, gflag != nullptr ? (std::string(" (second form: gather / one table ") + std::to_string((int) tg) + " us)").c_str() : "",
683                         ok[k] ? (gflag != nullptr && *gflag ? "ours (second form)" : "ours") : "native");
684    });
685    return ok[k];
686#else
687    (void) type;
688    return false;
689#endif
690}
691}  // namespace
692
693bool iq_cpu_fmt(int tg, int td, int n_embd, int n_ff, IqCpu& f, std::string& err) {
694    kc::NativeFmt n;
695    if (!kc::native_fmt(tg, td, n_embd, n_ff, n, err)) return false;
696    f.tg = tg;
697    f.td = td;
698    f.n_embd = n_embd;
699    f.n_ff = n_ff;
700    f.gu_act = n.gu_act;
701    f.d_act = n.d_act;
702    f.gu_row = n.gu_row;
703    f.d_row = n.d_row;
704    f.act_bytes = n.act_bytes;
705    f.h_bytes = n.h_bytes;
706    return true;
707}
708
709void iq_cpu_quant(int act, const float* x, int k, void* dst) {
710    kc::NativeFmt n;
711    n.gu_act = act;
712    n.n_embd = k;
713    kc::native_quant_act(n, x, dst);
714}
715
716void iq_cpu_gate_up(const IqCpu& f, const uint8_t* gate, const uint8_t* up, const void* const* act, int nt,
717                    float* const* ff, int r0, int r1, float limit) {
718    if (r1 > r0 && own(f.tg)) {   // (native_gu_rows_split's epilogue, on the same values)
719        const int nr = r1 - r0;
720        thread_local std::vector<float> buf;
721        thread_local std::vector<float*> go, uo;
722        buf.resize((size_t) 2 * nt * nr);
723        go.resize((size_t) nt);
724        uo.resize((size_t) nt);
725        for (int t = 0; t < nt; ++t) {
726            go[(size_t) t] = buf.data() + (size_t) t * nr;
727            uo[(size_t) t] = buf.data() + (size_t) (nt + t) * nr;
728        }
729        own_rows(f.tg, gate, f.gu_row, f.n_embd, act, nt, go.data(), r0, r1);
730        own_rows(f.tg, up, f.gu_row, f.n_embd, act, nt, uo.data(), r0, r1);
731        for (int t = 0; t < nt; ++t)
732            for (int r = 0; r < nr; ++r) {
733                float g = go[(size_t) t][r], u = uo[(size_t) t][r];
734                if (limit > 0.0f) {
735                    g = std::fmin(g, limit);
736                    u = std::fmin(std::fmax(u, -limit), limit);
737                }
738                ff[t][r0 + r] = (g / (1.f + std::exp(-g))) * u;
739            }
740        return;
741    }
742    kc::native_gu_rows_split(native(f), gate, up, act, nt, ff, r0, r1,
743                             limit > 0.0f ? limit : std::numeric_limits<float>::infinity());
744}
745
746void iq_cpu_down(const IqCpu& f, const uint8_t* down, const void* const* hq, int nt, float* const* out, int r0, int r1) {
747    if (r1 > r0 && own(f.td)) {
748        thread_local std::vector<float*> o;
749        o.resize((size_t) nt);
750        for (int t = 0; t < nt; ++t) o[(size_t) t] = out[t] + r0;
751        own_rows(f.td, down, f.d_row, f.n_ff, hq, nt, o.data(), r0, r1);
752        return;
753    }
754    kc::native_down_rows_split(native(f), down, hq, nt, out, r0, r1);
755}
756
757bool iq_cpu_own_rows(int type) { return own(type); }
758#else
759// (no ggml-cpu lane for the low-bit types here; the trellis types have their own: ds4_tq_cpu.cpp)
760bool iq_cpu_fmt(int tg, int td, int n_embd, int n_ff, IqCpu& f, std::string& err) {
761    if (tq_cpu_type(tg) && tq_cpu_type(td)) return tq_cpu_fmt(tg, td, n_embd, n_ff, f, err);
762    if (gg_cpu_type(tg) && gg_cpu_type(td)) return gg_cpu_fmt(tg, td, n_embd, n_ff, f, err);
763    err = "this build has no ggml-cpu (STRATA_NATIVE_EXPERTS=OFF): the low-bit experts CPU lane is not in it";
764    return false;
765}
766void iq_cpu_quant(int act, const float* x, int k, void* dst) {
767    if (act == tq_cpu_act()) tq_cpu_quant(x, k, dst);
768    else gg_cpu_quant(act, x, k, dst);
769}
770void iq_cpu_gate_up(const IqCpu& f, const uint8_t* gate, const uint8_t* up, const void* const* act, int nt, float* const* ff,
771                    int r0, int r1, float limit) {
772    if (tq_cpu_type(f.tg)) tq_cpu_gate_up(f, gate, up, act, nt, ff, r0, r1, limit);
773    else gg_cpu_gate_up(f, gate, up, act, nt, ff, r0, r1, limit);
774}
775void iq_cpu_down(const IqCpu& f, const uint8_t* down, const void* const* hq, int nt, float* const* out, int r0, int r1) {
776    if (tq_cpu_type(f.td)) tq_cpu_down(f, down, hq, nt, out, r0, r1);
777    else gg_cpu_down(f, down, hq, nt, out, r0, r1);
778}
779bool iq_cpu_own_rows(int) { return false; }
780#endif
781
782}  // namespace strata::ds4::gguf