Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/MojoLlama/blob/main/src/mojollama/kernels/legacy-c/cengine3.c # MojoLlama Archived Mojo/MAX quantizer and serving research repository (read-only history) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `MojoLlama / src / mojollama / kernels / legacy-c / cengine3.c` 155 lines · 7299 bytes Raw Blame History 1 /* cengine3.c — Pure C forward pass using proven quant_matmul_omp. 2 * Calls quant_kernels_omp.so for ALL matmuls — bit-identical to v7.7. 3 * Inline rms_norm and gqa_attention (also proven). 4 * 5 * Compile: 6 * gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC \ 7 * -o cengine3.so cengine3.c \ 8 * -L. -l:quant_kernels_omp.so \ 9 * -Wl,-rpath,/onedev-workspace/work/src/mojollama/kernels -lm 10 */ 11 #include 12 #include 13 #include 14 #include 15 #include 16 17 /* External: proven OMP kernels */ 18 extern void quant_matmul_omp(const unsigned char*, const float*, float*, int, int, int); 19 extern void batch_qkv_omp(const unsigned char*, const unsigned char*, const unsigned char*, 20 const float*, float*, float*, float*, 21 int, int, int, int, int, int, int); 22 extern void batch_gate_up_omp(const unsigned char*, const unsigned char*, 23 const float*, float*, float*, 24 int, int, int, int, int); 25 26 /* ═══ Inline RMS norm (AVX2, matches quant_kernels_omp.c) ══════════ */ 27 static inline void rms_norm_f32(float *out, const float *x, const float *w, int n, float eps) { 28 __m256 ss = _mm256_setzero_ps(); int i; 29 for (i = 0; i <= n - 8; i += 8) { __m256 v = _mm256_loadu_ps(x + i); ss = _mm256_fmadd_ps(v, v, ss); } 30 __m256 h = _mm256_hadd_ps(ss, _mm256_permute2f128_ps(ss, ss, 1)); 31 h = _mm256_hadd_ps(h, h); h = _mm256_hadd_ps(h, h); 32 float sse = _mm256_cvtss_f32(h); for (; i < n; i++) sse += x[i] * x[i]; 33 float inv = 1.0f / sqrtf(sse / n + eps); __m256 iv = _mm256_set1_ps(inv); 34 for (i = 0; i <= n - 8; i += 8) { 35 __m256 xv = _mm256_loadu_ps(x + i), wv = _mm256_loadu_ps(w + i); 36 _mm256_storeu_ps(out + i, _mm256_mul_ps(_mm256_mul_ps(xv, iv), wv)); 37 } 38 for (; i < n; i++) out[i] = x[i] * inv * w[i]; 39 } 40 41 /* ═══ Inline GQA attention (AVX2, matches gqa_attention.c) ═════════ */ 42 static inline float hsum_ps(__m256 v) { 43 __m128 l = _mm256_castps256_ps128(v), h = _mm256_extractf128_ps(v, 1); 44 l = _mm_add_ps(l, h); l = _mm_hadd_ps(l, l); l = _mm_hadd_ps(l, l); 45 return _mm_cvtss_f32(l); 46 } 47 static void gqa_attn(float *out, const float *q, const float *kc, const float *vc, 48 int sl, int nh, int nkh, int hd) { 49 int gr = nh / nkh; 50 #pragma omp parallel for schedule(static) 51 for (int h = 0; h < nh; h++) { 52 int kv = h / gr; const float *qh = q + h * hd, *kb = kc + kv * hd, *vb = vc + kv * hd; 53 int ks = nkh * hd; float *oh = out + h * hd, sc[4096], mx = -1e30f, inv = 1.0f / sqrtf(hd); 54 for (int s = 0; s < sl; s++) { 55 const float *ks_ = kb + s * ks; __m256 su = _mm256_setzero_ps(); int d; 56 for (d = 0; d <= hd - 8; d += 8) su = _mm256_fmadd_ps(_mm256_loadu_ps(qh + d), _mm256_loadu_ps(ks_ + d), su); 57 float dot = hsum_ps(su); for (; d < hd; d++) dot += qh[d] * ks_[d]; 58 sc[s] = dot * inv; if (sc[s] > mx) mx = sc[s]; 59 } 60 float se = 0; for (int s = 0; s < sl; s++) { sc[s] = expf(sc[s] - mx); se += sc[s]; } 61 float is = 1.0f / se; memset(oh, 0, hd * sizeof(float)); 62 for (int s = 0; s < sl; s++) { 63 const float *vs_ = vb + s * ks; float w = sc[s] * is; __m256 wv = _mm256_set1_ps(w); int d; 64 for (d = 0; d <= hd - 8; d += 8) { 65 __m256 vv = _mm256_loadu_ps(vs_ + d); 66 __m256 ac = _mm256_loadu_ps(oh + d); 67 _mm256_storeu_ps(oh + d, _mm256_fmadd_ps(wv, vv, ac)); 68 } 69 for (; d < hd; d++) oh[d] += w * vs_[d]; 70 } 71 } 72 } 73 74 /* ═══ Engine config struct ═════════════════════════════════════════ */ 75 typedef struct { 76 int L, N, NH, NKH, HD, FF, V; float eps; 77 /* Per-layer weight pointers [L] */ 78 const unsigned char **wQ, **wK, **wV, **wO, **wG, **wU, **wD; 79 const float **wAN, **wFN; 80 const int *nQ, *nK, *nV, *nO, *nG, *nU, *nD; 81 int nc; /* n_cols for QKV/GateUp/O (always n_embd) */ 82 const float *emb, *onw; 83 const unsigned char *wOut; int outNR, outNC; 84 float *kvK, *kvV; int *kvL; int MP; 85 float *x, *xn, *res, *q, *k, *v, *att, *gate, *up, *silu, *oproj, *ffn; 86 } EngineConfig3; 87 88 /* ═══ engine_forward — pure C, no Python, uses proven quant_matmul_omp ═══ */ 89 void engine_forward(int tok, const EngineConfig3 *c, float *logits) { 90 int N=c->N, NH=c->NH, NKH=c->NKH, HD=c->HD, FF=c->FF, L=c->L, nc=c->nc; 91 float *x=c->x, *xn=c->xn, *res=c->res, *q=c->q, *k=c->k, *v=c->v; 92 float *att=c->att, *gate=c->gate, *up=c->up, *silu=c->silu, *o=c->oproj, *ffn=c->ffn; 93 94 memcpy(x, c->emb + (size_t)tok * N, N * sizeof(float)); 95 96 for (int l = 0; l < L; l++) { 97 memcpy(res, x, N * sizeof(float)); 98 rms_norm_f32(xn, x, c->wAN[l], N, c->eps); 99 100 /* Q, K, V — batched OMP (single fork/join for all 3) */ 101 batch_qkv_omp(c->wQ[l], c->wK[l], c->wV[l], xn, q, k, v, 102 c->nQ[l], c->nK[l], c->nV[l], nc, 2, 2, 2); 103 104 /* RoPE — on-the-fly */ 105 int pos = c->kvL[l]; 106 for (int hh = 0; hh < NH; hh++) { float *qh = q + hh * HD; 107 for (int j = 0; j < HD/2; j++) { 108 double ang = (double)pos / pow(10000.0, 2.0*j/HD); 109 double cs = cos(ang), sn = sin(ang); 110 float a = qh[j], b = qh[j+HD/2]; 111 qh[j] = (float)(a*cs - b*sn); qh[j+HD/2] = (float)(b*cs + a*sn); 112 } 113 } 114 for (int hh = 0; hh < NKH; hh++) { float *kh = k + hh * HD; 115 for (int j = 0; j < HD/2; j++) { 116 double ang = (double)pos / pow(10000.0, 2.0*j/HD); 117 double cs = cos(ang), sn = sin(ang); 118 float a = kh[j], b = kh[j+HD/2]; 119 kh[j] = (float)(a*cs - b*sn); kh[j+HD/2] = (float)(b*cs + a*sn); 120 } 121 } 122 123 /* KV store */ 124 memcpy(c->kvK + ((size_t)l*c->MP + pos) * NKH * HD, k, NKH * HD * sizeof(float)); 125 memcpy(c->kvV + ((size_t)l*c->MP + pos) * NKH * HD, v, NKH * HD * sizeof(float)); 126 127 /* GQA attention */ 128 gqa_attn(att, q, c->kvK + (size_t)l*c->MP*NKH*HD, c->kvV + (size_t)l*c->MP*NKH*HD, 129 pos+1, NH, NKH, HD); 130 c->kvL[l]++; 131 132 /* O projection — proven quant_matmul_omp, n_cols = nc */ 133 quant_matmul_omp(c->wO[l], att, o, c->nO[l], nc, 2); 134 for (int i = 0; i < N; i++) x[i] = res[i] + o[i]; 135 136 /* FFN */ 137 memcpy(res, x, N * sizeof(float)); 138 rms_norm_f32(xn, x, c->wFN[l], N, c->eps); 139 140 /* Gate + Up — batched OMP */ 141 batch_gate_up_omp(c->wG[l], c->wU[l], xn, gate, up, 142 c->nG[l], c->nU[l], nc, 2, 2); 143 144 /* SiLU(gate) * up */ 145 for (int i = 0; i < FF; i++) { float g = gate[i]; silu[i] = (g / (1.0f + expf(-g))) * up[i]; } 146 147 /* Down — n_cols = FF (n_ff), NOT nc (n_embd)! */ 148 quant_matmul_omp(c->wD[l], silu, ffn, c->nD[l], FF, 2); 149 for (int i = 0; i < N; i++) x[i] = res[i] + ffn[i]; 150 } 151 152 /* Final norm + output */ 153 rms_norm_f32(xn, x, c->onw, N, c->eps); 154 quant_matmul_omp(c->wOut, xn, logits, c->outNR, c->outNC, 8); 155 }