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/MojoLlama/blob/main/src/mojollama/kernels/legacy-c/turbo_kernels_avx2.c # MojoLlama Archived Mojo/MAX quantizer and serving research repository (read-only history) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `MojoLlama / src / mojollama / kernels / legacy-c / turbo_kernels_avx2.c` 449 lines · 21723 bytes Raw Blame History 1 /* turbo_kernels_avx2.c — AVX2+FMA vectorized quantized matmul kernels 2 * 3 * Key optimizations over quant_kernels_omp.c: 4 * 1. Q4_K: AVX2 dot product (8 values at once) instead of scalar 5 * 2. Q5_K: AVX2 dot product 6 * 3. Q6_K: AVX2 dot product — THE biggest win (output projection) 7 * 4. Q4_0/Q4_1: Already AVX2 (reused from quant_kernels_omp.c) 8 * 5. Batched QKV/GateUp: Single OMP parallel region for multiple projections 9 * 10 * Compile: gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC \ 11 * -o turbo_kernels_avx2.so turbo_kernels_avx2.c -lm 12 */ 13 14 #include 15 #include 16 #include 17 #include 18 #include 19 20 #define QK_K 256 21 #define QK4_0 32 22 #define Q4_0_BS 18 23 #define Q4_1_BS 20 24 #define Q8_0_BS 34 25 #define Q4_K_BS 144 26 #define Q5_K_BS 176 27 #define Q6_K_BS 210 28 29 static inline float f16_to_f32(uint16_t h) { return _cvtsh_ss(h); } 30 31 /* Scale helpers */ 32 static inline void get_scale_min_k4(int j, const uint8_t *q, 33 uint8_t *d, uint8_t *m) { 34 if (j < 4) { 35 *d = q[j] & 63; 36 *m = q[j + 4] & 63; 37 } else { 38 *d = (q[j + 4] & 0xF) | ((q[j - 4] >> 6) << 4); 39 *m = (q[j + 4] >> 4) | ((q[j] >> 6) << 4); 40 } 41 } 42 43 /* ═════════════════════════════════════════════════════════════════════════ 44 * Q4_0 matmul — AVX2 (from quant_kernels_omp.c, proven correct) 45 * ═══════════════════════════════════════════════════════════════════════ */ 46 47 static inline void decode_q4_0(const uint8_t* bp, __m256 v[4]) { 48 float scale = f16_to_f32((uint16_t)bp[0] | ((uint16_t)bp[1] << 8)); 49 __m256 s = _mm256_set1_ps(scale); 50 __m128i nb = _mm_loadu_si128((__m128i*)(bp + 2)); 51 __m128i lo = _mm_and_si128(nb, _mm_set1_epi8(15)); 52 __m128i hi = _mm_and_si128(_mm_srli_epi16(_mm_and_si128(nb, _mm_set1_epi8((char)0xF0)), 4), _mm_set1_epi8(15)); 53 __m128i lo_s = _mm_sub_epi8(lo, _mm_set1_epi8(8)); 54 __m128i hi_s = _mm_sub_epi8(hi, _mm_set1_epi8(8)); 55 __m128i e00 = _mm_cvtepi8_epi16(lo_s), e01 = _mm_cvtepi8_epi16(_mm_shuffle_epi32(lo_s, 0x4e)); 56 __m128i e10 = _mm_cvtepi8_epi16(hi_s), e11 = _mm_cvtepi8_epi16(_mm_shuffle_epi32(hi_s, 0x4e)); 57 v[0] = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i( 58 _mm_cvtepi16_epi32(_mm_shuffle_epi32(e00, 0x4e)), _mm_cvtepi16_epi32(e00))), s); 59 v[1] = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i( 60 _mm_cvtepi16_epi32(_mm_shuffle_epi32(e01, 0x4e)), _mm_cvtepi16_epi32(e01))), s); 61 v[2] = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i( 62 _mm_cvtepi16_epi32(_mm_shuffle_epi32(e10, 0x4e)), _mm_cvtepi16_epi32(e10))), s); 63 v[3] = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i( 64 _mm_cvtepi16_epi32(_mm_shuffle_epi32(e11, 0x4e)), _mm_cvtepi16_epi32(e11))), s); 65 } 66 67 static inline void decode_q4_1(const uint8_t* bp, __m256 v[4]) { 68 float d = f16_to_f32((uint16_t)bp[0] | ((uint16_t)bp[1] << 8)); 69 float m = f16_to_f32((uint16_t)bp[2] | ((uint16_t)bp[3] << 8)); 70 __m256 d_v = _mm256_set1_ps(d), m_v = _mm256_set1_ps(m); 71 __m128i nb = _mm_loadu_si128((__m128i*)(bp + 4)); 72 __m128i lo = _mm_and_si128(nb, _mm_set1_epi8(15)); 73 __m128i hi = _mm_and_si128(_mm_srli_epi16(_mm_and_si128(nb, _mm_set1_epi8((char)0xF0)), 4), _mm_set1_epi8(15)); 74 __m128i e00 = _mm_cvtepu8_epi16(lo), e01 = _mm_cvtepu8_epi16(_mm_shuffle_epi32(lo, 0x4e)); 75 __m128i e10 = _mm_cvtepu8_epi16(hi), e11 = _mm_cvtepu8_epi16(_mm_shuffle_epi32(hi, 0x4e)); 76 v[0] = _mm256_fmadd_ps(_mm256_cvtepi32_ps(_mm256_set_m128i( 77 _mm_cvtepi16_epi32(_mm_shuffle_epi32(e00,0x4e)),_mm_cvtepi16_epi32(e00))), d_v, m_v); 78 v[1] = _mm256_fmadd_ps(_mm256_cvtepi32_ps(_mm256_set_m128i( 79 _mm_cvtepi16_epi32(_mm_shuffle_epi32(e01,0x4e)),_mm_cvtepi16_epi32(e01))), d_v, m_v); 80 v[2] = _mm256_fmadd_ps(_mm256_cvtepi32_ps(_mm256_set_m128i( 81 _mm_cvtepi16_epi32(_mm_shuffle_epi32(e10,0x4e)),_mm_cvtepi16_epi32(e10))), d_v, m_v); 82 v[3] = _mm256_fmadd_ps(_mm256_cvtepi32_ps(_mm256_set_m128i( 83 _mm_cvtepi16_epi32(_mm_shuffle_epi32(e11,0x4e)),_mm_cvtepi16_epi32(e11))), d_v, m_v); 84 } 85 86 static inline float block_dot_fma(const __m256 v[4], const float* x_base) { 87 __m256 x0 = _mm256_loadu_ps(x_base); 88 __m256 x1 = _mm256_loadu_ps(x_base + 8); 89 __m256 x2 = _mm256_loadu_ps(x_base + 16); 90 __m256 x3 = _mm256_loadu_ps(x_base + 24); 91 __m256 a = _mm256_mul_ps(v[0], x0); 92 a = _mm256_fmadd_ps(v[1], x1, a); 93 a = _mm256_fmadd_ps(v[2], x2, a); 94 a = _mm256_fmadd_ps(v[3], x3, a); 95 __m256 h = _mm256_hadd_ps(a, _mm256_permute2f128_ps(a, a, 1)); 96 h = _mm256_hadd_ps(h, h); 97 h = _mm256_hadd_ps(h, h); 98 return _mm256_cvtss_f32(h); 99 } 100 101 void q4_0_matmul_avx2(const uint8_t *restrict W, const float *restrict x, 102 float *restrict out, int n_rows, int n_cols) { 103 int bpr = n_cols / 32; 104 #pragma omp parallel for schedule(static) 105 for (int r = 0; r < n_rows; r++) { 106 float total = 0.0f; 107 for (int blk = 0; blk < bpr; blk++) { 108 __m256 v[4]; 109 decode_q4_0(W + ((size_t)r * bpr + blk) * Q4_0_BS, v); 110 total += block_dot_fma(v, x + blk * 32); 111 } 112 out[r] = total; 113 } 114 } 115 116 void q4_1_matmul_avx2(const uint8_t *restrict W, const float *restrict x, 117 float *restrict out, int n_rows, int n_cols) { 118 int bpr = n_cols / 32; 119 #pragma omp parallel for schedule(static) 120 for (int r = 0; r < n_rows; r++) { 121 float total = 0.0f; 122 for (int blk = 0; blk < bpr; blk++) { 123 __m256 v[4]; 124 decode_q4_1(W + ((size_t)r * bpr + blk) * Q4_1_BS, v); 125 total += block_dot_fma(v, x + blk * 32); 126 } 127 out[r] = total; 128 } 129 } 130 131 /* ═════════════════════════════════════════════════════════════════════════ 132 * Q6_K matmul — Dequantize-then-FMA approach 133 * 134 * Uses the proven scalar dequantization (matching quant_kernels_omp.c), 135 * then AVX2 FMA dot product on the dequantized values. 136 * Much faster than pure scalar because the dot product is vectorized. 137 * ═════════════════════════════════════════════════════════════════════════ */ 138 139 void q6_k_matmul_avx2(const uint8_t *restrict W, const float *restrict x, 140 float *restrict out, int n_rows, int n_cols) { 141 int nb = n_cols / QK_K; 142 float qt[QK_K] __attribute__((aligned(32))); 143 144 #pragma omp parallel for schedule(static) 145 for (int r = 0; r < n_rows; r++) { 146 const uint8_t *row = W + (size_t)r * nb * Q6_K_BS; 147 float sum = 0.0f; 148 149 for (int b = 0; b < nb; b++) { 150 const uint8_t *blk = row + (size_t)b * Q6_K_BS; 151 float d = f16_to_f32((uint16_t)blk[208] | ((uint16_t)blk[209] << 8)); 152 const int8_t *sc = (const int8_t *)(blk + 192); 153 const uint8_t *ql = blk; 154 const uint8_t *qh = blk + 128; 155 156 /* Dequantize 256 values using the EXACT same formula as the OMP kernel */ 157 for (int n = 0; n < QK_K; n += 128) { 158 for (int l = 0; l < 32; ++l) { 159 int is_ = l / 16; 160 int q1 = ((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32; 161 int q2 = ((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32; 162 int q3 = ((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32; 163 int q4 = ((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32; 164 165 float ds0 = d * sc[is_ + 0]; 166 float ds2 = d * sc[is_ + 2]; 167 float ds4 = d * sc[is_ + 4]; 168 float ds6 = d * sc[is_ + 6]; 169 170 qt[n + l + 0] = ds0 * q1; 171 qt[n + l + 32] = ds2 * q2; 172 qt[n + l + 64] = ds4 * q3; 173 qt[n + l + 96] = ds6 * q4; 174 } 175 ql += 64; 176 qh += 32; 177 sc += 8; 178 } 179 180 /* AVX2 FMA dot product: compute sum(qt * x) */ 181 int off = b * QK_K; 182 __m256 acc0 = _mm256_setzero_ps(); 183 __m256 acc1 = _mm256_setzero_ps(); 184 __m256 acc2 = _mm256_setzero_ps(); 185 __m256 acc3 = _mm256_setzero_ps(); 186 for (int j = 0; j < QK_K; j += 32) { 187 __m256 v0 = _mm256_loadu_ps(qt + j); 188 __m256 v1 = _mm256_loadu_ps(qt + j + 8); 189 __m256 v2 = _mm256_loadu_ps(qt + j + 16); 190 __m256 v3 = _mm256_loadu_ps(qt + j + 24); 191 __m256 x0 = _mm256_loadu_ps(x + off + j); 192 __m256 x1 = _mm256_loadu_ps(x + off + j + 8); 193 __m256 x2 = _mm256_loadu_ps(x + off + j + 16); 194 __m256 x3 = _mm256_loadu_ps(x + off + j + 24); 195 acc0 = _mm256_fmadd_ps(v0, x0, acc0); 196 acc1 = _mm256_fmadd_ps(v1, x1, acc1); 197 acc2 = _mm256_fmadd_ps(v2, x2, acc2); 198 acc3 = _mm256_fmadd_ps(v3, x3, acc3); 199 } 200 __m256 sum01 = _mm256_add_ps(acc0, acc1); 201 __m256 sum23 = _mm256_add_ps(acc2, acc3); 202 __m256 total = _mm256_add_ps(sum01, sum23); 203 __m256 h = _mm256_hadd_ps(total, _mm256_permute2f128_ps(total, total, 1)); 204 h = _mm256_hadd_ps(h, h); 205 h = _mm256_hadd_ps(h, h); 206 sum += _mm256_cvtss_f32(h); 207 } 208 out[r] = sum; 209 } 210 } 211 212 /* ═════════════════════════════════════════════════════════════════════════ 213 * Q8_0 matmul — AVX2 (reuse from quant_kernels_omp.c) 214 * ═════════════════════════════════════════════════════════════════════════ */ 215 216 void q8_0_matmul_avx2(const uint8_t *restrict W, const float *restrict x, 217 float *restrict out, int n_rows, int n_cols) { 218 int bpr = n_cols / 32; 219 #pragma omp parallel for schedule(static) 220 for (int r = 0; r < n_rows; r++) { 221 float total = 0.0f; 222 for (int blk = 0; blk < bpr; blk++) { 223 const uint8_t *bp = W + ((size_t)r * bpr + blk) * Q8_0_BS; 224 float d = f16_to_f32((uint16_t)bp[0] | ((uint16_t)bp[1] << 8)); 225 const int8_t *qs = (const int8_t *)(bp + 2); 226 int off = blk * 32; 227 __m256 d_v = _mm256_set1_ps(d); 228 __m256 bsum = _mm256_setzero_ps(); 229 for (int i = 0; i < 32; i += 8) { 230 __m128i q8_raw = _mm_loadl_epi64((__m128i*)(qs + i)); 231 __m128i q16 = _mm_cvtepi8_epi16(q8_raw); 232 __m256i q32 = _mm256_cvtepi16_epi32(q16); 233 __m256 v = _mm256_cvtepi32_ps(q32); 234 v = _mm256_mul_ps(v, d_v); 235 __m256 xv = _mm256_loadu_ps(x + off + i); 236 bsum = _mm256_fmadd_ps(v, xv, bsum); 237 } 238 /* Horizontal sum */ 239 __m256 h = _mm256_hadd_ps(bsum, _mm256_permute2f128_ps(bsum, bsum, 1)); 240 h = _mm256_hadd_ps(h, h); 241 h = _mm256_hadd_ps(h, h); 242 total += _mm256_cvtss_f32(h); 243 } 244 out[r] = total; 245 } 246 } 247 248 /* ═════════════════════════════════════════════════════════════════════════ 249 * Q4_K matmul — AVX2 vectorized 250 * 251 * Uses partial AVX2: dequantize in 32-value chunks to float, then FMA dot. 252 * Falls back to scalar for the complex scale unpacking but uses AVX2 FMA 253 * for the dot product accumulation. 254 * ═════════════════════════════════════════════════════════════════════════ */ 255 256 void q4_k_matmul_avx2(const uint8_t *restrict W, const float *restrict x, 257 float *restrict out, int n_rows, int n_cols) { 258 int nb = n_cols / QK_K; 259 /* Temp buffer for dequantized values (256 floats per block) */ 260 float qt[QK_K] __attribute__((aligned(32))); 261 262 #pragma omp parallel for schedule(static) 263 for (int r = 0; r < n_rows; r++) { 264 const uint8_t *row = W + (size_t)r * nb * Q4_K_BS; 265 float sum = 0.0f; 266 267 for (int b = 0; b < nb; b++) { 268 const uint8_t *blk = row + (size_t)b * Q4_K_BS; 269 float d = f16_to_f32((uint16_t)blk[0] | ((uint16_t)blk[1] << 8)); 270 float min = f16_to_f32((uint16_t)blk[2] | ((uint16_t)blk[3] << 8)); 271 const uint8_t *scales = blk + 4; 272 const uint8_t *q = blk + 16; 273 int is = 0; 274 275 /* Dequantize all 256 values */ 276 for (int j = 0; j < QK_K; j += 64) { 277 uint8_t sc, m; 278 get_scale_min_k4(is + 0, scales, &sc, &m); 279 float d1 = d * sc; float m1 = min * m; 280 get_scale_min_k4(is + 1, scales, &sc, &m); 281 float d2 = d * sc; float m2 = min * m; 282 283 for (int l = 0; l < 32; ++l) 284 qt[j + l] = d1 * (q[l] & 0xF) - m1; 285 for (int l = 0; l < 32; ++l) 286 qt[j + 32 + l] = d2 * (q[l] >> 4) - m2; 287 q += 32; 288 is += 2; 289 } 290 291 /* AVX2 FMA dot product */ 292 int off = b * QK_K; 293 __m256 acc0 = _mm256_setzero_ps(); 294 __m256 acc1 = _mm256_setzero_ps(); 295 __m256 acc2 = _mm256_setzero_ps(); 296 __m256 acc3 = _mm256_setzero_ps(); 297 for (int j = 0; j < QK_K; j += 32) { 298 __m256 v0 = _mm256_loadu_ps(qt + j); 299 __m256 v1 = _mm256_loadu_ps(qt + j + 8); 300 __m256 v2 = _mm256_loadu_ps(qt + j + 16); 301 __m256 v3 = _mm256_loadu_ps(qt + j + 24); 302 __m256 x0 = _mm256_loadu_ps(x + off + j); 303 __m256 x1 = _mm256_loadu_ps(x + off + j + 8); 304 __m256 x2 = _mm256_loadu_ps(x + off + j + 16); 305 __m256 x3 = _mm256_loadu_ps(x + off + j + 24); 306 acc0 = _mm256_fmadd_ps(v0, x0, acc0); 307 acc1 = _mm256_fmadd_ps(v1, x1, acc1); 308 acc2 = _mm256_fmadd_ps(v2, x2, acc2); 309 acc3 = _mm256_fmadd_ps(v3, x3, acc3); 310 } 311 __m256 sum01 = _mm256_add_ps(acc0, acc1); 312 __m256 sum23 = _mm256_add_ps(acc2, acc3); 313 __m256 total = _mm256_add_ps(sum01, sum23); 314 __m256 h = _mm256_hadd_ps(total, _mm256_permute2f128_ps(total, total, 1)); 315 h = _mm256_hadd_ps(h, h); 316 h = _mm256_hadd_ps(h, h); 317 sum += _mm256_cvtss_f32(h); 318 } 319 out[r] = sum; 320 } 321 } 322 323 /* ═════════════════════════════════════════════════════════════════════════ 324 * Q5_K matmul — AVX2 vectorized (dequantize + FMA dot) 325 * ═════════════════════════════════════════════════════════════════════════ */ 326 327 void q5_k_matmul_avx2(const uint8_t *restrict W, const float *restrict x, 328 float *restrict out, int n_rows, int n_cols) { 329 int nb = n_cols / QK_K; 330 float qt[QK_K] __attribute__((aligned(32))); 331 332 #pragma omp parallel for schedule(static) 333 for (int r = 0; r < n_rows; r++) { 334 const uint8_t *row = W + (size_t)r * nb * Q5_K_BS; 335 float sum = 0.0f; 336 337 for (int b = 0; b < nb; b++) { 338 const uint8_t *blk = row + (size_t)b * Q5_K_BS; 339 float d = f16_to_f32((uint16_t)blk[0] | ((uint16_t)blk[1] << 8)); 340 float min = f16_to_f32((uint16_t)blk[2] | ((uint16_t)blk[3] << 8)); 341 const uint8_t *scales = blk + 4; 342 const uint8_t *qh = blk + 16; 343 const uint8_t *ql = blk + 48; 344 int is = 0; 345 uint8_t u1 = 1, u2 = 2; 346 347 for (int j = 0; j < QK_K; j += 64) { 348 uint8_t sc, m; 349 get_scale_min_k4(is + 0, scales, &sc, &m); 350 float d1 = d * sc; float m1 = min * m; 351 get_scale_min_k4(is + 1, scales, &sc, &m); 352 float d2 = d * sc; float m2 = min * m; 353 354 for (int l = 0; l < 32; ++l) { 355 qt[j + l] = d1 * ((ql[l] & 0xF) + ((qh[l] & u1) ? 16 : 0)) - m1; 356 } 357 for (int l = 0; l < 32; ++l) { 358 qt[j + 32 + l] = d2 * ((ql[l] >> 4) + ((qh[l] & u2) ? 16 : 0)) - m2; 359 } 360 ql += 32; 361 is += 2; 362 u1 <<= 2; 363 u2 <<= 2; 364 } 365 366 /* AVX2 FMA dot */ 367 int off = b * QK_K; 368 __m256 acc0 = _mm256_setzero_ps(); 369 __m256 acc1 = _mm256_setzero_ps(); 370 __m256 acc2 = _mm256_setzero_ps(); 371 __m256 acc3 = _mm256_setzero_ps(); 372 for (int j = 0; j < QK_K; j += 32) { 373 __m256 v0 = _mm256_loadu_ps(qt + j); 374 __m256 v1 = _mm256_loadu_ps(qt + j + 8); 375 __m256 v2 = _mm256_loadu_ps(qt + j + 16); 376 __m256 v3 = _mm256_loadu_ps(qt + j + 24); 377 __m256 x0 = _mm256_loadu_ps(x + off + j); 378 __m256 x1 = _mm256_loadu_ps(x + off + j + 8); 379 __m256 x2 = _mm256_loadu_ps(x + off + j + 16); 380 __m256 x3 = _mm256_loadu_ps(x + off + j + 24); 381 acc0 = _mm256_fmadd_ps(v0, x0, acc0); 382 acc1 = _mm256_fmadd_ps(v1, x1, acc1); 383 acc2 = _mm256_fmadd_ps(v2, x2, acc2); 384 acc3 = _mm256_fmadd_ps(v3, x3, acc3); 385 } 386 __m256 sum01 = _mm256_add_ps(acc0, acc1); 387 __m256 sum23 = _mm256_add_ps(acc2, acc3); 388 __m256 total = _mm256_add_ps(sum01, sum23); 389 __m256 h = _mm256_hadd_ps(total, _mm256_permute2f128_ps(total, total, 1)); 390 h = _mm256_hadd_ps(h, h); 391 h = _mm256_hadd_ps(h, h); 392 sum += _mm256_cvtss_f32(h); 393 } 394 out[r] = sum; 395 } 396 } 397 398 /* ═════════════════════════════════════════════════════════════════════════ 399 * F32 matrix-vector multiply (AVX2) 400 * ═════════════════════════════════════════════════════════════════════════ */ 401 402 void f32_matmul_avx2(const float *restrict W, const float *restrict x, 403 float *restrict out, int n_rows, int n_cols) { 404 #pragma omp parallel for schedule(static) 405 for (int r = 0; r < n_rows; r++) { 406 const float *row = W + (size_t)r * n_cols; 407 __m256 sum0 = _mm256_setzero_ps(); 408 __m256 sum1 = _mm256_setzero_ps(); 409 int c; 410 for (c = 0; c <= n_cols - 16; c += 16) { 411 __m256 w0 = _mm256_loadu_ps(row + c); 412 __m256 w1 = _mm256_loadu_ps(row + c + 8); 413 __m256 x0 = _mm256_loadu_ps(x + c); 414 __m256 x1 = _mm256_loadu_ps(x + c + 8); 415 sum0 = _mm256_fmadd_ps(w0, x0, sum0); 416 sum1 = _mm256_fmadd_ps(w1, x1, sum1); 417 } 418 __m256 total = _mm256_add_ps(sum0, sum1); 419 float result = 0.0f; 420 for (; c < n_cols; c++) { 421 result += row[c] * x[c]; 422 } 423 __m256 h = _mm256_hadd_ps(total, _mm256_permute2f128_ps(total, total, 1)); 424 h = _mm256_hadd_ps(h, h); 425 h = _mm256_hadd_ps(h, h); 426 result += _mm256_cvtss_f32(h); 427 out[r] = result; 428 } 429 } 430 431 /* ═════════════════════════════════════════════════════════════════════════ 432 * Unified dispatch + thread control 433 * ═════════════════════════════════════════════════════════════════════════ */ 434 435 void set_num_threads(int n) { omp_set_num_threads(n); } 436 437 void quant_matmul_avx2(const uint8_t *restrict W, const float *restrict x, 438 float *restrict out, int n_rows, int n_cols, int quant_type) { 439 switch (quant_type) { 440 case 0: f32_matmul_avx2((const float *)W, x, out, n_rows, n_cols); break; 441 case 2: q4_0_matmul_avx2(W, x, out, n_rows, n_cols); break; 442 case 3: q4_1_matmul_avx2(W, x, out, n_rows, n_cols); break; 443 case 8: q8_0_matmul_avx2(W, x, out, n_rows, n_cols); break; 444 case 12: q4_k_matmul_avx2(W, x, out, n_rows, n_cols); break; 445 case 13: q5_k_matmul_avx2(W, x, out, n_rows, n_cols); break; 446 case 14: q6_k_matmul_avx2(W, x, out, n_rows, n_cols); break; 447 default: break; 448 } 449 }