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 / mojo_engine / tl_gen.c

196 lines · 7182 bytes Raw Blame History
1/* tl_gen.c — TinyLlama autoregressive generation with f16 .bin weights */
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <stdint.h>
6#include <math.h>
7#include <omp.h>
8#include <immintrin.h>
9
10#define NE 2048
11#define NH 32
12#define NK 4
13#define HD 64
14#define NL 22
15#define NF 5632
16#define NV 32000
17#define MAX_SEQ 128
18
19static void* load_file(const char *dir, const char *name) {
20 char p[1024]; snprintf(p, sizeof(p), "%s/%s", dir, name);
21 FILE *f = fopen(p, "rb"); if (!f) return NULL;
22 fseek(f, 0, SEEK_END); size_t sz = ftell(f); fseek(f, 0, SEEK_SET);
23 void *b = malloc(sz); fread(b, 1, sz, f); fclose(f); return b;
24}
25
26static float h2f(uint16_t h) {
27 uint32_t s=(h>>15)&1, e=(h>>10)&0x1F, m=h&0x3FF;
28 if (!e) { float r=(float)m*5.960464477539063e-8f; return s?-r:r; }
29 if (e==31) return 0;
30 uint32_t b=(s<<31)|((e+112)<<23)|(m<<13); float r; memcpy(&r,&b,4); return r;
31}
32
33static void mm_f16(const void *w, const float *x, float *o, int M, int N) {
34 #pragma omp parallel for
35 for (int r=0; r<M; r++) {
36 __m256 a=_mm256_setzero_ps();
37 const uint16_t *wt=(const uint16_t*)w + r*N;
38 int c; for (c=0; c+8<=N; c+=8) {
39 a=_mm256_fmadd_ps(_mm256_cvtph_ps(_mm_loadu_si128((const __m128i*)(wt+c))),
40 _mm256_loadu_ps(x+c), a);
41 }
42 float tail=0; for (; c<N; c++) tail+=h2f(wt[c])*x[c];
43 __m128 hi=_mm_add_ps(_mm256_castps256_ps128(a), _mm256_extractf128_ps(a,1));
44 hi=_mm_hadd_ps(hi,hi); hi=_mm_hadd_ps(hi,hi);
45 o[r]=_mm_cvtss_f32(hi)+tail;
46 }
47}
48
49typedef struct { int id; char *text; int len; } Entry;
50
51static Entry* load_vocab(const char *path, int *n) {
52 FILE *f=fopen(path,"rb"); if(!f) return NULL;
53 fread(n,4,1,f);
54 Entry *v=calloc(*n,sizeof(Entry));
55 for (int i=0;i<*n;i++) {
56 int pl; fread(&pl,4,1,f);
57 v[i].text=malloc(pl+1); fread(v[i].text,1,pl,f); v[i].text[pl]=0; v[i].len=pl;
58 fread(&v[i].id,4,1,f);
59 }
60 fclose(f); return v;
61}
62
63static void decode(FILE *out, Entry *voc, int n, int tok) {
64 for (int i=0;i<n;i++) if (voc[i].id==tok) {
65 const char *p=voc[i].text; int l=voc[i].len;
66 if (l>=3 && (unsigned char)p[0]==0xE2 && (unsigned char)p[1]==0x96 && (unsigned char)p[2]==0x81)
67 { p+=3; l-=3; }
68 fwrite(p,1,l,out);
69 return;
70 }
71}
72
73static void decode_raw(FILE *out, Entry *voc, int n, int tok) {
74 for (int i=0;i<n;i++) if (voc[i].id==tok) {
75 fwrite(voc[i].text,1,voc[i].len,out);
76 return;
77 }
78}
79
80int main(int argc,char**argv) {
81 const char *wdir=argc>1?argv[1]:"/tmp/weights_tl";
82 const char *vpath=argc>2?argv[2]:"/tmp/vocab.bin";
83
84 int nv; Entry *voc=load_vocab(vpath,&nv);
85 if(!voc){fprintf(stderr,"FAIL: vocab\n");return 1;}
86 printf("Vocab: %d\n",nv);
87
88 double t0=omp_get_wtime();
89 void *emb=load_file(wdir,"token_embd_weight.bin");
90 void *onw=load_file(wdir,"output_norm_weight.bin");
91 void *lmw=load_file(wdir,"output_weight.bin");
92 void *wl[NL][9];
93 char*sfx[9]={"_attn_norm_weight.bin","_ffn_norm_weight.bin",
94 "_attn_q_weight.bin","_attn_k_weight.bin","_attn_v_weight.bin",
95 "_attn_output_weight.bin","_ffn_gate_weight.bin","_ffn_up_weight.bin","_ffn_down_weight.bin"};
96 char nm[256];
97 for(int l=0;l<NL;l++) for(int f=0;f<9;f++)
98 {snprintf(nm,256,"blk_%d%s",l,sfx[f]); wl[l][f]=load_file(wdir,nm);}
99 printf("Load: %.0f ms\n",(omp_get_wtime()-t0)*1000);
100
101 // Prompt "2+2="
102 int pr[]={1,29871,29906,29974,29906,29922}, np=6, ng=10;
103 int toks[MAX_SEQ]; for(int i=0;i<np;i++) toks[i]=pr[i];
104 int nt=np;
105
106 float *kc=calloc(NL*NK*MAX_SEQ*HD,4);
107 float *vc=calloc(NL*NK*MAX_SEQ*HD,4);
108 float hp[NE],bp[NE],qp[NH*HD],kp[NK*HD],vbuf[NK*HD],gp[NF],up[NF],dp[NE],lp[NV];
109
110 printf("Prompt: "); for(int i=0;i<np;i++) decode_raw(stdout,voc,nv,toks[i]);
111 printf("\nOutput: "); fflush(stdout);
112
113 t0=omp_get_wtime();
114 for(int pos=0;pos<ng&&nt<MAX_SEQ;pos++){
115 int tok=toks[pos];
116 const uint16_t *e=(const uint16_t*)emb;
117 for(int i=0;i<NE;i++) hp[i]=h2f(e[tok*NE+i]);
118
119 for(int l=0;l<NL;l++){
120 float ss=0; for(int i=0;i<NE;i++) ss+=hp[i]*hp[i];
121 float in=1.0f/sqrtf(ss/NE+1e-6f);
122 const float *an=(const float*)wl[l][0];
123 for(int i=0;i<NE;i++) bp[i]=hp[i]*an[i]*in;
124
125 mm_f16(wl[l][2],bp,qp,NH*HD,NE);
126 mm_f16(wl[l][3],bp,kp,NK*HD,NE);
127 mm_f16(wl[l][4],bp,vbuf,NK*HD,NE);
128
129 for(int h=0;h<NH;h++) for(int d2=0;d2<HD;d2+=2){
130 float f=pos/powf(10000,(float)d2/HD),c=cosf(f),s=sinf(f);
131 float x0=qp[h*HD+d2],x1=qp[h*HD+d2+1];
132 qp[h*HD+d2]=x0*c-x1*s; qp[h*HD+d2+1]=x0*s+x1*c;
133 }
134 for(int h=0;h<NK;h++) for(int d2=0;d2<HD;d2+=2){
135 float f=pos/powf(10000,(float)d2/HD),c=cosf(f),s=sinf(f);
136 float x0=kp[h*HD+d2],x1=kp[h*HD+d2+1];
137 kp[h*HD+d2]=x0*c-x1*s; kp[h*HD+d2+1]=x0*s+x1*c;
138 }
139
140 int lo=l*NK*MAX_SEQ*HD;
141 for(int h=0;h<NK;h++) for(int d=0;d<HD;d++){
142 kc[lo+h*MAX_SEQ*HD+pos*HD+d]=kp[h*HD+d];
143 vc[lo+h*MAX_SEQ*HD+pos*HD+d]=vbuf[h*HD+d];
144 }
145
146 int kr=NH/NK;
147 for(int hq=0;hq<NH;hq++){
148 int hk=hq/kr;
149 float sc[MAX_SEQ],sm=-1e9;
150 for(int p=0;p<=pos;p++){
151 float s=0;
152 for(int d=0;d<HD;d++) s+=qp[hq*HD+d]*kc[lo+hk*MAX_SEQ*HD+p*HD+d];
153 s/=sqrtf(HD); sc[p]=s; if(s>sm)sm=s;
154 }
155 float su=0; for(int p=0;p<=pos;p++){sc[p]=expf(sc[p]-sm); su+=sc[p];}
156 for(int d=0;d<HD;d++){
157 float o=0;
158 for(int p=0;p<=pos;p++) o+=vc[lo+hk*MAX_SEQ*HD+p*HD+d]*(sc[p]/su);
159 qp[hq*HD+d]=o;
160 }
161 }
162
163 mm_f16(wl[l][5],qp,bp,NE,NH*HD);
164 for(int i=0;i<NE;i++) hp[i]+=bp[i];
165
166 ss=0; for(int i=0;i<NE;i++) ss+=hp[i]*hp[i];
167 in=1.0f/sqrtf(ss/NE+1e-6f);
168 const float *fn=(const float*)wl[l][1];
169 for(int i=0;i<NE;i++) bp[i]=hp[i]*fn[i]*in;
170
171 mm_f16(wl[l][6],bp,gp,NF,NE);
172 mm_f16(wl[l][7],bp,up,NF,NE);
173 for(int i=0;i<NF;i++){float gv=gp[i];if(gv<-80)gv=-80;if(gv>80)gv=80;
174 gp[i]=(gv/(1+expf(-gv)))*up[i];}
175 mm_f16(wl[l][8],gp,dp,NE,NF);
176 for(int i=0;i<NE;i++) hp[i]+=dp[i];
177 }
178
179 float ss=0; for(int i=0;i<NE;i++) ss+=hp[i]*hp[i];
180 float in=1.0f/sqrtf(ss/NE+1e-6f);
181 const float *on=(const float*)onw;
182 for(int i=0;i<NE;i++) bp[i]=hp[i]*on[i]*in;
183 mm_f16(lmw,bp,lp,NV,NE);
184
185 int best=0; float bv=lp[0];
186 for(int i=1;i<NV;i++) if(lp[i]>bv){bv=lp[i];best=i;}
187 toks[nt++]=best;
188 if(best==2) break;
189 decode(stdout,voc,nv,best);
190 fflush(stdout);
191 }
192 printf("\n");
193 double t1=omp_get_wtime();
194 printf("Generated %d tokens in %.0f ms (%.1f tok/s)\n",nt-np,(t1-t0)*1000,(nt-np)/(t1-t0));
195 return 0;
196}