interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-cpu / arch / arm / quants.c

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