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