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/c_engine.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 / c_engine.c` 197 lines · 9254 bytes Raw Blame History 1 /* c_engine.c — Complete transformer forward pass with struct API. 2 * 3 * engine_forward() does 22-layer TinyLlama forward pass in C. 4 * Uses EngineConfig struct for clean API. 5 * 6 * Compile: 7 * gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC \ 8 * -o c_engine.so c_engine.c -lm 9 */ 10 #include 11 #include 12 #include 13 #include 14 #include 15 16 #define Q4_0_BS 18 17 #define Q8_0_BS 34 18 19 /* ═══ AVX2 helpers ═════════════════════════════════════════════════════ */ 20 static inline float f16_to_f32(uint16_t h) { return _cvtsh_ss(h); } 21 static inline float hsum_ps(__m256 v) { 22 __m128 lo=_mm256_castps256_ps128(v), hi=_mm256_extractf128_ps(v,1); 23 lo=_mm_add_ps(lo,hi); lo=_mm_hadd_ps(lo,lo); lo=_mm_hadd_ps(lo,lo); 24 return _mm_cvtss_f32(lo); 25 } 26 27 /* ═══ Q4_0 dot (inline AVX2, no function pointers) ═══════════════════ */ 28 static inline float q4_row(const uint8_t *W, const float *x, int nc, int r) { 29 int bp=nc/32; __m256 s0=_mm256_setzero_ps(),s1=_mm256_setzero_ps(); 30 for(int b=0;bmx)mx=dot;} 95 float se=0;for(int s=0;sN,NH=c->NH,NKH=c->NKH,HD=c->HD,FF=c->FF,L=c->L,nc=c->nc; 124 float *x=c->x,*xn=c->xn,*res=c->res,*q=c->q,*k=c->k,*v=c->v; 125 float *att=c->att,*gate=c->gate,*up=c->up,*silu=c->silu,*o=c->oproj,*ffn=c->ffn; 126 127 memcpy(x, c->emb + (size_t)tok * N, N*sizeof(float)); 128 129 for(int l=0;lwAN[l], N, c->eps); 132 133 /* QKV batch */ 134 int tq=c->nQ[l]+c->nK[l]+c->nV[l]; 135 #pragma omp parallel for schedule(static) 136 for(int i=0;inQ[l]) q[i]=q4_row(c->wQ[l],xn,nc,i); 138 else if(inQ[l]+c->nK[l]) k[i-c->nQ[l]]=q4_row(c->wK[l],xn,nc,i-c->nQ[l]); 139 else v[i-c->nQ[l]-c->nK[l]]=q4_row(c->wV[l],xn,nc,i-c->nQ[l]-c->nK[l]); 140 } 141 142 /* RoPE (pre-computed tables — cos_tbl/sin_tbl stored at end of EC) */ 143 int pos=c->kvL[l], hd=HD, h=hd/2; 144 const float *ct=c->x+4096*h; /* FIXME: use proper cos/sin pointers */ 145 /* Simple RoPE with on-the-fly computation (fast enough for <100 pos) */ 146 for(int hh=0;hhkvK+((size_t)l*c->MP+c->kvL[l])*NKH*HD, k, NKH*HD*sizeof(float)); 159 memcpy(c->kvV+((size_t)l*c->MP+c->kvL[l])*NKH*HD, v, NKH*HD*sizeof(float)); 160 161 /* GQA attention */ 162 int sl=c->kvL[l]+1; 163 gqa(att, q, c->kvK+(size_t)l*c->MP*NKH*HD, c->kvV+(size_t)l*c->MP*NKH*HD, 164 sl, NH, NKH, HD); 165 c->kvL[l]++; 166 167 /* O proj + residual */ 168 #pragma omp parallel for schedule(static) 169 for(int i=0;inO[l];i++) o[i]=q4_row(c->wO[l],att,nc,i); 170 for(int i=0;iwFN[l], N, c->eps); 175 176 /* Gate+Up batch */ 177 int tg=c->nG[l]+c->nU[l]; 178 #pragma omp parallel for schedule(static) 179 for(int i=0;inG[l]) gate[i]=q4_row(c->wG[l],xn,nc,i); 181 else up[i-c->nG[l]]=q4_row(c->wU[l],xn,nc,i-c->nG[l]); 182 } 183 184 /* SiLU * up */ 185 for(int i=0;inD[l];i++) ffn[i]=q4_row(c->wD[l],silu,nc,i); 190 for(int i=0;ionw, N, c->eps); 195 #pragma omp parallel for schedule(static) 196 for(int i=0;ioutNR;i++) logits[i]=q8_row(c->wOut,xn,c->outNC,i); 197 }