Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-cpu/arch/x86/quants.c # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-cpu / arch / x86 / quants.c` 4172 lines ยท 200035 bytes Raw Blame History 1 #define GGML_COMMON_IMPL_C 2 #include "ggml-common.h" 3 #include "ggml-quants.h" 4 #include "ggml-impl.h" 5 #include "ggml-cpu.h" 6 #include "simd-mappings.h" 7 8 #include "../../quants.h" 9 #include "../../ggml-cpu-impl.h" 10 11 #include 12 #include 13 #include 14 #include // for qsort 15 #include // for GGML_ASSERT 16 17 #define GROUP_MAX_EPS 1e-15f 18 #define GROUP_MAX_EPS_IQ3_XXS 1e-8f 19 #define GROUP_MAX_EPS_IQ2_S 1e-8f 20 #define GROUP_MAX_EPS_IQ1_M 1e-7f 21 #define GROUP_MAX_EPS_IQ1_S 1e-12f 22 23 #define UNUSED GGML_UNUSED 24 25 // some compilers don't provide _mm256_set_m128i, e.g. gcc 7 26 #define MM256_SET_M128I(a, b) _mm256_insertf128_si256(_mm256_castsi128_si256(b), (a), 1) 27 28 #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) 29 // multiply int8_t, add results pairwise twice 30 static inline __m128i mul_sum_i8_pairs(const __m128i x, const __m128i y) { 31 // Get absolute values of x vectors 32 const __m128i ax = _mm_sign_epi8(x, x); 33 // Sign the values of the y vectors 34 const __m128i sy = _mm_sign_epi8(y, x); 35 // Perform multiplication and create 16-bit values 36 const __m128i dot = _mm_maddubs_epi16(ax, sy); 37 const __m128i ones = _mm_set1_epi16(1); 38 return _mm_madd_epi16(ones, dot); 39 } 40 41 #if __AVX__ || __AVX2__ || __AVX512F__ 42 // horizontally add 8 floats 43 static inline float hsum_float_8(const __m256 x) { 44 __m128 res = _mm256_extractf128_ps(x, 1); 45 res = _mm_add_ps(res, _mm256_castps256_ps128(x)); 46 res = _mm_add_ps(res, _mm_movehl_ps(res, res)); 47 res = _mm_add_ss(res, _mm_movehdup_ps(res)); 48 return _mm_cvtss_f32(res); 49 } 50 51 // horizontally add 8 int32_t 52 static inline int hsum_i32_8(const __m256i a) { 53 const __m128i sum128 = _mm_add_epi32(_mm256_castsi256_si128(a), _mm256_extractf128_si256(a, 1)); 54 const __m128i hi64 = _mm_unpackhi_epi64(sum128, sum128); 55 const __m128i sum64 = _mm_add_epi32(hi64, sum128); 56 const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1)); 57 return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32)); 58 } 59 60 // horizontally add 4 int32_t 61 static inline int hsum_i32_4(const __m128i a) { 62 const __m128i hi64 = _mm_unpackhi_epi64(a, a); 63 const __m128i sum64 = _mm_add_epi32(hi64, a); 64 const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1)); 65 return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32)); 66 } 67 68 #if defined(__AVX2__) || defined(__AVX512F__) 69 static inline __m256i mul_add_epi8(const __m256i x, const __m256i y) { 70 const __m256i ax = _mm256_sign_epi8(x, x); 71 const __m256i sy = _mm256_sign_epi8(y, x); 72 return _mm256_maddubs_epi16(ax, sy); 73 } 74 75 // spread 32 bits to 32 bytes { 0x00, 0xFF } 76 static inline __m256i bytes_from_bits_32(const uint8_t * x) { 77 uint32_t x32; 78 memcpy(&x32, x, sizeof(uint32_t)); 79 const __m256i shuf_mask = _mm256_set_epi64x( 80 0x0303030303030303, 0x0202020202020202, 81 0x0101010101010101, 0x0000000000000000); 82 __m256i bytes = _mm256_shuffle_epi8(_mm256_set1_epi32(x32), shuf_mask); 83 const __m256i bit_mask = _mm256_set1_epi64x(0x7fbfdfeff7fbfdfe); 84 bytes = _mm256_or_si256(bytes, bit_mask); 85 return _mm256_cmpeq_epi8(bytes, _mm256_set1_epi64x(-1)); 86 } 87 88 // Unpack 32 4-bit fields into 32 bytes 89 // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval 90 static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi) 91 { 92 const __m128i tmp = _mm_loadu_si128((const __m128i *)rsi); 93 const __m256i bytes = MM256_SET_M128I(_mm_srli_epi16(tmp, 4), tmp); 94 const __m256i lowMask = _mm256_set1_epi8( 0xF ); 95 return _mm256_and_si256(lowMask, bytes); 96 } 97 98 // add int16_t pairwise and return as float vector 99 static inline __m256 sum_i16_pairs_float(const __m256i x) { 100 const __m256i ones = _mm256_set1_epi16(1); 101 const __m256i summed_pairs = _mm256_madd_epi16(ones, x); 102 return _mm256_cvtepi32_ps(summed_pairs); 103 } 104 105 static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) { 106 #if defined(__AVX512VNNI__) && defined(__AVX512VL__) 107 const __m256i zero = _mm256_setzero_si256(); 108 const __m256i summed_pairs = _mm256_dpbusd_epi32(zero, ax, sy); 109 return _mm256_cvtepi32_ps(summed_pairs); 110 #elif defined(__AVXVNNI__) 111 const __m256i zero = _mm256_setzero_si256(); 112 const __m256i summed_pairs = _mm256_dpbusd_avx_epi32(zero, ax, sy); 113 return _mm256_cvtepi32_ps(summed_pairs); 114 #else 115 // Perform multiplication and create 16-bit values 116 const __m256i dot = _mm256_maddubs_epi16(ax, sy); 117 return sum_i16_pairs_float(dot); 118 #endif 119 } 120 121 // multiply int8_t, add results pairwise twice and return as float vector 122 static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) { 123 #if __AVXVNNIINT8__ 124 const __m256i zero = _mm256_setzero_si256(); 125 const __m256i summed_pairs = _mm256_dpbssd_epi32(zero, x, y); 126 return _mm256_cvtepi32_ps(summed_pairs); 127 #else 128 // Get absolute values of x vectors 129 const __m256i ax = _mm256_sign_epi8(x, x); 130 // Sign the values of the y vectors 131 const __m256i sy = _mm256_sign_epi8(y, x); 132 return mul_sum_us8_pairs_float(ax, sy); 133 #endif 134 } 135 136 static inline __m128i packNibbles( __m256i bytes ) 137 { 138 // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh 139 #if __AVX512F__ 140 const __m256i bytes_srli_4 = _mm256_srli_epi16(bytes, 4); // 0000_0000_abcd_0000 141 bytes = _mm256_or_si256(bytes, bytes_srli_4); // 0000_abcd_abcd_efgh 142 return _mm256_cvtepi16_epi8(bytes); // abcd_efgh 143 #else 144 const __m256i lowByte = _mm256_set1_epi16( 0xFF ); 145 __m256i high = _mm256_andnot_si256( lowByte, bytes ); 146 __m256i low = _mm256_and_si256( lowByte, bytes ); 147 high = _mm256_srli_epi16( high, 4 ); 148 bytes = _mm256_or_si256( low, high ); 149 150 // Compress uint16_t lanes into bytes 151 __m128i r0 = _mm256_castsi256_si128( bytes ); 152 __m128i r1 = _mm256_extracti128_si256( bytes, 1 ); 153 return _mm_packus_epi16( r0, r1 ); 154 #endif 155 } 156 #elif defined(__AVX__) 157 static inline __m128i packNibbles( __m128i bytes1, __m128i bytes2 ) 158 { 159 // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh 160 const __m128i lowByte = _mm_set1_epi16( 0xFF ); 161 __m128i high = _mm_andnot_si128( lowByte, bytes1 ); 162 __m128i low = _mm_and_si128( lowByte, bytes1 ); 163 high = _mm_srli_epi16( high, 4 ); 164 bytes1 = _mm_or_si128( low, high ); 165 high = _mm_andnot_si128( lowByte, bytes2 ); 166 low = _mm_and_si128( lowByte, bytes2 ); 167 high = _mm_srli_epi16( high, 4 ); 168 bytes2 = _mm_or_si128( low, high ); 169 170 return _mm_packus_epi16( bytes1, bytes2); 171 } 172 173 static inline __m128i mul_add_epi8_sse(const __m128i x, const __m128i y) { 174 const __m128i ax = _mm_sign_epi8(x, x); 175 const __m128i sy = _mm_sign_epi8(y, x); 176 return _mm_maddubs_epi16(ax, sy); 177 } 178 179 // spread 32 bits to 32 bytes { 0x00, 0xFF } 180 static inline __m256i bytes_from_bits_32(const uint8_t * x) { 181 uint32_t x32; 182 memcpy(&x32, x, sizeof(uint32_t)); 183 const __m128i shuf_maskl = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000); 184 const __m128i shuf_maskh = _mm_set_epi64x(0x0303030303030303, 0x0202020202020202); 185 __m128i bytesl = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskl); 186 __m128i bytesh = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskh); 187 const __m128i bit_mask = _mm_set1_epi64x(0x7fbfdfeff7fbfdfe); 188 bytesl = _mm_or_si128(bytesl, bit_mask); 189 bytesh = _mm_or_si128(bytesh, bit_mask); 190 bytesl = _mm_cmpeq_epi8(bytesl, _mm_set1_epi64x(-1)); 191 bytesh = _mm_cmpeq_epi8(bytesh, _mm_set1_epi64x(-1)); 192 return MM256_SET_M128I(bytesh, bytesl); 193 } 194 195 // Unpack 32 4-bit fields into 32 bytes 196 // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval 197 static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi) 198 { 199 // Load 16 bytes from memory 200 __m128i tmpl = _mm_loadu_si128((const __m128i *)rsi); 201 __m128i tmph = _mm_srli_epi16(tmpl, 4); 202 const __m128i lowMask = _mm_set1_epi8(0xF); 203 tmpl = _mm_and_si128(lowMask, tmpl); 204 tmph = _mm_and_si128(lowMask, tmph); 205 return MM256_SET_M128I(tmph, tmpl); 206 } 207 208 // add int16_t pairwise and return as float vector 209 static inline __m256 sum_i16_pairs_float(const __m128i xh, const __m128i xl) { 210 const __m128i ones = _mm_set1_epi16(1); 211 const __m128i summed_pairsl = _mm_madd_epi16(ones, xl); 212 const __m128i summed_pairsh = _mm_madd_epi16(ones, xh); 213 const __m256i summed_pairs = MM256_SET_M128I(summed_pairsh, summed_pairsl); 214 return _mm256_cvtepi32_ps(summed_pairs); 215 } 216 217 static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) { 218 const __m128i axl = _mm256_castsi256_si128(ax); 219 const __m128i axh = _mm256_extractf128_si256(ax, 1); 220 const __m128i syl = _mm256_castsi256_si128(sy); 221 const __m128i syh = _mm256_extractf128_si256(sy, 1); 222 // Perform multiplication and create 16-bit values 223 const __m128i dotl = _mm_maddubs_epi16(axl, syl); 224 const __m128i doth = _mm_maddubs_epi16(axh, syh); 225 return sum_i16_pairs_float(doth, dotl); 226 } 227 228 // multiply int8_t, add results pairwise twice and return as float vector 229 static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) { 230 const __m128i xl = _mm256_castsi256_si128(x); 231 const __m128i xh = _mm256_extractf128_si256(x, 1); 232 const __m128i yl = _mm256_castsi256_si128(y); 233 const __m128i yh = _mm256_extractf128_si256(y, 1); 234 // Get absolute values of x vectors 235 const __m128i axl = _mm_sign_epi8(xl, xl); 236 const __m128i axh = _mm_sign_epi8(xh, xh); 237 // Sign the values of the y vectors 238 const __m128i syl = _mm_sign_epi8(yl, xl); 239 const __m128i syh = _mm_sign_epi8(yh, xh); 240 // Perform multiplication and create 16-bit values 241 const __m128i dotl = _mm_maddubs_epi16(axl, syl); 242 const __m128i doth = _mm_maddubs_epi16(axh, syh); 243 return sum_i16_pairs_float(doth, dotl); 244 } 245 246 // larger version of mul_sum_i8_pairs_float where x and y are each represented by four 128-bit vectors 247 static inline __m256 mul_sum_i8_quad_float(const __m128i x_1_0, const __m128i x_1_1, const __m128i x_2_0, const __m128i x_2_1, 248 const __m128i y_1_0, const __m128i y_1_1, const __m128i y_2_0, const __m128i y_2_1) { 249 const __m128i mone = _mm_set1_epi16(1); 250 251 const __m128i p16_1_0 = mul_add_epi8_sse(x_1_0, y_1_0); 252 const __m128i p16_1_1 = mul_add_epi8_sse(x_1_1, y_1_1); 253 const __m128i p16_2_0 = mul_add_epi8_sse(x_2_0, y_2_0); 254 const __m128i p16_2_1 = mul_add_epi8_sse(x_2_1, y_2_1); 255 const __m128i p_1_0 = _mm_madd_epi16(p16_1_0, mone); 256 const __m128i p_1_1 = _mm_madd_epi16(p16_1_1, mone); 257 const __m128i p_2_0 = _mm_madd_epi16(p16_2_0, mone); 258 const __m128i p_2_1 = _mm_madd_epi16(p16_2_1, mone); 259 const __m128i p_1 = _mm_add_epi32(p_1_0, p_1_1); 260 const __m128i p_2 = _mm_add_epi32(p_2_0, p_2_1); 261 return _mm256_cvtepi32_ps(MM256_SET_M128I(p_2, p_1)); 262 } 263 264 // quad fp16 delta calculation 265 static inline __m256 quad_fp16_delta_float(const float x0, const float y0, const float x1, const float y1) { 266 // GGML_CPU_FP16_TO_FP32 is faster than Intel F16C 267 return _mm256_set_m128(_mm_set1_ps(GGML_CPU_FP16_TO_FP32(x1) * GGML_CPU_FP16_TO_FP32(y1)), 268 _mm_set1_ps(GGML_CPU_FP16_TO_FP32(x0) * GGML_CPU_FP16_TO_FP32(y0))); 269 } 270 271 static inline __m256 quad_mx_delta_float(const uint8_t x0, const float y0, const uint8_t x1, const float y1) { 272 return _mm256_set_m128(_mm_set1_ps(GGML_CPU_E8M0_TO_FP32_HALF(x1) * GGML_CPU_FP16_TO_FP32(y1)), 273 _mm_set1_ps(GGML_CPU_E8M0_TO_FP32_HALF(x0) * GGML_CPU_FP16_TO_FP32(y0))); 274 } 275 #endif 276 #elif defined(__SSSE3__) 277 static inline __m128i bytes_from_bits_16(const uint8_t * x) { 278 uint16_t x16; 279 memcpy(&x16, x, sizeof(uint16_t)); 280 281 const __m128i shuf_mask = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000); 282 __m128i bytes = _mm_shuffle_epi8(_mm_set1_epi16((short) x16), shuf_mask); 283 const __m128i bit_mask = _mm_set_epi64x(0x7fbfdfeff7fbfdfe, 0x7fbfdfeff7fbfdfe); 284 bytes = _mm_or_si128(bytes, bit_mask); 285 286 return _mm_cmpeq_epi8(bytes, _mm_set1_epi64x(-1)); 287 } 288 289 // horizontally add 4x4 floats 290 static inline float hsum_float_4x4(const __m128 a, const __m128 b, const __m128 c, const __m128 d) { 291 __m128 res_0 =_mm_hadd_ps(a, b); 292 __m128 res_1 =_mm_hadd_ps(c, d); 293 __m128 res =_mm_hadd_ps(res_0, res_1); 294 res =_mm_hadd_ps(res, res); 295 res =_mm_hadd_ps(res, res); 296 297 return _mm_cvtss_f32(res); 298 } 299 #endif // __AVX__ || __AVX2__ || __AVX512F__ 300 #endif // defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) 301 302 void quantize_row_q8_0(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t k) { 303 assert(QK8_0 == 32); 304 assert(k % QK8_0 == 0); 305 const int nb = k / QK8_0; 306 307 block_q8_0 * GGML_RESTRICT y = vy; 308 309 #if defined(__AVX2__) || defined(__AVX__) 310 for (int i = 0; i < nb; i++) { 311 // Load elements into 4 AVX vectors 312 __m256 v0 = _mm256_loadu_ps( x ); 313 __m256 v1 = _mm256_loadu_ps( x + 8 ); 314 __m256 v2 = _mm256_loadu_ps( x + 16 ); 315 __m256 v3 = _mm256_loadu_ps( x + 24 ); 316 x += 32; 317 318 // Compute max(abs(e)) for the block 319 const __m256 signBit = _mm256_set1_ps( -0.0f ); 320 __m256 maxAbs = _mm256_andnot_ps( signBit, v0 ); 321 maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) ); 322 maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) ); 323 maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) ); 324 325 __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) ); 326 max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) ); 327 max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) ); 328 const float maxScalar = _mm_cvtss_f32( max4 ); 329 330 // Quantize these floats 331 const float d = maxScalar / 127.f; 332 y[i].d = GGML_CPU_FP32_TO_FP16(d); 333 const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f; 334 const __m256 mul = _mm256_set1_ps( id ); 335 336 // Apply the multiplier 337 v0 = _mm256_mul_ps( v0, mul ); 338 v1 = _mm256_mul_ps( v1, mul ); 339 v2 = _mm256_mul_ps( v2, mul ); 340 v3 = _mm256_mul_ps( v3, mul ); 341 342 // Round to nearest integer 343 v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST ); 344 v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST ); 345 v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST ); 346 v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST ); 347 348 // Convert floats to integers 349 __m256i i0 = _mm256_cvtps_epi32( v0 ); 350 __m256i i1 = _mm256_cvtps_epi32( v1 ); 351 __m256i i2 = _mm256_cvtps_epi32( v2 ); 352 __m256i i3 = _mm256_cvtps_epi32( v3 ); 353 354 #if defined(__AVX2__) 355 // Convert int32 to int16 356 i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15 357 i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31 358 // Convert int16 to int8 359 i0 = _mm256_packs_epi16( i0, i2 ); // 0, 1, 2, 3, 8, 9, 10, 11, 16, 17, 18, 19, 24, 25, 26, 27, 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 360 361 // We got our precious signed bytes, but the order is now wrong 362 // These AVX2 pack instructions process 16-byte pieces independently 363 // The following instruction is fixing the order 364 const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 ); 365 i0 = _mm256_permutevar8x32_epi32( i0, perm ); 366 367 _mm256_storeu_si256((__m256i *)y[i].qs, i0); 368 #else 369 // Since we don't have in AVX some necessary functions, 370 // we split the registers in half and call AVX2 analogs from SSE 371 __m128i ni0 = _mm256_castsi256_si128( i0 ); 372 __m128i ni1 = _mm256_extractf128_si256( i0, 1); 373 __m128i ni2 = _mm256_castsi256_si128( i1 ); 374 __m128i ni3 = _mm256_extractf128_si256( i1, 1); 375 __m128i ni4 = _mm256_castsi256_si128( i2 ); 376 __m128i ni5 = _mm256_extractf128_si256( i2, 1); 377 __m128i ni6 = _mm256_castsi256_si128( i3 ); 378 __m128i ni7 = _mm256_extractf128_si256( i3, 1); 379 380 // Convert int32 to int16 381 ni0 = _mm_packs_epi32( ni0, ni1 ); 382 ni2 = _mm_packs_epi32( ni2, ni3 ); 383 ni4 = _mm_packs_epi32( ni4, ni5 ); 384 ni6 = _mm_packs_epi32( ni6, ni7 ); 385 // Convert int16 to int8 386 ni0 = _mm_packs_epi16( ni0, ni2 ); 387 ni4 = _mm_packs_epi16( ni4, ni6 ); 388 389 _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0); 390 _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4); 391 #endif 392 } 393 #else 394 GGML_UNUSED(nb); 395 // scalar 396 quantize_row_q8_0_ref(x, y, k); 397 #endif 398 } 399 400 void quantize_row_q8_1(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t k) { 401 assert(k % QK8_1 == 0); 402 const int nb = k / QK8_1; 403 404 block_q8_1 * GGML_RESTRICT y = vy; 405 #if defined(__AVX2__) || defined(__AVX__) 406 for (int i = 0; i < nb; i++) { 407 // Load elements into 4 AVX vectors 408 __m256 v0 = _mm256_loadu_ps( x ); 409 __m256 v1 = _mm256_loadu_ps( x + 8 ); 410 __m256 v2 = _mm256_loadu_ps( x + 16 ); 411 __m256 v3 = _mm256_loadu_ps( x + 24 ); 412 x += 32; 413 414 // Compute max(abs(e)) for the block 415 const __m256 signBit = _mm256_set1_ps( -0.0f ); 416 __m256 maxAbs = _mm256_andnot_ps( signBit, v0 ); 417 maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) ); 418 maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) ); 419 maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) ); 420 421 __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) ); 422 max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) ); 423 max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) ); 424 const float max_scalar = _mm_cvtss_f32( max4 ); 425 426 // Quantize these floats 427 const float d = max_scalar / 127.f; 428 y[i].d = GGML_CPU_FP32_TO_FP16(d); 429 const float id = ( max_scalar != 0.0f ) ? 127.f / max_scalar : 0.0f; 430 const __m256 mul = _mm256_set1_ps( id ); 431 432 // Apply the multiplier 433 v0 = _mm256_mul_ps( v0, mul ); 434 v1 = _mm256_mul_ps( v1, mul ); 435 v2 = _mm256_mul_ps( v2, mul ); 436 v3 = _mm256_mul_ps( v3, mul ); 437 438 // Round to nearest integer 439 v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST ); 440 v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST ); 441 v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST ); 442 v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST ); 443 444 // Convert floats to integers 445 __m256i i0 = _mm256_cvtps_epi32( v0 ); 446 __m256i i1 = _mm256_cvtps_epi32( v1 ); 447 __m256i i2 = _mm256_cvtps_epi32( v2 ); 448 __m256i i3 = _mm256_cvtps_epi32( v3 ); 449 450 #if defined(__AVX2__) 451 // Compute the sum of the quants and set y[i].s 452 y[i].s = GGML_CPU_FP32_TO_FP16(d * hsum_i32_8(_mm256_add_epi32(_mm256_add_epi32(i0, i1), _mm256_add_epi32(i2, i3)))); 453 454 // Convert int32 to int16 455 i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15 456 i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31 457 // Convert int16 to int8 458 i0 = _mm256_packs_epi16( i0, i2 ); // 0, 1, 2, 3, 8, 9, 10, 11, 16, 17, 18, 19, 24, 25, 26, 27, 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 459 460 // We got our precious signed bytes, but the order is now wrong 461 // These AVX2 pack instructions process 16-byte pieces independently 462 // The following instruction is fixing the order 463 const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 ); 464 i0 = _mm256_permutevar8x32_epi32( i0, perm ); 465 466 _mm256_storeu_si256((__m256i *)y[i].qs, i0); 467 #else 468 // Since we don't have in AVX some necessary functions, 469 // we split the registers in half and call AVX2 analogs from SSE 470 __m128i ni0 = _mm256_castsi256_si128( i0 ); 471 __m128i ni1 = _mm256_extractf128_si256( i0, 1); 472 __m128i ni2 = _mm256_castsi256_si128( i1 ); 473 __m128i ni3 = _mm256_extractf128_si256( i1, 1); 474 __m128i ni4 = _mm256_castsi256_si128( i2 ); 475 __m128i ni5 = _mm256_extractf128_si256( i2, 1); 476 __m128i ni6 = _mm256_castsi256_si128( i3 ); 477 __m128i ni7 = _mm256_extractf128_si256( i3, 1); 478 479 // Compute the sum of the quants and set y[i].s 480 const __m128i s0 = _mm_add_epi32(_mm_add_epi32(ni0, ni1), _mm_add_epi32(ni2, ni3)); 481 const __m128i s1 = _mm_add_epi32(_mm_add_epi32(ni4, ni5), _mm_add_epi32(ni6, ni7)); 482 y[i].s = GGML_CPU_FP32_TO_FP16(d * hsum_i32_4(_mm_add_epi32(s0, s1))); 483 484 // Convert int32 to int16 485 ni0 = _mm_packs_epi32( ni0, ni1 ); 486 ni2 = _mm_packs_epi32( ni2, ni3 ); 487 ni4 = _mm_packs_epi32( ni4, ni5 ); 488 ni6 = _mm_packs_epi32( ni6, ni7 ); 489 // Convert int16 to int8 490 ni0 = _mm_packs_epi16( ni0, ni2 ); 491 ni4 = _mm_packs_epi16( ni4, ni6 ); 492 493 _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0); 494 _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4); 495 #endif 496 } 497 #else 498 GGML_UNUSED(nb); 499 // scalar 500 quantize_row_q8_1_ref(x, y, k); 501 #endif 502 } 503 504 // placeholder implementation for Apple targets 505 void quantize_row_q8_K(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k) { 506 quantize_row_q8_K_ref(x, y, k); 507 } 508 509 //===================================== Dot products ================================= 510 511 // 512 // Helper functions 513 // 514 515 #if __AVX__ || __AVX2__ || __AVX512F__ 516 517 // shuffles to pick the required scales in dot products 518 static inline __m256i get_scale_shuffle_q3k(int i) { 519 static const uint8_t k_shuffle[128] = { 520 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 521 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 522 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11, 523 12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13, 14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15, 524 }; 525 return _mm256_loadu_si256((const __m256i*)k_shuffle + i); 526 } 527 static inline __m256i get_scale_shuffle_k4(int i) { 528 static const uint8_t k_shuffle[256] = { 529 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 530 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 531 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 532 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 533 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 534 10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11, 535 12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13, 536 14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15 537 }; 538 return _mm256_loadu_si256((const __m256i*)k_shuffle + i); 539 } 540 static inline __m128i get_scale_shuffle(int i) { 541 static const uint8_t k_shuffle[128] = { 542 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 543 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 544 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 545 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 546 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 547 10,10,10,10,10,10,10,10, 11,11,11,11,11,11,11,11, 548 12,12,12,12,12,12,12,12, 13,13,13,13,13,13,13,13, 549 14,14,14,14,14,14,14,14, 15,15,15,15,15,15,15,15 550 }; 551 return _mm_loadu_si128((const __m128i*)k_shuffle + i); 552 } 553 #endif 554 555 void ggml_vec_dot_q1_0_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { 556 const int qk = QK1_0; 557 const int nb = n / qk; 558 559 assert(n % qk == 0); 560 assert(nrc == 1); 561 UNUSED(nrc); 562 UNUSED(bx); 563 UNUSED(by); 564 UNUSED(bs); 565 566 const block_q1_0 * GGML_RESTRICT x = vx; 567 const block_q8_0 * GGML_RESTRICT y = vy; 568 569 #if defined(__AVX2__) 570 const __m256i ones_8 = _mm256_set1_epi8(1); 571 const __m256i ones_16 = _mm256_set1_epi16(1); 572 const __m256i byte_shuf = _mm256_setr_epi8( 573 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 574 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3); 575 const __m256i bit_masks = _mm256_setr_epi8( 576 1, 2, 4, 8, 16, 32, 64, (char) -128, 1, 2, 4, 8, 16, 32, 64, (char) -128, 577 1, 2, 4, 8, 16, 32, 64, (char) -128, 1, 2, 4, 8, 16, 32, 64, (char) -128); 578 const __m256i zero = _mm256_setzero_si256(); 579 __m256 acc = _mm256_setzero_ps(); 580 581 for (int ib = 0; ib < nb; ++ib) { 582 const float d0 = GGML_CPU_FP16_TO_FP32(x[ib].d); 583 const uint32_t * GGML_RESTRICT qs32 = (const uint32_t *) x[ib].qs; 584 const block_q8_0 * GGML_RESTRICT y_ptr = &y[ib * 4]; 585 586 __m256 acc_block; 587 { 588 const __m256i qy = _mm256_loadu_si256((const __m256i *) y_ptr[0].qs); 589 const __m256i sm = _mm256_cmpeq_epi8( 590 _mm256_and_si256(_mm256_shuffle_epi8(_mm256_set1_epi32((int) qs32[0]), byte_shuf), bit_masks), zero); 591 const __m256i sy = _mm256_sub_epi8(_mm256_xor_si256(qy, sm), sm); 592 const __m256i s32 = _mm256_madd_epi16(_mm256_maddubs_epi16(ones_8, sy), ones_16); 593 acc_block = _mm256_mul_ps(_mm256_set1_ps(GGML_CPU_FP16_TO_FP32(y_ptr[0].d)), _mm256_cvtepi32_ps(s32)); 594 } 595 for (int K = 1; K < 4; ++K) { 596 const __m256i qy = _mm256_loadu_si256((const __m256i *) y_ptr[K].qs); 597 const __m256i sm = _mm256_cmpeq_epi8( 598 _mm256_and_si256(_mm256_shuffle_epi8(_mm256_set1_epi32((int) qs32[K]), byte_shuf), bit_masks), zero); 599 const __m256i sy = _mm256_sub_epi8(_mm256_xor_si256(qy, sm), sm); 600 const __m256i s32 = _mm256_madd_epi16(_mm256_maddubs_epi16(ones_8, sy), ones_16); 601 acc_block = _mm256_fmadd_ps(_mm256_set1_ps(GGML_CPU_FP16_TO_FP32(y_ptr[K].d)), _mm256_cvtepi32_ps(s32), acc_block); 602 } 603 acc = _mm256_fmadd_ps(_mm256_set1_ps(d0), acc_block, acc); 604 } 605 606 *s = hsum_float_8(acc); 607 #elif defined(__AVX__) 608 const __m128i ones_8 = _mm_set1_epi8(1); 609 const __m128i ones_16 = _mm_set1_epi16(1); 610 const __m128i zero = _mm_setzero_si128(); 611 __m256 acc = _mm256_setzero_ps(); 612 613 for (int ib = 0; ib < nb; ++ib) { 614 const float d0 = GGML_CPU_FP16_TO_FP32(x[ib].d); 615 const block_q8_0 * GGML_RESTRICT y_ptr = &y[ib * 4]; 616 __m256 acc_block; 617 { 618 const __m256i bit_mask = bytes_from_bits_32(&x[ib].qs[0]); 619 const __m128i bit_mask_0 = _mm256_castsi256_si128(bit_mask); 620 const __m128i bit_mask_1 = _mm256_extractf128_si256(bit_mask, 1); 621 const __m128i qy_0 = _mm_loadu_si128((const __m128i *) &y_ptr[0].qs[0]); 622 const __m128i qy_1 = _mm_loadu_si128((const __m128i *) &y_ptr[0].qs[16]); 623 const __m128i sign_mask_0 = _mm_cmpeq_epi8(bit_mask_0, zero); 624 const __m128i sign_mask_1 = _mm_cmpeq_epi8(bit_mask_1, zero); 625 const __m128i sy_0 = _mm_sub_epi8(_mm_xor_si128(qy_0, sign_mask_0), sign_mask_0); 626 const __m128i sy_1 = _mm_sub_epi8(_mm_xor_si128(qy_1, sign_mask_1), sign_mask_1); 627 const __m128i sum16_0 = _mm_maddubs_epi16(ones_8, sy_0); 628 const __m128i sum16_1 = _mm_maddubs_epi16(ones_8, sy_1); 629 const __m128i sum32_0 = _mm_madd_epi16(sum16_0, ones_16); 630 const __m128i sum32_1 = _mm_madd_epi16(sum16_1, ones_16); 631 const __m256 q = _mm256_cvtepi32_ps(MM256_SET_M128I(sum32_1, sum32_0)); 632 acc_block = _mm256_mul_ps(_mm256_set1_ps(GGML_CPU_FP16_TO_FP32(y_ptr[0].d)), q); 633 } 634 for(int K = 1; K < 4; ++K) { 635 const __m256i bit_mask = bytes_from_bits_32(&x[ib].qs[(K) * 4]); 636 const __m128i bit_mask_0 = _mm256_castsi256_si128(bit_mask); 637 const __m128i bit_mask_1 = _mm256_extractf128_si256(bit_mask, 1); 638 const __m128i qy_0 = _mm_loadu_si128((const __m128i *) &y_ptr[(K)].qs[0]); 639 const __m128i qy_1 = _mm_loadu_si128((const __m128i *) &y_ptr[(K)].qs[16]); 640 const __m128i sign_mask_0 = _mm_cmpeq_epi8(bit_mask_0, zero); 641 const __m128i sign_mask_1 = _mm_cmpeq_epi8(bit_mask_1, zero); 642 const __m128i sy_0 = _mm_sub_epi8(_mm_xor_si128(qy_0, sign_mask_0), sign_mask_0); 643 const __m128i sy_1 = _mm_sub_epi8(_mm_xor_si128(qy_1, sign_mask_1), sign_mask_1); 644 const __m128i sum16_0 = _mm_maddubs_epi16(ones_8, sy_0); 645 const __m128i sum16_1 = _mm_maddubs_epi16(ones_8, sy_1); 646 const __m128i sum32_0 = _mm_madd_epi16(sum16_0, ones_16); 647 const __m128i sum32_1 = _mm_madd_epi16(sum16_1, ones_16); 648 const __m256 q = _mm256_cvtepi32_ps(MM256_SET_M128I(sum32_1, sum32_0)); 649 acc_block = _mm256_add_ps(acc_block, _mm256_mul_ps(_mm256_set1_ps(GGML_CPU_FP16_TO_FP32(y_ptr[(K)].d)), q)); 650 } 651 #undef Q1_AVX_BLOCK 652 653 acc = _mm256_add_ps(acc, _mm256_mul_ps(_mm256_set1_ps(d0), acc_block)); 654 } 655 656 *s = hsum_float_8(acc); 657 #elif defined(__SSSE3__) 658 const __m128i ones_8 = _mm_set1_epi8(1); 659 const __m128i ones_16 = _mm_set1_epi16(1); 660 const __m128i zero = _mm_setzero_si128(); 661 __m128 acc_0 = _mm_setzero_ps(); 662 __m128 acc_1 = _mm_setzero_ps(); 663 __m128 acc_2 = _mm_setzero_ps(); 664 __m128 acc_3 = _mm_setzero_ps(); 665 666 for (int ib = 0; ib < nb; ++ib) { 667 const __m128 d0 = _mm_set1_ps(GGML_CPU_FP16_TO_FP32(x[ib].d)); 668 const block_q8_0 * GGML_RESTRICT y_ptr = &y[ib * 4]; 669 670 #define Q1_SSSE3_BLOCK(QS_OFF, Y_IDX, ACC) \ 671 { \ 672 const __m128i bit_mask_0 = bytes_from_bits_16(&x[ib].qs[(QS_OFF) + 0]); \ 673 const __m128i bit_mask_1 = bytes_from_bits_16(&x[ib].qs[(QS_OFF) + 2]); \ 674 const __m128i qy_0 = _mm_loadu_si128((const __m128i *) &y_ptr[(Y_IDX)].qs[0]); \ 675 const __m128i qy_1 = _mm_loadu_si128((const __m128i *) &y_ptr[(Y_IDX)].qs[16]); \ 676 const __m128i sign_mask_0 = _mm_cmpeq_epi8(bit_mask_0, zero); \ 677 const __m128i sign_mask_1 = _mm_cmpeq_epi8(bit_mask_1, zero); \ 678 const __m128i sy_0 = _mm_sub_epi8(_mm_xor_si128(qy_0, sign_mask_0), sign_mask_0); \ 679 const __m128i sy_1 = _mm_sub_epi8(_mm_xor_si128(qy_1, sign_mask_1), sign_mask_1); \ 680 const __m128i sum_0 = _mm_madd_epi16(_mm_maddubs_epi16(ones_8, sy_0), ones_16); \ 681 const __m128i sum_1 = _mm_madd_epi16(_mm_maddubs_epi16(ones_8, sy_1), ones_16); \ 682 const __m128 q = _mm_cvtepi32_ps(_mm_add_epi32(sum_0, sum_1)); \ 683 (ACC) = _mm_add_ps((ACC), _mm_mul_ps(_mm_mul_ps(d0, _mm_set1_ps(GGML_CPU_FP16_TO_FP32(y_ptr[(Y_IDX)].d))), q)); \ 684 } 685 Q1_SSSE3_BLOCK(0, 0, acc_0) 686 Q1_SSSE3_BLOCK(4, 1, acc_1) 687 Q1_SSSE3_BLOCK(8, 2, acc_2) 688 Q1_SSSE3_BLOCK(12, 3, acc_3) 689 #undef Q1_SSSE3_BLOCK 690 } 691 692 *s = hsum_float_4x4(acc_0, acc_1, acc_2, acc_3); 693 #else 694 UNUSED(nb); 695 UNUSED(x); 696 UNUSED(y); 697 ggml_vec_dot_q1_0_q8_0_generic(n, s, bs, vx, bx, vy, by, nrc); 698 #endif 699 } 700 701 #if defined(__AVX2__) 702 // four consecutive Q2_0 weights per byte: byte b becomes b & 3, (b >> 2) & 3, (b >> 4) & 3, (b >> 6) & 3 (OR, never add: the overlapping copies only touch masked-off bits) 703 static inline __m256i q2_0_unpack_32_avx2(const uint8_t * qs) { 704 __m256i v = _mm256_cvtepu8_epi32(_mm_loadl_epi64((const __m128i *) qs)); 705 v = _mm256_or_si256(v, _mm256_slli_epi32(v, 12)); 706 v = _mm256_or_si256(v, _mm256_slli_epi32(v, 6)); 707 return _mm256_and_si256(v, _mm256_set1_epi32(0x03030303)); 708 } 709 710 // 32 weights against one Q8_0 block: sum((v - 1) * y) = sum(v * y) - sum(y), exact for every int8 y 711 static inline __m256 q2_0_dot_32_avx2(const uint8_t * qs, const int8_t * qy) { 712 const __m256i v = q2_0_unpack_32_avx2(qs); 713 const __m256i y = _mm256_loadu_si256((const __m256i *) qy); 714 const __m256i p = _mm256_sub_epi16(_mm256_maddubs_epi16(v, y), _mm256_maddubs_epi16(_mm256_set1_epi8(1), y)); 715 return _mm256_cvtepi32_ps(_mm256_madd_epi16(p, _mm256_set1_epi16(1))); 716 } 717 718 static inline __m256 q2_0_acc_block_avx2(const block_q2_0 * x, const block_q8_0 * y, __m256 acc) { 719 const float dx = GGML_CPU_FP16_TO_FP32(x->d); 720 const __m256 d0 = _mm256_set1_ps(dx * GGML_CPU_FP16_TO_FP32(y[0].d)); 721 const __m256 d1 = _mm256_set1_ps(dx * GGML_CPU_FP16_TO_FP32(y[1].d)); 722 const __m256 p0 = q2_0_dot_32_avx2(x->qs + 0, y[0].qs); 723 const __m256 p1 = q2_0_dot_32_avx2(x->qs + 8, y[1].qs); 724 #if defined(__FMA__) 725 acc = _mm256_fmadd_ps(d0, p0, acc); 726 return _mm256_fmadd_ps(d1, p1, acc); 727 #else 728 acc = _mm256_add_ps(acc, _mm256_mul_ps(d0, p0)); 729 return _mm256_add_ps(acc, _mm256_mul_ps(d1, p1)); 730 #endif 731 } 732 #endif 733 734 void ggml_vec_dot_q2_0_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { 735 #if defined(__AVX2__) 736 assert(n % QK2_0 == 0); 737 assert(nrc == 1); 738 UNUSED(nrc); 739 UNUSED(bx); 740 UNUSED(by); 741 UNUSED(bs); 742 743 const block_q2_0 * GGML_RESTRICT x = vx; 744 const block_q8_0 * GGML_RESTRICT y = vy; 745 const int nb = n / QK2_0; 746 747 // two Q2_0 blocks per step, each into its own accumulator (two FMA chains) 748 __m256 acc0 = _mm256_setzero_ps(); 749 __m256 acc1 = _mm256_setzero_ps(); 750 int i = 0; 751 for (; i + 1 < nb; i += 2) { 752 acc0 = q2_0_acc_block_avx2(x + i + 0, y + 2*i + 0, acc0); 753 acc1 = q2_0_acc_block_avx2(x + i + 1, y + 2*i + 2, acc1); 754 } 755 if (i < nb) { 756 acc0 = q2_0_acc_block_avx2(x + i, y + 2*i, acc0); 757 } 758 759 *s = hsum_float_8(_mm256_add_ps(acc0, acc1)); 760 #else 761 ggml_vec_dot_q2_0_q8_0_generic(n, s, bs, vx, bx, vy, by, nrc); 762 #endif 763 } 764 765 void ggml_vec_dot_q4_0_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { 766 const int qk = QK8_0; 767 const int nb = n / qk; 768 769 assert(n % qk == 0); 770 assert(nrc == 1); 771 UNUSED(nrc); 772 UNUSED(bx); 773 UNUSED(by); 774 UNUSED(bs); 775 776 const block_q4_0 * GGML_RESTRICT x = vx; 777 const block_q8_0 * GGML_RESTRICT y = vy; 778 779 int ib = 0; 780 float sumf = 0; 781 782 #if defined(__AVX2__) 783 // Initialize accumulator with zeros 784 __m256 acc = _mm256_setzero_ps(); 785 786 // Main loop 787 for (; ib < nb; ++ib) { 788 /* Compute combined scale for the block */ 789 const __m256 d = _mm256_set1_ps( GGML_CPU_FP16_TO_FP32(x[ib].d) * GGML_CPU_FP16_TO_FP32(y[ib].d) ); 790 791 __m256i qx = bytes_from_nibbles_32(x[ib].qs); 792 793 // Now we have a vector with bytes in [ 0 .. 15 ] interval. Offset them into [ -8 .. +7 ] interval. 794 const __m256i off = _mm256_set1_epi8( 8 ); 795 qx = _mm256_sub_epi8( qx, off ); 796 797 __m256i qy = _mm256_loadu_si256((const __m256i *)y[ib].qs); 798 799 const __m256 q = mul_sum_i8_pairs_float(qx, qy); 800 801 /* Multiply q with scale and accumulate */ 802 acc = _mm256_fmadd_ps( d, q, acc ); 803 } 804 805 sumf = hsum_float_8(acc); 806 #elif defined(__AVX__) 807 __m256 accum = _mm256_setzero_ps(); 808 for (; ib + 1 < nb; ib += 2) { 809 const __m128i q4bits_1 = _mm_loadu_si128((const __m128i *)x[ib + 0].qs); 810 const __m128i q4bits_2 = _mm_loadu_si128((const __m128i *)x[ib + 1].qs); 811 const __m128i q8b_1_0 = _mm_loadu_si128((const __m128i *)y[ib + 0].qs); 812 const __m128i q8b_1_1 = _mm_loadu_si128((const __m128i *)y[ib + 0].qs + 1); 813 const __m128i q8b_2_0 = _mm_loadu_si128((const __m128i *)y[ib + 1].qs); 814 const __m128i q8b_2_1 = _mm_loadu_si128((const __m128i *)y[ib + 1].qs + 1); 815 816 const __m128i q4b_1_0 = _mm_sub_epi8(_mm_and_si128(_mm_set1_epi8(15), q4bits_1), _mm_set1_epi8(8)); 817 const __m128i q4b_1_1 = _mm_sub_epi8(_mm_and_si128(_mm_set1_epi8(15), _mm_srli_epi16(q4bits_1, 4)), _mm_set1_epi8(8)); 818 const __m128i q4b_2_0 = _mm_sub_epi8(_mm_and_si128(_mm_set1_epi8(15), q4bits_2), _mm_set1_epi8(8)); 819 const __m128i q4b_2_1 = _mm_sub_epi8(_mm_and_si128(_mm_set1_epi8(15), _mm_srli_epi16(q4bits_2, 4)), _mm_set1_epi8(8)); 820 821 const __m128i p16_1_0 = mul_add_epi8_sse(q4b_1_0, q8b_1_0); 822 const __m128i p16_1_1 = mul_add_epi8_sse(q4b_1_1, q8b_1_1); 823 const __m128i p16_2_0 = mul_add_epi8_sse(q4b_2_0, q8b_2_0); 824 const __m128i p16_2_1 = mul_add_epi8_sse(q4b_2_1, q8b_2_1); 825 const __m128i p_1 = _mm_add_epi16(p16_1_0, p16_1_1); 826 const __m128i p_2 = _mm_add_epi16(p16_2_0, p16_2_1); 827 const __m256 p = sum_i16_pairs_float(p_2, p_1); 828 829 const __m256 deltas = quad_fp16_delta_float(x[ib].d, y[ib].d, x[ib + 1].d, y[ib + 1].d); 830 accum = _mm256_add_ps(_mm256_mul_ps(deltas, p), accum); 831 } 832 833 sumf = hsum_float_8(accum); 834 #elif defined(__SSSE3__) 835 // set constants 836 const __m128i lowMask = _mm_set1_epi8(0xF); 837 const __m128i off = _mm_set1_epi8(8); 838 839 // Initialize accumulator with zeros 840 __m128 acc_0 = _mm_setzero_ps(); 841 __m128 acc_1 = _mm_setzero_ps(); 842 __m128 acc_2 = _mm_setzero_ps(); 843 __m128 acc_3 = _mm_setzero_ps(); 844 845 for (; ib + 1 < nb; ib += 2) { 846 _mm_prefetch(&x[ib] + sizeof(block_q4_0), _MM_HINT_T0); 847 _mm_prefetch(&y[ib] + sizeof(block_q8_0), _MM_HINT_T0); 848 849 // Compute combined scale for the block 0 and 1 850 const __m128 d_0_1 = _mm_set1_ps( GGML_CPU_FP16_TO_FP32(x[ib].d) * GGML_CPU_FP16_TO_FP32(y[ib].d) ); 851 852 const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[ib].qs); 853 854 __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1); 855 __m128i by_0 = _mm_loadu_si128((const __m128i *)y[ib].qs); 856 bx_0 = _mm_sub_epi8(bx_0, off); 857 const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0); 858 859 __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4)); 860 __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[ib].qs + 16)); 861 bx_1 = _mm_sub_epi8(bx_1, off); 862 const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1); 863 864 _mm_prefetch(&x[ib] + 2 * sizeof(block_q4_0), _MM_HINT_T0); 865 _mm_prefetch(&y[ib] + 2 * sizeof(block_q8_0), _MM_HINT_T0); 866 867 // Compute combined scale for the block 2 and 3 868 const __m128 d_2_3 = _mm_set1_ps( GGML_CPU_FP16_TO_FP32(x[ib + 1].d) * GGML_CPU_FP16_TO_FP32(y[ib + 1].d) ); 869 870 const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[ib + 1].qs); 871 872 __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3); 873 __m128i by_2 = _mm_loadu_si128((const __m128i *)y[ib + 1].qs); 874 bx_2 = _mm_sub_epi8(bx_2, off); 875 const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2); 876 877 __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4)); 878 __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[ib + 1].qs + 16)); 879 bx_3 = _mm_sub_epi8(bx_3, off); 880 const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3); 881 882 // Convert int32_t to float 883 __m128 p0 = _mm_cvtepi32_ps(i32_0); 884 __m128 p1 = _mm_cvtepi32_ps(i32_1); 885 __m128 p2 = _mm_cvtepi32_ps(i32_2); 886 __m128 p3 = _mm_cvtepi32_ps(i32_3); 887 888 // Apply the scale 889 __m128 p0_d = _mm_mul_ps( d_0_1, p0 ); 890 __m128 p1_d = _mm_mul_ps( d_0_1, p1 ); 891 __m128 p2_d = _mm_mul_ps( d_2_3, p2 ); 892 __m128 p3_d = _mm_mul_ps( d_2_3, p3 ); 893 894 // Acummulate 895 acc_0 = _mm_add_ps(p0_d, acc_0); 896 acc_1 = _mm_add_ps(p1_d, acc_1); 897 acc_2 = _mm_add_ps(p2_d, acc_2); 898 acc_3 = _mm_add_ps(p3_d, acc_3); 899 } 900 901 sumf = hsum_float_4x4(acc_0, acc_1, acc_2, acc_3); 902 903 #endif 904 for (; ib < nb; ++ib) { 905 int sumi0 = 0; 906 int sumi1 = 0; 907 908 for (int j = 0; j < qk/2; ++j) { 909 const int v0 = (x[ib].qs[j] & 0x0F) - 8; 910 const int v1 = (x[ib].qs[j] >> 4) - 8; 911 912 sumi0 += (v0 * y[ib].qs[j]); 913 sumi1 += (v1 * y[ib].qs[j + qk/2]); 914 } 915 916 int sumi = sumi0 + sumi1; 917 sumf += sumi*GGML_CPU_FP16_TO_FP32(x[ib].d)*GGML_CPU_FP16_TO_FP32(y[ib].d); 918 } 919 920 *s = sumf; 921 } 922 923 void ggml_vec_dot_q4_1_q8_1(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { 924 const int qk = QK8_1; 925 const int nb = n / qk; 926 927 assert(n % qk == 0); 928 assert(nrc == 1); 929 UNUSED(nrc); 930 UNUSED(bx); 931 UNUSED(by); 932 UNUSED(bs); 933 934 const block_q4_1 * GGML_RESTRICT x = vx; 935 const block_q8_1 * GGML_RESTRICT y = vy; 936 937 int ib = 0; 938 939 #if defined(__AVX2__) || defined(__AVX__) 940 // Initialize accumulator with zeros 941 __m256 acc = _mm256_setzero_ps(); 942 943 float summs = 0; 944 945 // Main loop 946 for (; ib < nb; ++ib) { 947 const float d0 = GGML_CPU_FP16_TO_FP32(x[ib].d); 948 const float d1 = GGML_CPU_FP16_TO_FP32(y[ib].d); 949 950 summs += GGML_CPU_FP16_TO_FP32(x[ib].m) * GGML_CPU_FP16_TO_FP32(y[ib].s); 951 952 const __m256 d0v = _mm256_set1_ps( d0 ); 953 const __m256 d1v = _mm256_set1_ps( d1 ); 954 955 // Compute combined scales 956 const __m256 d0d1 = _mm256_mul_ps( d0v, d1v ); 957 958 // Load 16 bytes, and unpack 4 bit fields into bytes, making 32 bytes 959 const __m256i qx = bytes_from_nibbles_32(x[ib].qs); 960 const __m256i qy = _mm256_loadu_si256( (const __m256i *)y[ib].qs ); 961 962 const __m256 xy = mul_sum_us8_pairs_float(qx, qy); 963 964 // Accumulate d0*d1*x*y 965 #if defined(__AVX2__) 966 acc = _mm256_fmadd_ps( d0d1, xy, acc ); 967 #else 968 acc = _mm256_add_ps( _mm256_mul_ps( d0d1, xy ), acc ); 969 #endif 970 } 971 972 *s = hsum_float_8(acc) + summs; 973 #else 974 UNUSED(nb); 975 UNUSED(x); 976 UNUSED(y); 977 UNUSED(ib); 978 ggml_vec_dot_q4_1_q8_1_generic(n, s, bs, vx, bx, vy, by, nrc); 979 #endif 980 } 981 982 void ggml_vec_dot_mxfp4_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { 983 assert(nrc == 1); 984 UNUSED(nrc); 985 UNUSED(bx); 986 UNUSED(by); 987 UNUSED(bs); 988 assert(n % QK_MXFP4 == 0); 989 static_assert(QK_MXFP4 == QK8_0, "QK_MXFP4 and QK8_0 must be the same"); 990 991 const block_mxfp4 * GGML_RESTRICT x = vx; 992 const block_q8_0 * GGML_RESTRICT y = vy; 993 994 const int nb = n / QK_MXFP4; 995 996 int ib = 0; 997 float sumf = 0; 998 999 #if defined __AVX2__ 1000 1001 const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_fp4); 1002 const __m128i m4b = _mm_set1_epi8(0x0f); 1003 const __m256i mone = _mm256_set1_epi16(1); 1004 1005 __m256 accum1 = _mm256_setzero_ps(); 1006 __m256 accum2 = _mm256_setzero_ps(); 1007 1008 for (; ib + 1 < nb; ib += 2) { 1009 const __m128i q4bits_1 = _mm_loadu_si128((const __m128i*)x[ib + 0].qs); 1010 const __m128i q4bits_2 = _mm_loadu_si128((const __m128i*)x[ib + 1].qs); 1011 const __m256i q8b_1 = _mm256_loadu_si256((const __m256i *)y[ib + 0].qs); 1012 const __m256i q8b_2 = _mm256_loadu_si256((const __m256i *)y[ib + 1].qs); 1013 const __m256i q4b_1 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)), 1014 _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b))); 1015 const __m256i q4b_2 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)), 1016 _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b))); 1017 const __m256i p16_1 = mul_add_epi8(q4b_1, q8b_1); 1018 const __m256i p16_2 = mul_add_epi8(q4b_2, q8b_2); 1019 const __m256i p_1 = _mm256_madd_epi16(p16_1, mone); 1020 const __m256i p_2 = _mm256_madd_epi16(p16_2, mone); 1021 const __m256 scale0 = _mm256_set1_ps(GGML_CPU_FP16_TO_FP32(y[ib + 0].d)*GGML_CPU_E8M0_TO_FP32_HALF(x[ib + 0].e)); 1022 const __m256 scale1 = _mm256_set1_ps(GGML_CPU_FP16_TO_FP32(y[ib + 1].d)*GGML_CPU_E8M0_TO_FP32_HALF(x[ib + 1].e)); 1023 accum1 = _mm256_fmadd_ps(scale0, _mm256_cvtepi32_ps(p_1), accum1); 1024 accum2 = _mm256_fmadd_ps(scale1, _mm256_cvtepi32_ps(p_2), accum2); 1025 } 1026 1027 sumf = hsum_float_8(_mm256_add_ps(accum1, accum2)); 1028 1029 #elif defined __AVX__ 1030 const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_fp4); 1031 const __m128i m4b = _mm_set1_epi8(0x0f); 1032 1033 __m256 accum = _mm256_setzero_ps(); 1034 for (; ib + 1 < nb; ib += 2) { 1035 const __m128i q4bits_1 = _mm_loadu_si128((const __m128i *)x[ib + 0].qs); 1036 const __m128i q4bits_2 = _mm_loadu_si128((const __m128i *)x[ib + 1].qs); 1037 const __m128i q8b_1_0 = _mm_loadu_si128((const __m128i *)y[ib + 0].qs); 1038 const __m128i q8b_1_1 = _mm_loadu_si128((const __m128i *)y[ib + 0].qs + 1); 1039 const __m128i q8b_2_0 = _mm_loadu_si128((const __m128i *)y[ib + 1].qs); 1040 const __m128i q8b_2_1 = _mm_loadu_si128((const __m128i *)y[ib + 1].qs + 1); 1041 1042 const __m128i q4b_1_0 = _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b)); 1043 const __m128i q4b_1_1 = _mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)); 1044 const __m128i q4b_2_0 = _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b)); 1045 const __m128i q4b_2_1 = _mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)); 1046 1047 const __m256 p = mul_sum_i8_quad_float(q4b_1_0, q4b_1_1, q4b_2_0, q4b_2_1, q8b_1_0, q8b_1_1, q8b_2_0, q8b_2_1); 1048 const __m256 deltas = quad_mx_delta_float(x[ib].e, y[ib].d, x[ib + 1].e, y[ib + 1].d); 1049 accum = _mm256_add_ps(_mm256_mul_ps(deltas, p), accum); 1050 } 1051 1052 sumf = hsum_float_8(accum); 1053 1054 #endif 1055 for (; ib < nb; ++ib) { 1056 const float d = GGML_CPU_FP16_TO_FP32(y[ib].d)*GGML_CPU_E8M0_TO_FP32_HALF(x[ib].e); 1057 int sumi1 = 0; 1058 int sumi2 = 0; 1059 for (int j = 0; j < QK_MXFP4/2; ++j) { 1060 sumi1 += y[ib].qs[j + 0] * kvalues_fp4[x[ib].qs[j] & 0xf]; 1061 sumi2 += y[ib].qs[j + QK_MXFP4/2] * kvalues_fp4[x[ib].qs[j] >> 4]; 1062 } 1063 sumf += d * (sumi1 + sumi2); 1064 } 1065 *s = sumf; 1066 } 1067 1068 void ggml_vec_dot_nvfp4_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { 1069 assert(nrc == 1); 1070 UNUSED(nrc); 1071 UNUSED(bx); 1072 UNUSED(by); 1073 UNUSED(bs); 1074 assert(n % QK_NVFP4 == 0); 1075 1076 const block_nvfp4 * GGML_RESTRICT x = vx; 1077 const block_q8_0 * GGML_RESTRICT y = vy; 1078 1079 const int nb = n / QK_NVFP4; 1080 int ib = 0; 1081 float sumf = 0; 1082 1083 #if defined(__AVX2__) 1084 1085 const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_fp4); 1086 const __m128i m4b = _mm_set1_epi8(0x0f); 1087 const __m256i mone = _mm256_set1_epi16(1); 1088 1089 __m256 accum = _mm256_setzero_ps(); 1090 for(; ib < nb; ib++){ 1091 1092 const __m128i q4bits_01 = _mm_loadu_si128((const __m128i *)(x[ib].qs + 0)); 1093 const __m128i q4bits_23 = _mm_loadu_si128((const __m128i *)(x[ib].qs + 16)); 1094 1095 const __m256i q8_01 = _mm256_loadu_si256((const __m256i *)y[2*ib + 0].qs); 1096 const __m256i q8_23 = _mm256_loadu_si256((const __m256i *)y[2*ib + 1].qs); 1097 1098 const __m128i q4_01_lo = _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_01, m4b)); 1099 const __m128i q4_01_hi = _mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_01, 4), m4b)); 1100 const __m128i q4_23_lo = _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_23, m4b)); 1101 const __m128i q4_23_hi = _mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_23, 4), m4b)); 1102 1103 //reordering 1104 const __m256i q4_01 = MM256_SET_M128I(_mm_unpackhi_epi64(q4_01_lo,q4_01_hi), _mm_unpacklo_epi64(q4_01_lo,q4_01_hi)); 1105 const __m256i q4_23 = MM256_SET_M128I(_mm_unpackhi_epi64(q4_23_lo,q4_23_hi),_mm_unpacklo_epi64(q4_23_lo,q4_23_hi)); 1106 1107 const __m256i p01 = mul_add_epi8(q4_01,q8_01); 1108 const __m256i p_1 = _mm256_madd_epi16(p01, mone); 1109 1110 const __m256i p23 = mul_add_epi8(q4_23,q8_23); 1111 const __m256i p_2 = _mm256_madd_epi16(p23, mone); 1112 1113 const float dy0 = GGML_CPU_FP16_TO_FP32(y[2*ib].d); 1114 const float dy1 = GGML_CPU_FP16_TO_FP32(y[2*ib+1].d); 1115 1116 const float s0 = GGML_CPU_UE4M3_TO_FP32(x[ib].d[0]) * dy0; 1117 const float s1 = GGML_CPU_UE4M3_TO_FP32(x[ib].d[1]) * dy0; 1118 const float s2 = GGML_CPU_UE4M3_TO_FP32(x[ib].d[2]) * dy1; 1119 const float s3 = GGML_CPU_UE4M3_TO_FP32(x[ib].d[3]) * dy1; 1120 1121 const __m256 scales01 = _mm256_set_m128(_mm_set1_ps(s1), _mm_set1_ps(s0)); 1122 const __m256 scales23 = _mm256_set_m128(_mm_set1_ps(s3), _mm_set1_ps(s2)); 1123 1124 accum = _mm256_fmadd_ps(scales01, _mm256_cvtepi32_ps(p_1), accum); 1125 accum = _mm256_fmadd_ps(scales23, _mm256_cvtepi32_ps(p_2), accum); 1126 } 1127 sumf = hsum_float_8(accum); 1128 1129 #elif defined(__AVX__) 1130 1131 const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_fp4); 1132 const __m128i m4b = _mm_set1_epi8(0x0f); 1133 1134 __m256 accum = _mm256_setzero_ps(); 1135 for(; ib < nb; ib++){ 1136 1137 const __m128i q4bits_01 = _mm_loadu_si128((const __m128i *)(x[ib].qs + 0)); 1138 const __m128i q4bits_23 = _mm_loadu_si128((const __m128i *)(x[ib].qs + 16)); 1139 1140 const __m128i q8_0 = _mm_loadu_si128((const __m128i *)(y[2*ib + 0].qs + 0)); 1141 const __m128i q8_1 = _mm_loadu_si128((const __m128i *)(y[2*ib + 0].qs + 16)); 1142 const __m128i q8_2 = _mm_loadu_si128((const __m128i *)(y[2*ib + 1].qs + 0)); 1143 const __m128i q8_3 = _mm_loadu_si128((const __m128i *)(y[2*ib + 1].qs + 16)); 1144 1145 const __m128i q4_01_lo = _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_01, m4b)); 1146 const __m128i q4_01_hi = _mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_01, 4), m4b)); 1147 const __m128i q4_23_lo = _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_23, m4b)); 1148 const __m128i q4_23_hi = _mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_23, 4), m4b)); 1149 1150 const __m128i q4_0 = _mm_unpacklo_epi64(q4_01_lo, q4_01_hi); 1151 const __m128i q4_1 = _mm_unpackhi_epi64(q4_01_lo, q4_01_hi); 1152 const __m128i q4_2 = _mm_unpacklo_epi64(q4_23_lo, q4_23_hi); 1153 const __m128i q4_3 = _mm_unpackhi_epi64(q4_23_lo, q4_23_hi); 1154 1155 const __m128i p0_i32 = mul_sum_i8_pairs(q4_0, q8_0); 1156 const __m128i p1_i32 = mul_sum_i8_pairs(q4_1, q8_1); 1157 const __m128i p2_i32 = mul_sum_i8_pairs(q4_2, q8_2); 1158 const __m128i p3_i32 = mul_sum_i8_pairs(q4_3, q8_3); 1159 1160 const __m128 p0 = _mm_cvtepi32_ps(p0_i32); 1161 const __m128 p1 = _mm_cvtepi32_ps(p1_i32); 1162 const __m128 p2 = _mm_cvtepi32_ps(p2_i32); 1163 const __m128 p3 = _mm_cvtepi32_ps(p3_i32); 1164 1165 const __m256 p01 = _mm256_set_m128(p1, p0); 1166 const __m256 p23 = _mm256_set_m128(p3, p2); 1167 1168 const float dy0 = GGML_CPU_FP16_TO_FP32(y[2*ib].d); 1169 const float dy1 = GGML_CPU_FP16_TO_FP32(y[2*ib+1].d); 1170 1171 const float s0 = GGML_CPU_UE4M3_TO_FP32(x[ib].d[0]) * dy0; 1172 const float s1 = GGML_CPU_UE4M3_TO_FP32(x[ib].d[1]) * dy0; 1173 const float s2 = GGML_CPU_UE4M3_TO_FP32(x[ib].d[2]) * dy1; 1174 const float s3 = GGML_CPU_UE4M3_TO_FP32(x[ib].d[3]) * dy1; 1175 1176 const __m256 scales01 = _mm256_set_m128(_mm_set1_ps(s1), _mm_set1_ps(s0)); 1177 const __m256 scales23 = _mm256_set_m128(_mm_set1_ps(s3), _mm_set1_ps(s2)); 1178 1179 accum = _mm256_add_ps(accum, _mm256_mul_ps(p01, scales01)); 1180 accum = _mm256_add_ps(accum, _mm256_mul_ps(p23, scales23)); 1181 } 1182 sumf = hsum_float_8(accum); 1183 1184 #endif 1185 1186 for (;ib < nb; ++ib) { 1187 for (int s_idx = 0; s_idx < 4; ++s_idx) { 1188 const float d = GGML_CPU_UE4M3_TO_FP32(x[ib].d[s_idx]); 1189 const int q8_block = s_idx / 2; 1190 const int q8_off = (s_idx % 2) * QK_NVFP4_SUB; 1191 const float dy = GGML_CPU_FP16_TO_FP32(y[2*ib + q8_block].d); 1192 1193 int sumi_lo = 0, sumi_hi = 0; 1194 for (int j = 0; j < QK_NVFP4_SUB/2; ++j) { 1195 const uint8_t qv = x[ib].qs[s_idx*(QK_NVFP4_SUB/2) + j]; 1196 sumi_lo += y[2*ib + q8_block].qs[q8_off + j + 0] * kvalues_fp4[qv & 0xf]; 1197 sumi_hi += y[2*ib + q8_block].qs[q8_off + j + QK_NVFP4_SUB/2] * kvalues_fp4[qv >> 4]; 1198 } 1199 1200 sumf += dy * d * (sumi_lo + sumi_hi); 1201 } 1202 } 1203 *s = sumf; 1204 } 1205 1206 void ggml_vec_dot_q5_0_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { 1207 const int qk = QK8_0; 1208 const int nb = n / qk; 1209 1210 int ib = 0; 1211 1212 assert(n % qk == 0); 1213 assert(qk == QK5_0); 1214 assert(nrc == 1); 1215 UNUSED(nrc); 1216 UNUSED(bx); 1217 UNUSED(by); 1218 UNUSED(bs); 1219 1220 const block_q5_0 * GGML_RESTRICT x = vx; 1221 const block_q8_0 * GGML_RESTRICT y = vy; 1222 1223 #if defined(__AVX2__) 1224 // Initialize accumulator with zeros 1225 __m256 acc = _mm256_setzero_ps(); 1226 1227 // Main loop 1228 for (; ib < nb; ++ib) { 1229 /* Compute combined scale for the block */ 1230 const __m256 d = _mm256_set1_ps(GGML_CPU_FP16_TO_FP32(x[ib].d) * GGML_CPU_FP16_TO_FP32(y[ib].d)); 1231 1232 __m256i qx = bytes_from_nibbles_32(x[ib].qs); 1233 __m256i bxhi = bytes_from_bits_32(x[ib].qh); 1234 bxhi = _mm256_andnot_si256(bxhi, _mm256_set1_epi8((char)0xF0)); 1235 qx = _mm256_or_si256(qx, bxhi); 1236 1237 __m256i qy = _mm256_loadu_si256((const __m256i *)y[ib].qs); 1238 1239 const __m256 q = mul_sum_i8_pairs_float(qx, qy); 1240 1241 /* Multiply q with scale and accumulate */ 1242 acc = _mm256_fmadd_ps(d, q, acc); 1243 } 1244 1245 *s = hsum_float_8(acc); 1246 #elif defined(__AVX__) 1247 // Initialize accumulator with zeros 1248 __m256 acc = _mm256_setzero_ps(); 1249 __m128i mask = _mm_set1_epi8((char)0xF0); 1250 1251 // Main loop 1252 for (; ib < nb; ++ib) { 1253 /* Compute combined scale for the block */ 1254 const __m256 d = _mm256_set1_ps(GGML_CPU_FP16_TO_FP32(x[ib].d) * GGML_CPU_FP16_TO_FP32(y[ib].d)); 1255 1256 __m256i bx_0 = bytes_from_nibbles_32(x[ib].qs); 1257 const __m256i bxhi = bytes_from_bits_32(x[ib].qh); 1258 __m128i bxhil = _mm256_castsi256_si128(bxhi); 1259 __m128i bxhih = _mm256_extractf128_si256(bxhi, 1); 1260 bxhil = _mm_andnot_si128(bxhil, mask); 1261 bxhih = _mm_andnot_si128(bxhih, mask); 1262 __m128i bxl = _mm256_castsi256_si128(bx_0); 1263 __m128i bxh = _mm256_extractf128_si256(bx_0, 1); 1264 bxl = _mm_or_si128(bxl, bxhil); 1265 bxh = _mm_or_si128(bxh, bxhih); 1266 bx_0 = MM256_SET_M128I(bxh, bxl); 1267 1268 const __m256i by_0 = _mm256_loadu_si256((const __m256i *)y[ib].qs); 1269 1270 const __m256 q = mul_sum_i8_pairs_float(bx_0, by_0); 1271 1272 /* Multiply q with scale and accumulate */ 1273 acc = _mm256_add_ps(_mm256_mul_ps(d, q), acc); 1274 } 1275 1276 *s = hsum_float_8(acc); 1277 #else 1278 UNUSED(nb); 1279 UNUSED(ib); 1280 UNUSED(x); 1281 UNUSED(y); 1282 ggml_vec_dot_q5_0_q8_0_generic(n, s, bs, vx, bx, vy, by, nrc); 1283 #endif 1284 } 1285 1286 void ggml_vec_dot_q5_1_q8_1(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { 1287 const int qk = QK8_1; 1288 const int nb = n / qk; 1289 1290 int ib = 0; 1291 1292 assert(n % qk == 0); 1293 assert(qk == QK5_1); 1294 assert(nrc == 1); 1295 UNUSED(nrc); 1296 UNUSED(bx); 1297 UNUSED(by); 1298 UNUSED(bs); 1299 1300 const block_q5_1 * GGML_RESTRICT x = vx; 1301 const block_q8_1 * GGML_RESTRICT y = vy; 1302 1303 #if defined(__AVX2__) 1304 // Initialize accumulator with zeros 1305 __m256 acc = _mm256_setzero_ps(); 1306 1307 float summs = 0.0f; 1308 1309 // Main loop 1310 for (; ib < nb; ++ib) { 1311 const __m256 dx = _mm256_set1_ps(GGML_CPU_FP16_TO_FP32(x[ib].d)); 1312 1313 summs += GGML_CPU_FP16_TO_FP32(x[ib].m) * GGML_CPU_FP16_TO_FP32(y[ib].s); 1314 1315 __m256i qx = bytes_from_nibbles_32(x[ib].qs); 1316 __m256i bxhi = bytes_from_bits_32(x[ib].qh); 1317 bxhi = _mm256_and_si256(bxhi, _mm256_set1_epi8(0x10)); 1318 qx = _mm256_or_si256(qx, bxhi); 1319 1320 const __m256 dy = _mm256_set1_ps(GGML_CPU_FP16_TO_FP32(y[ib].d)); 1321 const __m256i qy = _mm256_loadu_si256((const __m256i *)y[ib].qs); 1322 1323 const __m256 q = mul_sum_us8_pairs_float(qx, qy); 1324 1325 acc = _mm256_fmadd_ps(q, _mm256_mul_ps(dx, dy), acc); 1326 } 1327 1328 *s = hsum_float_8(acc) + summs; 1329 #elif defined(__AVX__) 1330 // Initialize accumulator with zeros 1331 __m256 acc = _mm256_setzero_ps(); 1332 __m128i mask = _mm_set1_epi8(0x10); 1333 1334 float summs = 0.0f; 1335 1336 // Main loop 1337 for (; ib < nb; ++ib) { 1338 const __m256 dx = _mm256_set1_ps(GGML_CPU_FP16_TO_FP32(x[ib].d)); 1339 1340 summs += GGML_CPU_FP16_TO_FP32(x[ib].m) * GGML_CPU_FP16_TO_FP32(y[ib].s); 1341 1342 __m256i bx_0 = bytes_from_nibbles_32(x[ib].qs); 1343 const __m256i bxhi = bytes_from_bits_32(x[ib].qh); 1344 __m128i bxhil = _mm256_castsi256_si128(bxhi); 1345 __m128i bxhih = _mm256_extractf128_si256(bxhi, 1); 1346 bxhil = _mm_and_si128(bxhil, mask); 1347 bxhih = _mm_and_si128(bxhih, mask); 1348 __m128i bxl = _mm256_castsi256_si128(bx_0); 1349 __m128i bxh = _mm256_extractf128_si256(bx_0, 1); 1350 bxl = _mm_or_si128(bxl, bxhil); 1351 bxh = _mm_or_si128(bxh, bxhih); 1352 bx_0 = MM256_SET_M128I(bxh, bxl); 1353 1354 const __m256 dy = _mm256_set1_ps(GGML_CPU_FP16_TO_FP32(y[ib].d)); 1355 const __m256i by_0 = _mm256_loadu_si256((const __m256i *)y[ib].qs); 1356 1357 const __m256 q = mul_sum_us8_pairs_float(bx_0, by_0); 1358 1359 acc = _mm256_add_ps(_mm256_mul_ps(q, _mm256_mul_ps(dx, dy)), acc); 1360 } 1361 1362 *s = hsum_float_8(acc) + summs; 1363 #else 1364 UNUSED(nb); 1365 UNUSED(ib); 1366 UNUSED(x); 1367 UNUSED(y); 1368 ggml_vec_dot_q5_1_q8_1_generic(n, s, bs, vx, bx, vy, by, nrc); 1369 #endif 1370 } 1371 1372 void ggml_vec_dot_q8_0_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { 1373 const int qk = QK8_0; 1374 const int nb = n / qk; 1375 1376 assert(n % qk == 0); 1377 assert(nrc == 1); 1378 UNUSED(nrc); 1379 UNUSED(bx); 1380 UNUSED(by); 1381 UNUSED(bs); 1382 1383 const block_q8_0 * GGML_RESTRICT x = vx; 1384 const block_q8_0 * GGML_RESTRICT y = vy; 1385 1386 int ib = 0; 1387 float sumf = 0; 1388 1389 #if defined(__AVX2__) 1390 // Initialize accumulator with zeros 1391 __m256 acc = _mm256_setzero_ps(); 1392 1393 // Main loop 1394 for (; ib < nb; ++ib) { 1395 // Compute combined scale for the block 1396 const __m256 d = _mm256_set1_ps(GGML_CPU_FP16_TO_FP32(x[ib].d) * GGML_CPU_FP16_TO_FP32(y[ib].d)); 1397 __m256i qx = _mm256_loadu_si256((const __m256i *)x[ib].qs); 1398 __m256i qy = _mm256_loadu_si256((const __m256i *)y[ib].qs); 1399 1400 const __m256 q = mul_sum_i8_pairs_float(qx, qy); 1401 1402 // Multiply q with scale and accumulate 1403 acc = _mm256_fmadd_ps( d, q, acc ); 1404 } 1405 1406 sumf = hsum_float_8(acc); 1407 #elif defined(__AVX__) 1408 __m256 accum = _mm256_setzero_ps(); 1409 1410 for (; ib + 1 < nb; ib += 2) { 1411 const __m128i qx_1_0 = _mm_loadu_si128((const __m128i *)x[ib].qs); 1412 const __m128i qx_1_1 = _mm_loadu_si128((const __m128i *)x[ib].qs + 1); 1413 const __m128i qx_2_0 = _mm_loadu_si128((const __m128i *)x[ib + 1].qs); 1414 const __m128i qx_2_1 = _mm_loadu_si128((const __m128i *)x[ib + 1].qs + 1); 1415 const __m128i qy_1_0 = _mm_loadu_si128((const __m128i *)y[ib].qs); 1416 const __m128i qy_1_1 = _mm_loadu_si128((const __m128i *)y[ib].qs + 1); 1417 const __m128i qy_2_0 = _mm_loadu_si128((const __m128i *)y[ib + 1].qs); 1418 const __m128i qy_2_1 = _mm_loadu_si128((const __m128i *)y[ib + 1].qs + 1); 1419 1420 const __m256 p = mul_sum_i8_quad_float(qx_1_0, qx_1_1, qx_2_0, qx_2_1, qy_1_0, qy_1_1, qy_2_0, qy_2_1); 1421 const __m256 deltas = quad_fp16_delta_float(x[ib].d, y[ib].d, x[ib + 1].d, y[ib + 1].d); 1422 accum = _mm256_add_ps(_mm256_mul_ps(deltas, p), accum); 1423 } 1424 1425 sumf = hsum_float_8(accum); 1426 #endif 1427 for (; ib < nb; ++ib) { 1428 int sumi = 0; 1429 1430 for (int j = 0; j < qk; j++) { 1431 sumi += x[ib].qs[j]*y[ib].qs[j]; 1432 } 1433 1434 sumf += sumi*(GGML_CPU_FP16_TO_FP32(x[ib].d)*GGML_CPU_FP16_TO_FP32(y[ib].d)); 1435 } 1436 1437 *s = sumf; 1438 } 1439 1440 void ggml_vec_dot_tq1_0_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { 1441 assert(nrc == 1); 1442 UNUSED(nrc); 1443 UNUSED(bx); 1444 UNUSED(by); 1445 UNUSED(bs); 1446 1447 const block_tq1_0 * GGML_RESTRICT x = vx; 1448 const block_q8_K * GGML_RESTRICT y = vy; 1449 1450 const int nb = n / QK_K; 1451 1452 #if defined(__AVX2__) 1453 __m256 sumf = _mm256_setzero_ps(); 1454 1455 for (int i = 0; i < nb; ++i) { 1456 // 16-bit sums 1457 __m256i sumi0 = _mm256_setzero_si256(); 1458 __m256i sumi1 = _mm256_setzero_si256(); 1459 __m256i sumi2 = _mm256_setzero_si256(); 1460 1461 // first 32 bytes of 5 elements 1462 { 1463 __m256i qx0 = _mm256_loadu_si256((const __m256i *) (x[i].qs)); 1464 // 8-bit multiplies with shifts, masks and adds 1465 __m256i qx1 = _mm256_add_epi8(qx0, _mm256_add_epi8(qx0, qx0)); // 1 * 3 1466 __m256i qx2 = _mm256_add_epi8(_mm256_and_si256(_mm256_slli_epi16(qx0, 3), _mm256_set1_epi8(-8)), qx0); // 1 * 9 1467 __m256i qx3 = _mm256_add_epi8(_mm256_and_si256(_mm256_slli_epi16(qx1, 3), _mm256_set1_epi8(-8)), qx1); // 3 * 9 1468 __m256i qx4 = _mm256_add_epi8(_mm256_and_si256(_mm256_slli_epi16(qx2, 3), _mm256_set1_epi8(-8)), qx2); // 9 * 9 1469 1470 // TODO: can _mm256_mulhi_epu16 be faster even if 16-bits? 1471 1472 // Cancel the +1 from avg so that it behaves like a halving add 1473 qx0 = _mm256_subs_epu8(qx0, _mm256_set1_epi8(1)); 1474 qx1 = _mm256_subs_epu8(qx1, _mm256_set1_epi8(1)); 1475 qx2 = _mm256_subs_epu8(qx2, _mm256_set1_epi8(1)); 1476 qx3 = _mm256_subs_epu8(qx3, _mm256_set1_epi8(1)); 1477 qx4 = _mm256_subs_epu8(qx4, _mm256_set1_epi8(1)); 1478 // Multiply by 3 and get the top 2 bits 1479 qx0 = _mm256_avg_epu8(qx0, _mm256_avg_epu8(qx0, _mm256_setzero_si256())); 1480 qx1 = _mm256_avg_epu8(qx1, _mm256_avg_epu8(qx1, _mm256_setzero_si256())); 1481 qx2 = _mm256_avg_epu8(qx2, _mm256_avg_epu8(qx2, _mm256_setzero_si256())); 1482 qx3 = _mm256_avg_epu8(qx3, _mm256_avg_epu8(qx3, _mm256_setzero_si256())); 1483 qx4 = _mm256_avg_epu8(qx4, _mm256_avg_epu8(qx4, _mm256_setzero_si256())); 1484 qx0 = _mm256_and_si256(_mm256_srli_epi16(qx0, 6), _mm256_set1_epi8(3)); 1485 qx1 = _mm256_and_si256(_mm256_srli_epi16(qx1, 6), _mm256_set1_epi8(3)); 1486 qx2 = _mm256_and_si256(_mm256_srli_epi16(qx2, 6), _mm256_set1_epi8(3)); 1487 qx3 = _mm256_and_si256(_mm256_srli_epi16(qx3, 6), _mm256_set1_epi8(3)); 1488 qx4 = _mm256_and_si256(_mm256_srli_epi16(qx4, 6), _mm256_set1_epi8(3)); 1489 1490 const __m256i qy0 = _mm256_loadu_si256((const __m256i *) (y[i].qs + 0)); 1491 const __m256i qy1 = _mm256_loadu_si256((const __m256i *) (y[i].qs + 32)); 1492 const __m256i qy2 = _mm256_loadu_si256((const __m256i *) (y[i].qs + 64)); 1493 const __m256i qy3 = _mm256_loadu_si256((const __m256i *) (y[i].qs + 96)); 1494 const __m256i qy4 = _mm256_loadu_si256((const __m256i *) (y[i].qs + 128)); 1495 1496 qx0 = _mm256_maddubs_epi16(qx0, qy0); 1497 qx1 = _mm256_maddubs_epi16(qx1, qy1); 1498 qx2 = _mm256_maddubs_epi16(qx2, qy2); 1499 qx3 = _mm256_maddubs_epi16(qx3, qy3); 1500 qx4 = _mm256_maddubs_epi16(qx4, qy4); 1501 1502 sumi0 = _mm256_add_epi16(sumi0, _mm256_add_epi16(qx0, qx1)); 1503 sumi1 = _mm256_add_epi16(sumi1, _mm256_add_epi16(qx2, qx3)); 1504 sumi2 = _mm256_add_epi16(sumi2, qx4); 1505 } 1506 1507 // last 16 bytes of 5-element, along with the 4 bytes of 4 elements 1508 { 1509 __m128i qx0 = _mm_loadu_si128((const __m128i *) (x[i].qs + 32)); 1510 uint32_t qh; 1511 memcpy(&qh, x[i].qh, sizeof(qh)); // potentially unaligned 1512 __m256i qx5_l = _mm256_cvtepu8_epi16(_mm_set1_epi32(qh)); 1513 __m128i qx1 = _mm_add_epi8(qx0, _mm_add_epi8(qx0, qx0)); // 1 * 3 1514 __m128i qx2 = _mm_add_epi8(_mm_and_si128(_mm_slli_epi16(qx0, 3), _mm_set1_epi8(-8)), qx0); // 1 * 9 1515 __m128i qx3 = _mm_add_epi8(_mm_and_si128(_mm_slli_epi16(qx1, 3), _mm_set1_epi8(-8)), qx1); // 3 * 9 1516 __m128i qx4 = _mm_add_epi8(_mm_and_si128(_mm_slli_epi16(qx2, 3), _mm_set1_epi8(-8)), qx2); // 9 * 9 1517 __m256i qx01 = MM256_SET_M128I(qx1, qx0); 1518 __m256i qx23 = MM256_SET_M128I(qx3, qx2); 1519 1520 // avx2 does not have 8-bit multiplies, so 16-bit it is. 1521 qx5_l = _mm256_mullo_epi16(qx5_l, _mm256_set_epi16(27, 27, 27, 27, 9, 9, 9, 9, 3, 3, 3, 3, 1, 1, 1, 1)); 1522 qx5_l = _mm256_and_si256(qx5_l, _mm256_set1_epi16(0xFF)); 1523 __m128i qx5 = _mm_packus_epi16(_mm256_castsi256_si128(qx5_l), _mm256_extracti128_si256(qx5_l, 1)); 1524 1525 __m256i qx45 = MM256_SET_M128I(qx5, qx4); 1526 1527 // Cancel the +1 from avg so that it behaves like a halving add 1528 qx01 = _mm256_subs_epu8(qx01, _mm256_set1_epi8(1)); 1529 qx23 = _mm256_subs_epu8(qx23, _mm256_set1_epi8(1)); 1530 qx45 = _mm256_subs_epu8(qx45, _mm256_set1_epi8(1)); 1531 // Multiply by 3 and get the top 2 bits 1532 qx01 = _mm256_avg_epu8(qx01, _mm256_avg_epu8(qx01, _mm256_setzero_si256())); 1533 qx23 = _mm256_avg_epu8(qx23, _mm256_avg_epu8(qx23, _mm256_setzero_si256())); 1534 qx45 = _mm256_avg_epu8(qx45, _mm256_avg_epu8(qx45, _mm256_setzero_si256())); 1535 qx01 = _mm256_and_si256(_mm256_srli_epi16(qx01, 6), _mm256_set1_epi8(3)); 1536 qx23 = _mm256_and_si256(_mm256_srli_epi16(qx23, 6), _mm256_set1_epi8(3)); 1537 qx45 = _mm256_and_si256(_mm256_srli_epi16(qx45, 6), _mm256_set1_epi8(3)); 1538 1539 const __m256i qy01 = _mm256_loadu_si256((const __m256i *) (y[i].qs + 160)); 1540 const __m256i qy23 = _mm256_loadu_si256((const __m256i *) (y[i].qs + 192)); 1541 const __m256i qy45 = _mm256_loadu_si256((const __m256i *) (y[i].qs + 224)); 1542 1543 qx01 = _mm256_maddubs_epi16(qx01, qy01); 1544 qx23 = _mm256_maddubs_epi16(qx23, qy23); 1545 qx45 = _mm256_maddubs_epi16(qx45, qy45); 1546 1547 sumi0 = _mm256_add_epi16(sumi0, qx01); 1548 sumi1 = _mm256_add_epi16(sumi1, qx23); 1549 sumi2 = _mm256_add_epi16(sumi2, qx45); 1550 } 1551 1552 const __m256i ysum = _mm256_loadu_si256((const __m256i *) y[i].bsums); 1553 const __m256 d = _mm256_set1_ps(y[i].d * GGML_CPU_FP16_TO_FP32(x[i].d)); 1554 1555 sumi0 = _mm256_sub_epi16(sumi0, ysum); 1556 sumi0 = _mm256_add_epi16(sumi0, _mm256_add_epi16(sumi1, sumi2)); 1557 sumi0 = _mm256_madd_epi16(sumi0, _mm256_set1_epi16(1)); 1558 1559 sumf = _mm256_add_ps(_mm256_mul_ps(_mm256_cvtepi32_ps(sumi0), d), sumf); 1560 } 1561 1562 *s = hsum_float_8(sumf); 1563 1564 #else 1565 UNUSED(x); 1566 UNUSED(y); 1567 UNUSED(nb); 1568 ggml_vec_dot_tq1_0_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc); 1569 #endif 1570 } 1571 1572 void ggml_vec_dot_tq2_0_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { 1573 assert(nrc == 1); 1574 UNUSED(nrc); 1575 UNUSED(bx); 1576 UNUSED(by); 1577 UNUSED(bs); 1578 1579 const block_tq2_0 * GGML_RESTRICT x = vx; 1580 const block_q8_K * GGML_RESTRICT y = vy; 1581 1582 const int nb = n / QK_K; 1583 1584 #if defined(__AVX2__) 1585 __m256 sumf = _mm256_setzero_ps(); 1586 1587 for (int i = 0; i < nb; ++i) { 1588 // 16-bit sums, because 256*127 still fits 1589 __m256i sumi0 = _mm256_setzero_si256(); 1590 __m256i sumi1 = _mm256_setzero_si256(); 1591 1592 for (size_t j = 0; j < sizeof(x->qs); j += 32) { 1593 __m256i qx0 = _mm256_loadu_si256((const __m256i *) (x[i].qs + j)); 1594 __m256i qx1 = _mm256_srli_epi16(qx0, 2); 1595 __m256i qx2 = _mm256_srli_epi16(qx0, 4); 1596 __m256i qx3 = _mm256_srli_epi16(qx0, 6); 1597 1598 // 0, 1, 2 (should not be 3) 1599 qx0 = _mm256_and_si256(qx0, _mm256_set1_epi8(3)); 1600 qx1 = _mm256_and_si256(qx1, _mm256_set1_epi8(3)); 1601 qx2 = _mm256_and_si256(qx2, _mm256_set1_epi8(3)); 1602 qx3 = _mm256_and_si256(qx3, _mm256_set1_epi8(3)); 1603 1604 const __m256i qy0 = _mm256_loadu_si256((const __m256i *) (y[i].qs + j*4 + 0)); 1605 const __m256i qy1 = _mm256_loadu_si256((const __m256i *) (y[i].qs + j*4 + 32)); 1606 const __m256i qy2 = _mm256_loadu_si256((const __m256i *) (y[i].qs + j*4 + 64)); 1607 const __m256i qy3 = _mm256_loadu_si256((const __m256i *) (y[i].qs + j*4 + 96)); 1608 1609 qx0 = _mm256_maddubs_epi16(qx0, qy0); 1610 qx1 = _mm256_maddubs_epi16(qx1, qy1); 1611 qx2 = _mm256_maddubs_epi16(qx2, qy2); 1612 qx3 = _mm256_maddubs_epi16(qx3, qy3); 1613 1614 sumi0 = _mm256_add_epi16(sumi0, _mm256_add_epi16(qx0, qx1)); 1615 sumi1 = _mm256_add_epi16(sumi1, _mm256_add_epi16(qx2, qx3)); 1616 } 1617 1618 const __m256i ysum = _mm256_loadu_si256((const __m256i *) y[i].bsums); 1619 const __m256 d = _mm256_set1_ps(y[i].d * GGML_CPU_FP16_TO_FP32(x[i].d)); 1620 1621 sumi0 = _mm256_add_epi16(sumi0, sumi1); 1622 sumi0 = _mm256_sub_epi16(sumi0, ysum); 1623 sumi0 = _mm256_madd_epi16(sumi0, _mm256_set1_epi16(1)); 1624 1625 sumf = _mm256_add_ps(_mm256_mul_ps(_mm256_cvtepi32_ps(sumi0), d), sumf); 1626 } 1627 1628 *s = hsum_float_8(sumf); 1629 1630 #else 1631 UNUSED(x); 1632 UNUSED(y); 1633 UNUSED(nb); 1634 ggml_vec_dot_tq2_0_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc); 1635 #endif 1636 } 1637 1638 void ggml_vec_dot_q2_K_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { 1639 assert(nrc == 1); 1640 UNUSED(nrc); 1641 UNUSED(bx); 1642 UNUSED(by); 1643 UNUSED(bs); 1644 1645 const block_q2_K * GGML_RESTRICT x = vx; 1646 const block_q8_K * GGML_RESTRICT y = vy; 1647 1648 const int nb = n / QK_K; 1649 1650 #if defined __AVX2__ 1651 1652 const __m256i m3 = _mm256_set1_epi8(3); 1653 const __m128i m4 = _mm_set1_epi8(0xF); 1654 1655 __m256 acc = _mm256_setzero_ps(); 1656 1657 for (int i = 0; i < nb; ++i) { 1658 1659 const float d = y[i].d * GGML_CPU_FP16_TO_FP32(x[i].d); 1660 const float dmin = -y[i].d * GGML_CPU_FP16_TO_FP32(x[i].dmin); 1661 1662 const uint8_t * GGML_RESTRICT q2 = x[i].qs; 1663 const int8_t * GGML_RESTRICT q8 = y[i].qs; 1664 1665 const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales); 1666 const __m128i scales8 = _mm_and_si128(mins_and_scales, m4); 1667 const __m128i mins8 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4); 1668 const __m256i mins = _mm256_cvtepi8_epi16(mins8); 1669 const __m256i prod = _mm256_madd_epi16(mins, _mm256_loadu_si256((const __m256i*)y[i].bsums)); 1670 1671 acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(prod), acc); 1672 1673 const __m256i all_scales = _mm256_cvtepi8_epi16(scales8); 1674 const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0); 1675 const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1); 1676 const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)}; 1677 1678 __m256i sumi = _mm256_setzero_si256(); 1679 1680 for (int j = 0; j < QK_K/128; ++j) { 1681 1682 const __m256i q2bits = _mm256_loadu_si256((const __m256i*)q2); q2 += 32; 1683 1684 const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32; 1685 const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32; 1686 const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32; 1687 const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32; 1688 1689 const __m256i q2_0 = _mm256_and_si256(q2bits, m3); 1690 const __m256i q2_1 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 2), m3); 1691 const __m256i q2_2 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 4), m3); 1692 const __m256i q2_3 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 6), m3); 1693 1694 __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0); 1695 __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1); 1696 __m256i p2 = _mm256_maddubs_epi16(q2_2, q8_2); 1697 __m256i p3 = _mm256_maddubs_epi16(q2_3, q8_3); 1698 1699 p0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(0)), p0); 1700 p1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(1)), p1); 1701 p2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(2)), p2); 1702 p3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(3)), p3); 1703 1704 p0 = _mm256_add_epi32(p0, p1); 1705 p2 = _mm256_add_epi32(p2, p3); 1706 1707 sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p0, p2)); 1708 } 1709 1710 acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc); 1711 1712 } 1713 1714 *s = hsum_float_8(acc); 1715 1716 #elif defined __AVX__ 1717 1718 const __m128i m3 = _mm_set1_epi8(0x3); 1719 const __m128i m4 = _mm_set1_epi8(0xF); 1720 const __m128i m2 = _mm_set1_epi8(0x2); 1721 1722 __m256 acc = _mm256_setzero_ps(); 1723 1724 for (int i = 0; i < nb; ++i) { 1725 1726 const float dall = y[i].d * GGML_CPU_FP16_TO_FP32(x[i].d); 1727 const float dmin = -y[i].d * GGML_CPU_FP16_TO_FP32(x[i].dmin); 1728 1729 const uint8_t * GGML_RESTRICT q2 = x[i].qs; 1730 const int8_t * GGML_RESTRICT q8 = y[i].qs; 1731 1732 // load mins and scales from block_q2_K.scales[QK_K/16] 1733 const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales); 1734 const __m128i scales16 = _mm_and_si128(mins_and_scales, m4); 1735 const __m128i mins16 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4); 1736 const __m128i mins_0 = _mm_cvtepi8_epi16(mins16); 1737 const __m128i mins_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(mins16, mins16)); 1738 1739 // summs = y[i].bsums * (x[i].scales >> 4) in 16bits*8*2 to 32bits*4*2 1740 const __m128i summs_0 = _mm_madd_epi16(mins_0, _mm_loadu_si128((const __m128i*)&y[i].bsums[0])); 1741 const __m128i summs_1 = _mm_madd_epi16(mins_1, _mm_loadu_si128((const __m128i*)&y[i].bsums[8])); 1742 1743 // sumf += -dmin * summs in 32bits*8 1744 acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(MM256_SET_M128I(summs_1, summs_0))), acc); 1745 1746 const __m128i scales_0 = _mm_cvtepi8_epi16(scales16); 1747 const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales16, scales16)); 1748 const __m128i scales[2] = { scales_0, scales_1 }; 1749 1750 __m128i sumi_0 = _mm_setzero_si128(); 1751 __m128i sumi_1 = _mm_setzero_si128(); 1752 1753 for (int j = 0; j < QK_K/128; ++j) { 1754 1755 // load Q8 quants int8*16*8 from block_q8_K.qs[QK_K] 1756 const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 1757 const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 1758 const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 1759 const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 1760 const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 1761 const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 1762 const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 1763 const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 1764 1765 // load 2bits*16*8 from block_q2_K.qs[QK_K/4] 1766 __m128i q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16; 1767 const __m128i q2_0 = _mm_and_si128(q2bits, m3); 1768 const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3); 1769 const __m128i q2_4 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3); 1770 const __m128i q2_6 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3); 1771 q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16; 1772 const __m128i q2_1 = _mm_and_si128(q2bits, m3); 1773 const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3); 1774 const __m128i q2_5 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3); 1775 const __m128i q2_7 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3); 1776 1777 // isuml = q8[l] * ((q2[l] >> shift) & 3) in 8bits*16*8 to 16bits*8*8 1778 __m128i p0 = _mm_maddubs_epi16(q2_0, q8_0); 1779 __m128i p1 = _mm_maddubs_epi16(q2_1, q8_1); 1780 __m128i p2 = _mm_maddubs_epi16(q2_2, q8_2); 1781 __m128i p3 = _mm_maddubs_epi16(q2_3, q8_3); 1782 __m128i p4 = _mm_maddubs_epi16(q2_4, q8_4); 1783 __m128i p5 = _mm_maddubs_epi16(q2_5, q8_5); 1784 __m128i p6 = _mm_maddubs_epi16(q2_6, q8_6); 1785 __m128i p7 = _mm_maddubs_epi16(q2_7, q8_7); 1786 1787 // isum += (x[i].scales[is++] & 0xF) * isuml in 16bits*8*8 to 32bits*4*8 1788 __m128i shuffle = _mm_set1_epi16(0x0100); 1789 p0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p0); 1790 shuffle = _mm_add_epi16(shuffle, m2); 1791 p1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p1); 1792 shuffle = _mm_add_epi16(shuffle, m2); 1793 p2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p2); 1794 shuffle = _mm_add_epi16(shuffle, m2); 1795 p3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p3); 1796 shuffle = _mm_add_epi16(shuffle, m2); 1797 p4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p4); 1798 shuffle = _mm_add_epi16(shuffle, m2); 1799 p5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p5); 1800 shuffle = _mm_add_epi16(shuffle, m2); 1801 p6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p6); 1802 shuffle = _mm_add_epi16(shuffle, m2); 1803 p7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p7); 1804 1805 p0 = _mm_add_epi32(p0, p1); 1806 p2 = _mm_add_epi32(p2, p3); 1807 p4 = _mm_add_epi32(p4, p5); 1808 p6 = _mm_add_epi32(p6, p7); 1809 1810 // isum in 32bits*4*2 1811 sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p0, p2)); 1812 sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p4, p6)); 1813 } 1814 1815 // sumf += dall * isum - dmin * summs in 32bits 1816 __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0); 1817 acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dall), _mm256_cvtepi32_ps(sumi)), acc); 1818 } 1819 1820 *s = hsum_float_8(acc); 1821 1822 #else 1823 UNUSED(x); 1824 UNUSED(y); 1825 UNUSED(nb); 1826 ggml_vec_dot_q2_K_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc); 1827 #endif 1828 } 1829 1830 void ggml_vec_dot_q3_K_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { 1831 assert(n % QK_K == 0); 1832 assert(nrc == 1); 1833 UNUSED(nrc); 1834 UNUSED(bx); 1835 UNUSED(by); 1836 UNUSED(bs); 1837 1838 const uint32_t kmask1 = 0x03030303; 1839 const uint32_t kmask2 = 0x0f0f0f0f; 1840 1841 const block_q3_K * GGML_RESTRICT x = vx; 1842 const block_q8_K * GGML_RESTRICT y = vy; 1843 1844 const int nb = n / QK_K; 1845 1846 #if defined __AVX2__ 1847 1848 const __m256i m3 = _mm256_set1_epi8(3); 1849 const __m256i mone = _mm256_set1_epi8(1); 1850 const __m128i m32 = _mm_set1_epi8(32); 1851 1852 __m256 acc = _mm256_setzero_ps(); 1853 1854 uint32_t aux[3]; 1855 1856 for (int i = 0; i < nb; ++i) { 1857 1858 const float d = y[i].d * GGML_CPU_FP16_TO_FP32(x[i].d); 1859 1860 const uint8_t * GGML_RESTRICT q3 = x[i].qs; 1861 const int8_t * GGML_RESTRICT q8 = y[i].qs; 1862 1863 // Set up scales 1864 memcpy(aux, x[i].scales, 12); 1865 __m128i scales128 = _mm_set_epi32( 1866 ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4), 1867 ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4), 1868 (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4), 1869 (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4)); 1870 scales128 = _mm_sub_epi8(scales128, m32); 1871 const __m256i all_scales = _mm256_cvtepi8_epi16(scales128); 1872 const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0); 1873 const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1); 1874 const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)}; 1875 1876 // high bit 1877 const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].hmask); 1878 1879 // integer accumulator 1880 __m256i sumi = _mm256_setzero_si256(); 1881 1882 int bit = 0; 1883 int is = 0; 1884 1885 for (int j = 0; j < QK_K/128; ++j) { 1886 // load low 2 bits 1887 const __m256i q3bits = _mm256_loadu_si256((const __m256i*)q3); q3 += 32; 1888 1889 // prepare low and high bits 1890 const __m256i q3l_0 = _mm256_and_si256(q3bits, m3); 1891 const __m256i q3h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2); 1892 ++bit; 1893 1894 const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 2), m3); 1895 const __m256i q3h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2); 1896 ++bit; 1897 1898 const __m256i q3l_2 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 4), m3); 1899 const __m256i q3h_2 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2); 1900 ++bit; 1901 1902 const __m256i q3l_3 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 6), m3); 1903 const __m256i q3h_3 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2); 1904 ++bit; 1905 1906 // load Q8 quants 1907 const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32; 1908 const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32; 1909 const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32; 1910 const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32; 1911 1912 // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16, 1913 // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set, 1914 // and 2 if the high bit was set) 1915 __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0); 1916 __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1); 1917 __m256i q8s_2 = _mm256_maddubs_epi16(q3h_2, q8_2); 1918 __m256i q8s_3 = _mm256_maddubs_epi16(q3h_3, q8_3); 1919 1920 __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0); 1921 __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1); 1922 __m256i p16_2 = _mm256_maddubs_epi16(q3l_2, q8_2); 1923 __m256i p16_3 = _mm256_maddubs_epi16(q3l_3, q8_3); 1924 1925 p16_0 = _mm256_sub_epi16(p16_0, q8s_0); 1926 p16_1 = _mm256_sub_epi16(p16_1, q8s_1); 1927 p16_2 = _mm256_sub_epi16(p16_2, q8s_2); 1928 p16_3 = _mm256_sub_epi16(p16_3, q8s_3); 1929 1930 // multiply with scales 1931 p16_0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 0)), p16_0); 1932 p16_1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 1)), p16_1); 1933 p16_2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 2)), p16_2); 1934 p16_3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 3)), p16_3); 1935 1936 // accumulate 1937 p16_0 = _mm256_add_epi32(p16_0, p16_1); 1938 p16_2 = _mm256_add_epi32(p16_2, p16_3); 1939 sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_2)); 1940 1941 } 1942 1943 // multiply with block scale and accumulate 1944 acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc); 1945 1946 } 1947 1948 *s = hsum_float_8(acc); 1949 1950 #elif defined __AVX__ 1951 1952 const __m128i m3 = _mm_set1_epi8(3); 1953 const __m128i mone = _mm_set1_epi8(1); 1954 const __m128i m32 = _mm_set1_epi8(32); 1955 const __m128i m2 = _mm_set1_epi8(2); 1956 1957 __m256 acc = _mm256_setzero_ps(); 1958 1959 const uint32_t *aux; 1960 1961 for (int i = 0; i < nb; ++i) { 1962 1963 const float d = y[i].d * GGML_CPU_FP16_TO_FP32(x[i].d); 1964 1965 const uint8_t * GGML_RESTRICT q3 = x[i].qs; 1966 const int8_t * GGML_RESTRICT q8 = y[i].qs; 1967 1968 // Set up scales 1969 aux = (const uint32_t *)x[i].scales; 1970 __m128i scales128 = _mm_set_epi32( 1971 ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4), 1972 ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4), 1973 (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4), 1974 (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4)); 1975 scales128 = _mm_sub_epi8(scales128, m32); 1976 const __m128i scales_0 = _mm_cvtepi8_epi16(scales128); 1977 const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales128, scales128)); 1978 const __m128i scales[2] = { scales_0, scales_1 }; 1979 1980 // high bit *128*2 from block_q3_K.hmask[QK_K/8] 1981 const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].hmask[0]); 1982 const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].hmask[16]); 1983 1984 // integer accumulator 1985 __m128i sumi_0 = _mm_setzero_si128(); 1986 __m128i sumi_1 = _mm_setzero_si128(); 1987 1988 for (int j = 0; j < QK_K/128; ++j) { 1989 // load low 2 bits *64*2 from block_q3_K.qs[QK_K/4] 1990 const __m128i q3bits_0 = _mm_loadu_si128((const __m128i*)q3); q3 += 16; 1991 const __m128i q3bits_1 = _mm_loadu_si128((const __m128i*)q3); q3 += 16; 1992 1993 // prepare low and high bits 1994 const int bit = j << 2; 1995 1996 const __m128i q3l_0 = _mm_and_si128(q3bits_0, m3); 1997 const __m128i q3l_1 = _mm_and_si128(q3bits_1, m3); 1998 const __m128i q3h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit)), bit), 2); 1999 const __m128i q3h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit)), bit), 2); 2000 2001 const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 2), m3); 2002 const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 2), m3); 2003 const __m128i q3h_2 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+1)), bit+1), 2); 2004 const __m128i q3h_3 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+1)), bit+1), 2); 2005 2006 const __m128i q3l_4 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 4), m3); 2007 const __m128i q3l_5 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 4), m3); 2008 const __m128i q3h_4 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+2)), bit+2), 2); 2009 const __m128i q3h_5 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+2)), bit+2), 2); 2010 2011 const __m128i q3l_6 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 6), m3); 2012 const __m128i q3l_7 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 6), m3); 2013 const __m128i q3h_6 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+3)), bit+3), 2); 2014 const __m128i q3h_7 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+3)), bit+3), 2); 2015 2016 // load Q8 quants from block_q8_K.qs[QK_K] 2017 const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 2018 const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 2019 const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 2020 const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 2021 const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 2022 const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 2023 const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 2024 const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 2025 2026 // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16, 2027 // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set, 2028 // and 2 if the high bit was set) 2029 __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, q8_0); 2030 __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, q8_1); 2031 __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, q8_2); 2032 __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, q8_3); 2033 __m128i q8s_4 = _mm_maddubs_epi16(q3h_4, q8_4); 2034 __m128i q8s_5 = _mm_maddubs_epi16(q3h_5, q8_5); 2035 __m128i q8s_6 = _mm_maddubs_epi16(q3h_6, q8_6); 2036 __m128i q8s_7 = _mm_maddubs_epi16(q3h_7, q8_7); 2037 2038 __m128i p16_0 = _mm_maddubs_epi16(q3l_0, q8_0); 2039 __m128i p16_1 = _mm_maddubs_epi16(q3l_1, q8_1); 2040 __m128i p16_2 = _mm_maddubs_epi16(q3l_2, q8_2); 2041 __m128i p16_3 = _mm_maddubs_epi16(q3l_3, q8_3); 2042 __m128i p16_4 = _mm_maddubs_epi16(q3l_4, q8_4); 2043 __m128i p16_5 = _mm_maddubs_epi16(q3l_5, q8_5); 2044 __m128i p16_6 = _mm_maddubs_epi16(q3l_6, q8_6); 2045 __m128i p16_7 = _mm_maddubs_epi16(q3l_7, q8_7); 2046 2047 p16_0 = _mm_sub_epi16(p16_0, q8s_0); 2048 p16_1 = _mm_sub_epi16(p16_1, q8s_1); 2049 p16_2 = _mm_sub_epi16(p16_2, q8s_2); 2050 p16_3 = _mm_sub_epi16(p16_3, q8s_3); 2051 p16_4 = _mm_sub_epi16(p16_4, q8s_4); 2052 p16_5 = _mm_sub_epi16(p16_5, q8s_5); 2053 p16_6 = _mm_sub_epi16(p16_6, q8s_6); 2054 p16_7 = _mm_sub_epi16(p16_7, q8s_7); 2055 2056 // multiply with scales 2057 __m128i shuffle = _mm_set1_epi16(0x0100); 2058 p16_0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_0); 2059 shuffle = _mm_add_epi16(shuffle, m2); 2060 p16_1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_1); 2061 shuffle = _mm_add_epi16(shuffle, m2); 2062 p16_2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_2); 2063 shuffle = _mm_add_epi16(shuffle, m2); 2064 p16_3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_3); 2065 shuffle = _mm_add_epi16(shuffle, m2); 2066 p16_4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_4); 2067 shuffle = _mm_add_epi16(shuffle, m2); 2068 p16_5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_5); 2069 shuffle = _mm_add_epi16(shuffle, m2); 2070 p16_6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_6); 2071 shuffle = _mm_add_epi16(shuffle, m2); 2072 p16_7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_7); 2073 2074 // accumulate 2075 p16_0 = _mm_add_epi32(p16_0, p16_1); 2076 p16_2 = _mm_add_epi32(p16_2, p16_3); 2077 p16_4 = _mm_add_epi32(p16_4, p16_5); 2078 p16_6 = _mm_add_epi32(p16_6, p16_7); 2079 sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2)); 2080 sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_4, p16_6)); 2081 2082 } 2083 2084 // multiply with block scale and accumulate 2085 __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0); 2086 acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc); 2087 2088 } 2089 2090 *s = hsum_float_8(acc); 2091 2092 #else 2093 UNUSED(kmask1); 2094 UNUSED(kmask2); 2095 UNUSED(x); 2096 UNUSED(y); 2097 UNUSED(nb); 2098 ggml_vec_dot_q3_K_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc); 2099 #endif 2100 } 2101 2102 void ggml_vec_dot_q4_K_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { 2103 assert(n % QK_K == 0); 2104 assert(nrc == 1); 2105 UNUSED(nrc); 2106 UNUSED(bx); 2107 UNUSED(by); 2108 UNUSED(bs); 2109 2110 const block_q4_K * GGML_RESTRICT x = vx; 2111 const block_q8_K * GGML_RESTRICT y = vy; 2112 2113 const int nb = n / QK_K; 2114 2115 static const uint32_t kmask1 = 0x3f3f3f3f; 2116 static const uint32_t kmask2 = 0x0f0f0f0f; 2117 static const uint32_t kmask3 = 0x03030303; 2118 2119 uint32_t utmp[4]; 2120 2121 #if defined __AVX2__ 2122 2123 const __m256i m4 = _mm256_set1_epi8(0xF); 2124 2125 __m256 acc = _mm256_setzero_ps(); 2126 __m128 acc_m = _mm_setzero_ps(); 2127 2128 for (int i = 0; i < nb; ++i) { 2129 2130 const float d = y[i].d * GGML_CPU_FP16_TO_FP32(x[i].d); 2131 const float dmin = -y[i].d * GGML_CPU_FP16_TO_FP32(x[i].dmin); 2132 2133 memcpy(utmp, x[i].scales, 12); 2134 utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4); 2135 const uint32_t uaux = utmp[1] & kmask1; 2136 utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4); 2137 utmp[2] = uaux; 2138 utmp[0] &= kmask1; 2139 2140 const uint8_t * GGML_RESTRICT q4 = x[i].qs; 2141 const int8_t * GGML_RESTRICT q8 = y[i].qs; 2142 2143 const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0])); 2144 2145 const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums); 2146 const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1)); 2147 const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s); 2148 acc_m = _mm_fmadd_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod), acc_m); 2149 2150 const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0); 2151 const __m256i scales = MM256_SET_M128I(sc128, sc128); 2152 2153 __m256i sumi = _mm256_setzero_si256(); 2154 2155 for (int j = 0; j < QK_K/64; ++j) { 2156 2157 const __m256i scale_l = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0)); 2158 const __m256i scale_h = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1)); 2159 2160 const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4); q4 += 32; 2161 const __m256i q4l = _mm256_and_si256(q4bits, m4); 2162 const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4); 2163 2164 const __m256i q8l = _mm256_loadu_si256((const __m256i*)q8); q8 += 32; 2165 __m256i p16l = _mm256_maddubs_epi16(q4l, q8l); 2166 p16l = _mm256_madd_epi16(scale_l, p16l); 2167 2168 const __m256i q8h = _mm256_loadu_si256((const __m256i*)q8); q8 += 32; 2169 __m256i p16h = _mm256_maddubs_epi16(q4h, q8h); 2170 p16h = _mm256_madd_epi16(scale_h, p16h); 2171 const __m256i sumj = _mm256_add_epi32(p16l, p16h); 2172 2173 sumi = _mm256_add_epi32(sumi, sumj); 2174 } 2175 2176 __m256 vd = _mm256_set1_ps(d); 2177 acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc); 2178 2179 } 2180 2181 acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m)); 2182 acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m)); 2183 2184 *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m); 2185 2186 #elif defined __AVX__ 2187 2188 const __m128i m4 = _mm_set1_epi8(0xF); 2189 const __m128i m2 = _mm_set1_epi8(0x2); 2190 2191 __m256 acc = _mm256_setzero_ps(); 2192 __m128 acc_m = _mm_setzero_ps(); 2193 2194 for (int i = 0; i < nb; ++i) { 2195 2196 const float d = y[i].d * GGML_CPU_FP16_TO_FP32(x[i].d); 2197 const float dmin = -y[i].d * GGML_CPU_FP16_TO_FP32(x[i].dmin); 2198 2199 const uint8_t * GGML_RESTRICT q4 = x[i].qs; 2200 const int8_t * GGML_RESTRICT q8 = y[i].qs; 2201 2202 memcpy(utmp, x[i].scales, 12); 2203 utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4); 2204 const uint32_t uaux = utmp[1] & kmask1; 2205 utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4); 2206 utmp[2] = uaux; 2207 utmp[0] &= kmask1; 2208 2209 const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]); 2210 const __m128i scales = _mm_cvtepu8_epi16(utmps); 2211 const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps)); 2212 2213 const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]); 2214 const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]); 2215 const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1); 2216 const __m128i prod = _mm_madd_epi16(mins, q8s); 2217 acc_m = _mm_add_ps(_mm_mul_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod)), acc_m); 2218 2219 __m128i sumi_0 = _mm_setzero_si128(); 2220 __m128i sumi_1 = _mm_setzero_si128(); 2221 2222 __m128i shuffle = _mm_set1_epi16(0x0100); 2223 for (int j = 0; j < QK_K/64; ++j) { 2224 2225 const __m128i scale_l = _mm_shuffle_epi8(scales, shuffle); 2226 shuffle = _mm_add_epi16(shuffle, m2); 2227 const __m128i scale_h = _mm_shuffle_epi8(scales, shuffle); 2228 shuffle = _mm_add_epi16(shuffle, m2); 2229 2230 __m128i q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16; 2231 const __m128i q4l_0 = _mm_and_si128(q4bits, m4); 2232 const __m128i q4h_0 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4); 2233 q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16; 2234 const __m128i q4l_1 = _mm_and_si128(q4bits, m4); 2235 const __m128i q4h_1 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4); 2236 2237 const __m128i q8l_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 2238 __m128i p16l = _mm_maddubs_epi16(q4l_0, q8l_0); 2239 p16l = _mm_madd_epi16(scale_l, p16l); 2240 sumi_0 = _mm_add_epi32(sumi_0, p16l); 2241 const __m128i q8l_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 2242 p16l = _mm_maddubs_epi16(q4l_1, q8l_1); 2243 p16l = _mm_madd_epi16(scale_l, p16l); 2244 sumi_1 = _mm_add_epi32(sumi_1, p16l); 2245 2246 const __m128i q8h_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 2247 __m128i p16h = _mm_maddubs_epi16(q4h_0, q8h_0); 2248 p16h = _mm_madd_epi16(scale_h, p16h); 2249 sumi_0 = _mm_add_epi32(sumi_0, p16h); 2250 const __m128i q8h_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 2251 p16h = _mm_maddubs_epi16(q4h_1, q8h_1); 2252 p16h = _mm_madd_epi16(scale_h, p16h); 2253 sumi_1 = _mm_add_epi32(sumi_1, p16h); 2254 2255 } 2256 2257 __m256 vd = _mm256_set1_ps(d); 2258 __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0); 2259 acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc); 2260 2261 } 2262 2263 acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m)); 2264 acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m)); 2265 2266 *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m); 2267 2268 #else 2269 UNUSED(x); 2270 UNUSED(y); 2271 UNUSED(nb); 2272 UNUSED(kmask1); 2273 UNUSED(kmask2); 2274 UNUSED(kmask3); 2275 UNUSED(utmp); 2276 ggml_vec_dot_q4_K_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc); 2277 #endif 2278 } 2279 2280 void ggml_vec_dot_q5_K_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { 2281 assert(n % QK_K == 0); 2282 assert(nrc == 1); 2283 UNUSED(nrc); 2284 UNUSED(bx); 2285 UNUSED(by); 2286 UNUSED(bs); 2287 2288 const block_q5_K * GGML_RESTRICT x = vx; 2289 const block_q8_K * GGML_RESTRICT y = vy; 2290 2291 const int nb = n / QK_K; 2292 2293 static const uint32_t kmask1 = 0x3f3f3f3f; 2294 static const uint32_t kmask2 = 0x0f0f0f0f; 2295 static const uint32_t kmask3 = 0x03030303; 2296 2297 uint32_t utmp[4]; 2298 2299 #if defined __AVX2__ 2300 2301 const __m256i m4 = _mm256_set1_epi8(0xF); 2302 const __m128i mzero = _mm_setzero_si128(); 2303 const __m256i mone = _mm256_set1_epi8(1); 2304 2305 __m256 acc = _mm256_setzero_ps(); 2306 2307 float summs = 0.f; 2308 2309 for (int i = 0; i < nb; ++i) { 2310 const uint8_t * GGML_RESTRICT q5 = x[i].qs; 2311 const int8_t * GGML_RESTRICT q8 = y[i].qs; 2312 2313 const float d = y[i].d * GGML_CPU_FP16_TO_FP32(x[i].d); 2314 const float dmin = -y[i].d * GGML_CPU_FP16_TO_FP32(x[i].dmin); 2315 2316 memcpy(utmp, x[i].scales, 12); 2317 utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4); 2318 const uint32_t uaux = utmp[1] & kmask1; 2319 utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4); 2320 utmp[2] = uaux; 2321 utmp[0] &= kmask1; 2322 2323 const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0])); 2324 2325 const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums); 2326 const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1)); 2327 const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s); 2328 const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero); 2329 summs += dmin * _mm_extract_epi32(hsum, 0); 2330 2331 const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0); 2332 const __m256i scales = MM256_SET_M128I(sc128, sc128); 2333 2334 const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].qh); 2335 __m256i hmask = mone; 2336 2337 __m256i sumi = _mm256_setzero_si256(); 2338 2339 int bit = 0; 2340 2341 for (int j = 0; j < QK_K/64; ++j) { 2342 2343 const __m256i scale_0 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0)); 2344 const __m256i scale_1 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1)); 2345 2346 const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5); q5 += 32; 2347 2348 const __m256i q5l_0 = _mm256_and_si256(q5bits, m4); 2349 const __m256i q5h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4); 2350 const __m256i q5_0 = _mm256_add_epi8(q5l_0, q5h_0); 2351 hmask = _mm256_slli_epi16(hmask, 1); 2352 2353 const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4); 2354 const __m256i q5h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4); 2355 const __m256i q5_1 = _mm256_add_epi8(q5l_1, q5h_1); 2356 hmask = _mm256_slli_epi16(hmask, 1); 2357 2358 const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32; 2359 const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32; 2360 2361 __m256i p16_0 = _mm256_maddubs_epi16(q5_0, q8_0); 2362 __m256i p16_1 = _mm256_maddubs_epi16(q5_1, q8_1); 2363 2364 p16_0 = _mm256_madd_epi16(scale_0, p16_0); 2365 p16_1 = _mm256_madd_epi16(scale_1, p16_1); 2366 2367 sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1)); 2368 2369 } 2370 2371 __m256 vd = _mm256_set1_ps(d); 2372 acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc); 2373 2374 } 2375 2376 *s = hsum_float_8(acc) + summs; 2377 2378 #elif defined __AVX__ 2379 2380 const __m128i m4 = _mm_set1_epi8(0xF); 2381 const __m128i mzero = _mm_setzero_si128(); 2382 const __m128i mone = _mm_set1_epi8(1); 2383 const __m128i m2 = _mm_set1_epi8(2); 2384 2385 __m256 acc = _mm256_setzero_ps(); 2386 2387 float summs = 0.f; 2388 2389 for (int i = 0; i < nb; ++i) { 2390 2391 const float d = y[i].d * GGML_CPU_FP16_TO_FP32(x[i].d); 2392 const float dmin = -y[i].d * GGML_CPU_FP16_TO_FP32(x[i].dmin); 2393 2394 const uint8_t * GGML_RESTRICT q5 = x[i].qs; 2395 const int8_t * GGML_RESTRICT q8 = y[i].qs; 2396 2397 memcpy(utmp, x[i].scales, 12); 2398 utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4); 2399 const uint32_t uaux = utmp[1] & kmask1; 2400 utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4); 2401 utmp[2] = uaux; 2402 utmp[0] &= kmask1; 2403 2404 const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]); 2405 const __m128i scales = _mm_cvtepu8_epi16(utmps); 2406 const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps)); 2407 2408 const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]); 2409 const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]); 2410 const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1); 2411 const __m128i prod = _mm_madd_epi16(mins, q8s); 2412 const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero); 2413 summs += dmin * _mm_extract_epi32(hsum, 0); 2414 2415 const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].qh[0]); 2416 const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].qh[16]); 2417 __m128i hmask = mone; 2418 2419 __m128i sumi_0 = _mm_setzero_si128(); 2420 __m128i sumi_1 = _mm_setzero_si128(); 2421 2422 int bit = 0; 2423 2424 __m128i shuffle = _mm_set1_epi16(0x0100); 2425 for (int j = 0; j < QK_K/64; ++j) { 2426 2427 const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle); 2428 shuffle = _mm_add_epi16(shuffle, m2); 2429 const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle); 2430 shuffle = _mm_add_epi16(shuffle, m2); 2431 2432 const __m128i q5bits_0 = _mm_loadu_si128((const __m128i*)q5); q5 += 16; 2433 const __m128i q5bits_1 = _mm_loadu_si128((const __m128i*)q5); q5 += 16; 2434 2435 __m128i q5l_0 = _mm_and_si128(q5bits_0, m4); 2436 __m128i q5l_1 = _mm_and_si128(q5bits_1, m4); 2437 __m128i q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4); 2438 __m128i q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4); 2439 __m128i q5_0 = _mm_add_epi8(q5l_0, q5h_0); 2440 __m128i q5_1 = _mm_add_epi8(q5l_1, q5h_1); 2441 hmask = _mm_slli_epi16(hmask, 1); 2442 2443 __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 2444 __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 2445 __m128i p16_0 = _mm_maddubs_epi16(q5_0, q8_0); 2446 __m128i p16_1 = _mm_maddubs_epi16(q5_1, q8_1); 2447 p16_0 = _mm_madd_epi16(scale_0, p16_0); 2448 p16_1 = _mm_madd_epi16(scale_0, p16_1); 2449 2450 q5l_0 = _mm_and_si128(_mm_srli_epi16(q5bits_0, 4), m4); 2451 q5l_1 = _mm_and_si128(_mm_srli_epi16(q5bits_1, 4), m4); 2452 q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4); 2453 q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4); 2454 q5_0 = _mm_add_epi8(q5l_0, q5h_0); 2455 q5_1 = _mm_add_epi8(q5l_1, q5h_1); 2456 hmask = _mm_slli_epi16(hmask, 1); 2457 2458 q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 2459 q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 2460 __m128i p16_2 = _mm_maddubs_epi16(q5_0, q8_0); 2461 __m128i p16_3 = _mm_maddubs_epi16(q5_1, q8_1); 2462 p16_2 = _mm_madd_epi16(scale_1, p16_2); 2463 p16_3 = _mm_madd_epi16(scale_1, p16_3); 2464 2465 sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2)); 2466 sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3)); 2467 2468 } 2469 2470 __m256 vd = _mm256_set1_ps(d); 2471 __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0); 2472 acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc); 2473 2474 } 2475 2476 *s = hsum_float_8(acc) + summs; 2477 2478 #else 2479 UNUSED(x); 2480 UNUSED(y); 2481 UNUSED(nb); 2482 UNUSED(kmask1); 2483 UNUSED(kmask2); 2484 UNUSED(kmask3); 2485 UNUSED(utmp); 2486 ggml_vec_dot_q5_K_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc); 2487 #endif 2488 } 2489 2490 void ggml_vec_dot_q6_K_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { 2491 assert(n % QK_K == 0); 2492 assert(nrc == 1); 2493 UNUSED(nrc); 2494 UNUSED(bx); 2495 UNUSED(by); 2496 UNUSED(bs); 2497 2498 const block_q6_K * GGML_RESTRICT x = vx; 2499 const block_q8_K * GGML_RESTRICT y = vy; 2500 2501 const int nb = n / QK_K; 2502 2503 #if defined __AVX2__ 2504 2505 const __m256i m3 = _mm256_set1_epi8(3); 2506 const __m256i m15 = _mm256_set1_epi8(15); 2507 2508 __m256 acc = _mm256_setzero_ps(); 2509 2510 for (int i = 0; i < nb; ++i) { 2511 2512 const float d = y[i].d * GGML_CPU_FP16_TO_FP32(x[i].d); 2513 2514 const uint8_t * GGML_RESTRICT q4 = x[i].ql; 2515 const uint8_t * GGML_RESTRICT qh = x[i].qh; 2516 const int8_t * GGML_RESTRICT q8 = y[i].qs; 2517 2518 const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums); 2519 const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales); 2520 const __m256i scales_16 = _mm256_cvtepi8_epi16(scales); 2521 const __m256i q8sclsub = _mm256_slli_epi32(_mm256_madd_epi16(q8sums, scales_16), 5); 2522 2523 __m256i sumi = _mm256_setzero_si256(); 2524 2525 int is = 0; 2526 2527 for (int j = 0; j < QK_K/128; ++j) { 2528 const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32; 2529 const __m256i q4bits2 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32; 2530 const __m256i q4bitsH = _mm256_loadu_si256((const __m256i*)qh); qh += 32; 2531 2532 const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(q4bitsH, m3), 4); 2533 const __m256i q4h_1 = _mm256_slli_epi16(_mm256_and_si256(q4bitsH, _mm256_set1_epi8(12)), 2); 2534 const __m256i q4h_2 = _mm256_and_si256(q4bitsH, _mm256_set1_epi8(48)); 2535 const __m256i q4h_3 = _mm256_srli_epi16(_mm256_and_si256(q4bitsH, _mm256_set1_epi8(-64)), 2); 2536 2537 const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m15), q4h_0); 2538 const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(q4bits2, m15), q4h_1); 2539 const __m256i q4_2 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m15), q4h_2); 2540 const __m256i q4_3 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits2, 4), m15), q4h_3); 2541 2542 const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32; 2543 const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32; 2544 const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32; 2545 const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32; 2546 2547 __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0); 2548 __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1); 2549 __m256i p16_2 = _mm256_maddubs_epi16(q4_2, q8_2); 2550 __m256i p16_3 = _mm256_maddubs_epi16(q4_3, q8_3); 2551 2552 const __m128i scale_0 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 0)); 2553 const __m128i scale_1 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 1)); 2554 const __m128i scale_2 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 2)); 2555 const __m128i scale_3 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 3)); 2556 is += 4; 2557 2558 p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0); 2559 p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1); 2560 p16_2 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_2), p16_2); 2561 p16_3 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_3), p16_3); 2562 2563 sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1)); 2564 sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_2, p16_3)); 2565 2566 } 2567 2568 sumi = _mm256_sub_epi32(sumi, q8sclsub); 2569 acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc); 2570 } 2571 2572 *s = hsum_float_8(acc); 2573 2574 #elif defined __AVX__ 2575 2576 const __m128i m3 = _mm_set1_epi8(3); 2577 const __m128i m15 = _mm_set1_epi8(15); 2578 2579 __m256 acc = _mm256_setzero_ps(); 2580 2581 for (int i = 0; i < nb; ++i) { 2582 2583 const float d = y[i].d * GGML_CPU_FP16_TO_FP32(x[i].d); 2584 2585 const uint8_t * GGML_RESTRICT q4 = x[i].ql; 2586 const uint8_t * GGML_RESTRICT qh = x[i].qh; 2587 const int8_t * GGML_RESTRICT q8 = y[i].qs; 2588 2589 // handle the q6_k -32 offset separately using bsums 2590 const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)y[i].bsums); 2591 const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)y[i].bsums + 1); 2592 const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales); 2593 const __m128i scales_16_0 = _mm_cvtepi8_epi16(scales); 2594 const __m128i scales_16_1 = _mm_cvtepi8_epi16(_mm_bsrli_si128(scales, 8)); 2595 const __m128i q8sclsub_0 = _mm_slli_epi32(_mm_madd_epi16(q8sums_0, scales_16_0), 5); 2596 const __m128i q8sclsub_1 = _mm_slli_epi32(_mm_madd_epi16(q8sums_1, scales_16_1), 5); 2597 2598 __m128i sumi_0 = _mm_setzero_si128(); 2599 __m128i sumi_1 = _mm_setzero_si128(); 2600 2601 int is = 0; 2602 2603 for (int j = 0; j < QK_K/128; ++j) { 2604 2605 const __m128i q4bitsH_0 = _mm_loadu_si128((const __m128i*)qh); qh += 16; 2606 const __m128i q4bitsH_1 = _mm_loadu_si128((const __m128i*)qh); qh += 16; 2607 2608 const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH_0, m3), 4); 2609 const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(q4bitsH_1, m3), 4); 2610 const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(q4bitsH_0, _mm_set1_epi8(12)), 2); 2611 const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(q4bitsH_1, _mm_set1_epi8(12)), 2); 2612 const __m128i q4h_4 = _mm_and_si128(q4bitsH_0, _mm_set1_epi8(48)); 2613 const __m128i q4h_5 = _mm_and_si128(q4bitsH_1, _mm_set1_epi8(48)); 2614 const __m128i q4h_6 = _mm_srli_epi16(_mm_and_si128(q4bitsH_0, _mm_set1_epi8(-64)), 2); 2615 const __m128i q4h_7 = _mm_srli_epi16(_mm_and_si128(q4bitsH_1, _mm_set1_epi8(-64)), 2); 2616 2617 const __m128i q4bits1_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16; 2618 const __m128i q4bits1_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16; 2619 const __m128i q4bits2_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16; 2620 const __m128i q4bits2_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16; 2621 2622 const __m128i q4_0 = _mm_or_si128(_mm_and_si128(q4bits1_0, m15), q4h_0); 2623 const __m128i q4_1 = _mm_or_si128(_mm_and_si128(q4bits1_1, m15), q4h_1); 2624 const __m128i q4_2 = _mm_or_si128(_mm_and_si128(q4bits2_0, m15), q4h_2); 2625 const __m128i q4_3 = _mm_or_si128(_mm_and_si128(q4bits2_1, m15), q4h_3); 2626 const __m128i q4_4 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_0, 4), m15), q4h_4); 2627 const __m128i q4_5 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_1, 4), m15), q4h_5); 2628 const __m128i q4_6 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_0, 4), m15), q4h_6); 2629 const __m128i q4_7 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_1, 4), m15), q4h_7); 2630 2631 const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 2632 const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 2633 const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 2634 const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 2635 const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 2636 const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 2637 const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 2638 const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16; 2639 2640 __m128i p16_0 = _mm_maddubs_epi16(q4_0, q8_0); 2641 __m128i p16_1 = _mm_maddubs_epi16(q4_1, q8_1); 2642 __m128i p16_2 = _mm_maddubs_epi16(q4_2, q8_2); 2643 __m128i p16_3 = _mm_maddubs_epi16(q4_3, q8_3); 2644 __m128i p16_4 = _mm_maddubs_epi16(q4_4, q8_4); 2645 __m128i p16_5 = _mm_maddubs_epi16(q4_5, q8_5); 2646 __m128i p16_6 = _mm_maddubs_epi16(q4_6, q8_6); 2647 __m128i p16_7 = _mm_maddubs_epi16(q4_7, q8_7); 2648 2649 const __m128i scale_0 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 0)); 2650 const __m128i scale_1 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 1)); 2651 const __m128i scale_2 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 2)); 2652 const __m128i scale_3 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 3)); 2653 is += 4; 2654 2655 p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0); 2656 p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_bsrli_si128(scale_0, 8)), p16_1); 2657 p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2); 2658 p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_bsrli_si128(scale_1, 8)), p16_3); 2659 p16_4 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_2), p16_4); 2660 p16_5 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_bsrli_si128(scale_2, 8)), p16_5); 2661 p16_6 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_3), p16_6); 2662 p16_7 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_bsrli_si128(scale_3, 8)), p16_7); 2663 2664 sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2)); 2665 sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3)); 2666 sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_4, p16_6)); 2667 sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_5, p16_7)); 2668 2669 } 2670 2671 sumi_0 = _mm_sub_epi32(sumi_0, q8sclsub_0); 2672 sumi_1 = _mm_sub_epi32(sumi_1, q8sclsub_1); 2673 const __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0); 2674 acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(sumi)), acc); 2675 } 2676 2677 *s = hsum_float_8(acc); 2678 2679 #else 2680 UNUSED(x); 2681 UNUSED(y); 2682 UNUSED(nb); 2683 ggml_vec_dot_q6_K_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc); 2684 #endif 2685 } 2686 2687 #if defined (__AVX__) || defined (__AVX2__) 2688 static const int8_t keven_signs_q2xs[1024] = { 2689 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 2690 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1, 2691 1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, -1, 2692 1, 1, -1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 2693 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1, 2694 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 2695 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1, 2696 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, -1, 2697 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1, 2698 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1, 2699 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1, 2700 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1, 2701 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1, 2702 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1, 2703 1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1, 2704 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 2705 1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, -1, 2706 1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, 1, 2707 1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, 2708 1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1, 2709 1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, 1, 2710 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1, 2711 1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1, 2712 1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1, 2713 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1, 2714 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1, 2715 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1, 2716 1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1, 2717 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1, 2718 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1, 2719 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1, 2720 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 2721 }; 2722 #endif 2723 2724 void ggml_vec_dot_iq2_xxs_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { 2725 assert(n % QK_K == 0); 2726 assert(nrc == 1); 2727 UNUSED(nrc); 2728 UNUSED(bx); 2729 UNUSED(by); 2730 UNUSED(bs); 2731 2732 const block_iq2_xxs * GGML_RESTRICT x = vx; 2733 const block_q8_K * GGML_RESTRICT y = vy; 2734 2735 const int nb = n / QK_K; 2736 2737 #if defined(__AVX2__) 2738 2739 const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs; 2740 2741 uint32_t aux32[4]; 2742 const uint8_t * aux8 = (const uint8_t *)aux32; 2743 2744 __m256 accumf = _mm256_setzero_ps(); 2745 for (int i = 0; i < nb; ++i) { 2746 const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d; 2747 const uint16_t * GGML_RESTRICT q2 = x[i].qs; 2748 const int8_t * GGML_RESTRICT q8 = y[i].qs; 2749 __m256i sumi1 = _mm256_setzero_si256(); 2750 __m256i sumi2 = _mm256_setzero_si256(); 2751 for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) { 2752 const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32; 2753 const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32; 2754 memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8; 2755 const __m256i q2_1 = _mm256_set_epi64x(iq2xxs_grid[aux8[ 3]], iq2xxs_grid[aux8[ 2]], iq2xxs_grid[aux8[1]], iq2xxs_grid[aux8[0]]); 2756 const __m256i q2_2 = _mm256_set_epi64x(iq2xxs_grid[aux8[11]], iq2xxs_grid[aux8[10]], iq2xxs_grid[aux8[9]], iq2xxs_grid[aux8[8]]); 2757 const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127], 2758 signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]); 2759 const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[3] >> 21) & 127], signs64[(aux32[3] >> 14) & 127], 2760 signs64[(aux32[3] >> 7) & 127], signs64[(aux32[3] >> 0) & 127]); 2761 const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1); 2762 const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2); 2763 const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1); 2764 const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2); 2765 const uint16_t ls1 = aux32[1] >> 28; 2766 const uint16_t ls2 = aux32[3] >> 28; 2767 const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1)); 2768 const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1)); 2769 sumi1 = _mm256_add_epi32(sumi1, p1); 2770 sumi2 = _mm256_add_epi32(sumi2, p2); 2771 } 2772 2773 accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf); 2774 2775 } 2776 2777 *s = 0.125f * hsum_float_8(accumf); 2778 2779 #elif defined(__AVX__) 2780 const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs; 2781 2782 uint32_t aux32[4]; 2783 const uint8_t * aux8 = (const uint8_t *)aux32; 2784 2785 __m256 accumf = _mm256_setzero_ps(); 2786 for (int i = 0; i < nb; ++i) { 2787 const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d; 2788 const uint16_t * GGML_RESTRICT q2 = x[i].qs; 2789 const int8_t * GGML_RESTRICT q8 = y[i].qs; 2790 __m128i sumi1_0 = _mm_setzero_si128(); 2791 __m128i sumi1_1 = _mm_setzero_si128(); 2792 __m128i sumi2_0 = _mm_setzero_si128(); 2793 __m128i sumi2_1 = _mm_setzero_si128(); 2794 for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) { 2795 const __m128i q8_1_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 2796 const __m128i q8_1_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 2797 const __m128i q8_2_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 2798 const __m128i q8_2_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 2799 memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8; 2800 const __m128i q2_1_0 = _mm_set_epi64x(iq2xxs_grid[aux8[1]], iq2xxs_grid[aux8[0]]); 2801 const __m128i q2_1_1 = _mm_set_epi64x(iq2xxs_grid[aux8[3]], iq2xxs_grid[aux8[2]]); 2802 const __m128i q2_2_0 = _mm_set_epi64x(iq2xxs_grid[aux8[9]], iq2xxs_grid[aux8[8]]); 2803 const __m128i q2_2_1 = _mm_set_epi64x(iq2xxs_grid[aux8[11]], iq2xxs_grid[aux8[10]]); 2804 const __m128i s2_1_0 = _mm_set_epi64x(signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]); 2805 const __m128i s2_1_1 = _mm_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127]); 2806 const __m128i s2_2_0 = _mm_set_epi64x(signs64[(aux32[3] >> 7) & 127], signs64[(aux32[3] >> 0) & 127]); 2807 const __m128i s2_2_1 = _mm_set_epi64x(signs64[(aux32[3] >> 21) & 127], signs64[(aux32[3] >> 14) & 127]); 2808 const __m128i q8s_1_0 = _mm_sign_epi8(q8_1_0, s2_1_0); 2809 const __m128i q8s_1_1 = _mm_sign_epi8(q8_1_1, s2_1_1); 2810 const __m128i q8s_2_0 = _mm_sign_epi8(q8_2_0, s2_2_0); 2811 const __m128i q8s_2_1 = _mm_sign_epi8(q8_2_1, s2_2_1); 2812 const __m128i dot1_0 = _mm_maddubs_epi16(q2_1_0, q8s_1_0); 2813 const __m128i dot1_1 = _mm_maddubs_epi16(q2_1_1, q8s_1_1); 2814 const __m128i dot2_0 = _mm_maddubs_epi16(q2_2_0, q8s_2_0); 2815 const __m128i dot2_1 = _mm_maddubs_epi16(q2_2_1, q8s_2_1); 2816 const uint16_t ls1 = aux32[1] >> 28; 2817 const uint16_t ls2 = aux32[3] >> 28; 2818 const __m128i p1_0 = _mm_madd_epi16(dot1_0, _mm_set1_epi16(2*ls1+1)); 2819 const __m128i p1_1 = _mm_madd_epi16(dot1_1, _mm_set1_epi16(2*ls1+1)); 2820 const __m128i p2_0 = _mm_madd_epi16(dot2_0, _mm_set1_epi16(2*ls2+1)); 2821 const __m128i p2_1 = _mm_madd_epi16(dot2_1, _mm_set1_epi16(2*ls2+1)); 2822 sumi1_0 = _mm_add_epi32(sumi1_0, p1_0); 2823 sumi1_1 = _mm_add_epi32(sumi1_1, p1_1); 2824 sumi2_0 = _mm_add_epi32(sumi2_0, p2_0); 2825 sumi2_1 = _mm_add_epi32(sumi2_1, p2_1); 2826 } 2827 2828 accumf = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(_mm_add_epi32(sumi1_1, sumi2_1), _mm_add_epi32(sumi1_0, sumi2_0)))), accumf); 2829 2830 } 2831 2832 *s = 0.125f * hsum_float_8(accumf); 2833 2834 #else 2835 UNUSED(x); 2836 UNUSED(y); 2837 UNUSED(nb); 2838 ggml_vec_dot_iq2_xxs_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc); 2839 #endif 2840 } 2841 2842 void ggml_vec_dot_iq2_xs_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { 2843 assert(n % QK_K == 0); 2844 assert(nrc == 1); 2845 UNUSED(nrc); 2846 UNUSED(bx); 2847 UNUSED(by); 2848 UNUSED(bs); 2849 2850 const block_iq2_xs * GGML_RESTRICT x = vx; 2851 const block_q8_K * GGML_RESTRICT y = vy; 2852 2853 const int nb = n / QK_K; 2854 2855 #if defined(__AVX2__) 2856 2857 const __m256i mone = _mm256_set1_epi8(1); 2858 static const char block_sign_shuffle_mask_1[32] = { 2859 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 2860 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 2861 }; 2862 static const char block_sign_shuffle_mask_2[32] = { 2863 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 2864 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 2865 }; 2866 static const uint8_t bit_selector_mask_bytes[32] = { 2867 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 2868 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 2869 }; 2870 2871 const __m256i bit_selector_mask = _mm256_loadu_si256((const __m256i*)bit_selector_mask_bytes); 2872 const __m256i block_sign_shuffle_1 = _mm256_loadu_si256((const __m256i*)block_sign_shuffle_mask_1); 2873 const __m256i block_sign_shuffle_2 = _mm256_loadu_si256((const __m256i*)block_sign_shuffle_mask_2); 2874 2875 static const uint8_t k_bit_helper[32] = { 2876 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00, 2877 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00, 2878 }; 2879 const __m256i bit_helper = _mm256_loadu_si256((const __m256i*)k_bit_helper); 2880 const __m256i m511 = _mm256_set1_epi16(511); 2881 const __m128i m4 = _mm_set1_epi8(0xf); 2882 const __m128i m1 = _mm_set1_epi8(1); 2883 2884 uint64_t aux64; 2885 2886 // somewhat hacky, but gives a significant boost in performance 2887 __m256i aux_gindex; 2888 const uint16_t * gindex = (const uint16_t *)&aux_gindex; 2889 2890 __m256 accumf = _mm256_setzero_ps(); 2891 for (int i = 0; i < nb; ++i) { 2892 const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d; 2893 const uint16_t * GGML_RESTRICT q2 = x[i].qs; 2894 const int8_t * GGML_RESTRICT q8 = y[i].qs; 2895 2896 memcpy(&aux64, x[i].scales, 8); 2897 __m128i stmp = _mm_set1_epi64x(aux64); 2898 stmp = _mm_unpacklo_epi8(_mm_and_si128(stmp, m4), _mm_and_si128(_mm_srli_epi16(stmp, 4), m4)); 2899 const __m128i scales = _mm_add_epi8(_mm_slli_epi16(stmp, 1), m1); 2900 2901 __m256i sumi1 = _mm256_setzero_si256(); 2902 __m256i sumi2 = _mm256_setzero_si256(); 2903 for (int ib32 = 0; ib32 < QK_K/32; ib32 += 4) { 2904 2905 const __m256i q2_data = _mm256_loadu_si256((const __m256i*)q2); q2 += 16; 2906 aux_gindex = _mm256_and_si256(q2_data, m511); 2907 2908 const __m256i partial_sign_bits = _mm256_srli_epi16(q2_data, 9); 2909 const __m256i partial_sign_bits_upper = _mm256_srli_epi16(q2_data, 13); 2910 const __m256i partial_sign_bits_for_counting = _mm256_xor_si256(partial_sign_bits, partial_sign_bits_upper); 2911 2912 const __m256i odd_bits = _mm256_shuffle_epi8(bit_helper, partial_sign_bits_for_counting); 2913 const __m256i full_sign_bits = _mm256_or_si256(partial_sign_bits, odd_bits); 2914 2915 const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32; 2916 const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32; 2917 const __m256i q8_3 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32; 2918 const __m256i q8_4 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32; 2919 2920 const __m256i q2_1 = _mm256_set_epi64x(iq2xs_grid[gindex[ 3]], iq2xs_grid[gindex[ 2]], 2921 iq2xs_grid[gindex[ 1]], iq2xs_grid[gindex[ 0]]); 2922 const __m256i q2_2 = _mm256_set_epi64x(iq2xs_grid[gindex[ 7]], iq2xs_grid[gindex[ 6]], 2923 iq2xs_grid[gindex[ 5]], iq2xs_grid[gindex[ 4]]); 2924 const __m256i q2_3 = _mm256_set_epi64x(iq2xs_grid[gindex[11]], iq2xs_grid[gindex[10]], 2925 iq2xs_grid[gindex[ 9]], iq2xs_grid[gindex[ 8]]); 2926 const __m256i q2_4 = _mm256_set_epi64x(iq2xs_grid[gindex[15]], iq2xs_grid[gindex[14]], 2927 iq2xs_grid[gindex[13]], iq2xs_grid[gindex[12]]); 2928 2929 const __m128i full_signs_l = _mm256_castsi256_si128(full_sign_bits); 2930 const __m128i full_signs_h = _mm256_extractf128_si256(full_sign_bits, 1); 2931 const __m256i full_signs_1 = MM256_SET_M128I(full_signs_l, full_signs_l); 2932 const __m256i full_signs_2 = MM256_SET_M128I(full_signs_h, full_signs_h); 2933 2934 __m256i signs; 2935 signs = _mm256_shuffle_epi8(full_signs_1, block_sign_shuffle_1); 2936 signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask); 2937 const __m256i q8s_1 = _mm256_sign_epi8(q8_1, _mm256_or_si256(signs, mone)); 2938 2939 signs = _mm256_shuffle_epi8(full_signs_1, block_sign_shuffle_2); 2940 signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask); 2941 const __m256i q8s_2 = _mm256_sign_epi8(q8_2, _mm256_or_si256(signs, mone)); 2942 2943 signs = _mm256_shuffle_epi8(full_signs_2, block_sign_shuffle_1); 2944 signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask); 2945 const __m256i q8s_3 = _mm256_sign_epi8(q8_3, _mm256_or_si256(signs, mone)); 2946 2947 signs = _mm256_shuffle_epi8(full_signs_2, block_sign_shuffle_2); 2948 signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask); 2949 const __m256i q8s_4 = _mm256_sign_epi8(q8_4, _mm256_or_si256(signs, mone)); 2950 2951 const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1); 2952 const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2); 2953 const __m256i dot3 = _mm256_maddubs_epi16(q2_3, q8s_3); 2954 const __m256i dot4 = _mm256_maddubs_epi16(q2_4, q8s_4); 2955 2956 const __m256i sc1 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+0))); 2957 const __m256i sc2 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+1))); 2958 const __m256i sc3 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+2))); 2959 const __m256i sc4 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+3))); 2960 2961 sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot1, sc1)); 2962 sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot2, sc2)); 2963 sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot3, sc3)); 2964 sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot4, sc4)); 2965 } 2966 2967 accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf); 2968 2969 } 2970 2971 *s = 0.125f * hsum_float_8(accumf); 2972 2973 #elif defined(__AVX__) 2974 const __m128i mone = _mm_set1_epi8(1); 2975 static const char block_sign_shuffle_mask_1[32] = { 2976 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 2977 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 2978 }; 2979 static const char block_sign_shuffle_mask_2[32] = { 2980 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 2981 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 2982 }; 2983 static const uint8_t bit_selector_mask_bytes[32] = { 2984 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 2985 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 2986 }; 2987 2988 const __m128i bit_selector_mask_0 = _mm_loadu_si128((const __m128i*)bit_selector_mask_bytes); 2989 const __m128i bit_selector_mask_1 = _mm_loadu_si128((const __m128i*)bit_selector_mask_bytes + 1); 2990 const __m128i block_sign_shuffle_1_0 = _mm_loadu_si128((const __m128i*)block_sign_shuffle_mask_1); 2991 const __m128i block_sign_shuffle_1_1 = _mm_loadu_si128((const __m128i*)block_sign_shuffle_mask_1 + 1); 2992 const __m128i block_sign_shuffle_2_0 = _mm_loadu_si128((const __m128i*)block_sign_shuffle_mask_2); 2993 const __m128i block_sign_shuffle_2_1 = _mm_loadu_si128((const __m128i*)block_sign_shuffle_mask_2 + 1); 2994 2995 static const uint8_t k_bit_helper[32] = { 2996 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00, 2997 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00, 2998 }; 2999 const __m128i bit_helper_0 = _mm_loadu_si128((const __m128i*)k_bit_helper); 3000 const __m128i bit_helper_1 = _mm_loadu_si128((const __m128i*)k_bit_helper + 1); 3001 const __m128i m511 = _mm_set1_epi16(511); 3002 const __m128i m4 = _mm_set1_epi8(0xf); 3003 const __m128i m1 = _mm_set1_epi8(1); 3004 3005 uint64_t aux64; 3006 3007 // somewhat hacky, but gives a significant boost in performance 3008 __m256i aux_gindex; 3009 const uint16_t * gindex = (const uint16_t *)&aux_gindex; 3010 3011 __m256 accumf = _mm256_setzero_ps(); 3012 for (int i = 0; i < nb; ++i) { 3013 const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d; 3014 const uint16_t * GGML_RESTRICT q2 = x[i].qs; 3015 const int8_t * GGML_RESTRICT q8 = y[i].qs; 3016 3017 memcpy(&aux64, x[i].scales, 8); 3018 __m128i stmp = _mm_set1_epi64x(aux64); 3019 stmp = _mm_unpacklo_epi8(_mm_and_si128(stmp, m4), _mm_and_si128(_mm_srli_epi16(stmp, 4), m4)); 3020 const __m128i scales = _mm_add_epi8(_mm_slli_epi16(stmp, 1), m1); 3021 3022 __m128i sumi1_0 = _mm_setzero_si128(); 3023 __m128i sumi1_1 = _mm_setzero_si128(); 3024 __m128i sumi2_0 = _mm_setzero_si128(); 3025 __m128i sumi2_1 = _mm_setzero_si128(); 3026 for (int ib32 = 0; ib32 < QK_K/32; ib32 += 4) { 3027 3028 const __m128i q2_data_0 = _mm_loadu_si128((const __m128i*)q2); 3029 const __m128i q2_data_1 = _mm_loadu_si128((const __m128i*)q2 + 1); q2 += 16; 3030 aux_gindex = MM256_SET_M128I(_mm_and_si128(q2_data_1, m511), _mm_and_si128(q2_data_0, m511)); 3031 3032 const __m128i partial_sign_bits_0 = _mm_srli_epi16(q2_data_0, 9); 3033 const __m128i partial_sign_bits_1 = _mm_srli_epi16(q2_data_1, 9); 3034 const __m128i partial_sign_bits_upper_0 = _mm_srli_epi16(q2_data_0, 13); 3035 const __m128i partial_sign_bits_upper_1 = _mm_srli_epi16(q2_data_1, 13); 3036 const __m128i partial_sign_bits_for_counting_0 = _mm_xor_si128(partial_sign_bits_0, partial_sign_bits_upper_0); 3037 const __m128i partial_sign_bits_for_counting_1 = _mm_xor_si128(partial_sign_bits_1, partial_sign_bits_upper_1); 3038 3039 const __m128i odd_bits_0 = _mm_shuffle_epi8(bit_helper_0, partial_sign_bits_for_counting_0); 3040 const __m128i odd_bits_1 = _mm_shuffle_epi8(bit_helper_1, partial_sign_bits_for_counting_1); 3041 const __m128i full_sign_bits_0 = _mm_or_si128(partial_sign_bits_0, odd_bits_0); 3042 const __m128i full_sign_bits_1 = _mm_or_si128(partial_sign_bits_1, odd_bits_1); 3043 3044 const __m128i q8_1_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3045 const __m128i q8_1_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3046 const __m128i q8_2_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3047 const __m128i q8_2_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3048 const __m128i q8_3_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3049 const __m128i q8_3_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3050 const __m128i q8_4_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3051 const __m128i q8_4_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3052 3053 const __m128i q2_1_0 = _mm_set_epi64x(iq2xs_grid[gindex[1]], iq2xs_grid[gindex[0]]); 3054 const __m128i q2_1_1 = _mm_set_epi64x(iq2xs_grid[gindex[3]], iq2xs_grid[gindex[2]]); 3055 const __m128i q2_2_0 = _mm_set_epi64x(iq2xs_grid[gindex[5]], iq2xs_grid[gindex[4]]); 3056 const __m128i q2_2_1 = _mm_set_epi64x(iq2xs_grid[gindex[7]], iq2xs_grid[gindex[6]]); 3057 const __m128i q2_3_0 = _mm_set_epi64x(iq2xs_grid[gindex[9]], iq2xs_grid[gindex[8]]); 3058 const __m128i q2_3_1 = _mm_set_epi64x(iq2xs_grid[gindex[11]], iq2xs_grid[gindex[10]]); 3059 const __m128i q2_4_0 = _mm_set_epi64x(iq2xs_grid[gindex[13]], iq2xs_grid[gindex[12]]); 3060 const __m128i q2_4_1 = _mm_set_epi64x(iq2xs_grid[gindex[15]], iq2xs_grid[gindex[14]]); 3061 3062 // AVX2 full_signs_1 is full_sign_bits_0 here 3063 // AVX2 full_signs_2 is full_sign_bits_1 here 3064 __m128i signs_0, signs_1; 3065 signs_0 = _mm_shuffle_epi8(full_sign_bits_0, block_sign_shuffle_1_0); 3066 signs_1 = _mm_shuffle_epi8(full_sign_bits_0, block_sign_shuffle_1_1); 3067 signs_0 = _mm_cmpeq_epi8(_mm_and_si128(signs_0, bit_selector_mask_0), bit_selector_mask_0); 3068 signs_1 = _mm_cmpeq_epi8(_mm_and_si128(signs_1, bit_selector_mask_1), bit_selector_mask_1); 3069 const __m128i q8s_1_0 = _mm_sign_epi8(q8_1_0, _mm_or_si128(signs_0, mone)); 3070 const __m128i q8s_1_1 = _mm_sign_epi8(q8_1_1, _mm_or_si128(signs_1, mone)); 3071 3072 signs_0 = _mm_shuffle_epi8(full_sign_bits_0, block_sign_shuffle_2_0); 3073 signs_1 = _mm_shuffle_epi8(full_sign_bits_0, block_sign_shuffle_2_1); 3074 signs_0 = _mm_cmpeq_epi8(_mm_and_si128(signs_0, bit_selector_mask_0), bit_selector_mask_0); 3075 signs_1 = _mm_cmpeq_epi8(_mm_and_si128(signs_1, bit_selector_mask_1), bit_selector_mask_1); 3076 const __m128i q8s_2_0 = _mm_sign_epi8(q8_2_0, _mm_or_si128(signs_0, mone)); 3077 const __m128i q8s_2_1 = _mm_sign_epi8(q8_2_1, _mm_or_si128(signs_1, mone)); 3078 3079 signs_0 = _mm_shuffle_epi8(full_sign_bits_1, block_sign_shuffle_1_0); 3080 signs_1 = _mm_shuffle_epi8(full_sign_bits_1, block_sign_shuffle_1_1); 3081 signs_0 = _mm_cmpeq_epi8(_mm_and_si128(signs_0, bit_selector_mask_0), bit_selector_mask_0); 3082 signs_1 = _mm_cmpeq_epi8(_mm_and_si128(signs_1, bit_selector_mask_1), bit_selector_mask_1); 3083 const __m128i q8s_3_0 = _mm_sign_epi8(q8_3_0, _mm_or_si128(signs_0, mone)); 3084 const __m128i q8s_3_1 = _mm_sign_epi8(q8_3_1, _mm_or_si128(signs_1, mone)); 3085 3086 signs_0 = _mm_shuffle_epi8(full_sign_bits_1, block_sign_shuffle_2_0); 3087 signs_1 = _mm_shuffle_epi8(full_sign_bits_1, block_sign_shuffle_2_1); 3088 signs_0 = _mm_cmpeq_epi8(_mm_and_si128(signs_0, bit_selector_mask_0), bit_selector_mask_0); 3089 signs_1 = _mm_cmpeq_epi8(_mm_and_si128(signs_1, bit_selector_mask_1), bit_selector_mask_1); 3090 const __m128i q8s_4_0 = _mm_sign_epi8(q8_4_0, _mm_or_si128(signs_0, mone)); 3091 const __m128i q8s_4_1 = _mm_sign_epi8(q8_4_1, _mm_or_si128(signs_1, mone)); 3092 3093 const __m128i dot1_0 = _mm_maddubs_epi16(q2_1_0, q8s_1_0); 3094 const __m128i dot1_1 = _mm_maddubs_epi16(q2_1_1, q8s_1_1); 3095 const __m128i dot2_0 = _mm_maddubs_epi16(q2_2_0, q8s_2_0); 3096 const __m128i dot2_1 = _mm_maddubs_epi16(q2_2_1, q8s_2_1); 3097 const __m128i dot3_0 = _mm_maddubs_epi16(q2_3_0, q8s_3_0); 3098 const __m128i dot3_1 = _mm_maddubs_epi16(q2_3_1, q8s_3_1); 3099 const __m128i dot4_0 = _mm_maddubs_epi16(q2_4_0, q8s_4_0); 3100 const __m128i dot4_1 = _mm_maddubs_epi16(q2_4_1, q8s_4_1); 3101 3102 __m128i sc_tmp = _mm_shuffle_epi8(scales, get_scale_shuffle(ib32+0)); 3103 const __m128i sc1_0 = _mm_cvtepi8_epi16(sc_tmp); 3104 const __m128i sc1_1 = _mm_cvtepi8_epi16(_mm_srli_si128(sc_tmp, 8)); 3105 sc_tmp = _mm_shuffle_epi8(scales, get_scale_shuffle(ib32+1)); 3106 const __m128i sc2_0 = _mm_cvtepi8_epi16(sc_tmp); 3107 const __m128i sc2_1 = _mm_cvtepi8_epi16(_mm_srli_si128(sc_tmp, 8)); 3108 sc_tmp = _mm_shuffle_epi8(scales, get_scale_shuffle(ib32+2)); 3109 const __m128i sc3_0 = _mm_cvtepi8_epi16(sc_tmp); 3110 const __m128i sc3_1 = _mm_cvtepi8_epi16(_mm_srli_si128(sc_tmp, 8)); 3111 sc_tmp = _mm_shuffle_epi8(scales, get_scale_shuffle(ib32+3)); 3112 const __m128i sc4_0 = _mm_cvtepi8_epi16(sc_tmp); 3113 const __m128i sc4_1 = _mm_cvtepi8_epi16(_mm_srli_si128(sc_tmp, 8)); 3114 3115 sumi1_0 = _mm_add_epi32(sumi1_0, _mm_madd_epi16(dot1_0, sc1_0)); 3116 sumi1_1 = _mm_add_epi32(sumi1_1, _mm_madd_epi16(dot1_1, sc1_1)); 3117 sumi2_0 = _mm_add_epi32(sumi2_0, _mm_madd_epi16(dot2_0, sc2_0)); 3118 sumi2_1 = _mm_add_epi32(sumi2_1, _mm_madd_epi16(dot2_1, sc2_1)); 3119 sumi1_0 = _mm_add_epi32(sumi1_0, _mm_madd_epi16(dot3_0, sc3_0)); 3120 sumi1_1 = _mm_add_epi32(sumi1_1, _mm_madd_epi16(dot3_1, sc3_1)); 3121 sumi2_0 = _mm_add_epi32(sumi2_0, _mm_madd_epi16(dot4_0, sc4_0)); 3122 sumi2_1 = _mm_add_epi32(sumi2_1, _mm_madd_epi16(dot4_1, sc4_1)); 3123 } 3124 3125 accumf = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(_mm_add_epi32(sumi1_1, sumi2_1), _mm_add_epi32(sumi1_0, sumi2_0)))), accumf); 3126 3127 } 3128 3129 *s = 0.125f * hsum_float_8(accumf); 3130 3131 #else 3132 UNUSED(x); 3133 UNUSED(y); 3134 UNUSED(nb); 3135 ggml_vec_dot_iq2_xs_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc); 3136 #endif 3137 } 3138 3139 void ggml_vec_dot_iq2_s_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { 3140 assert(n % QK_K == 0); 3141 assert(nrc == 1); 3142 UNUSED(nrc); 3143 UNUSED(bx); 3144 UNUSED(by); 3145 UNUSED(bs); 3146 3147 const block_iq2_s * GGML_RESTRICT x = vx; 3148 const block_q8_K * GGML_RESTRICT y = vy; 3149 3150 const int nb = n / QK_K; 3151 3152 #if defined(__AVX2__) 3153 3154 static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 3155 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03 3156 }; 3157 3158 static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 3159 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 3160 }; 3161 3162 const __m128i m4 = _mm_set1_epi8(0xf); 3163 const __m128i m1 = _mm_set1_epi8(1); 3164 3165 const __m256i mask1 = _mm256_loadu_si256((const __m256i*)k_mask1); 3166 const __m256i mask2 = _mm256_loadu_si256((const __m256i*)k_mask2); 3167 3168 uint64_t aux64; 3169 3170 __m256 accumf = _mm256_setzero_ps(); 3171 for (int i = 0; i < nb; ++i) { 3172 const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d; 3173 const uint8_t * GGML_RESTRICT qs = x[i].qs; 3174 const uint8_t * GGML_RESTRICT qh = x[i].qh; 3175 const uint16_t * GGML_RESTRICT signs = (const uint16_t *)(x[i].qs + QK_K/8); 3176 const int8_t * GGML_RESTRICT q8 = y[i].qs; 3177 3178 memcpy(&aux64, x[i].scales, 8); 3179 const __m128i scales8 = _mm_add_epi8(_mm_slli_epi16(_mm_and_si128(_mm_set_epi64x(aux64 >> 4, aux64), m4), 1), m1); 3180 const __m256i scales16 = _mm256_cvtepi8_epi16(scales8); // 0 2 4 6 8 10 12 14 1 3 5 7 9 11 13 15 3181 3182 __m256i sumi1 = _mm256_setzero_si256(); 3183 __m256i sumi2 = _mm256_setzero_si256(); 3184 for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) { 3185 const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32; 3186 const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32; 3187 const __m256i q2_1 = _mm256_set_epi64x(iq2s_grid[qs[3] | ((qh[ib32+0] << 2) & 0x300)], 3188 iq2s_grid[qs[2] | ((qh[ib32+0] << 4) & 0x300)], 3189 iq2s_grid[qs[1] | ((qh[ib32+0] << 6) & 0x300)], 3190 iq2s_grid[qs[0] | ((qh[ib32+0] << 8) & 0x300)]); 3191 const __m256i q2_2 = _mm256_set_epi64x(iq2s_grid[qs[7] | ((qh[ib32+1] << 2) & 0x300)], 3192 iq2s_grid[qs[6] | ((qh[ib32+1] << 4) & 0x300)], 3193 iq2s_grid[qs[5] | ((qh[ib32+1] << 6) & 0x300)], 3194 iq2s_grid[qs[4] | ((qh[ib32+1] << 8) & 0x300)]); 3195 qs += 8; 3196 3197 __m256i aux256 = _mm256_set1_epi32(signs[0] | ((uint32_t) signs[1] << 16)); 3198 aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2); 3199 const __m256i s2_1 = _mm256_cmpeq_epi8(aux256, mask2); 3200 const __m256i q8s_1 = _mm256_sub_epi8(_mm256_xor_si256(s2_1, q8_1), s2_1); 3201 3202 aux256 = _mm256_set1_epi32(signs[2] | ((uint32_t) signs[3] << 16)); 3203 aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2); 3204 const __m256i s2_2 = _mm256_cmpeq_epi8(aux256, mask2); 3205 const __m256i q8s_2 = _mm256_sub_epi8(_mm256_xor_si256(s2_2, q8_2), s2_2); 3206 3207 signs += 4; 3208 3209 const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1); // blocks 2*ib32+0, 2*ib32+1 3210 const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2); // blocks 2*ib32+2, 2*ib32+3 3211 3212 const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_shuffle_epi8(scales16, get_scale_shuffle_k4(ib32+0))); 3213 const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_shuffle_epi8(scales16, get_scale_shuffle_k4(ib32+1))); 3214 sumi1 = _mm256_add_epi32(sumi1, p1); 3215 sumi2 = _mm256_add_epi32(sumi2, p2); 3216 } 3217 3218 accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf); 3219 3220 } 3221 3222 *s = 0.125f * hsum_float_8(accumf); 3223 3224 #elif defined(__AVX__) 3225 static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 3226 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03 3227 }; 3228 3229 static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 3230 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 3231 }; 3232 3233 const __m128i m4 = _mm_set1_epi8(0xf); 3234 const __m128i m1 = _mm_set1_epi8(1); 3235 3236 const __m128i mask1_0 = _mm_loadu_si128((const __m128i*)k_mask1); 3237 const __m128i mask1_1 = _mm_loadu_si128((const __m128i*)k_mask1 + 1); 3238 const __m128i mask2_0 = _mm_loadu_si128((const __m128i*)k_mask2); 3239 const __m128i mask2_1 = _mm_loadu_si128((const __m128i*)k_mask2 + 1); 3240 3241 uint64_t aux64; 3242 3243 __m256 accumf = _mm256_setzero_ps(); 3244 for (int i = 0; i < nb; ++i) { 3245 const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d; 3246 const uint8_t * GGML_RESTRICT qs = x[i].qs; 3247 const uint8_t * GGML_RESTRICT qh = x[i].qh; 3248 const uint16_t * GGML_RESTRICT signs = (const uint16_t *)(x[i].qs + QK_K/8); 3249 const int8_t * GGML_RESTRICT q8 = y[i].qs; 3250 3251 memcpy(&aux64, x[i].scales, 8); 3252 const __m128i scales8 = _mm_add_epi8(_mm_slli_epi16(_mm_and_si128(_mm_set_epi64x(aux64 >> 4, aux64), m4), 1), m1); 3253 const __m128i scales16_0 = _mm_cvtepi8_epi16(scales8); 3254 const __m128i scales16_1 = _mm_cvtepi8_epi16(_mm_srli_si128(scales8, 8)); 3255 3256 __m128i sumi1_0 = _mm_setzero_si128(); 3257 __m128i sumi1_1 = _mm_setzero_si128(); 3258 __m128i sumi2_0 = _mm_setzero_si128(); 3259 __m128i sumi2_1 = _mm_setzero_si128(); 3260 for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) { 3261 const __m128i q8_1_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3262 const __m128i q8_1_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3263 const __m128i q8_2_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3264 const __m128i q8_2_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3265 const __m128i q2_1_0 = _mm_set_epi64x(iq2s_grid[qs[1] | ((qh[ib32+0] << 6) & 0x300)], 3266 iq2s_grid[qs[0] | ((qh[ib32+0] << 8) & 0x300)]); 3267 const __m128i q2_1_1 = _mm_set_epi64x(iq2s_grid[qs[3] | ((qh[ib32+0] << 2) & 0x300)], 3268 iq2s_grid[qs[2] | ((qh[ib32+0] << 4) & 0x300)]); 3269 const __m128i q2_2_0 = _mm_set_epi64x(iq2s_grid[qs[5] | ((qh[ib32+1] << 6) & 0x300)], 3270 iq2s_grid[qs[4] | ((qh[ib32+1] << 8) & 0x300)]); 3271 const __m128i q2_2_1 = _mm_set_epi64x(iq2s_grid[qs[7] | ((qh[ib32+1] << 2) & 0x300)], 3272 iq2s_grid[qs[6] | ((qh[ib32+1] << 4) & 0x300)]); 3273 qs += 8; 3274 3275 __m128i aux128_0 = _mm_set1_epi32(signs[0] | ((uint32_t) signs[1] << 16)); 3276 __m128i aux128_1 = aux128_0; 3277 aux128_0 = _mm_and_si128(_mm_shuffle_epi8(aux128_0,mask1_0), mask2_0); 3278 aux128_1 = _mm_and_si128(_mm_shuffle_epi8(aux128_1,mask1_1), mask2_1); 3279 const __m128i s2_1_0 = _mm_cmpeq_epi8(aux128_0, mask2_0); 3280 const __m128i s2_1_1 = _mm_cmpeq_epi8(aux128_1, mask2_1); 3281 const __m128i q8s_1_0 = _mm_sub_epi8(_mm_xor_si128(s2_1_0, q8_1_0), s2_1_0); 3282 const __m128i q8s_1_1 = _mm_sub_epi8(_mm_xor_si128(s2_1_1, q8_1_1), s2_1_1); 3283 3284 aux128_0 = _mm_set1_epi32(signs[2] | ((uint32_t) signs[3] << 16)); 3285 aux128_1 = aux128_0; 3286 aux128_0 = _mm_and_si128(_mm_shuffle_epi8(aux128_0,mask1_0), mask2_0); 3287 aux128_1 = _mm_and_si128(_mm_shuffle_epi8(aux128_1,mask1_1), mask2_1); 3288 const __m128i s2_2_0 = _mm_cmpeq_epi8(aux128_0, mask2_0); 3289 const __m128i s2_2_1 = _mm_cmpeq_epi8(aux128_1, mask2_1); 3290 const __m128i q8s_2_0 = _mm_sub_epi8(_mm_xor_si128(s2_2_0, q8_2_0), s2_2_0); 3291 const __m128i q8s_2_1 = _mm_sub_epi8(_mm_xor_si128(s2_2_1, q8_2_1), s2_2_1); 3292 3293 signs += 4; 3294 3295 const __m128i dot1_0 = _mm_maddubs_epi16(q2_1_0, q8s_1_0); 3296 const __m128i dot1_1 = _mm_maddubs_epi16(q2_1_1, q8s_1_1); 3297 const __m128i dot2_0 = _mm_maddubs_epi16(q2_2_0, q8s_2_0); 3298 const __m128i dot2_1 = _mm_maddubs_epi16(q2_2_1, q8s_2_1); 3299 3300 const __m128i p1_0 = _mm_madd_epi16(dot1_0, _mm_shuffle_epi8(scales16_0, _mm256_extractf128_si256(get_scale_shuffle_k4(ib32+0), 0))); 3301 const __m128i p1_1 = _mm_madd_epi16(dot1_1, _mm_shuffle_epi8(scales16_1, _mm256_extractf128_si256(get_scale_shuffle_k4(ib32+0), 1))); 3302 const __m128i p2_0 = _mm_madd_epi16(dot2_0, _mm_shuffle_epi8(scales16_0, _mm256_extractf128_si256(get_scale_shuffle_k4(ib32+1), 0))); 3303 const __m128i p2_1 = _mm_madd_epi16(dot2_1, _mm_shuffle_epi8(scales16_1, _mm256_extractf128_si256(get_scale_shuffle_k4(ib32+1), 1))); 3304 sumi1_0 = _mm_add_epi32(sumi1_0, p1_0); 3305 sumi1_1 = _mm_add_epi32(sumi1_1, p1_1); 3306 sumi2_0 = _mm_add_epi32(sumi2_0, p2_0); 3307 sumi2_1 = _mm_add_epi32(sumi2_1, p2_1); 3308 } 3309 3310 accumf = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(_mm_add_epi32(sumi1_1, sumi2_1), _mm_add_epi32(sumi1_0, sumi2_0)))), accumf); 3311 3312 } 3313 3314 *s = 0.125f * hsum_float_8(accumf); 3315 3316 #else 3317 UNUSED(x); 3318 UNUSED(y); 3319 UNUSED(nb); 3320 ggml_vec_dot_iq2_s_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc); 3321 #endif 3322 } 3323 3324 void ggml_vec_dot_iq3_xxs_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { 3325 assert(n % QK_K == 0); 3326 assert(nrc == 1); 3327 UNUSED(nrc); 3328 UNUSED(bx); 3329 UNUSED(by); 3330 UNUSED(bs); 3331 3332 const block_iq3_xxs * GGML_RESTRICT x = vx; 3333 const block_q8_K * GGML_RESTRICT y = vy; 3334 3335 const int nb = n / QK_K; 3336 3337 #if defined(__AVX2__) 3338 3339 const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs; 3340 3341 uint32_t aux32[2]; 3342 3343 __m256 accumf = _mm256_setzero_ps(); 3344 for (int i = 0; i < nb; ++i) { 3345 const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d; 3346 const uint8_t * GGML_RESTRICT q3 = x[i].qs; 3347 const uint8_t * GGML_RESTRICT gas = x[i].qs + QK_K/4; 3348 const int8_t * GGML_RESTRICT q8 = y[i].qs; 3349 __m256i sumi1 = _mm256_setzero_si256(); 3350 __m256i sumi2 = _mm256_setzero_si256(); 3351 for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) { 3352 const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32; 3353 const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32; 3354 const __m256i q2_1 = _mm256_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]], 3355 iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]); 3356 q3 += 8; 3357 const __m256i q2_2 = _mm256_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]], 3358 iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]); 3359 q3 += 8; 3360 memcpy(aux32, gas, 8); gas += 8; 3361 const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[0] >> 21) & 127], signs64[(aux32[0] >> 14) & 127], 3362 signs64[(aux32[0] >> 7) & 127], signs64[(aux32[0] >> 0) & 127]); 3363 const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127], 3364 signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]); 3365 const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1); 3366 const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2); 3367 const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1); 3368 const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2); 3369 const uint16_t ls1 = aux32[0] >> 28; 3370 const uint16_t ls2 = aux32[1] >> 28; 3371 const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1)); 3372 const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1)); 3373 sumi1 = _mm256_add_epi32(sumi1, p1); 3374 sumi2 = _mm256_add_epi32(sumi2, p2); 3375 } 3376 3377 accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf); 3378 3379 } 3380 3381 *s = 0.25f * hsum_float_8(accumf); 3382 3383 #elif defined(__AVX__) 3384 const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs; 3385 3386 uint32_t aux32[2]; 3387 3388 __m256 accumf = _mm256_setzero_ps(); 3389 for (int i = 0; i < nb; ++i) { 3390 const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d; 3391 const uint8_t * GGML_RESTRICT q3 = x[i].qs; 3392 const uint8_t * GGML_RESTRICT gas = x[i].qs + QK_K/4; 3393 const int8_t * GGML_RESTRICT q8 = y[i].qs; 3394 __m128i sumi1_0 = _mm_setzero_si128(); 3395 __m128i sumi1_1 = _mm_setzero_si128(); 3396 __m128i sumi2_0 = _mm_setzero_si128(); 3397 __m128i sumi2_1 = _mm_setzero_si128(); 3398 for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) { 3399 const __m128i q8_1_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3400 const __m128i q8_1_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3401 const __m128i q8_2_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3402 const __m128i q8_2_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3403 const __m128i q2_1_0 = _mm_set_epi32(iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]); 3404 const __m128i q2_1_1 = _mm_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]]); 3405 q3 += 8; 3406 const __m128i q2_2_0 = _mm_set_epi32(iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]); 3407 const __m128i q2_2_1 = _mm_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]]); 3408 q3 += 8; 3409 memcpy(aux32, gas, 8); gas += 8; 3410 const __m128i s2_1_0 = _mm_set_epi64x(signs64[(aux32[0] >> 7) & 127], signs64[(aux32[0] >> 0) & 127]); 3411 const __m128i s2_1_1 = _mm_set_epi64x(signs64[(aux32[0] >> 21) & 127], signs64[(aux32[0] >> 14) & 127]); 3412 const __m128i s2_2_0 = _mm_set_epi64x(signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]); 3413 const __m128i s2_2_1 = _mm_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127]); 3414 const __m128i q8s_1_0 = _mm_sign_epi8(q8_1_0, s2_1_0); 3415 const __m128i q8s_1_1 = _mm_sign_epi8(q8_1_1, s2_1_1); 3416 const __m128i q8s_2_0 = _mm_sign_epi8(q8_2_0, s2_2_0); 3417 const __m128i q8s_2_1 = _mm_sign_epi8(q8_2_1, s2_2_1); 3418 const __m128i dot1_0 = _mm_maddubs_epi16(q2_1_0, q8s_1_0); 3419 const __m128i dot1_1 = _mm_maddubs_epi16(q2_1_1, q8s_1_1); 3420 const __m128i dot2_0 = _mm_maddubs_epi16(q2_2_0, q8s_2_0); 3421 const __m128i dot2_1 = _mm_maddubs_epi16(q2_2_1, q8s_2_1); 3422 const uint16_t ls1 = aux32[0] >> 28; 3423 const uint16_t ls2 = aux32[1] >> 28; 3424 const __m128i p1_0 = _mm_madd_epi16(dot1_0, _mm_set1_epi16(2*ls1+1)); 3425 const __m128i p1_1 = _mm_madd_epi16(dot1_1, _mm_set1_epi16(2*ls1+1)); 3426 const __m128i p2_0 = _mm_madd_epi16(dot2_0, _mm_set1_epi16(2*ls2+1)); 3427 const __m128i p2_1 = _mm_madd_epi16(dot2_1, _mm_set1_epi16(2*ls2+1)); 3428 sumi1_0 = _mm_add_epi32(sumi1_0, p1_0); 3429 sumi1_1 = _mm_add_epi32(sumi1_1, p1_1); 3430 sumi2_0 = _mm_add_epi32(sumi2_0, p2_0); 3431 sumi2_1 = _mm_add_epi32(sumi2_1, p2_1); 3432 } 3433 3434 accumf = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(_mm_add_epi32(sumi1_1, sumi2_1), _mm_add_epi32(sumi1_0, sumi2_0)))), accumf); 3435 3436 } 3437 3438 *s = 0.25f * hsum_float_8(accumf); 3439 3440 #else 3441 UNUSED(x); 3442 UNUSED(y); 3443 UNUSED(nb); 3444 ggml_vec_dot_iq3_xxs_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc); 3445 #endif 3446 } 3447 3448 void ggml_vec_dot_iq3_s_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { 3449 assert(n % QK_K == 0); 3450 assert(nrc == 1); 3451 UNUSED(nrc); 3452 UNUSED(bx); 3453 UNUSED(by); 3454 UNUSED(bs); 3455 3456 const block_iq3_s * GGML_RESTRICT x = vx; 3457 const block_q8_K * GGML_RESTRICT y = vy; 3458 3459 const int nb = n / QK_K; 3460 3461 #if defined(__AVX2__) 3462 3463 static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 3464 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03 3465 }; 3466 3467 static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 3468 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 3469 }; 3470 3471 const __m256i mask1 = _mm256_loadu_si256((const __m256i*)k_mask1); 3472 const __m256i mask2 = _mm256_loadu_si256((const __m256i*)k_mask2); 3473 3474 const __m256i idx_shift = _mm256_set_epi32(1, 2, 3, 4, 5, 6, 7, 8); 3475 const __m256i idx_mask = _mm256_set1_epi32(256); 3476 3477 typedef union { 3478 __m256i vec[2]; 3479 uint32_t index[16]; 3480 } index_t; 3481 3482 index_t idx; 3483 3484 __m256 accumf = _mm256_setzero_ps(); 3485 for (int i = 0; i < nb; ++i) { 3486 const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d; 3487 const uint8_t * GGML_RESTRICT qs = x[i].qs; 3488 const uint8_t * GGML_RESTRICT qh = x[i].qh; 3489 const uint16_t * GGML_RESTRICT signs = (const uint16_t *)x[i].signs; 3490 const int8_t * GGML_RESTRICT q8 = y[i].qs; 3491 __m256i sumi1 = _mm256_setzero_si256(); 3492 __m256i sumi2 = _mm256_setzero_si256(); 3493 for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) { 3494 const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32; 3495 const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32; 3496 const __m256i idx_l = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)qs)); qs += 16; 3497 idx.vec[0] = _mm256_set1_epi32(qh[ib32+0]); 3498 idx.vec[1] = _mm256_set1_epi32(qh[ib32+1]); 3499 idx.vec[0] = _mm256_and_si256(_mm256_sllv_epi32(idx.vec[0], idx_shift), idx_mask); 3500 idx.vec[1] = _mm256_and_si256(_mm256_sllv_epi32(idx.vec[1], idx_shift), idx_mask); 3501 idx.vec[0] = _mm256_or_si256(idx.vec[0], _mm256_cvtepi16_epi32(_mm256_castsi256_si128(idx_l))); 3502 idx.vec[1] = _mm256_or_si256(idx.vec[1], _mm256_cvtepi16_epi32(_mm256_extractf128_si256(idx_l, 1))); 3503 3504 // At leat on my CPU (Ryzen 7950X), using _mm256_i32gather_epi32 is slower than _mm256_set_epi32. Strange. 3505 //const __m256i q2_1 = _mm256_i32gather_epi32((const int *)iq3s_grid, idx.vec[0], 4); 3506 //const __m256i q2_2 = _mm256_i32gather_epi32((const int *)iq3s_grid, idx.vec[1], 4); 3507 const __m256i q2_1 = _mm256_set_epi32( 3508 iq3s_grid[idx.index[7]], iq3s_grid[idx.index[6]], iq3s_grid[idx.index[5]], iq3s_grid[idx.index[4]], 3509 iq3s_grid[idx.index[3]], iq3s_grid[idx.index[2]], iq3s_grid[idx.index[1]], iq3s_grid[idx.index[0]] 3510 ); 3511 const __m256i q2_2 = _mm256_set_epi32( 3512 iq3s_grid[idx.index[15]], iq3s_grid[idx.index[14]], iq3s_grid[idx.index[13]], iq3s_grid[idx.index[12]], 3513 iq3s_grid[idx.index[11]], iq3s_grid[idx.index[10]], iq3s_grid[idx.index[ 9]], iq3s_grid[idx.index[ 8]] 3514 ); 3515 3516 __m256i aux256 = _mm256_set1_epi32(signs[0] | (signs[1] << 16)); 3517 aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2); 3518 const __m256i s2_1 = _mm256_cmpeq_epi8(aux256, mask2); 3519 const __m256i q8s_1 = _mm256_sub_epi8(_mm256_xor_si256(s2_1, q8_1), s2_1); 3520 3521 aux256 = _mm256_set1_epi32(signs[2] | (signs[3] << 16)); 3522 aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2); 3523 const __m256i s2_2 = _mm256_cmpeq_epi8(aux256, mask2); 3524 const __m256i q8s_2 = _mm256_sub_epi8(_mm256_xor_si256(s2_2, q8_2), s2_2); 3525 3526 signs += 4; 3527 3528 const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1); 3529 const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2); 3530 const uint16_t ls1 = x[i].scales[ib32/2] & 0xf; 3531 const uint16_t ls2 = x[i].scales[ib32/2] >> 4; 3532 const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1)); 3533 const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1)); 3534 sumi1 = _mm256_add_epi32(sumi1, p1); 3535 sumi2 = _mm256_add_epi32(sumi2, p2); 3536 } 3537 3538 accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf); 3539 3540 } 3541 3542 *s = hsum_float_8(accumf); 3543 3544 #elif defined(__AVX__) 3545 static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 3546 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03 3547 }; 3548 3549 static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 3550 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 3551 }; 3552 3553 const __m128i mask1_0 = _mm_loadu_si128((const __m128i*)k_mask1); 3554 const __m128i mask1_1 = _mm_loadu_si128((const __m128i*)k_mask1 + 1); 3555 const __m128i mask2_0 = _mm_loadu_si128((const __m128i*)k_mask2); 3556 const __m128i mask2_1 = _mm_loadu_si128((const __m128i*)k_mask2 + 1); 3557 3558 const __m128i idx_mul_0 = _mm_set_epi32(32, 64, 128, 256); 3559 const __m128i idx_mul_1 = _mm_set_epi32(2, 4, 8, 16); 3560 const __m128i idx_mask = _mm_set1_epi32(256); 3561 3562 typedef union { 3563 __m128i vec[4]; 3564 uint32_t index[16]; 3565 } index_t; 3566 3567 index_t idx; 3568 3569 __m256 accumf = _mm256_setzero_ps(); 3570 for (int i = 0; i < nb; ++i) { 3571 const float d = GGML_CPU_FP16_TO_FP32(x[i].d) * y[i].d; 3572 const uint8_t * GGML_RESTRICT qs = x[i].qs; 3573 const uint8_t * GGML_RESTRICT qh = x[i].qh; 3574 const uint16_t * GGML_RESTRICT signs = (const uint16_t *)x[i].signs; 3575 const int8_t * GGML_RESTRICT q8 = y[i].qs; 3576 __m128i sumi1_0 = _mm_setzero_si128(); 3577 __m128i sumi1_1 = _mm_setzero_si128(); 3578 __m128i sumi2_0 = _mm_setzero_si128(); 3579 __m128i sumi2_1 = _mm_setzero_si128(); 3580 for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) { 3581 const __m128i q8_1_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3582 const __m128i q8_1_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3583 const __m128i q8_2_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3584 const __m128i q8_2_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3585 const __m128i qs_tmp = _mm_loadu_si128((const __m128i *)qs); 3586 const __m128i idx_l_0 = _mm_cvtepu8_epi16(qs_tmp); 3587 const __m128i idx_l_1 = _mm_cvtepu8_epi16(_mm_srli_si128(qs_tmp, 8)); qs += 16; 3588 idx.vec[0] = _mm_set1_epi32(qh[ib32+0]); 3589 idx.vec[1] = idx.vec[0]; 3590 idx.vec[2] = _mm_set1_epi32(qh[ib32+1]); 3591 idx.vec[3] = idx.vec[2]; 3592 3593 idx.vec[0] = _mm_and_si128(_mm_mullo_epi32(idx.vec[0], idx_mul_0), idx_mask); 3594 idx.vec[1] = _mm_and_si128(_mm_mullo_epi32(idx.vec[1], idx_mul_1), idx_mask); 3595 idx.vec[2] = _mm_and_si128(_mm_mullo_epi32(idx.vec[2], idx_mul_0), idx_mask); 3596 idx.vec[3] = _mm_and_si128(_mm_mullo_epi32(idx.vec[3], idx_mul_1), idx_mask); 3597 3598 idx.vec[0] = _mm_or_si128(idx.vec[0], _mm_cvtepi16_epi32(idx_l_0)); 3599 idx.vec[1] = _mm_or_si128(idx.vec[1], _mm_cvtepi16_epi32(_mm_srli_si128(idx_l_0, 8))); 3600 idx.vec[2] = _mm_or_si128(idx.vec[2], _mm_cvtepi16_epi32(idx_l_1)); 3601 idx.vec[3] = _mm_or_si128(idx.vec[3], _mm_cvtepi16_epi32(_mm_srli_si128(idx_l_1, 8))); 3602 3603 const __m128i q2_1_0 = _mm_set_epi32(iq3s_grid[idx.index[3]], iq3s_grid[idx.index[2]], iq3s_grid[idx.index[1]], iq3s_grid[idx.index[0]]); 3604 const __m128i q2_1_1 = _mm_set_epi32(iq3s_grid[idx.index[7]], iq3s_grid[idx.index[6]], iq3s_grid[idx.index[5]], iq3s_grid[idx.index[4]]); 3605 const __m128i q2_2_0 = _mm_set_epi32(iq3s_grid[idx.index[11]], iq3s_grid[idx.index[10]], iq3s_grid[idx.index[9]], iq3s_grid[idx.index[8]]); 3606 const __m128i q2_2_1 = _mm_set_epi32(iq3s_grid[idx.index[15]], iq3s_grid[idx.index[14]], iq3s_grid[idx.index[13]], iq3s_grid[idx.index[12]]); 3607 3608 __m128i aux128_0 = _mm_set1_epi32(signs[0] | (signs[1] << 16)); 3609 __m128i aux128_1 = aux128_0; 3610 aux128_0 = _mm_and_si128(_mm_shuffle_epi8(aux128_0,mask1_0), mask2_0); 3611 aux128_1 = _mm_and_si128(_mm_shuffle_epi8(aux128_1,mask1_1), mask2_1); 3612 const __m128i s2_1_0 = _mm_cmpeq_epi8(aux128_0, mask2_0); 3613 const __m128i s2_1_1 = _mm_cmpeq_epi8(aux128_1, mask2_1); 3614 const __m128i q8s_1_0 = _mm_sub_epi8(_mm_xor_si128(s2_1_0, q8_1_0), s2_1_0); 3615 const __m128i q8s_1_1 = _mm_sub_epi8(_mm_xor_si128(s2_1_1, q8_1_1), s2_1_1); 3616 3617 aux128_0 = _mm_set1_epi32(signs[2] | (signs[3] << 16)); 3618 aux128_1 = aux128_0; 3619 aux128_0 = _mm_and_si128(_mm_shuffle_epi8(aux128_0,mask1_0), mask2_0); 3620 aux128_1 = _mm_and_si128(_mm_shuffle_epi8(aux128_1,mask1_1), mask2_1); 3621 const __m128i s2_2_0 = _mm_cmpeq_epi8(aux128_0, mask2_0); 3622 const __m128i s2_2_1 = _mm_cmpeq_epi8(aux128_1, mask2_1); 3623 const __m128i q8s_2_0 = _mm_sub_epi8(_mm_xor_si128(s2_2_0, q8_2_0), s2_2_0); 3624 const __m128i q8s_2_1 = _mm_sub_epi8(_mm_xor_si128(s2_2_1, q8_2_1), s2_2_1); 3625 3626 signs += 4; 3627 3628 const __m128i dot1_0 = _mm_maddubs_epi16(q2_1_0, q8s_1_0); 3629 const __m128i dot1_1 = _mm_maddubs_epi16(q2_1_1, q8s_1_1); 3630 const __m128i dot2_0 = _mm_maddubs_epi16(q2_2_0, q8s_2_0); 3631 const __m128i dot2_1 = _mm_maddubs_epi16(q2_2_1, q8s_2_1); 3632 const uint16_t ls1 = x[i].scales[ib32/2] & 0xf; 3633 const uint16_t ls2 = x[i].scales[ib32/2] >> 4; 3634 const __m128i p1_0 = _mm_madd_epi16(dot1_0, _mm_set1_epi16(2*ls1+1)); 3635 const __m128i p1_1 = _mm_madd_epi16(dot1_1, _mm_set1_epi16(2*ls1+1)); 3636 const __m128i p2_0 = _mm_madd_epi16(dot2_0, _mm_set1_epi16(2*ls2+1)); 3637 const __m128i p2_1 = _mm_madd_epi16(dot2_1, _mm_set1_epi16(2*ls2+1)); 3638 sumi1_0 = _mm_add_epi32(sumi1_0, p1_0); 3639 sumi1_1 = _mm_add_epi32(sumi1_1, p1_1); 3640 sumi2_0 = _mm_add_epi32(sumi2_0, p2_0); 3641 sumi2_1 = _mm_add_epi32(sumi2_1, p2_1); 3642 } 3643 3644 accumf = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(_mm_add_epi32(sumi1_1, sumi2_1), _mm_add_epi32(sumi1_0, sumi2_0)))), accumf); 3645 3646 } 3647 3648 *s = hsum_float_8(accumf); 3649 3650 #else 3651 UNUSED(x); 3652 UNUSED(y); 3653 UNUSED(nb); 3654 ggml_vec_dot_iq3_s_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc); 3655 #endif 3656 } 3657 3658 void ggml_vec_dot_iq1_s_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { 3659 assert(n % QK_K == 0); 3660 assert(nrc == 1); 3661 UNUSED(nrc); 3662 UNUSED(bx); 3663 UNUSED(by); 3664 UNUSED(bs); 3665 3666 const block_iq1_s * GGML_RESTRICT x = vx; 3667 const block_q8_K * GGML_RESTRICT y = vy; 3668 3669 const int nb = n / QK_K; 3670 3671 #if defined __AVX2__ 3672 3673 __m256 accum = _mm256_setzero_ps(); 3674 float accum1 = 0; 3675 for (int i = 0; i < nb; ++i) { 3676 3677 const int8_t * q8 = y[i].qs; 3678 const uint8_t * qs = x[i].qs; 3679 const uint16_t * qh = x[i].qh; 3680 3681 __m256i sumi = _mm256_setzero_si256(); 3682 int sumi1 = 0; 3683 for (int ib = 0; ib < QK_K/32; ib += 2) { 3684 #ifdef __BMI2__ 3685 const uint64_t packed_idx1 = _pdep_u64(*(const uint32_t *)qs, 0x00ff00ff00ff00ffULL) | _pdep_u64(qh[ib], 0x700070007000700ULL); 3686 const uint64_t packed_idx2 = _pdep_u64(*(const uint32_t *)(qs + 4), 0x00ff00ff00ff00ffULL) | _pdep_u64(qh[ib + 1], 0x700070007000700ULL); 3687 const uint16_t *idx1 = (const uint16_t *)(&packed_idx1); 3688 const uint16_t *idx2 = (const uint16_t *)(&packed_idx2); 3689 const __m256i q1b_1 = _mm256_set_epi64x(iq1s_grid[idx1[3]], iq1s_grid[idx1[2]], iq1s_grid[idx1[1]], iq1s_grid[idx1[0]]); 3690 const __m256i q1b_2 = _mm256_set_epi64x(iq1s_grid[idx2[3]], iq1s_grid[idx2[2]], iq1s_grid[idx2[1]], iq1s_grid[idx2[0]]); 3691 #else 3692 const __m256i q1b_1 = _mm256_set_epi64x(iq1s_grid[qs[3] | ((qh[ib+0] >> 1) & 0x700)], iq1s_grid[qs[2] | ((qh[ib+0] << 2) & 0x700)], 3693 iq1s_grid[qs[1] | ((qh[ib+0] << 5) & 0x700)], iq1s_grid[qs[0] | ((qh[ib+0] << 8) & 0x700)]); 3694 const __m256i q1b_2 = _mm256_set_epi64x(iq1s_grid[qs[7] | ((qh[ib+1] >> 1) & 0x700)], iq1s_grid[qs[6] | ((qh[ib+1] << 2) & 0x700)], 3695 iq1s_grid[qs[5] | ((qh[ib+1] << 5) & 0x700)], iq1s_grid[qs[4] | ((qh[ib+1] << 8) & 0x700)]); 3696 #endif 3697 qs += 8; 3698 const __m256i q8b_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32; 3699 const __m256i q8b_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32; 3700 3701 const __m256i dot1 = mul_add_epi8(q1b_1, q8b_1); 3702 const __m256i dot2 = mul_add_epi8(q1b_2, q8b_2); 3703 const int16_t ls1 = 2*((qh[ib+0] >> 12) & 7) + 1; 3704 const int16_t ls2 = 2*((qh[ib+1] >> 12) & 7) + 1; 3705 const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(ls1)); 3706 const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(ls2)); 3707 3708 sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p1, p2)); 3709 sumi1 += (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]) * (qh[ib+0] & 0x8000 ? -1 : 1) * ls1 3710 + (y[i].bsums[2*ib+2] + y[i].bsums[2*ib+3]) * (qh[ib+1] & 0x8000 ? -1 : 1) * ls2; 3711 } 3712 3713 const float d = y[i].d * GGML_CPU_FP16_TO_FP32(x[i].d); 3714 accum = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(sumi), accum); 3715 accum1 += d * sumi1; 3716 3717 } 3718 3719 *s = hsum_float_8(accum) + IQ1S_DELTA * accum1; 3720 3721 #elif defined __AVX__ 3722 __m256 accum = _mm256_setzero_ps(); 3723 float accum1 = 0; 3724 for (int i = 0; i < nb; ++i) { 3725 3726 const int8_t * q8 = y[i].qs; 3727 const uint8_t * qs = x[i].qs; 3728 const uint16_t * qh = x[i].qh; 3729 3730 __m128i sumi1_0 = _mm_setzero_si128(); 3731 __m128i sumi1_1 = _mm_setzero_si128(); 3732 int sumi1 = 0; 3733 for (int ib = 0; ib < QK_K/32; ib += 2) { 3734 const __m128i q1b_1_0 = _mm_set_epi64x(iq1s_grid[qs[1] | ((qh[ib+0] << 5) & 0x700)], iq1s_grid[qs[0] | ((qh[ib+0] << 8) & 0x700)]); 3735 const __m128i q1b_1_1 = _mm_set_epi64x(iq1s_grid[qs[3] | ((qh[ib+0] >> 1) & 0x700)], iq1s_grid[qs[2] | ((qh[ib+0] << 2) & 0x700)]); 3736 const __m128i q1b_2_0 = _mm_set_epi64x(iq1s_grid[qs[5] | ((qh[ib+1] << 5) & 0x700)], iq1s_grid[qs[4] | ((qh[ib+1] << 8) & 0x700)]); 3737 const __m128i q1b_2_1 = _mm_set_epi64x(iq1s_grid[qs[7] | ((qh[ib+1] >> 1) & 0x700)], iq1s_grid[qs[6] | ((qh[ib+1] << 2) & 0x700)]); 3738 qs += 8; 3739 const __m128i q8b_1_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3740 const __m128i q8b_1_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3741 const __m128i q8b_2_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3742 const __m128i q8b_2_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3743 3744 const __m128i dot1_0 = mul_add_epi8_sse(q1b_1_0, q8b_1_0); 3745 const __m128i dot1_1 = mul_add_epi8_sse(q1b_1_1, q8b_1_1); 3746 const __m128i dot2_0 = mul_add_epi8_sse(q1b_2_0, q8b_2_0); 3747 const __m128i dot2_1 = mul_add_epi8_sse(q1b_2_1, q8b_2_1); 3748 const int16_t ls1 = 2*((qh[ib+0] >> 12) & 7) + 1; 3749 const int16_t ls2 = 2*((qh[ib+1] >> 12) & 7) + 1; 3750 const __m128i p1_0 = _mm_madd_epi16(dot1_0, _mm_set1_epi16(ls1)); 3751 const __m128i p1_1 = _mm_madd_epi16(dot1_1, _mm_set1_epi16(ls1)); 3752 const __m128i p2_0 = _mm_madd_epi16(dot2_0, _mm_set1_epi16(ls2)); 3753 const __m128i p2_1 = _mm_madd_epi16(dot2_1, _mm_set1_epi16(ls2)); 3754 3755 sumi1_0 = _mm_add_epi32(sumi1_0, _mm_add_epi32(p1_0, p2_0)); 3756 sumi1_1 = _mm_add_epi32(sumi1_1, _mm_add_epi32(p1_1, p2_1)); 3757 sumi1 += (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]) * (qh[ib+0] & 0x8000 ? -1 : 1) * ls1 3758 + (y[i].bsums[2*ib+2] + y[i].bsums[2*ib+3]) * (qh[ib+1] & 0x8000 ? -1 : 1) * ls2; 3759 } 3760 3761 const float d = y[i].d * GGML_CPU_FP16_TO_FP32(x[i].d); 3762 accum = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(sumi1_1, sumi1_0))), accum); 3763 accum1 += d * sumi1; 3764 3765 } 3766 3767 *s = hsum_float_8(accum) + IQ1S_DELTA * accum1; 3768 3769 #else 3770 UNUSED(x); 3771 UNUSED(y); 3772 UNUSED(nb); 3773 ggml_vec_dot_iq1_s_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc); 3774 #endif 3775 } 3776 3777 void ggml_vec_dot_iq1_m_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { 3778 assert(n % QK_K == 0); 3779 assert(nrc == 1); 3780 UNUSED(nrc); 3781 UNUSED(bx); 3782 UNUSED(by); 3783 UNUSED(bs); 3784 3785 const block_iq1_m * GGML_RESTRICT x = vx; 3786 const block_q8_K * GGML_RESTRICT y = vy; 3787 3788 const int nb = n / QK_K; 3789 3790 iq1m_scale_t scale; 3791 3792 #if defined __AVX2__ 3793 3794 const __m256i mask = _mm256_set1_epi16(0x7); 3795 const __m256i mone = _mm256_set1_epi16(1); 3796 const __m256i mone8 = _mm256_set1_epi8(1); 3797 const __m256i mtwo8 = _mm256_set1_epi8(2); 3798 // VPSHUFB cannot cross 128-bit lanes so odd shifts go to upper half. 3799 const __m256i scales_shift = _mm256_set_epi64x(9, 3, 6, 0); 3800 3801 __m256 accum1 = _mm256_setzero_ps(); 3802 __m256 accum2 = _mm256_setzero_ps(); 3803 for (int i = 0; i < nb; ++i) { 3804 3805 const int8_t * q8 = y[i].qs; 3806 const uint8_t * qs = x[i].qs; 3807 const uint8_t * qh = x[i].qh; 3808 const uint16_t * sc = (const uint16_t *)x[i].scales; 3809 3810 scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000); 3811 // Extract 3-bit scales (16 values) 3812 __m256i scales = _mm256_set1_epi64x(*(const uint64_t*)sc); 3813 scales = _mm256_srlv_epi64(scales, scales_shift); 3814 scales = _mm256_add_epi16(_mm256_slli_epi16(_mm256_and_si256(scales, mask), 1), mone); 3815 3816 // Indices to repeat each scale 8 times. 3817 __m256i scales_idx1 = _mm256_set1_epi16(0x0100); 3818 __m256i scales_idx2 = _mm256_add_epi8(scales_idx1, _mm256_set1_epi8(8)); 3819 3820 __m256i sumi1 = _mm256_setzero_si256(); 3821 __m256i sumi2 = _mm256_setzero_si256(); 3822 for (int ib = 0; ib < QK_K/32; ib += 2) { 3823 #ifdef __BMI2__ 3824 const uint64_t packed_idx1 = _pdep_u64(*(const uint32_t *)qs, 0x00ff00ff00ff00ffULL) 3825 | _pdep_u64(*(const uint16_t*)(qh) & 0x7777, 0xf000f000f000f00ULL); 3826 const uint64_t packed_idx2 = _pdep_u64(*(const uint32_t *)(qs + 4), 0x00ff00ff00ff00ffULL) 3827 | _pdep_u64(*(const uint16_t*)(qh + 2) & 0x7777, 0xf000f000f000f00ULL); 3828 const uint16_t *idx1 = (const uint16_t *)(&packed_idx1); 3829 const uint16_t *idx2 = (const uint16_t *)(&packed_idx2); 3830 const __m256i q1b_1 = _mm256_set_epi64x(iq1s_grid[idx1[3]], iq1s_grid[idx1[2]], iq1s_grid[idx1[1]], iq1s_grid[idx1[0]]); 3831 const __m256i q1b_2 = _mm256_set_epi64x(iq1s_grid[idx2[3]], iq1s_grid[idx2[2]], iq1s_grid[idx2[1]], iq1s_grid[idx2[0]]); 3832 3833 // Convert signs to bytes 0x81 (negative) or 0x01 (positive) 3834 const uint64_t delta_sign = _pdep_u64(*(const uint32_t*)(qh) & 0x88888888, 0xf0f0f0f0f0f0f0f0ULL); 3835 const __m256i delta1 = _mm256_or_si256(mone8, _mm256_cvtepi8_epi64(_mm_set1_epi32(delta_sign))); 3836 const __m256i delta2 = _mm256_or_si256(mone8, _mm256_cvtepi8_epi64(_mm_set1_epi32(delta_sign >> 32))); 3837 #else 3838 const __m256i q1b_1 = _mm256_set_epi64x( 3839 iq1s_grid[qs[3] | (((uint16_t)qh[1] << 4) & 0x700)], iq1s_grid[qs[2] | (((uint16_t)qh[1] << 8) & 0x700)], 3840 iq1s_grid[qs[1] | (((uint16_t)qh[0] << 4) & 0x700)], iq1s_grid[qs[0] | (((uint16_t)qh[0] << 8) & 0x700)] 3841 ); 3842 const __m256i q1b_2 = _mm256_set_epi64x( 3843 iq1s_grid[qs[7] | (((uint16_t)qh[3] << 4) & 0x700)], iq1s_grid[qs[6] | (((uint16_t)qh[3] << 8) & 0x700)], 3844 iq1s_grid[qs[5] | (((uint16_t)qh[2] << 4) & 0x700)], iq1s_grid[qs[4] | (((uint16_t)qh[2] << 8) & 0x700)] 3845 ); 3846 3847 const __m256i delta1 = _mm256_set_epi64x(qh[1] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101, 3848 qh[1] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101, 3849 qh[0] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101, 3850 qh[0] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101); 3851 const __m256i delta2 = _mm256_set_epi64x(qh[3] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101, 3852 qh[3] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101, 3853 qh[2] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101, 3854 qh[2] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101); 3855 #endif 3856 const __m256i q8b_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32; 3857 const __m256i q8b_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32; 3858 3859 const __m256i dot1 = mul_add_epi8(q1b_1, q8b_1); 3860 const __m256i dot2 = mul_add_epi8(q1b_2, q8b_2); 3861 const __m256i dot3 = _mm256_maddubs_epi16(mone8, _mm256_sign_epi8(q8b_1, delta1)); 3862 const __m256i dot4 = _mm256_maddubs_epi16(mone8, _mm256_sign_epi8(q8b_2, delta2)); 3863 3864 __m256i scale1 = _mm256_shuffle_epi8(scales, scales_idx1); 3865 __m256i scale2 = _mm256_shuffle_epi8(scales, scales_idx2); 3866 3867 scales_idx1 = _mm256_add_epi8(scales_idx1, mtwo8); 3868 scales_idx2 = _mm256_add_epi8(scales_idx2, mtwo8); 3869 3870 const __m256i p1 = _mm256_madd_epi16(dot1, scale1); 3871 const __m256i p2 = _mm256_madd_epi16(dot2, scale2); 3872 const __m256i p3 = _mm256_madd_epi16(dot3, scale1); 3873 const __m256i p4 = _mm256_madd_epi16(dot4, scale2); 3874 3875 sumi1 = _mm256_add_epi32(sumi1, _mm256_add_epi32(p1, p2)); 3876 sumi2 = _mm256_add_epi32(sumi2, _mm256_add_epi32(p3, p4)); 3877 3878 qs += 8; qh += 4; 3879 } 3880 3881 const __m256 d = _mm256_set1_ps(y[i].d * GGML_CPU_FP16_TO_FP32(scale.f16)); 3882 3883 accum1 = _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(sumi1), accum1); 3884 accum2 = _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(sumi2), accum2); 3885 } 3886 3887 *s = hsum_float_8(accum1) + IQ1M_DELTA * hsum_float_8(accum2); 3888 3889 #elif defined __AVX__ 3890 const __m128i mask = _mm_set1_epi16(0x7); 3891 const __m128i mone = _mm_set1_epi16(1); 3892 3893 __m256 accum1 = _mm256_setzero_ps(); 3894 __m256 accum2 = _mm256_setzero_ps(); 3895 for (int i = 0; i < nb; ++i) { 3896 3897 const int8_t * q8 = y[i].qs; 3898 const uint8_t * qs = x[i].qs; 3899 const uint8_t * qh = x[i].qh; 3900 const uint16_t * sc = (const uint16_t *)x[i].scales; 3901 3902 scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000); 3903 3904 __m128i sumi1_0 = _mm_setzero_si128(); 3905 __m128i sumi1_1 = _mm_setzero_si128(); 3906 __m128i sumi2_0 = _mm_setzero_si128(); 3907 __m128i sumi2_1 = _mm_setzero_si128(); 3908 for (int ib = 0; ib < QK_K/32; ib += 2) { 3909 const __m128i q1b_1_0 = _mm_set_epi64x( 3910 iq1s_grid[qs[1] | (((uint16_t)qh[0] << 4) & 0x700)], iq1s_grid[qs[0] | (((uint16_t)qh[0] << 8) & 0x700)]); 3911 const __m128i q1b_1_1 = _mm_set_epi64x( 3912 iq1s_grid[qs[3] | (((uint16_t)qh[1] << 4) & 0x700)], iq1s_grid[qs[2] | (((uint16_t)qh[1] << 8) & 0x700)]); 3913 const __m128i q1b_2_0 = _mm_set_epi64x( 3914 iq1s_grid[qs[5] | (((uint16_t)qh[2] << 4) & 0x700)], iq1s_grid[qs[4] | (((uint16_t)qh[2] << 8) & 0x700)]); 3915 const __m128i q1b_2_1 = _mm_set_epi64x( 3916 iq1s_grid[qs[7] | (((uint16_t)qh[3] << 4) & 0x700)], iq1s_grid[qs[6] | (((uint16_t)qh[3] << 8) & 0x700)]); 3917 const __m128i q8b_1_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3918 const __m128i q8b_1_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3919 const __m128i q8b_2_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3920 const __m128i q8b_2_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 3921 3922 const __m128i dot1_0 = mul_add_epi8_sse(q1b_1_0, q8b_1_0); 3923 const __m128i dot1_1 = mul_add_epi8_sse(q1b_1_1, q8b_1_1); 3924 const __m128i dot2_0 = mul_add_epi8_sse(q1b_2_0, q8b_2_0); 3925 const __m128i dot2_1 = mul_add_epi8_sse(q1b_2_1, q8b_2_1); 3926 3927 const __m128i delta1_0 = _mm_set_epi64x(qh[0] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101, 3928 qh[0] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101); 3929 const __m128i delta1_1 = _mm_set_epi64x(qh[1] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101, 3930 qh[1] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101); 3931 const __m128i delta2_0 = _mm_set_epi64x(qh[2] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101, 3932 qh[2] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101); 3933 const __m128i delta2_1 = _mm_set_epi64x(qh[3] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101, 3934 qh[3] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101); 3935 3936 const __m128i dot3_0 = mul_add_epi8_sse(delta1_0, q8b_1_0); 3937 const __m128i dot3_1 = mul_add_epi8_sse(delta1_1, q8b_1_1); 3938 const __m128i dot4_0 = mul_add_epi8_sse(delta2_0, q8b_2_0); 3939 const __m128i dot4_1 = mul_add_epi8_sse(delta2_1, q8b_2_1); 3940 3941 __m128i scale1_0 = _mm_set1_epi16(sc[ib/2] >> 0); 3942 __m128i scale1_1 = _mm_set1_epi16(sc[ib/2] >> 3); 3943 __m128i scale2_0 = _mm_set1_epi16(sc[ib/2] >> 6); 3944 __m128i scale2_1 = _mm_set1_epi16(sc[ib/2] >> 9); 3945 3946 scale1_0 = _mm_add_epi16(_mm_slli_epi16(_mm_and_si128(scale1_0, mask), 1), mone); 3947 scale1_1 = _mm_add_epi16(_mm_slli_epi16(_mm_and_si128(scale1_1, mask), 1), mone); 3948 scale2_0 = _mm_add_epi16(_mm_slli_epi16(_mm_and_si128(scale2_0, mask), 1), mone); 3949 scale2_1 = _mm_add_epi16(_mm_slli_epi16(_mm_and_si128(scale2_1, mask), 1), mone); 3950 const __m128i p1_0 = _mm_madd_epi16(dot1_0, scale1_0); 3951 const __m128i p1_1 = _mm_madd_epi16(dot1_1, scale1_1); 3952 const __m128i p2_0 = _mm_madd_epi16(dot2_0, scale2_0); 3953 const __m128i p2_1 = _mm_madd_epi16(dot2_1, scale2_1); 3954 const __m128i p3_0 = _mm_madd_epi16(dot3_0, scale1_0); 3955 const __m128i p3_1 = _mm_madd_epi16(dot3_1, scale1_1); 3956 const __m128i p4_0 = _mm_madd_epi16(dot4_0, scale2_0); 3957 const __m128i p4_1 = _mm_madd_epi16(dot4_1, scale2_1); 3958 3959 sumi1_0 = _mm_add_epi32(sumi1_0, _mm_add_epi32(p1_0, p2_0)); 3960 sumi1_1 = _mm_add_epi32(sumi1_1, _mm_add_epi32(p1_1, p2_1)); 3961 sumi2_0 = _mm_add_epi32(sumi2_0, _mm_add_epi32(p3_0, p4_0)); 3962 sumi2_1 = _mm_add_epi32(sumi2_1, _mm_add_epi32(p3_1, p4_1)); 3963 3964 qs += 8; qh += 4; 3965 } 3966 3967 const __m256 d = _mm256_set1_ps(y[i].d * GGML_CPU_FP16_TO_FP32(scale.f16)); 3968 3969 accum1 = _mm256_add_ps(_mm256_mul_ps(d, _mm256_cvtepi32_ps(MM256_SET_M128I(sumi1_1, sumi1_0))), accum1); 3970 accum2 = _mm256_add_ps(_mm256_mul_ps(d, _mm256_cvtepi32_ps(MM256_SET_M128I(sumi2_1, sumi2_0))), accum2); 3971 } 3972 3973 *s = hsum_float_8(accum1) + IQ1M_DELTA * hsum_float_8(accum2); 3974 3975 #else 3976 UNUSED(x); 3977 UNUSED(y); 3978 UNUSED(nb); 3979 UNUSED(scale); 3980 ggml_vec_dot_iq1_m_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc); 3981 #endif 3982 } 3983 3984 void ggml_vec_dot_iq4_nl_q8_0(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { 3985 assert(nrc == 1); 3986 UNUSED(nrc); 3987 UNUSED(bx); 3988 UNUSED(by); 3989 UNUSED(bs); 3990 assert(n % QK4_NL == 0); 3991 static_assert(QK4_NL == QK8_0, "QK4_NL and QK8_0 must be the same"); 3992 3993 const block_iq4_nl * GGML_RESTRICT x = vx; 3994 const block_q8_0 * GGML_RESTRICT y = vy; 3995 3996 const int nb = n / QK4_NL; 3997 3998 int ib = 0; 3999 float sumf = 0; 4000 4001 #if defined __AVX2__ 4002 4003 const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_iq4nl); 4004 const __m128i m4b = _mm_set1_epi8(0x0f); 4005 const __m256i mone = _mm256_set1_epi16(1); 4006 4007 __m256 accum1 = _mm256_setzero_ps(); 4008 __m256 accum2 = _mm256_setzero_ps(); 4009 for (; ib + 1 < nb; ib += 2) { 4010 const __m128i q4bits_1 = _mm_loadu_si128((const __m128i*)x[ib + 0].qs); 4011 const __m128i q4bits_2 = _mm_loadu_si128((const __m128i*)x[ib + 1].qs); 4012 const __m256i q8b_1 = _mm256_loadu_si256((const __m256i *)y[ib + 0].qs); 4013 const __m256i q8b_2 = _mm256_loadu_si256((const __m256i *)y[ib + 1].qs); 4014 const __m256i q4b_1 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)), 4015 _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b))); 4016 const __m256i q4b_2 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)), 4017 _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b))); 4018 const __m256i p16_1 = mul_add_epi8(q4b_1, q8b_1); 4019 const __m256i p16_2 = mul_add_epi8(q4b_2, q8b_2); 4020 const __m256i p_1 = _mm256_madd_epi16(p16_1, mone); 4021 const __m256i p_2 = _mm256_madd_epi16(p16_2, mone); 4022 accum1 = _mm256_fmadd_ps(_mm256_set1_ps(GGML_CPU_FP16_TO_FP32(y[ib + 0].d)*GGML_CPU_FP16_TO_FP32(x[ib + 0].d)), 4023 _mm256_cvtepi32_ps(p_1), accum1); 4024 accum2 = _mm256_fmadd_ps(_mm256_set1_ps(GGML_CPU_FP16_TO_FP32(y[ib + 1].d)*GGML_CPU_FP16_TO_FP32(x[ib + 1].d)), 4025 _mm256_cvtepi32_ps(p_2), accum2); 4026 } 4027 4028 sumf = hsum_float_8(_mm256_add_ps(accum1, accum2)); 4029 4030 #elif defined __AVX__ 4031 const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_iq4nl); 4032 const __m128i m4b = _mm_set1_epi8(0x0f); 4033 4034 __m256 accum = _mm256_setzero_ps(); 4035 for (; ib + 1 < nb; ib += 2) { 4036 const __m128i q4bits_1 = _mm_loadu_si128((const __m128i *)x[ib + 0].qs); 4037 const __m128i q4bits_2 = _mm_loadu_si128((const __m128i *)x[ib + 1].qs); 4038 const __m128i q8b_1_0 = _mm_loadu_si128((const __m128i *)y[ib + 0].qs); 4039 const __m128i q8b_1_1 = _mm_loadu_si128((const __m128i *)y[ib + 0].qs + 1); 4040 const __m128i q8b_2_0 = _mm_loadu_si128((const __m128i *)y[ib + 1].qs); 4041 const __m128i q8b_2_1 = _mm_loadu_si128((const __m128i *)y[ib + 1].qs + 1); 4042 4043 const __m128i q4b_1_0 = _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b)); 4044 const __m128i q4b_1_1 = _mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)); 4045 const __m128i q4b_2_0 = _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b)); 4046 const __m128i q4b_2_1 = _mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)); 4047 4048 const __m256 p = mul_sum_i8_quad_float(q4b_1_0, q4b_1_1, q4b_2_0, q4b_2_1, q8b_1_0, q8b_1_1, q8b_2_0, q8b_2_1); 4049 const __m256 deltas = quad_fp16_delta_float(x[ib].d, y[ib].d, x[ib + 1].d, y[ib + 1].d); 4050 accum = _mm256_add_ps(_mm256_mul_ps(deltas, p), accum); 4051 } 4052 4053 sumf = hsum_float_8(accum); 4054 4055 #endif 4056 for (; ib < nb; ++ib) { 4057 const float d = GGML_CPU_FP16_TO_FP32(y[ib].d)*GGML_CPU_FP16_TO_FP32(x[ib].d); 4058 int sumi1 = 0, sumi2 = 0; 4059 for (int j = 0; j < QK4_NL/2; ++j) { 4060 sumi1 += y[ib].qs[j+ 0] * kvalues_iq4nl[x[ib].qs[j] & 0xf]; 4061 sumi2 += y[ib].qs[j+QK4_NL/2] * kvalues_iq4nl[x[ib].qs[j] >> 4]; 4062 } 4063 sumf += d * (sumi1 + sumi2); 4064 } 4065 *s = sumf; 4066 } 4067 4068 void ggml_vec_dot_iq4_xs_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc) { 4069 assert(nrc == 1); 4070 UNUSED(nrc); 4071 UNUSED(bx); 4072 UNUSED(by); 4073 UNUSED(bs); 4074 assert(n % QK_K == 0); 4075 4076 const block_iq4_xs * GGML_RESTRICT x = vx; 4077 const block_q8_K * GGML_RESTRICT y = vy; 4078 4079 const int nb = n / QK_K; 4080 4081 #if defined __AVX2__ 4082 4083 const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_iq4nl); 4084 const __m128i m4b = _mm_set1_epi8(0x0f); 4085 4086 __m256 accum = _mm256_setzero_ps(); 4087 for (int ibl = 0; ibl < nb; ++ibl) { 4088 const uint8_t * qs = x[ibl].qs; 4089 const int8_t * q8 = y[ibl].qs; 4090 uint16_t sh = x[ibl].scales_h; 4091 __m256i sumi1 = _mm256_setzero_si256(); 4092 __m256i sumi2 = _mm256_setzero_si256(); 4093 for (int ib = 0; ib < QK_K/32; ib += 2) { 4094 const __m128i q4bits_1 = _mm_loadu_si128((const __m128i*)qs); qs += 16; 4095 const __m128i q4bits_2 = _mm_loadu_si128((const __m128i*)qs); qs += 16; 4096 const __m256i q8b_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32; 4097 const __m256i q8b_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32; 4098 const __m256i q4b_1 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)), 4099 _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b))); 4100 const __m256i q4b_2 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)), 4101 _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b))); 4102 const __m256i p16_1 = mul_add_epi8(q4b_1, q8b_1); 4103 const __m256i p16_2 = mul_add_epi8(q4b_2, q8b_2); 4104 const int16_t ls1 = ((x[ibl].scales_l[ib/2] & 0xf) | ((sh << 4) & 0x30)) - 32; 4105 const int16_t ls2 = ((x[ibl].scales_l[ib/2] >> 4) | ((sh << 2) & 0x30)) - 32; 4106 sh >>= 4; 4107 const __m256i p_1 = _mm256_madd_epi16(p16_1, _mm256_set1_epi16(ls1)); 4108 const __m256i p_2 = _mm256_madd_epi16(p16_2, _mm256_set1_epi16(ls2)); 4109 sumi1 = _mm256_add_epi32(p_1, sumi1); 4110 sumi2 = _mm256_add_epi32(p_2, sumi2); 4111 } 4112 accum = _mm256_fmadd_ps(_mm256_set1_ps(GGML_CPU_FP16_TO_FP32(x[ibl].d)*y[ibl].d), 4113 _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accum); 4114 } 4115 4116 *s = hsum_float_8(accum); 4117 4118 #elif defined __AVX__ 4119 const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_iq4nl); 4120 const __m128i m4b = _mm_set1_epi8(0x0f); 4121 4122 __m256 accum = _mm256_setzero_ps(); 4123 for (int ibl = 0; ibl < nb; ++ibl) { 4124 const uint8_t * qs = x[ibl].qs; 4125 const int8_t * q8 = y[ibl].qs; 4126 uint16_t sh = x[ibl].scales_h; 4127 __m128i sumi1_0 = _mm_setzero_si128(); 4128 __m128i sumi1_1 = _mm_setzero_si128(); 4129 __m128i sumi2_0 = _mm_setzero_si128(); 4130 __m128i sumi2_1 = _mm_setzero_si128(); 4131 for (int ib = 0; ib < QK_K/32; ib += 2) { 4132 const __m128i q4bits_1 = _mm_loadu_si128((const __m128i *)qs); qs += 16; 4133 const __m128i q4bits_2 = _mm_loadu_si128((const __m128i *)qs); qs += 16; 4134 const __m128i q8b_1_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 4135 const __m128i q8b_1_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 4136 const __m128i q8b_2_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 4137 const __m128i q8b_2_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16; 4138 const __m128i q4b_1_0 = _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b)); 4139 const __m128i q4b_1_1 = _mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)); 4140 const __m128i q4b_2_0 = _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b)); 4141 const __m128i q4b_2_1 = _mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)); 4142 const __m128i p16_1_0 = mul_add_epi8_sse(q4b_1_0, q8b_1_0); 4143 const __m128i p16_1_1 = mul_add_epi8_sse(q4b_1_1, q8b_1_1); 4144 const __m128i p16_2_0 = mul_add_epi8_sse(q4b_2_0, q8b_2_0); 4145 const __m128i p16_2_1 = mul_add_epi8_sse(q4b_2_1, q8b_2_1); 4146 const int16_t ls1 = ((x[ibl].scales_l[ib/2] & 0xf) | ((sh << 4) & 0x30)) - 32; 4147 const int16_t ls2 = ((x[ibl].scales_l[ib/2] >> 4) | ((sh << 2) & 0x30)) - 32; 4148 sh >>= 4; 4149 const __m128i p_1_0 = _mm_madd_epi16(p16_1_0, _mm_set1_epi16(ls1)); 4150 const __m128i p_1_1 = _mm_madd_epi16(p16_1_1, _mm_set1_epi16(ls1)); 4151 const __m128i p_2_0 = _mm_madd_epi16(p16_2_0, _mm_set1_epi16(ls2)); 4152 const __m128i p_2_1 = _mm_madd_epi16(p16_2_1, _mm_set1_epi16(ls2)); 4153 sumi1_0 = _mm_add_epi32(p_1_0, sumi1_0); 4154 sumi1_1 = _mm_add_epi32(p_1_1, sumi1_1); 4155 sumi2_0 = _mm_add_epi32(p_2_0, sumi2_0); 4156 sumi2_1 = _mm_add_epi32(p_2_1, sumi2_1); 4157 } 4158 __m128i sumi12_0 = _mm_add_epi32(sumi1_0, sumi2_0); 4159 __m128i sumi12_1 = _mm_add_epi32(sumi1_1, sumi2_1); 4160 accum = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(GGML_CPU_FP16_TO_FP32(x[ibl].d)*y[ibl].d), 4161 _mm256_cvtepi32_ps(MM256_SET_M128I(sumi12_1, sumi12_0))), accum); 4162 } 4163 4164 *s = hsum_float_8(accum); 4165 4166 #else 4167 UNUSED(x); 4168 UNUSED(y); 4169 UNUSED(nb); 4170 ggml_vec_dot_iq4_xs_q8_K_generic(n, s, bs, vx, bx, vy, by, nrc); 4171 #endif 4172 }