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_infer.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_infer.c` 134 lines · 6121 bytes Raw Blame History 1 /* tl_infer.c — Correct TinyLlama inference with f16 .bin weights 2 * Compile: gcc -O2 -mavx2 -mfma -mf16c tl_infer.c -o tl_infer -lm -lpthread 3 * Usage: ./tl_infer /tmp/weights_tl/ 4 */ 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 13 #define NE 2048 14 #define NH 32 15 #define NK 4 16 #define HD 64 17 #define NL 22 18 #define NF 5632 19 #define NV 32000 20 21 static void* load(const char *dir, const char *name) { 22 char p[1024]; snprintf(p, sizeof(p), "%s/%s", dir, name); 23 FILE *f = fopen(p, "rb"); if (!f) return NULL; 24 fseek(f, 0, SEEK_END); size_t sz = ftell(f); fseek(f, 0, SEEK_SET); 25 void *b = malloc(sz); fread(b, 1, sz, f); fclose(f); return b; 26 } 27 28 static inline float f16_to_f32(uint16_t h) { 29 uint32_t s = (h >> 15) & 1, e = (h >> 10) & 0x1F, m = h & 0x3FF; 30 if (e == 0) { float r = (float)m * 5.`960464477539063e`-8f; return s ? -r : r; } 31 if (e == 31) return 0.0f; 32 uint32_t b = (s << 31) | ((e + 112) << 23) | (m << 13); 33 float r; memcpy(&r, &b, 4); return r; 34 } 35 36 static void mm_f16(const void *w, const float *x, float *o, int M, int N) { 37 #pragma omp parallel for 38 for (int r = 0; r < M; r++) { 39 __m256 a = _mm256_setzero_ps(); 40 const uint16_t *wt = (const uint16_t*)w + r * N; 41 for (int c = 0; c < N; c += 8) { 42 __m128i w16 = _mm_loadu_si128((const __m128i*)(wt + c)); 43 __m256 wf = _mm256_cvtph_ps(w16); 44 a = _mm256_fmadd_ps(wf, _mm256_loadu_ps(x + c), a); 45 } 46 __m128 hi = _mm_add_ps(_mm256_castps256_ps128(a), _mm256_extractf128_ps(a, 1)); 47 hi = _mm_hadd_ps(hi, hi); hi = _mm_hadd_ps(hi, hi); 48 o[r] = _mm_cvtss_f32(hi); 49 } 50 } 51 52 int main(int argc, char **argv) { 53 const char *d = argc > 1 ? argv[1] : "/tmp/weights_tl"; 54 struct timespec ts0, ts1, ts2, ts3, ts4; clock_gettime(CLOCK_MONOTONIC, &ts0); 55 void *w[201] = {0}; int wi = 0; 56 w[wi++] = load(d, "token_embd_weight.bin"); 57 w[wi++] = load(d, "output_norm_weight.bin"); 58 w[wi++] = load(d, "output_weight.bin"); 59 char sfx[9][32] = {"_attn_norm_weight.bin","_ffn_norm_weight.bin", 60 "_attn_q_weight.bin","_attn_k_weight.bin","_attn_v_weight.bin", 61 "_attn_output_weight.bin","_ffn_gate_weight.bin","_ffn_up_weight.bin","_ffn_down_weight.bin"}; 62 char nm[256]; 63 for (int l = 0; l < NL; l++) 64 for (int f = 0; f < 9; f++) 65 { snprintf(nm, sizeof(nm), "blk_%d%s", l, sfx[f]); w[wi++] = load(d, nm); } 66 clock_gettime(CLOCK_MONOTONIC, &ts1); 67 printf("Load: %.0f ms\n", (double)(ts1.tv_sec-ts0.tv_sec)*1000 + (double)(ts1.tv_nsec-ts0.tv_nsec)/1e6); 68 69 float hp[NE], bp[NE], qp[NH*HD], kp[NK*HD], vp[NK*HD], gp[NF], up[NF], dp[NE], lp[NV]; 70 const uint16_t *emb = (const uint16_t*)w[0]; 71 int tok = 1; 72 for (int i = 0; i < NE; i++) hp[i] = f16_to_f32(emb[tok * NE + i]); 73 clock_gettime(CLOCK_MONOTONIC, &ts2); 74 printf("Embed: %.0f ms\n", (double)(ts2.tv_sec-ts1.tv_sec)*1000 + (double)(ts2.tv_nsec-ts1.tv_nsec)/1e6); 75 76 for (int l = 0; l < NL; l++) { 77 int lw = 3 + l * 9; 78 float ss = 0; 79 for (int i = 0; i < NE; i++) ss += hp[i] * hp[i]; 80 float inv = 1.0f / sqrtf(ss / NE + 1e-6f); 81 const float *anp = (const float*)w[lw]; 82 for (int i = 0; i < NE; i++) bp[i] = hp[i] * anp[i] * inv; 83 mm_f16(w[lw+2], bp, qp, NH*HD, NE); 84 mm_f16(w[lw+3], bp, kp, NK*HD, NE); 85 mm_f16(w[lw+4], bp, vp, NK*HD, NE); 86 for (int h = 0; h < NH; h++) for (int d2 = 0; d2 < HD; d2+=2) { 87 float f = tok / powf(10000, (float)d2/HD), c = cosf(f), s = sinf(f); 88 float x0 = qp[h*HD+d2], x1 = qp[h*HD+d2+1]; 89 qp[h*HD+d2] = x0*c - x1*s; qp[h*HD+d2+1] = x0*s + x1*c; 90 } 91 for (int h = 0; h < NK; h++) for (int d2 = 0; d2 < HD; d2+=2) { 92 float f = tok / powf(10000, (float)d2/HD), c = cosf(f), s = sinf(f); 93 float x0 = kp[h*HD+d2], x1 = kp[h*HD+d2+1]; 94 kp[h*HD+d2] = x0*c - x1*s; kp[h*HD+d2+1] = x0*s + x1*c; 95 } 96 for (int hq = 0; hq < NH; hq++) { 97 int hkv = hq / (NH/NK); float sc = 0; 98 for (int d = 0; d < HD; d++) sc += qp[hq*HD+d] * kp[hkv*HD+d]; 99 float wt = expf(sc / sqrtf(HD)) / expf(sc / sqrtf(HD)); 100 for (int d = 0; d < HD; d++) qp[hq*HD+d] = vp[hkv*HD+d] * wt; 101 } 102 mm_f16(w[lw+5], qp, bp, NE, NH*HD); 103 for (int i = 0; i < NE; i++) hp[i] += bp[i]; 104 ss = 0; for (int i = 0; i < NE; i++) ss += hp[i] * hp[i]; 105 inv = 1.0f / sqrtf(ss / NE + 1e-6f); 106 const float *fnp = (const float*)w[lw+1]; 107 for (int i = 0; i < NE; i++) bp[i] = hp[i] * fnp[i] * inv; 108 mm_f16(w[lw+6], bp, gp, NF, NE); 109 mm_f16(w[lw+7], bp, up, NF, NE); 110 for (int i = 0; i < NF; i++) { 111 float gv = gp[i]; if (gv < -80) gv = -80; if (gv > 80) gv = 80; 112 gp[i] = (gv / (1 + expf(-gv))) * up[i]; 113 } 114 mm_f16(w[lw+8], gp, dp, NE, NF); 115 for (int i = 0; i < NE; i++) hp[i] += dp[i]; 116 } 117 clock_gettime(CLOCK_MONOTONIC, &ts3); 118 ss = 0; for (int i = 0; i < NE; i++) ss += hp[i] * hp[i]; 119 inv = 1.0f / sqrtf(ss / NE + 1e-6f); 120 const float *onp = (const float*)w[1]; 121 for (int i = 0; i < NE; i++) bp[i] = hp[i] * onp[i] * inv; 122 mm_f16(w[2], bp, lp, NV, NE); 123 clock_gettime(CLOCK_MONOTONIC, &ts4); 124 int best = 0; float bv = lp[0]; 125 for (int i = 1; i < NV; i++) if (lp[i] > bv) { bv = lp[i]; best = i; } 126 double layers_ms = (double)(ts3.tv_sec-ts2.tv_sec)*1000 + (double)(ts3.tv_nsec-ts2.tv_nsec)/1e6; 127 double total_ms = (double)(ts4.tv_sec-ts2.tv_sec)*1000 + (double)(ts4.tv_nsec-ts2.tv_nsec)/1e6; 128 printf("First 5 logits: %.1f %.1f %.1f %.1f %.1f\n", lp[0], lp[1], lp[2], lp[3], lp[4]); 129 printf("Layers: %.0f ms (%.1f ms/layer)\n", layers_ms, layers_ms/NL); 130 printf("LM head: %.0f ms\n", (double)(ts4.tv_sec-ts3.tv_sec)*1000 + (double)(ts4.tv_nsec-ts3.tv_nsec)/1e6); 131 printf("Total: %.0f ms tok/s: %.1f\n", total_ms, 1.0/(total_ms/1000.0)); 132 printf("Best token: %d val: %.1f\n", best, bv); 133 return 0; 134 }