okami.mom/strata

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

waveringanadsv41: IQ1_M AVX2 rows multiply the signed activations by ones instead of |g| (same pair sums; ~3% on the synthetic expert pass)bfc5fdb5f

main
39.8 KiB670 linesraw
1// src/kernels/cpu/iq_avx2_rows.inl - the row kernels of iq_avx2.cpp.  Not a header: iq_avx2.cpp includes it twice,
2//
3//   STRATA_ROWS_VNNI 0   the AVX2 forms (namespace plain);
4//   STRATA_ROWS_VNNI 1   the same loops with AVX-VNNI (namespace vnni, built with STRATA_AVXVNNI): vpdpwssd for each
5//                        madd + add, vpdpbusd for maddubs + madd(ones).  Integer sums, so the same bits.
6//
7// Every function here is STRATA_ROWS_FN: nothing in the plain copy, GCC/Clang's target("avxvnni") in the VNNI copy, so
8// the AVX-VNNI intrinsics compile in these functions only and the plain copy stays AVX2 (-mavxvnni on the whole file
9// would let the compiler use the extension anywhere).  MSVC takes the intrinsics under /arch:AVX2.  The VNNI copy is
10// called only where cpu_avxvnni_ok().
11
12// acc + madd(p16, sc).  vpdpwssd is the madd and the add in one uop; both wrap the same way.
13STRATA_ROWS_FN inline __m256i madd_add(__m256i acc, __m256i p16, __m256i sc) {
14#if STRATA_ROWS_VNNI
15    return _mm256_dpwssd_avx_epi32(acc, p16, sc);
16#else
17    return _mm256_add_epi32(acc, _mm256_madd_epi16(p16, sc));
18#endif
19}
20
21// Four u8 x s8 products per int32 lane.  vpdpbusd does not saturate where maddubs would, but maddubs cannot here: the
22// u8 side is an IQ4_NL |w| <= 127, so a pair is at most 2 * 127 * 128.
23STRATA_ROWS_FN inline __m256i dot4u(__m256i u8, __m256i s8) {
24#if STRATA_ROWS_VNNI
25    return _mm256_dpbusd_avx_epi32(_mm256_setzero_si256(), u8, s8);
26#else
27    return _mm256_madd_epi16(_mm256_maddubs_epi16(u8, s8), _mm256_set1_epi16(1));
28#endif
29}
30
31STRATA_ROWS_FN inline float ggml_hsum8(__m256 x) {
32    __m128 s = _mm_add_ps(_mm256_extractf128_ps(x, 1), _mm256_castps256_ps128(x));
33    s = _mm_add_ps(s, _mm_movehl_ps(s, s));
34    s = _mm_add_ss(s, _mm_movehdup_ps(s));
35    return _mm_cvtss_f32(s);
36}
37
38template <int NT, bool GGML = false> STRATA_ROWS_FN
39inline void row_dot_iq2xs(const uint8_t* row, int nblocks, const block_q8_K* const* y, float* res);
40
41template <int TY> STRATA_ROWS_FN
42inline float row_dot_ggml_one(const uint8_t* row, int nblocks, const block_q8_K* y) {
43    if constexpr (TY == 17) {
44        const block_q8_K* act[1] = {y};
45        float result = 0.0f;
46        row_dot_iq2xs<1, true>(row, nblocks, act, &result);
47        return result;
48    }
49    __m256 accum = _mm256_setzero_ps();
50    const int pf = prefetch_distance();
51    for (int i = 0; i < nblocks; ++i) {
52        const uint8_t* blk = row + (size_t) i * Fmt32<TY>::bytes;
53        rows_ahead(blk, pf);
54        __m256i sums[2] = {_mm256_setzero_si256(), _mm256_setzero_si256()};
55        for (int j = 0; j < 4; ++j)
56            for (int half = 0; half < 2; ++half) {
57                __m256i g, sign, scale;
58                Fmt32<TY>::decode(blk, j, half, g, sign, scale);
59                const __m256i act = _mm256_loadu_si256((const __m256i*) (y[i].qs + 64 * j + 32 * half));
60                const __m256i signed_act = _mm256_sign_epi8(act, sign);
61                sums[half] = madd_add(sums[half], _mm256_maddubs_epi16(g, signed_act), scale);
62            }
63        const float d = h2f(u16(blk)) * y[i].d;
64        accum = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sums[0], sums[1])), accum);
65    }
66    if constexpr (TY == 16 || TY == 17 || TY == 22 || TY == 122) return 0.125f * ggml_hsum8(accum);
67    else if constexpr (TY == 18 || TY == 118) return 0.25f * ggml_hsum8(accum);
68    else return ggml_hsum8(accum);
69}
70
71template <int TY> STRATA_ROWS_FN
72void gu_ggml_one(const uint8_t* blob, size_t gu_row, size_t up_off, int n, const block_q8_K* y, float* ff,
73                 int r0, int r1) {
74    for (int r = r0; r < r1; ++r) {
75        const float g = row_dot_ggml_one<TY>(blob + (size_t) r * gu_row, n / QK_K, y);
76        const float u = row_dot_ggml_one<TY>(blob + up_off + (size_t) r * gu_row, n / QK_K, y);
77        ff[r] = (g / (1.f + std::exp(-g))) * u;
78    }
79}
80
81template <int TY, int NT, bool EX = false> STRATA_ROWS_FN
82inline void row_dot(const uint8_t* row, int nblocks, const block_q8_K* const* y, float* res) {
83    const int pf = prefetch_distance();
84    __m256 accf[NT];
85    for (int t = 0; t < NT; ++t) accf[t] = _mm256_setzero_ps();
86    for (int i = 0; i < nblocks; ++i) {
87        const uint8_t* blk = row + (size_t) i * Fmt32<TY>::bytes;
88        rows_ahead(blk, pf);
89        // EX: independent integer accumulators a token (A of them) cut the dependent madd+add chain from eight steps to 8/A -
90        // few tokens have too little else in flight to hide it (integer sums, the same bits)
91        constexpr int A = EX ? (NT == 1 ? 4 : NT == 2 ? 2 : 1) : 1;
92        __m256i acci[NT][A];
93        for (int t = 0; t < NT; ++t)
94            for (int a = 0; a < A; ++a) acci[t][a] = _mm256_setzero_si256();
95        for (int j = 0; j < 4; ++j) {
96            for (int half = 0; half < 2; ++half) {
97                __m256i g, sgn, sc;
98                Fmt32<TY>::decode(blk, j, half, g, sgn, sc);
99                const int off = 64 * j + 32 * half;
100                for (int t = 0; t < NT; ++t) {
101                    const __m256i yv = _mm256_loadu_si256((const __m256i*) (y[t][i].qs + off));
102                    const __m256i ys = _mm256_sign_epi8(yv, sgn);
103                    acci[t][(2 * j + half) % A] = madd_add(acci[t][(2 * j + half) % A], _mm256_maddubs_epi16(g, ys), sc);
104                }
105            }
106        }
107        for (int t = 0; t < NT; ++t)
108            for (int a = 1; a < A; ++a) acci[t][0] = _mm256_add_epi32(acci[t][0], acci[t][a]);
109        // EX (the DeepSeek CPU lane's rows): ggml's float order, so a token's dot is the same bits as ggml-cpu's whatever
110        // number of tokens share the pass - block scale f16(d) * y.d, the format's constant K after the sum, ggml's hsum.
111        // Without EX the order of the main engine's rows (unchanged).
112        const float dx = EX ? h2f(u16(blk)) : h2f(u16(blk)) * Fmt32<TY>::K;
113        for (int t = 0; t < NT; ++t)
114            accf[t] = _mm256_fmadd_ps(_mm256_set1_ps(dx * y[t][i].d), _mm256_cvtepi32_ps(acci[t][0]), accf[t]);
115    }
116    for (int t = 0; t < NT; ++t) res[t] = EX ? Fmt32<TY>::K * ggml_hsum8(accf[t]) : hsum8(accf[t]);
117}
118
119// ---- IQ2_XS (17) with ggml's vectorized sign decode (arch/x86/quants.c, ggml_vec_dot_iq2_xs_q8_K):
120// the 7-bit sign indices never touch ksigns_iq2xs - the 8th sign bit is reconstructed by parity (two
121// shifts, a xor and ggml's bit_helper pshufb) and the per-half sign vectors come out of shuffles of one
122// 32-byte load.  The 8 scale bytes become all 16 half-scales with ggml's unpack trick.  The grid lookups
123// stay scalar loads into set_epi64x (ggml does the same).  Every token still only pays a load, a sign,
124// a maddubs, a madd and an add per half, into alternating accumulators.
125template <int NT, bool GGML> STRATA_ROWS_FN
126inline void row_dot_iq2xs(const uint8_t* row, int nblocks, const block_q8_K* const* y, float* res) {
127    static const uint8_t bit_sel[32] = {
128        1, 2, 4, 8, 16, 32, 64, (uint8_t) 0x80, 1, 2, 4, 8, 16, 32, 64, (uint8_t) 0x80,
129        1, 2, 4, 8, 16, 32, 64, (uint8_t) 0x80, 1, 2, 4, 8, 16, 32, 64, (uint8_t) 0x80 };
130    static const char shuf_even[32] = {   // sign bits of u16 0,1,2,3 (even bytes of the sign register)
131        0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2,
132        4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 6, 6 };
133    static const char shuf_odd[32] = {    // sign bits of u16 4,5,6,7
134        8, 8, 8, 8, 8, 8, 8, 8, 10, 10, 10, 10, 10, 10, 10, 10,
135        12, 12, 12, 12, 12, 12, 12, 12, 14, 14, 14, 14, 14, 14, 14, 14 };
136    static const uint8_t bit_help[32] = {  // 0x80 when the 4-bit index has odd popcount (parity bit 7)
137        0x00, (uint8_t) 0x80, (uint8_t) 0x80, 0x00, (uint8_t) 0x80, 0x00, 0x00, (uint8_t) 0x80,
138        (uint8_t) 0x80, 0x00, 0x00, (uint8_t) 0x80, 0x00, (uint8_t) 0x80, (uint8_t) 0x80, 0x00,
139        0x00, (uint8_t) 0x80, (uint8_t) 0x80, 0x00, (uint8_t) 0x80, 0x00, 0x00, (uint8_t) 0x80,
140        (uint8_t) 0x80, 0x00, 0x00, (uint8_t) 0x80, 0x00, (uint8_t) 0x80, (uint8_t) 0x80, 0x00 };
141    static const uint8_t k_sc_shuffle[128] = {  // half h -> scale bytes 2h (lanes 0-7), 2h+1 (lanes 8-15)
142        0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
143        2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
144        4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
145        6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
146        8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
147        10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11,
148        12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13,
149        14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15 };
150    const __m256i bsel = _mm256_loadu_si256((const __m256i*) bit_sel);
151    const __m256i shev = _mm256_loadu_si256((const __m256i*) shuf_even);
152    const __m256i shod = _mm256_loadu_si256((const __m256i*) shuf_odd);
153    const __m256i bhelp = _mm256_loadu_si256((const __m256i*) bit_help);
154    const __m256i m511 = _mm256_set1_epi16(511);
155    const __m128i m4 = _mm_set1_epi8(0xf), m1 = _mm_set1_epi8(1);
156    const __m256i one8 = _mm256_set1_epi8(1);
157    const int pf = prefetch_distance();
158
159    __m256 accf[NT];
160    for (int t = 0; t < NT; ++t) accf[t] = _mm256_setzero_ps();
161    for (int i = 0; i < nblocks; ++i) {
162        const uint8_t* blk = row + (size_t) i * 74;
163        rows_ahead(blk, pf);
164        // the 8 scale bytes -> 16 half-scales of 2*s+1, interleaved [a0, b0, a1, b1, ...] (ggml's unpack)
165        __m128i st = _mm_set1_epi64x((long long) u64(blk + 66));
166        st = _mm_unpacklo_epi8(_mm_and_si128(st, m4), _mm_and_si128(_mm_srli_epi16(st, 4), m4));
167        const __m128i scales = _mm_add_epi8(_mm_slli_epi16(st, 1), m1);
168        constexpr int CH = GGML ? 4 : 2;   // (EX: four independent integer chains a token)
169        __m256i acc[NT][CH];
170        for (int t = 0; t < NT; ++t)
171            for (int c = 0; c < CH; ++c) acc[t][c] = _mm256_setzero_si256();
172        for (int jj = 0; jj < 2; ++jj) {   // 128 values (16 u16) per step
173            const __m256i q2 = _mm256_loadu_si256((const __m256i*) (blk + 2 + 32 * jj));
174            const __m256i p7 = _mm256_srli_epi16(q2, 9);              // 7 sign bits per u16, even bytes
175            const __m256i p3 = _mm256_srli_epi16(q2, 13);             // sign index bits 4-6
176            const __m256i fsb = _mm256_or_si256(p7,
177                _mm256_shuffle_epi8(bhelp, _mm256_xor_si256(p7, p3)));  // + the parity sign bit 7
178            const __m256i s01 = _mm256_broadcastsi128_si256(_mm256_castsi256_si128(fsb)); // u16 0-7
179            const __m256i s23 = _mm256_broadcastsi128_si256(_mm256_extracti128_si256(fsb, 1)); // u16 8-15
180            alignas(32) uint16_t gi[16];
181            _mm256_store_si256((__m256i*) gi, _mm256_and_si256(q2, m511));
182            for (int h = 0; h < 4; ++h) {   // the four 32-value halves of this group
183                const int H = 4 * jj + h;   // global half index -> scales byte pair
184                const __m256i g = _mm256_set_epi64x((long long) iq2xs_grid[gi[4 * h + 3]],
185                                                   (long long) iq2xs_grid[gi[4 * h + 2]],
186                                                   (long long) iq2xs_grid[gi[4 * h + 1]],
187                                                   (long long) iq2xs_grid[gi[4 * h]]);
188                const __m256i sb = _mm256_shuffle_epi8(h < 2 ? s01 : s23, (h & 1) ? shod : shev);
189                const __m256i sgn = _mm256_or_si256(
190                    _mm256_cmpeq_epi8(_mm256_and_si256(sb, bsel), bsel), one8);
191                const __m256i sc = _mm256_cvtepi8_epi16(
192                    _mm_shuffle_epi8(scales, _mm_loadu_si128((const __m128i*) k_sc_shuffle + H)));
193                const int off = 128 * jj + 32 * h;
194                for (int t = 0; t < NT; ++t) {
195                    const __m256i yv = _mm256_loadu_si256((const __m256i*) (y[t][i].qs + off));
196                    const __m256i ys = _mm256_sign_epi8(yv, sgn);
197                    acc[t][h % CH] = madd_add(acc[t][h % CH], _mm256_maddubs_epi16(g, ys), sc);
198                }
199            }
200        }
201        for (int t = 0; t < NT; ++t) {
202            const float d = h2f(u16(blk)) * (GGML ? y[t][i].d : 0.125f);
203            const float scale = GGML ? d : d * y[t][i].d;
204            accf[t] = _mm256_fmadd_ps(_mm256_set1_ps(scale),
205                _mm256_cvtepi32_ps(CH == 4 ? _mm256_add_epi32(_mm256_add_epi32(acc[t][0], acc[t][1]), _mm256_add_epi32(acc[t][2], acc[t][3]))
206                                                       : _mm256_add_epi32(acc[t][0], acc[t][1])), accf[t]);
207        }
208    }
209    for (int t = 0; t < NT; ++t) res[t] = GGML ? 0.125f * ggml_hsum8(accf[t]) : hsum8(accf[t]);
210}
211
212// ---- IQ1_M (29): 56-byte blocks (qs[32], qh[16], scales[8] holding the 3-bit sub-scales and, in their top nibbles, the
213// block's f16 d), ggml's ggml_vec_dot_iq1_m_q8_K (AVX2): s = hsum(sum d*sumi1) + 0.125 * hsum(sum d*sumi2), sumi1 the
214// grid dot, sumi2 the +-1 delta dot.  Both are kept lane for lane as ggml has them, so the bits are ggml's.
215//  - the grid values are -1/0/1 bytes: |g| and g's sign (the two halves of ggml's mul_add_epi8) are taken once per weight chunk;
216//  - the delta dot needs no weight bytes beyond one sign a group of 8: sign(q8, delta) summed in pairs and times the scale is
217//    (pair sums of q8) times (+-scale), so the pair sums of every activation chunk are made once per call (iq1m_pre_build) and
218//    a token's delta dot is one madd per 32 values;
219//  - the indices' high bits and the delta signs of eight groups come out of one 32-bit load by variable shifts (ggml's
220//    non-BMI2 path branches on every bit).
221
222// the pair sums maddubs(1, q8) of every 32-value chunk of an activation: 16 int16 a chunk, 8 chunks a block
223STRATA_ROWS_FN inline void iq1m_pre_build(const block_q8_K* y, int nblocks, int16_t* p) {
224    const __m256i one8 = _mm256_set1_epi8(1);
225    for (int i = 0; i < nblocks; ++i)
226        for (int c = 0; c < 8; ++c)
227            _mm256_storeu_si256((__m256i*) (p + ((size_t) i * 8 + c) * 16),
228                                _mm256_maddubs_epi16(one8, _mm256_loadu_si256((const __m256i*) (y[i].qs + 32 * c))));
229}
230
231struct Iq1MChunk { __m256i a1, g1, a2, g2, sa, sb, ssa, ssb; };
232
233STRATA_ROWS_FN inline void iq1m_apply(const Iq1MChunk& dc, const int8_t* q8, const int16_t* pairs,
234                                    __m256i& s1, __m256i& s2) {
235    const __m256i a = _mm256_loadu_si256((const __m256i*) q8);
236    const __m256i b = _mm256_loadu_si256((const __m256i*) (q8 + 32));
237    const __m256i d1 = _mm256_maddubs_epi16(dc.a1, _mm256_sign_epi8(a, dc.g1));
238    const __m256i d2 = _mm256_maddubs_epi16(dc.a2, _mm256_sign_epi8(b, dc.g2));
239    s1 = _mm256_add_epi32(s1, _mm256_add_epi32(_mm256_madd_epi16(d1, dc.sa), _mm256_madd_epi16(d2, dc.sb)));
240    s2 = _mm256_add_epi32(s2, _mm256_add_epi32(_mm256_madd_epi16(_mm256_loadu_si256((const __m256i*) pairs), dc.ssa),
241                                             _mm256_madd_epi16(_mm256_loadu_si256((const __m256i*) (pairs + 16)), dc.ssb)));
242}
243
244template <int NT> STRATA_ROWS_FN
245inline void row_dot_iq1m(const uint8_t* row, int nblocks, const block_q8_K* const* y, const int16_t* const* P, float* res) {
246    const __m256i mask = _mm256_set1_epi16(0x7);
247    const __m256i mone = _mm256_set1_epi16(1);
248    const __m256i mtwo8 = _mm256_set1_epi8(2);
249    const __m256i scales_shift = _mm256_set_epi64x(9, 3, 6, 0);
250    const __m256i nib_shift = _mm256_setr_epi32(0, 4, 8, 12, 16, 20, 24, 28);
251    const __m256i m7 = _mm256_set1_epi32(7), m8 = _mm256_set1_epi32(8);
252    const int pf = prefetch_distance();
253    __m256 acc1[NT], acc2[NT];
254    for (int t = 0; t < NT; ++t) acc1[t] = acc2[t] = _mm256_setzero_ps();
255    for (int i = 0; i < nblocks; ++i) {
256        const uint8_t* blk = row + (size_t) i * 56;
257        rows_ahead(blk, pf);
258        const uint8_t* qs = blk;
259        const uint8_t* qh = blk + 32;
260        const uint8_t* scb = blk + 48;
261        const uint16_t sc0 = u16(scb), sc1 = u16(scb + 2), sc2 = u16(scb + 4), sc3 = u16(scb + 6);
262        const uint16_t d16 = (uint16_t) ((sc0 >> 12) | ((sc1 >> 8) & 0x00f0) | ((sc2 >> 4) & 0x0f00) | (sc3 & 0xf000));
263        __m256i scales = _mm256_set1_epi64x((long long) u64(scb));
264        scales = _mm256_srlv_epi64(scales, scales_shift);
265        scales = _mm256_add_epi16(_mm256_slli_epi16(_mm256_and_si256(scales, mask), 1), mone);
266        __m256i sidx1 = _mm256_set1_epi16(0x0100);
267        __m256i sidx2 = _mm256_add_epi8(sidx1, _mm256_set1_epi8(8));
268        // Consume singleton chunks immediately; multiple tokens share the decoded chunks on the stack.
269        Iq1MChunk dec[4];
270        __m256i direct1 = _mm256_setzero_si256(), direct2 = _mm256_setzero_si256();
271        for (int it = 0; it < 4; ++it) {
272            const __m256i v8 = _mm256_srlv_epi32(_mm256_set1_epi32((int) u32(qh + 4 * it)), nib_shift);   // group k's qh nibble
273            const __m256i idx = _mm256_or_si256(_mm256_cvtepu8_epi32(_mm_loadl_epi64((const __m128i*) (qs + 8 * it))),
274                                                _mm256_slli_epi32(_mm256_and_si256(v8, m7), 8));
275            alignas(32) int32_t ix[8];
276            _mm256_store_si256((__m256i*) ix, idx);
277            const __m256i g1 = _mm256_set_epi64x((long long) iq1s_grid[ix[3]], (long long) iq1s_grid[ix[2]],
278                                                 (long long) iq1s_grid[ix[1]], (long long) iq1s_grid[ix[0]]);
279            const __m256i g2 = _mm256_set_epi64x((long long) iq1s_grid[ix[7]], (long long) iq1s_grid[ix[6]],
280                                                 (long long) iq1s_grid[ix[5]], (long long) iq1s_grid[ix[4]]);
281            const __m256i neg = _mm256_cmpeq_epi32(_mm256_and_si256(v8, m8), m8);   // delta -1: all ones a group
282            const __m256i n1 = _mm256_cvtepi32_epi64(_mm256_castsi256_si128(neg));   // 4 int16 lanes a group of 8 values
283            const __m256i n2 = _mm256_cvtepi32_epi64(_mm256_extracti128_si256(neg, 1));
284            const __m256i sc_a = _mm256_shuffle_epi8(scales, sidx1);
285            const __m256i sc_b = _mm256_shuffle_epi8(scales, sidx2);
286            // (maddubs by ones, not |g|: sign(q8, g) is already 0 where g is, so the pair sums are the same; vgather and
287            // movq/movhps loads of the grid measured slower here, Zen 3)
288            const __m256i one8 = _mm256_set1_epi8(1);
289            const Iq1MChunk dc{one8, g1, one8, g2, sc_a, sc_b,
290                              _mm256_sign_epi16(sc_a, _mm256_or_si256(n1, mone)), _mm256_sign_epi16(sc_b, _mm256_or_si256(n2, mone))};
291            if constexpr (NT == 1) {
292                iq1m_apply(dc, y[0][i].qs + 64 * it, P[0] + ((size_t) i * 8 + 2 * it) * 16, direct1, direct2);
293            } else dec[it] = dc;
294            sidx1 = _mm256_add_epi8(sidx1, mtwo8);
295            sidx2 = _mm256_add_epi8(sidx2, mtwo8);
296        }
297        const float dx = h2f(d16);
298        for (int t = 0; t < NT; ++t) {
299            __m256i s1 = direct1, s2 = direct2;
300            if constexpr (NT != 1) {
301                for (int it = 0; it < 4; ++it)
302                    iq1m_apply(dec[it], y[t][i].qs + 64 * it, P[t] + ((size_t) i * 8 + 2 * it) * 16, s1, s2);
303            }
304            const __m256 d = _mm256_set1_ps(y[t][i].d * dx);
305            acc1[t] = _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(s1), acc1[t]);
306            acc2[t] = _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(s2), acc2[t]);
307        }
308    }
309    for (int t = 0; t < NT; ++t) res[t] = ggml_hsum8(acc1[t]) + 0.125f * ggml_hsum8(acc2[t]);
310}
311
312template <int TY, int NT, bool EX = false> STRATA_ROWS_FN
313inline void row_dot_any(const uint8_t* row, int nblocks, const block_q8_K* const* y, float* res,
314                        const int16_t* const* aux = nullptr) {
315    if constexpr (TY == 29) row_dot_iq1m<NT>(row, nblocks, y, aux, res);
316    else if constexpr (TY == 17) row_dot_iq2xs<NT, EX>(row, nblocks, y, res);
317    else                    row_dot<TY, NT, EX>(row, nblocks, y, res);
318}
319
320template <int TY, int NT, bool EX> STRATA_ROWS_FN
321void gu_rows(const uint8_t* blob, size_t gu_row, size_t up_off, int n, const void* const* act, float* const* ff,
322             int r0, int r1) {
323    const block_q8_K* y[NT];
324    for (int t = 0; t < NT; ++t) y[t] = (const block_q8_K*) act[t];
325    const int nb = n / QK_K;
326    const int16_t* aux[NT] = {};
327    if constexpr (TY == 29) {   // (IQ1_M's activation pair sums, once for the rows of this call)
328        static thread_local std::vector<int16_t> pre;
329        pre.resize((size_t) NT * nb * 128);
330        for (int t = 0; t < NT; ++t) {
331            iq1m_pre_build(y[t], nb, pre.data() + (size_t) t * nb * 128);
332            aux[t] = pre.data() + (size_t) t * nb * 128;
333        }
334    }
335    float g[NT], u[NT];
336    for (int r = r0; r < r1; ++r) {
337        row_dot_any<TY, NT, EX>(blob + (size_t) r * gu_row, nb, y, g, aux);
338        row_dot_any<TY, NT, EX>(blob + up_off + (size_t) r * gu_row, nb, y, u, aux);
339        for (int t = 0; t < NT; ++t) ff[t][r] = (g[t] / (1.f + std::exp(-g[t]))) * u[t];
340    }
341}
342
343template <int TY, int NT, bool EX> STRATA_ROWS_FN
344void dot_rows(const uint8_t* w, size_t row_bytes, int n, const void* const* act, float* const* out, int r0, int r1) {
345    const block_q8_K* y[NT];
346    for (int t = 0; t < NT; ++t) y[t] = (const block_q8_K*) act[t];
347    const int nb = n / QK_K;
348    const int16_t* aux[NT] = {};
349    if constexpr (TY == 29) {   // (IQ1_M's activation pair sums, once for the rows of this call)
350        static thread_local std::vector<int16_t> pre;
351        pre.resize((size_t) NT * nb * 128);
352        for (int t = 0; t < NT; ++t) {
353            iq1m_pre_build(y[t], nb, pre.data() + (size_t) t * nb * 128);
354            aux[t] = pre.data() + (size_t) t * nb * 128;
355        }
356    }
357    float res[NT];
358    for (int r = r0; r < r1; ++r) {
359        row_dot_any<TY, NT, EX>(w + (size_t) r * row_bytes, nb, y, res, aux);
360        for (int t = 0; t < NT; ++t) out[t][r] = res[t];
361    }
362}
363
364// ---- many tokens an expert (the prompt: a chunk's tokens routed to one expert), EX rows only.
365// The weights of a 256-value block are decoded once into signed int8 (the grid magnitude times the sign) and every token of the
366// call applies them.  The activations come as u8 (q8 + 128, made once per call for all the rows): maddubs(act_u8, w_s8) needs no
367// per-token sign, and sum(w * q) = sum(w * (q + 128)) - 128 * sum(w) with the second sum made once per weight block (corr), so the
368// integer sums are exactly ggml's, lane for lane (no int16 overflow: |w| <= 62 for these grids, 255 * 62 * 2 < 32767).
369// A token costs, per 32 values, one maddubs, one madd and one add (the single-token form has a sign on top).
370constexpr int kWideTokens = 64;
371
372STRATA_ROWS_FN inline void wide_prep(const block_q8_K* y, int nb, uint8_t* u) {
373    const __m256i flip = _mm256_set1_epi8((char) 0x80);
374    for (int i = 0; i < nb; ++i)
375        for (int c = 0; c < 8; ++c)
376            _mm256_storeu_si256((__m256i*) (u + (size_t) i * 256 + 32 * c),
377                                _mm256_xor_si256(_mm256_loadu_si256((const __m256i*) (y[i].qs + 32 * c)), flip));
378}
379
380template <int TY> STRATA_ROWS_FN
381void dot_rows_wide(const uint8_t* w, size_t row_bytes, int n, const void* const* act, int T, float* const* out, int r0, int r1) {
382    // tokens in tiles of kTile and rows in groups of kRows: a tile's activation slice of one block (kTile * 256 B) and the group's
383    // accumulators (kRows * kTile * 32 B) stay in L1 while the group's rows go past, the tile's whole activation in L2
384#ifndef STRATA_WIDE_TILE
385#define STRATA_WIDE_TILE 16
386#endif
387#ifndef STRATA_WIDE_ROWS
388#define STRATA_WIDE_ROWS 8
389#endif
390    constexpr int kTile = STRATA_WIDE_TILE, kRows = STRATA_WIDE_ROWS;
391    const int nb = n / QK_K;
392    static thread_local std::vector<uint8_t> ubuf;
393    static thread_local std::vector<float> accb;
394    ubuf.resize((size_t) (T < kTile ? T : kTile) * n);
395    accb.resize((size_t) kRows * kTile * 8);
396    const __m256i one8 = _mm256_set1_epi8(1);
397    const int pf = prefetch_distance();
398    for (int t0 = 0; t0 < T; t0 += kTile) {
399        const int tt = T - t0 < kTile ? T - t0 : kTile;
400        const block_q8_K* y[kTile];
401        for (int t = 0; t < tt; ++t) {
402            y[t] = (const block_q8_K*) act[t0 + t];
403            wide_prep(y[t], nb, ubuf.data() + (size_t) t * n);
404        }
405        for (int rg = r0; rg < r1; rg += kRows) {
406            const int rr = r1 - rg < kRows ? r1 - rg : kRows;
407            for (int k = 0; k < rr * tt; ++k) _mm256_storeu_ps(&accb[(size_t) k * 8], _mm256_setzero_ps());
408            for (int i = 0; i < nb; ++i) {
409                for (int q = 0; q < rr; ++q) {
410                    const uint8_t* blk = w + (size_t) (rg + q) * row_bytes + (size_t) i * Fmt32<TY>::bytes;
411                    rows_ahead(blk, pf);
412                    __m256i w8[8], sc[8];
413                    __m256i corr = _mm256_setzero_si256();
414                    for (int j = 0; j < 4; ++j)
415                        for (int half = 0; half < 2; ++half) {
416                            __m256i g, sgn, s;
417                            Fmt32<TY>::decode(blk, j, half, g, sgn, s);
418                            const int k = 2 * j + half;
419                            w8[k] = _mm256_sign_epi8(g, sgn);
420                            sc[k] = s;
421                            corr = madd_add(corr, _mm256_maddubs_epi16(one8, w8[k]), s);
422                        }
423                    const __m256i corr128 = _mm256_slli_epi32(corr, 7);
424                    const float dx = h2f(u16(blk));
425                    float* accq = &accb[(size_t) q * tt * 8];
426                    for (int t = 0; t < tt; ++t) {
427                        const uint8_t* u = ubuf.data() + (size_t) t * n + (size_t) i * 256;
428                        __m256i a0 = _mm256_setzero_si256(), a1 = _mm256_setzero_si256();
429                        for (int k = 0; k < 8; k += 2) {
430                            a0 = madd_add(a0, _mm256_maddubs_epi16(_mm256_loadu_si256((const __m256i*) (u + 32 * k)), w8[k]), sc[k]);
431                            a1 = madd_add(a1, _mm256_maddubs_epi16(_mm256_loadu_si256((const __m256i*) (u + 32 * k + 32)), w8[k + 1]), sc[k + 1]);
432                        }
433                        const __m256i s = _mm256_sub_epi32(_mm256_add_epi32(a0, a1), corr128);
434                        float* ap = accq + (size_t) t * 8;
435                        _mm256_storeu_ps(ap, _mm256_fmadd_ps(_mm256_set1_ps(dx * y[t][i].d), _mm256_cvtepi32_ps(s), _mm256_loadu_ps(ap)));
436                    }
437                }
438            }
439            for (int q = 0; q < rr; ++q)
440                for (int t = 0; t < tt; ++t)
441                    out[t0 + t][rg + q] = Fmt32<TY>::K * ggml_hsum8(_mm256_loadu_ps(&accb[((size_t) q * tt + t) * 8]));
442        }
443    }
444}
445
446// IQ1_M the same way: its grid bytes are already signed (-1, 0, 1), and the delta dot keeps the pair sums of the activation
447// (maddubs(1, q8), made once per tile).  Two float accumulators a (row, token), ggml's sum d*sumi1 and sum d*sumi2.
448STRATA_ROWS_FN
449void dot_rows_wide_iq1m(const uint8_t* w, size_t row_bytes, int n, const void* const* act, int T, float* const* out, int r0, int r1) {
450    constexpr int kTile = STRATA_WIDE_TILE, kRows = STRATA_WIDE_ROWS;
451    const int nb = n / QK_K;
452    static thread_local std::vector<uint8_t> ubuf;
453    static thread_local std::vector<int16_t> pbuf;
454    static thread_local std::vector<float> accb;
455    ubuf.resize((size_t) (T < kTile ? T : kTile) * n);
456    pbuf.resize((size_t) (T < kTile ? T : kTile) * n);
457    accb.resize((size_t) kRows * kTile * 16);
458    const __m256i one8 = _mm256_set1_epi8(1);
459    const __m256i mask = _mm256_set1_epi16(0x7);
460    const __m256i mone = _mm256_set1_epi16(1);
461    const __m256i mtwo8 = _mm256_set1_epi8(2);
462    const __m256i scales_shift = _mm256_set_epi64x(9, 3, 6, 0);
463    const __m256i nib_shift = _mm256_setr_epi32(0, 4, 8, 12, 16, 20, 24, 28);
464    const __m256i m7 = _mm256_set1_epi32(7), m8 = _mm256_set1_epi32(8);
465    const int pf = prefetch_distance();
466    for (int t0 = 0; t0 < T; t0 += kTile) {
467        const int tt = T - t0 < kTile ? T - t0 : kTile;
468        const block_q8_K* y[kTile];
469        for (int t = 0; t < tt; ++t) {
470            y[t] = (const block_q8_K*) act[t0 + t];
471            wide_prep(y[t], nb, ubuf.data() + (size_t) t * n);
472            iq1m_pre_build(y[t], nb, pbuf.data() + (size_t) t * n);
473        }
474        for (int rg = r0; rg < r1; rg += kRows) {
475            const int rr = r1 - rg < kRows ? r1 - rg : kRows;
476            for (int k = 0; k < rr * tt * 2; ++k) _mm256_storeu_ps(&accb[(size_t) k * 8], _mm256_setzero_ps());
477            for (int i = 0; i < nb; ++i) {
478                for (int q = 0; q < rr; ++q) {
479                    const uint8_t* blk = w + (size_t) (rg + q) * row_bytes + (size_t) i * 56;
480                    rows_ahead(blk, pf);
481                    const uint8_t* qs = blk;
482                    const uint8_t* qh = blk + 32;
483                    const uint8_t* scb = blk + 48;
484                    const uint16_t sc0 = u16(scb), sc1 = u16(scb + 2), sc2 = u16(scb + 4), sc3 = u16(scb + 6);
485                    const uint16_t d16 = (uint16_t) ((sc0 >> 12) | ((sc1 >> 8) & 0x00f0) | ((sc2 >> 4) & 0x0f00) | (sc3 & 0xf000));
486                    __m256i scales = _mm256_set1_epi64x((long long) u64(scb));
487                    scales = _mm256_srlv_epi64(scales, scales_shift);
488                    scales = _mm256_add_epi16(_mm256_slli_epi16(_mm256_and_si256(scales, mask), 1), mone);
489                    __m256i sidx1 = _mm256_set1_epi16(0x0100);
490                    __m256i sidx2 = _mm256_add_epi8(sidx1, _mm256_set1_epi8(8));
491                    __m256i g1[4], g2[4], sa[4], sb[4], ssa[4], ssb[4];
492                    __m256i corr = _mm256_setzero_si256();
493                    for (int it = 0; it < 4; ++it) {
494                        const __m256i v8 = _mm256_srlv_epi32(_mm256_set1_epi32((int) u32(qh + 4 * it)), nib_shift);
495                        const __m256i idx = _mm256_or_si256(_mm256_cvtepu8_epi32(_mm_loadl_epi64((const __m128i*) (qs + 8 * it))),
496                                                            _mm256_slli_epi32(_mm256_and_si256(v8, m7), 8));
497                        alignas(32) int32_t ix[8];
498                        _mm256_store_si256((__m256i*) ix, idx);
499                        g1[it] = _mm256_set_epi64x((long long) iq1s_grid[ix[3]], (long long) iq1s_grid[ix[2]],
500                                                   (long long) iq1s_grid[ix[1]], (long long) iq1s_grid[ix[0]]);
501                        g2[it] = _mm256_set_epi64x((long long) iq1s_grid[ix[7]], (long long) iq1s_grid[ix[6]],
502                                                   (long long) iq1s_grid[ix[5]], (long long) iq1s_grid[ix[4]]);
503                        const __m256i neg = _mm256_cmpeq_epi32(_mm256_and_si256(v8, m8), m8);
504                        const __m256i n1 = _mm256_cvtepi32_epi64(_mm256_castsi256_si128(neg));
505                        const __m256i n2 = _mm256_cvtepi32_epi64(_mm256_extracti128_si256(neg, 1));
506                        sa[it] = _mm256_shuffle_epi8(scales, sidx1);
507                        sb[it] = _mm256_shuffle_epi8(scales, sidx2);
508                        ssa[it] = _mm256_sign_epi16(sa[it], _mm256_or_si256(n1, mone));
509                        ssb[it] = _mm256_sign_epi16(sb[it], _mm256_or_si256(n2, mone));
510                        sidx1 = _mm256_add_epi8(sidx1, mtwo8);
511                        sidx2 = _mm256_add_epi8(sidx2, mtwo8);
512                        corr = madd_add(madd_add(corr, _mm256_maddubs_epi16(one8, g1[it]), sa[it]), _mm256_maddubs_epi16(one8, g2[it]), sb[it]);
513                    }
514                    const __m256i corr128 = _mm256_slli_epi32(corr, 7);
515                    const float dx = h2f(d16);
516                    float* accq = &accb[(size_t) q * tt * 16];
517                    for (int t = 0; t < tt; ++t) {
518                        const uint8_t* u = ubuf.data() + (size_t) t * n + (size_t) i * 256;
519                        const int16_t* pp = pbuf.data() + (size_t) t * n + (size_t) i * 128;
520                        __m256i a0 = _mm256_setzero_si256(), a1 = _mm256_setzero_si256();
521                        __m256i b0 = _mm256_setzero_si256(), b1 = _mm256_setzero_si256();
522                        for (int it = 0; it < 4; ++it) {
523                            a0 = madd_add(a0, _mm256_maddubs_epi16(_mm256_loadu_si256((const __m256i*) (u + 64 * it)), g1[it]), sa[it]);
524                            a1 = madd_add(a1, _mm256_maddubs_epi16(_mm256_loadu_si256((const __m256i*) (u + 64 * it + 32)), g2[it]), sb[it]);
525                            b0 = madd_add(b0, _mm256_loadu_si256((const __m256i*) (pp + 32 * it)), ssa[it]);
526                            b1 = madd_add(b1, _mm256_loadu_si256((const __m256i*) (pp + 32 * it + 16)), ssb[it]);
527                        }
528                        const __m256i s1 = _mm256_sub_epi32(_mm256_add_epi32(a0, a1), corr128);
529                        const __m256i s2 = _mm256_add_epi32(b0, b1);
530                        const __m256 d = _mm256_set1_ps(y[t][i].d * dx);
531                        float* ap = accq + (size_t) t * 16;
532                        _mm256_storeu_ps(ap, _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(s1), _mm256_loadu_ps(ap)));
533                        _mm256_storeu_ps(ap + 8, _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(s2), _mm256_loadu_ps(ap + 8)));
534                    }
535                }
536            }
537            for (int q = 0; q < rr; ++q)
538                for (int t = 0; t < tt; ++t) {
539                    const float* ap = &accb[((size_t) q * tt + t) * 16];
540                    out[t0 + t][rg + q] = ggml_hsum8(_mm256_loadu_ps(ap)) + 0.125f * ggml_hsum8(_mm256_loadu_ps(ap + 8));
541                }
542        }
543    }
544}
545
546template <int TY, bool EX> STRATA_ROWS_FN
547void gu_rows_nt(int nt, const uint8_t* blob, size_t gu_row, size_t up_off, int n, const void* const* act,
548                float* const* ff, int r0, int r1) {
549    switch (nt) {
550        case 1: gu_rows<TY, 1, EX>(blob, gu_row, up_off, n, act, ff, r0, r1); break;
551        case 2: gu_rows<TY, 2, EX>(blob, gu_row, up_off, n, act, ff, r0, r1); break;
552        case 3: gu_rows<TY, 3, EX>(blob, gu_row, up_off, n, act, ff, r0, r1); break;
553        case 4: gu_rows<TY, 4, EX>(blob, gu_row, up_off, n, act, ff, r0, r1); break;
554        case 5: gu_rows<TY, 5, EX>(blob, gu_row, up_off, n, act, ff, r0, r1); break;
555        case 6: gu_rows<TY, 6, EX>(blob, gu_row, up_off, n, act, ff, r0, r1); break;
556        case 7: gu_rows<TY, 7, EX>(blob, gu_row, up_off, n, act, ff, r0, r1); break;
557        default: gu_rows<TY, 8, EX>(blob, gu_row, up_off, n, act, ff, r0, r1); break;
558    }
559}
560
561template <int TY, bool EX> STRATA_ROWS_FN
562void dot_rows_nt(int nt, const uint8_t* w, size_t row_bytes, int n, const void* const* act, float* const* out, int r0,
563                 int r1) {
564    if constexpr (EX) {
565        if (nt >= 7) {   // (a prompt chunk's tokens: one decode of a block for all of them)
566            if constexpr (TY == 29) dot_rows_wide_iq1m(w, row_bytes, n, act, nt, out, r0, r1);
567            else dot_rows_wide<TY>(w, row_bytes, n, act, nt, out, r0, r1);
568            return;
569        }
570    }
571    switch (nt) {
572        case 1: dot_rows<TY, 1, EX>(w, row_bytes, n, act, out, r0, r1); break;
573        case 2: dot_rows<TY, 2, EX>(w, row_bytes, n, act, out, r0, r1); break;
574        case 3: dot_rows<TY, 3, EX>(w, row_bytes, n, act, out, r0, r1); break;
575        case 4: dot_rows<TY, 4, EX>(w, row_bytes, n, act, out, r0, r1); break;
576        case 5: if constexpr (EX) { dot_rows<TY, 5, EX>(w, row_bytes, n, act, out, r0, r1); break; }   // (the DeepSeek lane's verify
577        [[fallthrough]];                                                                              //  rounds: one decode of 5 or 6)
578        case 6: if constexpr (EX) { dot_rows<TY, 6, EX>(w, row_bytes, n, act, out, r0, r1); break; }
579        [[fallthrough]];
580        default: for (int t0 = 0; t0 < nt; t0 += 4) {
581            const int k = nt - t0 < 4 ? nt - t0 : 4;
582            dot_rows_nt<TY, EX>(k, w, row_bytes, n, act + t0, out + t0, r0, r1);
583        }
584    }
585}
586
587// The decode of each format for a variant: the gathered one (kIq256Gather) for IQ3_XXS, IQ3_S and IQ2_S.
588template <bool G, bool EX = false> STRATA_ROWS_FN
589void gu_type(int type, int nt, const uint8_t* blob, size_t gu_row, size_t up_off, int n, const void* const* act,
590             float* const* ff, int r0, int r1) {
591    switch (type) {
592        case 16: gu_rows_nt<16, EX>(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break;
593        case 17: gu_rows_nt<17, EX>(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break;
594        case 18: gu_rows_nt<G ? 118 : 18, EX>(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break;
595        case 21: gu_rows_nt<G ? 121 : 21, EX>(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break;
596        case 22: gu_rows_nt<G ? 122 : 22, EX>(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break;
597        case 23: gu_rows_nt<23, EX>(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break;
598        case 29: gu_rows_nt<29, EX>(nt, blob, gu_row, up_off, n, act, ff, r0, r1); break;
599        default: break;
600    }
601}
602
603template <bool G, bool EX = false> STRATA_ROWS_FN
604void dot_type(int type, int nt, const uint8_t* w, size_t row_bytes, int n, const void* const* act, float* const* out,
605              int r0, int r1) {
606    switch (type) {
607        case 16: dot_rows_nt<16, EX>(nt, w, row_bytes, n, act, out, r0, r1); break;
608        case 17: dot_rows_nt<17, EX>(nt, w, row_bytes, n, act, out, r0, r1); break;
609        case 18: dot_rows_nt<G ? 118 : 18, EX>(nt, w, row_bytes, n, act, out, r0, r1); break;
610        case 21: dot_rows_nt<G ? 121 : 21, EX>(nt, w, row_bytes, n, act, out, r0, r1); break;
611        case 22: dot_rows_nt<G ? 122 : 22, EX>(nt, w, row_bytes, n, act, out, r0, r1); break;
612        case 23: dot_rows_nt<23, EX>(nt, w, row_bytes, n, act, out, r0, r1); break;
613        case 29: dot_rows_nt<29, EX>(nt, w, row_bytes, n, act, out, r0, r1); break;
614        default: break;
615    }
616}
617
618// ---- IQ4_NL (type 20): 32-value blocks of 18 bytes (f16 d + 16 nibble bytes) against Q8_0 activations.
619// ggml-cpu's own AVX-2 dot (ggml_vec_dot_iq4_nl_q8_0) is single-token: every token redoes the nibble ->
620// kvalues_iq4nl pshufb decode and the |w| half of the signed x signed product.  Here both are computed once
621// per block; every token then costs a sign, a maddubs, a madd and an fmadd.  The arithmetic is ggml's - only
622// the order of the float additions differs.
623template <int NT> STRATA_ROWS_FN
624void iq4nl_rows(const uint8_t* w, size_t row_bytes, int n, const block_q8_0* const* y, float* const* out,
625                int r0, int r1) {
626    const __m128i values = _mm_loadu_si128((const __m128i*) kvalues_iq4nl);
627    const __m128i m4b = _mm_set1_epi8(0x0f);
628    const int nb = n / QK4_NL;
629    const int pf = prefetch_distance();
630    for (int r = r0; r < r1; ++r) {
631        const uint8_t* row = w + (size_t) r * row_bytes;
632        __m256 accf[NT];
633        for (int t = 0; t < NT; ++t) accf[t] = _mm256_setzero_ps();
634        for (int ib = 0; ib < nb; ++ib) {
635            const uint8_t* blk = row + (size_t) ib * sizeof(block_iq4_nl);
636            rows_ahead(blk, pf);
637            const __m128i bits = _mm_loadu_si128((const __m128i*) (blk + 2));
638            const __m128i lo = _mm_and_si128(bits, m4b);                      // values 0..15
639            const __m128i hi = _mm_and_si128(_mm_srli_epi16(bits, 4), m4b);    // values 16..31
640            const __m256i q4 = _mm256_inserti128_si256(_mm256_castsi128_si256(_mm_shuffle_epi8(values, lo)),
641                                                       _mm_shuffle_epi8(values, hi), 1);
642            const __m256i aq = _mm256_sign_epi8(q4, q4);   // |w|: the unsigned operand of maddubs
643            const float dx = h2f(u16(blk));
644            for (int t = 0; t < NT; ++t) {
645                const block_q8_0& b = y[t][ib];
646                const __m256i q8 = _mm256_loadu_si256((const __m256i*) b.qs);
647                const __m256i p = dot4u(aq, _mm256_sign_epi8(q8, q4));
648                accf[t] = _mm256_fmadd_ps(_mm256_set1_ps(dx * h2f(b.d)), _mm256_cvtepi32_ps(p), accf[t]);
649            }
650        }
651        for (int t = 0; t < NT; ++t) out[t][r] = hsum8(accf[t]);
652    }
653}
654
655STRATA_ROWS_FN void iq4nl_nt(const uint8_t* w, size_t row_bytes, int n, const block_q8_0* const* y, int nt,
656                             float* const* out, int r0, int r1) {
657    switch (nt) {
658        case 1: iq4nl_rows<1>(w, row_bytes, n, y, out, r0, r1); break;
659        case 2: iq4nl_rows<2>(w, row_bytes, n, y, out, r0, r1); break;
660        case 3: iq4nl_rows<3>(w, row_bytes, n, y, out, r0, r1); break;
661        case 4: iq4nl_rows<4>(w, row_bytes, n, y, out, r0, r1); break;
662        default: for (int t0 = 0; t0 < nt; t0 += 4) {
663            const int k = nt - t0 < 4 ? nt - t0 : 4;
664            iq4nl_nt(w, row_bytes, n, y + t0, k, out + t0, r0, r1);
665        }
666    }
667}
668
669#undef STRATA_ROWS_VNNI
670#undef STRATA_ROWS_FN