interplane.Get Omega
Read in your language

MojoLlama

Archived Mojo/MAX quantizer and serving research repository (read-only history)

Brought by drakestapleton

branch main Branches Tags History

MojoLlama / src / mojollama / kernels / legacy-c / fused_layer.c

104 lines · 4455 bytes Raw Blame History
1/* fused_layer.c — All per-layer Q4_0 matmuls in ONE OMP parallel region.
2 *
3 * Fuses 5 projections (Q, K, V, Gate, Up) that share the x_norm input
4 * into a single OMP parallel for. This eliminates 1 OMP fork/join per layer
5 * and maximizes cache reuse of the input activation.
6 *
7 * Plus: fused SiLU + gate*up (C SIMD) to avoid numpy overhead.
8 *
9 * Compile:
10 * gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC \
11 * -o fused_layer.so fused_layer.c -lm
12 */
13#include <stdint.h>
14#include <math.h>
15#include <immintrin.h>
16#include <omp.h>
17
18#define Q4_0_BS 18
19
20static inline float f16_to_f32(uint16_t h) { return _cvtsh_ss(h); }
21
22static 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 one row of Q4_0 matmul: W[row, :] · x[:] */
32static inline float q4_0_dot_row(const uint8_t *row_base, const float *x, int n_cols) {
33 int bpr = n_cols / 32;
34 __m256 sum0 = _mm256_setzero_ps(), sum1 = _mm256_setzero_ps();
35 for (int blk = 0; blk < bpr; blk++) {
36 const uint8_t *bp = row_base + (size_t)blk * Q4_0_BS;
37 float s = f16_to_f32((uint16_t)bp[0] | ((uint16_t)bp[1] << 8));
38 __m256 scale = _mm256_set1_ps(s);
39 __m128i nb = _mm_loadu_si128((const __m128i*)(bp + 2));
40 __m128i lo = _mm_and_si128(nb, _mm_set1_epi8(15));
41 __m128i hi = _mm_and_si128(_mm_srli_epi16(nb, 4), _mm_set1_epi8(15));
42 __m128i lo_s = _mm_sub_epi8(lo, _mm_set1_epi8(8));
43 __m128i hi_s = _mm_sub_epi8(hi, _mm_set1_epi8(8));
44 __m256 v0 = _mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_mm_cvtepi8_epi16(lo_s)));
45 __m256 v1 = _mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_mm_cvtepi8_epi16(_mm_shuffle_epi32(lo_s, 0x4e))));
46 __m256 v2 = _mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_mm_cvtepi8_epi16(hi_s)));
47 __m256 v3 = _mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_mm_cvtepi8_epi16(_mm_shuffle_epi32(hi_s, 0x4e))));
48 v0 = _mm256_mul_ps(v0, scale); v1 = _mm256_mul_ps(v1, scale);
49 v2 = _mm256_mul_ps(v2, scale); v3 = _mm256_mul_ps(v3, scale);
50 int off = blk * 32;
51 sum0 = _mm256_fmadd_ps(v0, _mm256_loadu_ps(x + off), sum0);
52 sum0 = _mm256_fmadd_ps(v1, _mm256_loadu_ps(x + off + 8), sum0);
53 sum1 = _mm256_fmadd_ps(v2, _mm256_loadu_ps(x + off + 16), sum1);
54 sum1 = _mm256_fmadd_ps(v3, _mm256_loadu_ps(x + off + 24), sum1);
55 }
56 return hsum_ps(_mm256_add_ps(sum0, sum1));
57}
58
59/* Fused Q/K/V/Gate/Up matmuls in ONE OMP parallel region.
60 * All 5 projections use the same input x (x_norm).
61 * max_rows = max(nr_q, nr_k, nr_v, nr_gate, nr_up)
62 * Threads iterate rows 0..max_rows-1; projections without that row are skipped.
63 */
64void fused_qkv_gate_up(const uint8_t *w_q, const uint8_t *w_k, const uint8_t *w_v,
65 const uint8_t *w_gate, const uint8_t *w_up,
66 const float *x,
67 float *out_q, float *out_k, float *out_v,
68 float *out_gate, float *out_up,
69 int nr_q, int nr_k, int nr_v,
70 int nr_gate, int nr_up,
71 int n_cols) {
72 // Determine max row count
73 int max_rows = nr_q;
74 if (nr_k > max_rows) max_rows = nr_k;
75 if (nr_v > max_rows) max_rows = nr_v;
76 if (nr_gate > max_rows) max_rows = nr_gate;
77 if (nr_up > max_rows) max_rows = nr_up;
78
79 int bpr = n_cols / 32;
80 size_t stride_q = (size_t)bpr * Q4_0_BS;
81 size_t stride_k = (size_t)bpr * Q4_0_BS;
82 size_t stride_v = (size_t)bpr * Q4_0_BS;
83 size_t stride_g = (size_t)bpr * Q4_0_BS;
84 size_t stride_u = (size_t)bpr * Q4_0_BS;
85
86 #pragma omp parallel for schedule(static)
87 for (int row = 0; row < max_rows; row++) {
88 if (row < nr_q) {
89 out_q[row] = q4_0_dot_row(w_q + (size_t)row * stride_q, x, n_cols);
90 }
91 if (row < nr_k) {
92 out_k[row] = q4_0_dot_row(w_k + (size_t)row * stride_k, x, n_cols);
93 }
94 if (row < nr_v) {
95 out_v[row] = q4_0_dot_row(w_v + (size_t)row * stride_v, x, n_cols);
96 }
97 if (row < nr_gate) {
98 out_gate[row] = q4_0_dot_row(w_gate + (size_t)row * stride_g, x, n_cols);
99 }
100 if (row < nr_up) {
101 out_up[row] = q4_0_dot_row(w_up + (size_t)row * stride_u, x, n_cols);
102 }
103 }
104}