okami.mom/strata

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

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

main
21.7 KiB431 linesraw
1// src/kernels/cpu/kq_avx2.cpp - Unsloth UD-Q4_K_XL's expert rows (Q4_K gate/up, Q5_1 and Q8_0 down) for several
2// tokens at once, BIT-EXACT against ggml-cpu's own dot products.
3//
4// ggml-cpu is built for AVX2 here (STRATA_PORTABLE: GGML_AVX2=ON, GGML_AVX512=OFF), so its vec_dot for these formats
5// is the __AVX2__ branch of ggml-cpu/arch/x86/quants.c: ggml_vec_dot_q4_K_q8_K, ggml_vec_dot_q5_1_q8_1,
6// ggml_vec_dot_q8_0_q8_0.  The functions below are those branches with the loop over tokens moved inside the loop
7// over blocks: everything that depends only on the weights (the 4-bit unpacking, the 6-bit scales and mins, the
8// fifth bits, the absolute values of the Q8_0 weights, the fp16 scales) is done once per block, and every token then
9// runs exactly ggml's integer chain and ggml's float operations in ggml's order - so each token's result is ggml's
10// to the bit (native_expert_parity checks it), whatever the number of tokens.  Unlike the i-quant kernels (#152)
11// the group size therefore never changes an answer.
12//
13// The gain is the weight-side work and the weight bytes, read once per verify window instead of once per token.
14#include "strata/kernels/cpu/kq_avx2.hpp"
15
16#define GGML_COMMON_DECL_CPP
17#define GGML_COMMON_IMPL_CPP
18#include "ggml-common.h"
19
20#include <immintrin.h>
21
22#include <cmath>
23#include <cstring>
24
25namespace strata::kernels::cpu {
26
27void lat_quant_q8_1_qs_avx2(const float* x, const float* d_ptr, int8_t* q) {
28    // Probe-validated (probe_qs.cpp, 100k random blocks bit-identical): divide by d (NOT 1/d mul -
29    // the ulp difference moves exact .5 ties), round ties-away-from-zero like roundf (floor(x+0.5)
30    // for x>=0, ceil(x-0.5) = -floor(-x+0.5) for x<0 - vroundps has no away mode), clamp, then
31    // narrow int32->int16->int8 with the per-lane packs semantics (probe3/probe5).
32    const __m256 dv = _mm256_set1_ps(*d_ptr);
33    const __m256 lim = _mm256_set1_ps(127.0f);
34    const __m256 nlim = _mm256_sub_ps(_mm256_setzero_ps(), lim);
35    const __m256 half = _mm256_set1_ps(0.5f);
36    const __m256 zero = _mm256_setzero_ps();
37    for (int j = 0; j < 32; j += 8) {
38        const __m256 v = _mm256_div_ps(_mm256_loadu_ps(x + j), dv);
39        const __m256 fa = _mm256_floor_ps(_mm256_add_ps(v, half));                 // floor(x+0.5)
40        const __m256 tneg = _mm256_sub_ps(zero, _mm256_sub_ps(v, half));           // -x+0.5
41        const __m256 neg = _mm256_sub_ps(zero, _mm256_floor_ps(tneg));             // ceil(x-0.5)
42        const __m256 mask = _mm256_cmp_ps(v, zero, _CMP_GE_OQ);
43        const __m256 rf = _mm256_blendv_ps(neg, fa, mask);
44        const __m256 cl = _mm256_min_ps(_mm256_max_ps(rf, nlim), lim);
45        const __m256i r = _mm256_cvtps_epi32(cl);                       // 8 x int32 in [-127, 127]
46        const __m256i w16 = _mm256_packs_epi32(r, r);                   // per-lane int16: lo=[r0..r3,dup]
47        const __m128i lo16 = _mm256_castsi256_si128(w16);
48        const __m128i hi16 = _mm256_extracti128_si256(w16, 1);          // hi=[r4..r7,dup]
49        const __m128i m16 = _mm_unpacklo_epi64(lo16, hi16);             // [r0..r3, r4..r7] (int16)
50        const __m128i w8 = _mm_packs_epi16(m16, m16);                   // low 64: r0..r7 as int8
51        _mm_storel_epi64((__m128i*) (q + j), w8);                       // q[j..j+7]
52    }
53}
54
55namespace {
56
57constexpr int MAXT = 8;   // the verify window's tokens per group (kVerifyMaxT)
58
59inline float h2f(ggml_half h) {
60    uint16_t u;
61    std::memcpy(&u, &h, 2);
62    return _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128((int) u)));
63}
64// the fp16 pair {d, dmin} / {d, m} / {d, s} a block starts with (a union in ggml-common.h's C++ declarations)
65inline float half_at(const void* block, int i) {
66    ggml_half h;
67    std::memcpy(&h, (const uint8_t*) block + 2 * i, 2);
68    return h2f(h);
69}
70
71// ---- ggml's helpers (ggml-cpu/arch/x86/quants.c, the AVX2 / non-VNNI forms), verbatim
72inline float hsum_float_8(const __m256 x) {
73    __m128 res = _mm256_extractf128_ps(x, 1);
74    res = _mm_add_ps(res, _mm256_castps256_ps128(x));
75    res = _mm_add_ps(res, _mm_movehl_ps(res, res));
76    res = _mm_add_ss(res, _mm_movehdup_ps(res));
77    return _mm_cvtss_f32(res);
78}
79inline __m256i bytes_from_bits_32(const uint8_t* x) {
80    uint32_t x32;
81    std::memcpy(&x32, x, sizeof(uint32_t));
82    const __m256i shuf_mask = _mm256_set_epi64x(0x0303030303030303, 0x0202020202020202, 0x0101010101010101,
83                                                0x0000000000000000);
84    __m256i bytes = _mm256_shuffle_epi8(_mm256_set1_epi32((int) x32), shuf_mask);
85    const __m256i bit_mask = _mm256_set1_epi64x(0x7fbfdfeff7fbfdfe);
86    bytes = _mm256_or_si256(bytes, bit_mask);
87    return _mm256_cmpeq_epi8(bytes, _mm256_set1_epi64x(-1));
88}
89inline __m256i bytes_from_nibbles_32(const uint8_t* rsi) {
90    const __m128i tmp = _mm_loadu_si128((const __m128i*) rsi);
91    const __m256i bytes = _mm256_insertf128_si256(_mm256_castsi128_si256(tmp), _mm_srli_epi16(tmp, 4), 1);
92    const __m256i lowMask = _mm256_set1_epi8(0xF);
93    return _mm256_and_si256(lowMask, bytes);
94}
95inline __m256 sum_i16_pairs_float(const __m256i x) {
96    const __m256i ones = _mm256_set1_epi16(1);
97    return _mm256_cvtepi32_ps(_mm256_madd_epi16(ones, x));
98}
99inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
100    return sum_i16_pairs_float(_mm256_maddubs_epi16(ax, sy));
101}
102alignas(32) const uint8_t k_shuffle[256] = {
103     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
104     2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
105     4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5,
106     6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7,
107     8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9,
108    10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,
109    12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,
110    14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15};
111inline __m256i get_scale_shuffle_k4(int i) { return _mm256_loadu_si256((const __m256i*) k_shuffle + i); }
112
113// ---- Q4_K x Q8_K (ggml_vec_dot_q4_K_q8_K, __AVX2__)
114void q4k_dot(const block_q4_K* x, int nb, const void* const* act, int nt, float* res) {
115    static const uint32_t kmask1 = 0x3f3f3f3f, kmask2 = 0x0f0f0f0f, kmask3 = 0x03030303;
116    const __m256i m4 = _mm256_set1_epi8(0xF);
117    __m256 acc[MAXT];
118    __m128 acc_m[MAXT];
119    for (int t = 0; t < nt; ++t) { acc[t] = _mm256_setzero_ps(); acc_m[t] = _mm_setzero_ps(); }
120    for (int i = 0; i < nb; ++i) {
121        const float xd = half_at(&x[i], 0), xdmin = half_at(&x[i], 1);
122        uint32_t utmp[4];
123        std::memcpy(utmp, x[i].scales, 12);
124        utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
125        const uint32_t uaux = utmp[1] & kmask1;
126        utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
127        utmp[2] = uaux;
128        utmp[0] &= kmask1;
129        const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32((int) utmp[3], (int) utmp[2],
130                                                                           (int) utmp[1], (int) utmp[0]));
131        const __m128i mins = _mm256_extracti128_si256(mins_and_scales, 1);
132        const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
133        const __m256i scales = _mm256_insertf128_si256(_mm256_castsi128_si256(sc128), sc128, 1);
134        __m256i scale_l[4], scale_h[4], q4l[4], q4h[4];
135        const uint8_t* q4 = x[i].qs;
136        for (int j = 0; j < QK_K / 64; ++j) {
137            scale_l[j] = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2 * j + 0));
138            scale_h[j] = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2 * j + 1));
139            const __m256i q4bits = _mm256_loadu_si256((const __m256i*) q4);
140            q4 += 32;
141            q4l[j] = _mm256_and_si256(q4bits, m4);
142            q4h[j] = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
143        }
144        for (int t = 0; t < nt; ++t) {
145            const block_q8_K* y = (const block_q8_K*) act[t] + i;
146            const float d = y->d * xd;
147            const float dmin = -y->d * xdmin;
148            const int8_t* q8 = y->qs;
149            const __m256i q8sums = _mm256_loadu_si256((const __m256i*) y->bsums);
150            const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
151            const __m128i prod = _mm_madd_epi16(mins, q8s);
152            acc_m[t] = _mm_fmadd_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod), acc_m[t]);
153            __m256i sumi = _mm256_setzero_si256();
154            for (int j = 0; j < QK_K / 64; ++j) {
155                const __m256i q8l = _mm256_loadu_si256((const __m256i*) q8);
156                q8 += 32;
157                __m256i p16l = _mm256_maddubs_epi16(q4l[j], q8l);
158                p16l = _mm256_madd_epi16(scale_l[j], p16l);
159                const __m256i q8h = _mm256_loadu_si256((const __m256i*) q8);
160                q8 += 32;
161                __m256i p16h = _mm256_maddubs_epi16(q4h[j], q8h);
162                p16h = _mm256_madd_epi16(scale_h[j], p16h);
163                sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16l, p16h));
164            }
165            acc[t] = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(sumi), acc[t]);
166        }
167    }
168    for (int t = 0; t < nt; ++t) {
169        __m128 m = acc_m[t];
170        m = _mm_add_ps(m, _mm_movehl_ps(m, m));
171        m = _mm_add_ss(m, _mm_movehdup_ps(m));
172        res[t] = hsum_float_8(acc[t]) + _mm_cvtss_f32(m);
173    }
174}
175
176// ---- Q5_1 x Q8_1 (ggml_vec_dot_q5_1_q8_1, __AVX2__)
177template <int NT>
178void q5_1_dot(const block_q5_1* x, int nb, const void* const* act, float* res) {
179    __m256 acc[NT];
180    float summs[NT];
181    for (int t = 0; t < NT; ++t) { acc[t] = _mm256_setzero_ps(); summs[t] = 0.0f; }
182    for (int ib = 0; ib < nb; ++ib) {
183        const __m256 dx = _mm256_set1_ps(half_at(&x[ib], 0));
184        const float xm = half_at(&x[ib], 1);
185        __m256i qx = bytes_from_nibbles_32(x[ib].qs);
186        __m256i bxhi = bytes_from_bits_32(x[ib].qh);
187        bxhi = _mm256_and_si256(bxhi, _mm256_set1_epi8(0x10));
188        qx = _mm256_or_si256(qx, bxhi);
189        for (int t = 0; t < NT; ++t) {
190            const block_q8_1* y = (const block_q8_1*) act[t] + ib;
191            summs[t] += xm * half_at(y, 1);
192            const __m256 dy = _mm256_set1_ps(half_at(y, 0));
193            const __m256i qy = _mm256_loadu_si256((const __m256i*) y->qs);
194            const __m256 q = mul_sum_us8_pairs_float(qx, qy);
195            acc[t] = _mm256_fmadd_ps(q, _mm256_mul_ps(dx, dy), acc[t]);
196        }
197    }
198    for (int t = 0; t < NT; ++t) res[t] = hsum_float_8(acc[t]) + summs[t];
199}
200
201// ---- Q8_0 x Q8_0 (ggml_vec_dot_q8_0_q8_0, __AVX2__; mul_sum_i8_pairs_float without AVX-VNNI-INT8)
202template <int NT>
203void q8_0_dot(const block_q8_0* x, int nb, const void* const* act, float* res) {
204    __m256 acc[NT];
205    for (int t = 0; t < NT; ++t) acc[t] = _mm256_setzero_ps();
206    for (int ib = 0; ib < nb; ++ib) {
207        const float xd = h2f(x[ib].d);
208        const __m256i qx = _mm256_loadu_si256((const __m256i*) x[ib].qs);
209        const __m256i ax = _mm256_sign_epi8(qx, qx);
210        for (int t = 0; t < NT; ++t) {
211            const block_q8_0* y = (const block_q8_0*) act[t] + ib;
212            const __m256 d = _mm256_set1_ps(xd * h2f(y->d));
213            const __m256i qy = _mm256_loadu_si256((const __m256i*) y->qs);
214            const __m256i sy = _mm256_sign_epi8(qy, qx);
215            const __m256 q = mul_sum_us8_pairs_float(ax, sy);
216            acc[t] = _mm256_fmadd_ps(d, q, acc[t]);
217        }
218    }
219    for (int t = 0; t < NT; ++t) res[t] = hsum_float_8(acc[t]);
220}
221
222void q5_1_dot_nt(const block_q5_1* x, int nb, const void* const* act, int nt, float* res) {
223    for (int t0 = 0; t0 < nt; t0 += 4) {
224        const int k = nt - t0 < 4 ? nt - t0 : 4;
225        switch (k) {
226            case 1: q5_1_dot<1>(x, nb, act + t0, res + t0); break;
227            case 2: q5_1_dot<2>(x, nb, act + t0, res + t0); break;
228            case 3: q5_1_dot<3>(x, nb, act + t0, res + t0); break;
229            default: q5_1_dot<4>(x, nb, act + t0, res + t0); break;
230        }
231    }
232}
233
234void q8_0_dot_nt(const block_q8_0* x, int nb, const void* const* act, int nt, float* res) {
235    for (int t0 = 0; t0 < nt; t0 += 4) {
236        const int k = nt - t0 < 4 ? nt - t0 : 4;
237        switch (k) {
238            case 1: q8_0_dot<1>(x, nb, act + t0, res + t0); break;
239            case 2: q8_0_dot<2>(x, nb, act + t0, res + t0); break;
240            case 3: q8_0_dot<3>(x, nb, act + t0, res + t0); break;
241            default: q8_0_dot<4>(x, nb, act + t0, res + t0); break;
242        }
243    }
244}
245
246// ---- Q8_0 x Q8_1 (edge0 latent MoE's U stage; the weight row is Q8_0 blocks, the activation Q8_1's).
247// The Q8_1 block's d sits at +0 (f16) and its qs at +4; the sum word at +2 is not read.  NT tokens share
248// one loaded weight row (the scalar mode-7 kernel re-read it per token).
249template <int NT>
250void lat_q8_0_q8_1_dot(const block_q8_0* x, int nb, const void* const* act, float* res) {
251    __m256 acc[NT];
252    for (int t = 0; t < NT; ++t) acc[t] = _mm256_setzero_ps();
253    for (int ib = 0; ib < nb; ++ib) {
254        const float xd = h2f(x[ib].d);
255        const __m256i qx = _mm256_loadu_si256((const __m256i*) x[ib].qs);
256        const __m256i ax = _mm256_sign_epi8(qx, qx);
257        for (int t = 0; t < NT; ++t) {
258            const uint8_t* yb = (const uint8_t*) act[t] + (size_t) ib * 36;   // block_q8_1's stride
259            const float dy = h2f(*(const uint16_t*) (yb + 0));
260            const __m256 d = _mm256_set1_ps(xd * dy);
261            const __m256i qy = _mm256_loadu_si256((const __m256i*) (yb + 4));
262            const __m256i sy = _mm256_sign_epi8(qy, qx);
263            const __m256 q = mul_sum_us8_pairs_float(ax, sy);
264            acc[t] = _mm256_fmadd_ps(d, q, acc[t]);
265        }
266    }
267    for (int t = 0; t < NT; ++t) res[t] = hsum_float_8(acc[t]);
268}
269
270void dot_rows(int type, const uint8_t* row, int n, const void* const* act, int nt, float* res) {
271    switch (type) {
272        case 12: q4k_dot((const block_q4_K*) row, n / QK_K, act, nt, res); break;
273        case 7: q5_1_dot_nt((const block_q5_1*) row, n / QK5_1, act, nt, res); break;
274        case 8: q8_0_dot_nt((const block_q8_0*) row, n / QK8_0, act, nt, res); break;
275        default: break;
276    }
277}
278
279}  // namespace
280
281void bf16_rows_dot_multi(const uint16_t* w, int rows, int cols, const float* x, int nt, float* out) {
282    for (int t0 = 0; t0 < nt; t0 += 4) {
283        const int m = nt - t0 < 4 ? nt - t0 : 4;
284        const float* x0 = x + (size_t) t0 * cols;
285        const float* x1 = x0 + cols;
286        const float* x2 = x1 + cols;
287        const float* x3 = x2 + cols;
288        for (int r = 0; r < rows; ++r) {
289            const uint16_t* wr = w + (size_t) r * (size_t) cols;
290            __m256 a0 = _mm256_setzero_ps(), a1 = _mm256_setzero_ps(), a2 = _mm256_setzero_ps(),
291                   a3 = _mm256_setzero_ps();
292            for (int c = 0; c + 8 <= cols; c += 8) {
293                const __m256 wf = _mm256_castsi256_ps(
294                    _mm256_slli_epi32(_mm256_cvtepu16_epi32(_mm_loadu_si128((const __m128i*) (wr + c))), 16));
295                a0 = _mm256_fmadd_ps(wf, _mm256_loadu_ps(x0 + c), a0);
296                if (m > 1) a1 = _mm256_fmadd_ps(wf, _mm256_loadu_ps(x1 + c), a1);
297                if (m > 2) a2 = _mm256_fmadd_ps(wf, _mm256_loadu_ps(x2 + c), a2);
298                if (m > 3) a3 = _mm256_fmadd_ps(wf, _mm256_loadu_ps(x3 + c), a3);
299            }
300            out[(size_t) t0 * rows + r] = hsum_float_8(a0);
301            if (m > 1) out[(size_t) (t0 + 1) * rows + r] = hsum_float_8(a1);
302            if (m > 2) out[(size_t) (t0 + 2) * rows + r] = hsum_float_8(a2);
303            if (m > 3) out[(size_t) (t0 + 3) * rows + r] = hsum_float_8(a3);
304        }
305    }
306}
307
308void bf16_rows_dot(const uint16_t* w, int rows, int cols, const float* x, float* out) {
309    for (int r = 0; r < rows; ++r) {
310        const uint16_t* wr = w + (size_t) r * (size_t) cols;
311        __m256 acc = _mm256_setzero_ps();
312        int c = 0;
313        for (; c + 8 <= cols; c += 8) {
314            const __m256i h = _mm256_cvtepu16_epi32(_mm_loadu_si128((const __m128i*) (wr + c)));
315            acc = _mm256_fmadd_ps(_mm256_castsi256_ps(_mm256_slli_epi32(h, 16)), _mm256_loadu_ps(x + c), acc);
316        }
317        float s = hsum_float_8(acc);
318        for (; c < cols; ++c) {
319            const uint32_t b = (uint32_t) wr[c] << 16;
320            float f;
321            std::memcpy(&f, &b, 4);
322            s += f * x[c];
323        }
324        out[r] = s;
325    }
326}
327
328void kq256_gu_rows(int type, const uint8_t* blob, size_t gu_row, size_t up_off, int n, const void* const* act, int nt,
329                   float* const* ff, int r0, int r1) {
330    for (int t0 = 0; t0 < nt; t0 += MAXT) {
331        const int m = nt - t0 < MAXT ? nt - t0 : MAXT;
332        float g[MAXT], u[MAXT];
333        for (int r = r0; r < r1; ++r) {
334            dot_rows(type, blob + (size_t) r * gu_row, n, act + t0, m, g);
335            dot_rows(type, blob + up_off + (size_t) r * gu_row, n, act + t0, m, u);
336            // native_gu_rows' own SwiGLU expression: the same bits as the per-token ggml path
337            for (int t = 0; t < m; ++t) ff[t0 + t][r] = (g[t] / (1.f + std::exp(-g[t]))) * u[t];
338        }
339    }
340}
341
342void kq256_rows(int type, const uint8_t* w, size_t row_bytes, int n, const void* const* act, int nt, float* const* out,
343                int r0, int r1) {
344    for (int t0 = 0; t0 < nt; t0 += MAXT) {
345        const int m = nt - t0 < MAXT ? nt - t0 : MAXT;
346        float s[MAXT];
347        for (int r = r0; r < r1; ++r) {
348            dot_rows(type, w + (size_t) r * row_bytes, n, act + t0, m, s);
349            for (int t = 0; t < m; ++t) out[t0 + t][r] = s[t];
350        }
351    }
352}
353
354// ---- Q4_0 x Q8_1 (Q4DU: edge0 latent MoE's U stage, the Q4_0 pack).  The weight block is
355// {f16 d; uint8 nib[16]} - element j in byte j's low nibble, j+16 in its high one, value
356// (code - 8) * d.  The same maddubs sign trick as lat_q8_0_q8_1_dot above (u8 x s8 with the
357// weight's signs folded into the q8_1 codes), the same float order (acc += (dx*dw) * sumi per
358// block, FMA chain), sumi the whole 32-value integer dot of one block.
359template <int NT>
360void lat_q4_0_q8_1_dot(const uint8_t* x, int nb, const void* const* act, float* res) {
361    __m256 acc[NT];
362    for (int t = 0; t < NT; ++t) acc[t] = _mm256_setzero_ps();
363    const __m256i mask = _mm256_set1_epi8(0xF);
364    const __m256i ones = _mm256_set1_epi16(1);
365    for (int ib = 0; ib < nb; ++ib) {
366        const uint8_t* wb = x + (size_t) ib * 18;
367        const float xd = h2f(*(const ggml_half*) wb);
368        // 16 nibble bytes -> 32 PACKED code bytes (maddubs runs on bytes): the file's own
369        // bytes_from_nibbles_32 (ggml's) leaves byte j's low nibble at position j and its high
370        // nibble at position j+16 - exactly block_q4_0's element order.  Centred: code - 8.
371        const __m256i q4bytes = _mm256_and_si256(bytes_from_nibbles_32(wb + 2), mask);
372        const __m256i w = _mm256_sub_epi8(q4bytes, _mm256_set1_epi8(8));   // 32 centred codes, packed
373        const __m256i aw = _mm256_sign_epi8(w, w);                          // |w|, the u8 side of maddubs
374        for (int t = 0; t < NT; ++t) {
375            const uint8_t* yb = (const uint8_t*) act[t] + (size_t) ib * 36;   // block_q8_1's stride
376            const float dy = h2f(*(const ggml_half*) (yb + 0));
377            const __m256 d = _mm256_set1_ps(xd * dy);
378            const __m256i qy = _mm256_loadu_si256((const __m256i*) (yb + 4));  // 32 q8_1 codes, packed
379            // signed x signed byte dot: maddubs(aw, sign(qy, w)) = |w| * (qy flipped where w < 0)
380            // = w * qy, the q8_0_dot trick verbatim (a zero code contributes 0 either way).
381            const __m256i sq = _mm256_sign_epi8(qy, w);
382            __m256i p16 = _mm256_maddubs_epi16(aw, sq);
383            __m256i si = _mm256_madd_epi16(ones, p16);
384            const __m256 q = _mm256_cvtepi32_ps(si);
385            acc[t] = _mm256_fmadd_ps(d, q, acc[t]);
386        }
387    }
388    for (int t = 0; t < NT; ++t) res[t] = hsum_float_8(acc[t]);
389}
390
391void lat_q4_0_q8_1_rows(const uint8_t* w, size_t row_bytes, int n, const void* const* act, int nt,
392                         float* const* out, int r0, int r1) {
393    // Q4DU: the Q4_0 form of lat_q8_0_q8_1_rows (mode 7's Q4 arm), rows [r0, r1)
394    const int nb = n / 32;
395    for (int t0 = 0; t0 < nt; t0 += 4) {
396        const int m = nt - t0 < 4 ? nt - t0 : 4;
397        for (int r = r0; r < r1; ++r) {
398            const uint8_t* row = w + (size_t) r * row_bytes;
399            float s[4];
400            switch (m) {
401                case 1: lat_q4_0_q8_1_dot<1>(row, nb, act + t0, s); break;
402                case 2: lat_q4_0_q8_1_dot<2>(row, nb, act + t0, s); break;
403                case 3: lat_q4_0_q8_1_dot<3>(row, nb, act + t0, s); break;
404                default: lat_q4_0_q8_1_dot<4>(row, nb, act + t0, s); break;
405            }
406            for (int t = 0; t < m; ++t) out[t0 + t][r] = s[t];
407        }
408    }
409}
410
411void lat_q8_0_q8_1_rows(const uint8_t* w, size_t row_bytes, int n, const void* const* act, int nt,
412                         float* const* out, int r0, int r1) {
413    // edge0 latent MoE's U stage: Q8_0 weight rows x Q8_1 activations (the pool's mode 7), rows [r0, r1)
414    const int nb = n / 32;
415    for (int t0 = 0; t0 < nt; t0 += 4) {
416        const int m = nt - t0 < 4 ? nt - t0 : 4;
417        for (int r = r0; r < r1; ++r) {
418            const block_q8_0* row = (const block_q8_0*) (w + (size_t) r * row_bytes);
419            float s[4];
420            switch (m) {
421                case 1: lat_q8_0_q8_1_dot<1>(row, nb, act + t0, s); break;
422                case 2: lat_q8_0_q8_1_dot<2>(row, nb, act + t0, s); break;
423                case 3: lat_q8_0_q8_1_dot<3>(row, nb, act + t0, s); break;
424                default: lat_q8_0_q8_1_dot<4>(row, nb, act + t0, s); break;
425            }
426            for (int t = 0; t < m; ++t) out[t0 + t][r] = s[t];
427        }
428    }
429}
430
431}  // namespace strata::kernels::cpu