MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / src / mojollama / kernels / legacy-c / q4_kernel_batch.c
| 1 | /* Batched Q4_X matmul — 7 projections in one OpenMP call. |
| 2 | * Compile: gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC -o q4_kernel_batch.so q4_kernel_batch.c |
| 3 | */ |
| 4 | #include <stdint.h> |
| 5 | #include <immintrin.h> |
| 6 | #include <omp.h> |
| 7 | #include <string.h> |
| 8 | |
| 9 | #define MAX_PROJ 7 |
| 10 | |
| 11 | static inline float f16_to_f32(uint16_t h) { return _cvtsh_ss(h); } |
| 12 | |
| 13 | static void decode_q4_0(const uint8_t* bp, __m256 v[4]) { |
| 14 | float scale = f16_to_f32(bp[0] | ((uint16_t)bp[1] << 8)); |
| 15 | __m256 s = _mm256_set1_ps(scale); |
| 16 | __m128i nb = _mm_loadu_si128((__m128i*)(bp + 2)); |
| 17 | __m128i lo = _mm_and_si128(nb, _mm_set1_epi8(15)); |
| 18 | __m128i hi = _mm_and_si128(_mm_srli_epi16(nb, 4), _mm_set1_epi8(15)); |
| 19 | __m128i i0 = _mm_sub_epi8(_mm_unpacklo_epi8(lo, hi), _mm_set1_epi8(8)); |
| 20 | __m128i i1 = _mm_sub_epi8(_mm_unpackhi_epi8(lo, hi), _mm_set1_epi8(8)); |
| 21 | __m128i e00 = _mm_cvtepi8_epi16(i0), e01 = _mm_cvtepi8_epi16(_mm_shuffle_epi32(i0, 0x4e)); |
| 22 | __m128i e10 = _mm_cvtepi8_epi16(i1), e11 = _mm_cvtepi8_epi16(_mm_shuffle_epi32(i1, 0x4e)); |
| 23 | v[0] = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e00,0x4e)),_mm_cvtepi16_epi32(e00))),s); |
| 24 | v[1] = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e01,0x4e)),_mm_cvtepi16_epi32(e01))),s); |
| 25 | v[2] = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e10,0x4e)),_mm_cvtepi16_epi32(e10))),s); |
| 26 | v[3] = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e11,0x4e)),_mm_cvtepi16_epi32(e11))),s); |
| 27 | } |
| 28 | |
| 29 | static void decode_q4_1(const uint8_t* bp, __m256 v[4]) { |
| 30 | float dmin = f16_to_f32(bp[0] | ((uint16_t)bp[1] << 8)); |
| 31 | float dmax = f16_to_f32(bp[2] | ((uint16_t)bp[3] << 8)); |
| 32 | float diff = (dmax - dmin) / 15.0f; |
| 33 | __m256 dm = _mm256_set1_ps(dmin), df = _mm256_set1_ps(diff); |
| 34 | __m128i nb = _mm_loadu_si128((__m128i*)(bp + 4)); |
| 35 | __m128i lo = _mm_and_si128(nb, _mm_set1_epi8(15)); |
| 36 | __m128i hi = _mm_and_si128(_mm_srli_epi16(nb, 4), _mm_set1_epi8(15)); |
| 37 | __m128i i0 = _mm_unpacklo_epi8(lo, hi), i1 = _mm_unpackhi_epi8(lo, hi); |
| 38 | __m128i e00 = _mm_cvtepu8_epi16(i0), e01 = _mm_cvtepu8_epi16(_mm_shuffle_epi32(i0,0x4e)); |
| 39 | __m128i e10 = _mm_cvtepu8_epi16(i1), e11 = _mm_cvtepu8_epi16(_mm_shuffle_epi32(i1,0x4e)); |
| 40 | v[0] = _mm256_fmadd_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e00,0x4e)),_mm_cvtepi16_epi32(e00))),df,dm); |
| 41 | v[1] = _mm256_fmadd_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e01,0x4e)),_mm_cvtepi16_epi32(e01))),df,dm); |
| 42 | v[2] = _mm256_fmadd_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e10,0x4e)),_mm_cvtepi16_epi32(e10))),df,dm); |
| 43 | v[3] = _mm256_fmadd_ps(_mm256_cvtepi32_ps(_mm256_set_m128i(_mm_cvtepi16_epi32(_mm_shuffle_epi32(e11,0x4e)),_mm_cvtepi16_epi32(e11))),df,dm); |
| 44 | } |
| 45 | |
| 46 | /* Process one row: compute all projections that have data for this row */ |
| 47 | static void process_row(const uint8_t* w[MAX_PROJ], const float* x, float* out[MAX_PROJ], |
| 48 | int nrows[MAX_PROJ], int ncols[MAX_PROJ], int ts[MAX_PROJ], |
| 49 | int n_proj, int row) { |
| 50 | int bpr = ncols[0] / 32; // all projections share same n_cols pattern |
| 51 | for (int p = 0; p < n_proj; p++) { |
| 52 | float total = 0.0f; |
| 53 | int nc = ncols[p], nr = nrows[p]; |
| 54 | if (row >= nr) continue; |
| 55 | int bp = nc / 32; |
| 56 | void (*dec)(const uint8_t*, __m256*) = (ts[p] == 20) ? decode_q4_1 : decode_q4_0; |
| 57 | for (int blk = 0; blk < bp; blk++) { |
| 58 | const uint8_t* bp_w = w[p] + (row * bp + blk) * ts[p]; |
| 59 | int io = blk * 32; |
| 60 | __m256 x0 = _mm256_loadu_ps(x + io), x1 = _mm256_loadu_ps(x + io + 8); |
| 61 | __m256 x2 = _mm256_loadu_ps(x + io + 16), x3 = _mm256_loadu_ps(x + io + 24); |
| 62 | __m256 v[4]; dec(bp_w, v); |
| 63 | __m256 a = _mm256_mul_ps(v[0], x0); |
| 64 | a = _mm256_fmadd_ps(v[1], x1, a); a = _mm256_fmadd_ps(v[2], x2, a); |
| 65 | a = _mm256_fmadd_ps(v[3], x3, a); |
| 66 | __m256 h = _mm256_hadd_ps(a, _mm256_permute2f128_ps(a, a, 1)); |
| 67 | h = _mm256_hadd_ps(h, h); h = _mm256_hadd_ps(h, h); |
| 68 | total += _mm256_cvtss_f32(h); |
| 69 | } |
| 70 | out[p][row] = total; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /* Batched: 7 projections in one call. OpenMP parallelizes over rows. |
| 75 | * w[p], out[p], nrows[p], ncols[p], ts[p] for each projection. |
| 76 | */ |
| 77 | void batch_matmul(const uint8_t* w[MAX_PROJ], const float* x, float* out[MAX_PROJ], |
| 78 | int nrows[MAX_PROJ], int ncols[MAX_PROJ], int ts[MAX_PROJ], |
| 79 | int n_proj) { |
| 80 | int max_rows = 0; |
| 81 | for (int p = 0; p < n_proj; p++) { |
| 82 | memset(out[p], 0, nrows[p] * sizeof(float)); |
| 83 | if (nrows[p] > max_rows) max_rows = nrows[p]; |
| 84 | } |
| 85 | #pragma omp parallel for schedule(static) |
| 86 | for (int row = 0; row < max_rows; row++) { |
| 87 | process_row(w, x, out, nrows, ncols, ts, n_proj, row); |
| 88 | } |
| 89 | } |