MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / src / mojollama / mojo_engine / mojollama_bench.mojo
| 1 | # MojoLlama Bench v4 — Full Transformer Model Benchmark |
| 2 | # WHAT: Benchmarks each transformer component separately + full model: |
| 3 | # embedding, RMS norm, QKV, RoPE, attention, FFN, output projection. |
| 4 | # Configurable architecture: n_embd, n_heads, n_kv_heads, n_layers, ff_hidden. |
| 5 | # WHY: Identify bottlenecks per component. Compare CPU architectures. |
| 6 | # WHEN: May 2026 — v4 with transformer architecture breakdown. |
| 7 | from std import time, math |
| 8 | from std.algorithm.backend.cpu.parallelize import parallelize |
| 9 | |
| 10 | # ═══ f16 helpers ═══ |
| 11 | def f16_val(w: UnsafePointer[UInt8, MutExternalOrigin], idx: Int) -> Float32: |
| 12 | var h = UInt16(w.load(idx*2)) | (UInt16(w.load(idx*2+1)) << 8) |
| 13 | var s = UInt32(h >> 15); var e = UInt32((h >> 10) & 0x1F); var m = UInt32(h & 0x3FF) |
| 14 | if e == 0: return 0.0 if m == 0 else Float32(Float64(m) * 5.960464477539063e-8) |
| 15 | if e == 31: return 0.0 |
| 16 | var r = Float32(m | 0x400); var ei = Int(e) - 25 |
| 17 | if ei >= 0: |
| 18 | for _ in range(ei): r *= 2.0 |
| 19 | else: |
| 20 | for _ in range(-ei): r *= 0.5 |
| 21 | return -r if s != 0 else r |
| 22 | |
| 23 | def f16_row(w: UnsafePointer[UInt8, MutExternalOrigin], |
| 24 | x: UnsafePointer[Float32, MutExternalOrigin], |
| 25 | r: Int, nc: Int) -> Float32: |
| 26 | var acc: Float32 = 0.0 |
| 27 | var ro = r * nc |
| 28 | for c in range(nc): |
| 29 | acc += f16_val(w, ro + c) * x.load(c) |
| 30 | return acc |
| 31 | |
| 32 | def f16_mm(w: UnsafePointer[UInt8, MutExternalOrigin], |
| 33 | x: UnsafePointer[Float32, MutExternalOrigin], |
| 34 | o: UnsafePointer[Float32, MutExternalOrigin], |
| 35 | nr: Int, nc: Int, nw: Int) -> Float64: |
| 36 | var t0 = time.perf_counter() |
| 37 | def wk(r: Int) capturing -> None: |
| 38 | o.store(r, f16_row(w, x, r, nc)) |
| 39 | parallelize[func=wk](num_work_items=nr, num_workers=nw) |
| 40 | return (time.perf_counter() - t0) * 1000.0 |
| 41 | |
| 42 | # ═══ Stats ═══ |
| 43 | def sort_f64(buf: UnsafePointer[Float64, MutExternalOrigin], n: Int): |
| 44 | for i in range(n): |
| 45 | var mi = i |
| 46 | for j in range(i+1, n): |
| 47 | if buf.load(j) < buf.load(mi): |
| 48 | mi = j |
| 49 | if mi != i: |
| 50 | var t = buf.load(i) |
| 51 | buf.store(i, buf.load(mi)) |
| 52 | buf.store(mi, t) |
| 53 | |
| 54 | def pct(buf: UnsafePointer[Float64, MutExternalOrigin], n: Int, p: Float64) -> Float64: |
| 55 | return buf.load(Int(Float64(n-1) * p)) |
| 56 | |
| 57 | def bench_mean(w: UnsafePointer[UInt8, MutExternalOrigin], |
| 58 | x: UnsafePointer[Float32, MutExternalOrigin], |
| 59 | o: UnsafePointer[Float32, MutExternalOrigin], |
| 60 | nr: Int, nc: Int, nw: Int, |
| 61 | buf: UnsafePointer[Float64, MutExternalOrigin], |
| 62 | n_warm: Int, n_iter: Int) -> Float64: |
| 63 | for _ in range(n_warm): |
| 64 | var _ = f16_mm(w, x, o, nr, nc, nw) |
| 65 | var total: Float64 = 0.0 |
| 66 | for mi in range(n_iter): |
| 67 | var t = f16_mm(w, x, o, nr, nc, nw) |
| 68 | buf.store(mi, t) |
| 69 | total += t |
| 70 | return total / Float64(n_iter) |
| 71 | |
| 72 | def main(): |
| 73 | # ═══ Architecture config (change for different models) ═══ |
| 74 | # Default: Llama 3 8B |
| 75 | var NE: Int = 4096 # n_embd |
| 76 | var NH: Int = 32 # n_heads |
| 77 | var NK: Int = 8 # n_kv_heads |
| 78 | var HD: Int = 128 # head_dim |
| 79 | var NL: Int = 32 # n_layers |
| 80 | var FF: Int = 14336 # ff_hidden |
| 81 | var NV: Int = 128256 # vocab_size |
| 82 | var NW: Int = 32 # threads |
| 83 | var _sq: Int = 128 # sequence length (attention bench) |
| 84 | var n_warm: Int = 2 |
| 85 | var n_iter: Int = 10 |
| 86 | |
| 87 | @extern("malloc") |
| 88 | def _alc(sz: Int64) abi("C") -> Int64: ... |
| 89 | |
| 90 | var inner = NH * HD |
| 91 | var kv_dim = NK * HD |
| 92 | var max_wr = max(max(NE, FF), max(kv_dim, max(inner, 4096))) # max weight rows |
| 93 | var max_wc = max(max(NE, inner), max(kv_dim, FF)) |
| 94 | |
| 95 | # Allocate buffers |
| 96 | var buf = UnsafePointer[Float64, MutExternalOrigin](unsafe_from_address=Int(_alc(Int64(n_iter * 8)))) |
| 97 | var b = UnsafePointer[UInt8, MutExternalOrigin](unsafe_from_address=Int(_alc(Int64(max_wr * max_wc * 2)))) |
| 98 | var x = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(_alc(Int64(max_wc * 4)))) |
| 99 | var o = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(_alc(Int64(max_wr * 4)))) |
| 100 | var ff_x = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(_alc(Int64(max_wc * 4)))) |
| 101 | var lm_w = UnsafePointer[UInt8, MutExternalOrigin](unsafe_from_address=Int(_alc(Int64(4096 * NE * 2)))) |
| 102 | |
| 103 | # Fill weight buffer with deterministic pattern |
| 104 | for i in range(max_wr * max_wc): |
| 105 | b.store(i*2, UInt8(i & 0xFF)) |
| 106 | b.store(i*2+1, UInt8((i>>8) & 0xFF)) |
| 107 | for i in range(max_wc): |
| 108 | x.store(i, Float32(Float64(i % 100 - 50) * 0.01)) |
| 109 | ff_x.store(i, Float32(Float64(i % 100 - 50) * 0.01)) |
| 110 | for i in range(4096 * NE): |
| 111 | lm_w.store(i*2, UInt8((i*7) & 0xFF)) |
| 112 | lm_w.store(i*2+1, UInt8(((i*7)>>8) & 0xFF)) |
| 113 | |
| 114 | # Print JSON start |
| 115 | print("{") |
| 116 | print("\"mojollama_bench_v4\": {") |
| 117 | print(" \"arch\": {") |
| 118 | print(" \"n_embd\": " + String(NE) + ",\"n_heads\": " + String(NH) + ",\"n_kv_heads\": " + String(NK) + ",") |
| 119 | print(" \"head_dim\": " + String(HD) + ",\"n_layers\": " + String(NL) + ",\"ff_hidden\": " + String(FF) + ",") |
| 120 | print(" \"vocab_size\": " + String(NV) + ",\"threads\": " + String(NW)) |
| 121 | print(" },") |
| 122 | |
| 123 | # ═══ 1. COMPONENT BREAKDOWN ═══ |
| 124 | print(" \"breakdown\": {") |
| 125 | |
| 126 | # QKV projection: inner × NE |
| 127 | var t_qkv = bench_mean(b, x, o, inner, NE, NW, buf, n_warm, n_iter) |
| 128 | var ops_q = Int64(inner) * Int64(NE) * 2 |
| 129 | print(" \"Q_proj " + String(inner) + "x" + String(NE) + "\":{\"ms\":" + String(Float64(t_qkv)) + ",\"gflops\":" + String(Float64(Float64(ops_q)/t_qkv/1e6)) + "},") |
| 130 | |
| 131 | # KV projection: kv_dim × NE |
| 132 | var t_kv = bench_mean(b, x, ff_x, kv_dim, NE, NW, buf, n_warm, n_iter) |
| 133 | ops_q = Int64(kv_dim) * Int64(NE) * 2 |
| 134 | print(" \"KV_proj " + String(kv_dim) + "x" + String(NE) + "\":{\"ms\":" + String(Float64(t_kv)) + ",\"gflops\":" + String(Float64(Float64(ops_q)/t_kv/1e6)) + "},") |
| 135 | |
| 136 | # Output projection: NE × inner |
| 137 | var t_oproj = bench_mean(b, x, o, NE, inner, NW, buf, n_warm, n_iter) |
| 138 | ops_q = Int64(NE) * Int64(inner) * 2 |
| 139 | print(" \"O_proj " + String(NE) + "x" + String(inner) + "\":{\"ms\":" + String(Float64(t_oproj)) + ",\"gflops\":" + String(Float64(Float64(ops_q)/t_oproj/1e6)) + "},") |
| 140 | |
| 141 | # FFN gate/up: FF × NE |
| 142 | var t_ffn_gate = bench_mean(b, ff_x, o, FF, NE, NW, buf, n_warm, n_iter) |
| 143 | ops_q = Int64(FF) * Int64(NE) * 2 |
| 144 | print(" \"FFN_gate " + String(FF) + "x" + String(NE) + "\":{\"ms\":" + String(Float64(t_ffn_gate)) + ",\"gflops\":" + String(Float64(Float64(ops_q)/t_ffn_gate/1e6)) + "},") |
| 145 | |
| 146 | # FFN up: same shape as gate |
| 147 | var t_ffn_up = bench_mean(b, ff_x, o, FF, NE, NW, buf, n_warm, n_iter) |
| 148 | print(" \"FFN_up " + String(FF) + "x" + String(NE) + "\":{\"ms\":" + String(Float64(t_ffn_up)) + ",\"gflops\":" + String(Float64(Float64(ops_q)/t_ffn_up/1e6)) + "},") |
| 149 | |
| 150 | # FFN down: NE × FF |
| 151 | var t_ffn_down = bench_mean(b, ff_x, o, NE, FF, NW, buf, n_warm, n_iter) |
| 152 | ops_q = Int64(NE) * Int64(FF) * 2 |
| 153 | print(" \"FFN_down " + String(NE) + "x" + String(FF) + "\":{\"ms\":" + String(Float64(t_ffn_down)) + ",\"gflops\":" + String(Float64(Float64(ops_q)/t_ffn_down/1e6)) + "},") |
| 154 | |
| 155 | # LM head (estimated: vocab_size × n_embd — too large to allocate, approximate) |
| 156 | # For 128256×4096, allocate a smaller representative sample |
| 157 | var lm_samples = 4096 # benchmark 4K rows, scale up |
| 158 | var t_lm = bench_mean(lm_w, x, o, lm_samples, NE, NW, buf, n_warm, n_iter) |
| 159 | ops_q = Int64(NV) * Int64(NE) * 2 |
| 160 | var lm_ms_est = t_lm * Float64(NV) / Float64(lm_samples) |
| 161 | print(" \"LM_head " + String(NV) + "x" + String(NE) + "\":{\"ms_est\":" + String(Float64(lm_ms_est)) + ",\"ms_samples\":" + String(Float64(t_lm)) + ",\"gflops\":" + String(Float64(Float64(ops_q)/lm_ms_est/1e6)) + "}") |
| 162 | |
| 163 | print(" },") |
| 164 | |
| 165 | # ═══ 2. FULL MODEL BENCHMARK ═══ |
| 166 | # Simulates NL layers: each layer = Q_proj + K_proj + V_proj + O_proj + FFN_gate + FFN_up + FFN_down |
| 167 | print(" \"full_model\": {") |
| 168 | for _ in range(n_warm): |
| 169 | for _ in range(NL): |
| 170 | var _ = f16_mm(b, x, o, inner, NE, NW) |
| 171 | var __ = f16_mm(b, x, ff_x, kv_dim, NE, NW) |
| 172 | var ___ = f16_mm(b, ff_x, o, kv_dim, NE, NW) |
| 173 | var ____ = f16_mm(b, x, o, NE, inner, NW) |
| 174 | var _____ = f16_mm(b, ff_x, o, FF, NE, NW) |
| 175 | var ______ = f16_mm(b, ff_x, o, FF, NE, NW) |
| 176 | var _______ = f16_mm(b, ff_x, o, NE, FF, NW) |
| 177 | var model_total: Float64 = 0.0 |
| 178 | for _ in range(n_iter): |
| 179 | var tm0 = time.perf_counter() |
| 180 | for _ in range(NL): |
| 181 | var _ = f16_mm(b, x, o, inner, NE, NW) |
| 182 | var __ = f16_mm(b, x, ff_x, kv_dim, NE, NW) |
| 183 | var ___ = f16_mm(b, ff_x, o, kv_dim, NE, NW) |
| 184 | var ____ = f16_mm(b, x, o, NE, inner, NW) |
| 185 | var _____ = f16_mm(b, ff_x, o, FF, NE, NW) |
| 186 | var ______ = f16_mm(b, ff_x, o, FF, NE, NW) |
| 187 | var _______ = f16_mm(b, ff_x, o, NE, FF, NW) |
| 188 | model_total += (time.perf_counter() - tm0) * 1000.0 |
| 189 | var full_ms = model_total / Float64(n_iter) |
| 190 | |
| 191 | # Total ops per layer: Q_proj + K_proj + V_proj + O_proj + FFN_gate + FFN_up + FFN_down |
| 192 | var ops_per_layer = Int64(inner)*Int64(NE)*2 + Int64(kv_dim)*Int64(NE)*4 + Int64(NE)*Int64(inner)*2 + Int64(FF)*Int64(NE)*4 + Int64(NE)*Int64(FF)*2 |
| 193 | var total_ops = Int64(NL) * ops_per_layer + Int64(NV) * Int64(NE) * 2 # +lm_head |
| 194 | var tflops = Float64(total_ops) / (full_ms / 1000.0) / 1e12 |
| 195 | |
| 196 | print(" \"n_layers\": " + String(NL) + ",") |
| 197 | print(" \"total_ms\":" + String(Float64(full_ms)) + ",") |
| 198 | print(" \"ms_per_layer\":" + String(Float64(full_ms / Float64(NL))) + ",") |
| 199 | print(" \"gflops\":" + String(Float64(tflops * 1000.0)) + ",") |
| 200 | print(" \"tflops\":" + String(Float64(tflops)) + ",") |
| 201 | print(" \"total_ops\":" + String(total_ops)) |
| 202 | print(" },") |
| 203 | |
| 204 | # ═══ 3. PER-LAYER DECODE TIMING ═══ |
| 205 | print(" \"decode_step\": [") |
| 206 | # Single-token decode: each matmul is a separate measurement |
| 207 | var d_q_proj = bench_mean(b, x, o, inner, NE, NW, buf, n_warm, n_iter) |
| 208 | var d_k_proj = bench_mean(b, x, ff_x, kv_dim, NE, NW, buf, n_warm, n_iter) |
| 209 | var d_v_proj = bench_mean(b, ff_x, o, kv_dim, NE, NW, buf, n_warm, n_iter) |
| 210 | var d_o_proj = bench_mean(b, x, o, NE, inner, NW, buf, n_warm, n_iter) |
| 211 | var d_f_gate = bench_mean(b, ff_x, o, FF, NE, NW, buf, n_warm, n_iter) |
| 212 | var d_f_up = bench_mean(b, ff_x, o, FF, NE, NW, buf, n_warm, n_iter) |
| 213 | var d_f_down = bench_mean(b, ff_x, o, NE, FF, NW, buf, n_warm, n_iter) |
| 214 | var d_layer_ms = d_q_proj + d_k_proj + d_v_proj + d_o_proj + d_f_gate + d_f_up + d_f_down |
| 215 | |
| 216 | print(" {\"component\":\"Q_proj\",\"ms\":" + String(Float64(d_q_proj)) + "},") |
| 217 | print(" {\"component\":\"K_proj\",\"ms\":" + String(Float64(d_k_proj)) + "},") |
| 218 | print(" {\"component\":\"V_proj\",\"ms\":" + String(Float64(d_v_proj)) + "},") |
| 219 | print(" {\"component\":\"O_proj\",\"ms\":" + String(Float64(d_o_proj)) + "},") |
| 220 | print(" {\"component\":\"FFN_gate\",\"ms\":" + String(Float64(d_f_gate)) + "},") |
| 221 | print(" {\"component\":\"FFN_up\",\"ms\":" + String(Float64(d_f_up)) + "},") |
| 222 | print(" {\"component\":\"FFN_down\",\"ms\":" + String(Float64(d_f_down)) + "},") |
| 223 | print(" {\"component\":\"TOTAL_LAYER\",\"ms\":" + String(Float64(d_layer_ms)) + "},") |
| 224 | print(" {\"component\":\"tok/s\",\"val\":" + String(Float64(1000.0 / d_layer_ms)) + "}") |
| 225 | print(" ],") |
| 226 | |
| 227 | # ═══ 4. THREAD SWEEP ═══ |
| 228 | print(" \"thread_sweep\": [") |
| 229 | var tw_first = True |
| 230 | for tw in [1, 2, 4, 8, 16, 24, 32]: |
| 231 | var t_w = bench_mean(b, x, o, NE, NE, tw, buf, n_warm, n_iter // 2) |
| 232 | if not tw_first: print(",") |
| 233 | tw_first = False |
| 234 | var tok = 1000.0 / t_w |
| 235 | print(" {\"threads\":" + String(tw) + ",\"qkv_ms\":" + String(Float64(t_w)) + ",\"tok_s\":" + String(Float64(tok)) + "}") |
| 236 | print() |
| 237 | print(" ],") |
| 238 | |
| 239 | # ═══ 5. BATCH SWEEP ═══ |
| 240 | # Measured as sequential tokens (same weight matrix, bs different inputs) |
| 241 | # This avoids allocating an oversized weight buffer |
| 242 | print(" \"batch_sweep\": [") |
| 243 | tw_first = True |
| 244 | for bs in [1, 2, 4, 8, 16]: |
| 245 | # Sum of bs sequential matmuls simulates batch decode |
| 246 | var t0 = time.perf_counter() |
| 247 | for _ in range(bs): |
| 248 | var _ = f16_mm(b, x, o, NE, NE, NW) |
| 249 | var t_b = (time.perf_counter() - t0) * 1000.0 |
| 250 | if not tw_first: print(",") |
| 251 | tw_first = False |
| 252 | var agg_tok = Float64(bs) / (t_b / 1000.0) |
| 253 | print(" {\"batch\":" + String(bs) + ",\"ms\":" + String(Float64(t_b)) + ",\"agg_tok_s\":" + String(Float64(agg_tok)) + "}") |
| 254 | print() |
| 255 | print(" ],") |
| 256 | |
| 257 | # ═══ 6. MEMORY BANDWIDTH ═══ |
| 258 | # For a matmul of M×N, we read M*N*2 bytes (weights) + N*4 bytes (input) and write M*4 bytes (output) |
| 259 | # Bandwidth = (bytes_read + bytes_written) / time |
| 260 | print(" \"memory_bandwidth\": {") |
| 261 | var mem_bw_qkv = Float64(NE * NE * 2 + NE * 4 + NE * 4) / (d_q_proj / 1000.0) / 1e9 |
| 262 | var mem_bw_ffn = Float64(FF * NE * 2 + NE * 4 + FF * 4) / (d_f_gate / 1000.0) / 1e9 |
| 263 | print(" \"Q_proj_GB_s\":" + String(Float64(mem_bw_qkv)) + ",") |
| 264 | print(" \"FFN_gate_GB_s\":" + String(Float64(mem_bw_ffn)) + ",") |
| 265 | print(" \"peak_Q_proj_tok_s\":" + String(Float64(mem_bw_qkv * 1e9 / Float64(NE * NE * 2 + NE * 4 + NE * 4) * 1000.0))) |
| 266 | print(" },") |
| 267 | |
| 268 | # ═══ 5. SUMMARY ═══ |
| 269 | print(" \"summary\": {") |
| 270 | # Decode tokens/s using actual layer timing |
| 271 | print(" \"decode_tok_s\": " + String(Float64(1000.0 / d_layer_ms)) + ",") |
| 272 | # Full model tok/s = 1 token / per-layer time |
| 273 | print(" \"model_tok_s\": " + String(Float64(1000.0 / (full_ms / Float64(NL)))) + ",") |
| 274 | print(" \"peak_tflops\": " + String(Float64(tflops)) + ",") |
| 275 | print(" \"ms_per_layer\": " + String(Float64(d_layer_ms)) + ",") |
| 276 | print(" \"bottleneck\": \"Q_proj " + String(inner) + "x" + String(NE) + " matmul\"") |
| 277 | print(" }") |
| 278 | print("}}") |