char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Georgi Gerganovggml : use ggml_vqtbl1q_u8 for 32-bit compat (whisper/0)ac17f8ac1

char/aliceai-t5-moe
211.9 KiB4319 linesraw
1#define GGML_COMMON_IMPL_C
2#include "ggml-common.h"
3#include "ggml-quants.h"
4#include "ggml-impl.h"
5#include "ggml-cpu.h"
6#include "simd-mappings.h"
7
8#include "../../quants.h"
9#include "../../ggml-cpu-impl.h"
10
11#include <math.h>
12#include <string.h>
13#include <assert.h>
14#include <float.h>
15#include <stdlib.h> // for qsort
16#include <stdio.h>  // for GGML_ASSERT
17
18#define GROUP_MAX_EPS 1e-15f
19#define GROUP_MAX_EPS_IQ3_XXS 1e-8f
20#define GROUP_MAX_EPS_IQ2_S 1e-8f
21#define GROUP_MAX_EPS_IQ1_M 1e-7f
22#define GROUP_MAX_EPS_IQ1_S 1e-12f
23
24#define UNUSED GGML_UNUSED
25
26#if defined(__ARM_NEON)
27#define B1(c,s,n)  0x ## n ## c ,  0x ## n ## s
28#define B2(c,s,n) B1(c,s,n ## c), B1(c,s,n ## s)
29#define B3(c,s,n) B2(c,s,n ## c), B2(c,s,n ## s)
30#define B4(c,s,n) B3(c,s,n ## c), B3(c,s,n ## s)
31#define B5(c,s,n) B4(c,s,n ## c), B4(c,s,n ## s)
32#define B6(c,s,n) B5(c,s,n ## c), B5(c,s,n ## s)
33#define B7(c,s,n) B6(c,s,n ## c), B6(c,s,n ## s)
34#define B8(c,s  ) B7(c,s,     c), B7(c,s,     s)
35
36// precomputed tables for expanding 8bits to 8 bytes:
37static const uint64_t table_b2b_0[1 << 8] = { B8(00, 10) }; // ( b) << 4
38static const uint64_t table_b2b_1[1 << 8] = { B8(10, 00) }; // (!b) << 4
39#endif
40
41void quantize_row_q8_0(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t k) {
42    assert(QK8_0 == 32);
43    assert(k % QK8_0 == 0);
44    const int nb = k / QK8_0;
45
46    block_q8_0 * GGML_RESTRICT y = vy;
47
48#if defined(__ARM_NEON)
49    for (int i = 0; i < nb; i++) {
50        float32x4_t srcv [8];
51        float32x4_t asrcv[8];
52        float32x4_t amaxv[8];
53
54        for (int j = 0; j < 8; j++) srcv[j]  = vld1q_f32(x + i*32 + 4*j);
55        for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
56
57        for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
58        for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
59        for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
60
61        const float amax = vmaxvq_f32(amaxv[0]);
62
63        const float d = amax / ((1 << 7) - 1);
64        const float id = d ? 1.0f/d : 0.0f;
65
66        y[i].d = GGML_CPU_FP32_TO_FP16(d);
67
68        for (int j = 0; j < 8; j++) {
69            const float32x4_t v  = vmulq_n_f32(srcv[j], id);
70            const int32x4_t   vi = vcvtnq_s32_f32(v);
71
72            y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
73            y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
74            y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
75            y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
76        }
77    }
78#else
79    GGML_UNUSED(nb);
80    // scalar
81    quantize_row_q8_0_ref(x, y, k);
82#endif
83}
84
85void quantize_row_q8_1(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t k) {
86    assert(k % QK8_1 == 0);
87    const int nb = k / QK8_1;
88
89    block_q8_1 * GGML_RESTRICT y = vy;
90#if defined(__ARM_NEON)
91    for (int i = 0; i < nb; i++) {
92        float32x4_t srcv [8];
93        float32x4_t asrcv[8];
94        float32x4_t amaxv[8];
95
96        for (int j = 0; j < 8; j++) srcv[j]  = vld1q_f32(x + i*32 + 4*j);
97        for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
98
99        for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
100        for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
101        for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
102
103        const float amax = vmaxvq_f32(amaxv[0]);
104
105        const float d = amax / ((1 << 7) - 1);
106        const float id = d ? 1.0f/d : 0.0f;
107
108        y[i].d = GGML_CPU_FP32_TO_FP16(d);
109
110        int32x4_t accv = vdupq_n_s32(0);
111
112        for (int j = 0; j < 8; j++) {
113            const float32x4_t v  = vmulq_n_f32(srcv[j], id);
114            const int32x4_t   vi = vcvtnq_s32_f32(v);
115
116            y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
117            y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
118            y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
119            y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
120
121            accv = vaddq_s32(accv, vi);
122        }
123
124        y[i].s = GGML_CPU_FP32_TO_FP16(d * vaddvq_s32(accv));
125    }
126#else
127    GGML_UNUSED(nb);
128    // scalar
129    quantize_row_q8_1_ref(x, y, k);
130#endif
131}
132
133// placeholder implementation for Apple targets
134void quantize_row_q8_K(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k) {
135    quantize_row_q8_K_ref(x, y, k);
136}
137
138//===================================== Dot products =================================
139
140void ggml_vec_dot_q1_0_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
141    const int qk = QK1_0;  // 128
142    const int nb = n / qk;
143
144    assert(n % qk == 0);
145    assert(nrc == 1);
146    UNUSED(nrc);
147    UNUSED(bx);
148    UNUSED(by);
149    UNUSED(bs);
150
151    const block_q1_0 * GGML_RESTRICT x = vx;
152    const block_q8_0 * GGML_RESTRICT y = vy;
153
154#if defined(__ARM_NEON)
155    float32x4_t sumv = vdupq_n_f32(0.0f);
156
157    for (int i = 0; i < nb; i++) {
158        const float d0 = GGML_CPU_FP16_TO_FP32(x[i].d);
159
160        // Process 4 Q8_0 blocks (each has 32 elements)
161        for (int k = 0; k < 4; k++) {
162            const block_q8_0 * GGML_RESTRICT yb = &y[i * 4 + k];
163            const float d1 = GGML_CPU_FP16_TO_FP32(yb->d);
164
165            // Get the 4 bytes of bits for this Q8_0 block (32 bits = 4 bytes)
166            // Bits are at offset k*4 bytes in x[i].qs
167            const uint8_t * bits = &x[i].qs[k * 4];
168
169            // Load 32 int8 values from y
170            const int8x16_t y0 = vld1q_s8(yb->qs);
171            const int8x16_t y1 = vld1q_s8(yb->qs + 16);
172
173            // Byte 0-1: bits for y0[0..15]
174            const uint64_t expand0 = table_b2b_0[bits[0]];
175            const uint64_t expand1 = table_b2b_0[bits[1]];
176            // Byte 2-3: bits for y1[0..15]
177            const uint64_t expand2 = table_b2b_0[bits[2]];
178            const uint64_t expand3 = table_b2b_0[bits[3]];
179
180            // Build the sign vectors by reinterpreting the table values
181            uint8x8_t e0 = vcreate_u8(expand0);
182            uint8x8_t e1 = vcreate_u8(expand1);
183            uint8x8_t e2 = vcreate_u8(expand2);
184            uint8x8_t e3 = vcreate_u8(expand3);
185
186            // Shift right by 4 to get 0 or 1
187            int8x8_t s0 = vreinterpret_s8_u8(vshr_n_u8(e0, 4));
188            int8x8_t s1 = vreinterpret_s8_u8(vshr_n_u8(e1, 4));
189            int8x8_t s2 = vreinterpret_s8_u8(vshr_n_u8(e2, 4));
190            int8x8_t s3 = vreinterpret_s8_u8(vshr_n_u8(e3, 4));
191
192            // Convert 0/1 to -1/+1: sign = 2*val - 1
193            int8x8_t one = vdup_n_s8(1);
194            s0 = vsub_s8(vadd_s8(s0, s0), one);  // 2*s0 - 1
195            s1 = vsub_s8(vadd_s8(s1, s1), one);
196            s2 = vsub_s8(vadd_s8(s2, s2), one);
197            s3 = vsub_s8(vadd_s8(s3, s3), one);
198
199            // Combine into 16-element vectors
200            int8x16_t signs0 = vcombine_s8(s0, s1);
201            int8x16_t signs1 = vcombine_s8(s2, s3);
202
203            // Multiply signs with y values and accumulate
204            // dot(signs, y) where signs are +1/-1
205            int32x4_t p0 = ggml_vdotq_s32(vdupq_n_s32(0), signs0, y0);
206            int32x4_t p1 = ggml_vdotq_s32(p0, signs1, y1);
207
208            // Scale by d1 and accumulate
209            sumv = vmlaq_n_f32(sumv, vcvtq_f32_s32(p1), d0 * d1);
210        }
211    }
212
213    *s = vaddvq_f32(sumv);
214#else
215    UNUSED(nb);
216    UNUSED(x);
217    UNUSED(y);
218    ggml_vec_dot_q1_0_q8_0_generic(n, s, bs, vx, bx, vy, by, nrc);
219#endif
220}
221
222void ggml_vec_dot_q2_0_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
223    const int qk = QK2_0;
224    const int nb = n / qk;
225
226    assert(n % qk == 0);
227    assert(nrc == 1);
228    UNUSED(nrc);
229    UNUSED(bx);
230    UNUSED(by);
231    UNUSED(bs);
232
233    const block_q2_0 * GGML_RESTRICT x = vx;
234    const block_q8_0 * GGML_RESTRICT y = vy;
235
236    float sumf = 0.0f;
237
238#if defined(__ARM_NEON)
239    // Replicate pattern: each byte repeated 4 times
240    static const uint8_t tbl_idx_lo[16] = {0,0,0,0, 1,1,1,1, 2,2,2,2, 3,3,3,3};
241    static const uint8_t tbl_idx_hi[16] = {4,4,4,4, 5,5,5,5, 6,6,6,6, 7,7,7,7};
242    // Right-shift amounts: 0,2,4,6 repeated for each group of 4
243    static const int8_t shift_vals[16] = {0,-2,-4,-6, 0,-2,-4,-6, 0,-2,-4,-6, 0,-2,-4,-6};
244
245    const uint8x16_t idx_lo  = vld1q_u8(tbl_idx_lo);
246    const uint8x16_t idx_hi  = vld1q_u8(tbl_idx_hi);
247    const int8x16_t  shifts  = vld1q_s8(shift_vals);
248    const uint8x16_t mask2   = vdupq_n_u8(0x03);
249    const int8x16_t  one     = vdupq_n_s8(1);
250
251    float32x4_t sumv = vdupq_n_f32(0.0f);
252
253    for (int i = 0; i < nb; i++) {
254        const float d0 = GGML_CPU_FP16_TO_FP32(x[i].d);
255
256        // group 64: one Q2_0 block (64 weights) maps to two Q8_0 blocks (2 * 32 = 64)
257        for (int k = 0; k < 2; k++) {
258            const block_q8_0 * GGML_RESTRICT yb = &y[i * 2 + k];
259            const float d1 = GGML_CPU_FP16_TO_FP32(yb->d);
260
261            // Load 8 bytes of packed 2-bit values
262            const uint8x8_t raw = vld1_u8(&x[i].qs[k * 8]);
263            const uint8x16_t raw16 = vcombine_u8(raw, raw);
264
265            // First 16 elements: replicate bytes 0-3, shift, mask, subtract 1
266            uint8x16_t bytes0 = ggml_vqtbl1q_u8(raw16, idx_lo);
267            int8x16_t qv0 = vsubq_s8(
268                vreinterpretq_s8_u8(vandq_u8(vshlq_u8(bytes0, shifts), mask2)),
269                one);
270
271            // Second 16 elements: replicate bytes 4-7, shift, mask, subtract 1
272            uint8x16_t bytes1 = ggml_vqtbl1q_u8(raw16, idx_hi);
273            int8x16_t qv1 = vsubq_s8(
274                vreinterpretq_s8_u8(vandq_u8(vshlq_u8(bytes1, shifts), mask2)),
275                one);
276
277            // Load Q8_0 values and dot product
278            const int8x16_t y0 = vld1q_s8(yb->qs);
279            const int8x16_t y1 = vld1q_s8(yb->qs + 16);
280
281            int32x4_t p0 = ggml_vdotq_s32(vdupq_n_s32(0), qv0, y0);
282            int32x4_t p1 = ggml_vdotq_s32(p0, qv1, y1);
283
284            sumv = vmlaq_n_f32(sumv, vcvtq_f32_s32(p1), d0 * d1);
285        }
286    }
287
288    sumf = vaddvq_f32(sumv);
289#else
290    ggml_vec_dot_q2_0_q8_0_generic(n, s, bs, vx, bx, vy, by, nrc);
291    return;
292#endif
293
294    *s = sumf;
295}
296
297void ggml_vec_dot_q4_0_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
298    const int qk = QK8_0;
299    const int nb = n / qk;
300
301    assert(n % qk == 0);
302#if defined(__ARM_FEATURE_MATMUL_INT8)
303    assert((nrc == 2) || (nrc == 1));
304#else
305    assert(nrc == 1);
306#endif
307    UNUSED(nrc);
308    UNUSED(bx);
309    UNUSED(by);
310    UNUSED(bs);
311
312    const block_q4_0 * GGML_RESTRICT x = vx;
313    const block_q8_0 * GGML_RESTRICT y = vy;
314
315#if defined(__ARM_FEATURE_MATMUL_INT8)
316    if (nrc == 2) {
317        const block_q4_0 * GGML_RESTRICT vx0 = vx;
318        const block_q4_0 * GGML_RESTRICT vx1 = (const block_q4_0 *) ((const uint8_t*)vx + bx);
319        const block_q8_0 * GGML_RESTRICT vy0 = vy;
320        const block_q8_0 * GGML_RESTRICT vy1 = (const block_q8_0 *) ((const uint8_t*)vy + by);
321
322        float32x4_t sumv0 = vdupq_n_f32(0.0f);
323
324        for (int i = 0; i < nb; i++) {
325            const block_q4_0 * GGML_RESTRICT b_x0 = &vx0[i];
326            const block_q4_0 * GGML_RESTRICT b_x1 = &vx1[i];
327            const block_q8_0 * GGML_RESTRICT b_y0 = &vy0[i];
328            const block_q8_0 * GGML_RESTRICT b_y1 = &vy1[i];
329
330            const uint8x16_t m4b = vdupq_n_u8(0x0F);
331            const int8x16_t  s8b = vdupq_n_s8(0x8);
332
333            const uint8x16_t v0_0 = vld1q_u8(b_x0->qs);
334            const uint8x16_t v0_1 = vld1q_u8(b_x1->qs);
335
336            // 4-bit -> 8-bit
337            const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8  (v0_0, m4b));
338            const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
339            const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8  (v0_1, m4b));
340            const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
341
342            // sub 8
343            const int8x16_t x0_l = vsubq_s8(v0_0l, s8b);
344            const int8x16_t x0_h = vsubq_s8(v0_0h, s8b);
345            const int8x16_t x1_l = vsubq_s8(v0_1l, s8b);
346            const int8x16_t x1_h = vsubq_s8(v0_1h, s8b);
347
348            // load y
349            const int8x16_t y0_l = vld1q_s8(b_y0->qs);
350            const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
351            const int8x16_t y1_l = vld1q_s8(b_y1->qs);
352            const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
353
354            float32_t _scale[4] = {
355                GGML_CPU_FP16_TO_FP32(b_x0->d)*GGML_CPU_FP16_TO_FP32(b_y0->d),
356                GGML_CPU_FP16_TO_FP32(b_x0->d)*GGML_CPU_FP16_TO_FP32(b_y1->d),
357                GGML_CPU_FP16_TO_FP32(b_x1->d)*GGML_CPU_FP16_TO_FP32(b_y0->d),
358                GGML_CPU_FP16_TO_FP32(b_x1->d)*GGML_CPU_FP16_TO_FP32(b_y1->d)
359            };
360            float32x4_t scale = vld1q_f32(_scale);
361
362            int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
363            int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
364
365            int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
366            int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
367
368            int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
369            int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
370
371            int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
372            int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
373
374            sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
375                                                l1, r1)), l2, r2)), l3, r3))), scale);
376        }
377
378        float32x4_t sumv1 = vextq_f32 (sumv0, sumv0, 2);
379        float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
380
381        vst1_f32(s,      vget_low_f32 (sumv2));
382        vst1_f32(s + bs, vget_high_f32(sumv2));
383
384        return;
385    }
386#endif
387
388    int ib = 0;
389    float sumf = 0;
390
391#if defined(__ARM_FEATURE_SVE)
392    svfloat32_t sumv0 = svdup_n_f32(0.0f);
393    svfloat32_t sumv1 = svdup_n_f32(0.0f);
394
395    const int vector_length = ggml_cpu_get_sve_cnt()*8;
396
397    // VLA Implementation using switch case
398    switch (vector_length) {
399        case 128:
400            {
401                // predicate for activating higher lanes for 4 float32 elements
402                const svbool_t ph4 = svptrue_pat_b32(SV_VL4);
403
404                for (; ib + 1 < nb; ib += 2) {
405                    const block_q4_0 * GGML_RESTRICT x0 = &x[ib + 0];
406                    const block_q4_0 * GGML_RESTRICT x1 = &x[ib + 1];
407                    const block_q8_0 * GGML_RESTRICT y0 = &y[ib + 0];
408                    const block_q8_0 * GGML_RESTRICT y1 = &y[ib + 1];
409
410                    // load x
411                    const svuint8_t qx0r = svld1rq_u8(svptrue_b8(), x0->qs);
412                    const svuint8_t qx1r = svld1rq_u8(svptrue_b8(), x1->qs);
413
414                    // 4-bit -> 8-bit
415                    const svint8_t qx0l = svreinterpret_s8_u8(svand_n_u8_m(svptrue_b8(), qx0r, 0x0F));
416                    const svint8_t qx0h = svreinterpret_s8_u8(svlsr_n_u8_m(svptrue_b8(), qx0r, 0x04));
417                    const svint8_t qx1l = svreinterpret_s8_u8(svand_n_u8_m(svptrue_b8(), qx1r, 0x0F));
418                    const svint8_t qx1h = svreinterpret_s8_u8(svlsr_n_u8_m(svptrue_b8(), qx1r, 0x04));
419
420                    // sub 8
421                    const svint8_t qx0ls = svsub_n_s8_x(svptrue_b8(), qx0h, 8);
422                    const svint8_t qx0hs = svsub_n_s8_x(svptrue_b8(), qx0l, 8);
423                    const svint8_t qx1ls = svsub_n_s8_x(svptrue_b8(), qx1h, 8);
424                    const svint8_t qx1hs = svsub_n_s8_x(svptrue_b8(), qx1l, 8);
425
426                    // load y
427                    const svint8_t qy0h = svld1_s8(svptrue_b8(), y0->qs);
428                    const svint8_t qy0l = svld1_s8(svptrue_b8(), y0->qs + 16);
429                    const svint8_t qy1h = svld1_s8(svptrue_b8(), y1->qs);
430                    const svint8_t qy1l = svld1_s8(svptrue_b8(), y1->qs + 16);
431
432                    // dot product
433                    sumv0 = svmla_n_f32_x(ph4, sumv0, svcvt_f32_s32_x(ph4, svadd_x(ph4,
434                                    svdot_s32(svdup_n_s32(0), qx0ls, qy0l),
435                                    svdot_s32(svdup_n_s32(0), qx0hs, qy0h))), GGML_CPU_FP16_TO_FP32(x0->d)*GGML_CPU_FP16_TO_FP32(y0->d));
436                    sumv1 = svmla_n_f32_x(ph4, sumv1, svcvt_f32_s32_x(ph4, svadd_x(ph4,
437                                    svdot_s32(svdup_n_s32(0), qx1ls, qy1l),
438                                    svdot_s32(svdup_n_s32(0), qx1hs, qy1h))), GGML_CPU_FP16_TO_FP32(x1->d)*GGML_CPU_FP16_TO_FP32(y1->d));
439                }
440
441                sumf = svaddv_f32(svptrue_b32(), svadd_f32_x(svptrue_b32(), sumv0, sumv1));
442            } break;
443        case 256:
444            {
445                // predicate for activating higher lanes for 16 int8 elements
446                const svbool_t ph16 = svptrue_pat_b8(SV_VL16);
447                // predicate for activating lower lanes for  16 int8 elements
448                const svbool_t pl16 = svnot_b_z(svptrue_b8(), ph16);
449
450                for (; ib + 1 < nb; ib += 2) {
451                    const block_q4_0 * GGML_RESTRICT x0 = &x[ib + 0];
452                    const block_q4_0 * GGML_RESTRICT x1 = &x[ib + 1];
453                    const block_q8_0 * GGML_RESTRICT y0 = &y[ib + 0];
454                    const block_q8_0 * GGML_RESTRICT y1 = &y[ib + 1];
455
456                    // load x
457                    const svuint8_t qx0r = svld1rq_u8(svptrue_b8(), x0->qs);
458                    const svuint8_t qx1r = svld1rq_u8(svptrue_b8(), x1->qs);
459
460                    // 4-bit -> 8-bit
461                    const svint8_t qx0 = svreinterpret_s8_u8(svlsr_n_u8_m(pl16, svand_n_u8_m(ph16, qx0r, 0x0F), 0x04));
462                    const svint8_t qx1 = svreinterpret_s8_u8(svlsr_n_u8_m(pl16, svand_n_u8_m(ph16, qx1r, 0x0F), 0x04));
463
464                    // sub 8
465                    const svint8_t qx0s = svsub_n_s8_x(svptrue_b8(), qx0, 8);
466                    const svint8_t qx1s = svsub_n_s8_x(svptrue_b8(), qx1, 8);
467
468                    // load y
469                    const svint8_t qy0 = svld1_s8(svptrue_b8(), y0->qs);
470                    const svint8_t qy1 = svld1_s8(svptrue_b8(), y1->qs);
471
472                    // dot product
473                    sumv0 = svmla_n_f32_x(svptrue_b32(), sumv0, svcvt_f32_s32_x(svptrue_b32(),
474                                svdot_s32(svdup_n_s32(0), qx0s, qy0)), GGML_CPU_FP16_TO_FP32(x0->d)*GGML_CPU_FP16_TO_FP32(y0->d));
475                    sumv1 = svmla_n_f32_x(svptrue_b32(), sumv1, svcvt_f32_s32_x(svptrue_b32(),
476                                svdot_s32(svdup_n_s32(0), qx1s, qy1)), GGML_CPU_FP16_TO_FP32(x1->d)*GGML_CPU_FP16_TO_FP32(y1->d));
477                }
478
479                sumf = svaddv_f32(svptrue_b32(), svadd_f32_x(svptrue_b32(), sumv0, sumv1));
480            } break;
481        case 512:
482            {
483                // predicate for activating higher lanes for 32 int8 elements
484                const svbool_t ph32 = svptrue_pat_b8(SV_VL32);
485
486                // predicate for activating higher lanes for 16 int8 elements
487                const svbool_t ph16 = svptrue_pat_b8(SV_VL16);
488                // predicate for activating lower lanes for 16 int8 elements from first 32 int8 activated lanes
489                const svbool_t pl16 = svnot_b_z(ph32, ph16);
490
491                for (; ib + 1 < nb; ib += 2) {
492                    const block_q4_0 * GGML_RESTRICT x0 = &x[ib + 0];
493                    const block_q4_0 * GGML_RESTRICT x1 = &x[ib + 1];
494                    const block_q8_0 * GGML_RESTRICT y0 = &y[ib + 0];
495                    const block_q8_0 * GGML_RESTRICT y1 = &y[ib + 1];
496
497                    // load x
498                    const svuint8_t qx0r = svld1rq_u8(ph32, x0->qs);
499                    const svuint8_t qx1r = svld1rq_u8(ph32, x1->qs);
500
501                    // 4-bit -> 8-bit
502                    const svint8_t qx0 = svreinterpret_s8_u8(svlsr_n_u8_m(pl16, svand_n_u8_m(ph16, qx0r, 0x0F), 0x04));
503                    const svint8_t qx1 = svreinterpret_s8_u8(svlsr_n_u8_m(pl16, svand_n_u8_m(ph16, qx1r, 0x0F), 0x04));
504
505                    // sub 8
506                    const svint8_t qx0s = svsub_n_s8_x(ph32, qx0, 8);
507                    const svint8_t qx1s = svsub_n_s8_x(ph32, qx1, 8);
508
509                    // load y
510                    const svint8_t qy0 = svld1_s8(ph32, y0->qs);
511                    const svint8_t qy1 = svld1_s8(ph32, y1->qs);
512
513                    // dot product
514                    sumv0 = svmla_n_f32_x(ph32, sumv0, svcvt_f32_s32_x(ph32,
515                                svdot_s32(svdup_n_s32(0), qx0s, qy0)), GGML_CPU_FP16_TO_FP32(x0->d)*GGML_CPU_FP16_TO_FP32(y0->d));
516                    sumv1 = svmla_n_f32_x(ph32, sumv1, svcvt_f32_s32_x(ph32,
517                                svdot_s32(svdup_n_s32(0), qx1s, qy1)), GGML_CPU_FP16_TO_FP32(x1->d)*GGML_CPU_FP16_TO_FP32(y1->d));
518                }
519
520                sumf = svaddv_f32(ph32, svadd_f32_x(ph32, sumv0, sumv1));
521            } break;
522        default:
523            assert(false && "Unsupported vector length");
524            break;
525    }
526
527#elif defined(__ARM_NEON)
528    float32x4_t sumv0 = vdupq_n_f32(0.0f);
529    float32x4_t sumv1 = vdupq_n_f32(0.0f);
530
531    for (; ib + 1 < nb; ib += 2) {
532        const block_q4_0 * GGML_RESTRICT x0 = &x[ib + 0];
533        const block_q4_0 * GGML_RESTRICT x1 = &x[ib + 1];
534        const block_q8_0 * GGML_RESTRICT y0 = &y[ib + 0];
535        const block_q8_0 * GGML_RESTRICT y1 = &y[ib + 1];
536
537        const uint8x16_t m4b = vdupq_n_u8(0x0F);
538        const int8x16_t  s8b = vdupq_n_s8(0x8);
539
540        const uint8x16_t v0_0 = vld1q_u8(x0->qs);
541        const uint8x16_t v0_1 = vld1q_u8(x1->qs);
542
543        // 4-bit -> 8-bit
544        const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8  (v0_0, m4b));
545        const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
546        const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8  (v0_1, m4b));
547        const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
548
549        // sub 8
550        const int8x16_t v0_0ls = vsubq_s8(v0_0l, s8b);
551        const int8x16_t v0_0hs = vsubq_s8(v0_0h, s8b);
552        const int8x16_t v0_1ls = vsubq_s8(v0_1l, s8b);
553        const int8x16_t v0_1hs = vsubq_s8(v0_1h, s8b);
554
555        // load y
556        const int8x16_t v1_0l = vld1q_s8(y0->qs);
557        const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
558        const int8x16_t v1_1l = vld1q_s8(y1->qs);
559        const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
560
561        // dot product into int32x4_t
562        const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0ls, v1_0l), v0_0hs, v1_0h);
563        const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1ls, v1_1l), v0_1hs, v1_1h);
564
565        sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_CPU_FP16_TO_FP32(x0->d)*GGML_CPU_FP16_TO_FP32(y0->d));
566        sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_CPU_FP16_TO_FP32(x1->d)*GGML_CPU_FP16_TO_FP32(y1->d));
567    }
568
569    sumf = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
570#endif
571    for (; ib < nb; ++ib) {
572        int sumi0 = 0;
573        int sumi1 = 0;
574
575        for (int j = 0; j < qk/2; ++j) {
576            const int v0 = (x[ib].qs[j] & 0x0F) - 8;
577            const int v1 = (x[ib].qs[j] >>   4) - 8;
578
579            sumi0 += (v0 * y[ib].qs[j]);
580            sumi1 += (v1 * y[ib].qs[j + qk/2]);
581        }
582
583        int sumi = sumi0 + sumi1;
584        sumf += sumi*GGML_CPU_FP16_TO_FP32(x[ib].d)*GGML_CPU_FP16_TO_FP32(y[ib].d);
585    }
586
587    *s = sumf;
588}
589
590void ggml_vec_dot_q4_1_q8_1(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
591    const int qk = QK8_1;
592    const int nb = n / qk;
593
594    assert(n % qk == 0);
595#if defined(__ARM_FEATURE_MATMUL_INT8)
596    assert((nrc == 2) || (nrc == 1));
597#else
598    assert(nrc == 1);
599#endif
600    UNUSED(nrc);
601    UNUSED(bx);
602    UNUSED(by);
603    UNUSED(bs);
604
605    const block_q4_1 * GGML_RESTRICT x = vx;
606    const block_q8_1 * GGML_RESTRICT y = vy;
607
608#if defined(__ARM_FEATURE_MATMUL_INT8)
609    if (nrc == 2) {
610        const block_q4_1 * GGML_RESTRICT vx0 = vx;
611        const block_q4_1 * GGML_RESTRICT vx1 = (const block_q4_1 *) ((const uint8_t*)vx + bx);
612        const block_q8_1 * GGML_RESTRICT vy0 = vy;
613        const block_q8_1 * GGML_RESTRICT vy1 = (const block_q8_1 *) ((const uint8_t*)vy + by);
614
615        float32x4_t sumv0 = vdupq_n_f32(0.0f);
616        float32x4_t summs0 = vdupq_n_f32(0.0f);
617
618        for (int i = 0; i < nb; i++) {
619            const block_q4_1 * GGML_RESTRICT b_x0 = &vx0[i];
620            const block_q4_1 * GGML_RESTRICT b_x1 = &vx1[i];
621            const block_q8_1 * GGML_RESTRICT b_y0 = &vy0[i];
622            const block_q8_1 * GGML_RESTRICT b_y1 = &vy1[i];
623
624            float32_t summs_t[4] = {
625                GGML_CPU_FP16_TO_FP32(b_x0->m) * GGML_CPU_FP16_TO_FP32(b_y0->s),
626                GGML_CPU_FP16_TO_FP32(b_x1->m) * GGML_CPU_FP16_TO_FP32(b_y0->s),
627                GGML_CPU_FP16_TO_FP32(b_x0->m) * GGML_CPU_FP16_TO_FP32(b_y1->s),
628                GGML_CPU_FP16_TO_FP32(b_x1->m) * GGML_CPU_FP16_TO_FP32(b_y1->s)
629            };
630            summs0 = vaddq_f32(summs0, vld1q_f32(summs_t));
631
632            const uint8x16_t m4b = vdupq_n_u8(0x0F);
633
634            const uint8x16_t v0_0 = vld1q_u8(b_x0->qs);
635            const uint8x16_t v0_1 = vld1q_u8(b_x1->qs);
636
637            // 4-bit -> 8-bit
638            const int8x16_t x0_l = vreinterpretq_s8_u8(vandq_u8  (v0_0, m4b));
639            const int8x16_t x0_h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
640            const int8x16_t x1_l = vreinterpretq_s8_u8(vandq_u8  (v0_1, m4b));
641            const int8x16_t x1_h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
642
643            // load y
644            const int8x16_t y0_l = vld1q_s8(b_y0->qs);
645            const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
646            const int8x16_t y1_l = vld1q_s8(b_y1->qs);
647            const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
648
649            // mmla into int32x4_t
650            float32_t _scale[4] = {
651                GGML_CPU_FP16_TO_FP32(b_x0->d)*GGML_CPU_FP16_TO_FP32(b_y0->d),
652                GGML_CPU_FP16_TO_FP32(b_x0->d)*GGML_CPU_FP16_TO_FP32(b_y1->d),
653                GGML_CPU_FP16_TO_FP32(b_x1->d)*GGML_CPU_FP16_TO_FP32(b_y0->d),
654                GGML_CPU_FP16_TO_FP32(b_x1->d)*GGML_CPU_FP16_TO_FP32(b_y1->d)
655            };
656            float32x4_t scale = vld1q_f32(_scale);
657
658            int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
659            int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
660
661            int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
662            int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
663
664            int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
665            int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
666
667            int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
668            int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
669            sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
670                                                l1, r1)), l2, r2)), l3, r3))), scale);
671        }
672
673        float32x4_t sumv1 = vextq_f32 (sumv0, sumv0, 2);
674        float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
675
676        sumv2 = vaddq_f32(sumv2, summs0);
677
678        vst1_f32(s,      vget_low_f32 (sumv2));
679        vst1_f32(s + bs, vget_high_f32(sumv2));
680
681        return;
682    }
683#endif
684
685    int ib = 0;
686    float sumf = 0;
687
688#if defined(__ARM_NEON)
689    float32x4_t sumv0 = vdupq_n_f32(0.0f);
690    float32x4_t sumv1 = vdupq_n_f32(0.0f);
691
692    float summs = 0;
693
694    for (; ib + 1 < nb; ib += 2) {
695        const block_q4_1 * GGML_RESTRICT x0 = &x[ib + 0];
696        const block_q4_1 * GGML_RESTRICT x1 = &x[ib + 1];
697        const block_q8_1 * GGML_RESTRICT y0 = &y[ib + 0];
698        const block_q8_1 * GGML_RESTRICT y1 = &y[ib + 1];
699
700        summs += GGML_CPU_FP16_TO_FP32(x0->m) * GGML_CPU_FP16_TO_FP32(y0->s) + GGML_CPU_FP16_TO_FP32(x1->m) * GGML_CPU_FP16_TO_FP32(y1->s);
701
702        const uint8x16_t m4b = vdupq_n_u8(0x0F);
703
704        const uint8x16_t v0_0 = vld1q_u8(x0->qs);
705        const uint8x16_t v0_1 = vld1q_u8(x1->qs);
706
707        // 4-bit -> 8-bit
708        const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8  (v0_0, m4b));
709        const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
710        const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8  (v0_1, m4b));
711        const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
712
713        // load y
714        const int8x16_t v1_0l = vld1q_s8(y0->qs);
715        const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
716        const int8x16_t v1_1l = vld1q_s8(y1->qs);
717        const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
718
719        // dot product into int32x4_t
720        const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0l, v1_0l), v0_0h, v1_0h);
721        const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1l, v1_1l), v0_1h, v1_1h);
722
723        sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_CPU_FP16_TO_FP32(x0->d)*GGML_CPU_FP16_TO_FP32(y0->d));
724        sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_CPU_FP16_TO_FP32(x1->d)*GGML_CPU_FP16_TO_FP32(y1->d));
725    }
726
727    sumf = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs;
728
729#endif
730    for (; ib < nb; ++ib) {
731        int sumi0 = 0;
732        int sumi1 = 0;
733
734        for (int j = 0; j < qk/2; ++j) {
735            const int v0 = (x[ib].qs[j] & 0x0F);
736            const int v1 = (x[ib].qs[j] >>   4);
737
738            sumi0 += (v0 * y[ib].qs[j]);
739            sumi1 += (v1 * y[ib].qs[j + qk/2]);
740        }
741
742        int sumi = sumi0 + sumi1;
743        sumf += (GGML_CPU_FP16_TO_FP32(x[ib].d)*GGML_CPU_FP16_TO_FP32(y[ib].d))*sumi + GGML_CPU_FP16_TO_FP32(x[ib].m)*GGML_CPU_FP16_TO_FP32(y[ib].s);
744    }
745
746    *s = sumf;
747}
748
749void ggml_vec_dot_mxfp4_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
750    assert(nrc == 1);
751    UNUSED(nrc);
752    UNUSED(bx);
753    UNUSED(by);
754    UNUSED(bs);
755    assert(n % QK_MXFP4 == 0);
756    static_assert(QK_MXFP4 == QK8_0, "QK_MXFP4 and QK8_0 must be the same");
757
758    const block_mxfp4 * GGML_RESTRICT x = vx;
759    const block_q8_0 * GGML_RESTRICT y = vy;
760
761    const int nb = n / QK_MXFP4;
762
763    int ib = 0;
764    float sumf = 0;
765
766#if defined __ARM_NEON
767    const int8x16_t values = vld1q_s8(kvalues_mxfp4);
768    const uint8x16_t m4b = vdupq_n_u8(0x0f);
769    uint8x16x2_t q4bits;
770    int8x16x4_t q4b;
771    int8x16x4_t q8b;
772    int32x4_t prod_1;
773    int32x4_t prod_2;
774
775    for (; ib + 1 < nb; ib += 2) {
776        q4bits.val[0] = vld1q_u8(x[ib + 0].qs);
777        q4bits.val[1] = vld1q_u8(x[ib + 1].qs);
778        q8b.val[0]    = vld1q_s8(y[ib + 0].qs);
779        q8b.val[1]    = vld1q_s8(y[ib + 0].qs + 16);
780        q8b.val[2]    = vld1q_s8(y[ib + 1].qs);
781        q8b.val[3]    = vld1q_s8(y[ib + 1].qs + 16);
782
783        q4b.val[0] = ggml_vqtbl1q_s8(values, vandq_u8  (q4bits.val[0], m4b));
784        q4b.val[1] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[0], 4));
785        q4b.val[2] = ggml_vqtbl1q_s8(values, vandq_u8  (q4bits.val[1], m4b));
786        q4b.val[3] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[1], 4));
787
788        prod_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[0], q8b.val[0]), q4b.val[1], q8b.val[1]);
789        prod_2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[2], q8b.val[2]), q4b.val[3], q8b.val[3]);
790
791        sumf +=
792            GGML_E8M0_TO_FP32_HALF(x[ib + 0].e) * GGML_CPU_FP16_TO_FP32(y[ib + 0].d) * vaddvq_s32(prod_1) +
793            GGML_E8M0_TO_FP32_HALF(x[ib + 1].e) * GGML_CPU_FP16_TO_FP32(y[ib + 1].d) * vaddvq_s32(prod_2);
794    }
795
796#endif
797    for (; ib < nb; ++ib) {
798        const float d = GGML_CPU_FP16_TO_FP32(y[ib].d)*GGML_E8M0_TO_FP32_HALF(x[ib].e);
799        int sumi1 = 0;
800        int sumi2 = 0;
801        for (int j = 0; j < QK_MXFP4/2; ++j) {
802            sumi1 += y[ib].qs[j +          0] * kvalues_mxfp4[x[ib].qs[j] & 0xf];
803            sumi2 += y[ib].qs[j + QK_MXFP4/2] * kvalues_mxfp4[x[ib].qs[j] >>  4];
804        }
805        sumf += d * (sumi1 + sumi2);
806    }
807    *s = sumf;
808}
809
810void ggml_vec_dot_nvfp4_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
811    assert(nrc == 1);
812    UNUSED(nrc);
813    UNUSED(bx);
814    UNUSED(by);
815    UNUSED(bs);
816    assert(n % QK_NVFP4 == 0);
817
818    const block_nvfp4 * GGML_RESTRICT x = vx;
819    const block_q8_0 * GGML_RESTRICT y = vy;
820
821    // Each NVFP4 super-block (64 elements) spans 2 q8_0 blocks
822    const int nb = n / QK_NVFP4;
823
824    float sumf = 0;
825
826#if defined(__ARM_NEON) && defined(__ARM_FEATURE_FMA)
827    const int8x16_t values = vld1q_s8(kvalues_mxfp4);
828    const uint8x16_t m4b = vdupq_n_u8(0x0f);
829    float32x4_t acc = vdupq_n_f32(0.0f);
830
831    for (int ib = 0; ib < nb; ++ib) {
832        const uint8x16_t q4bits_0 = vld1q_u8(x[ib].qs);
833        const uint8x16_t q4bits_1 = vld1q_u8(x[ib].qs + 16);
834
835        const int8x16_t q4_lo_0 = ggml_vqtbl1q_s8(values, vandq_u8  (q4bits_0, m4b));
836        const int8x16_t q4_hi_0 = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits_0, 4));
837        const int8x16_t q4_lo_1 = ggml_vqtbl1q_s8(values, vandq_u8  (q4bits_1, m4b));
838        const int8x16_t q4_hi_1 = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits_1, 4));
839
840#if defined(__ARM_FEATURE_DOTPROD)
841        const int8x16_t q8_0a = vld1q_s8(y[2*ib].qs);
842        const int8x16_t q8_0b = vld1q_s8(y[2*ib].qs + 16);
843        const int8x16_t q8_lo_0 = vcombine_s8(vget_low_s8(q8_0a), vget_low_s8(q8_0b));
844        const int8x16_t q8_hi_0 = vcombine_s8(vget_high_s8(q8_0a), vget_high_s8(q8_0b));
845
846        const int8x16_t q8_1a = vld1q_s8(y[2*ib+1].qs);
847        const int8x16_t q8_1b = vld1q_s8(y[2*ib+1].qs + 16);
848        const int8x16_t q8_lo_1 = vcombine_s8(vget_low_s8(q8_1a), vget_low_s8(q8_1b));
849        const int8x16_t q8_hi_1 = vcombine_s8(vget_high_s8(q8_1a), vget_high_s8(q8_1b));
850
851        const int32x4_t p0 = vaddq_s32(
852            vdotq_s32(vdupq_n_s32(0), q4_lo_0, q8_lo_0),
853            vdotq_s32(vdupq_n_s32(0), q4_hi_0, q8_hi_0));
854        const int32x4_t p1 = vaddq_s32(
855            vdotq_s32(vdupq_n_s32(0), q4_lo_1, q8_lo_1),
856            vdotq_s32(vdupq_n_s32(0), q4_hi_1, q8_hi_1));
857
858        const int32x4_t sumi = vpaddq_s32(p0, p1);
859#else
860        const int8x8_t q4_0_lo = vget_low_s8(q4_lo_0);
861        const int8x8_t q4_0_hi = vget_low_s8(q4_hi_0);
862        const int8x8_t q4_1_lo = vget_high_s8(q4_lo_0);
863        const int8x8_t q4_1_hi = vget_high_s8(q4_hi_0);
864        const int8x8_t q4_2_lo = vget_low_s8(q4_lo_1);
865        const int8x8_t q4_2_hi = vget_low_s8(q4_hi_1);
866        const int8x8_t q4_3_lo = vget_high_s8(q4_lo_1);
867        const int8x8_t q4_3_hi = vget_high_s8(q4_hi_1);
868
869        const int8x8_t q8_0_lo = vld1_s8(y[2*ib].qs);
870        const int8x8_t q8_0_hi = vld1_s8(y[2*ib].qs + 8);
871        const int8x8_t q8_1_lo = vld1_s8(y[2*ib].qs + 16);
872        const int8x8_t q8_1_hi = vld1_s8(y[2*ib].qs + 24);
873        const int8x8_t q8_2_lo = vld1_s8(y[2*ib+1].qs);
874        const int8x8_t q8_2_hi = vld1_s8(y[2*ib+1].qs + 8);
875        const int8x8_t q8_3_lo = vld1_s8(y[2*ib+1].qs + 16);
876        const int8x8_t q8_3_hi = vld1_s8(y[2*ib+1].qs + 24);
877
878        const int32x4_t sumi = (int32x4_t){
879            vaddvq_s32(ggml_nvfp4_dot8(q4_0_lo, q8_0_lo, q4_0_hi, q8_0_hi)),
880            vaddvq_s32(ggml_nvfp4_dot8(q4_1_lo, q8_1_lo, q4_1_hi, q8_1_hi)),
881            vaddvq_s32(ggml_nvfp4_dot8(q4_2_lo, q8_2_lo, q4_2_hi, q8_2_hi)),
882            vaddvq_s32(ggml_nvfp4_dot8(q4_3_lo, q8_3_lo, q4_3_hi, q8_3_hi)),
883        };
884#endif
885
886        const float dy0 = GGML_CPU_FP16_TO_FP32(y[2*ib].d);
887        const float dy1 = GGML_CPU_FP16_TO_FP32(y[2*ib+1].d);
888        const float32x4_t nvsc = {
889            GGML_CPU_UE4M3_TO_FP32(x[ib].d[0]),
890            GGML_CPU_UE4M3_TO_FP32(x[ib].d[1]),
891            GGML_CPU_UE4M3_TO_FP32(x[ib].d[2]),
892            GGML_CPU_UE4M3_TO_FP32(x[ib].d[3])
893        };
894        const float32x4_t scales = vmulq_f32(nvsc, (float32x4_t){dy0, dy0, dy1, dy1});
895
896        acc = vfmaq_f32(acc, vcvtq_f32_s32(sumi), scales);
897    }
898    sumf = vaddvq_f32(acc);
899#else
900    for (int ib = 0; ib < nb; ++ib) {
901        for (int si = 0; si < 4; ++si) {
902            const float d = ggml_ue4m3_to_fp32(x[ib].d[si]);
903            const int q8b = si / 2;
904            const int q8o = (si % 2) * QK_NVFP4_SUB;
905            const float dy = GGML_CPU_FP16_TO_FP32(y[2*ib + q8b].d);
906
907            int sumi_lo = 0, sumi_hi = 0;
908            for (int j = 0; j < QK_NVFP4_SUB/2; ++j) {
909                const uint8_t qv = x[ib].qs[si*(QK_NVFP4_SUB/2) + j];
910                sumi_lo += y[2*ib + q8b].qs[q8o + j +               0] * kvalues_mxfp4[qv & 0xf];
911                sumi_hi += y[2*ib + q8b].qs[q8o + j + QK_NVFP4_SUB/2] * kvalues_mxfp4[qv >>  4];
912            }
913            sumf += dy * d * (sumi_lo + sumi_hi);
914        }
915    }
916#endif
917    *s = sumf;
918}
919
920void ggml_vec_dot_q5_0_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
921    const int qk = QK8_0;
922    const int nb = n / qk;
923
924    int ib = 0;
925    float sumf = 0;
926
927    assert(n % qk == 0);
928    assert(qk == QK5_0);
929    assert(nrc == 1);
930    UNUSED(nrc);
931    UNUSED(bx);
932    UNUSED(by);
933    UNUSED(bs);
934
935    const block_q5_0 * GGML_RESTRICT x = vx;
936    const block_q8_0 * GGML_RESTRICT y = vy;
937
938#if defined(__ARM_NEON)
939    float32x4_t sumv0 = vdupq_n_f32(0.0f);
940    float32x4_t sumv1 = vdupq_n_f32(0.0f);
941
942    uint32_t qh0;
943    uint32_t qh1;
944
945    uint64_t tmp0[4];
946    uint64_t tmp1[4];
947
948    for (; ib + 1 < nb; ib += 2) {
949        const block_q5_0 * GGML_RESTRICT x0 = &x[ib];
950        const block_q5_0 * GGML_RESTRICT x1 = &x[ib + 1];
951        const block_q8_0 * GGML_RESTRICT y0 = &y[ib];
952        const block_q8_0 * GGML_RESTRICT y1 = &y[ib + 1];
953
954        const uint8x16_t m4b = vdupq_n_u8(0x0F);
955
956        // extract the 5th bit via lookup table ((!b) << 4)
957        memcpy(&qh0, x0->qh, sizeof(qh0));
958        memcpy(&qh1, x1->qh, sizeof(qh1));
959
960        tmp0[0] = table_b2b_1[(qh0 >>  0) & 0xFF];
961        tmp0[1] = table_b2b_1[(qh0 >>  8) & 0xFF];
962        tmp0[2] = table_b2b_1[(qh0 >> 16) & 0xFF];
963        tmp0[3] = table_b2b_1[(qh0 >> 24)       ];
964
965        tmp1[0] = table_b2b_1[(qh1 >>  0) & 0xFF];
966        tmp1[1] = table_b2b_1[(qh1 >>  8) & 0xFF];
967        tmp1[2] = table_b2b_1[(qh1 >> 16) & 0xFF];
968        tmp1[3] = table_b2b_1[(qh1 >> 24)       ];
969
970        const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
971        const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
972        const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
973        const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
974
975        const uint8x16_t v0_0 = vld1q_u8(x0->qs);
976        const uint8x16_t v0_1 = vld1q_u8(x1->qs);
977
978        // 4-bit -> 8-bit
979        int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8  (v0_0, m4b));
980        int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
981        int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8  (v0_1, m4b));
982        int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
983
984        // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
985        const int8x16_t v0_0lf = vsubq_s8(v0_0l, qhl0);
986        const int8x16_t v0_0hf = vsubq_s8(v0_0h, qhh0);
987        const int8x16_t v0_1lf = vsubq_s8(v0_1l, qhl1);
988        const int8x16_t v0_1hf = vsubq_s8(v0_1h, qhh1);
989
990        // load y
991        const int8x16_t v1_0l = vld1q_s8(y0->qs);
992        const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
993        const int8x16_t v1_1l = vld1q_s8(y1->qs);
994        const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
995
996        sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
997                        ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
998                        ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_CPU_FP16_TO_FP32(x0->d)*GGML_CPU_FP16_TO_FP32(y0->d));
999        sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
1000                        ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
1001                        ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_CPU_FP16_TO_FP32(x1->d)*GGML_CPU_FP16_TO_FP32(y1->d));
1002    }
1003
1004    sumf = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
1005
1006#endif
1007    for (; ib < nb; ++ib) {
1008        uint32_t qh;
1009        memcpy(&qh, x[ib].qh, sizeof(qh));
1010
1011        int sumi0 = 0;
1012        int sumi1 = 0;
1013
1014        for (int j = 0; j < qk/2; ++j) {
1015            const uint8_t xh_0 = ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
1016            const uint8_t xh_1 = ((qh & (1u << (j + 16))) >> (j + 12));
1017
1018            const int32_t x0 = (int8_t)(((x[ib].qs[j] & 0x0F) | xh_0) - 16);
1019            const int32_t x1 = (int8_t)(((x[ib].qs[j] >>   4) | xh_1) - 16);
1020
1021            sumi0 += (x0 * y[ib].qs[j]);
1022            sumi1 += (x1 * y[ib].qs[j + qk/2]);
1023        }
1024
1025        int sumi = sumi0 + sumi1;
1026        sumf += (GGML_CPU_FP16_TO_FP32(x[ib].d)*GGML_CPU_FP16_TO_FP32(y[ib].d)) * sumi;
1027    }
1028
1029    *s = sumf;
1030}
1031
1032void ggml_vec_dot_q5_1_q8_1(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
1033    const int qk = QK8_1;
1034    const int nb = n / qk;
1035
1036    int ib = 0;
1037    float sumf = 0;
1038
1039    assert(n % qk == 0);
1040    assert(qk == QK5_1);
1041    assert(nrc == 1);
1042    UNUSED(nrc);
1043    UNUSED(bx);
1044    UNUSED(by);
1045    UNUSED(bs);
1046
1047    const block_q5_1 * GGML_RESTRICT x = vx;
1048    const block_q8_1 * GGML_RESTRICT y = vy;
1049
1050#if defined(__ARM_NEON)
1051    float32x4_t sumv0 = vdupq_n_f32(0.0f);
1052    float32x4_t sumv1 = vdupq_n_f32(0.0f);
1053
1054    float summs0 = 0.0f;
1055    float summs1 = 0.0f;
1056
1057    uint32_t qh0;
1058    uint32_t qh1;
1059
1060    uint64_t tmp0[4];
1061    uint64_t tmp1[4];
1062
1063    for (; ib + 1 < nb; ib += 2) {
1064        const block_q5_1 * GGML_RESTRICT x0 = &x[ib];
1065        const block_q5_1 * GGML_RESTRICT x1 = &x[ib + 1];
1066        const block_q8_1 * GGML_RESTRICT y0 = &y[ib];
1067        const block_q8_1 * GGML_RESTRICT y1 = &y[ib + 1];
1068
1069        const uint8x16_t m4b = vdupq_n_u8(0x0F);
1070
1071        summs0 += GGML_CPU_FP16_TO_FP32(x0->m) * GGML_CPU_FP16_TO_FP32(y0->s);
1072        summs1 += GGML_CPU_FP16_TO_FP32(x1->m) * GGML_CPU_FP16_TO_FP32(y1->s);
1073
1074        // extract the 5th bit via lookup table ((b) << 4)
1075        memcpy(&qh0, x0->qh, sizeof(qh0));
1076        memcpy(&qh1, x1->qh, sizeof(qh1));
1077
1078        tmp0[0] = table_b2b_0[(qh0 >>  0) & 0xFF];
1079        tmp0[1] = table_b2b_0[(qh0 >>  8) & 0xFF];
1080        tmp0[2] = table_b2b_0[(qh0 >> 16) & 0xFF];
1081        tmp0[3] = table_b2b_0[(qh0 >> 24)       ];
1082
1083        tmp1[0] = table_b2b_0[(qh1 >>  0) & 0xFF];
1084        tmp1[1] = table_b2b_0[(qh1 >>  8) & 0xFF];
1085        tmp1[2] = table_b2b_0[(qh1 >> 16) & 0xFF];
1086        tmp1[3] = table_b2b_0[(qh1 >> 24)       ];
1087
1088        const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
1089        const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
1090        const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
1091        const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
1092
1093        const uint8x16_t v0_0 = vld1q_u8(x0->qs);
1094        const uint8x16_t v0_1 = vld1q_u8(x1->qs);
1095
1096        // 4-bit -> 8-bit
1097        const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8  (v0_0, m4b));
1098        const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
1099        const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8  (v0_1, m4b));
1100        const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
1101
1102        // add high bit
1103        const int8x16_t v0_0lf = vorrq_s8(v0_0l, qhl0);
1104        const int8x16_t v0_0hf = vorrq_s8(v0_0h, qhh0);
1105        const int8x16_t v0_1lf = vorrq_s8(v0_1l, qhl1);
1106        const int8x16_t v0_1hf = vorrq_s8(v0_1h, qhh1);
1107
1108        // load y
1109        const int8x16_t v1_0l = vld1q_s8(y0->qs);
1110        const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
1111        const int8x16_t v1_1l = vld1q_s8(y1->qs);
1112        const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
1113
1114        sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
1115                        ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
1116                        ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_CPU_FP16_TO_FP32(x0->d)*GGML_CPU_FP16_TO_FP32(y0->d));
1117        sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
1118                        ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
1119                        ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_CPU_FP16_TO_FP32(x1->d)*GGML_CPU_FP16_TO_FP32(y1->d));
1120    }
1121
1122    sumf = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs0 + summs1;
1123
1124#endif
1125    for (; ib < nb; ++ib) {
1126        uint32_t qh;
1127        memcpy(&qh, x[ib].qh, sizeof(qh));
1128
1129        int sumi0 = 0;
1130        int sumi1 = 0;
1131
1132        for (int j = 0; j < qk/2; ++j) {
1133            const uint8_t xh_0 = ((qh >> (j +  0)) << 4) & 0x10;
1134            const uint8_t xh_1 = ((qh >> (j + 12))     ) & 0x10;
1135
1136            const int32_t x0 = (x[ib].qs[j] & 0xF) | xh_0;
1137            const int32_t x1 = (x[ib].qs[j] >>  4) | xh_1;
1138
1139            sumi0 += (x0 * y[ib].qs[j]);
1140            sumi1 += (x1 * y[ib].qs[j + qk/2]);
1141        }
1142
1143        int sumi = sumi0 + sumi1;
1144        sumf += (GGML_CPU_FP16_TO_FP32(x[ib].d)*GGML_CPU_FP16_TO_FP32(y[ib].d))*sumi + GGML_CPU_FP16_TO_FP32(x[ib].m)*GGML_CPU_FP16_TO_FP32(y[ib].s);
1145    }
1146
1147    *s = sumf;
1148}
1149
1150void ggml_vec_dot_q8_0_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
1151    const int qk = QK8_0;
1152    const int nb = n / qk;
1153
1154    assert(n % qk == 0);
1155#if defined(__ARM_FEATURE_MATMUL_INT8)
1156    assert((nrc == 2) || (nrc == 1));
1157#else
1158    assert(nrc == 1);
1159#endif
1160    UNUSED(nrc);
1161    UNUSED(bx);
1162    UNUSED(by);
1163    UNUSED(bs);
1164
1165    const block_q8_0 * GGML_RESTRICT x = vx;
1166    const block_q8_0 * GGML_RESTRICT y = vy;
1167
1168#if defined(__ARM_FEATURE_MATMUL_INT8)
1169    if (nrc == 2) {
1170        const block_q8_0 * GGML_RESTRICT vx0 = vx;
1171        const block_q8_0 * GGML_RESTRICT vx1 = (const block_q8_0 *) ((const uint8_t*)vx + bx);
1172        const block_q8_0 * GGML_RESTRICT vy0 = vy;
1173        const block_q8_0 * GGML_RESTRICT vy1 = (const block_q8_0 *) ((const uint8_t*)vy + by);
1174
1175        float32x4_t sumv0 = vdupq_n_f32(0.0f);
1176
1177        for (int i = 0; i < nb; i++) {
1178            const block_q8_0 * GGML_RESTRICT b_x0 = &vx0[i];
1179            const block_q8_0 * GGML_RESTRICT b_y0 = &vy0[i];
1180
1181            const block_q8_0 * GGML_RESTRICT b_x1 = &vx1[i];
1182            const block_q8_0 * GGML_RESTRICT b_y1 = &vy1[i];
1183
1184            const int8x16_t x0_l = vld1q_s8(b_x0->qs);
1185            const int8x16_t x0_h = vld1q_s8(b_x0->qs + 16);
1186            const int8x16_t x1_l = vld1q_s8(b_x1->qs);
1187            const int8x16_t x1_h = vld1q_s8(b_x1->qs + 16);
1188
1189            // load y
1190            const int8x16_t y0_l = vld1q_s8(b_y0->qs);
1191            const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
1192            const int8x16_t y1_l = vld1q_s8(b_y1->qs);
1193            const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
1194
1195            float32_t _scale[4] = {
1196                GGML_CPU_FP16_TO_FP32(b_x0->d)*GGML_CPU_FP16_TO_FP32(b_y0->d),
1197                GGML_CPU_FP16_TO_FP32(b_x0->d)*GGML_CPU_FP16_TO_FP32(b_y1->d),
1198                GGML_CPU_FP16_TO_FP32(b_x1->d)*GGML_CPU_FP16_TO_FP32(b_y0->d),
1199                GGML_CPU_FP16_TO_FP32(b_x1->d)*GGML_CPU_FP16_TO_FP32(b_y1->d)
1200            };
1201            float32x4_t scale = vld1q_f32(_scale);
1202
1203            int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
1204            int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
1205
1206            int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
1207            int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
1208
1209            int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
1210            int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
1211
1212            int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
1213            int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
1214
1215            sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
1216                                                l1, r1)), l2, r2)), l3, r3))), scale);
1217        }
1218
1219        float32x4_t sumv1 = vextq_f32 (sumv0, sumv0, 2);
1220        float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
1221
1222        vst1_f32(s,      vget_low_f32 (sumv2));
1223        vst1_f32(s + bs, vget_high_f32(sumv2));
1224
1225        return;
1226    }
1227#endif
1228
1229    int ib = 0;
1230    float sumf = 0;
1231
1232#if defined(__ARM_FEATURE_SVE)
1233    svfloat32_t sumv0 = svdup_n_f32(0.0f);
1234    svfloat32_t sumv1 = svdup_n_f32(0.0f);
1235
1236    const int vector_length = ggml_cpu_get_sve_cnt()*8;
1237
1238    //VLA Implementation for SVE
1239    switch (vector_length) {
1240        case 128:
1241            {
1242                // predicate for activating lanes for 16 Int8 elements
1243                const svbool_t ph16 = svptrue_pat_b8 (SV_VL16);
1244                const svbool_t pl16 = svptrue_pat_b32(SV_VL4);
1245
1246                for (; ib + 1 < nb; ib += 2) {
1247                    const block_q8_0 * GGML_RESTRICT x0 = &x[ib + 0];
1248                    const block_q8_0 * GGML_RESTRICT x1 = &x[ib + 1];
1249                    const block_q8_0 * GGML_RESTRICT y0 = &y[ib + 0];
1250                    const block_q8_0 * GGML_RESTRICT y1 = &y[ib + 1];
1251
1252                    // load x
1253                    const svint8_t qx0_0 = svld1_s8(ph16, x0->qs);
1254                    const svint8_t qx0_1 = svld1_s8(ph16, x0->qs+16);
1255                    const svint8_t qx1_0 = svld1_s8(ph16, x1->qs);
1256                    const svint8_t qx1_1 = svld1_s8(ph16, x1->qs+16);
1257
1258                    // load y
1259                    const svint8_t qy0_0 = svld1_s8(ph16, y0->qs);
1260                    const svint8_t qy0_1 = svld1_s8(ph16, y0->qs+16);
1261                    const svint8_t qy1_0 = svld1_s8(ph16, y1->qs);
1262                    const svint8_t qy1_1 = svld1_s8(ph16, y1->qs+16);
1263
1264                    sumv0 = svmla_n_f32_x(pl16, sumv0, svcvt_f32_s32_x(pl16, svadd_x(pl16,
1265                                    svdot_s32(svdup_n_s32(0), qx0_0, qy0_0),
1266                                    svdot_s32(svdup_n_s32(0), qx0_1, qy0_1))), GGML_CPU_FP16_TO_FP32(x0->d)*GGML_CPU_FP16_TO_FP32(y0->d));
1267                    sumv1 = svmla_n_f32_x(pl16, sumv1, svcvt_f32_s32_x(pl16, svadd_x(pl16,
1268                                    svdot_s32(svdup_n_s32(0), qx1_0, qy1_0),
1269                                    svdot_s32(svdup_n_s32(0), qx1_1, qy1_1))), GGML_CPU_FP16_TO_FP32(x1->d)*GGML_CPU_FP16_TO_FP32(y1->d));
1270                }
1271
1272                sumf = svaddv_f32(pl16, svadd_f32_x(pl16, sumv0, sumv1));
1273            } break;
1274        case 256:
1275            {
1276                //printf("sve256");
1277                for (; ib + 1 < nb; ib += 2) {
1278                    const block_q8_0 * GGML_RESTRICT x0 = &x[ib + 0];
1279                    const block_q8_0 * GGML_RESTRICT x1 = &x[ib + 1];
1280                    const block_q8_0 * GGML_RESTRICT y0 = &y[ib + 0];
1281                    const block_q8_0 * GGML_RESTRICT y1 = &y[ib + 1];
1282
1283                    // load x
1284                    const svint8_t qx0 = svld1_s8(svptrue_b8(), x0->qs);
1285                    const svint8_t qx1 = svld1_s8(svptrue_b8(), x1->qs);
1286
1287                    // load y
1288                    const svint8_t qy0 = svld1_s8(svptrue_b8(), y0->qs);
1289                    const svint8_t qy1 = svld1_s8(svptrue_b8(), y1->qs);
1290
1291                    sumv0 = svmla_n_f32_x(svptrue_b32(), sumv0, svcvt_f32_s32_x(svptrue_b32(),
1292                                svdot_s32(svdup_n_s32(0), qx0, qy0)), GGML_CPU_FP16_TO_FP32(x0->d)*GGML_CPU_FP16_TO_FP32(y0->d));
1293                    sumv1 = svmla_n_f32_x(svptrue_b32(), sumv1, svcvt_f32_s32_x(svptrue_b32(),
1294                                svdot_s32(svdup_n_s32(0), qx1, qy1)), GGML_CPU_FP16_TO_FP32(x1->d)*GGML_CPU_FP16_TO_FP32(y1->d));
1295                }
1296
1297                sumf = svaddv_f32(svptrue_b32(), svadd_f32_x(svptrue_b32(), sumv0, sumv1));
1298            } break;
1299        case 512:
1300            {
1301                // predicate for activating high 256 bit
1302                const svbool_t ph32 = svptrue_pat_b8(SV_VL32);
1303                // predicate for activating low 256 bit
1304                const svbool_t pl32 = svnot_b_z(svptrue_b8(), ph32);
1305
1306                // predicate for activating high lanes for 8 float32 elements
1307                const svbool_t ph8 = svptrue_pat_b32(SV_VL8);
1308                // predicate for activating low lanes for 8 float32 elements
1309                const svbool_t pl8 = svnot_b_z(svptrue_b32(), ph8);
1310
1311                svfloat32_t sumv00 = svdup_n_f32(0.0f);
1312
1313                for (; ib + 1 < nb; ib += 2) {
1314                    const block_q8_0 * GGML_RESTRICT x0 = &x[ib + 0];
1315                    const block_q8_0 * GGML_RESTRICT x1 = &x[ib + 1];
1316                    const block_q8_0 * GGML_RESTRICT y0 = &y[ib + 0];
1317                    const block_q8_0 * GGML_RESTRICT y1 = &y[ib + 1];
1318
1319                    //load 32 int8_t in first half of vector and put another 32 int8_t in second vector lower bits
1320                    // and add them to make one 64 element vector
1321                    // load x
1322                    const svint8_t qx_32 = svld1_s8(ph32, x0->qs);
1323                          svint8_t qx_64 = svld1_s8(pl32, x0->qs + 2);
1324
1325                    qx_64 = svadd_s8_x(svptrue_b8(), qx_32, qx_64);
1326
1327                    // load y
1328                    const svint8_t qy_32 = svld1_s8(ph32, y0->qs);
1329                          svint8_t qy_64 = svld1_s8(pl32, y0->qs + 2);
1330
1331                    qy_64 = svadd_s8_x(svptrue_b8(), qy_32, qy_64);
1332
1333                    // scale creation
1334                    const float32_t deq1 = GGML_CPU_FP16_TO_FP32(x0->d)*GGML_CPU_FP16_TO_FP32(y0->d);
1335                    const float32_t deq2 = GGML_CPU_FP16_TO_FP32(x1->d)*GGML_CPU_FP16_TO_FP32(y1->d);
1336
1337                    // duplicate deq1 in first half of vector and deq2 in second half of vector
1338                    const svfloat32_t temp = svdup_f32_m(svdup_f32_z(ph8, deq1), pl8, deq2);
1339
1340                    const svfloat32_t sumvt = svcvt_f32_s32_x(svptrue_b32(), svdot_s32(svdup_n_s32(0), qx_64, qy_64));
1341
1342                    sumv00 = svmla_f32_m(svptrue_b32(), sumv00, sumvt, temp);
1343                }
1344
1345                sumf = svaddv_f32(svptrue_b32(), sumv00);
1346                break;
1347            }
1348        default:
1349            assert(false && "Unsupported vector length");
1350            break;
1351    }
1352#elif defined(__ARM_NEON)
1353    float32x4_t sumv0 = vdupq_n_f32(0.0f);
1354    float32x4_t sumv1 = vdupq_n_f32(0.0f);
1355
1356    for (; ib + 1 < nb; ib += 2) {
1357        const block_q8_0 * GGML_RESTRICT x0 = &x[ib + 0];
1358        const block_q8_0 * GGML_RESTRICT x1 = &x[ib + 1];
1359        const block_q8_0 * GGML_RESTRICT y0 = &y[ib + 0];
1360        const block_q8_0 * GGML_RESTRICT y1 = &y[ib + 1];
1361
1362        const int8x16_t x0_0 = vld1q_s8(x0->qs);
1363        const int8x16_t x0_1 = vld1q_s8(x0->qs + 16);
1364        const int8x16_t x1_0 = vld1q_s8(x1->qs);
1365        const int8x16_t x1_1 = vld1q_s8(x1->qs + 16);
1366
1367        // load y
1368        const int8x16_t y0_0 = vld1q_s8(y0->qs);
1369        const int8x16_t y0_1 = vld1q_s8(y0->qs + 16);
1370        const int8x16_t y1_0 = vld1q_s8(y1->qs);
1371        const int8x16_t y1_1 = vld1q_s8(y1->qs + 16);
1372
1373        sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
1374                        ggml_vdotq_s32(vdupq_n_s32(0), x0_0, y0_0),
1375                        ggml_vdotq_s32(vdupq_n_s32(0), x0_1, y0_1))), GGML_CPU_FP16_TO_FP32(x0->d)*GGML_CPU_FP16_TO_FP32(y0->d));
1376
1377        sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
1378                        ggml_vdotq_s32(vdupq_n_s32(0), x1_0, y1_0),
1379                        ggml_vdotq_s32(vdupq_n_s32(0), x1_1, y1_1))), GGML_CPU_FP16_TO_FP32(x1->d)*GGML_CPU_FP16_TO_FP32(y1->d));
1380    }
1381
1382    sumf = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
1383#endif
1384    for (; ib < nb; ++ib) {
1385        int sumi = 0;
1386
1387        for (int j = 0; j < qk; j++) {
1388            sumi += x[ib].qs[j]*y[ib].qs[j];
1389        }
1390
1391        sumf += sumi*(GGML_CPU_FP16_TO_FP32(x[ib].d)*GGML_CPU_FP16_TO_FP32(y[ib].d));
1392    }
1393
1394    *s = sumf;
1395}
1396
1397void ggml_vec_dot_tq1_0_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
1398    assert(nrc == 1);
1399    UNUSED(nrc);
1400    UNUSED(bx);
1401    UNUSED(by);
1402    UNUSED(bs);
1403
1404    const block_tq1_0 * GGML_RESTRICT x = vx;
1405    const block_q8_K  * GGML_RESTRICT y = vy;
1406
1407    const int nb = n / QK_K;
1408
1409#if defined(__ARM_NEON)
1410    float sumf = 0.0f;
1411
1412    uint8_t k_shift[16] = {1, 1, 1, 1, 3, 3, 3, 3, 9, 9, 9, 9, 27, 27, 27, 27};
1413
1414    const uint8x16_t shift = vld1q_u8(k_shift);
1415
1416    for (int i = 0; i < nb; ++i) {
1417#if defined(__ARM_FEATURE_DOTPROD)
1418        int32x4_t sumi0 = vdupq_n_s32(0);
1419        int32x4_t sumi1 = vdupq_n_s32(0);
1420#else
1421        int16x8_t sumi0 = vdupq_n_s16(0);
1422        int16x8_t sumi1 = vdupq_n_s16(0);
1423#endif
1424
1425        // first 32 bytes of 5 elements
1426        {
1427            uint8x16_t qx0 = vld1q_u8(x[i].qs + 0);
1428            uint8x16_t qx1 = vld1q_u8(x[i].qs + 16);
1429            uint8x16_t qx2 = vmulq_u8(qx0, vdupq_n_u8(3));
1430            uint8x16_t qx3 = vmulq_u8(qx1, vdupq_n_u8(3));
1431            uint8x16_t qx4 = vmulq_u8(qx0, vdupq_n_u8(9));
1432            uint8x16_t qx5 = vmulq_u8(qx1, vdupq_n_u8(9));
1433            uint8x16_t qx6 = vmulq_u8(qx0, vdupq_n_u8(27));
1434            uint8x16_t qx7 = vmulq_u8(qx1, vdupq_n_u8(27));
1435            uint8x16_t qx8 = vmulq_u8(qx0, vdupq_n_u8(81));
1436            uint8x16_t qx9 = vmulq_u8(qx1, vdupq_n_u8(81));
1437
1438            // multiply by 3 and keep the 2 bits above 8 bits
1439            int8x16_t sqx0 = vreinterpretq_s8_u8(vshrq_n_u8(vhaddq_u8(qx0, vshrq_n_u8(qx0, 1)), 6));
1440            int8x16_t sqx1 = vreinterpretq_s8_u8(vshrq_n_u8(vhaddq_u8(qx1, vshrq_n_u8(qx1, 1)), 6));
1441            int8x16_t sqx2 = vreinterpretq_s8_u8(vshrq_n_u8(vhaddq_u8(qx2, vshrq_n_u8(qx2, 1)), 6));
1442            int8x16_t sqx3 = vreinterpretq_s8_u8(vshrq_n_u8(vhaddq_u8(qx3, vshrq_n_u8(qx3, 1)), 6));
1443            int8x16_t sqx4 = vreinterpretq_s8_u8(vshrq_n_u8(vhaddq_u8(qx4, vshrq_n_u8(qx4, 1)), 6));
1444            int8x16_t sqx5 = vreinterpretq_s8_u8(vshrq_n_u8(vhaddq_u8(qx5, vshrq_n_u8(qx5, 1)), 6));
1445            int8x16_t sqx6 = vreinterpretq_s8_u8(vshrq_n_u8(vhaddq_u8(qx6, vshrq_n_u8(qx6, 1)), 6));
1446            int8x16_t sqx7 = vreinterpretq_s8_u8(vshrq_n_u8(vhaddq_u8(qx7, vshrq_n_u8(qx7, 1)), 6));
1447            int8x16_t sqx8 = vreinterpretq_s8_u8(vshrq_n_u8(vhaddq_u8(qx8, vshrq_n_u8(qx8, 1)), 6));
1448            int8x16_t sqx9 = vreinterpretq_s8_u8(vshrq_n_u8(vhaddq_u8(qx9, vshrq_n_u8(qx9, 1)), 6));
1449
1450            const int8x16_t qy0 = vld1q_s8(y[i].qs +   0);
1451            const int8x16_t qy1 = vld1q_s8(y[i].qs +  16);
1452            const int8x16_t qy2 = vld1q_s8(y[i].qs +  32);
1453            const int8x16_t qy3 = vld1q_s8(y[i].qs +  48);
1454            const int8x16_t qy4 = vld1q_s8(y[i].qs +  64);
1455            const int8x16_t qy5 = vld1q_s8(y[i].qs +  80);
1456            const int8x16_t qy6 = vld1q_s8(y[i].qs +  96);
1457            const int8x16_t qy7 = vld1q_s8(y[i].qs + 112);
1458            const int8x16_t qy8 = vld1q_s8(y[i].qs + 128);
1459            const int8x16_t qy9 = vld1q_s8(y[i].qs + 144);
1460
1461#if defined(__ARM_FEATURE_DOTPROD)
1462            sumi0 = vdotq_s32(sumi0, sqx0, qy0);
1463            sumi1 = vdotq_s32(sumi1, sqx1, qy1);
1464            sumi0 = vdotq_s32(sumi0, sqx2, qy2);
1465            sumi1 = vdotq_s32(sumi1, sqx3, qy3);
1466            sumi0 = vdotq_s32(sumi0, sqx4, qy4);
1467            sumi1 = vdotq_s32(sumi1, sqx5, qy5);
1468            sumi0 = vdotq_s32(sumi0, sqx6, qy6);
1469            sumi1 = vdotq_s32(sumi1, sqx7, qy7);
1470            sumi0 = vdotq_s32(sumi0, sqx8, qy8);
1471            sumi1 = vdotq_s32(sumi1, sqx9, qy9);
1472#else
1473            sumi0 = vmlal_s8(sumi0, vget_low_s8(sqx0), vget_low_s8(qy0));
1474            sumi1 = vmlal_s8(sumi1, vget_high_s8(sqx0), vget_high_s8(qy0));
1475            sumi0 = vmlal_s8(sumi0, vget_low_s8(sqx1), vget_low_s8(qy1));
1476            sumi1 = vmlal_s8(sumi1, vget_high_s8(sqx1), vget_high_s8(qy1));
1477            sumi0 = vmlal_s8(sumi0, vget_low_s8(sqx2), vget_low_s8(qy2));
1478            sumi1 = vmlal_s8(sumi1, vget_high_s8(sqx2), vget_high_s8(qy2));
1479            sumi0 = vmlal_s8(sumi0, vget_low_s8(sqx3), vget_low_s8(qy3));
1480            sumi1 = vmlal_s8(sumi1, vget_high_s8(sqx3), vget_high_s8(qy3));
1481            sumi0 = vmlal_s8(sumi0, vget_low_s8(sqx4), vget_low_s8(qy4));
1482            sumi1 = vmlal_s8(sumi1, vget_high_s8(sqx4), vget_high_s8(qy4));
1483            sumi0 = vmlal_s8(sumi0, vget_low_s8(sqx5), vget_low_s8(qy5));
1484            sumi1 = vmlal_s8(sumi1, vget_high_s8(sqx5), vget_high_s8(qy5));
1485            sumi0 = vmlal_s8(sumi0, vget_low_s8(sqx6), vget_low_s8(qy6));
1486            sumi1 = vmlal_s8(sumi1, vget_high_s8(sqx6), vget_high_s8(qy6));
1487            sumi0 = vmlal_s8(sumi0, vget_low_s8(sqx7), vget_low_s8(qy7));
1488            sumi1 = vmlal_s8(sumi1, vget_high_s8(sqx7), vget_high_s8(qy7));
1489            sumi0 = vmlal_s8(sumi0, vget_low_s8(sqx8), vget_low_s8(qy8));
1490            sumi1 = vmlal_s8(sumi1, vget_high_s8(sqx8), vget_high_s8(qy8));
1491            sumi0 = vmlal_s8(sumi0, vget_low_s8(sqx9), vget_low_s8(qy9));
1492            sumi1 = vmlal_s8(sumi1, vget_high_s8(sqx9), vget_high_s8(qy9));
1493#endif
1494        }
1495
1496        // last 16 bytes of 5-element, along with the 4 bytes of 4 elements
1497        {
1498            uint8x16_t qx0 = vld1q_u8(x[i].qs + 32);
1499            uint8x16_t qx1 = vmulq_u8(qx0, vdupq_n_u8(3));
1500            uint8x16_t qx2 = vmulq_u8(qx0, vdupq_n_u8(9));
1501            uint8x16_t qx3 = vmulq_u8(qx0, vdupq_n_u8(27));
1502            uint8x16_t qx4 = vmulq_u8(qx0, vdupq_n_u8(81));
1503            uint32_t qh;
1504            memcpy(&qh, x[i].qh, sizeof(qh)); // potentially unaligned
1505            uint8x16_t qx5 = vreinterpretq_u8_u32(vdupq_n_u32(qh));
1506            qx5 = vmulq_u8(qx5, shift);
1507
1508            // multiply by 3 and keep the 2 bits above 8 bits
1509            int8x16_t sqx0 = vreinterpretq_s8_u8(vshrq_n_u8(vhaddq_u8(qx0, vshrq_n_u8(qx0, 1)), 6));
1510            int8x16_t sqx1 = vreinterpretq_s8_u8(vshrq_n_u8(vhaddq_u8(qx1, vshrq_n_u8(qx1, 1)), 6));
1511            int8x16_t sqx2 = vreinterpretq_s8_u8(vshrq_n_u8(vhaddq_u8(qx2, vshrq_n_u8(qx2, 1)), 6));
1512            int8x16_t sqx3 = vreinterpretq_s8_u8(vshrq_n_u8(vhaddq_u8(qx3, vshrq_n_u8(qx3, 1)), 6));
1513            int8x16_t sqx4 = vreinterpretq_s8_u8(vshrq_n_u8(vhaddq_u8(qx4, vshrq_n_u8(qx4, 1)), 6));
1514            int8x16_t sqx5 = vreinterpretq_s8_u8(vshrq_n_u8(vhaddq_u8(qx5, vshrq_n_u8(qx5, 1)), 6));
1515
1516            const int8x16_t qy0 = vld1q_s8(y[i].qs + 160);
1517            const int8x16_t qy1 = vld1q_s8(y[i].qs + 176);
1518            const int8x16_t qy2 = vld1q_s8(y[i].qs + 192);
1519            const int8x16_t qy3 = vld1q_s8(y[i].qs + 208);
1520            const int8x16_t qy4 = vld1q_s8(y[i].qs + 224);
1521            const int8x16_t qy5 = vld1q_s8(y[i].qs + 240);
1522
1523#if defined(__ARM_FEATURE_DOTPROD)
1524            sumi0 = vdotq_s32(sumi0, sqx0, qy0);
1525            sumi1 = vdotq_s32(sumi1, sqx1, qy1);
1526            sumi0 = vdotq_s32(sumi0, sqx2, qy2);
1527            sumi1 = vdotq_s32(sumi1, sqx3, qy3);
1528            sumi0 = vdotq_s32(sumi0, sqx4, qy4);
1529            sumi1 = vdotq_s32(sumi1, sqx5, qy5);
1530#else
1531            sumi0 = vmlal_s8(sumi0, vget_low_s8(sqx0), vget_low_s8(qy0));
1532            sumi1 = vmlal_s8(sumi1, vget_high_s8(sqx0), vget_high_s8(qy0));
1533            sumi0 = vmlal_s8(sumi0, vget_low_s8(sqx1), vget_low_s8(qy1));
1534            sumi1 = vmlal_s8(sumi1, vget_high_s8(sqx1), vget_high_s8(qy1));
1535            sumi0 = vmlal_s8(sumi0, vget_low_s8(sqx2), vget_low_s8(qy2));
1536            sumi1 = vmlal_s8(sumi1, vget_high_s8(sqx2), vget_high_s8(qy2));
1537            sumi0 = vmlal_s8(sumi0, vget_low_s8(sqx3), vget_low_s8(qy3));
1538            sumi1 = vmlal_s8(sumi1, vget_high_s8(sqx3), vget_high_s8(qy3));
1539            sumi0 = vmlal_s8(sumi0, vget_low_s8(sqx4), vget_low_s8(qy4));
1540            sumi1 = vmlal_s8(sumi1, vget_high_s8(sqx4), vget_high_s8(qy4));
1541            sumi0 = vmlal_s8(sumi0, vget_low_s8(sqx5), vget_low_s8(qy5));
1542            sumi1 = vmlal_s8(sumi1, vget_high_s8(sqx5), vget_high_s8(qy5));
1543#endif
1544        }
1545
1546        const int16x8_t ysum0 = vld1q_s16(y[i].bsums);
1547        const int16x8_t ysum1 = vld1q_s16(y[i].bsums + 8);
1548
1549        const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d;
1550
1551#if defined(__ARM_FEATURE_DOTPROD)
1552        sumi0 = vaddq_s32(sumi0, sumi1);
1553        sumi0 = vsubq_s32(sumi0, vpaddlq_s16(vaddq_s16(ysum0, ysum1)));
1554
1555        sumf += d * (float) vaddvq_s32(sumi0);
1556#else
1557        sumi0 = vaddq_s16(sumi0, sumi1);
1558        sumi0 = vsubq_s16(sumi0, vaddq_s16(ysum0, ysum1));
1559
1560        sumf += d * (float) vaddlvq_s16(sumi0);
1561#endif
1562    }
1563
1564    *s = sumf;
1565
1566#else
1567    UNUSED(x);
1568    UNUSED(y);
1569    UNUSED(nb);
1570    ggml_vec_dot_tq1_0_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc);
1571#endif
1572}
1573
1574void ggml_vec_dot_tq2_0_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
1575    assert(nrc == 1);
1576    UNUSED(nrc);
1577    UNUSED(bx);
1578    UNUSED(by);
1579    UNUSED(bs);
1580
1581    const block_tq2_0 * GGML_RESTRICT x = vx;
1582    const block_q8_K  * GGML_RESTRICT y = vy;
1583
1584    const int nb = n / QK_K;
1585
1586#if defined(__ARM_NEON)
1587    float sumf = 0.0f;
1588
1589    const uint8x16_t m3 = vdupq_n_u8(3);
1590
1591    for (int i = 0; i < nb; ++i) {
1592#if defined(__ARM_FEATURE_DOTPROD)
1593        int32x4_t sumi0 = vdupq_n_s32(0);
1594        int32x4_t sumi1 = vdupq_n_s32(0);
1595#else
1596        int16x8_t sumi0 = vdupq_n_s16(0);
1597        int16x8_t sumi1 = vdupq_n_s16(0);
1598#endif
1599
1600        for (size_t j = 0; j < sizeof(x->qs); j += 32) {
1601            uint8x16_t qx0 = vld1q_u8(x[i].qs + j);
1602            uint8x16_t qx1 = vld1q_u8(x[i].qs + j + 16);
1603            uint8x16_t qx2 = vshrq_n_u8(qx0, 2);
1604            uint8x16_t qx3 = vshrq_n_u8(qx1, 2);
1605            uint8x16_t qx4 = vshrq_n_u8(qx0, 4);
1606            uint8x16_t qx5 = vshrq_n_u8(qx1, 4);
1607            uint8x16_t qx6 = vshrq_n_u8(qx0, 6);
1608            uint8x16_t qx7 = vshrq_n_u8(qx1, 6);
1609
1610            int8x16_t sqx0 = vreinterpretq_s8_u8(vandq_u8(qx0, m3));
1611            int8x16_t sqx1 = vreinterpretq_s8_u8(vandq_u8(qx1, m3));
1612            int8x16_t sqx2 = vreinterpretq_s8_u8(vandq_u8(qx2, m3));
1613            int8x16_t sqx3 = vreinterpretq_s8_u8(vandq_u8(qx3, m3));
1614            int8x16_t sqx4 = vreinterpretq_s8_u8(vandq_u8(qx4, m3));
1615            int8x16_t sqx5 = vreinterpretq_s8_u8(vandq_u8(qx5, m3));
1616            int8x16_t sqx6 = vreinterpretq_s8_u8(vandq_u8(qx6, m3));
1617            int8x16_t sqx7 = vreinterpretq_s8_u8(vandq_u8(qx7, m3));
1618
1619            const int8x16_t qy0 = vld1q_s8(y[i].qs + j*4 +   0);
1620            const int8x16_t qy1 = vld1q_s8(y[i].qs + j*4 +  16);
1621            const int8x16_t qy2 = vld1q_s8(y[i].qs + j*4 +  32);
1622            const int8x16_t qy3 = vld1q_s8(y[i].qs + j*4 +  48);
1623            const int8x16_t qy4 = vld1q_s8(y[i].qs + j*4 +  64);
1624            const int8x16_t qy5 = vld1q_s8(y[i].qs + j*4 +  80);
1625            const int8x16_t qy6 = vld1q_s8(y[i].qs + j*4 +  96);
1626            const int8x16_t qy7 = vld1q_s8(y[i].qs + j*4 + 112);
1627
1628#if defined(__ARM_FEATURE_DOTPROD)
1629            sumi0 = vdotq_s32(sumi0, sqx0, qy0);
1630            sumi1 = vdotq_s32(sumi1, sqx1, qy1);
1631            sumi0 = vdotq_s32(sumi0, sqx2, qy2);
1632            sumi1 = vdotq_s32(sumi1, sqx3, qy3);
1633            sumi0 = vdotq_s32(sumi0, sqx4, qy4);
1634            sumi1 = vdotq_s32(sumi1, sqx5, qy5);
1635            sumi0 = vdotq_s32(sumi0, sqx6, qy6);
1636            sumi1 = vdotq_s32(sumi1, sqx7, qy7);
1637#else
1638            sumi0 = vmlal_s8(sumi0, vget_low_s8(sqx0), vget_low_s8(qy0));
1639            sumi1 = vmlal_s8(sumi1, vget_high_s8(sqx0), vget_high_s8(qy0));
1640            sumi0 = vmlal_s8(sumi0, vget_low_s8(sqx1), vget_low_s8(qy1));
1641            sumi1 = vmlal_s8(sumi1, vget_high_s8(sqx1), vget_high_s8(qy1));
1642            sumi0 = vmlal_s8(sumi0, vget_low_s8(sqx2), vget_low_s8(qy2));
1643            sumi1 = vmlal_s8(sumi1, vget_high_s8(sqx2), vget_high_s8(qy2));
1644            sumi0 = vmlal_s8(sumi0, vget_low_s8(sqx3), vget_low_s8(qy3));
1645            sumi1 = vmlal_s8(sumi1, vget_high_s8(sqx3), vget_high_s8(qy3));
1646            sumi0 = vmlal_s8(sumi0, vget_low_s8(sqx4), vget_low_s8(qy4));
1647            sumi1 = vmlal_s8(sumi1, vget_high_s8(sqx4), vget_high_s8(qy4));
1648            sumi0 = vmlal_s8(sumi0, vget_low_s8(sqx5), vget_low_s8(qy5));
1649            sumi1 = vmlal_s8(sumi1, vget_high_s8(sqx5), vget_high_s8(qy5));
1650            sumi0 = vmlal_s8(sumi0, vget_low_s8(sqx6), vget_low_s8(qy6));
1651            sumi1 = vmlal_s8(sumi1, vget_high_s8(sqx6), vget_high_s8(qy6));
1652            sumi0 = vmlal_s8(sumi0, vget_low_s8(sqx7), vget_low_s8(qy7));
1653            sumi1 = vmlal_s8(sumi1, vget_high_s8(sqx7), vget_high_s8(qy7));
1654#endif
1655        }
1656
1657        const int16x8_t ysum0 = vld1q_s16(y[i].bsums);
1658        const int16x8_t ysum1 = vld1q_s16(y[i].bsums + 8);
1659
1660        const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d;
1661
1662#if defined(__ARM_FEATURE_DOTPROD)
1663        sumi0 = vaddq_s32(sumi0, sumi1);
1664        sumi0 = vsubq_s32(sumi0, vpaddlq_s16(vaddq_s16(ysum0, ysum1)));
1665
1666        sumf += d * (float) vaddvq_s32(sumi0);
1667#else
1668        sumi0 = vaddq_s16(sumi0, sumi1);
1669        sumi0 = vsubq_s16(sumi0, vaddq_s16(ysum0, ysum1));
1670
1671        sumf += d * (float) vaddlvq_s16(sumi0);
1672#endif
1673    }
1674
1675    *s = sumf;
1676
1677#else
1678    UNUSED(x);
1679    UNUSED(y);
1680    UNUSED(nb);
1681    ggml_vec_dot_tq2_0_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc);
1682#endif
1683}
1684
1685void ggml_vec_dot_q2_K_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
1686    assert(nrc == 1);
1687    UNUSED(nrc);
1688    UNUSED(bx);
1689    UNUSED(by);
1690    UNUSED(bs);
1691
1692    const block_q2_K * GGML_RESTRICT x = vx;
1693    const block_q8_K * GGML_RESTRICT y = vy;
1694
1695    const int nb = n / QK_K;
1696
1697#ifdef __ARM_FEATURE_SVE
1698    const int vector_length = svcntb()*8;
1699    const svuint8_t m3s = svdup_n_u8(0x3);
1700    const svuint32_t m4s = svdup_n_u32(0xF);
1701    const svint32_t vzero_sv = svdup_n_s32(0);
1702    svfloat32_t acc_sum = svdup_n_f32(0);
1703    svbool_t pred_s32 = svptrue_pat_b32(SV_VL4);
1704
1705    switch (vector_length) {
1706        case 128:
1707            for (int i = 0; i < nb; ++i) {
1708                const float d = y[i].d * GGML_CPU_FP16_TO_FP32(x[i].d);
1709                svfloat32_t d_broad = svdup_n_f32((float32_t)d);
1710                const float dmin = -y[i].d * GGML_CPU_FP16_TO_FP32(x[i].dmin);
1711                svfloat32_t dmin_broad = svdup_n_f32((float32_t)dmin);
1712
1713                const uint8_t * GGML_RESTRICT q2 = x[i].qs;
1714                const int8_t  * GGML_RESTRICT q8_sv = y[i].qs;
1715                const uint8_t * GGML_RESTRICT sc = x[i].scales;
1716
1717                svuint32_t mins_and_scales_sve = svld1ub_u32(svptrue_b32(), sc);
1718                const svint32_t mins_sv_1 = svreinterpret_s32_u32(svlsr_n_u32_x(svptrue_b32(), mins_and_scales_sve, 4));
1719
1720                mins_and_scales_sve = svld1ub_u32(svptrue_b32(), sc+4);
1721                const svint32_t mins_sv_2 = svreinterpret_s32_u32(svlsr_n_u32_x(svptrue_b32(), mins_and_scales_sve, 4));
1722
1723                svint32_t q8sums_sv_1 = svld1sh_s32(svptrue_b32(), y[i].bsums);
1724                svint32_t q8sums_sv_2 = svld1sh_s32(svptrue_b32(), y[i].bsums+4);
1725
1726                const svint32_t s0 = svadd_s32_x(svptrue_b32(), svmul_s32_x(svptrue_b32(), mins_sv_1, q8sums_sv_1), svmul_s32_x(svptrue_b32(), mins_sv_2, q8sums_sv_2));
1727
1728                mins_and_scales_sve = svld1ub_u32(svptrue_b32(), sc+8);
1729                const svint32_t mins_sv_3 = svreinterpret_s32_u32(svlsr_n_u32_x(svptrue_b32(), mins_and_scales_sve, 4));
1730
1731                mins_and_scales_sve = svld1ub_u32(svptrue_b32(), sc+12);
1732                const svint32_t mins_sv_4 = svreinterpret_s32_u32(svlsr_n_u32_x(svptrue_b32(), mins_and_scales_sve, 4));
1733
1734                q8sums_sv_1 = svld1sh_s32(svptrue_b32(), y[i].bsums+8);
1735                q8sums_sv_2 = svld1sh_s32(svptrue_b32(), y[i].bsums+12);
1736
1737                svint32_t s1 = svadd_s32_x(svptrue_b32(), svmul_s32_x(svptrue_b32(), mins_sv_3, q8sums_sv_1), svmul_s32_x(svptrue_b32(), mins_sv_4, q8sums_sv_2));
1738
1739                svfloat32_t temp = svcvt_f32_s32_x(svptrue_b32(), svadd_s32_x(svptrue_b32(), s0, s1));
1740
1741                acc_sum = svmla_f32_m(svptrue_b32(), acc_sum, temp, dmin_broad);
1742
1743                svint32_t sumi1 = svdup_n_s32(0);
1744
1745                {
1746                    const svuint8_t q2bits_1 = svld1_u8(svptrue_b8(), q2);
1747                    svint8_t q2bytes_sv = svreinterpret_s8_u8(svand_u8_x(svptrue_b8(), q2bits_1, m3s));
1748                    svint8_t q8bytes_sv = svld1_s8(svptrue_b8(), q8_sv); q8_sv += 16;
1749                    const svint32_t scales_sv = svreinterpret_s32_u32(svand_u32_m(svptrue_b32(), svld1ub_u32(svptrue_b32(), sc), m4s));
1750
1751                    sumi1 = svmla_s32_m(svptrue_b32(), sumi1, svdot_s32(vzero_sv, q2bytes_sv, q8bytes_sv), svdup_lane_s32(scales_sv, 0));
1752
1753                    const svuint8_t q2bits_3 = svld1_u8(svptrue_b8(), q2+16);
1754                    q2bytes_sv = svreinterpret_s8_u8(svand_u8_x(svptrue_b8(), q2bits_3, m3s));
1755                    q8bytes_sv = svld1_s8(svptrue_b8(), q8_sv); q8_sv += 16;
1756
1757                    sumi1 = svmla_s32_m(svptrue_b32(), sumi1, svdot_s32(vzero_sv, q2bytes_sv, q8bytes_sv), svdup_lane_s32(scales_sv, 1));
1758
1759                    q2bytes_sv = svreinterpret_s8_u8(svand_u8_x(svptrue_b8(), svlsr_n_u8_x(svptrue_b8(), q2bits_1, 2), m3s));
1760                    q8bytes_sv = svld1_s8(svptrue_b8(), q8_sv); q8_sv += 16;
1761
1762                    sumi1 = svmla_s32_m(svptrue_b32(), sumi1, svdot_s32(vzero_sv, q2bytes_sv, q8bytes_sv), svdup_lane_s32(scales_sv, 2));
1763
1764                    q2bytes_sv = svreinterpret_s8_u8(svand_u8_x(svptrue_b8(), svlsr_n_u8_x(svptrue_b8(), q2bits_3, 2), m3s));
1765                    q8bytes_sv = svld1_s8(svptrue_b8(), q8_sv); q8_sv += 16;
1766
1767                    sumi1 = svmla_s32_m(svptrue_b32(), sumi1, svdot_s32(vzero_sv, q2bytes_sv, q8bytes_sv), svdup_lane_s32(scales_sv, 3));
1768
1769
1770                    const svint32_t scales_sv_1 = svreinterpret_s32_u32(svand_u32_m(svptrue_b32(), svld1ub_u32(svptrue_b32(), sc+4), m4s));
1771
1772                    q2bytes_sv = svreinterpret_s8_u8(svand_u8_x(svptrue_b8(), svlsr_n_u8_x(svptrue_b8(), q2bits_1, 4), m3s));
1773                    q8bytes_sv = svld1_s8(svptrue_b8(), q8_sv); q8_sv += 16;
1774
1775                    sumi1 = svmla_s32_m(svptrue_b32(), sumi1, svdot_s32(vzero_sv, q2bytes_sv, q8bytes_sv), svdup_lane_s32(scales_sv_1, 0));
1776
1777                    q2bytes_sv = svreinterpret_s8_u8(svand_u8_x(svptrue_b8(), svlsr_n_u8_x(svptrue_b8(), q2bits_3, 4), m3s));
1778                    q8bytes_sv = svld1_s8(svptrue_b8(), q8_sv); q8_sv += 16;
1779
1780                    sumi1 = svmla_s32_m(svptrue_b32(), sumi1, svdot_s32(vzero_sv, q2bytes_sv, q8bytes_sv), svdup_lane_s32(scales_sv_1, 1));
1781
1782                    q2bytes_sv = svreinterpret_s8_u8(svand_u8_x(svptrue_b8(), svlsr_n_u8_x(svptrue_b8(), q2bits_1, 6), m3s));
1783                    q8bytes_sv = svld1_s8(svptrue_b8(), q8_sv); q8_sv += 16;
1784
1785                    sumi1 = svmla_s32_m(svptrue_b32(), sumi1, svdot_s32(vzero_sv, q2bytes_sv, q8bytes_sv), svdup_lane_s32(scales_sv_1, 2));
1786
1787                    q2bytes_sv = svreinterpret_s8_u8(svand_u8_x(svptrue_b8(), svlsr_n_u8_x(svptrue_b8(), q2bits_3, 6), m3s));
1788                    q8bytes_sv = svld1_s8(svptrue_b8(), q8_sv); q8_sv += 16;
1789
1790                    sumi1 = svmla_s32_m(svptrue_b32(), sumi1, svdot_s32(vzero_sv, q2bytes_sv, q8bytes_sv), svdup_lane_s32(scales_sv_1, 3));
1791
1792                    //-------------------------------
1793
1794                    q2 += 32;
1795                    const svint32_t scales_sv_2 = svreinterpret_s32_u32(svand_u32_m(svptrue_b32(), svld1ub_u32(svptrue_b32(), sc+8), m4s));
1796                    const svuint8_t q2bits_2 = svld1_u8(svptrue_b8(), q2);
1797
1798                    q2bytes_sv = svreinterpret_s8_u8(svand_u8_x(svptrue_b8(), q2bits_2, m3s));
1799                    q8bytes_sv = svld1_s8(svptrue_b8(), q8_sv); q8_sv += 16;
1800
1801                    sumi1 = svmla_s32_m(svptrue_b32(), sumi1, svdot_s32(vzero_sv, q2bytes_sv, q8bytes_sv), svdup_lane_s32(scales_sv_2, 0));
1802
1803                    const svuint8_t q2bits_4 = svld1_u8(svptrue_b8(), q2+16);
1804                    q2bytes_sv = svreinterpret_s8_u8(svand_u8_x(svptrue_b8(), q2bits_4, m3s));
1805                    q8bytes_sv = svld1_s8(svptrue_b8(), q8_sv); q8_sv += 16;
1806
1807                    sumi1 = svmla_s32_m(svptrue_b32(), sumi1, svdot_s32(vzero_sv, q2bytes_sv, q8bytes_sv), svdup_lane_s32(scales_sv_2, 1));
1808
1809
1810                    q2bytes_sv = svreinterpret_s8_u8(svand_u8_x(svptrue_b8(), svlsr_n_u8_x(svptrue_b8(), q2bits_2, 2), m3s));
1811                    q8bytes_sv = svld1_s8(svptrue_b8(), q8_sv); q8_sv += 16;
1812
1813                    sumi1 = svmla_s32_m(svptrue_b32(), sumi1, svdot_s32(vzero_sv, q2bytes_sv, q8bytes_sv), svdup_lane_s32(scales_sv_2, 2));
1814
1815                    q2bytes_sv = svreinterpret_s8_u8(svand_u8_x(svptrue_b8(), svlsr_n_u8_x(svptrue_b8(), q2bits_4, 2), m3s));
1816                    q8bytes_sv = svld1_s8(svptrue_b8(), q8_sv); q8_sv += 16;
1817
1818                    sumi1 = svmla_s32_m(svptrue_b32(), sumi1, svdot_s32(vzero_sv, q2bytes_sv, q8bytes_sv), svdup_lane_s32(scales_sv_2, 3));
1819
1820
1821                    const svint32_t scales_sv_3 = svreinterpret_s32_u32(svand_u32_m(svptrue_b32(), svld1ub_u32(svptrue_b32(), sc+12), m4s));
1822
1823                    q2bytes_sv = svreinterpret_s8_u8(svand_u8_x(svptrue_b8(), svlsr_n_u8_x(svptrue_b8(), q2bits_2, 4), m3s));
1824                    q8bytes_sv = svld1_s8(svptrue_b8(), q8_sv); q8_sv += 16;
1825
1826                    sumi1 = svmla_s32_m(svptrue_b32(), sumi1, svdot_s32(vzero_sv, q2bytes_sv, q8bytes_sv), svdup_lane_s32(scales_sv_3, 0));
1827
1828                    q2bytes_sv = svreinterpret_s8_u8(svand_u8_x(svptrue_b8(), svlsr_n_u8_x(svptrue_b8(), q2bits_4, 4), m3s));
1829                    q8bytes_sv = svld1_s8(svptrue_b8(), q8_sv); q8_sv += 16;
1830
1831                    sumi1 = svmla_s32_m(svptrue_b32(), sumi1, svdot_s32(vzero_sv, q2bytes_sv, q8bytes_sv), svdup_lane_s32(scales_sv_3, 1));
1832
1833
1834
1835                    q2bytes_sv = svreinterpret_s8_u8(svand_u8_x(svptrue_b8(), svlsr_n_u8_x(svptrue_b8(), q2bits_2, 6), m3s));
1836                    q8bytes_sv = svld1_s8(svptrue_b8(), q8_sv); q8_sv += 16;
1837
1838                    sumi1 = svmla_s32_m(svptrue_b32(), sumi1, svdot_s32(vzero_sv, q2bytes_sv, q8bytes_sv), svdup_lane_s32(scales_sv_3, 2));
1839
1840                    q2bytes_sv = svreinterpret_s8_u8(svand_u8_x(svptrue_b8(), svlsr_n_u8_x(svptrue_b8(), q2bits_4, 6), m3s));
1841                    q8bytes_sv = svld1_s8(svptrue_b8(), q8_sv); q8_sv += 16;
1842
1843                    sumi1 = svmla_s32_m(svptrue_b32(), sumi1, svdot_s32(vzero_sv, q2bytes_sv, q8bytes_sv), svdup_lane_s32(scales_sv_3, 3));
1844                }
1845                acc_sum = svmla_f32_m(svptrue_b32(), acc_sum, svcvt_f32_s32_x(svptrue_b32(), sumi1), d_broad);
1846            }
1847            *s = svaddv_f32(svptrue_b32(), acc_sum);
1848            break;
1849
1850        case 256:
1851        case 512:
1852            for (int i = 0; i < nb; ++i) {
1853                const float d = y[i].d * GGML_CPU_FP16_TO_FP32(x[i].d);
1854                svfloat32_t d_broad = svdup_n_f32((float32_t)d);
1855                const float dmin = -y[i].d * GGML_CPU_FP16_TO_FP32(x[i].dmin);
1856                svfloat32_t dmin_broad = svdup_n_f32((float32_t)dmin);
1857
1858                const uint8_t * GGML_RESTRICT q2 = x[i].qs;
1859                const int8_t  * GGML_RESTRICT q8_sv = y[i].qs;
1860                const uint8_t * GGML_RESTRICT sc = x[i].scales;
1861
1862                const svuint32_t mins_and_scales_sve = svld1ub_u32(svptrue_pat_b32(SV_VL8), sc); sc += 8;
1863                const svint32_t scales_sv = svreinterpret_s32_u32(svand_u32_m(svptrue_pat_b32(SV_VL8), mins_and_scales_sve, m4s));
1864                const svint32_t mins_sv_1 = svreinterpret_s32_u32(svlsr_n_u32_x(svptrue_pat_b32(SV_VL8), mins_and_scales_sve, 4));
1865                svint32_t q8sums_sv_1 = svld1sh_s32(svptrue_pat_b32(SV_VL8), y[i].bsums);
1866
1867                const svuint32_t mins_and_scales_sve_1 = svld1ub_u32(svptrue_pat_b32(SV_VL8), sc);
1868                const svint32_t scales_sv_1 = svreinterpret_s32_u32(svand_u32_m(svptrue_pat_b32(SV_VL8), mins_and_scales_sve_1, m4s));
1869                const svint32_t mins_sv_2 = svreinterpret_s32_u32(svlsr_n_u32_x(svptrue_pat_b32(SV_VL8), mins_and_scales_sve_1, 4));
1870
1871                svint32_t q8sums_sv_2 = svld1sh_s32(svptrue_pat_b32(SV_VL8), y[i].bsums+8);
1872
1873                svfloat32_t temp = svcvt_f32_s32_x(svptrue_pat_b32(SV_VL8), svadd_s32_x(svptrue_pat_b32(SV_VL8), svmul_s32_x(svptrue_pat_b32(SV_VL8), mins_sv_1, q8sums_sv_1), svmul_s32_x(svptrue_pat_b32(SV_VL8), mins_sv_2, q8sums_sv_2)));
1874
1875                acc_sum = svmla_f32_m(svptrue_pat_b32(SV_VL8), acc_sum, temp, dmin_broad);
1876
1877                svint32_t sumi1 = svdup_n_s32(0);
1878
1879                {
1880                    const svuint8_t q2bits_1 = svld1_u8(svptrue_pat_b8(SV_VL32), q2);
1881                    svint8_t q2bytes_sv = svreinterpret_s8_u8(svand_u8_m(svptrue_pat_b8(SV_VL32), q2bits_1, m3s));
1882                    svint8_t q8bytes_sv = svld1_s8(svptrue_pat_b8(SV_VL32), q8_sv); q8_sv += 32;
1883
1884                    svint32_t scale_1 = svsel(pred_s32, svdup_lane_s32(scales_sv, 0), svdup_lane_s32(scales_sv, 1));
1885                    sumi1 = svmla_s32_m(svptrue_pat_b32(SV_VL8), sumi1, svdot_s32(vzero_sv, q2bytes_sv, q8bytes_sv), scale_1);
1886
1887                    q2bytes_sv = svreinterpret_s8_u8(svand_u8_m(svptrue_pat_b8(SV_VL32), svlsr_n_u8_x(svptrue_pat_b8(SV_VL32), q2bits_1, 2), m3s));
1888                    q8bytes_sv = svld1_s8(svptrue_pat_b8(SV_VL32), q8_sv); q8_sv += 32;
1889
1890                    svint32_t scale_2 = svsel(pred_s32, svdup_lane_s32(scales_sv, 2), svdup_lane_s32(scales_sv, 3));
1891                    sumi1 = svmla_s32_m(svptrue_pat_b32(SV_VL8), sumi1, svdot_s32(svdup_n_s32(0), q2bytes_sv, q8bytes_sv), scale_2);
1892
1893                    q2bytes_sv = svreinterpret_s8_u8(svand_u8_m(svptrue_pat_b8(SV_VL32), svlsr_n_u8_x(svptrue_pat_b8(SV_VL32), q2bits_1, 4), m3s));
1894                    q8bytes_sv = svld1_s8(svptrue_pat_b8(SV_VL32), q8_sv); q8_sv += 32;
1895
1896                    scale_1 = svsel(pred_s32, svdup_lane_s32(scales_sv, 4), svdup_lane_s32(scales_sv, 5));
1897                    sumi1 = svmla_s32_m(svptrue_pat_b32(SV_VL8), sumi1, svdot_s32(vzero_sv, q2bytes_sv, q8bytes_sv), scale_1);
1898
1899                    q2bytes_sv = svreinterpret_s8_u8(svand_u8_m(svptrue_pat_b8(SV_VL32), svlsr_n_u8_x(svptrue_pat_b8(SV_VL32), q2bits_1, 6), m3s));
1900                    q8bytes_sv = svld1_s8(svptrue_pat_b8(SV_VL32), q8_sv); q8_sv += 32;
1901
1902                    scale_2 = svsel(pred_s32, svdup_lane_s32(scales_sv, 6), svdup_lane_s32(scales_sv, 7));
1903                    sumi1 = svmla_s32_m(svptrue_pat_b32(SV_VL8), sumi1, svdot_s32(vzero_sv, q2bytes_sv, q8bytes_sv), scale_2);
1904
1905                    q2 += 32;
1906
1907                    const svuint8_t q2bits_2 = svld1_u8(svptrue_pat_b8(SV_VL32), q2);
1908                    q2bytes_sv = svreinterpret_s8_u8(svand_u8_m(svptrue_pat_b8(SV_VL32), q2bits_2, m3s));
1909                    q8bytes_sv = svld1_s8(svptrue_pat_b8(SV_VL32), q8_sv); q8_sv += 32;
1910
1911                    scale_1 = svsel(pred_s32, svdup_lane_s32(scales_sv_1, 0), svdup_lane_s32(scales_sv_1, 1));
1912                    sumi1 = svmla_s32_m(svptrue_pat_b32(SV_VL8), sumi1, svdot_s32(vzero_sv, q2bytes_sv, q8bytes_sv), scale_1);
1913
1914                    q2bytes_sv = svreinterpret_s8_u8(svand_u8_m(svptrue_pat_b8(SV_VL32), svlsr_n_u8_x(svptrue_pat_b8(SV_VL32), q2bits_2, 2), m3s));
1915                    q8bytes_sv = svld1_s8(svptrue_pat_b8(SV_VL32), q8_sv); q8_sv += 32;
1916
1917                    scale_2 = svsel(pred_s32, svdup_lane_s32(scales_sv_1, 2), svdup_lane_s32(scales_sv_1, 3));
1918                    sumi1 = svmla_s32_m(svptrue_pat_b32(SV_VL8), sumi1, svdot_s32(vzero_sv, q2bytes_sv, q8bytes_sv), scale_2);
1919
1920                    q2bytes_sv = svreinterpret_s8_u8(svand_u8_m(svptrue_pat_b8(SV_VL32), svlsr_n_u8_x(svptrue_pat_b8(SV_VL32), q2bits_2, 4), m3s));
1921                    q8bytes_sv = svld1_s8(svptrue_pat_b8(SV_VL32), q8_sv); q8_sv += 32;
1922
1923                    scale_1 = svsel(pred_s32, svdup_lane_s32(scales_sv_1, 4), svdup_lane_s32(scales_sv_1, 5));
1924                    sumi1 = svmla_s32_m(svptrue_pat_b32(SV_VL8), sumi1, svdot_s32(vzero_sv, q2bytes_sv, q8bytes_sv), scale_1);
1925
1926                    q2bytes_sv = svreinterpret_s8_u8(svand_u8_m(svptrue_pat_b8(SV_VL32), svlsr_n_u8_x(svptrue_pat_b8(SV_VL32), q2bits_2, 6), m3s));
1927                    q8bytes_sv = svld1_s8(svptrue_pat_b8(SV_VL32), q8_sv); q8_sv += 32;
1928
1929                    scale_2 = svsel(pred_s32, svdup_lane_s32(scales_sv_1, 6), svdup_lane_s32(scales_sv_1, 7));
1930                    sumi1 = svmla_s32_m(svptrue_pat_b32(SV_VL8), sumi1, svdot_s32(vzero_sv, q2bytes_sv, q8bytes_sv), scale_2);
1931                }
1932                acc_sum = svmla_f32_m(svptrue_pat_b32(SV_VL8), acc_sum, svcvt_f32_s32_x(svptrue_pat_b32(SV_VL8), sumi1), d_broad);
1933            }
1934            *s = svaddv_f32(svptrue_pat_b32(SV_VL8), acc_sum);
1935            break;
1936
1937        default:
1938            assert(false && "Unsupported vector length");
1939            break;
1940    }
1941
1942#elif __ARM_NEON
1943    const uint8x16_t m3 = vdupq_n_u8(0x3);
1944    const uint8x16_t m4 = vdupq_n_u8(0xF);
1945
1946    const int32x4_t vzero = vdupq_n_s32(0);
1947
1948    ggml_int8x16x2_t q2bytes;
1949    uint8_t aux[16];
1950
1951    float sum = 0;
1952
1953    for (int i = 0; i < nb; ++i) {
1954        const float d = y[i].d * GGML_CPU_FP16_TO_FP32(x[i].d);
1955        const float dmin = -y[i].d * GGML_CPU_FP16_TO_FP32(x[i].dmin);
1956
1957        const uint8_t * GGML_RESTRICT q2 = x[i].qs;
1958        const int8_t  * GGML_RESTRICT q8 = y[i].qs;
1959        const uint8_t * GGML_RESTRICT sc = x[i].scales;
1960
1961        const uint8x16_t mins_and_scales = vld1q_u8(sc);
1962        const uint8x16_t scales = vandq_u8(mins_and_scales, m4);
1963        vst1q_u8(aux, scales);
1964
1965        const uint8x16_t mins = vshrq_n_u8(mins_and_scales, 4);
1966        const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
1967        const ggml_int16x8x2_t mins16 = {{vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mins))), vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mins)))}};
1968        const int32x4_t s0 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[0]), vget_low_s16 (q8sums.val[0])),
1969                                       vmull_s16(vget_high_s16(mins16.val[0]), vget_high_s16(q8sums.val[0])));
1970        const int32x4_t s1 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[1]), vget_low_s16 (q8sums.val[1])),
1971                                       vmull_s16(vget_high_s16(mins16.val[1]), vget_high_s16(q8sums.val[1])));
1972        sum += dmin * vaddvq_s32(vaddq_s32(s0, s1));
1973
1974        int isum = 0;
1975        int is = 0;
1976
1977// We use this macro instead of a function call because for some reason
1978// the code runs 2-3% slower, even if the function is declared inline
1979#define MULTIPLY_ACCUM_WITH_SCALE(index)\
1980        isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * aux[is+(index)];\
1981        isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * aux[is+1+(index)];
1982
1983#define SHIFT_MULTIPLY_ACCUM_WITH_SCALE(shift, index)\
1984        q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;\
1985        q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[0], (shift)), m3));\
1986        q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[1], (shift)), m3));\
1987        MULTIPLY_ACCUM_WITH_SCALE((index));
1988
1989        for (int j = 0; j < QK_K/128; ++j) {
1990            const ggml_uint8x16x2_t q2bits = ggml_vld1q_u8_x2(q2); q2 += 32;
1991
1992            ggml_int8x16x2_t q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
1993            q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[0], m3));
1994            q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[1], m3));
1995
1996            MULTIPLY_ACCUM_WITH_SCALE(0);
1997
1998            SHIFT_MULTIPLY_ACCUM_WITH_SCALE(2, 2);
1999            SHIFT_MULTIPLY_ACCUM_WITH_SCALE(4, 4);
2000            SHIFT_MULTIPLY_ACCUM_WITH_SCALE(6, 6);
2001
2002            is += 8;
2003        }
2004
2005        sum += d * isum;
2006    }
2007
2008    *s = sum;
2009
2010#else
2011    UNUSED(x);
2012    UNUSED(y);
2013    UNUSED(nb);
2014    ggml_vec_dot_q2_K_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc);
2015#endif
2016}
2017
2018void ggml_vec_dot_q3_K_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
2019    assert(n % QK_K == 0);
2020    assert(nrc == 1);
2021    UNUSED(nrc);
2022    UNUSED(bx);
2023    UNUSED(by);
2024    UNUSED(bs);
2025
2026    const uint32_t kmask1 = 0x03030303;
2027    const uint32_t kmask2 = 0x0f0f0f0f;
2028
2029    const block_q3_K * GGML_RESTRICT x = vx;
2030    const block_q8_K * GGML_RESTRICT y = vy;
2031
2032    const int nb = n / QK_K;
2033
2034#if defined(__ARM_FEATURE_SVE)
2035
2036    uint32_t aux[3];
2037    uint32_t utmp[4];
2038
2039    const int8_t m32 = 32;
2040    const int vector_length = svcntb()*8;
2041    const svuint8_t m3b_sv = svdup_n_u8(0x3);
2042    const svint32_t vzero_sv = svdup_n_s32(0);
2043
2044    const svuint8_t m0_sv = svdup_n_u8(1);
2045    const svuint8_t m1_sv = svlsl_n_u8_x(svptrue_b8(), m0_sv, 1);
2046    const svuint8_t m2_sv = svlsl_n_u8_x(svptrue_b8(), m0_sv, 2);
2047    const svuint8_t m3_sv = svlsl_n_u8_x(svptrue_b8(), m0_sv, 3);
2048
2049    float sum = 0;
2050
2051    for (int i = 0; i < nb; ++i) {
2052
2053        const float d = y[i].d * GGML_CPU_FP16_TO_FP32(x[i].d);
2054
2055        const uint8_t * GGML_RESTRICT q3_sv = x[i].qs;
2056        const uint8_t * GGML_RESTRICT qh_sv = x[i].hmask;
2057        const int8_t  * GGML_RESTRICT q8_sv = y[i].qs;
2058
2059        // Set up scales
2060        memcpy(aux, x[i].scales, 12);
2061        utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
2062        utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
2063        utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
2064        utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
2065
2066        int8_t * scale = (int8_t *)utmp;
2067
2068        for (int j = 0; j < 16; ++j) scale[j] -= m32;
2069
2070        switch (vector_length) {
2071            case 128:
2072                {
2073                    svuint8_t qhbits_sv_1 = svld1_u8(svptrue_b8(), qh_sv);
2074                    svuint8_t qhbits_sv_2 = svld1_u8(svptrue_b8(), qh_sv+16);
2075                    svuint8_t q3h_sv;
2076
2077                    svint32_t sumi1_1 = svdup_n_s32(0);
2078                    svint8_t q3bytes_sv;
2079
2080                    for (int j = 0; j < QK_K/128; ++j) {
2081
2082                        const svuint8_t q3bits_sv = svld1_u8(svptrue_b8(), q3_sv); q3_sv += 16;
2083                        const svuint8_t q3bits_sv_1 = svld1_u8(svptrue_b8(), q3_sv); q3_sv += 16;
2084                        svint8_t q8bytes_1_sv_1 = svld1_s8(svptrue_b8(), q8_sv); q8_sv += 16;
2085                        svint8_t q8bytes_1_sv_2 = svld1_s8(svptrue_b8(), q8_sv); q8_sv += 16;
2086
2087                        q3h_sv = svlsl_n_u8_x(svptrue_b8(), svbic_u8_x(svptrue_b8(), m0_sv, qhbits_sv_1), 2);
2088                        q3bytes_sv = svsub_s8_x(svptrue_b8(), svreinterpret_s8_u8(svand_u8_m(svptrue_b8(), q3bits_sv, m3b_sv)), svreinterpret_s8_u8(q3h_sv));
2089
2090                        sumi1_1 = svmla_s32_m(svptrue_b32(), sumi1_1, svdot_s32(vzero_sv, q3bytes_sv, q8bytes_1_sv_1), svdup_n_s32((int32_t)scale[0]));
2091
2092                        q3h_sv = svlsl_n_u8_x(svptrue_b8(), svbic_u8_x(svptrue_b8(), m0_sv, qhbits_sv_2), 2);
2093                        q3bytes_sv = svsub_s8_x(svptrue_b8(), svreinterpret_s8_u8(svand_u8_m(svptrue_b8(), q3bits_sv_1, m3b_sv)), svreinterpret_s8_u8(q3h_sv));
2094
2095                        sumi1_1 = svmla_s32_m(svptrue_b32(), sumi1_1, svdot_s32(vzero_sv, q3bytes_sv, q8bytes_1_sv_2), svdup_n_s32((int32_t)scale[1]));
2096
2097                        q8bytes_1_sv_1 = svld1_s8(svptrue_b8(), q8_sv); q8_sv += 16;
2098                        q8bytes_1_sv_2 = svld1_s8(svptrue_b8(), q8_sv); q8_sv += 16;
2099
2100                        q3h_sv = svlsl_n_u8_x(svptrue_b8(), svbic_u8_x(svptrue_b8(), m1_sv, qhbits_sv_1), 1);
2101                        q3bytes_sv = svsub_s8_x(svptrue_b8(), svreinterpret_s8_u8(svand_u8_m(svptrue_b8(), svlsr_n_u8_x(svptrue_b8(), q3bits_sv, 2), m3b_sv)), svreinterpret_s8_u8(q3h_sv));
2102
2103                        sumi1_1 = svmla_s32_m(svptrue_b32(), sumi1_1, svdot_s32(vzero_sv, q3bytes_sv, q8bytes_1_sv_1), svdup_n_s32((int32_t)scale[2]));
2104
2105                        q3h_sv = svlsl_n_u8_x(svptrue_b8(), svbic_u8_x(svptrue_b8(), m1_sv, qhbits_sv_2), 1);
2106                        q3bytes_sv = svsub_s8_x(svptrue_b8(), svreinterpret_s8_u8(svand_u8_m(svptrue_b8(), svlsr_n_u8_x(svptrue_b8(), q3bits_sv_1, 2), m3b_sv)), svreinterpret_s8_u8(q3h_sv));
2107
2108                        sumi1_1 = svmla_s32_m(svptrue_b32(), sumi1_1, svdot_s32(vzero_sv, q3bytes_sv, q8bytes_1_sv_2), svdup_n_s32((int32_t)scale[3]));
2109
2110
2111                        scale += 4;
2112                        q8bytes_1_sv_1 = svld1_s8(svptrue_b8(), q8_sv); q8_sv += 16;
2113                        q8bytes_1_sv_2 = svld1_s8(svptrue_b8(), q8_sv); q8_sv += 16;
2114
2115                        q3h_sv = svbic_u8_x(svptrue_b8(), m2_sv, qhbits_sv_1);
2116                        q3bytes_sv = svsub_s8_x(svptrue_b8(), svreinterpret_s8_u8(svand_u8_m(svptrue_b8(), svlsr_n_u8_x(svptrue_b8(), q3bits_sv, 4), m3b_sv)), svreinterpret_s8_u8(q3h_sv));
2117
2118                        sumi1_1 = svmla_s32_m(svptrue_b32(), sumi1_1, svdot_s32(vzero_sv, q3bytes_sv, q8bytes_1_sv_1), svdup_n_s32((int32_t)scale[0]));
2119
2120                        q3h_sv = svbic_u8_x(svptrue_b8(), m2_sv, qhbits_sv_2);
2121                        q3bytes_sv = svsub_s8_x(svptrue_b8(), svreinterpret_s8_u8(svand_u8_m(svptrue_b8(), svlsr_n_u8_x(svptrue_b8(), q3bits_sv_1, 4), m3b_sv)), svreinterpret_s8_u8(q3h_sv));
2122
2123                        sumi1_1 = svmla_s32_m(svptrue_b32(), sumi1_1, svdot_s32(vzero_sv, q3bytes_sv, q8bytes_1_sv_2), svdup_n_s32((int32_t)scale[1]));
2124
2125
2126                        q8bytes_1_sv_1 = svld1_s8(svptrue_b8(), q8_sv); q8_sv += 16;
2127                        q8bytes_1_sv_2 = svld1_s8(svptrue_b8(), q8_sv); q8_sv += 16;
2128
2129                        q3h_sv = svlsr_n_u8_x(svptrue_b8(), svbic_u8_x(svptrue_b8(), m3_sv, qhbits_sv_1), 1);
2130                        q3bytes_sv = svsub_s8_x(svptrue_b8(), svreinterpret_s8_u8(svand_u8_m(svptrue_b8(), svlsr_n_u8_x(svptrue_b8(), q3bits_sv, 6), m3b_sv)), svreinterpret_s8_u8(q3h_sv));
2131
2132                        sumi1_1 = svmla_s32_m(svptrue_b32(), sumi1_1, svdot_s32(vzero_sv, q3bytes_sv, q8bytes_1_sv_1), svdup_n_s32((int32_t)scale[2]));
2133
2134                        q3h_sv = svlsr_n_u8_x(svptrue_b8(), svbic_u8_x(svptrue_b8(), m3_sv, qhbits_sv_2), 1);
2135                        q3bytes_sv = svsub_s8_x(svptrue_b8(), svreinterpret_s8_u8(svand_u8_m(svptrue_b8(), svlsr_n_u8_x(svptrue_b8(), q3bits_sv_1, 6), m3b_sv)), svreinterpret_s8_u8(q3h_sv));
2136
2137                        sumi1_1 = svmla_s32_m(svptrue_b32(), sumi1_1, svdot_s32(vzero_sv, q3bytes_sv, q8bytes_1_sv_2), svdup_n_s32((int32_t)scale[3]));
2138
2139                        if (j == 0) {
2140                            qhbits_sv_1 = svlsr_n_u8_x(svptrue_b8(), qhbits_sv_1, 4);
2141                            qhbits_sv_2 = svlsr_n_u8_x(svptrue_b8(), qhbits_sv_2, 4);
2142                        }
2143
2144                        scale += 4;
2145                    }
2146
2147                    sum += d * (svaddv_s32(svptrue_b32(), sumi1_1));
2148                } break;
2149            case 256:
2150            case 512:
2151                {
2152                    svuint8_t qhbits_sv = svld1_u8(svptrue_pat_b8(SV_VL32), qh_sv);
2153                    svuint8_t q3h_sv;
2154
2155                    svint32_t sumi1_1 = svdup_n_s32(0);
2156                    svint8_t q3bytes_sv;
2157
2158                    for (int j = 0; j < QK_K/128; ++j) {
2159
2160                        const svuint8_t q3bits_sv = svld1_u8(svptrue_pat_b8(SV_VL32), q3_sv); q3_sv += 32;
2161                        svint8_t q8bytes_1_sv_1 = svld1_s8(svptrue_pat_b8(SV_VL32), q8_sv); q8_sv += 32;
2162                        svint8_t q8bytes_1_sv_2 = svld1_s8(svptrue_pat_b8(SV_VL32), q8_sv); q8_sv += 32;
2163
2164                        q3h_sv = svlsl_n_u8_x(svptrue_pat_b8(SV_VL32), svbic_u8_x(svptrue_pat_b8(SV_VL32), m0_sv, qhbits_sv), 2);
2165                        q3bytes_sv = svsub_s8_x(svptrue_pat_b8(SV_VL32), svreinterpret_s8_u8(svand_u8_m(svptrue_pat_b8(SV_VL32), q3bits_sv, m3b_sv)), svreinterpret_s8_u8(q3h_sv));
2166
2167
2168                        svint32_t scale_1 = svsel_s32(svptrue_pat_b32(SV_VL4), svdup_n_s32((int32_t)scale[0]), svdup_n_s32((int32_t)scale[1]));
2169                        sumi1_1 = svmla_s32_m(svptrue_pat_b32(SV_VL8), sumi1_1, svdot_s32(vzero_sv, q3bytes_sv, q8bytes_1_sv_1), scale_1);
2170
2171                        q3h_sv = svlsl_n_u8_x(svptrue_pat_b8(SV_VL32), svbic_u8_x(svptrue_pat_b8(SV_VL32), m1_sv, qhbits_sv), 1);
2172                        q3bytes_sv = svsub_s8_x(svptrue_pat_b8(SV_VL32), svreinterpret_s8_u8(svand_u8_m(svptrue_pat_b8(SV_VL32), svlsr_n_u8_x(svptrue_pat_b8(SV_VL32), q3bits_sv, 2), m3b_sv)), svreinterpret_s8_u8(q3h_sv));
2173
2174                        scale_1 = svsel_s32(svptrue_pat_b32(SV_VL4), svdup_n_s32((int32_t)scale[2]), svdup_n_s32((int32_t)scale[3]));
2175                        sumi1_1 = svmla_s32_m(svptrue_pat_b32(SV_VL8), sumi1_1, svdot_s32(vzero_sv, q3bytes_sv, q8bytes_1_sv_2), scale_1);
2176
2177                        scale += 4;
2178                        q8bytes_1_sv_1 = svld1_s8(svptrue_pat_b8(SV_VL32), q8_sv); q8_sv += 32;
2179                        q8bytes_1_sv_2 = svld1_s8(svptrue_pat_b8(SV_VL32), q8_sv); q8_sv += 32;
2180
2181                        q3h_sv = svbic_u8_x(svptrue_pat_b8(SV_VL32), m2_sv, qhbits_sv);
2182                        q3bytes_sv = svsub_s8_x(svptrue_pat_b8(SV_VL32), svreinterpret_s8_u8(svand_u8_m(svptrue_pat_b8(SV_VL32), svlsr_n_u8_x(svptrue_pat_b8(SV_VL32), q3bits_sv, 4), m3b_sv)), svreinterpret_s8_u8(q3h_sv));
2183
2184                        scale_1 = svsel_s32(svptrue_pat_b32(SV_VL4), svdup_n_s32((int32_t)scale[0]), svdup_n_s32((int32_t)scale[1]));
2185                        sumi1_1 = svmla_s32_m(svptrue_pat_b32(SV_VL8), sumi1_1, svdot_s32(vzero_sv, q3bytes_sv, q8bytes_1_sv_1), scale_1);
2186
2187                        q3h_sv = svlsr_n_u8_x(svptrue_pat_b8(SV_VL32), svbic_u8_x(svptrue_pat_b8(SV_VL32), m3_sv, qhbits_sv), 1);
2188                        q3bytes_sv = svsub_s8_x(svptrue_pat_b8(SV_VL32), svreinterpret_s8_u8(svand_u8_m(svptrue_pat_b8(SV_VL32), svlsr_n_u8_x(svptrue_pat_b8(SV_VL32), q3bits_sv, 6), m3b_sv)), svreinterpret_s8_u8(q3h_sv));
2189
2190                        scale_1 = svsel_s32(svptrue_pat_b32(SV_VL4), svdup_n_s32((int32_t)scale[2]), svdup_n_s32((int32_t)scale[3]));
2191                        sumi1_1 = svmla_s32_m(svptrue_pat_b32(SV_VL8), sumi1_1, svdot_s32(vzero_sv, q3bytes_sv, q8bytes_1_sv_2), scale_1);
2192
2193                        if (j == 0) {
2194                            qhbits_sv = svlsr_n_u8_x(svptrue_pat_b8(SV_VL32), qhbits_sv, 4);
2195                        }
2196
2197                        scale += 4;
2198                    }
2199
2200                    sum += d * (svaddv_s32(svptrue_pat_b32(SV_VL8), sumi1_1));
2201                } break;
2202            default:
2203                assert(false && "Unsupported vector length");
2204                break;
2205        }
2206    }
2207    *s = sum;
2208
2209#elif __ARM_NEON
2210
2211    uint32_t aux[3];
2212    uint32_t utmp[4];
2213
2214    const uint8x16_t m3b = vdupq_n_u8(0x3);
2215    const int32x4_t  vzero = vdupq_n_s32(0);
2216
2217    const uint8x16_t m0 = vdupq_n_u8(1);
2218    const uint8x16_t m1 = vshlq_n_u8(m0, 1);
2219    const uint8x16_t m2 = vshlq_n_u8(m0, 2);
2220    const uint8x16_t m3 = vshlq_n_u8(m0, 3);
2221    const int8_t m32 = 32;
2222
2223    ggml_int8x16x4_t q3bytes;
2224
2225    float sum = 0;
2226
2227    for (int i = 0; i < nb; ++i) {
2228
2229        const float d = y[i].d * GGML_CPU_FP16_TO_FP32(x[i].d);
2230
2231        const uint8_t * GGML_RESTRICT q3 = x[i].qs;
2232        const uint8_t * GGML_RESTRICT qh = x[i].hmask;
2233        const int8_t  * GGML_RESTRICT q8 = y[i].qs;
2234
2235        ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
2236
2237        ggml_uint8x16x4_t q3h;
2238
2239        int32_t isum = 0;
2240
2241        // Set up scales
2242        memcpy(aux, x[i].scales, 12);
2243        utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
2244        utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
2245        utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
2246        utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
2247
2248        int8_t * scale = (int8_t *)utmp;
2249        for (int j = 0; j < 16; ++j) scale[j] -= m32;
2250
2251        for (int j = 0; j < QK_K/128; ++j) {
2252
2253            const ggml_uint8x16x2_t q3bits = ggml_vld1q_u8_x2(q3); q3 += 32;
2254            const ggml_int8x16x4_t q8bytes_1 = ggml_vld1q_s8_x4(q8); q8 += 64;
2255            const ggml_int8x16x4_t q8bytes_2 = ggml_vld1q_s8_x4(q8); q8 += 64;
2256
2257            q3h.val[0] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[0]), 2);
2258            q3h.val[1] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[1]), 2);
2259            q3h.val[2] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[0]), 1);
2260            q3h.val[3] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[1]), 1);
2261
2262            q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[0], m3b)), vreinterpretq_s8_u8(q3h.val[0]));
2263            q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[1], m3b)), vreinterpretq_s8_u8(q3h.val[1]));
2264            q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 2), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
2265            q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 2), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
2266
2267            isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_1.val[0])) * scale[0];
2268            isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_1.val[1])) * scale[1];
2269            isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_1.val[2])) * scale[2];
2270            isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_1.val[3])) * scale[3];
2271
2272            scale += 4;
2273
2274            q3h.val[0] = vbicq_u8(m2, qhbits.val[0]);
2275            q3h.val[1] = vbicq_u8(m2, qhbits.val[1]);
2276            q3h.val[2] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[0]), 1);
2277            q3h.val[3] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[1]), 1);
2278
2279            q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 4), m3b)), vreinterpretq_s8_u8(q3h.val[0]));
2280            q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 4), m3b)), vreinterpretq_s8_u8(q3h.val[1]));
2281            q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 6), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
2282            q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 6), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
2283
2284            isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_2.val[0])) * scale[0];
2285            isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_2.val[1])) * scale[1];
2286            isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_2.val[2])) * scale[2];
2287            isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_2.val[3])) * scale[3];
2288
2289            scale += 4;
2290
2291            if (j == 0) {
2292                qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 4);
2293                qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 4);
2294            }
2295
2296        }
2297        sum += d * isum;
2298
2299    }
2300
2301    *s = sum;
2302
2303#else
2304    UNUSED(kmask1);
2305    UNUSED(kmask2);
2306    UNUSED(x);
2307    UNUSED(y);
2308    UNUSED(nb);
2309    ggml_vec_dot_q3_K_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc);
2310#endif
2311
2312}
2313
2314#ifdef __ARM_FEATURE_SVE
2315static inline svuint32_t ggml_decode_q4scales_and_mins_for_mmla(const uint32_t * vx_scales) {
2316    const svbool_t pg_all   = svptrue_pat_b32(SV_VL4);
2317    const svbool_t pg_false = svpfalse_b();            // 0x0000
2318    const svbool_t pg_lo_8  = svwhilelt_b8_s32(0,  8); // 0x00ff
2319    const svbool_t pg_odd   = svzip1_b32(pg_false, pg_lo_8);
2320
2321    svuint32_t vutmp_hi, vutmp_lo;
2322    svuint32_t vx01 = svld1_u32(pg_lo_8, vx_scales);
2323    vutmp_hi = svzip1_u32(vx01, vx01);
2324    vutmp_hi = svlsr_n_u32_m(pg_odd, vutmp_hi, 2);
2325    vutmp_hi = svreinterpret_u32_u64(svand_n_u64_x(pg_all, svreinterpret_u64_u32(vutmp_hi), UINT64_C(0x303030303f3f3f3f)));
2326    const svuint32_t vx2 = svdup_u32(vx_scales[2]);
2327    vutmp_lo = svlsr_u32_x(pg_all, vx2, svreinterpret_u32_s32(svindex_s32(-2, 2)));
2328    vutmp_lo = svand_n_u32_z(pg_odd, vutmp_lo, UINT32_C(0x0f0f0f0f));
2329    svuint32_t vutmp = svorr_u32_z(pg_all, vutmp_hi, vutmp_lo);
2330    return vutmp;
2331}
2332#endif
2333
2334void ggml_vec_dot_q4_K_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
2335    assert(n % QK_K == 0);
2336#ifdef __ARM_FEATURE_MATMUL_INT8
2337    assert((nrc == 2) || (nrc == 1));
2338#else
2339    assert(nrc == 1);
2340#endif
2341    UNUSED(nrc);
2342    UNUSED(bx);
2343    UNUSED(by);
2344    UNUSED(bs);
2345
2346    const block_q4_K * GGML_RESTRICT x = vx;
2347    const block_q8_K * GGML_RESTRICT y = vy;
2348
2349    const int nb = n / QK_K;
2350
2351    static const uint32_t kmask1 = 0x3f3f3f3f;
2352    static const uint32_t kmask2 = 0x0f0f0f0f;
2353    static const uint32_t kmask3 = 0x03030303;
2354
2355    uint32_t utmp[4];
2356#ifdef __ARM_FEATURE_SVE
2357    const int vector_length = ggml_cpu_get_sve_cnt()*8;
2358#endif
2359
2360#if defined(__ARM_FEATURE_SVE) && defined(__ARM_FEATURE_MATMUL_INT8)
2361    if (nrc == 2) {
2362        svbool_t pg32_2 = svptrue_pat_b32(SV_VL2);
2363
2364        const block_q4_K * GGML_RESTRICT vx0 = vx;
2365        const block_q8_K * GGML_RESTRICT vy0 = vy;
2366        const block_q4_K * GGML_RESTRICT vx1 = (const block_q4_K *) ((const uint8_t*)vx + bx);
2367        const block_q8_K * GGML_RESTRICT vy1 = (const block_q8_K *) ((const uint8_t*)vy + by);
2368
2369        union {
2370            uint32_t u32[8];
2371            uint64_t u64[4];
2372        } new_utmp;
2373
2374        svfloat32_t sumf1 = svdup_n_f32(0);
2375
2376        switch (vector_length) {
2377            case 128:
2378                {
2379                    svbool_t pg_false = svpfalse_b();
2380                    svbool_t pg_lo_8  = svwhilelt_b8_s32(0,  8);
2381                    svbool_t vmins_mask1= svzip1_b32(pg_lo_8, pg_false);
2382                    svbool_t vmins_mask2 = svzip1_b32(pg_false, pg_lo_8);
2383                    svbool_t pg128_all  = svptrue_pat_b8(SV_VL16);
2384                    for (int i = 0; i < nb; ++i) {
2385                        svfloat32_t vy_d = svuzp1_f32(svdup_n_f32(vy0[i].d), svdup_n_f32(vy1[i].d));
2386                        svfloat32_t vx_d = svzip1_f32(svdup_n_f32(GGML_FP16_TO_FP32(vx0[i].d)), svdup_n_f32(GGML_FP16_TO_FP32(vx1[i].d)));
2387                        svfloat32_t svsuper_block_scales = svmul_f32_x(pg128_all, vy_d, vx_d);
2388                        svfloat32_t vx_dmins = svzip1_f32(svdup_n_f32(GGML_FP16_TO_FP32(vx0[i].dmin)), svdup_n_f32(GGML_FP16_TO_FP32(vx1[i].dmin)));
2389                        svfloat32_t vy_dmins = svuzp1_f32(svdup_n_f32(vy0[i].d), svdup_n_f32(vy1[i].d));
2390                        svfloat32_t svdmins = svmul_n_f32_x(pg128_all, svmul_f32_x(pg128_all, vy_dmins, vx_dmins), -1);
2391                        const uint8_t * GGML_RESTRICT q4_0 = vx0[i].qs;
2392                        const int8_t  * GGML_RESTRICT q8_0 = vy0[i].qs;
2393                        const uint8_t * GGML_RESTRICT q4_1 = vx1[i].qs;
2394                        const int8_t  * GGML_RESTRICT q8_1 = vy1[i].qs;
2395                        svint16_t lo = svld1_s16(pg128_all, vy0[i].bsums + 0);
2396                        svint16_t hi = svld1_s16(pg128_all, vy0[i].bsums + 8);
2397                        svint16_t sum_tmp1 = svuzp1_s16(lo, hi);
2398                        svint16_t sum_tmp2 = svuzp2_s16(lo, hi);
2399                        svint16_t svq8sums_0 = svadd_s16_x(pg128_all, sum_tmp1, sum_tmp2);
2400                        lo = svld1_s16(pg128_all, vy1[i].bsums + 0);
2401                        hi = svld1_s16(pg128_all, vy1[i].bsums + 8);
2402                        sum_tmp1 = svuzp1(lo, hi);
2403                        sum_tmp2 = svuzp2(lo, hi);
2404                        svint16_t svq8sums_1 = svadd_s16_x(pg128_all, sum_tmp1, sum_tmp2);
2405                        svuint32_t decoded_scales0 = ggml_decode_q4scales_and_mins_for_mmla((const uint32_t *)vx0[i].scales);
2406                        svuint32_t decoded_scales1 = ggml_decode_q4scales_and_mins_for_mmla((const uint32_t *)vx1[i].scales);
2407                        svuint32x2_t decoded_scales = svcreate2_u32(decoded_scales0, decoded_scales1);
2408                        svst2_u32(pg128_all, new_utmp.u32, decoded_scales);
2409                        svint16_t svmins8_0 = svreinterpret_s16_u16(svunpklo_u16(svreinterpret_u8_u32(svuzp1_u32(svld1_u32(vmins_mask1, new_utmp.u32+4), svdup_n_u32(0)))));
2410                        svint16_t svmins8_1 = svreinterpret_s16_u16(svunpklo_u16(svreinterpret_u8_u32(svuzp2_u32(svld1_u32(vmins_mask2, new_utmp.u32+4), svdup_n_u32(0)))));
2411                        svint32_t svsumfs_tmp1 = svreinterpret_s32_s64(svdot_s64(svdup_n_s64(0), svq8sums_0, svmins8_0));
2412                        svint32_t svsumfs_tmp2 = svreinterpret_s32_s64(svdot_s64(svdup_n_s64(0), svq8sums_0, svmins8_1));
2413                        svint32_t svsumfs_tmp3 = svtrn1_s32(svsumfs_tmp1, svsumfs_tmp2);
2414                        svint32_t svsumfs_tmp4 = svreinterpret_s32_s64(svdot_s64(svdup_n_s64(0), svq8sums_1, svmins8_0));
2415                        svint32_t svsumfs_tmp5 = svreinterpret_s32_s64(svdot_s64(svdup_n_s64(0), svq8sums_1, svmins8_1));
2416                        svint32_t svsumfs_tmp6 = svtrn1_s32(svsumfs_tmp4, svsumfs_tmp5);
2417                        svint32_t svsumfs_tmp7 = svreinterpret_s32_s64(svtrn2_s64(svreinterpret_s64_s32(svsumfs_tmp3), svreinterpret_s64_s32(svsumfs_tmp6)));
2418                        svint32_t svsumfs_tmp8 = svreinterpret_s32_s64(svtrn1_s64(svreinterpret_s64_s32(svsumfs_tmp3), svreinterpret_s64_s32(svsumfs_tmp6)));
2419                        svint32_t svsumfs_tmp = svadd_s32_x(pg128_all, svsumfs_tmp7, svsumfs_tmp8);
2420                        svint32_t svscales, sumi1, sumi2;
2421                        svint32_t acc_sumif1 = svdup_n_s32(0);
2422                        svint32_t acc_sumif2 = svdup_n_s32(0);
2423                        svint8_t q4bytes_0_l, q4bytes_0_h, q4bytes_1_l, q4bytes_1_h, l0, l1, l2, l3,
2424                                 q8bytes_0_h, q8bytes_0_l, q8bytes_1_h, q8bytes_1_l, r0, r1, r2, r3;
2425#pragma GCC unroll 1
2426                        for (int j = 0; j < QK_K/64; ++j) {
2427                            q4bytes_0_l = svreinterpret_s8_u8(svand_n_u8_x(pg128_all, svld1_u8(pg128_all, q4_0), 0xf));
2428                            q4bytes_1_l = svreinterpret_s8_u8(svand_n_u8_x(pg128_all, svld1_u8(pg128_all, q4_1), 0xf));
2429                            q4bytes_0_h = svreinterpret_s8_u8(svand_n_u8_x(pg128_all, svld1_u8(pg128_all, q4_0+16), 0xf));
2430                            q4bytes_1_h = svreinterpret_s8_u8(svand_n_u8_x(pg128_all, svld1_u8(pg128_all, q4_1+16), 0xf));
2431                            l0 = svreinterpret_s8_s64(svzip1_s64(svreinterpret_s64_s8(q4bytes_0_l), svreinterpret_s64_s8(q4bytes_1_l)));
2432                            l1 = svreinterpret_s8_s64(svzip2_s64(svreinterpret_s64_s8(q4bytes_0_l), svreinterpret_s64_s8(q4bytes_1_l)));
2433                            l2 = svreinterpret_s8_s64(svzip1_s64(svreinterpret_s64_s8(q4bytes_0_h), svreinterpret_s64_s8(q4bytes_1_h)));
2434                            l3 = svreinterpret_s8_s64(svzip2_s64(svreinterpret_s64_s8(q4bytes_0_h), svreinterpret_s64_s8(q4bytes_1_h)));
2435                            q8bytes_0_h = svld1_s8(pg128_all, q8_0);
2436                            q8bytes_1_h = svld1_s8(pg128_all, q8_1);
2437                            q8bytes_0_l = svld1_s8(pg128_all, q8_0+16);
2438                            q8bytes_1_l = svld1_s8(pg128_all, q8_1+16);
2439                            r0 = svreinterpret_s8_s64(svzip1_s64(svreinterpret_s64_s8(q8bytes_0_h), svreinterpret_s64_s8(q8bytes_1_h)));
2440                            r1 = svreinterpret_s8_s64(svzip2_s64(svreinterpret_s64_s8(q8bytes_0_h), svreinterpret_s64_s8(q8bytes_1_h)));
2441                            r2 = svreinterpret_s8_s64(svzip1_s64(svreinterpret_s64_s8(q8bytes_0_l), svreinterpret_s64_s8(q8bytes_1_l)));
2442                            r3 = svreinterpret_s8_s64(svzip2_s64(svreinterpret_s64_s8(q8bytes_0_l), svreinterpret_s64_s8(q8bytes_1_l)));
2443                            sumi1 = svmmla_s32(svmmla_s32(svmmla_s32(svmmla_s32(svdup_n_s32(0), r0, l0), r1, l1), r2, l2), r3, l3);
2444                            svscales = svreinterpret_s32_u32(svlsr_n_u32_x(pg128_all, svlsl_n_u32_x(pg128_all, svreinterpret_u32_u64(svdup_n_u64(new_utmp.u64[j/2])), 8*(4-2*(j%2)-1)), 24));
2445                            acc_sumif1 = svmla_s32_x(pg128_all, acc_sumif1, svscales, sumi1);
2446
2447                            q4bytes_0_l = svreinterpret_s8_u8(svlsr_n_u8_x(pg128_all, svld1_u8(pg128_all, q4_0), 4));
2448                            q4bytes_1_l = svreinterpret_s8_u8(svlsr_n_u8_x(pg128_all, svld1_u8(pg128_all, q4_1), 4));
2449                            q4bytes_0_h = svreinterpret_s8_u8(svlsr_n_u8_x(pg128_all, svld1_u8(pg128_all, q4_0+16), 4));
2450                            q4bytes_1_h = svreinterpret_s8_u8(svlsr_n_u8_x(pg128_all, svld1_u8(pg128_all, q4_1+16), 4));
2451                            l0 = svreinterpret_s8_s64(svzip1_s64(svreinterpret_s64_s8(q4bytes_0_l), svreinterpret_s64_s8(q4bytes_1_l)));
2452                            l1 = svreinterpret_s8_s64(svzip2_s64(svreinterpret_s64_s8(q4bytes_0_l), svreinterpret_s64_s8(q4bytes_1_l)));
2453                            l2 = svreinterpret_s8_s64(svzip1_s64(svreinterpret_s64_s8(q4bytes_0_h), svreinterpret_s64_s8(q4bytes_1_h)));
2454                            l3 = svreinterpret_s8_s64(svzip2_s64(svreinterpret_s64_s8(q4bytes_0_h), svreinterpret_s64_s8(q4bytes_1_h)));
2455                            q8bytes_0_h = svld1_s8(pg128_all, q8_0+32);
2456                            q8bytes_1_h = svld1_s8(pg128_all, q8_1+32);
2457                            q8bytes_0_l = svld1_s8(pg128_all, q8_0+48);
2458                            q8bytes_1_l = svld1_s8(pg128_all, q8_1+48);
2459                            r0 = svreinterpret_s8_s64(svzip1_s64(svreinterpret_s64_s8(q8bytes_0_h), svreinterpret_s64_s8(q8bytes_1_h)));
2460                            r1 = svreinterpret_s8_s64(svzip2_s64(svreinterpret_s64_s8(q8bytes_0_h), svreinterpret_s64_s8(q8bytes_1_h)));
2461                            r2 = svreinterpret_s8_s64(svzip1_s64(svreinterpret_s64_s8(q8bytes_0_l), svreinterpret_s64_s8(q8bytes_1_l)));
2462                            r3 = svreinterpret_s8_s64(svzip2_s64(svreinterpret_s64_s8(q8bytes_0_l), svreinterpret_s64_s8(q8bytes_1_l)));
2463                            sumi2 = svmmla_s32(svmmla_s32(svmmla_s32(svmmla_s32(svdup_n_s32(0), r0, l0), r1, l1), r2, l2), r3, l3);
2464                            svscales = svreinterpret_s32_u32(svlsr_n_u32_x(pg128_all, svlsl_n_u32_x(pg128_all, svreinterpret_u32_u64(svdup_n_u64(new_utmp.u64[j/2])), 8*(4-2*(j%2)-2)), 24));
2465                            acc_sumif2 = svmla_s32_x(pg128_all, acc_sumif2, svscales, sumi2);
2466                            q4_0 += 32; q4_1 += 32; q8_0 += 64; q8_1 += 64;
2467                        }
2468                        sumf1 = svmla_f32_x(pg128_all,
2469                                svmla_f32_x(pg128_all,
2470                                    sumf1,
2471                                    svcvt_f32_x(pg128_all,
2472                                        svadd_s32_x(pg128_all, acc_sumif1, acc_sumif2)),
2473                                    svsuper_block_scales),
2474                                svdmins,
2475                                svcvt_f32_s32_x(pg128_all, svsumfs_tmp));
2476                    }  //end of for nb
2477                } // end of case 128
2478                break;
2479            case 256:
2480            case 512:
2481                {
2482                    const svbool_t pg32_4 = svptrue_pat_b32(SV_VL4);
2483                    const svbool_t pg8_16 = svptrue_pat_b8(SV_VL16);
2484                    const svbool_t pg256_all = svptrue_pat_b8(SV_ALL);
2485                    for (int i = 0; i < nb; ++i) {
2486                        const uint8_t * GGML_RESTRICT q4_0 = vx0[i].qs;
2487                        const int8_t  * GGML_RESTRICT q8_0 = vy0[i].qs;
2488                        const uint8_t * GGML_RESTRICT q4_1 = vx1[i].qs;
2489                        const int8_t  * GGML_RESTRICT q8_1 = vy1[i].qs;
2490                        svint32_t svscales, sumi1, sumi2;
2491                        svint32_t acc_sumif1 = svdup_n_s32(0);
2492                        svint32_t acc_sumif2 = svdup_n_s32(0);
2493                        svint8_t l0, l1, l2, l3, r0, r1, r2, r3;
2494                        svfloat32_t vx_d = svzip1_f32(svdup_n_f32(GGML_FP16_TO_FP32(vx0[i].d)), svdup_n_f32(GGML_FP16_TO_FP32(vx1[i].d)));
2495                        svfloat64_t vy_d_tmp = svreinterpret_f64_f32(svuzp1_f32(svdup_n_f32(vy0[i].d), svdup_n_f32(vy1[i].d)));
2496                        svfloat32_t vy_d = svreinterpret_f32_f64(svuzp1_f64(vy_d_tmp, vy_d_tmp));
2497                        svfloat32_t svsuper_block_scales = svmul_f32_z(pg32_4, vy_d, vx_d);
2498                        svfloat32_t vx_dmins = svzip1_f32(svdup_n_f32(GGML_FP16_TO_FP32(vx0[i].dmin)), svdup_n_f32(GGML_FP16_TO_FP32(vx1[i].dmin)));
2499                        svfloat64_t vy_dmins_tmp = svreinterpret_f64_f32(svuzp1_f32(svdup_n_f32(vy0[i].d), svdup_n_f32(vy1[i].d)));
2500                        svfloat32_t vy_dmins = svreinterpret_f32_f64(svuzp1_f64(vy_dmins_tmp, vy_dmins_tmp));
2501                        svfloat32_t svdmins = svmul_n_f32_x(pg32_4, svmul_f32_x(pg32_4, vx_dmins, vy_dmins), -1);
2502                        svint16_t rc1 = svuzp1_s16(svld1_s16(pg256_all, vy0[i].bsums), svld1_s16(pg256_all, vy1[i].bsums));
2503                        svint16_t rc2 = svuzp2_s16(svld1_s16(pg256_all, vy0[i].bsums), svld1_s16(pg256_all, vy1[i].bsums));
2504                        svint16_t svq8sums = svadd_s16_x(pg256_all, rc1, rc2);
2505                        svuint32_t decoded_scales0 = ggml_decode_q4scales_and_mins_for_mmla((const uint32_t *)vx0[i].scales);
2506                        svuint32_t decoded_scales1 = ggml_decode_q4scales_and_mins_for_mmla((const uint32_t *)vx1[i].scales);
2507                        svuint32x2_t decoded_scales = svcreate2_u32(decoded_scales0, decoded_scales1);
2508                        svst2_u32(pg8_16, new_utmp.u32, decoded_scales);
2509                        svint16_t new_svq8sums_0 = svreinterpret_s16_u64(svtrn1_u64(svreinterpret_u64_s16(svq8sums), svreinterpret_u64_s16(svq8sums)));
2510                        svint16_t new_svq8sums_1 = svreinterpret_s16_u64(svtrn2_u64(svreinterpret_u64_s16(svq8sums), svreinterpret_u64_s16(svq8sums)));
2511                        svuint64_t new_mins_0 = svdup_u64(new_utmp.u64[2]);
2512                        svuint64_t new_mins_1 = svdup_u64(new_utmp.u64[3]);
2513                        svint16_t new_svmins8_0 = svreinterpret_s16_u16(svunpklo_u16(svreinterpret_u8_u64(new_mins_0)));
2514                        svint16_t new_svmins8_1 = svreinterpret_s16_u16(svunpklo_u16(svreinterpret_u8_u64(new_mins_1)));
2515                        svint64_t dot_prod_0 = svdot_s64(svdup_s64(0), new_svmins8_0, new_svq8sums_0);
2516                        svint64_t dot_prod_1 = svdot_s64(dot_prod_0, new_svmins8_1, new_svq8sums_1);
2517                        svfloat32_t converted_dot_prod_1 = svcvt_f32_s64_x(pg256_all, dot_prod_1);
2518                        svfloat32_t svsumfs_tmp = svuzp1_f32(converted_dot_prod_1, converted_dot_prod_1);
2519
2520#pragma GCC unroll 1
2521                        for (int j = 0; j < QK_K/64; ++j) {
2522                            svuint8_t q4bytes_0 = svand_n_u8_x(pg256_all, svld1_u8(pg256_all, q4_0), 0xf);
2523                            svuint8_t q4bytes_1 = svand_n_u8_x(pg256_all, svld1_u8(pg256_all, q4_1), 0xf);
2524                            svuint8_t q4bytes_2 = svlsr_n_u8_x(pg256_all, svld1_u8(pg256_all, q4_0), 4);
2525                            svuint8_t q4bytes_3 = svlsr_n_u8_x(pg256_all, svld1_u8(pg256_all, q4_1), 4);
2526                            l0 = svreinterpret_s8_u64(svzip1_u64(svreinterpret_u64_u8(q4bytes_0), svreinterpret_u64_u8(q4bytes_1)));
2527                            l1 = svreinterpret_s8_u64(svzip2_u64(svreinterpret_u64_u8(q4bytes_0), svreinterpret_u64_u8(q4bytes_1)));
2528                            l2 = svreinterpret_s8_u64(svzip1_u64(svreinterpret_u64_u8(q4bytes_2), svreinterpret_u64_u8(q4bytes_3)));
2529                            l3 = svreinterpret_s8_u64(svzip2_u64(svreinterpret_u64_u8(q4bytes_2), svreinterpret_u64_u8(q4bytes_3)));
2530                            svint8_t q8bytes_0 = svld1_s8(pg256_all, q8_0);
2531                            svint8_t q8bytes_1 = svld1_s8(pg256_all, q8_1);
2532                            svint8_t q8bytes_2 = svld1_s8(pg256_all, q8_0+32);
2533                            svint8_t q8bytes_3 = svld1_s8(pg256_all, q8_1+32);
2534                            r0 = svreinterpret_s8_s64(svzip1_s64(svreinterpret_s64_s8(q8bytes_0), svreinterpret_s64_s8(q8bytes_1)));
2535                            r1 = svreinterpret_s8_s64(svzip2_s64(svreinterpret_s64_s8(q8bytes_0), svreinterpret_s64_s8(q8bytes_1)));
2536                            r2 = svreinterpret_s8_s64(svzip1_s64(svreinterpret_s64_s8(q8bytes_2), svreinterpret_s64_s8(q8bytes_3)));
2537                            r3 = svreinterpret_s8_s64(svzip2_s64(svreinterpret_s64_s8(q8bytes_2), svreinterpret_s64_s8(q8bytes_3)));
2538                            sumi1 = svmmla(svmmla(svdup_n_s32(0), r0, l0), r1, l1);
2539                            svscales = svreinterpret_s32_u32(svlsr_n_u32_x(pg256_all, svlsl_n_u32_x(pg256_all, svreinterpret_u32_u64(svdup_n_u64(new_utmp.u64[j/2])), 8*(4-2*(j%2)-1)), 24));
2540                            acc_sumif1 = svmla_s32_x(pg256_all, acc_sumif1, svscales, sumi1);
2541                            sumi2 = svmmla(svmmla(svdup_n_s32(0), r2, l2), r3, l3);
2542                            svscales = svreinterpret_s32_u32(svlsr_n_u32_x(pg256_all, svlsl_n_u32_x(pg256_all, svreinterpret_u32_u64(svdup_n_u64(new_utmp.u64[j/2])), 8*(4-2*(j%2)-2)), 24));
2543                            acc_sumif2 = svmla_s32_x(pg256_all, acc_sumif2, svscales, sumi2);
2544                            q4_0 += 32; q4_1 += 32; q8_0 += 64; q8_1 += 64;
2545                        }
2546                        svint32_t acc_sumif = svadd_s32_x(pg256_all, acc_sumif1, acc_sumif2);
2547                        svint32_t swap_acc_sumif = svext_s32(acc_sumif, acc_sumif, 4);
2548                        acc_sumif = svadd_s32_x(pg32_4, acc_sumif, swap_acc_sumif);
2549                        sumf1 = svmla_f32_x(pg32_4,
2550                                svmla_f32_x(pg32_4,
2551                                    sumf1,
2552                                    svcvt_f32_x(pg32_4, acc_sumif),
2553                                    svsuper_block_scales),
2554                                svdmins,
2555                                svsumfs_tmp);
2556                    } // end of for nb
2557                } // end of case 256-512
2558                break;
2559            default:
2560                assert(false && "Unsupported vector length");
2561                break;
2562        }
2563
2564        svst1_f32(pg32_2, s, sumf1);
2565        svst1_f32(pg32_2, s + bs, svreinterpret_f32_u8(svext_u8(svreinterpret_u8_f32(sumf1), svdup_n_u8(0), 8)));
2566
2567        return;
2568    }
2569#elif defined(__ARM_FEATURE_MATMUL_INT8)
2570    if (nrc == 2) {
2571        const block_q4_K * GGML_RESTRICT x0 = x;
2572        const block_q4_K * GGML_RESTRICT x1 = (const block_q4_K *) ((const uint8_t *)vx + bx);
2573        const block_q8_K * GGML_RESTRICT y0 = y;
2574        const block_q8_K * GGML_RESTRICT y1 = (const block_q8_K *) ((const uint8_t *)vy + by);
2575
2576        const uint8x16_t m4b = vdupq_n_u8(0x0f);
2577
2578        float32x4_t vfsum = vdupq_n_f32(0.0f);
2579
2580        for (int i = 0; i < nb; ++i, ++x0, ++x1, ++y0, ++y1) {
2581            const uint8_t * GGML_RESTRICT qx0 = x0->qs;
2582            const uint8_t * GGML_RESTRICT qx1 = x1->qs;
2583            const  int8_t * GGML_RESTRICT qy0 = y0->qs;
2584            const  int8_t * GGML_RESTRICT qy1 = y1->qs;
2585
2586            // decode scales and mins
2587            int8_t x0_scales[8], x1_scales[8];
2588            int16x8_t x0_mins, x1_mins;
2589            {
2590                uint32_t scales_mins[3];
2591                memcpy(scales_mins, x0->scales, 12);
2592                const uint32_t mins_0_3 = scales_mins[1] & kmask1;
2593                const uint32_t mins_4_7 = ((scales_mins[2] >> 4) & kmask2) | (((scales_mins[1] >> 6) & kmask3) << 4);
2594                const uint32x2_t mins = {mins_0_3, mins_4_7};
2595                x0_mins = vreinterpretq_s16_u16(vmovl_u8(vreinterpret_u8_u32(mins)));
2596                uint32_t scales[2];
2597                scales[0] = scales_mins[0] & kmask1; // scales 0~3
2598                scales[1] = (scales_mins[2] & kmask2) | (((scales_mins[0] >> 6) & kmask3) << 4); // scales 4~7
2599                memcpy(x0_scales, scales, 8);
2600            }
2601            {
2602                uint32_t scales_mins[3];
2603                memcpy(scales_mins, x1->scales, 12);
2604                const uint32_t mins_0_3 = scales_mins[1] & kmask1;
2605                const uint32_t mins_4_7 = ((scales_mins[2] >> 4) & kmask2) | (((scales_mins[1] >> 6) & kmask3) << 4);
2606                const uint32x2_t mins = {mins_0_3, mins_4_7};
2607                x1_mins = vreinterpretq_s16_u16(vmovl_u8(vreinterpret_u8_u32(mins)));
2608                uint32_t scales[2];
2609                scales[0] = scales_mins[0] & kmask1; // scales 0~3
2610                scales[1] = (scales_mins[2] & kmask2) | (((scales_mins[0] >> 6) & kmask3) << 4); // scales 4~7
2611                memcpy(x1_scales, scales, 8);
2612            }
2613
2614            int32x4_t visum = {0};
2615
2616            // process 64 data points per iteration, totally 256 data points
2617            for (int j = 0; j < QK_K / 64; ++j, qx0 += 32, qx1 += 32, qy0 += 64, qy1 += 64) {
2618                const int8x16x4_t vy0 = vld1q_s8_x4(qy0);
2619                const int8x16x4_t vy1 = vld1q_s8_x4(qy1);
2620
2621                int8x16_t vx0[4], vx1[4];
2622                {
2623                    const uint8x16x2_t vv = vld1q_u8_x2(qx0);
2624                    vx0[0] = vreinterpretq_s8_u8(vandq_u8(vv.val[0], m4b));
2625                    vx0[1] = vreinterpretq_s8_u8(vandq_u8(vv.val[1], m4b));
2626                    vx0[2] = vreinterpretq_s8_u8(vshrq_n_u8(vv.val[0], 4));
2627                    vx0[3] = vreinterpretq_s8_u8(vshrq_n_u8(vv.val[1], 4));
2628                }
2629                {
2630                    const uint8x16x2_t vv = vld1q_u8_x2(qx1);
2631                    vx1[0] = vreinterpretq_s8_u8(vandq_u8(vv.val[0], m4b));
2632                    vx1[1] = vreinterpretq_s8_u8(vandq_u8(vv.val[1], m4b));
2633                    vx1[2] = vreinterpretq_s8_u8(vshrq_n_u8(vv.val[0], 4));
2634                    vx1[3] = vreinterpretq_s8_u8(vshrq_n_u8(vv.val[1], 4));
2635                }
2636
2637                // process 32 data points (share same block scale) per iteration
2638                for (int k = 0; k < 2; ++k) {
2639                    const int blk = j * 2 + k;
2640                    const int32x4_t block_scale = {
2641                        x0_scales[blk],
2642                        x0_scales[blk],
2643                        x1_scales[blk],
2644                        x1_scales[blk],
2645                    };
2646
2647                    int32x4_t vr = {0};
2648                    for (int l = 0; l < 2; ++l) {
2649                        const int idx = k * 2 + l;
2650                        const int64x2_t vx0_s64 = vreinterpretq_s64_s8(vx0[idx]);
2651                        const int64x2_t vx1_s64 = vreinterpretq_s64_s8(vx1[idx]);
2652                        const int64x2_t vy0_s64 = vreinterpretq_s64_s8(vy0.val[idx]);
2653                        const int64x2_t vy1_s64 = vreinterpretq_s64_s8(vy1.val[idx]);
2654                        const int8x16_t vx_l = vreinterpretq_s8_s64(vzip1q_s64(vx0_s64, vx1_s64));
2655                        const int8x16_t vx_h = vreinterpretq_s8_s64(vzip2q_s64(vx0_s64, vx1_s64));
2656                        const int8x16_t vy_l = vreinterpretq_s8_s64(vzip1q_s64(vy0_s64, vy1_s64));
2657                        const int8x16_t vy_h = vreinterpretq_s8_s64(vzip2q_s64(vy0_s64, vy1_s64));
2658                        vr = vmmlaq_s32(vr, vx_l, vy_l);
2659                        vr = vmmlaq_s32(vr, vx_h, vy_h);
2660                    }
2661                    // apply block scale, will NOT overflow
2662                    // block_scale * sum_256(int4*int8) <= 2^(8+8+4+8) = 28 bits
2663                    visum = vmlaq_s32(visum, vr, block_scale);
2664                }
2665            }
2666
2667            // adjust bias, apply superblock scale
2668            {
2669                int32_t bias[4];
2670                // no obvious uplift from sve sdot-16, just use neon mul add
2671                const int16x8_t y0_sums = vpaddq_s16(vld1q_s16(y0->bsums), vld1q_s16(y0->bsums+8));
2672                const int16x8_t y1_sums = vpaddq_s16(vld1q_s16(y1->bsums), vld1q_s16(y1->bsums+8));
2673                bias[0] = vaddvq_s32(vaddq_s32(vmull_s16(vget_low_s16(y0_sums), vget_low_s16(x0_mins)),
2674                                               vmull_s16(vget_high_s16(y0_sums), vget_high_s16(x0_mins))));
2675                bias[1] = vaddvq_s32(vaddq_s32(vmull_s16(vget_low_s16(y1_sums), vget_low_s16(x0_mins)),
2676                                               vmull_s16(vget_high_s16(y1_sums), vget_high_s16(x0_mins))));
2677                bias[2] = vaddvq_s32(vaddq_s32(vmull_s16(vget_low_s16(y0_sums), vget_low_s16(x1_mins)),
2678                                               vmull_s16(vget_high_s16(y0_sums), vget_high_s16(x1_mins))));
2679                bias[3] = vaddvq_s32(vaddq_s32(vmull_s16(vget_low_s16(y1_sums), vget_low_s16(x1_mins)),
2680                                               vmull_s16(vget_high_s16(y1_sums), vget_high_s16(x1_mins))));
2681                const float32x4_t dmins = {
2682                    GGML_CPU_FP16_TO_FP32(x0->dmin) * y0->d,
2683                    GGML_CPU_FP16_TO_FP32(x0->dmin) * y1->d,
2684                    GGML_CPU_FP16_TO_FP32(x1->dmin) * y0->d,
2685                    GGML_CPU_FP16_TO_FP32(x1->dmin) * y1->d,
2686                };
2687                vfsum = vmlsq_f32(vfsum, vcvtq_f32_s32(vld1q_s32(bias)), dmins);
2688
2689                const float32x4_t superblock_scale = {
2690                    GGML_CPU_FP16_TO_FP32(x0->d) * y0->d,
2691                    GGML_CPU_FP16_TO_FP32(x0->d) * y1->d,
2692                    GGML_CPU_FP16_TO_FP32(x1->d) * y0->d,
2693                    GGML_CPU_FP16_TO_FP32(x1->d) * y1->d,
2694                };
2695                vfsum = vmlaq_f32(vfsum, vcvtq_f32_s32(visum), superblock_scale);
2696            }
2697        }
2698
2699        // vfsum = ABCD -> ACBD
2700        // AC -> s, BD -> (s+bs)
2701        vfsum = vzip1q_f32(vfsum, vextq_f32(vfsum, vfsum, 2));
2702        vst1_f32(s,      vget_low_f32 (vfsum));
2703        vst1_f32(s + bs, vget_high_f32(vfsum));
2704
2705        return;
2706    }
2707#endif
2708
2709#ifdef __ARM_FEATURE_SVE
2710    float sumf = 0;
2711    for (int i = 0; i < nb; ++i) {
2712
2713        const float d = y[i].d * GGML_CPU_FP16_TO_FP32(x[i].d);
2714        const float dmin = y[i].d * GGML_CPU_FP16_TO_FP32(x[i].dmin);
2715
2716        const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
2717
2718        memcpy(utmp, x[i].scales, K_SCALE_SIZE);
2719
2720        uint32x2_t mins8 = { 0 };
2721        mins8 = vset_lane_u32(utmp[1] & kmask1, mins8, 0);
2722        mins8 = vset_lane_u32(((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4), mins8, 1);
2723
2724        utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
2725        utmp[0] &= kmask1;
2726
2727        const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(vreinterpret_u8_u32(mins8)));
2728        const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
2729                                         vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
2730        sumf -= dmin * vaddvq_s32(prod);
2731
2732        const uint8_t * scales = (const uint8_t *)utmp;
2733
2734        const uint8_t * GGML_RESTRICT q4 = x[i].qs;
2735        const int8_t  * GGML_RESTRICT q8 = y[i].qs;
2736
2737        const svuint8_t m4b = svdup_n_u8(0xf);
2738        const svint32_t mzero = svdup_n_s32(0);
2739        svint32_t sumi1 = svdup_n_s32(0);
2740        svint32_t sumi1_1 = svdup_n_s32(0);
2741        svint32_t sumi1_2 = svdup_n_s32(0);
2742        svint32_t sumi2 = svdup_n_s32(0);
2743        svint32_t sumi2_1 = svdup_n_s32(0);
2744        svint32_t sumi2_2 = svdup_n_s32(0);
2745        switch (vector_length) {
2746            case 128:
2747                {
2748                    for (int j = 0; j < QK_K/64; ++j) {
2749                        svint8_t q4bytes = svreinterpret_s8_u8(svand_u8_x(svptrue_b8(), svld1_u8(svptrue_b8(), q4), m4b));
2750                        svint8_t q8bytes = svld1_s8(svptrue_b8(), q8); q8 += 16;
2751                        sumi1_1 = svmla_n_s32_x(svptrue_b32(), sumi1_1, svdot_s32(mzero, q4bytes, q8bytes), scales[2*j+0]);
2752                        q4bytes = svreinterpret_s8_u8(svand_u8_x(svptrue_b8(), svld1_u8(svptrue_b8(), q4+16), m4b));
2753                        q8bytes = svld1_s8(svptrue_b8(), q8); q8 += 16;
2754                        sumi1_2 = svmla_n_s32_x(svptrue_b32(), sumi1_2, svdot_s32(mzero, q4bytes, q8bytes), scales[2*j+0]);
2755
2756                        q4bytes = svreinterpret_s8_u8(svlsr_n_u8_x(svptrue_b8(), svld1_u8(svptrue_b8(), q4), 4));
2757                        q8bytes = svld1_s8(svptrue_b8(), q8); q8 += 16;
2758                        sumi2_1 = svmla_n_s32_x(svptrue_b32(), sumi2_1, svdot_s32(mzero, q4bytes, q8bytes), scales[2*j+1]);
2759                        q4bytes = svreinterpret_s8_u8(svlsr_n_u8_x(svptrue_b8(), svld1_u8(svptrue_b8(), q4+16), 4));
2760                        q8bytes = svld1_s8(svptrue_b8(), q8); q8 += 16;
2761                        sumi2_2 = svmla_n_s32_x(svptrue_b32(), sumi2_2, svdot_s32(mzero, q4bytes, q8bytes), scales[2*j+1]);
2762                        q4 += 32;
2763                    }
2764                    sumi1 = svadd_s32_x(svptrue_b32(), sumi1_1, sumi1_2);
2765                    sumi2 = svadd_s32_x(svptrue_b32(), sumi2_1, sumi2_2);
2766                    sumf += d * (svaddv_s32(svptrue_b32(), svadd_s32_x(svptrue_b32(), sumi1, sumi2)));
2767                } break;
2768            case 256:
2769            case 512:
2770                {
2771                    for (int j = 0; j < QK_K/64; ++j) {
2772                        const svuint8_t q4bits  = svld1_u8(svptrue_pat_b8(SV_VL32), q4); q4 += 32;
2773                        svint8_t q4bytes = svreinterpret_s8_u8(svand_u8_x(svptrue_pat_b8(SV_VL32), q4bits, m4b));
2774                        svint8_t q8bytes = svld1_s8(svptrue_pat_b8(SV_VL32), q8); q8 += 32;
2775                        sumi1 = svmla_n_s32_x(svptrue_pat_b32(SV_VL8), sumi1, svdot_s32(mzero, q4bytes, q8bytes), scales[2*j+0]);
2776
2777                        q4bytes = svreinterpret_s8_u8(svlsr_n_u8_x(svptrue_pat_b8(SV_VL32), q4bits, 4));
2778                        q8bytes = svld1_s8(svptrue_pat_b8(SV_VL32), q8); q8 += 32;
2779                        sumi2 = svmla_n_s32_x(svptrue_pat_b32(SV_VL8), sumi2, svdot_s32(mzero, q4bytes, q8bytes), scales[2*j+1]);
2780                    }
2781                    sumf += d * (svaddv_s32(svptrue_pat_b32(SV_VL8), svadd_s32_x(svptrue_pat_b32(SV_VL8), sumi1, sumi2)));
2782                } break;
2783            default:
2784                assert(false && "Unsupported vector length");
2785                break;
2786        }
2787    }
2788    *s = sumf;
2789#elif defined __ARM_NEON
2790    const uint8x16_t m4b = vdupq_n_u8(0xf);
2791    const int32x4_t mzero = vdupq_n_s32(0);
2792
2793    ggml_int8x16x2_t q4bytes;
2794    ggml_int8x16x2_t q8bytes;
2795
2796    float sumf = 0;
2797
2798    for (int i = 0; i < nb; ++i) {
2799
2800        const float d = y[i].d * GGML_CPU_FP16_TO_FP32(x[i].d);
2801        const float dmin = y[i].d * GGML_CPU_FP16_TO_FP32(x[i].dmin);
2802
2803        const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
2804
2805        memcpy(utmp, x[i].scales, 12);
2806
2807        uint32x2_t mins8 = { 0 };
2808        mins8 = vset_lane_u32(utmp[1] & kmask1, mins8, 0);
2809        mins8 = vset_lane_u32(((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4), mins8, 1);
2810
2811        utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
2812        utmp[0] &= kmask1;
2813
2814        const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(vreinterpret_u8_u32(mins8)));
2815        const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
2816                                         vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
2817        sumf -= dmin * vaddvq_s32(prod);
2818
2819        const uint8_t * scales = (const uint8_t *)utmp;
2820
2821        const uint8_t * GGML_RESTRICT q4 = x[i].qs;
2822        const int8_t  * GGML_RESTRICT q8 = y[i].qs;
2823
2824        int32_t sumi1 = 0;
2825        int32_t sumi2 = 0;
2826
2827        for (int j = 0; j < QK_K/64; ++j) {
2828            const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
2829
2830            q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
2831            q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8  (q4bits.val[0], m4b));
2832            q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8  (q4bits.val[1], m4b));
2833
2834            const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
2835            sumi1 += vaddvq_s32(p1) * scales[2*j+0];
2836
2837            q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
2838            q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
2839            q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
2840
2841            const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
2842
2843            sumi2 += vaddvq_s32(p2) * scales[2*j+1];
2844        }
2845
2846        sumf += d * (sumi1 + sumi2);
2847
2848    }
2849
2850    *s = sumf;
2851
2852#else
2853    UNUSED(x);
2854    UNUSED(y);
2855    UNUSED(nb);
2856    UNUSED(kmask1);
2857    UNUSED(kmask2);
2858    UNUSED(kmask3);
2859    UNUSED(utmp);
2860    ggml_vec_dot_q4_K_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc);
2861#endif
2862}
2863
2864void ggml_vec_dot_q5_K_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy,  size_t by, int nrc) {
2865    assert(n % QK_K == 0);
2866    assert(nrc == 1);
2867    UNUSED(nrc);
2868    UNUSED(bx);
2869    UNUSED(by);
2870    UNUSED(bs);
2871
2872    const block_q5_K * GGML_RESTRICT x = vx;
2873    const block_q8_K * GGML_RESTRICT y = vy;
2874
2875    const int nb = n / QK_K;
2876
2877    static const uint32_t kmask1 = 0x3f3f3f3f;
2878    static const uint32_t kmask2 = 0x0f0f0f0f;
2879    static const uint32_t kmask3 = 0x03030303;
2880
2881    uint32_t utmp[4];
2882
2883
2884#ifdef __ARM_NEON
2885    const uint8x16_t m4b = vdupq_n_u8(0xf);
2886    const uint8x16_t mone = vdupq_n_u8(1);
2887    const uint8x16_t mtwo = vdupq_n_u8(2);
2888    const int32x4_t mzero = vdupq_n_s32(0);
2889
2890    ggml_int8x16x4_t q5bytes;
2891
2892    float sumf = 0;
2893
2894    for (int i = 0; i < nb; ++i) {
2895
2896        const float d = y[i].d * GGML_CPU_FP16_TO_FP32(x[i].d);
2897        const float dmin = y[i].d * GGML_CPU_FP16_TO_FP32(x[i].dmin);
2898
2899        const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
2900
2901        memcpy(utmp, x[i].scales, 12);
2902        utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
2903        const uint32_t uaux = utmp[1] & kmask1;
2904        utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
2905        utmp[2] = uaux;
2906        utmp[0] &= kmask1;
2907
2908        const uint8x8_t mins8 = vld1_u8((const uint8_t*)utmp + 8);
2909        const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(mins8));
2910        const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
2911                                         vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
2912        int32_t sumi_mins = vaddvq_s32(prod);
2913
2914        const uint8_t * scales = (const uint8_t *)utmp;
2915
2916        const uint8_t * GGML_RESTRICT q5 = x[i].qs;
2917        const uint8_t * GGML_RESTRICT qh = x[i].qh;
2918        const int8_t  * GGML_RESTRICT q8 = y[i].qs;
2919
2920        ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
2921
2922        ggml_uint8x16x4_t q5h;
2923
2924        int32_t sumi = 0;
2925
2926        for (int j = 0; j < QK_K/64; ++j) {
2927
2928            const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5); q5 += 32;
2929            const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
2930
2931            q5h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
2932            q5h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
2933            q5h.val[2] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[0]), 3);
2934            q5h.val[3] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[1]), 3);
2935            qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 2);
2936            qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 2);
2937
2938            q5bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[0], m4b), q5h.val[0]));
2939            q5bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[1], m4b), q5h.val[1]));
2940            q5bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[0], 4), q5h.val[2]));
2941            q5bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[1], 4), q5h.val[3]));
2942
2943            sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]), q5bytes.val[1], q8bytes.val[1])) * *scales++;
2944            sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]), q5bytes.val[3], q8bytes.val[3])) * *scales++;
2945        }
2946
2947        sumf += d * sumi - dmin * sumi_mins;
2948    }
2949
2950    *s = sumf;
2951
2952#else
2953    UNUSED(x);
2954    UNUSED(y);
2955    UNUSED(nb);
2956    UNUSED(kmask1);
2957    UNUSED(kmask2);
2958    UNUSED(kmask3);
2959    UNUSED(utmp);
2960    ggml_vec_dot_q5_K_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc);
2961#endif
2962}
2963
2964void ggml_vec_dot_q6_K_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
2965    assert(n % QK_K == 0);
2966#ifdef __ARM_FEATURE_MATMUL_INT8
2967    assert((nrc == 2) || (nrc == 1));
2968#else
2969    assert(nrc == 1);
2970#endif
2971    UNUSED(nrc);
2972    UNUSED(bx);
2973    UNUSED(by);
2974    UNUSED(bs);
2975
2976    const block_q6_K * GGML_RESTRICT x = vx;
2977    const block_q8_K * GGML_RESTRICT y = vy;
2978
2979    const int nb = n / QK_K;
2980
2981#ifdef __ARM_FEATURE_SVE
2982    const int vector_length = ggml_cpu_get_sve_cnt()*8;
2983#endif
2984#if defined(__ARM_FEATURE_SVE) && defined(__ARM_FEATURE_MATMUL_INT8)
2985    if (nrc == 2) {
2986        const svbool_t pg32_2 = svptrue_pat_b32(SV_VL2);
2987
2988        svfloat32_t sum = svdup_n_f32(0);
2989
2990        const block_q6_K * GGML_RESTRICT vx0 = vx;
2991        const block_q8_K * GGML_RESTRICT vy0 = vy;
2992        const block_q6_K * GGML_RESTRICT vx1 = (const block_q6_K *) ((const uint8_t*)vx + bx);
2993        const block_q8_K * GGML_RESTRICT vy1 = (const block_q8_K *) ((const uint8_t*)vy + by);
2994
2995        switch (vector_length) {
2996            case 128:
2997                {
2998                    const svbool_t pg128_all = svptrue_pat_b8(SV_ALL);
2999                    for (int i = 0; i < nb; ++i) {
3000                        const uint8_t * GGML_RESTRICT ql0 = vx0[i].ql;
3001                        const uint8_t * GGML_RESTRICT qh0 = vx0[i].qh;
3002                        const uint8_t * GGML_RESTRICT ql1 = vx1[i].ql;
3003                        const uint8_t * GGML_RESTRICT qh1 = vx1[i].qh;
3004                        const int8_t  * GGML_RESTRICT q80 = vy0[i].qs;
3005                        const int8_t  * GGML_RESTRICT q81 = vy1[i].qs;
3006
3007                        const int8_t * GGML_RESTRICT scale0 = vx0[i].scales;
3008                        const int8_t * GGML_RESTRICT scale1 = vx1[i].scales;
3009
3010                        svfloat32_t vy_d = svuzp1_f32(svdup_n_f32(vy0[i].d), svdup_n_f32(vy1[i].d));
3011                        svfloat32_t vx_d = svzip1_f32(svdup_n_f32(GGML_FP16_TO_FP32(vx0[i].d)), svdup_n_f32(GGML_FP16_TO_FP32(vx1[i].d)));
3012                        svfloat32_t svsuper_block_scales = svmul_f32_x(pg128_all, vy_d, vx_d);
3013                        // process q8sum summation 128 bit route
3014                        const svint16_t q8sums_01 = svld1_s16(pg128_all, vy0[i].bsums);
3015                        const svint16_t q8sums_02 = svld1_s16(pg128_all, vy0[i].bsums + 8);
3016                        const svint16_t q8sums_11 = svld1_s16(pg128_all, vy1[i].bsums);
3017                        const svint16_t q8sums_12 = svld1_s16(pg128_all, vy1[i].bsums + 8);
3018                        const svint64x2_t q6scales_0_tmp = svld2_s64(pg128_all, (const int64_t *)scale0);
3019                        const svint16_t q6scales_01 = svunpklo_s16(svreinterpret_s8_s64(svget2_s64(q6scales_0_tmp, 0)));
3020                        const svint16_t q6scales_02 = svunpklo_s16(svreinterpret_s8_s64(svget2_s64(q6scales_0_tmp, 1)));
3021                        const svint64x2_t q6scales_1_tmp = svld2_s64(pg128_all, (const int64_t *)scale1);
3022                        const svint16_t q6scales_11 = svunpklo_s16(svreinterpret_s8_s64(svget2_s64(q6scales_1_tmp, 0)));
3023                        const svint16_t q6scales_12 = svunpklo_s16(svreinterpret_s8_s64(svget2_s64(q6scales_1_tmp, 1)));
3024                        const svint64_t prod = svdup_n_s64(0);
3025
3026                        svint32_t isum_tmp1 = svreinterpret_s32_s64(svdot_s64(svdot_s64(prod, q8sums_01, q6scales_01), q8sums_02, q6scales_02));
3027                        svint32_t isum_tmp2 = svreinterpret_s32_s64(svdot_s64(svdot_s64(prod, q8sums_01, q6scales_11), q8sums_02, q6scales_12));
3028                        svint32_t isum_tmp3 = svtrn1_s32(isum_tmp1, isum_tmp2);
3029                        svint32_t isum_tmp4 = svreinterpret_s32_s64(svdot_s64(svdot_s64(prod, q8sums_11, q6scales_01), q8sums_12, q6scales_02));
3030                        svint32_t isum_tmp5 = svreinterpret_s32_s64(svdot_s64(svdot_s64(prod, q8sums_11, q6scales_11), q8sums_12, q6scales_12));
3031                        svint32_t isum_tmp6 = svtrn1_s32(isum_tmp4, isum_tmp5);
3032                        svint32_t isum_tmp7 = svreinterpret_s32_s64(svtrn2_s64(svreinterpret_s64_s32(isum_tmp3), svreinterpret_s64_s32(isum_tmp6)));
3033                        svint32_t isum_tmp8 = svreinterpret_s32_s64(svtrn1_s64(svreinterpret_s64_s32(isum_tmp3), svreinterpret_s64_s32(isum_tmp6)));
3034                        svint32_t svisum_mins = svadd_s32_x(pg128_all, isum_tmp7, isum_tmp8);
3035
3036                        // process mmla
3037                        svint8_t  l0, l1, r0, r1;
3038                        svint32_t isum_tmp = svdup_n_s32(0);
3039                        for (int j = 0; j < QK_K/128; ++j) {
3040                            for (int k = 0; k < 8; ++k) {
3041                                svuint8_t qhbits_0 = svld1_u8(pg128_all, qh0+16*(k%2));
3042                                svuint8_t qhbits_1 = svld1_u8(pg128_all, qh1+16*(k%2));
3043                                svuint8_t q6bits_0 = svld1_u8(pg128_all, ql0+16*(k%4));
3044                                svuint8_t q6bits_1 = svld1_u8(pg128_all, ql1+16*(k%4));
3045                                const int ql_pos = (k/4)*4;
3046                                svuint8_t q6bytes_0_lo = (ql_pos < 4) ? svand_n_u8_x(pg128_all, q6bits_0, 0xf) : svlsr_n_u8_x(pg128_all, q6bits_0, 4);
3047                                svuint8_t q6bytes_1_lo = (ql_pos < 4) ? svand_n_u8_x(pg128_all, q6bits_1, 0xf) : svlsr_n_u8_x(pg128_all, q6bits_1, 4);
3048                                const int qh_pos = (k/2)*2;
3049                                svuint8_t q6bytes_0_hi = svand_n_u8_x(pg128_all, qhbits_0, 0x3 << qh_pos);
3050                                svuint8_t q6bytes_1_hi = svand_n_u8_x(pg128_all, qhbits_1, 0x3 << qh_pos);
3051                                svint8_t  q6bytes_0, q6bytes_1;
3052                                if (qh_pos <= 4) {
3053                                    q6bytes_0 = svreinterpret_s8_u8(svmla_n_u8_x(pg128_all, q6bytes_0_lo, q6bytes_0_hi, 1 << (4 - qh_pos)));
3054                                    q6bytes_1 = svreinterpret_s8_u8(svmla_n_u8_x(pg128_all, q6bytes_1_lo, q6bytes_1_hi, 1 << (4 - qh_pos)));
3055                                } else {
3056                                    q6bytes_0 = svreinterpret_s8_u8(svorr_u8_x(pg128_all, q6bytes_0_lo, svlsr_n_u8_x(pg128_all, q6bytes_0_hi, (qh_pos - 4))));
3057                                    q6bytes_1 = svreinterpret_s8_u8(svorr_u8_x(pg128_all, q6bytes_1_lo, svlsr_n_u8_x(pg128_all, q6bytes_1_hi, (qh_pos - 4))));
3058                                }
3059                                svint8_t  q8bytes_0 = svld1_s8(pg128_all, q80+16*(k%8));
3060                                svint8_t  q8bytes_1 = svld1_s8(pg128_all, q81+16*(k%8));
3061                                l0 = svreinterpret_s8_s64(svzip1_s64(svreinterpret_s64_s8(q6bytes_0), svreinterpret_s64_s8(q6bytes_1)));
3062                                l1 = svreinterpret_s8_s64(svzip2_s64(svreinterpret_s64_s8(q6bytes_0), svreinterpret_s64_s8(q6bytes_1)));
3063                                r0 = svreinterpret_s8_s64(svzip1_s64(svreinterpret_s64_s8(q8bytes_0), svreinterpret_s64_s8(q8bytes_1)));
3064                                r1 = svreinterpret_s8_s64(svzip2_s64(svreinterpret_s64_s8(q8bytes_0), svreinterpret_s64_s8(q8bytes_1)));
3065                                svint32_t svscale = svzip1_s32(svdup_n_s32(scale0[k]), svdup_n_s32(scale1[k]));
3066                                isum_tmp = svmla_s32_x(pg128_all, isum_tmp, svmmla_s32(svmmla_s32(svdup_n_s32(0), r0, l0), r1, l1), svscale);
3067                            }
3068                            qh0 += 32;  qh1 += 32;
3069                            ql0 += 64;  ql1 += 64;
3070                            q80 += 128; q81 += 128;
3071                            scale0 += 8; scale1 += 8;
3072                        }
3073                        sum = svmla_f32_x(pg128_all, sum,
3074                                svcvt_f32_x(pg128_all, svmla_s32_x(pg128_all, isum_tmp,
3075                                        svisum_mins, svdup_n_s32(-32))),
3076                                svsuper_block_scales);
3077                    }
3078                } // end of case 128
3079                break;
3080            case 256:
3081            case 512:
3082                {
3083                    const svbool_t pg256_all = svptrue_pat_b8(SV_ALL);
3084                    const svbool_t pg32_4 = svptrue_pat_b32(SV_VL4);
3085                    for (int i = 0; i < nb; ++i) {
3086                        const uint8_t * GGML_RESTRICT ql0 = vx0[i].ql;
3087                        const uint8_t * GGML_RESTRICT qh0 = vx0[i].qh;
3088                        const uint8_t * GGML_RESTRICT ql1 = vx1[i].ql;
3089                        const uint8_t * GGML_RESTRICT qh1 = vx1[i].qh;
3090                        const int8_t  * GGML_RESTRICT q80 = vy0[i].qs;
3091                        const int8_t  * GGML_RESTRICT q81 = vy1[i].qs;
3092
3093                        const int8_t * GGML_RESTRICT scale0 = vx0[i].scales;
3094                        const int8_t * GGML_RESTRICT scale1 = vx1[i].scales;
3095                        svfloat32_t vx_d = svzip1_f32(svdup_n_f32(GGML_FP16_TO_FP32(vx0[i].d)), svdup_n_f32(GGML_FP16_TO_FP32(vx1[i].d)));
3096                        svfloat64_t vy_d_tmp = svreinterpret_f64_f32(svuzp1_f32(svdup_n_f32(vy0[i].d), svdup_n_f32(vy1[i].d)));
3097                        svfloat32_t vy_d = svreinterpret_f32_f64(svuzp1_f64(vy_d_tmp, vy_d_tmp));
3098                        svfloat32_t svsuper_block_scales = svmul_f32_x(pg32_4, vy_d, vx_d);
3099                        // process q8sum summation 256 bit route
3100                        const svint16_t q8sums_0 = svld1_s16(pg256_all, vy0[i].bsums);
3101                        const svint16_t q8sums_1 = svld1_s16(pg256_all, vy1[i].bsums);
3102                        const svint16_t q6scales_0 = svunpklo_s16(svld1_s8(pg256_all, scale0));
3103                        const svint16_t q6scales_1 = svunpklo_s16(svld1_s8(pg256_all, scale1));
3104                        const svint64_t prod = svdup_n_s64(0);
3105                        svint32_t isum_tmp1  = svreinterpret_s32_s64(svdot_s64(prod, q8sums_0, q6scales_0));
3106                        svint32_t isum_tmp2  = svreinterpret_s32_s64(svdot_s64(prod, q8sums_0, q6scales_1));
3107                        svint32_t isum_tmp3  = svreinterpret_s32_s64(svdot_s64(prod, q8sums_1, q6scales_0));
3108                        svint32_t isum_tmp4  = svreinterpret_s32_s64(svdot_s64(prod, q8sums_1, q6scales_1));
3109                        svint32_t isum_tmp5  = svtrn1_s32(isum_tmp1, isum_tmp2);
3110                        svint32_t isum_tmp6  = svtrn1_s32(isum_tmp3, isum_tmp4);
3111                        svint32_t isum_tmp7  = svreinterpret_s32_s64(svtrn2_s64(svreinterpret_s64_s32(isum_tmp5), svreinterpret_s64_s32(isum_tmp6)));
3112                        svint32_t isum_tmp8  = svreinterpret_s32_s64(svtrn1_s64(svreinterpret_s64_s32(isum_tmp5), svreinterpret_s64_s32(isum_tmp6)));
3113                        svint32_t isum_tmp9  = svadd_s32_x(pg256_all, isum_tmp7, isum_tmp8);
3114                        svint32_t isum_tmp10 = svreinterpret_s32_u8(svext_u8(svreinterpret_u8_s32(isum_tmp9), svreinterpret_u8_s32(isum_tmp9), 16));
3115                        svint32_t svisum_mins = svadd_s32_z(pg32_4, isum_tmp9, isum_tmp10);
3116
3117                        // process mmla
3118                        svint8_t l0, l1, r0, r1;
3119                        svint32_t isum_tmp = svdup_n_s32(0);
3120                        for (int j = 0; j < QK_K/128; ++j) {
3121                            for (int k = 0; k < 8; k+=2) { // process 2 block
3122                                svuint8_t qhbits_0  = svld1_u8(pg256_all, qh0);
3123                                svuint8_t qhbits_1  = svld1_u8(pg256_all, qh1);
3124                                svuint8_t q6bits_0  = svld1_u8(pg256_all, ql0+32*((k%4)/2));
3125                                svuint8_t q6bits_1  = svld1_u8(pg256_all, ql1+32*((k%4)/2));
3126                                const int ql_pos = (k/4)*4;
3127                                svuint8_t q6bytes_0_lo = (ql_pos < 4) ? svand_n_u8_x(pg256_all, q6bits_0, 0xf) : svlsr_n_u8_x(pg256_all, q6bits_0, 4);
3128                                svuint8_t q6bytes_1_lo = (ql_pos < 4) ? svand_n_u8_x(pg256_all, q6bits_1, 0xf) : svlsr_n_u8_x(pg256_all, q6bits_1, 4);
3129                                const int qh_pos = (k/2)*2;
3130                                svuint8_t q6bytes_0_hi = svand_n_u8_x(pg256_all, qhbits_0, 0x3 << qh_pos);
3131                                svuint8_t q6bytes_1_hi = svand_n_u8_x(pg256_all, qhbits_1, 0x3 << qh_pos);
3132                                svint8_t  q6bytes_0, q6bytes_1;
3133                                if (qh_pos <= 4) {
3134                                    q6bytes_0 = svreinterpret_s8_u8(svmla_n_u8_x(pg256_all, q6bytes_0_lo, q6bytes_0_hi, 1 << (4 - qh_pos)));
3135                                    q6bytes_1 = svreinterpret_s8_u8(svmla_n_u8_x(pg256_all, q6bytes_1_lo, q6bytes_1_hi, 1 << (4 - qh_pos)));
3136                                } else {
3137                                    q6bytes_0 = svreinterpret_s8_u8(svorr_u8_x(pg256_all, q6bytes_0_lo, svlsr_n_u8_x(pg256_all, q6bytes_0_hi, (qh_pos - 4))));
3138                                    q6bytes_1 = svreinterpret_s8_u8(svorr_u8_x(pg256_all, q6bytes_1_lo, svlsr_n_u8_x(pg256_all, q6bytes_1_hi, (qh_pos - 4))));
3139                                }
3140                                svint8_t  q8bytes_0 = svld1_s8(pg256_all, q80+32*(k/2));
3141                                svint8_t  q8bytes_1 = svld1_s8(pg256_all, q81+32*(k/2));
3142                                l0 = svreinterpret_s8_s64(svzip1_s64(svreinterpret_s64_s8(q6bytes_0), svreinterpret_s64_s8(q6bytes_1)));
3143                                l1 = svreinterpret_s8_s64(svzip2_s64(svreinterpret_s64_s8(q6bytes_0), svreinterpret_s64_s8(q6bytes_1)));
3144                                r0 = svreinterpret_s8_s64(svzip1_s64(svreinterpret_s64_s8(q8bytes_0), svreinterpret_s64_s8(q8bytes_1)));
3145                                r1 = svreinterpret_s8_s64(svzip2_s64(svreinterpret_s64_s8(q8bytes_0), svreinterpret_s64_s8(q8bytes_1)));
3146                                svint32_t svscale0 = svzip1_s32(svdup_n_s32(scale0[k]), svdup_n_s32(scale1[k]));
3147                                svint32_t svscale1 = svzip1_s32(svdup_n_s32(scale0[k+1]), svdup_n_s32(scale1[k+1]));
3148                                isum_tmp = svmla_s32_x(pg256_all, isum_tmp, svmmla_s32(svdup_n_s32(0), r0, l0), svscale0);
3149                                isum_tmp = svmla_s32_x(pg256_all, isum_tmp, svmmla_s32(svdup_n_s32(0), r1, l1), svscale1);
3150                            }
3151                            qh0 += 32;  qh1 += 32;
3152                            ql0 += 64;  ql1 += 64;
3153                            q80 += 128; q81 += 128;
3154                            scale0 += 8; scale1 += 8;
3155                        } // end of for
3156                        svint32_t swap_isum_tmp = svext_s32(isum_tmp, isum_tmp, 4);
3157                        isum_tmp = svadd_s32_x(pg32_4, isum_tmp, swap_isum_tmp);
3158                        sum = svmla_f32_x(pg32_4, sum,
3159                                svcvt_f32_x(pg32_4, svmla_s32_x(pg32_4, isum_tmp,
3160                                        svisum_mins, svdup_n_s32(-32))),
3161                                svsuper_block_scales);
3162                    }
3163                } // end of case 256
3164                break;
3165            default:
3166                assert(false && "Unsupported vector length");
3167                break;
3168        } // end of switch
3169
3170        svst1_f32(pg32_2, s, sum);
3171        svst1_f32(pg32_2, s + bs, svreinterpret_f32_u8(svext_u8(svreinterpret_u8_f32(sum), svdup_n_u8(0), 8)));
3172
3173        return;
3174    }
3175#elif defined(__ARM_FEATURE_MATMUL_INT8)
3176    if (nrc == 2) {
3177        const block_q6_K * GGML_RESTRICT x0 = x;
3178        const block_q6_K * GGML_RESTRICT x1 = (const block_q6_K *) ((const uint8_t *)vx + bx);
3179        const block_q8_K * GGML_RESTRICT y0 = y;
3180        const block_q8_K * GGML_RESTRICT y1 = (const block_q8_K *) ((const uint8_t *)vy + by);
3181
3182        float32x4_t vfsum = vdupq_n_f32(0.0f);
3183
3184        for (int i = 0; i < nb; ++i, ++x0, ++x1, ++y0, ++y1) {
3185            const uint8_t * GGML_RESTRICT ql0 = x0->ql;
3186            const uint8_t * GGML_RESTRICT ql1 = x1->ql;
3187            const uint8_t * GGML_RESTRICT qh0 = x0->qh;
3188            const uint8_t * GGML_RESTRICT qh1 = x1->qh;
3189            const  int8_t * GGML_RESTRICT qy0 = y0->qs;
3190            const  int8_t * GGML_RESTRICT qy1 = y1->qs;
3191
3192            const uint8x16_t mone = vdupq_n_u8(0x30);
3193            const uint8x16_t  m4b = vdupq_n_u8(0x0f);
3194
3195            int32x4_t visum = vdupq_n_s32(0);
3196
3197            // process 8 blocks per iteration, totally 16 blocks
3198            for (int j = 0; j < 2; ++j, qh0 += 32, ql0 += 64, qh1 += 32, ql1 += 64) {
3199                int8x16_t vx0[8], vx1[8];
3200
3201                // de-quantize vx0[8]
3202                {
3203                    const uint8x16x2_t qh_bits = vld1q_u8_x2(qh0);
3204                    const uint8x16x4_t ql_bits = vld1q_u8_x4(ql0);
3205
3206                    uint8x16_t q6h_0 = vandq_u8(mone, vshlq_n_u8(qh_bits.val[0], 4));
3207                    uint8x16_t q6h_1 = vandq_u8(mone, vshlq_n_u8(qh_bits.val[1], 4));
3208                    uint8x16_t q6h_2 = vandq_u8(mone, vshlq_n_u8(qh_bits.val[0], 2));
3209                    uint8x16_t q6h_3 = vandq_u8(mone, vshlq_n_u8(qh_bits.val[1], 2));
3210
3211                    vx0[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(ql_bits.val[0], m4b), q6h_0));
3212                    vx0[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(ql_bits.val[1], m4b), q6h_1));
3213                    vx0[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(ql_bits.val[2], m4b), q6h_2));
3214                    vx0[3] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(ql_bits.val[3], m4b), q6h_3));
3215
3216                    q6h_0 = vandq_u8(mone, qh_bits.val[0]);
3217                    q6h_1 = vandq_u8(mone, qh_bits.val[1]);
3218                    q6h_2 = vandq_u8(mone, vshrq_n_u8(qh_bits.val[0], 2));
3219                    q6h_3 = vandq_u8(mone, vshrq_n_u8(qh_bits.val[1], 2));
3220
3221                    vx0[4] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(ql_bits.val[0], 4), q6h_0));
3222                    vx0[5] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(ql_bits.val[1], 4), q6h_1));
3223                    vx0[6] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(ql_bits.val[2], 4), q6h_2));
3224                    vx0[7] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(ql_bits.val[3], 4), q6h_3));
3225                }
3226
3227                // de-quantize vx1[8]
3228                {
3229                    const uint8x16x2_t qh_bits = vld1q_u8_x2(qh1);
3230                    const uint8x16x4_t ql_bits = vld1q_u8_x4(ql1);
3231
3232                    uint8x16_t q6h_0 = vandq_u8(mone, vshlq_n_u8(qh_bits.val[0], 4));
3233                    uint8x16_t q6h_1 = vandq_u8(mone, vshlq_n_u8(qh_bits.val[1], 4));
3234                    uint8x16_t q6h_2 = vandq_u8(mone, vshlq_n_u8(qh_bits.val[0], 2));
3235                    uint8x16_t q6h_3 = vandq_u8(mone, vshlq_n_u8(qh_bits.val[1], 2));
3236
3237                    vx1[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(ql_bits.val[0], m4b), q6h_0));
3238                    vx1[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(ql_bits.val[1], m4b), q6h_1));
3239                    vx1[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(ql_bits.val[2], m4b), q6h_2));
3240                    vx1[3] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(ql_bits.val[3], m4b), q6h_3));
3241
3242                    q6h_0 = vandq_u8(mone, qh_bits.val[0]);
3243                    q6h_1 = vandq_u8(mone, qh_bits.val[1]);
3244                    q6h_2 = vandq_u8(mone, vshrq_n_u8(qh_bits.val[0], 2));
3245                    q6h_3 = vandq_u8(mone, vshrq_n_u8(qh_bits.val[1], 2));
3246
3247                    vx1[4] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(ql_bits.val[0], 4), q6h_0));
3248                    vx1[5] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(ql_bits.val[1], 4), q6h_1));
3249                    vx1[6] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(ql_bits.val[2], 4), q6h_2));
3250                    vx1[7] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(ql_bits.val[3], 4), q6h_3));
3251                }
3252
3253                // process 16 elements (one block with same scale) per iteration
3254                // - vx = concat(ql, qh) - 32
3255                // - r1,r2,r3,r4 = smmla(vx, vy)
3256                for (int k = 0; k < 8; ++k) {
3257                    const int blk = j * 8 + k;
3258
3259                    const int8x16_t vy0 = vld1q_s8(qy0);
3260                    const int8x16_t vy1 = vld1q_s8(qy1);
3261                    qy0 += 16;
3262                    qy1 += 16;
3263
3264                    const int32x4_t block_scale = {
3265                        x0->scales[blk],
3266                        x0->scales[blk],
3267                        x1->scales[blk],
3268                        x1->scales[blk],
3269                    };
3270
3271                    // calculate four results at once with outer product
3272                    const int8x16_t vx_l = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(vx0[k]), vreinterpretq_s64_s8(vx1[k])));
3273                    const int8x16_t vx_h = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(vx0[k]), vreinterpretq_s64_s8(vx1[k])));
3274                    const int8x16_t vy_l = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(vy0), vreinterpretq_s64_s8(vy1)));
3275                    const int8x16_t vy_h = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(vy0), vreinterpretq_s64_s8(vy1)));
3276                    int32x4_t vr = vdupq_n_s32(0);
3277                    vr = vmmlaq_s32(vr, vx_l, vy_l);
3278                    vr = vmmlaq_s32(vr, vx_h, vy_h);
3279
3280                    // apply block scale, will NOT overflow
3281                    // block_scale * sum_256(int6*int8) <= 2^(8+8+6+8) = 30 bits
3282                    visum = vmlaq_s32(visum, vr, block_scale);
3283                }
3284            }
3285
3286            // adjust bias, apply superblock scale
3287            {
3288                int32_t bias[4];
3289                // NEON doesn't support int16 dot product, fallback to separated mul and add
3290                const int16x8x2_t q8sums0 = vld1q_s16_x2(y0->bsums);
3291                const int16x8x2_t q8sums1 = vld1q_s16_x2(y1->bsums);
3292
3293                int8x16_t scales_s8 = vld1q_s8(x0->scales);
3294                const int16x8x2_t q6scales0 = {{vmovl_s8(vget_low_s8(scales_s8)), vmovl_s8(vget_high_s8(scales_s8))}};
3295                scales_s8 = vld1q_s8(x1->scales);
3296                const int16x8x2_t q6scales1 = {{vmovl_s8(vget_low_s8(scales_s8)), vmovl_s8(vget_high_s8(scales_s8))}};
3297
3298                int32x4_t prod;
3299                prod = vaddq_s32(vaddq_s32(vmull_s16(vget_low_s16 (q8sums0.val[0]), vget_low_s16 (q6scales0.val[0])),
3300                                           vmull_s16(vget_high_s16(q8sums0.val[0]), vget_high_s16(q6scales0.val[0]))),
3301                                 vaddq_s32(vmull_s16(vget_low_s16 (q8sums0.val[1]), vget_low_s16 (q6scales0.val[1])),
3302                                           vmull_s16(vget_high_s16(q8sums0.val[1]), vget_high_s16(q6scales0.val[1]))));
3303                bias[0] = vaddvq_s32(prod);
3304                prod = vaddq_s32(vaddq_s32(vmull_s16(vget_low_s16 (q8sums1.val[0]), vget_low_s16 (q6scales0.val[0])),
3305                                           vmull_s16(vget_high_s16(q8sums1.val[0]), vget_high_s16(q6scales0.val[0]))),
3306                                 vaddq_s32(vmull_s16(vget_low_s16 (q8sums1.val[1]), vget_low_s16 (q6scales0.val[1])),
3307                                           vmull_s16(vget_high_s16(q8sums1.val[1]), vget_high_s16(q6scales0.val[1]))));
3308                bias[1] = vaddvq_s32(prod);
3309                prod = vaddq_s32(vaddq_s32(vmull_s16(vget_low_s16 (q8sums0.val[0]), vget_low_s16 (q6scales1.val[0])),
3310                                           vmull_s16(vget_high_s16(q8sums0.val[0]), vget_high_s16(q6scales1.val[0]))),
3311                                 vaddq_s32(vmull_s16(vget_low_s16 (q8sums0.val[1]), vget_low_s16 (q6scales1.val[1])),
3312                                           vmull_s16(vget_high_s16(q8sums0.val[1]), vget_high_s16(q6scales1.val[1]))));
3313                bias[2] = vaddvq_s32(prod);
3314                prod = vaddq_s32(vaddq_s32(vmull_s16(vget_low_s16 (q8sums1.val[0]), vget_low_s16 (q6scales1.val[0])),
3315                                           vmull_s16(vget_high_s16(q8sums1.val[0]), vget_high_s16(q6scales1.val[0]))),
3316                                 vaddq_s32(vmull_s16(vget_low_s16 (q8sums1.val[1]), vget_low_s16 (q6scales1.val[1])),
3317                                           vmull_s16(vget_high_s16(q8sums1.val[1]), vget_high_s16(q6scales1.val[1]))));
3318                bias[3] = vaddvq_s32(prod);
3319
3320                const int32x4_t vibias = vmulq_n_s32(vld1q_s32(bias), 32);
3321
3322                const float32x4_t superblock_scale = {
3323                    GGML_CPU_FP16_TO_FP32(x0->d) * y0->d,
3324                    GGML_CPU_FP16_TO_FP32(x0->d) * y1->d,
3325                    GGML_CPU_FP16_TO_FP32(x1->d) * y0->d,
3326                    GGML_CPU_FP16_TO_FP32(x1->d) * y1->d,
3327                };
3328
3329                visum = vsubq_s32(visum, vibias);
3330                vfsum = vmlaq_f32(vfsum, vcvtq_f32_s32(visum), superblock_scale);
3331            }
3332        }
3333
3334        // vfsum = ABCD -> ACBD
3335        // AC -> s, BD -> (s+bs)
3336        vfsum = vzip1q_f32(vfsum, vextq_f32(vfsum, vfsum, 2));
3337        vst1_f32(s,      vget_low_f32 (vfsum));
3338        vst1_f32(s + bs, vget_high_f32(vfsum));
3339
3340        return;
3341    }
3342#endif
3343
3344#ifdef __ARM_FEATURE_SVE
3345    float sum = 0;
3346    svuint8_t m4b = svdup_n_u8(0xf);
3347    svint32_t vzero = svdup_n_s32(0);
3348    svuint8_t mone = svdup_n_u8(0x30);
3349    svint8_t q6bytes_1, q6bytes_2, q6bytes_3, q6bytes_4;
3350    svuint8_t q6h_1, q6h_2, q6h_3, q6h_4;
3351
3352    for (int i = 0; i < nb; ++i) {
3353        const float d_all = GGML_CPU_FP16_TO_FP32(x[i].d);
3354
3355        const uint8_t * GGML_RESTRICT q6 = x[i].ql;
3356        const uint8_t * GGML_RESTRICT qh = x[i].qh;
3357        const int8_t  * GGML_RESTRICT q8 = y[i].qs;
3358
3359        const int8_t * GGML_RESTRICT scale = x[i].scales;
3360
3361        const svbool_t pg16_8 = svptrue_pat_b16(SV_VL8);
3362        const svint16_t q8sums_1 = svld1_s16(pg16_8, y[i].bsums);
3363        const svint16_t q8sums_2 = svld1_s16(pg16_8, y[i].bsums + 8);
3364        const svint16_t q6scales_1 = svunpklo_s16(svld1_s8(svptrue_pat_b8(SV_VL8), scale));
3365        const svint16_t q6scales_2 = svunpklo_s16(svld1_s8(svptrue_pat_b8(SV_VL8), scale + 8));
3366        const svint64_t prod = svdup_n_s64(0);
3367        int32_t isum_mins = svaddv_s64(svptrue_b64(), svadd_s64_x(svptrue_b64(), svdot_s64(prod, q8sums_1, q6scales_1),
3368                                                                                 svdot_s64(prod, q8sums_2, q6scales_2)));
3369        int32_t isum = 0;
3370
3371        switch (vector_length) {
3372            case 128:
3373                {
3374                    const svbool_t pg32_4 = svptrue_pat_b32(SV_VL4);
3375                    const svbool_t pg8_16 = svptrue_pat_b8(SV_VL16);
3376                    svint32_t isum_tmp = svdup_n_s32(0);
3377                    for (int j = 0; j < QK_K/128; ++j) {
3378                        svuint8_t qhbits_1 = svld1_u8(pg8_16, qh);
3379                        svuint8_t qhbits_2 = svld1_u8(pg8_16, qh+16);
3380                        qh += 32;
3381                        svuint8_t q6bits_1 = svld1_u8(pg8_16, q6);
3382                        svuint8_t q6bits_2 = svld1_u8(pg8_16, q6+16);
3383                        svuint8_t q6bits_3 = svld1_u8(pg8_16, q6+32);
3384                        svuint8_t q6bits_4 = svld1_u8(pg8_16, q6+48);
3385                        q6 += 64;
3386                        svint8_t q8bytes_1 = svld1_s8(pg8_16, q8);
3387                        svint8_t q8bytes_2 = svld1_s8(pg8_16, q8+16);
3388                        svint8_t q8bytes_3 = svld1_s8(pg8_16, q8+32);
3389                        svint8_t q8bytes_4 = svld1_s8(pg8_16, q8+48);
3390                        q8 += 64;
3391
3392                        q6h_1 = svand_u8_x(pg16_8, mone, svlsl_n_u8_x(pg16_8, qhbits_1, 4));
3393                        q6h_2 = svand_u8_x(pg16_8, mone, svlsl_n_u8_x(pg16_8, qhbits_2, 4));
3394                        q6h_3 = svand_u8_x(pg16_8, mone, svlsl_n_u8_x(pg16_8, qhbits_1, 2));
3395                        q6h_4 = svand_u8_x(pg16_8, mone, svlsl_n_u8_x(pg16_8, qhbits_2, 2));
3396                        q6bytes_1 = svreinterpret_s8_u8(svorr_u8_x(pg8_16, svand_u8_x(pg8_16, q6bits_1, m4b), q6h_1));
3397                        q6bytes_2 = svreinterpret_s8_u8(svorr_u8_x(pg8_16, svand_u8_x(pg8_16, q6bits_2, m4b), q6h_2));
3398                        q6bytes_3 = svreinterpret_s8_u8(svorr_u8_x(pg8_16, svand_u8_x(pg8_16, q6bits_3, m4b), q6h_3));
3399                        q6bytes_4 = svreinterpret_s8_u8(svorr_u8_x(pg8_16, svand_u8_x(pg8_16, q6bits_4, m4b), q6h_4));
3400                        isum_tmp = svmla_n_s32_x(pg32_4, isum_tmp, svdot_s32(vzero, q6bytes_1, q8bytes_1), scale[0]);
3401                        isum_tmp = svmla_n_s32_x(pg32_4, isum_tmp, svdot_s32(vzero, q6bytes_2, q8bytes_2), scale[1]);
3402                        isum_tmp = svmla_n_s32_x(pg32_4, isum_tmp, svdot_s32(vzero, q6bytes_3, q8bytes_3), scale[2]);
3403                        isum_tmp = svmla_n_s32_x(pg32_4, isum_tmp, svdot_s32(vzero, q6bytes_4, q8bytes_4), scale[3]);
3404
3405                        scale += 4;
3406                        q8bytes_1 = svld1_s8(pg8_16, q8);
3407                        q8bytes_2 = svld1_s8(pg8_16, q8+16);
3408                        q8bytes_3 = svld1_s8(pg8_16, q8+32);
3409                        q8bytes_4 = svld1_s8(pg8_16, q8+48);
3410                        q8 += 64;
3411
3412                        q6h_1 = svand_u8_x(pg16_8, mone, qhbits_1);
3413                        q6h_2 = svand_u8_x(pg16_8, mone, qhbits_2);
3414                        q6h_3 = svand_u8_x(pg16_8, mone, svlsr_n_u8_x(pg16_8, qhbits_1, 2));
3415                        q6h_4 = svand_u8_x(pg16_8, mone, svlsr_n_u8_x(pg16_8, qhbits_2, 2));
3416                        q6bytes_1 = svreinterpret_s8_u8(svorr_u8_x(pg8_16, svlsr_n_u8_x(pg8_16, q6bits_1, 4), q6h_1));
3417                        q6bytes_2 = svreinterpret_s8_u8(svorr_u8_x(pg8_16, svlsr_n_u8_x(pg8_16, q6bits_2, 4), q6h_2));
3418                        q6bytes_3 = svreinterpret_s8_u8(svorr_u8_x(pg8_16, svlsr_n_u8_x(pg8_16, q6bits_3, 4), q6h_3));
3419                        q6bytes_4 = svreinterpret_s8_u8(svorr_u8_x(pg8_16, svlsr_n_u8_x(pg8_16, q6bits_4, 4), q6h_4));
3420                        isum_tmp = svmla_n_s32_x(pg32_4, isum_tmp, svdot_s32(vzero, q6bytes_1, q8bytes_1), scale[0]);
3421                        isum_tmp = svmla_n_s32_x(pg32_4, isum_tmp, svdot_s32(vzero, q6bytes_2, q8bytes_2), scale[1]);
3422                        isum_tmp = svmla_n_s32_x(pg32_4, isum_tmp, svdot_s32(vzero, q6bytes_3, q8bytes_3), scale[2]);
3423                        isum_tmp = svmla_n_s32_x(pg32_4, isum_tmp, svdot_s32(vzero, q6bytes_4, q8bytes_4), scale[3]);
3424                        scale += 4;
3425                    }
3426                    isum += svaddv_s32(pg32_4, isum_tmp);
3427                    sum += d_all * y[i].d * (isum - 32 * isum_mins);
3428                }
3429                break;
3430            case 256:
3431            case 512:
3432                {
3433                    const svbool_t pg8_2 = svptrue_pat_b8(SV_VL2);
3434                    const svbool_t pg32_8 = svptrue_pat_b32(SV_VL8);
3435                    const svbool_t pg8_32 = svptrue_pat_b8(SV_VL32);
3436                    svint32_t isum_tmp = svdup_n_s32(0);
3437                    for (int j = 0; j < QK_K/128; j++) {
3438                        svuint8_t qhbits_1 = svld1_u8(pg8_32, qh);
3439                        qh += 32;
3440                        svuint8_t q6bits_1 = svld1_u8(pg8_32, q6);
3441                        svuint8_t q6bits_2 = svld1_u8(pg8_32, q6+32);
3442                        q6 += 64;
3443                        svint8_t q8bytes_1 = svld1_s8(pg8_32, q8);
3444                        svint8_t q8bytes_2 = svld1_s8(pg8_32, q8+32);
3445                        svint8_t q8bytes_3 = svld1_s8(pg8_32, q8+64);
3446                        svint8_t q8bytes_4 = svld1_s8(pg8_32, q8+96);
3447                        q8 += 128;
3448                        q6h_1 = svand_u8_x(pg8_32, mone, svlsl_n_u8_x(pg8_32, qhbits_1, 4));
3449                        q6h_2 = svand_u8_x(pg8_32, mone, svlsl_n_u8_x(pg8_32, qhbits_1, 2));
3450                        q6h_3 = svand_u8_x(pg8_32, mone, qhbits_1);
3451                        q6h_4 = svand_u8_x(pg8_32, mone, svlsr_n_u8_x(pg8_32, qhbits_1, 2));
3452                        q6bytes_1 = svreinterpret_s8_u8(svorr_u8_x(pg8_32, svand_u8_x(pg8_32, q6bits_1, m4b), q6h_1));
3453                        q6bytes_2 = svreinterpret_s8_u8(svorr_u8_x(pg8_32, svand_u8_x(pg8_32, q6bits_2, m4b), q6h_2));
3454                        q6bytes_3 = svreinterpret_s8_u8(svorr_u8_x(pg8_32, svlsr_n_u8_x(pg8_32, q6bits_1, 4), q6h_3));
3455                        q6bytes_4 = svreinterpret_s8_u8(svorr_u8_x(pg8_32, svlsr_n_u8_x(pg8_32, q6bits_2, 4), q6h_4));
3456
3457                        svint8_t scale_lane_1_tmp = svld1_s8(pg8_2, scale);
3458                        scale_lane_1_tmp= svzip1_s8(scale_lane_1_tmp, scale_lane_1_tmp);
3459                        scale_lane_1_tmp= svzip1_s8(scale_lane_1_tmp, scale_lane_1_tmp);
3460                        svint8_t scale_lane_2_tmp = svld1_s8(pg8_2, scale+2);
3461                        scale_lane_2_tmp = svzip1_s8(scale_lane_2_tmp, scale_lane_2_tmp);
3462                        scale_lane_2_tmp = svzip1_s8(scale_lane_2_tmp, scale_lane_2_tmp);
3463                        svint8_t scale_lane_3_tmp = svld1_s8(pg8_2, scale+4);
3464                        scale_lane_3_tmp = svzip1_s8(scale_lane_3_tmp, scale_lane_3_tmp);
3465                        scale_lane_3_tmp = svzip1_s8(scale_lane_3_tmp, scale_lane_3_tmp);
3466                        svint8_t scale_lane_4_tmp = svld1_s8(pg8_2, scale+6);
3467                        scale_lane_4_tmp = svzip1_s8(scale_lane_4_tmp, scale_lane_4_tmp);
3468                        scale_lane_4_tmp = svzip1_s8(scale_lane_4_tmp, scale_lane_4_tmp);
3469                        svint32_t scale_lane_1 = svunpklo_s32(svunpklo_s16(scale_lane_1_tmp));
3470                        svint32_t scale_lane_2 = svunpklo_s32(svunpklo_s16(scale_lane_2_tmp));
3471                        svint32_t scale_lane_3 = svunpklo_s32(svunpklo_s16(scale_lane_3_tmp));
3472                        svint32_t scale_lane_4 = svunpklo_s32(svunpklo_s16(scale_lane_4_tmp));
3473
3474                        isum_tmp = svmla_s32_x(pg32_8, isum_tmp, svdot_s32(vzero, q6bytes_1, q8bytes_1), scale_lane_1);
3475                        isum_tmp = svmla_s32_x(pg32_8, isum_tmp, svdot_s32(vzero, q6bytes_2, q8bytes_2), scale_lane_2);
3476                        isum_tmp = svmla_s32_x(pg32_8, isum_tmp, svdot_s32(vzero, q6bytes_3, q8bytes_3), scale_lane_3);
3477                        isum_tmp = svmla_s32_x(pg32_8, isum_tmp, svdot_s32(vzero, q6bytes_4, q8bytes_4), scale_lane_4);
3478                        scale += 8;
3479                    }
3480                    isum += svaddv_s32(pg32_8, isum_tmp);
3481                    sum += d_all * y[i].d * (isum - 32 * isum_mins);
3482                }
3483                break;
3484            default:
3485                assert(false && "Unsupported vector length");
3486                break;
3487        }
3488    }
3489
3490    *s = sum;
3491
3492#elif __ARM_NEON
3493    float sum = 0;
3494
3495    const uint8x16_t m4b = vdupq_n_u8(0xF);
3496    const int32x4_t  vzero = vdupq_n_s32(0);
3497    //const int8x16_t  m32s = vdupq_n_s8(32);
3498
3499    const uint8x16_t mone = vdupq_n_u8(3);
3500
3501    ggml_int8x16x4_t q6bytes;
3502    ggml_uint8x16x4_t q6h;
3503
3504    for (int i = 0; i < nb; ++i) {
3505
3506        const float d_all = GGML_CPU_FP16_TO_FP32(x[i].d);
3507
3508        const uint8_t * GGML_RESTRICT q6 = x[i].ql;
3509        const uint8_t * GGML_RESTRICT qh = x[i].qh;
3510        const int8_t  * GGML_RESTRICT q8 = y[i].qs;
3511
3512        const int8_t * GGML_RESTRICT scale = x[i].scales;
3513
3514        const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
3515        const int8x16_t scales = vld1q_s8(scale);
3516        const ggml_int16x8x2_t q6scales = {{vmovl_s8(vget_low_s8(scales)), vmovl_s8(vget_high_s8(scales))}};
3517
3518        const int32x4_t prod = vaddq_s32(vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[0]), vget_low_s16 (q6scales.val[0])),
3519                                                   vmull_s16(vget_high_s16(q8sums.val[0]), vget_high_s16(q6scales.val[0]))),
3520                                         vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[1]), vget_low_s16 (q6scales.val[1])),
3521                                                   vmull_s16(vget_high_s16(q8sums.val[1]), vget_high_s16(q6scales.val[1]))));
3522        int32_t isum_mins = vaddvq_s32(prod);
3523
3524        int32_t isum = 0;
3525
3526        for (int j = 0; j < QK_K/128; ++j) {
3527
3528            ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh); qh += 32;
3529            ggml_uint8x16x4_t q6bits = ggml_vld1q_u8_x4(q6); q6 += 64;
3530            ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
3531
3532            q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
3533            q6h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
3534            uint8x16_t shifted = vshrq_n_u8(qhbits.val[0], 2);
3535            q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
3536            shifted = vshrq_n_u8(qhbits.val[1], 2);
3537            q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
3538
3539            //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
3540            //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
3541            //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2])), m32s);
3542            //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3])), m32s);
3543            q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0]));
3544            q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1]));
3545            q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2]));
3546            q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3]));
3547
3548            isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
3549                    vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
3550                    vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
3551                    vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
3552
3553            scale += 4;
3554
3555            q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
3556
3557            shifted = vshrq_n_u8(qhbits.val[0], 4);
3558            q6h.val[0] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
3559            shifted = vshrq_n_u8(qhbits.val[1], 4);
3560            q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
3561            shifted = vshrq_n_u8(qhbits.val[0], 6);
3562            q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
3563            shifted = vshrq_n_u8(qhbits.val[1], 6);
3564            q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
3565
3566            //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0])), m32s);
3567            //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1])), m32s);
3568            //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2])), m32s);
3569            //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3])), m32s);
3570            q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0]));
3571            q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1]));
3572            q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2]));
3573            q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3]));
3574
3575            isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
3576                    vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
3577                    vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
3578                    vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
3579            scale += 4;
3580        }
3581        //sum += isum * d_all * y[i].d;
3582        sum += d_all * y[i].d * (isum - 32 * isum_mins);
3583
3584    }
3585    *s = sum;
3586#else
3587    UNUSED(x);
3588    UNUSED(y);
3589    UNUSED(nb);
3590    ggml_vec_dot_q6_K_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc);
3591#endif
3592}
3593
3594#if defined (__ARM_NEON)
3595static const int8_t keven_signs_q2xs[1024] = {
3596     1,  1,  1,  1,  1,  1,  1,  1, -1,  1,  1,  1,  1,  1,  1, -1,  1, -1,  1,  1,  1,  1,  1, -1, -1, -1,  1,  1,  1,  1,  1,  1,
3597     1,  1, -1,  1,  1,  1,  1, -1, -1,  1, -1,  1,  1,  1,  1,  1,  1, -1, -1,  1,  1,  1,  1,  1, -1, -1, -1,  1,  1,  1,  1, -1,
3598     1,  1,  1, -1,  1,  1,  1, -1, -1,  1,  1, -1,  1,  1,  1,  1,  1, -1,  1, -1,  1,  1,  1,  1, -1, -1,  1, -1,  1,  1,  1, -1,
3599     1,  1, -1, -1,  1,  1,  1,  1, -1,  1, -1, -1,  1,  1,  1, -1,  1, -1, -1, -1,  1,  1,  1, -1, -1, -1, -1, -1,  1,  1,  1,  1,
3600     1,  1,  1,  1, -1,  1,  1, -1, -1,  1,  1,  1, -1,  1,  1,  1,  1, -1,  1,  1, -1,  1,  1,  1, -1, -1,  1,  1, -1,  1,  1, -1,
3601     1,  1, -1,  1, -1,  1,  1,  1, -1,  1, -1,  1, -1,  1,  1, -1,  1, -1, -1,  1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1,  1,  1,
3602     1,  1,  1, -1, -1,  1,  1,  1, -1,  1,  1, -1, -1,  1,  1, -1,  1, -1,  1, -1, -1,  1,  1, -1, -1, -1,  1, -1, -1,  1,  1,  1,
3603     1,  1, -1, -1, -1,  1,  1, -1, -1,  1, -1, -1, -1,  1,  1,  1,  1, -1, -1, -1, -1,  1,  1,  1, -1, -1, -1, -1, -1,  1,  1, -1,
3604     1,  1,  1,  1,  1, -1,  1, -1, -1,  1,  1,  1,  1, -1,  1,  1,  1, -1,  1,  1,  1, -1,  1,  1, -1, -1,  1,  1,  1, -1,  1, -1,
3605     1,  1, -1,  1,  1, -1,  1,  1, -1,  1, -1,  1,  1, -1,  1, -1,  1, -1, -1,  1,  1, -1,  1, -1, -1, -1, -1,  1,  1, -1,  1,  1,
3606     1,  1,  1, -1,  1, -1,  1,  1, -1,  1,  1, -1,  1, -1,  1, -1,  1, -1,  1, -1,  1, -1,  1, -1, -1, -1,  1, -1,  1, -1,  1,  1,
3607     1,  1, -1, -1,  1, -1,  1, -1, -1,  1, -1, -1,  1, -1,  1,  1,  1, -1, -1, -1,  1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1, -1,
3608     1,  1,  1,  1, -1, -1,  1,  1, -1,  1,  1,  1, -1, -1,  1, -1,  1, -1,  1,  1, -1, -1,  1, -1, -1, -1,  1,  1, -1, -1,  1,  1,
3609     1,  1, -1,  1, -1, -1,  1, -1, -1,  1, -1,  1, -1, -1,  1,  1,  1, -1, -1,  1, -1, -1,  1,  1, -1, -1, -1,  1, -1, -1,  1, -1,
3610     1,  1,  1, -1, -1, -1,  1, -1, -1,  1,  1, -1, -1, -1,  1,  1,  1, -1,  1, -1, -1, -1,  1,  1, -1, -1,  1, -1, -1, -1,  1, -1,
3611     1,  1, -1, -1, -1, -1,  1,  1, -1,  1, -1, -1, -1, -1,  1, -1,  1, -1, -1, -1, -1, -1,  1, -1, -1, -1, -1, -1, -1, -1,  1,  1,
3612     1,  1,  1,  1,  1,  1, -1, -1, -1,  1,  1,  1,  1,  1, -1,  1,  1, -1,  1,  1,  1,  1, -1,  1, -1, -1,  1,  1,  1,  1, -1, -1,
3613     1,  1, -1,  1,  1,  1, -1,  1, -1,  1, -1,  1,  1,  1, -1, -1,  1, -1, -1,  1,  1,  1, -1, -1, -1, -1, -1,  1,  1,  1, -1,  1,
3614     1,  1,  1, -1,  1,  1, -1,  1, -1,  1,  1, -1,  1,  1, -1, -1,  1, -1,  1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1,  1, -1,  1,
3615     1,  1, -1, -1,  1,  1, -1, -1, -1,  1, -1, -1,  1,  1, -1,  1,  1, -1, -1, -1,  1,  1, -1,  1, -1, -1, -1, -1,  1,  1, -1, -1,
3616     1,  1,  1,  1, -1,  1, -1,  1, -1,  1,  1,  1, -1,  1, -1, -1,  1, -1,  1,  1, -1,  1, -1, -1, -1, -1,  1,  1, -1,  1, -1,  1,
3617     1,  1, -1,  1, -1,  1, -1, -1, -1,  1, -1,  1, -1,  1, -1,  1,  1, -1, -1,  1, -1,  1, -1,  1, -1, -1, -1,  1, -1,  1, -1, -1,
3618     1,  1,  1, -1, -1,  1, -1, -1, -1,  1,  1, -1, -1,  1, -1,  1,  1, -1,  1, -1, -1,  1, -1,  1, -1, -1,  1, -1, -1,  1, -1, -1,
3619     1,  1, -1, -1, -1,  1, -1,  1, -1,  1, -1, -1, -1,  1, -1, -1,  1, -1, -1, -1, -1,  1, -1, -1, -1, -1, -1, -1, -1,  1, -1,  1,
3620     1,  1,  1,  1,  1, -1, -1,  1, -1,  1,  1,  1,  1, -1, -1, -1,  1, -1,  1,  1,  1, -1, -1, -1, -1, -1,  1,  1,  1, -1, -1,  1,
3621     1,  1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1,  1, -1, -1,  1,  1, -1, -1,  1,  1, -1, -1,  1, -1, -1, -1,  1,  1, -1, -1, -1,
3622     1,  1,  1, -1,  1, -1, -1, -1, -1,  1,  1, -1,  1, -1, -1,  1,  1, -1,  1, -1,  1, -1, -1,  1, -1, -1,  1, -1,  1, -1, -1, -1,
3623     1,  1, -1, -1,  1, -1, -1,  1, -1,  1, -1, -1,  1, -1, -1, -1,  1, -1, -1, -1,  1, -1, -1, -1, -1, -1, -1, -1,  1, -1, -1,  1,
3624     1,  1,  1,  1, -1, -1, -1, -1, -1,  1,  1,  1, -1, -1, -1,  1,  1, -1,  1,  1, -1, -1, -1,  1, -1, -1,  1,  1, -1, -1, -1, -1,
3625     1,  1, -1,  1, -1, -1, -1,  1, -1,  1, -1,  1, -1, -1, -1, -1,  1, -1, -1,  1, -1, -1, -1, -1, -1, -1, -1,  1, -1, -1, -1,  1,
3626     1,  1,  1, -1, -1, -1, -1,  1, -1,  1,  1, -1, -1, -1, -1, -1,  1, -1,  1, -1, -1, -1, -1, -1, -1, -1,  1, -1, -1, -1, -1,  1,
3627     1,  1, -1, -1, -1, -1, -1, -1, -1,  1, -1, -1, -1, -1, -1,  1,  1, -1, -1, -1, -1, -1, -1,  1, -1, -1, -1, -1, -1, -1, -1, -1,
3628};
3629#endif
3630
3631void ggml_vec_dot_iq2_xxs_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
3632    assert(n % QK_K == 0);
3633    assert(nrc == 1);
3634    UNUSED(nrc);
3635    UNUSED(bx);
3636    UNUSED(by);
3637    UNUSED(bs);
3638
3639    const block_iq2_xxs * GGML_RESTRICT x = vx;
3640    const block_q8_K    * GGML_RESTRICT y = vy;
3641
3642    const int nb = n / QK_K;
3643
3644#if defined(__ARM_NEON)
3645
3646    const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
3647
3648    uint32_t aux32[4];
3649    const uint8_t * aux8 = (const uint8_t *)aux32;
3650
3651    ggml_int8x16x4_t q2u;
3652    ggml_int8x16x4_t q2s;
3653    ggml_int8x16x4_t q8b;
3654
3655    float sumf = 0;
3656    for (int i = 0; i < nb; ++i) {
3657        const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d;
3658        const uint16_t * GGML_RESTRICT q2 = x[i].qs;
3659        const int8_t   * GGML_RESTRICT q8 = y[i].qs;
3660        float sumf1 = 0, sumf2 = 0;
3661        for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
3662            q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
3663            memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
3664            q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 0])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 1])));
3665            q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 2])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 3])));
3666            q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 8])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 9])));
3667            q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[10])), vld1_s8((const void *)(iq2xxs_grid + aux8[11])));
3668            q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >>  0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >>  7) & 127))));
3669            q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
3670            q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >>  0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >>  7) & 127))));
3671            q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 21) & 127))));
3672            q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
3673            q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
3674            q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
3675            q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
3676            const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[0], q8b.val[0]), q2u.val[1], q8b.val[1]);
3677            const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[2], q8b.val[2]), q2u.val[3], q8b.val[3]);
3678            sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[1] >> 28));
3679            sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[3] >> 28));
3680        }
3681        sumf += d*(sumf1 + sumf2);
3682    }
3683    *s = 0.25f * sumf;
3684
3685#else
3686    UNUSED(x);
3687    UNUSED(y);
3688    UNUSED(nb);
3689    ggml_vec_dot_iq2_xxs_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc);
3690#endif
3691}
3692
3693void ggml_vec_dot_iq2_xs_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
3694    assert(n % QK_K == 0);
3695    assert(nrc == 1);
3696    UNUSED(nrc);
3697    UNUSED(bx);
3698    UNUSED(by);
3699    UNUSED(bs);
3700
3701    const block_iq2_xs * GGML_RESTRICT x = vx;
3702    const block_q8_K   * GGML_RESTRICT y = vy;
3703
3704    const int nb = n / QK_K;
3705
3706#if defined(__ARM_NEON)
3707
3708    const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
3709
3710    ggml_int8x16x4_t q2u;
3711    ggml_int8x16x4_t q2s;
3712    ggml_int8x16x4_t q8b;
3713
3714    int32x4x4_t scales32;
3715
3716    float sumf = 0;
3717    for (int i = 0; i < nb; ++i) {
3718        const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d;
3719        const uint16_t * GGML_RESTRICT q2 = x[i].qs;
3720        const int8_t   * GGML_RESTRICT q8 = y[i].qs;
3721        const uint8x8_t scales8 = vld1_u8(x[i].scales);
3722        const uint8x8_t scales_l = vand_u8(scales8, vdup_n_u8(0xf));
3723        const uint8x8_t scales_h = vshr_n_u8(scales8, 4);
3724        uint8x16_t scales = vcombine_u8(vzip1_u8(scales_l, scales_h), vzip2_u8(scales_l, scales_h));
3725        scales = vaddq_u8(vshlq_n_u8(scales, 1), vdupq_n_u8(1));
3726        const uint16x8_t scales1 = vmovl_u8(vget_low_u8(scales));
3727        const uint16x8_t scales2 = vmovl_u8(vget_high_u8(scales));
3728        scales32.val[0] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales1)));
3729        scales32.val[1] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales1)));
3730        scales32.val[2] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales2)));
3731        scales32.val[3] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales2)));
3732        int32x4_t sumi = vdupq_n_s32(0);
3733        for (int ib64 = 0; ib64 < QK_K/64; ++ib64) {
3734            q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
3735            q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[0] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[1] & 511))));
3736            q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[2] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[3] & 511))));
3737            q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[4] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[5] & 511))));
3738            q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[6] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[7] & 511))));
3739            q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[0] >> 9))), vld1_s8((const void *)(signs64 + (q2[1] >> 9))));
3740            q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[2] >> 9))), vld1_s8((const void *)(signs64 + (q2[3] >> 9))));
3741            q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[4] >> 9))), vld1_s8((const void *)(signs64 + (q2[5] >> 9))));
3742            q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[6] >> 9))), vld1_s8((const void *)(signs64 + (q2[7] >> 9))));
3743            q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
3744            q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
3745            q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
3746            q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
3747            const int32x4_t p1 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[0], q8b.val[0]);
3748            const int32x4_t p2 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[1], q8b.val[1]);
3749            const int32x4_t p3 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[2], q8b.val[2]);
3750            const int32x4_t p4 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[3], q8b.val[3]);
3751            const int32x4_t p = vpaddq_s32(vpaddq_s32(p1, p2), vpaddq_s32(p3, p4));
3752            sumi = vmlaq_s32(sumi, p, scales32.val[ib64]);
3753            q2 += 8;
3754        }
3755        sumf += d*vaddvq_s32(sumi);
3756    }
3757    *s = 0.125f * sumf;
3758
3759#else
3760    UNUSED(x);
3761    UNUSED(y);
3762    UNUSED(nb);
3763    ggml_vec_dot_iq2_xs_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc);
3764#endif
3765}
3766
3767void ggml_vec_dot_iq2_s_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
3768    assert(n % QK_K == 0);
3769    assert(nrc == 1);
3770    UNUSED(nrc);
3771    UNUSED(bx);
3772    UNUSED(by);
3773    UNUSED(bs);
3774
3775    const block_iq2_s * GGML_RESTRICT x = vx;
3776    const block_q8_K  * GGML_RESTRICT y = vy;
3777
3778    const int nb = n / QK_K;
3779
3780#if defined(__ARM_NEON)
3781
3782   static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
3783                                       0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
3784   };
3785
3786    static const uint8_t k_mask2[16] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,};
3787
3788    const ggml_uint8x16x2_t mask1 = ggml_vld1q_u8_x2(k_mask1);
3789    const uint8x16_t        mask2 = vld1q_u8(k_mask2);
3790    const uint8x16_t m1 = vdupq_n_u8(1);
3791    const int32x4_t vzero = vdupq_n_s32(0);
3792
3793    uint8x16x2_t vs;
3794    ggml_int8x16x4_t q2s;
3795    ggml_int8x16x4_t q8b;
3796
3797    float sumf = 0;
3798    for (int i = 0; i < nb; ++i) {
3799
3800        const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d;
3801
3802        const uint8_t * GGML_RESTRICT qs = x[i].qs;
3803        const uint8_t * GGML_RESTRICT qh = x[i].qh;
3804        const uint16_t * GGML_RESTRICT signs = (const uint16_t *)(x[i].qs + QK_K/8);
3805        const int8_t  * GGML_RESTRICT q8 = y[i].qs;
3806
3807        int sumi1 = 0, sumi2 = 0;
3808        for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
3809            q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
3810            q2s.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[0] | ((qh[ib32+0] << 8) & 0x300)))),
3811                                     vld1_s8((const int8_t *)(iq2s_grid + (qs[1] | ((qh[ib32+0] << 6) & 0x300)))));
3812            q2s.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[2] | ((qh[ib32+0] << 4) & 0x300)))),
3813                                     vld1_s8((const int8_t *)(iq2s_grid + (qs[3] | ((qh[ib32+0] << 2) & 0x300)))));
3814            q2s.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[4] | ((qh[ib32+1] << 8) & 0x300)))),
3815                                     vld1_s8((const int8_t *)(iq2s_grid + (qs[5] | ((qh[ib32+1] << 6) & 0x300)))));
3816            q2s.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[6] | ((qh[ib32+1] << 4) & 0x300)))),
3817                                     vld1_s8((const int8_t *)(iq2s_grid + (qs[7] | ((qh[ib32+1] << 2) & 0x300)))));
3818            qs += 8;
3819
3820            vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[0] | ((uint32_t) signs[1] << 16)));
3821            vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
3822            vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
3823            vs.val[0] = vceqq_u8(vs.val[0], mask2);
3824            vs.val[1] = vceqq_u8(vs.val[1], mask2);
3825
3826            q2s.val[0] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[0], m1)), q2s.val[0]);
3827            q2s.val[1] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[1], m1)), q2s.val[1]);
3828
3829            vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[2] | ((uint32_t) signs[3] << 16)));
3830            vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
3831            vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
3832            vs.val[0] = vceqq_u8(vs.val[0], mask2);
3833            vs.val[1] = vceqq_u8(vs.val[1], mask2);
3834
3835            signs += 4;
3836
3837            q2s.val[2] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[0], m1)), q2s.val[2]);
3838            q2s.val[3] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[1], m1)), q2s.val[3]);
3839
3840            const int32x4_t p1 = ggml_vdotq_s32(vzero, q2s.val[0], q8b.val[0]);
3841            const int32x4_t p2 = ggml_vdotq_s32(vzero, q2s.val[1], q8b.val[1]);
3842            const int32x4_t p3 = ggml_vdotq_s32(vzero, q2s.val[2], q8b.val[2]);
3843            const int32x4_t p4 = ggml_vdotq_s32(vzero, q2s.val[3], q8b.val[3]);
3844
3845            sumi1 += vaddvq_s32(p1) * (1 + 2*(x[i].scales[ib32+0] & 0xf));
3846            sumi2 += vaddvq_s32(p2) * (1 + 2*(x[i].scales[ib32+0] >>  4));
3847            sumi1 += vaddvq_s32(p3) * (1 + 2*(x[i].scales[ib32+1] & 0xf));
3848            sumi2 += vaddvq_s32(p4) * (1 + 2*(x[i].scales[ib32+1] >>  4));
3849        }
3850        sumf += d*(sumi1 + sumi2);
3851    }
3852
3853    *s = 0.125f * sumf;
3854
3855#else
3856    UNUSED(x);
3857    UNUSED(y);
3858    UNUSED(nb);
3859    ggml_vec_dot_iq2_s_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc);
3860#endif
3861
3862}
3863
3864void ggml_vec_dot_iq3_xxs_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
3865    assert(n % QK_K == 0);
3866    assert(nrc == 1);
3867    UNUSED(nrc);
3868    UNUSED(bx);
3869    UNUSED(by);
3870    UNUSED(bs);
3871
3872    const block_iq3_xxs * GGML_RESTRICT x = vx;
3873    const block_q8_K    * GGML_RESTRICT y = vy;
3874
3875    const int nb = n / QK_K;
3876
3877#if defined(__ARM_NEON)
3878
3879    const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
3880
3881    uint32_t aux32[2];
3882
3883    ggml_int8x16x4_t q3s;
3884    ggml_int8x16x4_t q8b;
3885
3886    float sumf = 0;
3887    for (int i = 0; i < nb; ++i) {
3888        const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d;
3889        const uint8_t * GGML_RESTRICT q3 = x[i].qs;
3890        const uint8_t * GGML_RESTRICT gas = x[i].qs + QK_K/4;
3891        const int8_t   * GGML_RESTRICT q8 = y[i].qs;
3892        float sumf1 = 0, sumf2 = 0;
3893        for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
3894            q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
3895            memcpy(aux32, gas, 2*sizeof(uint32_t)); gas += 2*sizeof(uint32_t);
3896            const uint32x4_t aux32x4_0 = ggml_vld1q_u32(iq3xxs_grid[q3[ 0]], iq3xxs_grid[q3[ 1]], iq3xxs_grid[q3[ 2]], iq3xxs_grid[q3[ 3]]);
3897            const uint32x4_t aux32x4_1 = ggml_vld1q_u32(iq3xxs_grid[q3[ 4]], iq3xxs_grid[q3[ 5]], iq3xxs_grid[q3[ 6]], iq3xxs_grid[q3[ 7]]);
3898            const uint32x4_t aux32x4_2 = ggml_vld1q_u32(iq3xxs_grid[q3[ 8]], iq3xxs_grid[q3[ 9]], iq3xxs_grid[q3[10]], iq3xxs_grid[q3[11]]);
3899            const uint32x4_t aux32x4_3 = ggml_vld1q_u32(iq3xxs_grid[q3[12]], iq3xxs_grid[q3[13]], iq3xxs_grid[q3[14]], iq3xxs_grid[q3[15]]);
3900            q3 += 16;
3901            q3s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[0] >>  0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[0] >>  7) & 127))));
3902            q3s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[0] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[0] >> 21) & 127))));
3903            q3s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >>  0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >>  7) & 127))));
3904            q3s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
3905            q3s.val[0] = vmulq_s8(q3s.val[0], vreinterpretq_s8_u32(aux32x4_0));
3906            q3s.val[1] = vmulq_s8(q3s.val[1], vreinterpretq_s8_u32(aux32x4_1));
3907            q3s.val[2] = vmulq_s8(q3s.val[2], vreinterpretq_s8_u32(aux32x4_2));
3908            q3s.val[3] = vmulq_s8(q3s.val[3], vreinterpretq_s8_u32(aux32x4_3));
3909            const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q3s.val[0], q8b.val[0]), q3s.val[1], q8b.val[1]);
3910            const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q3s.val[2], q8b.val[2]), q3s.val[3], q8b.val[3]);
3911            sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[0] >> 28));
3912            sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[1] >> 28));
3913        }
3914        sumf += d*(sumf1 + sumf2);
3915    }
3916    *s = 0.5f * sumf;
3917
3918#else
3919    UNUSED(x);
3920    UNUSED(y);
3921    UNUSED(nb);
3922    ggml_vec_dot_iq3_xxs_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc);
3923#endif
3924}
3925
3926void ggml_vec_dot_iq3_s_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
3927    assert(n % QK_K == 0);
3928    assert(nrc == 1);
3929    UNUSED(nrc);
3930    UNUSED(bx);
3931    UNUSED(by);
3932    UNUSED(bs);
3933
3934    const block_iq3_s * GGML_RESTRICT x = vx;
3935    const block_q8_K  * GGML_RESTRICT y = vy;
3936
3937    const int nb = n / QK_K;
3938
3939#if defined(__ARM_NEON)
3940
3941    typedef union {
3942        uint16x8_t vec_index;
3943        uint16_t   index[8];
3944    } vec_index_t;
3945
3946   static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
3947                                       0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
3948   };
3949
3950    static const uint8_t k_mask2[16] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,};
3951
3952    static const int16_t k_shift[8] = {8, 7, 6, 5, 4, 3, 2, 1};
3953
3954    const ggml_uint8x16x2_t mask1 = ggml_vld1q_u8_x2(k_mask1);
3955    const uint8x16_t        mask2 = vld1q_u8(k_mask2);
3956
3957    const int16x8_t  hshift = vld1q_s16(k_shift);
3958    const uint16x8_t m256   = vdupq_n_u16(256);
3959    const uint8x16_t m1     = vdupq_n_u8(1);
3960
3961    uint8x16x2_t vs;
3962    ggml_int8x16x4_t q3s;
3963    ggml_int8x16x4_t q8b;
3964    vec_index_t idx;
3965
3966    uint32_t scales32[2];
3967    const uint8_t * scales8 = (const uint8_t *)scales32;
3968
3969    float sumf = 0;
3970    for (int i = 0; i < nb; ++i) {
3971        const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d;
3972        const uint8_t * GGML_RESTRICT qs = x[i].qs;
3973        const uint8_t * GGML_RESTRICT qh = x[i].qh;
3974        const uint16_t * GGML_RESTRICT signs = (const uint16_t *)x[i].signs;
3975        const int8_t   * GGML_RESTRICT q8 = y[i].qs;
3976
3977        memcpy(scales32, x[i].scales, 4);
3978        scales32[1] = (((scales32[0] >> 4) & 0x0f0f0f0f) << 1) | 0x01010101;
3979        scales32[0] = ((scales32[0] & 0x0f0f0f0f) << 1) | 0x01010101;
3980
3981        int sumi1 = 0, sumi2 = 0;
3982        for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
3983            q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
3984
3985            const uint8x16_t idx_l = vld1q_u8(qs); qs += 16;
3986            idx.vec_index = vorrq_u16(vmovl_u8(vget_low_u8 (idx_l)), vandq_u16(vshlq_u16(vdupq_n_u16(qh[ib32+0]), hshift), m256));
3987            const uint32x4_t aux32x4_0 = ggml_vld1q_u32(iq3s_grid[idx.index[0]], iq3s_grid[idx.index[1]],
3988                                                        iq3s_grid[idx.index[2]], iq3s_grid[idx.index[3]]);
3989            const uint32x4_t aux32x4_1 = ggml_vld1q_u32(iq3s_grid[idx.index[4]], iq3s_grid[idx.index[5]],
3990                                                        iq3s_grid[idx.index[6]], iq3s_grid[idx.index[7]]);
3991            idx.vec_index = vorrq_u16(vmovl_u8(vget_high_u8(idx_l)), vandq_u16(vshlq_u16(vdupq_n_u16(qh[ib32+1]), hshift), m256));
3992            const uint32x4_t aux32x4_2 = ggml_vld1q_u32(iq3s_grid[idx.index[0]], iq3s_grid[idx.index[1]],
3993                                                        iq3s_grid[idx.index[2]], iq3s_grid[idx.index[3]]);
3994            const uint32x4_t aux32x4_3 = ggml_vld1q_u32(iq3s_grid[idx.index[4]], iq3s_grid[idx.index[5]],
3995                                                        iq3s_grid[idx.index[6]], iq3s_grid[idx.index[7]]);
3996
3997
3998            vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[0] | ((uint32_t) signs[1] << 16)));
3999            vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
4000            vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
4001            vs.val[0] = vorrq_u8(vceqq_u8(vs.val[0], mask2), m1);
4002            vs.val[1] = vorrq_u8(vceqq_u8(vs.val[1], mask2), m1);
4003
4004            q3s.val[0] = vmulq_s8(vreinterpretq_s8_u8(vs.val[0]), vreinterpretq_s8_u32(aux32x4_0));
4005            q3s.val[1] = vmulq_s8(vreinterpretq_s8_u8(vs.val[1]), vreinterpretq_s8_u32(aux32x4_1));
4006
4007            vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[2] | ((uint32_t) signs[3] << 16)));
4008            vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
4009            vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
4010            vs.val[0] = vorrq_u8(vceqq_u8(vs.val[0], mask2), m1);
4011            vs.val[1] = vorrq_u8(vceqq_u8(vs.val[1], mask2), m1);
4012
4013            signs += 4;
4014
4015            q3s.val[2] = vmulq_s8(vreinterpretq_s8_u8(vs.val[0]), vreinterpretq_s8_u32(aux32x4_2));
4016            q3s.val[3] = vmulq_s8(vreinterpretq_s8_u8(vs.val[1]), vreinterpretq_s8_u32(aux32x4_3));
4017
4018            const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q3s.val[0], q8b.val[0]), q3s.val[1], q8b.val[1]);
4019            const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q3s.val[2], q8b.val[2]), q3s.val[3], q8b.val[3]);
4020
4021            sumi1 += vaddvq_s32(p1) * scales8[ib32/2+0];
4022            sumi2 += vaddvq_s32(p2) * scales8[ib32/2+4];
4023        }
4024        sumf += d*(sumi1 + sumi2);
4025    }
4026    *s = sumf;
4027
4028#else
4029    UNUSED(x);
4030    UNUSED(y);
4031    UNUSED(nb);
4032    ggml_vec_dot_iq3_s_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc);
4033#endif
4034}
4035
4036void ggml_vec_dot_iq1_s_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
4037    assert(n % QK_K == 0);
4038    assert(nrc == 1);
4039    UNUSED(nrc);
4040    UNUSED(bx);
4041    UNUSED(by);
4042    UNUSED(bs);
4043
4044    const block_iq1_s * GGML_RESTRICT x = vx;
4045    const block_q8_K  * GGML_RESTRICT y = vy;
4046
4047    const int nb = n / QK_K;
4048
4049#if defined __ARM_NEON
4050
4051    ggml_int8x16x4_t q1b;
4052    ggml_int8x16x4_t q8b;
4053
4054    float sumf = 0;
4055    for (int i = 0; i < nb; ++i) {
4056
4057        const int8_t   * q8 = y[i].qs;
4058        const uint8_t  * qs = x[i].qs;
4059        const uint16_t * qh = x[i].qh;
4060
4061        int sumi1 = 0, sumi2 = 0, sumi3 = 0;
4062
4063        for (int ib = 0; ib < QK_K/32; ib += 2) {
4064
4065            q1b.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[0] | ((qh[ib+0] << 8) & 0x700)))),
4066                                     vld1_s8((const int8_t *)(iq1s_grid + (qs[1] | ((qh[ib+0] << 5) & 0x700)))));
4067            q1b.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[2] | ((qh[ib+0] << 2) & 0x700)))),
4068                                     vld1_s8((const int8_t *)(iq1s_grid + (qs[3] | ((qh[ib+0] >> 1) & 0x700)))));
4069            q1b.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[4] | ((qh[ib+1] << 8) & 0x700)))),
4070                                     vld1_s8((const int8_t *)(iq1s_grid + (qs[5] | ((qh[ib+1] << 5) & 0x700)))));
4071            q1b.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[6] | ((qh[ib+1] << 2) & 0x700)))),
4072                                     vld1_s8((const int8_t *)(iq1s_grid + (qs[7] | ((qh[ib+1] >> 1) & 0x700)))));
4073            qs += 8;
4074
4075            q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
4076
4077            const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q1b.val[0], q8b.val[0]), q1b.val[1], q8b.val[1]);
4078            const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q1b.val[2], q8b.val[2]), q1b.val[3], q8b.val[3]);
4079
4080            const int ls1 = 2*((qh[ib+0] >> 12) & 7) + 1;
4081            const int ls2 = 2*((qh[ib+1] >> 12) & 7) + 1;
4082            sumi1 += vaddvq_s32(p1) * ls1;
4083            sumi2 += vaddvq_s32(p2) * ls2;
4084            sumi3 += (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]) * ls1 * (qh[ib+0] & 0x8000 ? -1 : 1)
4085                   + (y[i].bsums[2*ib+2] + y[i].bsums[2*ib+3]) * ls2 * (qh[ib+1] & 0x8000 ? -1 : 1);
4086
4087        }
4088
4089        sumf += y[i].d * GGML_CPU_FP16_TO_FP32(x[i].d) * (sumi1 + sumi2 + IQ1S_DELTA * sumi3);
4090    }
4091
4092    *s = sumf;
4093
4094#else
4095    UNUSED(x);
4096    UNUSED(y);
4097    UNUSED(nb);
4098    ggml_vec_dot_iq1_s_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc);
4099#endif
4100}
4101
4102void ggml_vec_dot_iq1_m_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
4103    assert(n % QK_K == 0);
4104    assert(nrc == 1);
4105    UNUSED(nrc);
4106    UNUSED(bx);
4107    UNUSED(by);
4108    UNUSED(bs);
4109
4110    const block_iq1_m * GGML_RESTRICT x = vx;
4111    const block_q8_K  * GGML_RESTRICT y = vy;
4112
4113    const int nb = n / QK_K;
4114
4115    iq1m_scale_t scale;
4116
4117#if defined __ARM_NEON
4118    const int32x4_t mask  = vdupq_n_s32(0x7);
4119    const int32x4_t mone  = vdupq_n_s32(1);
4120    const int32x4_t mzero = vdupq_n_s32(0);
4121
4122    ggml_int8x16x4_t deltas;
4123    deltas.val[0] = vcombine_s8(vdup_n_s8(+1), vdup_n_s8(+1));
4124    deltas.val[1] = vcombine_s8(vdup_n_s8(-1), vdup_n_s8(+1));
4125    deltas.val[2] = vcombine_s8(vdup_n_s8(+1), vdup_n_s8(-1));
4126    deltas.val[3] = vcombine_s8(vdup_n_s8(-1), vdup_n_s8(-1));
4127
4128    ggml_int8x16x4_t q1b;
4129    ggml_int8x16x4_t q8b;
4130
4131    uint32_t aux32;
4132    const uint8_t * aux8 = (const uint8_t *)&aux32;
4133
4134    float sumf = 0;
4135    for (int i = 0; i < nb; ++i) {
4136
4137        const int8_t   * q8 = y[i].qs;
4138        const uint8_t  * qs = x[i].qs;
4139        const uint8_t  * qh = x[i].qh;
4140        const uint16_t * sc = (const uint16_t *)x[i].scales;
4141
4142        scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
4143
4144        int32x4_t sumi1 = mzero;
4145        int32x4_t sumi2 = mzero;
4146
4147        for (int ib = 0; ib < QK_K/32; ib += 2) {
4148
4149            q1b.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[0] | ((qh[0] << 8) & 0x700)))),
4150                                     vld1_s8((const int8_t *)(iq1s_grid + (qs[1] | ((qh[0] << 4) & 0x700)))));
4151            q1b.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[2] | ((qh[1] << 8) & 0x700)))),
4152                                     vld1_s8((const int8_t *)(iq1s_grid + (qs[3] | ((qh[1] << 4) & 0x700)))));
4153            q1b.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[4] | ((qh[2] << 8) & 0x700)))),
4154                                     vld1_s8((const int8_t *)(iq1s_grid + (qs[5] | ((qh[2] << 4) & 0x700)))));
4155            q1b.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[6] | ((qh[3] << 8) & 0x700)))),
4156                                     vld1_s8((const int8_t *)(iq1s_grid + (qs[7] | ((qh[3] << 4) & 0x700)))));
4157
4158            q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
4159
4160            const int32x4_t p1 = vpaddq_s32(ggml_vdotq_s32(mzero, q1b.val[0], q8b.val[0]), ggml_vdotq_s32(mzero, q1b.val[1], q8b.val[1]));
4161            const int32x4_t p2 = vpaddq_s32(ggml_vdotq_s32(mzero, q1b.val[2], q8b.val[2]), ggml_vdotq_s32(mzero, q1b.val[3], q8b.val[3]));
4162            const int32x4_t p12 = vpaddq_s32(p1, p2);
4163
4164            const uint32_t * qh32 = (const uint32_t *)qh; // we are 4-byte aligned, so we can do that
4165            aux32 = ((qh32[0] >> 3) & 0x01010101) | ((qh32[0] >> 6) & 0x02020202);
4166
4167            const int32x4_t p3 = vpaddq_s32(ggml_vdotq_s32(mzero, deltas.val[aux8[0]], q8b.val[0]), ggml_vdotq_s32(mzero, deltas.val[aux8[1]], q8b.val[1]));
4168            const int32x4_t p4 = vpaddq_s32(ggml_vdotq_s32(mzero, deltas.val[aux8[2]], q8b.val[2]), ggml_vdotq_s32(mzero, deltas.val[aux8[3]], q8b.val[3]));
4169            const int32x4_t p34 = vpaddq_s32(p3, p4);
4170
4171            int32x4_t scales_4 = ggml_vld1q_u32(sc[ib/2] >> 0, sc[ib/2] >> 3, sc[ib/2] >> 6, sc[ib/2] >> 9);
4172
4173            scales_4 = vaddq_s32(vshlq_n_s32(vandq_s32(scales_4, mask), 1), mone);
4174
4175            sumi1 = vmlaq_s32(sumi1, scales_4, p12);
4176            sumi2 = vmlaq_s32(sumi2, scales_4, p34);
4177
4178            qs += 8; qh += 4;
4179
4180        }
4181
4182        sumf += y[i].d * GGML_CPU_FP16_TO_FP32(scale.f16) * (vaddvq_s32(sumi1) + IQ1M_DELTA * vaddvq_s32(sumi2));
4183    }
4184
4185    *s = sumf;
4186
4187#else
4188    UNUSED(x);
4189    UNUSED(y);
4190    UNUSED(nb);
4191    UNUSED(scale);
4192    ggml_vec_dot_iq1_m_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc);
4193#endif
4194}
4195
4196void ggml_vec_dot_iq4_nl_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
4197    assert(nrc == 1);
4198    UNUSED(nrc);
4199    UNUSED(bx);
4200    UNUSED(by);
4201    UNUSED(bs);
4202    assert(n % QK4_NL == 0);
4203    static_assert(QK4_NL == QK8_0, "QK4_NL and QK8_0 must be the same");
4204
4205    const block_iq4_nl * GGML_RESTRICT x = vx;
4206    const block_q8_0   * GGML_RESTRICT y = vy;
4207
4208    const int nb = n / QK4_NL;
4209
4210    int ib = 0;
4211    float sumf = 0;
4212
4213#if defined __ARM_NEON
4214    const int8x16_t values = vld1q_s8(kvalues_iq4nl);
4215    const uint8x16_t m4b = vdupq_n_u8(0x0f);
4216    uint8x16x2_t q4bits;
4217    int8x16x4_t q4b;
4218    int8x16x4_t q8b;
4219    int32x4_t prod_1, prod_2;
4220
4221    for (; ib + 1 < nb; ib += 2) {
4222
4223        q4bits.val[0] = vld1q_u8(x[ib + 0].qs);
4224        q4bits.val[1] = vld1q_u8(x[ib + 1].qs);
4225        q8b.val[0]    = vld1q_s8(y[ib + 0].qs);
4226        q8b.val[1]    = vld1q_s8(y[ib + 0].qs + 16);
4227        q8b.val[2]    = vld1q_s8(y[ib + 1].qs);
4228        q8b.val[3]    = vld1q_s8(y[ib + 1].qs + 16);
4229
4230        q4b.val[0] = ggml_vqtbl1q_s8(values, vandq_u8  (q4bits.val[0], m4b));
4231        q4b.val[1] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[0], 4));
4232        q4b.val[2] = ggml_vqtbl1q_s8(values, vandq_u8  (q4bits.val[1], m4b));
4233        q4b.val[3] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[1], 4));
4234
4235        prod_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[0], q8b.val[0]), q4b.val[1], q8b.val[1]);
4236        prod_2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[2], q8b.val[2]), q4b.val[3], q8b.val[3]);
4237
4238        sumf +=
4239            GGML_CPU_FP16_TO_FP32(x[ib+0].d) * GGML_CPU_FP16_TO_FP32(y[ib + 0].d) * vaddvq_s32(prod_1) +
4240            GGML_CPU_FP16_TO_FP32(x[ib+1].d) * GGML_CPU_FP16_TO_FP32(y[ib + 1].d) * vaddvq_s32(prod_2);
4241    }
4242
4243#endif
4244    for (; ib < nb; ++ib) {
4245        const float d = GGML_CPU_FP16_TO_FP32(y[ib].d)*GGML_CPU_FP16_TO_FP32(x[ib].d);
4246        int sumi1 = 0, sumi2 = 0;
4247        for (int j = 0; j < QK4_NL/2; ++j) {
4248            sumi1 += y[ib].qs[j+       0] * kvalues_iq4nl[x[ib].qs[j] & 0xf];
4249            sumi2 += y[ib].qs[j+QK4_NL/2] * kvalues_iq4nl[x[ib].qs[j] >>  4];
4250        }
4251        sumf += d * (sumi1 + sumi2);
4252    }
4253    *s = sumf;
4254}
4255
4256void ggml_vec_dot_iq4_xs_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) {
4257    assert(nrc == 1);
4258    UNUSED(nrc);
4259    UNUSED(bx);
4260    UNUSED(by);
4261    UNUSED(bs);
4262    assert(n % QK_K == 0);
4263
4264    const block_iq4_xs * GGML_RESTRICT x = vx;
4265    const block_q8_K   * GGML_RESTRICT y = vy;
4266
4267    const int nb = n / QK_K;
4268
4269#if defined __ARM_NEON
4270    const int8x16_t values = vld1q_s8(kvalues_iq4nl);
4271    const uint8x16_t m4b = vdupq_n_u8(0x0f);
4272    ggml_uint8x16x2_t q4bits;
4273    ggml_int8x16x4_t q4b;
4274    ggml_int8x16x4_t q8b;
4275    int32x4_t prod_1, prod_2;
4276
4277    float sumf = 0;
4278
4279    for (int ibl = 0; ibl < nb; ++ibl) {
4280
4281        const int8_t  * q8 = y[ibl].qs;
4282        const uint8_t * q4 = x[ibl].qs;
4283        uint16_t h = x[ibl].scales_h;
4284
4285        int sumi1 = 0, sumi2 = 0;
4286        for (int ib = 0; ib < QK_K/64; ++ib) {
4287
4288            q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
4289            q8b    = ggml_vld1q_s8_x4(q8); q8 += 64;
4290
4291            q4b.val[0] = ggml_vqtbl1q_s8(values, vandq_u8  (q4bits.val[0], m4b));
4292            q4b.val[1] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[0], 4));
4293            q4b.val[2] = ggml_vqtbl1q_s8(values, vandq_u8  (q4bits.val[1], m4b));
4294            q4b.val[3] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[1], 4));
4295
4296            prod_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[0], q8b.val[0]), q4b.val[1], q8b.val[1]);
4297            prod_2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[2], q8b.val[2]), q4b.val[3], q8b.val[3]);
4298
4299            int ls1 = ((x[ibl].scales_l[ib] & 0xf) | ((h << 4) & 0x30)) - 32;
4300            int ls2 = ((x[ibl].scales_l[ib] >>  4) | ((h << 2) & 0x30)) - 32;
4301            h >>= 4;
4302            sumi1 += vaddvq_s32(prod_1) * ls1;
4303            sumi2 += vaddvq_s32(prod_2) * ls2;
4304
4305        }
4306
4307        sumf += GGML_CPU_FP16_TO_FP32(x[ibl].d) * y[ibl].d * (sumi1 + sumi2);
4308    }
4309
4310    *s = sumf;
4311
4312#else
4313    UNUSED(x);
4314    UNUSED(y);
4315    UNUSED(nb);
4316    ggml_vec_dot_iq4_xs_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc);
4317#endif
4318}
4319