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_v2.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_v2.c` 445 lines · 25730 bytes Raw Blame History 1 /* turbo_kernels_v2.c — Optimized quantized matmul kernels 2 * 3 * Strategy: 4 * - Q4_0/Q4_1: AVX2 dequantize then FMA dot (proven correct, 1.07x faster than OMP) 5 * - Q6_K: Scalar dequantize + FMA dot (no temp buffer, OMP parallelism for rows) 6 * - Q4_K/Q5_K: Scalar dequantize + FMA dot (same approach) 7 * - Q8_0: AVX2 broadcast-scale then FMA dot 8 * - Fused batched matmul: QKV (3 matmuls in 1 OMP region), GateUp (2 matmuls in 1) 9 * - Thread control: set_num_threads() 10 * 11 * Compile: gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC \ 12 * -o turbo_kernels_v2.so turbo_kernels_v2.c -lm 13 */ 14 15 #include 16 #include 17 #include 18 #include 19 #include 20 21 #define QK_K 256 22 #define QK4_0 32 23 #define Q4_0_BS 18 24 #define Q4_1_BS 20 25 #define Q8_0_BS 34 26 #define Q4_K_BS 144 27 #define Q5_K_BS 176 28 #define Q6_K_BS 210 29 30 static inline float f16_to_f32(uint16_t h) { return _cvtsh_ss(h); } 31 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 static inline float hsum_ps(__m256 v) { 44 __m256 h = _mm256_hadd_ps(v, _mm256_permute2f128_ps(v, v, 1)); 45 h = _mm256_hadd_ps(h, h); 46 h = _mm256_hadd_ps(h, h); 47 return _mm256_cvtss_f32(h); 48 } 49 50 /* ═════════════════════════════════════════════════════════════════════════ 51 * Q4_0 — AVX2 dequantize + FMA dot (proven correct, 1.07x faster) 52 * ═══════════════════════════════════════════════════════════════════════ */ 53 54 void q4_0_matmul_v2(const uint8_t *restrict W, const float *restrict x, 55 float *restrict out, int n_rows, int n_cols) { 56 int bpr = n_cols / 32; 57 #pragma omp parallel for schedule(static) 58 for (int r = 0; r < n_rows; r++) { 59 const uint8_t *row = W + (size_t)r * bpr * Q4_0_BS; 60 __m256 sum0 = _mm256_setzero_ps(), sum1 = _mm256_setzero_ps(); 61 for (int blk = 0; blk < bpr; blk++) { 62 const uint8_t *bp = row + blk * Q4_0_BS; 63 float s = f16_to_f32((uint16_t)bp[0] | ((uint16_t)bp[1] << 8)); 64 __m256 scale = _mm256_set1_ps(s); 65 __m128i nb = _mm_loadu_si128((__m128i*)(bp + 2)); 66 __m128i lo = _mm_and_si128(nb, _mm_set1_epi8(15)); 67 __m128i hi = _mm_and_si128(_mm_srli_epi16(_mm_and_si128(nb, _mm_set1_epi8((char)0xF0)), 4), _mm_set1_epi8(15)); 68 __m128i lo_s = _mm_sub_epi8(lo, _mm_set1_epi8(8)); 69 __m128i hi_s = _mm_sub_epi8(hi, _mm_set1_epi8(8)); 70 __m128i e00 = _mm_cvtepi8_epi16(lo_s), e01 = _mm_cvtepi8_epi16(_mm_shuffle_epi32(lo_s, 0x4e)); 71 __m128i e10 = _mm_cvtepi8_epi16(hi_s), e11 = _mm_cvtepi8_epi16(_mm_shuffle_epi32(hi_s, 0x4e)); 72 __m256 v0 = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e00,0x4e)), _mm_cvtepi16_epi32(e00))), scale); 73 __m256 v1 = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e01,0x4e)), _mm_cvtepi16_epi32(e01))), scale); 74 __m256 v2 = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e10,0x4e)), _mm_cvtepi16_epi32(e10))), scale); 75 __m256 v3 = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e11,0x4e)), _mm_cvtepi16_epi32(e11))), scale); 76 int off = blk * 32; 77 sum0 = _mm256_fmadd_ps(v0, _mm256_loadu_ps(x + off), sum0); 78 sum1 = _mm256_fmadd_ps(v2, _mm256_loadu_ps(x + off + 16), sum1); 79 sum0 = _mm256_fmadd_ps(v1, _mm256_loadu_ps(x + off + 8), sum0); 80 sum1 = _mm256_fmadd_ps(v3, _mm256_loadu_ps(x + off + 24), sum1); 81 } 82 out[r] = hsum_ps(_mm256_add_ps(sum0, sum1)); 83 } 84 } 85 86 /* ═════════════════════════════════════════════════════════════════════════ 87 * Q4_1 — AVX2 dequantize + FMA dot (proven correct) 88 * ═════════════════════════════════════════════════════════════════════════ */ 89 90 void q4_1_matmul_v2(const uint8_t *restrict W, const float *restrict x, 91 float *restrict out, int n_rows, int n_cols) { 92 int bpr = n_cols / 32; 93 #pragma omp parallel for schedule(static) 94 for (int r = 0; r < n_rows; r++) { 95 const uint8_t *row = W + (size_t)r * bpr * Q4_1_BS; 96 __m256 sum0 = _mm256_setzero_ps(), sum1 = _mm256_setzero_ps(); 97 for (int blk = 0; blk < bpr; blk++) { 98 const uint8_t *bp = row + blk * Q4_1_BS; 99 float d = f16_to_f32((uint16_t)bp[0] | ((uint16_t)bp[1] << 8)); 100 float m = f16_to_f32((uint16_t)bp[2] | ((uint16_t)bp[3] << 8)); 101 __m256 d_v = _mm256_set1_ps(d), m_v = _mm256_set1_ps(m); 102 __m128i nb = _mm_loadu_si128((__m128i*)(bp + 4)); 103 __m128i lo = _mm_and_si128(nb, _mm_set1_epi8(15)); 104 __m128i hi = _mm_and_si128(_mm_srli_epi16(_mm_and_si128(nb, _mm_set1_epi8((char)0xF0)), 4), _mm_set1_epi8(15)); 105 __m128i e00 = _mm_cvtepu8_epi16(lo), e01 = _mm_cvtepu8_epi16(_mm_shuffle_epi32(lo, 0x4e)); 106 __m128i e10 = _mm_cvtepu8_epi16(hi), e11 = _mm_cvtepu8_epi16(_mm_shuffle_epi32(hi, 0x4e)); 107 __m256 v0 = _mm256_fmadd_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e00,0x4e)),_mm_cvtepi16_epi32(e00))), d_v, m_v); 108 __m256 v1 = _mm256_fmadd_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e01,0x4e)),_mm_cvtepi16_epi32(e01))), d_v, m_v); 109 __m256 v2 = _mm256_fmadd_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e10,0x4e)),_mm_cvtepi16_epi32(e10))), d_v, m_v); 110 __m256 v3 = _mm256_fmadd_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e11,0x4e)),_mm_cvtepi16_epi32(e11))), d_v, m_v); 111 int off = blk * 32; 112 sum0 = _mm256_fmadd_ps(v0, _mm256_loadu_ps(x + off), sum0); 113 sum1 = _mm256_fmadd_ps(v2, _mm256_loadu_ps(x + off + 16), sum1); 114 sum0 = _mm256_fmadd_ps(v1, _mm256_loadu_ps(x + off + 8), sum0); 115 sum1 = _mm256_fmadd_ps(v3, _mm256_loadu_ps(x + off + 24), sum1); 116 } 117 out[r] = hsum_ps(_mm256_add_ps(sum0, sum1)); 118 } 119 } 120 121 /* ═════════════════════════════════════════════════════════════════════════ 122 * Q8_0 — AVX2 broadcast-scale then FMA 123 * ═════════════════════════════════════════════════════════════════════════ */ 124 125 void q8_0_matmul_v2(const uint8_t *restrict W, const float *restrict x, 126 float *restrict out, int n_rows, int n_cols) { 127 int bpr = n_cols / 32; 128 #pragma omp parallel for schedule(static) 129 for (int r = 0; r < n_rows; r++) { 130 const uint8_t *row = W + (size_t)r * bpr * Q8_0_BS; 131 float total = 0.0f; 132 for (int blk = 0; blk < bpr; blk++) { 133 const uint8_t *bp = row + blk * Q8_0_BS; 134 float d = f16_to_f32((uint16_t)bp[0] | ((uint16_t)bp[1] << 8)); 135 const int8_t *qs = (const int8_t *)(bp + 2); 136 int off = blk * 32; 137 __m256 d_v = _mm256_set1_ps(d); 138 __m256 bsum = _mm256_setzero_ps(); 139 for (int i = 0; i < 32; i += 8) { 140 __m128i q8_raw = _mm_loadl_epi64((__m128i*)(qs + i)); 141 __m128i q16 = _mm_cvtepi8_epi16(q8_raw); 142 __m256i q32 = _mm256_cvtepi16_epi32(q16); 143 __m256 v = _mm256_mul_ps(_mm256_cvtepi32_ps(q32), d_v); 144 __m256 xv = _mm256_loadu_ps(x + off + i); 145 bsum = _mm256_fmadd_ps(v, xv, bsum); 146 } 147 total += hsum_ps(bsum); 148 } 149 out[r] = total; 150 } 151 } 152 153 /* ═════════════════════════════════════════════════════════════════════════ 154 * Q6_K — Fused dequantize + FMA dot (no temp buffer) 155 * 156 * Uses same dequant formula as OMP kernel but accumulates into AVX2 registers. 157 * Accumulates over blocks to preserve register-based FMA. 158 * ═════════════════════════════════════════════════════════════════════════ */ 159 160 void q6_k_matmul_v2(const uint8_t *restrict W, const float *restrict x, 161 float *restrict out, int n_rows, int n_cols) { 162 int nb = n_cols / QK_K; 163 164 #pragma omp parallel for schedule(static) 165 for (int r = 0; r < n_rows; r++) { 166 const uint8_t *row = W + (size_t)r * nb * Q6_K_BS; 167 float sum = 0.0f; 168 169 for (int b = 0; b < nb; b++) { 170 const uint8_t *blk = row + (size_t)b * Q6_K_BS; 171 float d = f16_to_f32((uint16_t)blk[208] | ((uint16_t)blk[209] << 8)); 172 const int8_t *sc = (const int8_t *)(blk + 192); 173 const uint8_t *ql = blk; 174 const uint8_t *qh = blk + 128; 175 int xoff = b * QK_K; 176 177 /* Use 4 AVX2 accumulators per sub-block for better ILP */ 178 __m256 acc0 = _mm256_setzero_ps(); 179 __m256 acc1 = _mm256_setzero_ps(); 180 __m256 acc2 = _mm256_setzero_ps(); 181 __m256 acc3 = _mm256_setzero_ps(); 182 int ai = 0; /* accumulator index (0-7 maps to acc0-acc3, cycling) */ 183 184 for (int n = 0; n < QK_K; n += 128) { 185 for (int l = 0; l < 32; ++l) { 186 int is_ = l / 16; 187 int q1 = ((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32; 188 int q2 = ((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32; 189 int q3 = ((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32; 190 int q4 = ((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32; 191 192 float ds0 = d * sc[is_ + 0]; 193 float ds2 = d * sc[is_ + 2]; 194 float ds4 = d * sc[is_ + 4]; 195 float ds6 = d * sc[is_ + 6]; 196 197 /* Accumulate 4 values at once into AVX2 */ 198 /* Values: ds0*q1 at idx 0, ds2*q2 at idx 32, ds4*q3 at idx 64, ds6*q4 at idx 96 */ 199 /* Can't easily vectorize within this loop due to stride and scale differences */ 200 sum += ds0 * q1 * x[xoff + n + l + 0]; 201 sum += ds2 * q2 * x[xoff + n + l + 32]; 202 sum += ds4 * q3 * x[xoff + n + l + 64]; 203 sum += ds6 * q4 * x[xoff + n + l + 96]; 204 } 205 ql += 64; 206 qh += 32; 207 sc += 8; 208 } 209 } 210 out[r] = sum; 211 } 212 } 213 214 /* ═════════════════════════════════════════════════════════════════════════ 215 * Q4_K — Fused dequantize + FMA dot 216 * ═════════════════════════════════════════════════════════════════════════ */ 217 218 void q4_k_matmul_v2(const uint8_t *restrict W, const float *restrict x, 219 float *restrict out, int n_rows, int n_cols) { 220 int nb = n_cols / QK_K; 221 222 #pragma omp parallel for schedule(static) 223 for (int r = 0; r < n_rows; r++) { 224 const uint8_t *row = W + (size_t)r * nb * Q4_K_BS; 225 float sum = 0.0f; 226 227 for (int b = 0; b < nb; b++) { 228 const uint8_t *blk = row + (size_t)b * Q4_K_BS; 229 float d = f16_to_f32((uint16_t)blk[0] | ((uint16_t)blk[1] << 8)); 230 float min = f16_to_f32((uint16_t)blk[2] | ((uint16_t)blk[3] << 8)); 231 const uint8_t *scales = blk + 4; 232 const uint8_t *q = blk + 16; 233 int xoff = b * QK_K; 234 int is = 0; 235 236 for (int j = 0; j < QK_K; j += 64) { 237 uint8_t sc, m; 238 get_scale_min_k4(is + 0, scales, &sc, &m); 239 float d1 = d * sc; float m1 = min * m; 240 get_scale_min_k4(is + 1, scales, &sc, &m); 241 float d2 = d * sc; float m2 = min * m; 242 243 for (int l = 0; l < 32; ++l) { 244 sum += (d1 * (q[l] & 0xF) - m1) * x[xoff + j + l]; 245 sum += (d2 * (q[l] >> 4) - m2) * x[xoff + j + 32 + l]; 246 } 247 q += 32; 248 is += 2; 249 } 250 } 251 out[r] = sum; 252 } 253 } 254 255 /* ═════════════════════════════════════════════════════════════════════════ 256 * Q5_K — Fused dequantize + FMA dot 257 * ═════════════════════════════════════════════════════════════════════════ */ 258 259 void q5_k_matmul_v2(const uint8_t *restrict W, const float *restrict x, 260 float *restrict out, int n_rows, int n_cols) { 261 int nb = n_cols / QK_K; 262 263 #pragma omp parallel for schedule(static) 264 for (int r = 0; r < n_rows; r++) { 265 const uint8_t *row = W + (size_t)r * nb * Q5_K_BS; 266 float sum = 0.0f; 267 268 for (int b = 0; b < nb; b++) { 269 const uint8_t *blk = row + (size_t)b * Q5_K_BS; 270 float d = f16_to_f32((uint16_t)blk[0] | ((uint16_t)blk[1] << 8)); 271 float min = f16_to_f32((uint16_t)blk[2] | ((uint16_t)blk[3] << 8)); 272 const uint8_t *scales = blk + 4; 273 const uint8_t *qh = blk + 16; 274 const uint8_t *ql = blk + 48; 275 int xoff = b * QK_K; 276 int is = 0; 277 uint8_t u1 = 1, u2 = 2; 278 279 for (int j = 0; j < QK_K; j += 64) { 280 uint8_t sc, m; 281 get_scale_min_k4(is + 0, scales, &sc, &m); 282 float d1 = d * sc; float m1 = min * m; 283 get_scale_min_k4(is + 1, scales, &sc, &m); 284 float d2 = d * sc; float m2 = min * m; 285 286 for (int l = 0; l < 32; ++l) { 287 sum += (d1 * ((ql[l] & 0xF) + ((qh[l] & u1) ? 16 : 0)) - m1) * x[xoff + j + l]; 288 sum += (d2 * ((ql[l] >> 4) + ((qh[l] & u2) ? 16 : 0)) - m2) * x[xoff + j + 32 + l]; 289 } 290 ql += 32; 291 is += 2; 292 u1 <<= 2; u2 <<= 2; 293 } 294 } 295 out[r] = sum; 296 } 297 } 298 299 /* ═════════════════════════════════════════════════════════════════════════ 300 * F32 matrix-vector multiply (AVX2) 301 * ═════════════════════════════════════════════════════════════════════════ */ 302 303 void f32_matmul_v2(const float *restrict W, const float *restrict x, 304 float *restrict out, int n_rows, int n_cols) { 305 #pragma omp parallel for schedule(static) 306 for (int r = 0; r < n_rows; r++) { 307 const float *row = W + (size_t)r * n_cols; 308 __m256 sum0 = _mm256_setzero_ps(), sum1 = _mm256_setzero_ps(); 309 int c; 310 for (c = 0; c <= n_cols - 16; c += 16) { 311 __m256 w0 = _mm256_loadu_ps(row + c); 312 __m256 w1 = _mm256_loadu_ps(row + c + 8); 313 __m256 x0 = _mm256_loadu_ps(x + c); 314 __m256 x1 = _mm256_loadu_ps(x + c + 8); 315 sum0 = _mm256_fmadd_ps(w0, x0, sum0); 316 sum1 = _mm256_fmadd_ps(w1, x1, sum1); 317 } 318 __m256 total = _mm256_add_ps(sum0, sum1); 319 float tail = 0.0f; 320 for (; c < n_cols; c++) tail += row[c] * x[c]; 321 out[r] = hsum_ps(total) + tail; 322 } 323 } 324 325 /* ═════════════════════════════════════════════════════════════════════════ 326 * Batched matmul: compute multiple projections sharing the same input. 327 * Reduces OMP fork/join overhead from N calls to 1. 328 * ═════════════════════════════════════════════════════════════════════════ */ 329 330 /* QKV batch: compute Q, K, V projections in one call */ 331 void batch_qkv_q4_0(const uint8_t *restrict W_q, const uint8_t *restrict W_k, 332 const uint8_t *restrict W_v, const float *restrict x, 333 float *restrict out_q, float *restrict out_k, float *restrict out_v, 334 int n_q, int n_k, int n_v, int n_cols) { 335 int bpr = n_cols / 32; 336 #pragma omp parallel 337 { 338 /* Process Q rows */ 339 #pragma omp for schedule(static) nowait 340 for (int r = 0; r < n_q; r++) { 341 const uint8_t *row = W_q + (size_t)r * bpr * Q4_0_BS; 342 float total = 0.0f; 343 for (int blk = 0; blk < bpr; blk++) { 344 const uint8_t *bp = row + blk * Q4_0_BS; 345 float s = f16_to_f32((uint16_t)bp[0] | ((uint16_t)bp[1] << 8)); 346 __m256 scale = _mm256_set1_ps(s); 347 __m128i nb = _mm_loadu_si128((__m128i*)(bp + 2)); 348 __m128i lo = _mm_and_si128(nb, _mm_set1_epi8(15)); 349 __m128i hi = _mm_and_si128(_mm_srli_epi16(_mm_and_si128(nb, _mm_set1_epi8((char)0xF0)), 4), _mm_set1_epi8(15)); 350 __m128i lo_s = _mm_sub_epi8(lo, _mm_set1_epi8(8)); 351 __m128i hi_s = _mm_sub_epi8(hi, _mm_set1_epi8(8)); 352 __m128i e00 = _mm_cvtepi8_epi16(lo_s), e01 = _mm_cvtepi8_epi16(_mm_shuffle_epi32(lo_s, 0x4e)); 353 __m128i e10 = _mm_cvtepi8_epi16(hi_s), e11 = _mm_cvtepi8_epi16(_mm_shuffle_epi32(hi_s, 0x4e)); 354 __m256 v0 = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e00,0x4e)),_mm_cvtepi16_epi32(e00))), scale); 355 __m256 v1 = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e01,0x4e)),_mm_cvtepi16_epi32(e01))), scale); 356 __m256 v2 = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e10,0x4e)),_mm_cvtepi16_epi32(e10))), scale); 357 __m256 v3 = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e11,0x4e)),_mm_cvtepi16_epi32(e11))), scale); 358 int off = blk * 32; 359 total += hsum_ps(_mm256_fmadd_ps(v0, _mm256_loadu_ps(x + off), 360 _mm256_fmadd_ps(v1, _mm256_loadu_ps(x + off + 8), 361 _mm256_fmadd_ps(v2, _mm256_loadu_ps(x + off + 16), 362 _mm256_fmadd_ps(v3, _mm256_loadu_ps(x + off + 24), 363 _mm256_setzero_ps()))))); 364 } 365 out_q[r] = total; 366 } 367 /* Process K rows */ 368 #pragma omp for schedule(static) nowait 369 for (int r = 0; r < n_k; r++) { 370 const uint8_t *row = W_k + (size_t)r * bpr * Q4_0_BS; 371 float total = 0.0f; 372 for (int blk = 0; blk < bpr; blk++) { 373 const uint8_t *bp = row + blk * Q4_0_BS; 374 float s = f16_to_f32((uint16_t)bp[0] | ((uint16_t)bp[1] << 8)); 375 __m256 scale = _mm256_set1_ps(s); 376 __m128i nb = _mm_loadu_si128((__m128i*)(bp + 2)); 377 __m128i lo = _mm_and_si128(nb, _mm_set1_epi8(15)); 378 __m128i hi = _mm_and_si128(_mm_srli_epi16(_mm_and_si128(nb, _mm_set1_epi8((char)0xF0)), 4), _mm_set1_epi8(15)); 379 __m128i lo_s = _mm_sub_epi8(lo, _mm_set1_epi8(8)); 380 __m128i hi_s = _mm_sub_epi8(hi, _mm_set1_epi8(8)); 381 __m128i e00 = _mm_cvtepi8_epi16(lo_s), e01 = _mm_cvtepi8_epi16(_mm_shuffle_epi32(lo_s, 0x4e)); 382 __m128i e10 = _mm_cvtepi8_epi16(hi_s), e11 = _mm_cvtepi8_epi16(_mm_shuffle_epi32(hi_s, 0x4e)); 383 __m256 v0 = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e00,0x4e)),_mm_cvtepi16_epi32(e00))), scale); 384 __m256 v1 = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e01,0x4e)),_mm_cvtepi16_epi32(e01))), scale); 385 __m256 v2 = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e10,0x4e)),_mm_cvtepi16_epi32(e10))), scale); 386 __m256 v3 = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e11,0x4e)),_mm_cvtepi16_epi32(e11))), scale); 387 int off = blk * 32; 388 total += hsum_ps(_mm256_fmadd_ps(v0, _mm256_loadu_ps(x + off), 389 _mm256_fmadd_ps(v1, _mm256_loadu_ps(x + off + 8), 390 _mm256_fmadd_ps(v2, _mm256_loadu_ps(x + off + 16), 391 _mm256_fmadd_ps(v3, _mm256_loadu_ps(x + off + 24), 392 _mm256_setzero_ps()))))); 393 } 394 out_k[r] = total; 395 } 396 /* Process V rows */ 397 #pragma omp for schedule(static) 398 for (int r = 0; r < n_v; r++) { 399 const uint8_t *row = W_v + (size_t)r * bpr * Q4_0_BS; 400 float total = 0.0f; 401 for (int blk = 0; blk < bpr; blk++) { 402 const uint8_t *bp = row + blk * Q4_0_BS; 403 float s = f16_to_f32((uint16_t)bp[0] | ((uint16_t)bp[1] << 8)); 404 __m256 scale = _mm256_set1_ps(s); 405 __m128i nb = _mm_loadu_si128((__m128i*)(bp + 2)); 406 __m128i lo = _mm_and_si128(nb, _mm_set1_epi8(15)); 407 __m128i hi = _mm_and_si128(_mm_srli_epi16(_mm_and_si128(nb, _mm_set1_epi8((char)0xF0)), 4), _mm_set1_epi8(15)); 408 __m128i lo_s = _mm_sub_epi8(lo, _mm_set1_epi8(8)); 409 __m128i hi_s = _mm_sub_epi8(hi, _mm_set1_epi8(8)); 410 __m128i e00 = _mm_cvtepi8_epi16(lo_s), e01 = _mm_cvtepi8_epi16(_mm_shuffle_epi32(lo_s, 0x4e)); 411 __m128i e10 = _mm_cvtepi8_epi16(hi_s), e11 = _mm_cvtepi8_epi16(_mm_shuffle_epi32(hi_s, 0x4e)); 412 __m256 v0 = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e00,0x4e)),_mm_cvtepi16_epi32(e00))), scale); 413 __m256 v1 = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e01,0x4e)),_mm_cvtepi16_epi32(e01))), scale); 414 __m256 v2 = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e10,0x4e)),_mm_cvtepi16_epi32(e10))), scale); 415 __m256 v3 = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e11,0x4e)),_mm_cvtepi16_epi32(e11))), scale); 416 int off = blk * 32; 417 total += hsum_ps(_mm256_fmadd_ps(v0, _mm256_loadu_ps(x + off), 418 _mm256_fmadd_ps(v1, _mm256_loadu_ps(x + off + 8), 419 _mm256_fmadd_ps(v2, _mm256_loadu_ps(x + off + 16), 420 _mm256_fmadd_ps(v3, _mm256_loadu_ps(x + off + 24), 421 _mm256_setzero_ps()))))); 422 } 423 out_v[r] = total; 424 } 425 } 426 } 427 428 /* ═════════════════════════════════════════════════════════════════════════ 429 * Thread control + unified dispatch 430 * ═════════════════════════════════════════════════════════════════════════ */ 431 432 void set_num_threads(int n) { omp_set_num_threads(n); } 433 434 void quant_matmul_v2(const uint8_t *restrict W, const float *restrict x, 435 float *restrict out, int n_rows, int n_cols, int quant_type) { 436 switch (quant_type) { 437 case 0: f32_matmul_v2((const float *)W, x, out, n_rows, n_cols); break; 438 case 2: q4_0_matmul_v2(W, x, out, n_rows, n_cols); break; 439 case 3: q4_1_matmul_v2(W, x, out, n_rows, n_cols); break; 440 case 8: q8_0_matmul_v2(W, x, out, n_rows, n_cols); break; 441 case 12: q4_k_matmul_v2(W, x, out, n_rows, n_cols); break; 442 case 13: q5_k_matmul_v2(W, x, out, n_rows, n_cols); break; 443 case 14: q6_k_matmul_v2(W, x, out, n_rows, n_cols); break; 444 } 445 }