MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / src / mojollama / kernels / legacy-c / q4_kernel_avx2.c
| 1 | /* Q4_0/Q4_1 matmul AVX2 kernel (single-threaded, for per-row threading). |
| 2 | * Compile: gcc -O3 -mavx2 -mfma -mf16c -shared -fPIC -o q4_kernel_avx2.so q4_kernel_avx2.c |
| 3 | */ |
| 4 | #include <stdint.h> |
| 5 | #include <math.h> |
| 6 | #include <immintrin.h> |
| 7 | |
| 8 | static inline float f16_to_f32(uint16_t h) { return _cvtsh_ss(h); } |
| 9 | |
| 10 | static inline void decode_q4_0(const uint8_t* bp, __m256 v[4]) { |
| 11 | float scale = f16_to_f32((uint16_t)bp[0] | ((uint16_t)bp[1] << 8)); |
| 12 | __m256 s = _mm256_set1_ps(scale); |
| 13 | __m128i nb = _mm_loadu_si128((__m128i*)(bp + 2)); |
| 14 | __m128i lo = _mm_and_si128(nb, _mm_set1_epi8(15)); |
| 15 | __m128i hi = _mm_and_si128(_mm_srli_epi16(nb, 4), _mm_set1_epi8(15)); |
| 16 | __m128i i0 = _mm_sub_epi8(_mm_unpacklo_epi8(lo, hi), _mm_set1_epi8(8)); |
| 17 | __m128i i1 = _mm_sub_epi8(_mm_unpackhi_epi8(lo, hi), _mm_set1_epi8(8)); |
| 18 | __m128i e00 = _mm_cvtepi8_epi16(i0), e01 = _mm_cvtepi8_epi16(_mm_shuffle_epi32(i0, 0x4e)); |
| 19 | __m128i e10 = _mm_cvtepi8_epi16(i1), e11 = _mm_cvtepi8_epi16(_mm_shuffle_epi32(i1, 0x4e)); |
| 20 | v[0] = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e00, 0x4e)), _mm_cvtepi16_epi32(e00))), s); |
| 21 | v[1] = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e01, 0x4e)), _mm_cvtepi16_epi32(e01))), s); |
| 22 | v[2] = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e10, 0x4e)), _mm_cvtepi16_epi32(e10))), s); |
| 23 | v[3] = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e11, 0x4e)), _mm_cvtepi16_epi32(e11))), s); |
| 24 | } |
| 25 | |
| 26 | static inline void decode_q4_1(const uint8_t* bp, __m256 v[4]) { |
| 27 | float dmin = f16_to_f32((uint16_t)bp[0] | ((uint16_t)bp[1] << 8)); |
| 28 | float dmax = f16_to_f32((uint16_t)bp[2] | ((uint16_t)bp[3] << 8)); |
| 29 | float diff = (dmax - dmin) / 15.0f; |
| 30 | __m256 dmin_v = _mm256_set1_ps(dmin), diff_v = _mm256_set1_ps(diff); |
| 31 | __m128i nb = _mm_loadu_si128((__m128i*)(bp + 4)); |
| 32 | __m128i lo = _mm_and_si128(nb, _mm_set1_epi8(15)); |
| 33 | __m128i hi = _mm_and_si128(_mm_srli_epi16(nb, 4), _mm_set1_epi8(15)); |
| 34 | __m128i i0 = _mm_unpacklo_epi8(lo, hi), i1 = _mm_unpackhi_epi8(lo, hi); |
| 35 | __m128i e00 = _mm_cvtepu8_epi16(i0), e01 = _mm_cvtepu8_epi16(_mm_shuffle_epi32(i0, 0x4e)); |
| 36 | __m128i e10 = _mm_cvtepu8_epi16(i1), e11 = _mm_cvtepu8_epi16(_mm_shuffle_epi32(i1, 0x4e)); |
| 37 | v[0] = _mm256_fmadd_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e00,0x4e)),_mm_cvtepi16_epi32(e00))), diff_v, dmin_v); |
| 38 | v[1] = _mm256_fmadd_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e01,0x4e)),_mm_cvtepi16_epi32(e01))), diff_v, dmin_v); |
| 39 | v[2] = _mm256_fmadd_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e10,0x4e)),_mm_cvtepi16_epi32(e10))), diff_v, dmin_v); |
| 40 | v[3] = _mm256_fmadd_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e11,0x4e)),_mm_cvtepi16_epi32(e11))), diff_v, dmin_v); |
| 41 | } |
| 42 | |
| 43 | /* Row-outer accumulation: total=0, accumulate blocks, write once per row. */ |
| 44 | void q4_matmul_avx2( |
| 45 | const uint8_t* w, const float* x, float* out, |
| 46 | int n_rows, int n_cols, int start, int end, int type_size |
| 47 | ) { |
| 48 | int bpr = n_cols / 32; |
| 49 | void (*decode)(const uint8_t*, __m256*) = (type_size == 20) ? decode_q4_1 : decode_q4_0; |
| 50 | for (int r = start; r < end; r++) { |
| 51 | float total = 0.0f; |
| 52 | for (int blk = 0; blk < bpr; blk++) { |
| 53 | __m256 v[4]; decode(w + (r * bpr + blk) * type_size, v); |
| 54 | int io = blk * 32; |
| 55 | __m256 a = _mm256_mul_ps(v[0], _mm256_loadu_ps(x + io)); |
| 56 | a = _mm256_fmadd_ps(v[1], _mm256_loadu_ps(x + io + 8), a); |
| 57 | a = _mm256_fmadd_ps(v[2], _mm256_loadu_ps(x + io + 16), a); |
| 58 | a = _mm256_fmadd_ps(v[3], _mm256_loadu_ps(x + io + 24), a); |
| 59 | __m256 h = _mm256_hadd_ps(a, _mm256_permute2f128_ps(a, a, 1)); |
| 60 | h = _mm256_hadd_ps(h, h); h = _mm256_hadd_ps(h, h); |
| 61 | total += _mm256_cvtss_f32(h); |
| 62 | } |
| 63 | out[r - start] = total; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void q4_0_matmul(const uint8_t* w, const float* x, float* out, |
| 68 | int n_rows, int n_cols, int start, int end) { |
| 69 | q4_matmul_avx2(w, x, out, n_rows, n_cols, start, end, 18); |
| 70 | } |
| 71 | |
| 72 | void q4_1_matmul(const uint8_t* w, const float* x, float* out, |
| 73 | int n_rows, int n_cols, int start, int end) { |
| 74 | q4_matmul_avx2(w, x, out, n_rows, n_cols, start, end, 20); |
| 75 | } |