MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / src / mojollama / kernels / legacy-c / avx2_batch_kernels.c
| 1 | /* avx2_batch_kernels.c — Q4_0 batch kernels with inline AVX2 dequant+FMA. |
| 2 | * |
| 3 | * Replaces quant_kernels_omp.c's batch_qkv_omp and batch_gate_up_omp |
| 4 | * with Q4_0-only versions that inline the AVX2 dequant+FMA loop |
| 5 | * (no function pointers, no switch dispatch, no per-row function call). |
| 6 | * |
| 7 | * Expected: 1.3-1.5x faster on Q4_0 batch matmuls = ~60% of matmul time |
| 8 | * → overall ~10-15% engine speedup → 82 → 90+ tok/s |
| 9 | * |
| 10 | * Compile: |
| 11 | * gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC \ |
| 12 | * -o avx2_batch_kernels.so avx2_batch_kernels.c -lm |
| 13 | */ |
| 14 | #include <stdint.h> |
| 15 | #include <immintrin.h> |
| 16 | #include <omp.h> |
| 17 | |
| 18 | #define Q4_0_BS 18 |
| 19 | |
| 20 | static inline float f16_to_f32(uint16_t h) { return _cvtsh_ss(h); } |
| 21 | |
| 22 | static inline float hsum_ps(__m256 v) { |
| 23 | __m128 lo = _mm256_castps256_ps128(v); |
| 24 | __m128 hi = _mm256_extractf128_ps(v, 1); |
| 25 | lo = _mm_add_ps(lo, hi); |
| 26 | lo = _mm_hadd_ps(lo, lo); |
| 27 | lo = _mm_hadd_ps(lo, lo); |
| 28 | return _mm_cvtss_f32(lo); |
| 29 | } |
| 30 | |
| 31 | /* Compute Q4_0 dot product for row r of weight matrix W against input x. |
| 32 | * Inline AVX2 dequant+FMA — no function calls, no switch. |
| 33 | */ |
| 34 | static inline float q4_0_dot_inline(const uint8_t *W, const float *x, |
| 35 | int n_cols, int row) { |
| 36 | int bpr = n_cols / 32; |
| 37 | __m256 sum0 = _mm256_setzero_ps(); |
| 38 | __m256 sum1 = _mm256_setzero_ps(); |
| 39 | for (int blk = 0; blk < bpr; blk++) { |
| 40 | const uint8_t *bp = W + ((size_t)row * bpr + blk) * Q4_0_BS; |
| 41 | float s = f16_to_f32((uint16_t)bp[0] | ((uint16_t)bp[1] << 8)); |
| 42 | __m256 scale = _mm256_set1_ps(s); |
| 43 | __m128i nb = _mm_loadu_si128((const __m128i*)(bp + 2)); |
| 44 | __m128i lo = _mm_and_si128(nb, _mm_set1_epi8(15)); |
| 45 | __m128i hi = _mm_and_si128(_mm_srli_epi16(nb, 4), _mm_set1_epi8(15)); |
| 46 | __m128i lo_s = _mm_sub_epi8(lo, _mm_set1_epi8(8)); |
| 47 | __m128i hi_s = _mm_sub_epi8(hi, _mm_set1_epi8(8)); |
| 48 | // Dequantize 32 values into 4 x __m256 |
| 49 | __m256 v0 = _mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_mm_cvtepi8_epi16(lo_s))); |
| 50 | __m256 v1 = _mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_mm_cvtepi8_epi16(_mm_shuffle_epi32(lo_s, 0x4e)))); |
| 51 | __m256 v2 = _mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_mm_cvtepi8_epi16(hi_s))); |
| 52 | __m256 v3 = _mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_mm_cvtepi8_epi16(_mm_shuffle_epi32(hi_s, 0x4e)))); |
| 53 | // Scale |
| 54 | v0 = _mm256_mul_ps(v0, scale); v1 = _mm256_mul_ps(v1, scale); |
| 55 | v2 = _mm256_mul_ps(v2, scale); v3 = _mm256_mul_ps(v3, scale); |
| 56 | // FMA dot with input |
| 57 | int off = blk * 32; |
| 58 | sum0 = _mm256_fmadd_ps(v0, _mm256_loadu_ps(x + off), sum0); |
| 59 | sum0 = _mm256_fmadd_ps(v1, _mm256_loadu_ps(x + off + 8), sum0); |
| 60 | sum1 = _mm256_fmadd_ps(v2, _mm256_loadu_ps(x + off + 16), sum1); |
| 61 | sum1 = _mm256_fmadd_ps(v3, _mm256_loadu_ps(x + off + 24), sum1); |
| 62 | } |
| 63 | return hsum_ps(_mm256_add_ps(sum0, sum1)); |
| 64 | } |
| 65 | |
| 66 | /* Fused Q+K+V batch matmul — Q4_0 only, inline AVX2 dequant. |
| 67 | * |
| 68 | * Processes all rows of Q, K, V projections in a single OMP parallel region. |
| 69 | * Uses row-level dispatch based on accumulated row offsets. |
| 70 | */ |
| 71 | void batch_qkv_q4_0(const uint8_t *Wq, const uint8_t *Wk, const uint8_t *Wv, |
| 72 | const float *x, |
| 73 | float *out_q, float *out_k, float *out_v, |
| 74 | int nq, int nk, int nv, int n_cols) { |
| 75 | int total = nq + nk + nv; |
| 76 | #pragma omp parallel for schedule(static) |
| 77 | for (int i = 0; i < total; i++) { |
| 78 | if (i < nq) { |
| 79 | out_q[i] = q4_0_dot_inline(Wq, x, n_cols, i); |
| 80 | } else if (i < nq + nk) { |
| 81 | int r = i - nq; |
| 82 | out_k[r] = q4_0_dot_inline(Wk, x, n_cols, r); |
| 83 | } else { |
| 84 | int r = i - nq - nk; |
| 85 | out_v[r] = q4_0_dot_inline(Wv, x, n_cols, r); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /* Fused Gate+Up batch matmul — Q4_0 only, inline AVX2 dequant. */ |
| 91 | void batch_gate_up_q4_0(const uint8_t *Wg, const uint8_t *Wu, |
| 92 | const float *x, |
| 93 | float *out_gate, float *out_up, |
| 94 | int ng, int nu, int n_cols) { |
| 95 | int total = ng + nu; |
| 96 | #pragma omp parallel for schedule(static) |
| 97 | for (int i = 0; i < total; i++) { |
| 98 | if (i < ng) { |
| 99 | out_gate[i] = q4_0_dot_inline(Wg, x, n_cols, i); |
| 100 | } else { |
| 101 | int r = i - ng; |
| 102 | out_up[r] = q4_0_dot_inline(Wu, x, n_cols, r); |
| 103 | } |
| 104 | } |
| 105 | } |