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 / cengine2.c

194 lines · 8818 bytes Raw Blame History
1/* cengine2.c — Clean C forward pass. No struct, flat arrays.
2 *
3 * engine_forward(tok, ...all weight pointers and dims as flat arrays..., logits)
4 *
5 * Uses only the call stack and explicit parameters — no struct layout worries.
6 *
7 * Compile:
8 * gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC \
9 * -o cengine2.so cengine2.c -lm
10 */
11#include <stdint.h>
12#include <string.h>
13#include <math.h>
14#include <immintrin.h>
15#include <omp.h>
16
17#define Q4_0_BS 18
18#define Q8_0_BS 34
19
20static inline float f16_to_f32(uint16_t h) { return _cvtsh_ss(h); }
21static 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_0 dot — inline AVX2 */
27static inline float q4d(const uint8_t *W, const float *x, int nc, int r) {
28 int bp=nc/32; float total=0.0f;
29 for(int b=0;b<bp;b++){
30 const uint8_t *p=W+((size_t)r*bp+b)*Q4_0_BS;
31 __builtin_prefetch(p + Q4_0_BS, 0, 0); /* prefetch next block */
32 float sc=f16_to_f32((uint16_t)p[0]|((uint16_t)p[1]<<8));
33 __m256 sv=_mm256_set1_ps(sc);
34 __m128i nb=_mm_loadu_si128((const __m128i*)(p+2));
35 __m128i lo=_mm_and_si128(nb,_mm_set1_epi8(15));
36 __m128i hi=_mm_and_si128(_mm_srli_epi16(_mm_and_si128(nb,_mm_set1_epi8((char)0xF0)),4),_mm_set1_epi8(15));
37 __m128i ls=_mm_sub_epi8(lo,_mm_set1_epi8(8)),hs=_mm_sub_epi8(hi,_mm_set1_epi8(8));
38 __m256 v0=_mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_mm_cvtepi8_epi16(ls)));
39 __m256 v1=_mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_mm_cvtepi8_epi16(_mm_shuffle_epi32(ls,0x4e))));
40 __m256 v2=_mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_mm_cvtepi8_epi16(hs)));
41 __m256 v3=_mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_mm_cvtepi8_epi16(_mm_shuffle_epi32(hs,0x4e))));
42 v0=_mm256_mul_ps(v0,sv);v1=_mm256_mul_ps(v1,sv);v2=_mm256_mul_ps(v2,sv);v3=_mm256_mul_ps(v3,sv);
43 int o=b*32; __m256 acc=_mm256_mul_ps(v0,_mm256_loadu_ps(x+o));
44 acc=_mm256_fmadd_ps(v1,_mm256_loadu_ps(x+o+8),acc);
45 acc=_mm256_fmadd_ps(v2,_mm256_loadu_ps(x+o+16),acc);
46 acc=_mm256_fmadd_ps(v3,_mm256_loadu_ps(x+o+24),acc);
47 __m128 lo128=_mm256_castps256_ps128(acc),hi128=_mm256_extractf128_ps(acc,1);
48 lo128=_mm_add_ps(lo128,hi128);lo128=_mm_hadd_ps(lo128,lo128);lo128=_mm_hadd_ps(lo128,lo128);
49 total+=_mm_cvtss_f32(lo128);
50 }
51 return total;
52}
53
54/* Q8_0 dot */
55static inline float q8d(const uint8_t *W, const float *x, int nc, int r) {
56 int bp=nc/32; float t=0;
57 for(int b=0;b<bp;b++){
58 const uint8_t *p=W+((size_t)r*bp+b)*Q8_0_BS;
59 float d=f16_to_f32((uint16_t)p[0]|((uint16_t)p[1]<<8));
60 const int8_t *qs=(const int8_t*)(p+2);
61 __m256 dv=_mm256_set1_ps(d),bs=_mm256_setzero_ps(); int o=b*32;
62 for(int i=0;i<32;i+=8){
63 __m128i q8=_mm_loadl_epi64((const __m128i*)(qs+i));
64 __m256 v=_mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_mm_cvtepi8_epi16(q8))),dv);
65 bs=_mm256_fmadd_ps(v,_mm256_loadu_ps(x+o+i),bs);}
66 t+=hsum_ps(bs);
67 }
68 return t;
69}
70
71/* RMS norm */
72static inline void rms(float *o, const float *x, const float *w, int n, float e){
73 __m256 ss=_mm256_setzero_ps(); int i;
74 for(i=0;i<=n-8;i+=8){__m256 v=_mm256_loadu_ps(x+i);ss=_mm256_fmadd_ps(v,v,ss);}
75 __m256 hh=_mm256_hadd_ps(ss,_mm256_permute2f128_ps(ss,ss,1));
76 hh=_mm256_hadd_ps(hh,hh);hh=_mm256_hadd_ps(hh,hh);float sse=_mm256_cvtss_f32(hh);
77 for(;i<n;i++)sse+=x[i]*x[i];float ir=1.0f/sqrtf(sse/n+e);__m256 iv=_mm256_set1_ps(ir);
78 for(i=0;i<=n-8;i+=8){__m256 xv=_mm256_loadu_ps(x+i),wv=_mm256_loadu_ps(w+i);
79 _mm256_storeu_ps(o+i,_mm256_mul_ps(_mm256_mul_ps(xv,iv),wv));}
80 for(;i<n;i++)o[i]=x[i]*ir*w[i];
81}
82
83/* GQA attention */
84static void gqa(float *o, const float *q, const float *kc, const float *vc,
85 int sl, int nh, int nkh, int hd){
86 int gr=nh/nkh;
87 #pragma omp parallel for schedule(static, 64)
88 for(int h=0;h<nh;h++){
89 int kv=h/gr;const float *qh=q+h*hd,*kb=kc+kv*hd,*vb=vc+kv*hd;
90 int ks=nkh*hd;float *oh=o+h*hd,sc[4096],mx=-1e30f,inv=1.0f/sqrtf(hd);
91 for(int s=0;s<sl;s++){const float*ks_=kb+s*ks;__m256 su=_mm256_setzero_ps();int d;
92 for(d=0;d<=hd-8;d+=8)su=_mm256_fmadd_ps(_mm256_loadu_ps(qh+d),_mm256_loadu_ps(ks_+d),su);
93 float dot=hsum_ps(su);for(;d<hd;d++)dot+=qh[d]*ks_[d];dot*=inv;sc[s]=dot;if(dot>mx)mx=dot;}
94 float se=0;for(int s=0;s<sl;s++){sc[s]=expf(sc[s]-mx);se+=sc[s];}float is=1.0f/se;
95 memset(oh,0,hd*sizeof(float));
96 for(int s=0;s<sl;s++){const float*vs_=vb+s*ks;float w=sc[s]*is;__m256 wv=_mm256_set1_ps(w);int d;
97 for(d=0;d<=hd-8;d+=8){__m256 vv=_mm256_loadu_ps(vs_+d);
98 __m256 ac=_mm256_loadu_ps(oh+d);_mm256_storeu_ps(oh+d,_mm256_fmadd_ps(wv,vv,ac));}
99 for(;d<hd;d++)oh[d]+=w*vs_[d];}
100 }
101}
102
103/* ═════════════════════════════════════════════════════════════════════════
104 * engine_forward — flat arrays API
105 * All pointer arrays are [L] length
106 * ═════════════════════════════════════════════════════════════════════════ */
107void engine_forward(
108 int tok,
109 /* Model dims */
110 int L, int N, int NH, int NKH, int HD, int FF, int V,
111 float eps,
112 /* Per-layer weight pointers [L] */
113 const uint8_t **wQ, const uint8_t **wK, const uint8_t **wV,
114 const uint8_t **wO, const uint8_t **wG, const uint8_t **wU, const uint8_t **wD,
115 const float **wAN, const float **wFN,
116 /* Per-layer row counts [L] */
117 const int *nQ, const int *nK, const int *nV,
118 const int *nO, const int *nG, const int *nU, const int *nD,
119 int nc,
120 /* Embed + output */
121 const float *emb, const float *onw,
122 const uint8_t *wOut, int outNR, int outNC,
123 /* KV cache + mutable state */
124 float *kvK, float *kvV, int *kvL, int MP,
125 /* Workspace */
126 float *x, float *xn, float *res,
127 float *q, float *k, float *v,
128 float *att, float *gate, float *up, float *silu,
129 float *oproj, float *ffn,
130 /* Output */
131 float *logits
132) {
133 memcpy(x, emb + (size_t)tok * N, N*sizeof(float));
134
135 for(int l=0;l<L;l++){
136 memcpy(res, x, N*sizeof(float));
137 rms(xn, x, wAN[l], N, eps);
138
139 /* QKV batch */
140 int tq=nQ[l]+nK[l]+nV[l];
141 #pragma omp parallel for schedule(static, 64)
142 for(int i=0;i<tq;i++){
143 if(i<nQ[l]) q[i]=q4d(wQ[l],xn,nc,i);
144 else if(i<nQ[l]+nK[l]) k[i-nQ[l]]=q4d(wK[l],xn,nc,i-nQ[l]);
145 else v[i-nQ[l]-nK[l]]=q4d(wV[l],xn,nc,i-nQ[l]-nK[l]);
146 }
147
148 /* RoPE (on-the-fly with precomputed freq) */
149 int pos=kvL[l];
150 for(int hh=0;hh<NH;hh++){float *qh=q+hh*HD;
151 for(int j=0;j<HD/2;j++){
152 double ang=(double)pos/pow(10000.0,2.0*j/HD);
153 double cs=cos(ang),sn=sin(ang);
154 float a=qh[j],b=qh[j+HD/2];qh[j]=(float)(a*cs-b*sn);qh[j+HD/2]=(float)(b*cs+a*sn);}}
155 for(int hh=0;hh<NKH;hh++){float *kh=k+hh*HD;
156 for(int j=0;j<HD/2;j++){
157 double ang=(double)pos/pow(10000.0,2.0*j/HD);
158 double cs=cos(ang),sn=sin(ang);
159 float a=kh[j],b=kh[j+HD/2];kh[j]=(float)(a*cs-b*sn);kh[j+HD/2]=(float)(b*cs+a*sn);}}
160
161 /* KV store */
162 memcpy(kvK+((size_t)l*MP+kvL[l])*NKH*HD, k, NKH*HD*sizeof(float));
163 memcpy(kvV+((size_t)l*MP+kvL[l])*NKH*HD, v, NKH*HD*sizeof(float));
164
165 /* GQA attention */
166 gqa(att, q, kvK+(size_t)l*MP*NKH*HD, kvV+(size_t)l*MP*NKH*HD, kvL[l]+1, NH, NKH, HD);
167 kvL[l]++;
168
169 /* O proj + residual */
170 #pragma omp parallel for schedule(static, 64)
171 for(int i=0;i<nO[l];i++) oproj[i]=q4d(wO[l],att,nc,i);
172 for(int i=0;i<N;i++) x[i]=res[i]+oproj[i];
173
174 /* FFN */
175 memcpy(res, x, N*sizeof(float));
176 rms(xn, x, wFN[l], N, eps);
177
178 int tg=nG[l]+nU[l];
179 #pragma omp parallel for schedule(static, 64)
180 for(int i=0;i<tg;i++){
181 if(i<nG[l]) gate[i]=q4d(wG[l],xn,nc,i);
182 else up[i-nG[l]]=q4d(wU[l],xn,nc,i-nG[l]);}
183
184 for(int i=0;i<FF;i++){float g=gate[i];silu[i]=(g/(1.0f+expf(-g)))*up[i];}
185
186 #pragma omp parallel for schedule(static, 64)
187 for(int i=0;i<nD[l];i++) ffn[i]=q4d(wD[l],silu,FF,i);
188 for(int i=0;i<N;i++) x[i]=res[i]+ffn[i];
189 }
190
191 rms(xn, x, onw, N, eps);
192 #pragma omp parallel for schedule(static, 64)
193 for(int i=0;i<outNR;i++) logits[i]=q8d(wOut,xn,outNC,i);
194}