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/mojo_engine/tl_gen.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 / 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 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 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 19 static 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 26 static 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 33 static 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=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 73 static void decode_raw(FILE *out, Entry *voc, int n, int tok) { 74 for (int i=0;i1?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;lsm)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;d80)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;ibv){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 }