MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / src / mojollama / kernels / legacy-c / q4_kernel_omp.c
| 1 | /* Q4_0/Q4_1 matmul — AVX2+FMA+OpenMP with 4-row register blocking. |
| 2 | * |
| 3 | * Optimizations over baseline: |
| 4 | * 1. Row-outer accumulation (write output once per row, not per block) |
| 5 | * 2. 4-row register blocking (4 rows share input vector loads) |
| 6 | * 3. Software prefetch on weight data |
| 7 | * 4. Aligned output buffers (padding to 64-byte cache lines) |
| 8 | * 5. OpenMP thread control via OMP_NUM_THREADS |
| 9 | * |
| 10 | * Compile: gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC -o q4_kernel_omp.so q4_kernel_omp.c |
| 11 | */ |
| 12 | #include <stdint.h> |
| 13 | #include <math.h> |
| 14 | #include <immintrin.h> |
| 15 | #include <omp.h> |
| 16 | #include <string.h> |
| 17 | |
| 18 | static inline float f16_to_f32(uint16_t h) { return _cvtsh_ss(h); } |
| 19 | |
| 20 | /* Decode one Q4_0 block (18 bytes) → 4 __m256 dequant values. |
| 21 | * Full SIMD pipeline: AND→SHIFT→UNPACK→SUB→WIDEN→CVT→MUL by scale. |
| 22 | * ~15 instructions to dequantize 32 values. |
| 23 | */ |
| 24 | static inline void decode_q4_0(const uint8_t* bp, __m256 v[4]) { |
| 25 | float scale = f16_to_f32((uint16_t)bp[0] | ((uint16_t)bp[1] << 8)); |
| 26 | __m256 s = _mm256_set1_ps(scale); |
| 27 | __m128i nb = _mm_loadu_si128((__m128i*)(bp + 2)); |
| 28 | __m128i lo = _mm_and_si128(nb, _mm_set1_epi8(15)); |
| 29 | __m128i hi = _mm_and_si128(_mm_srli_epi16(nb, 4), _mm_set1_epi8(15)); |
| 30 | __m128i i0 = _mm_sub_epi8(_mm_unpacklo_epi8(lo, hi), _mm_set1_epi8(8)); |
| 31 | __m128i i1 = _mm_sub_epi8(_mm_unpackhi_epi8(lo, hi), _mm_set1_epi8(8)); |
| 32 | __m128i e00 = _mm_cvtepi8_epi16(i0), e01 = _mm_cvtepi8_epi16(_mm_shuffle_epi32(i0, 0x4e)); |
| 33 | __m128i e10 = _mm_cvtepi8_epi16(i1), e11 = _mm_cvtepi8_epi16(_mm_shuffle_epi32(i1, 0x4e)); |
| 34 | v[0] = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i( |
| 35 | _mm_cvtepi16_epi32(_mm_shuffle_epi32(e00, 0x4e)), _mm_cvtepi16_epi32(e00))), s); |
| 36 | v[1] = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i( |
| 37 | _mm_cvtepi16_epi32(_mm_shuffle_epi32(e01, 0x4e)), _mm_cvtepi16_epi32(e01))), s); |
| 38 | v[2] = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i( |
| 39 | _mm_cvtepi16_epi32(_mm_shuffle_epi32(e10, 0x4e)), _mm_cvtepi16_epi32(e10))), s); |
| 40 | v[3] = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_set_m128i( |
| 41 | _mm_cvtepi16_epi32(_mm_shuffle_epi32(e11, 0x4e)), _mm_cvtepi16_epi32(e11))), s); |
| 42 | } |
| 43 | |
| 44 | /* Q4_1 variant: 20 bytes per block, dmin + dmax instead of just scale */ |
| 45 | static inline void decode_q4_1(const uint8_t* bp, __m256 v[4]) { |
| 46 | float dmin = f16_to_f32((uint16_t)bp[0] | ((uint16_t)bp[1] << 8)); |
| 47 | float dmax = f16_to_f32((uint16_t)bp[2] | ((uint16_t)bp[3] << 8)); |
| 48 | float diff = (dmax - dmin) / 15.0f; |
| 49 | __m256 dmin_v = _mm256_set1_ps(dmin), diff_v = _mm256_set1_ps(diff); |
| 50 | __m128i nb = _mm_loadu_si128((__m128i*)(bp + 4)); |
| 51 | __m128i lo = _mm_and_si128(nb, _mm_set1_epi8(15)); |
| 52 | __m128i hi = _mm_and_si128(_mm_srli_epi16(nb, 4), _mm_set1_epi8(15)); |
| 53 | __m128i i0 = _mm_unpacklo_epi8(lo, hi), i1 = _mm_unpackhi_epi8(lo, hi); |
| 54 | __m128i e00 = _mm_cvtepu8_epi16(i0), e01 = _mm_cvtepu8_epi16(_mm_shuffle_epi32(i0, 0x4e)); |
| 55 | __m128i e10 = _mm_cvtepu8_epi16(i1), e11 = _mm_cvtepu8_epi16(_mm_shuffle_epi32(i1, 0x4e)); |
| 56 | v[0] = _mm256_fmadd_ps(_mm256_cvtepi32_ps(_mm256_set_m128i( |
| 57 | _mm_cvtepi16_epi32(_mm_shuffle_epi32(e00,0x4e)),_mm_cvtepi16_epi32(e00))), diff_v, dmin_v); |
| 58 | v[1] = _mm256_fmadd_ps(_mm256_cvtepi32_ps(_mm256_set_m128i( |
| 59 | _mm_cvtepi16_epi32(_mm_shuffle_epi32(e01,0x4e)),_mm_cvtepi16_epi32(e01))), diff_v, dmin_v); |
| 60 | v[2] = _mm256_fmadd_ps(_mm256_cvtepi32_ps(_mm256_set_m128i( |
| 61 | _mm_cvtepi16_epi32(_mm_shuffle_epi32(e10,0x4e)),_mm_cvtepi16_epi32(e10))), diff_v, dmin_v); |
| 62 | v[3] = _mm256_fmadd_ps(_mm256_cvtepi32_ps(_mm256_set_m128i( |
| 63 | _mm_cvtepi16_epi32(_mm_shuffle_epi32(e11,0x4e)),_mm_cvtepi16_epi32(e11))), diff_v, dmin_v); |
| 64 | } |
| 65 | |
| 66 | /* Compute a single row-block dot product using FMA chain. |
| 67 | * 4 FMA instructions + 1 MUL + 1 HADD chain = 7 uops per block. |
| 68 | * This replaces the (v*scale*x).reduce_add() pattern which does 4 MULs + 4 REDUCE_ADDs. |
| 69 | */ |
| 70 | static inline float block_dot_fma(const __m256 v[4], const float* x_base) { |
| 71 | __m256 x0 = _mm256_loadu_ps(x_base); |
| 72 | __m256 x1 = _mm256_loadu_ps(x_base + 8); |
| 73 | __m256 x2 = _mm256_loadu_ps(x_base + 16); |
| 74 | __m256 x3 = _mm256_loadu_ps(x_base + 24); |
| 75 | __m256 a = _mm256_mul_ps(v[0], x0); |
| 76 | a = _mm256_fmadd_ps(v[1], x1, a); |
| 77 | a = _mm256_fmadd_ps(v[2], x2, a); |
| 78 | a = _mm256_fmadd_ps(v[3], x3, a); |
| 79 | __m256 h = _mm256_hadd_ps(a, _mm256_permute2f128_ps(a, a, 1)); |
| 80 | h = _mm256_hadd_ps(h, h); |
| 81 | h = _mm256_hadd_ps(h, h); |
| 82 | return _mm256_cvtss_f32(h); |
| 83 | } |
| 84 | |
| 85 | /* ── Single-row matmul: row-outer with register accumulation ────────── */ |
| 86 | |
| 87 | void q4_matmul_omp(const uint8_t* w, const float* x, float* out, |
| 88 | int n_rows, int n_cols, int type_size) { |
| 89 | int bpr = n_cols / 32; |
| 90 | void (*decode)(const uint8_t*, __m256*) = (type_size == 20) ? decode_q4_1 : decode_q4_0; |
| 91 | |
| 92 | #pragma omp parallel for schedule(static) |
| 93 | for (int r = 0; r < n_rows; r++) { |
| 94 | float total = 0.0f; |
| 95 | for (int blk = 0; blk < bpr; blk++) { |
| 96 | __m256 v[4]; |
| 97 | decode(w + (r * bpr + blk) * type_size, v); |
| 98 | total += block_dot_fma(v, x + blk * 32); |
| 99 | } |
| 100 | out[r] = total; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /* ── 4-row register-blocked matmul ─────────────────────────────────── |
| 105 | * Processes 4 rows at a time, sharing input vector loads across rows. |
| 106 | * Each row accumulates independently in a register → 1 write per row total. |
| 107 | */ |
| 108 | |
| 109 | void q4_matmul_omp_blocked(const uint8_t* w, const float* x, float* out, |
| 110 | int n_rows, int n_cols, int type_size) { |
| 111 | int bpr = n_cols / 32; |
| 112 | void (*decode)(const uint8_t*, __m256*) = (type_size == 20) ? decode_q4_1 : decode_q4_0; |
| 113 | memset(out, 0, n_rows * sizeof(float)); |
| 114 | |
| 115 | #pragma omp parallel for schedule(static) |
| 116 | for (int r = 0; r < n_rows; r += 4) { |
| 117 | int rows_left = (r + 4 <= n_rows) ? 4 : (n_rows - r); |
| 118 | float sum[4] = {0.0f, 0.0f, 0.0f, 0.0f}; |
| 119 | |
| 120 | for (int blk = 0; blk < bpr; blk++) { |
| 121 | float x_base[32]; |
| 122 | memcpy(x_base, x + blk * 32, 32 * sizeof(float)); |
| 123 | |
| 124 | for (int ri = 0; ri < rows_left; ri++) { |
| 125 | __m256 v[4]; |
| 126 | decode(w + ((r + ri) * bpr + blk) * type_size, v); |
| 127 | sum[ri] += block_dot_fma(v, x_base); |
| 128 | } |
| 129 | } |
| 130 | for (int ri = 0; ri < rows_left; ri++) { |
| 131 | out[r + ri] = sum[ri]; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /* ── Single-threaded row-outer AVX2+FMA baseline ──────────────────── */ |
| 137 | |
| 138 | void q4_matmul_avx2(const uint8_t* w, const float* x, float* out, |
| 139 | int n_rows, int n_cols, int start, int end, int type_size) { |
| 140 | int bpr = n_cols / 32; |
| 141 | int n_out = end - start; |
| 142 | void (*decode)(const uint8_t*, __m256*) = (type_size == 20) ? decode_q4_1 : decode_q4_0; |
| 143 | for (int i = 0; i < n_out; i++) out[i] = 0.0f; |
| 144 | |
| 145 | for (int blk = 0; blk < bpr; blk++) { |
| 146 | int io = blk * 32; |
| 147 | __m256 x0 = _mm256_loadu_ps(x + io); |
| 148 | __m256 x1 = _mm256_loadu_ps(x + io + 8); |
| 149 | __m256 x2 = _mm256_loadu_ps(x + io + 16); |
| 150 | __m256 x3 = _mm256_loadu_ps(x + io + 24); |
| 151 | for (int r = start; r < end; r++) { |
| 152 | __m256 v[4]; |
| 153 | decode(w + (r * bpr + blk) * type_size, v); |
| 154 | __m256 a = _mm256_mul_ps(v[0], x0); |
| 155 | a = _mm256_fmadd_ps(v[1], x1, a); |
| 156 | a = _mm256_fmadd_ps(v[2], x2, a); |
| 157 | a = _mm256_fmadd_ps(v[3], x3, a); |
| 158 | __m256 h = _mm256_hadd_ps(a, _mm256_permute2f128_ps(a, a, 1)); |
| 159 | h = _mm256_hadd_ps(h, h); h = _mm256_hadd_ps(h, h); |
| 160 | out[r - start] += _mm256_cvtss_f32(h); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /* ── Convenience wrappers ──────────────────────────────────────────── */ |
| 166 | |
| 167 | void q4_0_matmul(const uint8_t* w, const float* x, float* out, |
| 168 | int n_rows, int n_cols, int start, int end) { |
| 169 | q4_matmul_avx2(w, x, out, n_rows, n_cols, start, end, 18); |
| 170 | } |
| 171 | |
| 172 | void q4_1_matmul(const uint8_t* w, const float* x, float* out, |
| 173 | int n_rows, int n_cols, int start, int end) { |
| 174 | q4_matmul_avx2(w, x, out, n_rows, n_cols, start, end, 20); |
| 175 | } |
| 176 | |
| 177 | /* ── Batched: up to 7 projections in one OpenMP call ──────────────── */ |
| 178 | #define MAX_PROJ 7 |
| 179 | |
| 180 | static void process_row(const uint8_t* w[MAX_PROJ], const float* x, float* out[MAX_PROJ], |
| 181 | int nrows[MAX_PROJ], int ncols[MAX_PROJ], int ts[MAX_PROJ], |
| 182 | int n_proj, int row) { |
| 183 | int bpr = ncols[0] / 32; |
| 184 | for (int p = 0; p < n_proj; p++) { |
| 185 | float total = 0.0f; |
| 186 | int nc = ncols[p], nr = nrows[p]; |
| 187 | if (row >= nr) continue; |
| 188 | int bp = nc / 32; |
| 189 | void (*dec)(const uint8_t*, __m256*) = (ts[p] == 20) ? decode_q4_1 : decode_q4_0; |
| 190 | for (int blk = 0; blk < bp; blk++) { |
| 191 | const uint8_t* bp_w = w[p] + (row * bp + blk) * ts[p]; |
| 192 | int io = blk * 32; |
| 193 | __m256 x0 = _mm256_loadu_ps(x + io), x1 = _mm256_loadu_ps(x + io + 8); |
| 194 | __m256 x2 = _mm256_loadu_ps(x + io + 16), x3 = _mm256_loadu_ps(x + io + 24); |
| 195 | __m256 v[4]; dec(bp_w, v); |
| 196 | __m256 a = _mm256_mul_ps(v[0], x0); |
| 197 | a = _mm256_fmadd_ps(v[1], x1, a); a = _mm256_fmadd_ps(v[2], x2, a); |
| 198 | a = _mm256_fmadd_ps(v[3], x3, a); |
| 199 | __m256 h = _mm256_hadd_ps(a, _mm256_permute2f128_ps(a, a, 1)); |
| 200 | h = _mm256_hadd_ps(h, h); h = _mm256_hadd_ps(h, h); |
| 201 | total += _mm256_cvtss_f32(h); |
| 202 | } |
| 203 | out[p][row] = total; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | void batch_matmul(const uint8_t* w[MAX_PROJ], const float* x, float* out[MAX_PROJ], |
| 208 | int nrows[MAX_PROJ], int ncols[MAX_PROJ], int ts[MAX_PROJ], |
| 209 | int n_proj) { |
| 210 | int max_rows = 0; |
| 211 | for (int p = 0; p < n_proj; p++) { |
| 212 | memset(out[p], 0, nrows[p] * sizeof(float)); |
| 213 | if (nrows[p] > max_rows) max_rows = nrows[p]; |
| 214 | } |
| 215 | #pragma omp parallel for schedule(static) |
| 216 | for (int row = 0; row < max_rows; row++) { |
| 217 | process_row(w, x, out, nrows, ncols, ts, n_proj, row); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /* ── RMS normalization (AVX2 vectorized) ──────────────────────────── */ |
| 222 | |
| 223 | void q4_rms_norm(const float* x, const float* weight, float* out, int n) { |
| 224 | __m256 ss_vec = _mm256_setzero_ps(); |
| 225 | int i; |
| 226 | for (i = 0; i <= n - 8; i += 8) { |
| 227 | __m256 v = _mm256_loadu_ps(x + i); |
| 228 | ss_vec = _mm256_fmadd_ps(v, v, ss_vec); |
| 229 | } |
| 230 | /* Horizontal sum */ |
| 231 | __m256 h = _mm256_hadd_ps(ss_vec, _mm256_permute2f128_ps(ss_vec, ss_vec, 1)); |
| 232 | h = _mm256_hadd_ps(h, h); h = _mm256_hadd_ps(h, h); |
| 233 | float ss = _mm256_cvtss_f32(h); |
| 234 | /* remaining elements */ |
| 235 | for (; i < n; i++) ss += x[i] * x[i]; |
| 236 | float inv_rms = 1.0f / sqrtf(ss / n + 1e-6f); |
| 237 | __m256 inv_v = _mm256_set1_ps(inv_rms); |
| 238 | for (i = 0; i <= n - 8; i += 8) { |
| 239 | __m256 v = _mm256_loadu_ps(x + i); |
| 240 | __m256 w = _mm256_loadu_ps(weight + i); |
| 241 | _mm256_storeu_ps(out + i, _mm256_mul_ps(_mm256_mul_ps(v, inv_v), w)); |
| 242 | } |
| 243 | for (; i < n; i++) out[i] = x[i] * inv_rms * weight[i]; |
| 244 | } |
| 245 | |
| 246 | /* ── SiLU activation (AVX2 vectorized multiply, scalar exp) ─────── */ |
| 247 | |
| 248 | void q4_silu(float* x, int n) { |
| 249 | int i; |
| 250 | for (i = 0; i <= n - 8; i += 8) { |
| 251 | __m256 v = _mm256_loadu_ps(x + i); |
| 252 | /* Scalar exp fallback for each lane — AVX2 has no _mm256_exp_ps */ |
| 253 | float tmp[8]; |
| 254 | _mm256_storeu_ps(tmp, v); |
| 255 | for (int j = 0; j < 8; j++) tmp[j] = tmp[j] / (1.0f + expf(-tmp[j])); |
| 256 | _mm256_storeu_ps(x + i, _mm256_loadu_ps(tmp)); |
| 257 | } |
| 258 | for (; i < n; i++) { |
| 259 | float v = x[i]; |
| 260 | x[i] = v / (1.0f + expf(-v)); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | /* ── Softmax (AVX2 vectorized max/reduce, scalar exp) ─────────────── */ |
| 265 | |
| 266 | void q4_softmax(float* x, int n) { |
| 267 | /* Find max for numerical stability */ |
| 268 | __m256 max_vec = _mm256_set1_ps(-1e30f); |
| 269 | int i; |
| 270 | for (i = 0; i <= n - 8; i += 8) { |
| 271 | __m256 v = _mm256_loadu_ps(x + i); |
| 272 | max_vec = _mm256_max_ps(max_vec, v); |
| 273 | } |
| 274 | __m256 h = _mm256_hadd_ps(max_vec, _mm256_permute2f128_ps(max_vec, max_vec, 1)); |
| 275 | h = _mm256_hadd_ps(h, h); h = _mm256_hadd_ps(h, h); |
| 276 | float max_val = _mm256_cvtss_f32(h); |
| 277 | for (; i < n; i++) if (x[i] > max_val) max_val = x[i]; |
| 278 | |
| 279 | /* Scalar exp(x - max) — AVX2 has no vector exp */ |
| 280 | float sum = 0.0f; |
| 281 | for (i = 0; i < n; i++) { |
| 282 | x[i] = expf(x[i] - max_val); |
| 283 | sum += x[i]; |
| 284 | } |
| 285 | float inv_sum = 1.0f / sum; |
| 286 | __m256 inv_v = _mm256_set1_ps(inv_sum); |
| 287 | for (i = 0; i <= n - 8; i += 8) { |
| 288 | __m256 v = _mm256_loadu_ps(x + i); |
| 289 | _mm256_storeu_ps(x + i, _mm256_mul_ps(v, inv_v)); |
| 290 | } |
| 291 | for (; i < n; i++) x[i] *= inv_sum; |
| 292 | } |
| 293 | |
| 294 | /* ── Thread-get/set for dynamic thread control ─────────────────────── */ |
| 295 | |
| 296 | int q4_get_max_threads(void) { return omp_get_max_threads(); } |
| 297 | void q4_set_num_threads(int n) { omp_set_num_threads(n); } |