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

93 lines · 3801 bytes Raw Blame History
1/* cengine_final.c — Uses proven quant_kernels_omp for ALL matmuls.
2 * Drop-in replacement for q4d — calls quant_matmul_omp externally.
3 * Compile:
4 * gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC \
5 * -o cengine_final.so cengine_final.c \
6 * -L. -l:quant_kernels_omp.so -Wl,-rpath,/onedev-workspace/work/src/mojollama/kernels -lm
7 */
8#include <stdio.h>
9#include <string.h>
10#include <math.h>
11#include <omp.h>
12
13/* External: proven OMP kernels */
14extern void quant_matmul_omp(const unsigned char*, const float*, float*, int, int, int);
15extern void rms_norm(const float*, const float*, float*, int, float);
16extern void silu(float*, int);
17
18/* External: GQA attention */
19extern void gqa_attention_decode(const float*, const float*, const float*,
20 float*, int, int, int, int);
21
22void process_layer_final(
23 int layer_id, float *x,
24 int L, int N, int NH, int NKH, int HD, int FF, float eps,
25 const unsigned char *w_q, const unsigned char *w_k, const unsigned char *w_v,
26 const unsigned char *w_o, const unsigned char *w_g, const unsigned char *w_u, const unsigned char *w_d,
27 const float *w_an, const float *w_fn,
28 int n_q, int n_k, int n_v, int n_o, int n_g, int n_u, int n_d,
29 int nc_qkv, int nc_down, int nc_o, /* n_cols per projection */
30 float *kv_k, float *kv_v, int *kv_len, int max_pos,
31 float *xn, float *res, float *q, float *k, float *v,
32 float *att, float *gate, float *up, float *silu_buf,
33 float *oproj, float *ffn
34) {
35 int pos = *kv_len;
36
37 memcpy(res, x, N * sizeof(float));
38 rms_norm(x, xn, w_an, N, eps); /* note: rms_norm(out, x, w, n, eps) */
39
40 /* QKV — use proven quant_matmul_omp for individual projections */
41 quant_matmul_omp(w_q, xn, q, n_q, nc_qkv, 2);
42 quant_matmul_omp(w_k, xn, k, n_k, nc_qkv, 2);
43 quant_matmul_omp(w_v, xn, v, n_v, nc_qkv, 2);
44
45 /* RoPE — using on-the-fly computation */
46 for(int hh=0;hh<NH;hh++){float *qh=q+hh*HD;
47 for(int j=0;j<HD/2;j++){
48 double ang=(double)pos/pow(10000.0,2.0*j/HD);
49 double cs=cos(ang),sn=sin(ang);
50 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);}}
51 for(int hh=0;hh<NKH;hh++){float *kh=k+hh*HD;
52 for(int j=0;j<HD/2;j++){
53 double ang=(double)pos/pow(10000.0,2.0*j/HD);
54 double cs=cos(ang),sn=sin(ang);
55 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);}}
56
57 /* KV store */
58 memcpy(kv_k + ((size_t)layer_id*max_pos+pos)*NKH*HD, k, NKH*HD*sizeof(float));
59 memcpy(kv_v + ((size_t)layer_id*max_pos+pos)*NKH*HD, v, NKH*HD*sizeof(float));
60
61 /* Attention */
62 gqa_attention_decode(q,
63 kv_k+(size_t)layer_id*max_pos*NKH*HD,
64 kv_v+(size_t)layer_id*max_pos*NKH*HD,
65 att, pos+1, NH, NKH, HD);
66 (*kv_len)++;
67
68 /* O proj + residual */
69 quant_matmul_omp(w_o, att, oproj, n_o, nc_o, 2);
70 for(int i=0;i<N;i++) x[i]=res[i]+oproj[i];
71
72 /* FFN */
73 memcpy(res, x, N*sizeof(float));
74 rms_norm(x, xn, w_fn, N, eps); /* note: rms_norm(out, x, w, n, eps) — WARNING: arg order mismatch! */
75
76 /* Gate + Up */
77 quant_matmul_omp(w_g, xn, gate, n_g, nc_qkv, 2);
78 quant_matmul_omp(w_u, xn, up, n_u, nc_qkv, 2);
79
80 /* SiLU(gate) * up */
81 for(int i=0;i<FF;i++){float g=gate[i];silu_buf[i]=(g/(1.0f+expf(-g)))*up[i];}
82
83 /* Down + residual */
84 quant_matmul_omp(w_d, silu_buf, ffn, n_d, nc_down, 2);
85 for(int i=0;i<N;i++) x[i]=res[i]+ffn[i];
86}
87
88void process_final(float *x, int N, int HD, float eps,
89 const float *onw, const unsigned char *w_out, int out_nr, int out_nc,
90 float *xn, float *logits) {
91 rms_norm(x, xn, onw, N, eps);
92 quant_matmul_omp(w_out, xn, logits, out_nr, out_nc, 8); /* Q8_0 type = 8 */
93}