MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / src / mojollama / kernels / legacy-c / cengine_batch_v2.c
| 1 | /* cengine_batch.c — Unified batched inference engine with K-quant + MoE support. |
| 2 | * Supports: Q4_0, Q8_0, Q4_K, Q6_K quant formats, dense and MoE architectures. |
| 3 | */ |
| 4 | #include <stdint.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | #include <math.h> |
| 8 | #include <immintrin.h> |
| 9 | #include <omp.h> |
| 10 | #define Q4_0_BS 18 |
| 11 | #define Q8_0_BS 34 |
| 12 | #define BLOCK_SIZE 64 |
| 13 | #define MAX_BLOCKS 1024 |
| 14 | #define QK_K 256 |
| 15 | |
| 16 | /* K-quant block structures (from llama.cpp) */ |
| 17 | typedef struct { uint16_t d; uint16_t dm; uint8_t scales[12]; uint8_t qs[128]; } block_q4_K; |
| 18 | typedef struct { uint8_t ql[128]; uint8_t qh[64]; int8_t scales[16]; uint16_t d; } block_q6_K; |
| 19 | |
| 20 | static inline float gf16(uint16_t h) { return _cvtsh_ss(h); } |
| 21 | static inline float hsum_ps(__m256 v) { |
| 22 | __m128 l=_mm256_castps256_ps128(v),h=_mm256_extractf128_ps(v,1); |
| 23 | l=_mm_add_ps(l,h);l=_mm_hadd_ps(l,l);l=_mm_hadd_ps(l,l);return _mm_cvtss_f32(l); |
| 24 | } |
| 25 | |
| 26 | /* Q4_K scale extraction (matches llama.cpp get_scale_min_k4) */ |
| 27 | static inline void k4_scale(int j, const uint8_t *q, uint8_t *d, uint8_t *m) { |
| 28 | if (j < 4) { *d = q[j] & 63; *m = q[j+4] & 63; } |
| 29 | else { *d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4); |
| 30 | *m = (q[j+4] >> 4) | ((q[j] >> 6) << 4); } |
| 31 | } |
| 32 | |
| 33 | /* ── AVX2 Batch Matmuls ──────────────────────── */ |
| 34 | void q4_0_batch_matmul(const uint8_t *W, const float *x, float *out, int n_rows, int nc, int B); |
| 35 | void q8_0_batch_matmul(const uint8_t *W, const float *x, float *out, int n_rows, int nc, int B); |
| 36 | void q4_k_batch_matmul(const uint8_t *W, const float *x, float *out, int n_rows, int nc, int B); |
| 37 | void q6_k_batch_matmul(const uint8_t *W, const float *x, float *out, int n_rows, int nc, int B); |
| 38 | |
| 39 | /* Dispatch batch matmul by quant type */ |
| 40 | /* Simple FP32 batch matmul for dequantized weights */ |
| 41 | void f32_batch_matmul(const float *W, const float *x, float *out, |
| 42 | int n_rows, int nc, int B) { |
| 43 | #pragma omp parallel for schedule(static, 8) |
| 44 | for (int r = 0; r < n_rows; r++) { |
| 45 | for (int b = 0; b < B; b++) { |
| 46 | float dot = 0; |
| 47 | for (int i = 0; i < nc; i++) |
| 48 | dot += W[(size_t)r * nc + i] * x[(size_t)b * nc + i]; |
| 49 | out[(size_t)b * n_rows + r] = dot; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | static inline void batch_matmul(int qt, const uint8_t *W, const float *x, float *out, |
| 55 | int n_rows, int nc, int B) { |
| 56 | if (qt == 0) f32_batch_matmul((const float*)W, x, out, n_rows, nc, B); |
| 57 | else if (qt == 14) q6_k_batch_matmul(W, x, out, n_rows, nc, B); |
| 58 | else if (qt == 12) q4_k_batch_matmul(W, x, out, n_rows, nc, B); |
| 59 | else if (qt == 8) q8_0_batch_matmul(W, x, out, n_rows, nc, B); |
| 60 | else q4_0_batch_matmul(W, x, out, n_rows, nc, B); |
| 61 | } |