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_f16.c

168 lines · 6442 bytes Raw Blame History
1/* tl_f16.c — Correct TinyLlama f16 .bin inference reference.
2 * Loads raw f16 weights (not Q4_0), runs forward pass, measures real tok/s.
3 * Compile: gcc -O2 -mavx2 -mfma -mf16c -fopenmp tl_f16.c -o tl_f16 -lm
4 * Usage: ./tl_f16 /tmp/weights_tl/
5 */
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <stdint.h>
10#include <math.h>
11#include <omp.h>
12#include <immintrin.h>
13
14#define NE 2048
15#define NH 32
16#define NK 4
17#define HD 64
18#define NL 22
19#define NF 5632
20#define NV 32000
21
22static void* load(const char *dir, const char *name) {
23 char p[1024]; snprintf(p, sizeof(p), "%s/%s", dir, name);
24 FILE *f = fopen(p, "rb"); if (!f) return NULL;
25 fseek(f, 0, SEEK_END); size_t sz = ftell(f); fseek(f, 0, SEEK_SET);
26 void *b = malloc(sz); fread(b, 1, sz, f); fclose(f); return b;
27}
28
29static float f16_to_f32(uint16_t h) {
30 uint32_t s = (h >> 15) & 1, e = (h >> 10) & 0x1F, m = h & 0x3FF;
31 if (e == 0) { float r = (float)m * 5.960464477539063e-8f; return s ? -r : r; }
32 if (e == 31) return 0.0f;
33 uint32_t b = (s << 31) | ((e + 112) << 23) | (m << 13);
34 float r; memcpy(&r, &b, 4); return r;
35}
36
37static void mm_f16(const void *w, const float *x, float *o, int M, int N) {
38 #pragma omp parallel for
39 for (int r = 0; r < M; r++) {
40 __m256 a = _mm256_setzero_ps();
41 const uint16_t *wt = (const uint16_t*)w + r * N;
42 int c;
43 for (c = 0; c + 8 <= N; c += 8) {
44 __m128i w16 = _mm_loadu_si128((const __m128i*)(wt + c));
45 a = _mm256_fmadd_ps(_mm256_cvtph_ps(w16), _mm256_loadu_ps(x + c), a);
46 }
47 float s = 0;
48 for (; c < N; c++) s += f16_to_f32(wt[c]) * x[c];
49 __m128 hi = _mm_add_ps(_mm256_castps256_ps128(a), _mm256_extractf128_ps(a, 1));
50 hi = _mm_hadd_ps(hi, hi); hi = _mm_hadd_ps(hi, hi);
51 o[r] = _mm_cvtss_f32(hi) + s;
52 }
53}
54
55int main(int argc, char **argv) {
56 const char *d = argc > 1 ? argv[1] : "/tmp/weights_tl";
57 double t0 = omp_get_wtime();
58
59 // Load weights
60 void *w_emb = load(d, "token_embd_weight.bin");
61 void *w_on = load(d, "output_norm_weight.bin");
62 void *w_lm = load(d, "output_weight.bin");
63 if (!w_emb || !w_on || !w_lm) { fprintf(stderr, "FAIL: base weights\n"); return 1; }
64
65 void *wl[NL][9];
66 char sfx[9][32] = {"_attn_norm_weight.bin","_ffn_norm_weight.bin",
67 "_attn_q_weight.bin","_attn_k_weight.bin","_attn_v_weight.bin",
68 "_attn_output_weight.bin","_ffn_gate_weight.bin","_ffn_up_weight.bin","_ffn_down_weight.bin"};
69 char nm[256];
70 for (int l = 0; l < NL; l++)
71 for (int f = 0; f < 9; f++)
72 { snprintf(nm, sizeof(nm), "blk_%d%s", l, sfx[f]); wl[l][f] = load(d, nm);
73 if (!wl[l][f]) { fprintf(stderr, "FAIL: %s\n", nm); return 1; } }
74
75 double t1 = omp_get_wtime();
76 printf("Load: %.0f ms\n", (t1-t0)*1000);
77
78 // Buffers
79 float hp[NE], bp[NE], qp[NH*HD], kp[NK*HD], vp[NK*HD], gp[NF], up[NF], dp[NE], lp[NV];
80
81 // Embed BOS token (1)
82 const uint16_t *emb = (const uint16_t*)w_emb;
83 for (int i = 0; i < NE; i++) hp[i] = f16_to_f32(emb[1 * NE + i]);
84 double t2 = omp_get_wtime();
85 printf("Embed: %.0f ms\n", (t2-t1)*1000);
86
87 // Forward pass
88 for (int l = 0; l < NL; l++) {
89 // RMS Norm pre-attention
90 float ss = 0; for (int i = 0; i < NE; i++) ss += hp[i] * hp[i];
91 float inv = 1.0f / sqrtf(ss / NE + 1e-6f);
92 const float *anp = (const float*)wl[l][0];
93 for (int i = 0; i < NE; i++) bp[i] = hp[i] * anp[i] * inv;
94
95 // QKV
96 mm_f16(wl[l][2], bp, qp, NH*HD, NE);
97 mm_f16(wl[l][3], bp, kp, NK*HD, NE);
98 mm_f16(wl[l][4], bp, vp, NK*HD, NE);
99
100 // RoPE
101 for (int h = 0; h < NH; h++) for (int d2 = 0; d2 < HD; d2 += 2) {
102 float f = 1 / powf(10000, (float)d2/HD);
103 float c = cosf(f), s = sinf(f);
104 float x0 = qp[h*HD+d2], x1 = qp[h*HD+d2+1];
105 qp[h*HD+d2] = x0*c - x1*s; qp[h*HD+d2+1] = x0*s + x1*c;
106 }
107 for (int h = 0; h < NK; h++) for (int d2 = 0; d2 < HD; d2 += 2) {
108 float f = 1 / powf(10000, (float)d2/HD);
109 float c = cosf(f), s = sinf(f);
110 float x0 = kp[h*HD+d2], x1 = kp[h*HD+d2+1];
111 kp[h*HD+d2] = x0*c - x1*s; kp[h*HD+d2+1] = x0*s + x1*c;
112 }
113
114 // GQA (single token, no KV cache)
115 int khr = NH / NK;
116 for (int hq = 0; hq < NH; hq++) {
117 int hkv = hq / khr;
118 float sc = 0;
119 for (int d = 0; d < HD; d++) sc += qp[hq*HD+d] * kp[hkv*HD+d];
120 sc = expf(sc / sqrtf(HD)) / expf(sc / sqrtf(HD)); // softmax(1-element) = 1.0
121 for (int d = 0; d < HD; d++) qp[hq*HD+d] = vp[hkv*HD+d] * sc;
122 }
123
124 // O proj
125 mm_f16(wl[l][5], qp, bp, NE, NH*HD);
126 for (int i = 0; i < NE; i++) hp[i] += bp[i];
127
128 // RMS Norm pre-FFN
129 ss = 0; for (int i = 0; i < NE; i++) ss += hp[i] * hp[i];
130 inv = 1.0f / sqrtf(ss / NE + 1e-6f);
131 const float *fnp = (const float*)wl[l][1];
132 for (int i = 0; i < NE; i++) bp[i] = hp[i] * fnp[i] * inv;
133
134 // FFN gate + up
135 mm_f16(wl[l][6], bp, gp, NF, NE);
136 mm_f16(wl[l][7], bp, up, NF, NE);
137 for (int i = 0; i < NF; i++) {
138 float gv = gp[i];
139 if (gv < -80) gv = -80; if (gv > 80) gv = 80;
140 gp[i] = (gv / (1 + expf(-gv))) * up[i];
141 }
142
143 // FFN down
144 mm_f16(wl[l][8], gp, dp, NE, NF);
145 for (int i = 0; i < NE; i++) hp[i] += dp[i];
146 }
147
148 double t3 = omp_get_wtime();
149
150 // Final norm + LM head
151 float ss = 0; for (int i = 0; i < NE; i++) ss += hp[i] * hp[i];
152 float inv = 1.0f / sqrtf(ss / NE + 1e-6f);
153 const float *onp = (const float*)w_on;
154 for (int i = 0; i < NE; i++) bp[i] = hp[i] * onp[i] * inv;
155 mm_f16(w_lm, bp, lp, NV, NE);
156
157 double t4 = omp_get_wtime();
158
159 int best = 0; float bv = lp[0];
160 for (int i = 1; i < NV; i++) if (lp[i] > bv) { bv = lp[i]; best = i; }
161
162 printf("First 5 logits: %.2f %.2f %.2f %.2f %.2f\n", lp[0], lp[1], lp[2], lp[3], lp[4]);
163 printf("Best token: %d (val=%.1f)\n", best, bv);
164 printf("Layers: %.0f ms (%.2f ms/layer)\n", (t3-t2)*1000, (t3-t2)*1000/NL);
165 printf("LM head: %.0f ms\n", (t4-t3)*1000);
166 printf("Total forward: %.0f ms tok/s: %.1f\n", (t4-t2)*1000, 1.0/(t4-t2));
167 return 0;
168}