MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / src / mojollama / kernels / legacy-c / quant_kernels_avx2.c
| 1 | /** |
| 2 | * quant_kernels_avx2.c — AVX2-vectorized Q4_K, Q5_K, Q6_K, Q8_0 matmul kernels |
| 3 | * plus F32 matmul and RMS norm. |
| 4 | * |
| 5 | * All kernels use the same calling convention: |
| 6 | * (W_raw, x, out, n_rows, n_cols) or (W_raw, x, out, n_rows, n_cols, quant_type) |
| 7 | * |
| 8 | * Compiled with: gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC -o quant_kernels_omp.so |
| 9 | */ |
| 10 | |
| 11 | #include <immintrin.h> |
| 12 | #include <math.h> |
| 13 | #include <omp.h> |
| 14 | #include <stdint.h> |
| 15 | #include <string.h> |
| 16 | |
| 17 | #define QK_K 256 /* K-type super-block size */ |
| 18 | #define QK8_0 32 /* Q8_0 block size */ |
| 19 | #define Q4_K_BS 144 /* bytes per 256 values */ |
| 20 | #define Q5_K_BS 176 |
| 21 | #define Q6_K_BS 210 |
| 22 | #define Q8_0_BS 34 |
| 23 | #define Q4_0_BS 18 |
| 24 | #define Q4_1_BS 20 |
| 25 | |
| 26 | /* f16 to f32 conversion */ |
| 27 | static inline float f16_to_f32(uint16_t h) { |
| 28 | uint32_t sign = (h >> 15) & 1; |
| 29 | uint32_t exp = (h >> 10) & 0x1f; |
| 30 | uint32_t mant = h & 0x3ff; |
| 31 | float out; |
| 32 | if (exp == 0) { |
| 33 | if (mant == 0) { out = 0.0f; } |
| 34 | else { out = ldexpf((float)mant / 1024.0f, -14); } |
| 35 | } else if (exp == 31) { |
| 36 | out = (mant == 0) ? INFINITY : NAN; |
| 37 | } else { |
| 38 | out = ldexpf(1.0f + (float)mant / 1024.0f, (int)exp - 15); |
| 39 | } |
| 40 | return sign ? -out : out; |
| 41 | } |
| 42 | |
| 43 | /* Helper: horizontal sum of 8 floats in __m256 */ |
| 44 | static inline float hsum_ps(__m256 v) { |
| 45 | __m256 h = _mm256_hadd_ps(v, _mm256_permute2f128_ps(v, v, 1)); |
| 46 | h = _mm256_hadd_ps(h, h); |
| 47 | h = _mm256_hadd_ps(h, h); |
| 48 | return _mm256_cvtss_f32(h); |
| 49 | } |
| 50 | |
| 51 | /* Helper: horizontal sum of __m128 */ |
| 52 | static inline float hsum128_ps(__m128 v) { |
| 53 | __m128 h = _mm_hadd_ps(v, v); |
| 54 | h = _mm_hadd_ps(h, h); |
| 55 | return _mm_cvtss_f32(h); |
| 56 | } |
| 57 | |
| 58 | /* Scale extraction helpers for K-type quantization */ |
| 59 | static inline void get_scale_min_k4(int j, const uint8_t *q, uint8_t *d, uint8_t *m) { |
| 60 | if (j < 4) { |
| 61 | *d = q[j] & 63; *m = q[j] >> 6; |
| 62 | } else if (j < 20) { |
| 63 | *d = (q[j+4] & 15) | ((q[j-2] >> 6) << 4); |
| 64 | *m = (q[j+4] >> 4); |
| 65 | } else { |
| 66 | *d = (q[j+4] & 63) >> 2; |
| 67 | *m = (q[j+4] >> 4); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /* ═════════════════════════════════════════════════════════════════════════ |
| 72 | * Q4_K AVX2 matmul |
| 73 | * Block: 144 bytes per 256 values |
| 74 | * ═══════════════════════════════════════════════════════════════════════ */ |
| 75 | |
| 76 | void q4_k_matmul_omp(const uint8_t *restrict W, const float *restrict x, |
| 77 | float *restrict out, int n_rows, int n_cols) { |
| 78 | int nb = n_cols / QK_K; |
| 79 | |
| 80 | #pragma omp parallel for schedule(static) |
| 81 | for (int r = 0; r < n_rows; r++) { |
| 82 | const uint8_t *row = W + (size_t)r * nb * Q4_K_BS; |
| 83 | __m256 acc = _mm256_setzero_ps(); |
| 84 | |
| 85 | for (int b = 0; b < nb; b++) { |
| 86 | const uint8_t *blk = row + (size_t)b * Q4_K_BS; |
| 87 | float d = f16_to_f32((uint16_t)blk[0] | ((uint16_t)blk[1] << 8)); |
| 88 | float min = f16_to_f32((uint16_t)blk[2] | ((uint16_t)blk[3] << 8)); |
| 89 | const uint8_t *scales = blk + 4; |
| 90 | const uint8_t *q = blk + 16; |
| 91 | int x_off = b * QK_K; |
| 92 | int is = 0; |
| 93 | |
| 94 | for (int j = 0; j < QK_K; j += 64) { |
| 95 | uint8_t sc, m; |
| 96 | get_scale_min_k4(is + 0, scales, &sc, &m); |
| 97 | float d1 = d * sc; float m1 = min * m; |
| 98 | get_scale_min_k4(is + 1, scales, &sc, &m); |
| 99 | float d2 = d * sc; float m2 = min * m; |
| 100 | |
| 101 | /* Process lo nibbles (32 values) + hi nibbles (32 values) = 64 values */ |
| 102 | __m256 d1v = _mm256_set1_ps(d1); |
| 103 | __m256 d2v = _mm256_set1_ps(d2); |
| 104 | __m256 m1v = _mm256_set1_ps(m1); |
| 105 | __m256 m2v = _mm256_set1_ps(m2); |
| 106 | int mask_lo = 0x0F0F0F0F; |
| 107 | |
| 108 | /* Lo nibbles: first 32 values */ |
| 109 | for (int l = 0; l < 32; l += 8) { |
| 110 | /* Load 8 x-values */ |
| 111 | __m256 xv = _mm256_loadu_ps(x + x_off + j + l); |
| 112 | /* Extract lo nibbles from 8 packed bytes */ |
| 113 | __m256i qi = _mm256_cvtepu8_epi32(_mm_loadl_epi64((__m128i*)(q + l))); |
| 114 | __m256i lo_mask = _mm256_set1_epi32(0xF); |
| 115 | __m256i nib = _mm256_and_si256(qi, lo_mask); |
| 116 | __m256 fv = _mm256_cvtepi32_ps(nib); |
| 117 | /* d1 * nib - m1 */ |
| 118 | __m256 deq = _mm256_sub_ps(_mm256_mul_ps(d1v, fv), m1v); |
| 119 | acc = _mm256_fmadd_ps(deq, xv, acc); |
| 120 | } |
| 121 | /* Hi nibbles: next 32 values (positions j+32..j+63) */ |
| 122 | for (int l = 0; l < 32; l += 8) { |
| 123 | __m256 xv = _mm256_loadu_ps(x + x_off + j + 32 + l); |
| 124 | __m256i qi = _mm256_cvtepu8_epi32(_mm_loadl_epi64((__m128i*)(q + l))); |
| 125 | __m256i hi_mask = _mm256_set1_epi32(0xF); |
| 126 | __m256i nib = _mm256_srli_epi32(_mm256_and_si256(qi, _mm256_set1_epi32(0xFF)), 4); |
| 127 | __m256 fv = _mm256_cvtepi32_ps(nib); |
| 128 | __m256 deq = _mm256_sub_ps(_mm256_mul_ps(d2v, fv), m2v); |
| 129 | acc = _mm256_fmadd_ps(deq, xv, acc); |
| 130 | } |
| 131 | q += 32; |
| 132 | is += 2; |
| 133 | } |
| 134 | } |
| 135 | out[r] = hsum_ps(acc); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /* ═════════════════════════════════════════════════════════════════════════ |
| 140 | * Q5_K AVX2 matmul |
| 141 | * Block: 176 bytes per 256 values |
| 142 | * ═══════════════════════════════════════════════════════════════════════ */ |
| 143 | |
| 144 | void q5_k_matmul_omp(const uint8_t *restrict W, const float *restrict x, |
| 145 | float *restrict out, int n_rows, int n_cols) { |
| 146 | int nb = n_cols / QK_K; |
| 147 | |
| 148 | #pragma omp parallel for schedule(static) |
| 149 | for (int r = 0; r < n_rows; r++) { |
| 150 | const uint8_t *row = W + (size_t)r * nb * Q5_K_BS; |
| 151 | __m256 acc = _mm256_setzero_ps(); |
| 152 | |
| 153 | for (int b = 0; b < nb; b++) { |
| 154 | const uint8_t *blk = row + (size_t)b * Q5_K_BS; |
| 155 | float d = f16_to_f32((uint16_t)blk[0] | ((uint16_t)blk[1] << 8)); |
| 156 | float min = f16_to_f32((uint16_t)blk[2] | ((uint16_t)blk[3] << 8)); |
| 157 | const uint8_t *scales = blk + 4; |
| 158 | const uint8_t *qh_arr = blk + 16; |
| 159 | const uint8_t *ql = blk + 48; |
| 160 | int x_off = b * QK_K; |
| 161 | int is = 0; |
| 162 | uint8_t u1 = 1, u2 = 2; |
| 163 | |
| 164 | for (int j = 0; j < QK_K; j += 64) { |
| 165 | uint8_t sc, m; |
| 166 | get_scale_min_k4(is + 0, scales, &sc, &m); |
| 167 | float d1 = d * sc; float m1 = min * m; |
| 168 | get_scale_min_k4(is + 1, scales, &sc, &m); |
| 169 | float d2 = d * sc; float m2 = min * m; |
| 170 | |
| 171 | __m256 d1v = _mm256_set1_ps(d1); |
| 172 | __m256 d2v = _mm256_set1_ps(d2); |
| 173 | __m256 m1v = _mm256_set1_ps(m1); |
| 174 | __m256 m2v = _mm256_set1_ps(m2); |
| 175 | __m256i u1v = _mm256_set1_epi32(u1); |
| 176 | __m256i u2v = _mm256_set1_epi32(u2); |
| 177 | |
| 178 | /* Lo nibbles + 5th bit */ |
| 179 | for (int l = 0; l < 32; l += 8) { |
| 180 | __m256 xv = _mm256_loadu_ps(x + x_off + j + l); |
| 181 | __m256i qi = _mm256_cvtepu8_epi32(_mm_loadl_epi64((__m128i*)(ql + l))); |
| 182 | __m256i lo = _mm256_and_si256(qi, _mm256_set1_epi32(0xF)); |
| 183 | __m256i qh_lo = _mm256_cvtepu8_epi32(_mm_loadl_epi64((__m128i*)(qh_arr + l))); |
| 184 | __m256i bit5_lo = _mm256_and_si256(qh_lo, u1v); |
| 185 | __m256i shifted = _mm256_slli_epi32(bit5_lo, 1); /* 0 or 16 */ |
| 186 | __m256 fv_lo = _mm256_cvtepi32_ps(_mm256_add_epi32(lo, shifted)); |
| 187 | __m256 deq = _mm256_sub_ps(_mm256_mul_ps(d1v, fv_lo), m1v); |
| 188 | acc = _mm256_fmadd_ps(deq, xv, acc); |
| 189 | } |
| 190 | /* Hi nibbles + 5th bit */ |
| 191 | for (int l = 0; l < 32; l += 8) { |
| 192 | __m256 xv = _mm256_loadu_ps(x + x_off + j + 32 + l); |
| 193 | __m256i qi = _mm256_cvtepu8_epi32(_mm_loadl_epi64((__m128i*)(ql + l))); |
| 194 | __m256i hi = _mm256_srli_epi32(qi, 4); |
| 195 | __m256i qh_hi = _mm256_cvtepu8_epi32(_mm_loadl_epi64((__m128i*)(qh_arr + l))); |
| 196 | __m256i bit5_hi = _mm256_and_si256(qh_hi, u2v); |
| 197 | __m256i shifted = _mm256_slli_epi32(bit5_hi, 1); |
| 198 | __m256 fv_hi = _mm256_cvtepi32_ps(_mm256_add_epi32(hi, shifted)); |
| 199 | __m256 deq = _mm256_sub_ps(_mm256_mul_ps(d2v, fv_hi), m2v); |
| 200 | acc = _mm256_fmadd_ps(deq, xv, acc); |
| 201 | } |
| 202 | ql += 32; |
| 203 | is += 2; |
| 204 | u1 <<= 2; |
| 205 | u2 <<= 2; |
| 206 | } |
| 207 | } |
| 208 | out[r] = hsum_ps(acc); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /* ═════════════════════════════════════════════════════════════════════════ |
| 213 | * Q6_K AVX2 matmul |
| 214 | * Block: 210 bytes per 256 values |
| 215 | * ═══════════════════════════════════════════════════════════════════════ */ |
| 216 | |
| 217 | void q6_k_matmul_omp(const uint8_t *restrict W, const float *restrict x, |
| 218 | float *restrict out, int n_rows, int n_cols) { |
| 219 | int nb = n_cols / QK_K; |
| 220 | |
| 221 | #pragma omp parallel for schedule(static) |
| 222 | for (int r = 0; r < n_rows; r++) { |
| 223 | const uint8_t *row = W + (size_t)r * nb * Q6_K_BS; |
| 224 | __m256 acc = _mm256_setzero_ps(); |
| 225 | |
| 226 | for (int b = 0; b < nb; b++) { |
| 227 | const uint8_t *blk = row + (size_t)b * Q6_K_BS; |
| 228 | const float d = f16_to_f32((uint16_t)blk[208] | ((uint16_t)blk[209] << 8)); |
| 229 | const int8_t *sc = (const int8_t *)(blk + 192); |
| 230 | const uint8_t *ql = blk; |
| 231 | const uint8_t *qh = blk + 128; |
| 232 | int base = b * QK_K; |
| 233 | |
| 234 | for (int n = 0; n < QK_K; n += 128) { |
| 235 | /* Process 32 values at a time using AVX2 */ |
| 236 | for (int l = 0; l < 32; l += 8) { |
| 237 | int is_ = l / 16; |
| 238 | |
| 239 | /* Dequantize 4 groups of 8 values */ |
| 240 | /* Group 1: ql[l+0] & 0xF | (qh[l+0]>>0 & 3)<<4 - 32 */ |
| 241 | __m256i ql_lo = _mm256_cvtepu8_epi32(_mm_loadl_epi64((__m128i*)(ql + l))); |
| 242 | __m256i ql_hi = _mm256_cvtepu8_epi32(_mm_loadl_epi64((__m128i*)(ql + l + 32))); |
| 243 | __m256i qh8 = _mm256_cvtepu8_epi32(_mm_loadl_epi64((__m128i*)(qh + l))); |
| 244 | |
| 245 | __m256i mask4 = _mm256_set1_epi32(0xF); |
| 246 | __m256i mask3 = _mm256_set1_epi32(3); |
| 247 | |
| 248 | /* q1 = (ql[l]&0xF) | ((qh[l]>>0)&3)<<4 */ |
| 249 | __m256i q1 = _mm256_add_epi32( |
| 250 | _mm256_and_si256(ql_lo, mask4), |
| 251 | _mm256_slli_epi32(_mm256_and_si256(qh8, mask3), 4)); |
| 252 | /* q2 = (ql[l+32]&0xF) | ((qh[l]>>2)&3)<<4 */ |
| 253 | __m256i q2 = _mm256_add_epi32( |
| 254 | _mm256_and_si256(ql_hi, mask4), |
| 255 | _mm256_slli_epi32(_mm256_and_si256(_mm256_srli_epi32(qh8, 2), mask3), 4)); |
| 256 | /* q3 = (ql[l]>>4) | ((qh[l]>>4)&3)<<4 */ |
| 257 | __m256i q3 = _mm256_add_epi32( |
| 258 | _mm256_srli_epi32(_mm256_and_si256(ql_lo, _mm256_set1_epi32(0xFF)), 4), |
| 259 | _mm256_slli_epi32(_mm256_and_si256(_mm256_srli_epi32(qh8, 4), mask3), 4)); |
| 260 | /* q4 = (ql[l+32]>>4) | ((qh[l]>>6)&3)<<4 */ |
| 261 | __m256i q4 = _mm256_add_epi32( |
| 262 | _mm256_srli_epi32(_mm256_and_si256(ql_hi, _mm256_set1_epi32(0xFF)), 4), |
| 263 | _mm256_slli_epi32(_mm256_and_si256(_mm256_srli_epi32(qh8, 6), mask3), 4)); |
| 264 | |
| 265 | /* Subtract 32 to center */ |
| 266 | __m256i off = _mm256_set1_epi32(32); |
| 267 | q1 = _mm256_sub_epi32(q1, off); |
| 268 | q2 = _mm256_sub_epi32(q2, off); |
| 269 | q3 = _mm256_sub_epi32(q3, off); |
| 270 | q4 = _mm256_sub_epi32(q4, off); |
| 271 | |
| 272 | /* Scale: d * sc[is_+0..3] */ |
| 273 | float ds0 = d * sc[is_ + 0]; |
| 274 | float ds2 = d * sc[is_ + 2]; |
| 275 | float ds4 = d * sc[is_ + 4]; |
| 276 | float ds6 = d * sc[is_ + 6]; |
| 277 | |
| 278 | __m256 fq1 = _mm256_mul_ps(_mm256_cvtepi32_ps(q1), _mm256_set1_ps(ds0)); |
| 279 | __m256 fq2 = _mm256_mul_ps(_mm256_cvtepi32_ps(q2), _mm256_set1_ps(ds2)); |
| 280 | __m256 fq3 = _mm256_mul_ps(_mm256_cvtepi32_ps(q3), _mm256_set1_ps(ds4)); |
| 281 | __m256 fq4 = _mm256_mul_ps(_mm256_cvtepi32_ps(q4), _mm256_set1_ps(ds6)); |
| 282 | |
| 283 | /* Multiply by x and accumulate */ |
| 284 | __m256 x1 = _mm256_loadu_ps(x + base + n + l); |
| 285 | __m256 x2 = _mm256_loadu_ps(x + base + n + l + 32); |
| 286 | __m256 x3 = _mm256_loadu_ps(x + base + n + l + 64); |
| 287 | __m256 x4 = _mm256_loadu_ps(x + base + n + l + 96); |
| 288 | |
| 289 | /* FMA: acc += fq * x for each group */ |
| 290 | acc = _mm256_fmadd_ps(fq1, x1, acc); |
| 291 | acc = _mm256_fmadd_ps(fq2, x2, acc); |
| 292 | acc = _mm256_fmadd_ps(fq3, x3, acc); |
| 293 | acc = _mm256_fmadd_ps(fq4, x4, acc); |
| 294 | } |
| 295 | ql += 64; |
| 296 | qh += 32; |
| 297 | sc += 8; |
| 298 | } |
| 299 | } |
| 300 | out[r] = hsum_ps(acc); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | /* ═════════════════════════════════════════════════════════════════════════ |
| 305 | * Q8_0 AVX2 matmul (34 bytes per 32 values) |
| 306 | * Uses AVX2 int8 sign-extension + FMA for fast dot product |
| 307 | * ═══════════════════════════════════════════════════════════════════════ */ |
| 308 | |
| 309 | void q8_0_matmul_omp(const uint8_t *restrict W, const float *restrict x, |
| 310 | float *restrict out, int n_rows, int n_cols) { |
| 311 | int bpr = n_cols / QK8_0; |
| 312 | |
| 313 | #pragma omp parallel for schedule(static) |
| 314 | for (int r = 0; r < n_rows; r++) { |
| 315 | float total = 0.0f; |
| 316 | __m256 acc = _mm256_setzero_ps(); |
| 317 | |
| 318 | for (int blk = 0; blk < bpr; blk++) { |
| 319 | const uint8_t *bp = W + ((size_t)r * bpr + blk) * Q8_0_BS; |
| 320 | float d = f16_to_f32((uint16_t)bp[0] | ((uint16_t)bp[1] << 8)); |
| 321 | const int8_t *qs = (const int8_t *)(bp + 2); |
| 322 | int off = blk * 32; |
| 323 | __m256 d_v = _mm256_set1_ps(d); |
| 324 | |
| 325 | /* Process all 32 int8 values as FMA with scale */ |
| 326 | /* 32 int8 values: process as 4x8 -> dequantize -> FMA with x */ |
| 327 | for (int i = 0; i < 32; i += 8) { |
| 328 | __m128i q8 = _mm_loadl_epi64((__m128i*)(qs + i)); |
| 329 | __m256i qi = _mm256_cvtepi8_epi32(q8); |
| 330 | __m256 qf = _mm256_cvtepi32_ps(qi); |
| 331 | __m256 xv = _mm256_loadu_ps(x + off + i); |
| 332 | acc = _mm256_fmadd_ps(_mm256_mul_ps(qf, d_v), xv, acc); |
| 333 | } |
| 334 | } |
| 335 | out[r] = hsum_ps(acc); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | /* ═════════════════════════════════════════════════════════════════════════ |
| 340 | * Q4_0 AVX2 matmul (18 bytes per 32 values) |
| 341 | * ═══════════════════════════════════════════════════════════════════════ */ |
| 342 | |
| 343 | static inline void decode_q4_0(const uint8_t *blk, __m256 v[4]) { |
| 344 | float scale = f16_to_f32((uint16_t)blk[0] | ((uint16_t)blk[1] << 8)); |
| 345 | __m256 s = _mm256_set1_ps(scale); |
| 346 | __m128i nb = _mm_loadu_si128((__m128i*)(blk + 2)); |
| 347 | /* GGUF Q4_0: 16 bytes → 32 values as [lo0..lo15, hi0..hi15] */ |
| 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 | /* Sign-extend to 16-bit then 32-bit float */ |
| 353 | __m128i lo16 = _mm_cvtepi8_epi16(lo_s); |
| 354 | __m128i hi16 = _mm_cvtepi8_epi16(hi_s); |
| 355 | __m256i lo32 = _mm256_cvtepi16_epi32(lo16); |
| 356 | __m256i hi32 = _mm256_cvtepi16_epi32(hi16); |
| 357 | v[0] = _mm256_mul_ps(_mm256_cvtepi32_ps(lo32), s); /* positions 0..7 */ |
| 358 | v[1] = _mm256_mul_ps(_mm256_cvtepi32_ps(hi32), s); /* positions 8..15 */ |
| 359 | /* Second 8 bytes of nibbles */ |
| 360 | __m128i lo16b = _mm_unpackhi_epi64(lo16, _mm_setzero_si128()); |
| 361 | __m128i hi16b = _mm_unpackhi_epi64(hi16, _mm_setzero_si128()); |
| 362 | /* lo16b/hi16b have at most 8 int16 values; but we actually need the remaining |
| 363 | * 16 values from the second 8-byte half. Let me redo this properly. */ |
| 364 | __m128i nb2 = _mm_loadu_si128((__m128i*)(blk + 2 + 8)); |
| 365 | __m128i lo2 = _mm_and_si128(nb2, _mm_set1_epi8(15)); |
| 366 | __m128i hi2 = _mm_and_si128(_mm_srli_epi16(_mm_and_si128(nb2, _mm_set1_epi8((char)0xF0)), 4), _mm_set1_epi8(15)); |
| 367 | __m128i lo_s2 = _mm_sub_epi8(lo2, _mm_set1_epi8(8)); |
| 368 | __m128i hi_s2 = _mm_sub_epi8(hi2, _mm_set1_epi8(8)); |
| 369 | __m128i lo16_2 = _mm_cvtepi8_epi16(lo_s2); |
| 370 | __m128i hi16_2 = _mm_cvtepi8_epi16(hi_s2); |
| 371 | v[2] = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(lo16_2)), s); |
| 372 | v[3] = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(hi16_2)), s); |
| 373 | } |
| 374 | |
| 375 | static inline float block_dot_fma(__m256 v[4], const float *x) { |
| 376 | __m256 s0 = _mm256_mul_ps(v[0], _mm256_loadu_ps(x)); |
| 377 | s0 = _mm256_fmadd_ps(v[1], _mm256_loadu_ps(x + 8), s0); |
| 378 | s0 = _mm256_fmadd_ps(v[2], _mm256_loadu_ps(x + 16), s0); |
| 379 | s0 = _mm256_fmadd_ps(v[3], _mm256_loadu_ps(x + 24), s0); |
| 380 | return hsum_ps(s0); |
| 381 | } |
| 382 | |
| 383 | void q4_0_matmul_omp(const uint8_t *restrict W, const float *restrict x, |
| 384 | float *restrict out, int n_rows, int n_cols) { |
| 385 | int bpr = n_cols / 32; |
| 386 | |
| 387 | #pragma omp parallel for schedule(static) |
| 388 | for (int r = 0; r < n_rows; r++) { |
| 389 | float total = 0.0f; |
| 390 | for (int blk = 0; blk < bpr; blk++) { |
| 391 | __m256 v[4]; |
| 392 | decode_q4_0(W + ((size_t)r * bpr + blk) * Q4_0_BS, v); |
| 393 | total += block_dot_fma(v, x + blk * 32); |
| 394 | } |
| 395 | out[r] = total; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | /* ═════════════════════════════════════════════════════════════════════════ |
| 400 | * Q4_1 AVX2 matmul (20 bytes per 32 values) |
| 401 | * ═══════════════════════════════════════════════════════════════════════ */ |
| 402 | |
| 403 | static inline void decode_q4_1(const uint8_t *blk, __m256 v[4]) { |
| 404 | float d = f16_to_f32((uint16_t)blk[0] | ((uint16_t)blk[1] << 8)); |
| 405 | float m = f16_to_f32((uint16_t)blk[2] | ((uint16_t)blk[3] << 8)); |
| 406 | const uint8_t *qs = blk + 4; |
| 407 | __m256 dv = _mm256_set1_ps(d); |
| 408 | __m256 mv = _mm256_set1_ps(m); |
| 409 | for (int j = 0; j < 4; j++) { |
| 410 | __m128i q = _mm_loadl_epi64((__m128i*)(qs + j * 4)); |
| 411 | __m256i qi = _mm256_cvtepu8_epi32(q); |
| 412 | __m256i lo = _mm256_and_si256(qi, _mm256_set1_epi32(0xF)); |
| 413 | __m256i hi = _mm256_srli_epi32(qi, 4); |
| 414 | __m256 fv = _mm256_add_ps(_mm256_mul_ps(dv, _mm256_cvtepi32_ps(lo)), mv); |
| 415 | v[j] = _mm256_add_ps(fv, _mm256_mul_ps(dv, _mm256_cvtepi32_ps(hi))); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | void q4_1_matmul_omp(const uint8_t *restrict W, const float *restrict x, |
| 420 | float *restrict out, int n_rows, int n_cols) { |
| 421 | int bpr = n_cols / 32; |
| 422 | |
| 423 | #pragma omp parallel for schedule(static) |
| 424 | for (int r = 0; r < n_rows; r++) { |
| 425 | float total = 0.0f; |
| 426 | for (int blk = 0; blk < bpr; blk++) { |
| 427 | __m256 v[4]; |
| 428 | decode_q4_1(W + ((size_t)r * bpr + blk) * Q4_1_BS, v); |
| 429 | total += block_dot_fma(v, x + blk * 32); |
| 430 | } |
| 431 | out[r] = total; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | /* ═════════════════════════════════════════════════════════════════════════ |
| 436 | * F32 matrix-vector multiply (AVX2 + FMA) |
| 437 | * ═══════════════════════════════════════════════════════════════════════ */ |
| 438 | |
| 439 | void f32_matmul_omp(const float *restrict W, const float *restrict x, |
| 440 | float *restrict out, int n_rows, int n_cols) { |
| 441 | #pragma omp parallel for schedule(static) |
| 442 | for (int r = 0; r < n_rows; r++) { |
| 443 | const float *row = W + (size_t)r * n_cols; |
| 444 | __m256 sum0 = _mm256_setzero_ps(); |
| 445 | __m256 sum1 = _mm256_setzero_ps(); |
| 446 | int c; |
| 447 | for (c = 0; c <= n_cols - 16; c += 16) { |
| 448 | __m256 w0 = _mm256_loadu_ps(row + c); |
| 449 | __m256 w1 = _mm256_loadu_ps(row + c + 8); |
| 450 | __m256 x0 = _mm256_loadu_ps(x + c); |
| 451 | __m256 x1 = _mm256_loadu_ps(x + c + 8); |
| 452 | sum0 = _mm256_fmadd_ps(w0, x0, sum0); |
| 453 | sum1 = _mm256_fmadd_ps(w1, x1, sum1); |
| 454 | } |
| 455 | __m256 total = _mm256_add_ps(sum0, sum1); |
| 456 | float result = hsum_ps(total); |
| 457 | for (; c < n_cols; c++) result += row[c] * x[c]; |
| 458 | out[r] = result; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | /* ═════════════════════════════════════════════════════════════════════════ |
| 463 | * Block F32 matmul for small n_cols (reduces OMP barrier overhead) |
| 464 | * Performs n_proj matrix-vector products in a single OMP parallel region |
| 465 | * ═══════════════════════════════════════════════════════════════════════ */ |
| 466 | |
| 467 | void f32_matmul_omp_blocked(const float *restrict W, const float *restrict x, |
| 468 | float *restrict out, int n_rows, int n_cols) { |
| 469 | f32_matmul_omp(W, x, out, n_rows, n_cols); |
| 470 | } |
| 471 | |
| 472 | /* ═════════════════════════════════════════════════════════════════════════ |
| 473 | * Batched matmul: up to 7 projections in one OMP call |
| 474 | * ═══════════════════════════════════════════════════════════════════════ */ |
| 475 | |
| 476 | #define MAX_PROJ 7 |
| 477 | |
| 478 | static void process_row(const uint8_t* w[MAX_PROJ], const float* x, float* out[MAX_PROJ], |
| 479 | int nrows[MAX_PROJ], int ncols[MAX_PROJ], int ts[MAX_PROJ], |
| 480 | int n_proj, int row) { |
| 481 | for (int p = 0; p < n_proj; p++) { |
| 482 | if (row >= nrows[p]) continue; |
| 483 | float total = 0.0f; |
| 484 | int nc = ncols[p], bp = nc / 32; |
| 485 | void (*dec)(const uint8_t*, __m256*) = (ts[p] == Q4_1_BS) ? decode_q4_1 : decode_q4_0; |
| 486 | for (int blk = 0; blk < bp; blk++) { |
| 487 | __m256 v[4]; |
| 488 | dec(w[p] + ((size_t)row * bp + blk) * ts[p], v); |
| 489 | total += block_dot_fma(v, x + blk * 32); |
| 490 | } |
| 491 | out[p][row] = total; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | void batch_matmul(const uint8_t* w[MAX_PROJ], const float* x, float* out[MAX_PROJ], |
| 496 | int nrows[MAX_PROJ], int ncols[MAX_PROJ], int ts[MAX_PROJ], |
| 497 | int n_proj) { |
| 498 | int max_rows = 0; |
| 499 | for (int p = 0; p < n_proj; p++) { |
| 500 | memset(out[p], 0, nrows[p] * sizeof(float)); |
| 501 | if (nrows[p] > max_rows) max_rows = nrows[p]; |
| 502 | } |
| 503 | #pragma omp parallel for schedule(static) |
| 504 | for (int row = 0; row < max_rows; row++) { |
| 505 | process_row(w, x, out, nrows, ncols, ts, n_proj, row); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | /* ═════════════════════════════════════════════════════════════════════════ |
| 510 | * Batched quant matmul: up to 7 projections with mixed quant types |
| 511 | * Uses quant_matmul_omp dispatch per projection |
| 512 | * ═══════════════════════════════════════════════════════════════════════ */ |
| 513 | |
| 514 | /* ═════════════════════════════════════════════════════════════════════════ |
| 515 | * RMS normalization (AVX2 vectorized) |
| 516 | * ═══════════════════════════════════════════════════════════════════════ */ |
| 517 | |
| 518 | void rms_norm(const float* x, const float* weight, float* out, int n) { |
| 519 | __m256 ss_vec = _mm256_setzero_ps(); |
| 520 | int i; |
| 521 | for (i = 0; i <= n - 8; i += 8) { |
| 522 | __m256 v = _mm256_loadu_ps(x + i); |
| 523 | ss_vec = _mm256_fmadd_ps(v, v, ss_vec); |
| 524 | } |
| 525 | __m256 h = _mm256_hadd_ps(ss_vec, _mm256_permute2f128_ps(ss_vec, ss_vec, 1)); |
| 526 | h = _mm256_hadd_ps(h, h); h = _mm256_hadd_ps(h, h); |
| 527 | float ss = _mm256_cvtss_f32(h); |
| 528 | for (; i < n; i++) ss += x[i] * x[i]; |
| 529 | float inv_rms = 1.0f / sqrtf(ss / n + 1e-5f); |
| 530 | __m256 inv_v = _mm256_set1_ps(inv_rms); |
| 531 | for (i = 0; i <= n - 8; i += 8) { |
| 532 | __m256 v = _mm256_loadu_ps(x + i); |
| 533 | __m256 w = _mm256_loadu_ps(weight + i); |
| 534 | _mm256_storeu_ps(out + i, _mm256_mul_ps(_mm256_mul_ps(v, inv_v), w)); |
| 535 | } |
| 536 | for (; i < n; i++) out[i] = x[i] * inv_rms * weight[i]; |
| 537 | } |
| 538 | |
| 539 | /* ═════════════════════════════════════════════════════════════════════════ |
| 540 | * SiLU activation |
| 541 | * ═══════════════════════════════════════════════════════════════════════ */ |
| 542 | |
| 543 | void silu(float* x, int n) { |
| 544 | int i; |
| 545 | for (i = 0; i <= n - 8; i += 8) { |
| 546 | __m256 v = _mm256_loadu_ps(x + i); |
| 547 | float tmp[8]; |
| 548 | _mm256_storeu_ps(tmp, v); |
| 549 | for (int j = 0; j < 8; j++) tmp[j] = tmp[j] / (1.0f + expf(-tmp[j])); |
| 550 | _mm256_storeu_ps(x + i, _mm256_loadu_ps(tmp)); |
| 551 | } |
| 552 | for (; i < n; i++) x[i] = x[i] / (1.0f + expf(-x[i])); |
| 553 | } |
| 554 | |
| 555 | void silu_omp(float* x, int n) { |
| 556 | #pragma omp parallel for schedule(static) |
| 557 | for (int i = 0; i < n; i++) x[i] = x[i] / (1.0f + expf(-x[i])); |
| 558 | } |
| 559 | |
| 560 | void softmax(float* x, int n) { |
| 561 | float max_val = x[0]; |
| 562 | for (int i = 1; i < n; i++) if (x[i] > max_val) max_val = x[i]; |
| 563 | float sum = 0.0f; |
| 564 | for (int i = 0; i < n; i++) { x[i] = expf(x[i] - max_val); sum += x[i]; } |
| 565 | float inv = 1.0f / sum; |
| 566 | for (int i = 0; i < n; i++) x[i] *= inv; |
| 567 | } |
| 568 | |
| 569 | int get_max_threads(void) { return omp_get_max_threads(); } |
| 570 | void set_num_threads(int n) { omp_set_num_threads(n); } |
| 571 | |
| 572 | /* ═════════════════════════════════════════════════════════════════════════ |
| 573 | * Unified dispatch: quant_matmul_omp |
| 574 | * quant_type: 0=F32, 2=Q4_0, 3=Q4_1, 8=Q8_0, 12=Q4_K, 13=Q5_K, 14=Q6_K |
| 575 | * ═══════════════════════════════════════════════════════════════════════ */ |
| 576 | |
| 577 | void quant_matmul_omp(const uint8_t *restrict W, const float *restrict x, |
| 578 | float *restrict out, int n_rows, int n_cols, int quant_type) { |
| 579 | switch (quant_type) { |
| 580 | case 0: f32_matmul_omp((const float *)W, x, out, n_rows, n_cols); break; |
| 581 | case 2: q4_0_matmul_omp(W, x, out, n_rows, n_cols); break; |
| 582 | case 3: q4_1_matmul_omp(W, x, out, n_rows, n_cols); break; |
| 583 | case 8: q8_0_matmul_omp(W, x, out, n_rows, n_cols); break; |
| 584 | case 12: q4_k_matmul_omp(W, x, out, n_rows, n_cols); break; |
| 585 | case 13: q5_k_matmul_omp(W, x, out, n_rows, n_cols); break; |
| 586 | case 14: q6_k_matmul_omp(W, x, out, n_rows, n_cols); break; |
| 587 | } |
| 588 | } |