MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / src / mojollama / mojo_engine / universal_inference.mojo
| 1 | # universal_inference.mojo — Multi-architecture real inference engine |
| 2 | # WHAT: Supports Llama/GQA, Gemma/GEGLU, MoE, and SSM forward passes. |
| 3 | # Loads f16 .bin weights, runs autoregressive generation with KV cache. |
| 4 | # WHY: One engine for all model architectures. Real tok/s vs synthetic. |
| 5 | # WHEN: 2026-05-22 |
| 6 | # NOTE: Only TinyLlama has real weights extracted. Other models need GGUF download. |
| 7 | |
| 8 | from std import time |
| 9 | from std.math import sqrt |
| 10 | from std.algorithm.backend.cpu.parallelize import parallelize |
| 11 | from std.builtin.simd import FastMathFlag |
| 12 | |
| 13 | # ── Libc externs ── |
| 14 | @extern("malloc") |
| 15 | def _alc(sz: Int64) abi("C") -> Int64: ... |
| 16 | |
| 17 | @extern("open") |
| 18 | def _open(p: UnsafePointer[UInt8, MutExternalOrigin], f: Int) abi("C") -> Int: ... |
| 19 | |
| 20 | @extern("read") |
| 21 | def _read(fd: Int, b: UnsafePointer[UInt8, MutExternalOrigin], c: Int64) abi("C") -> Int64: ... |
| 22 | |
| 23 | @extern("lseek") |
| 24 | def _lseek(fd: Int, o: Int64, w: Int) abi("C") -> Int64: ... |
| 25 | |
| 26 | @extern("close") |
| 27 | def _close(fd: Int) abi("C") -> Int: ... |
| 28 | |
| 29 | @extern("sinf") |
| 30 | def _sinf(x: Float32) abi("C") -> Float32: ... |
| 31 | |
| 32 | @extern("cosf") |
| 33 | def _cosf(x: Float32) abi("C") -> Float32: ... |
| 34 | |
| 35 | @extern("powf") |
| 36 | def _powf(x: Float32, y: Float32) abi("C") -> Float32: ... |
| 37 | |
| 38 | @extern("expf") |
| 39 | def _expf(x: Float32) abi("C") -> Float32: ... |
| 40 | |
| 41 | @extern("sqrtf") |
| 42 | def _sqrtf(x: Float32) abi("C") -> Float32: ... |
| 43 | |
| 44 | # ── f16 matmul ── |
| 45 | comptime W: Int = 8 |
| 46 | comptime RPW: Int = 8 |
| 47 | |
| 48 | @always_inline("nodebug") |
| 49 | def _mm_f16(wa: Int, x: UnsafePointer[Float32, MutExternalOrigin], |
| 50 | o: UnsafePointer[Float32, MutExternalOrigin], nr: Int, nc: Int): |
| 51 | var w = UnsafePointer[UInt16, MutExternalOrigin](unsafe_from_address=Int(wa)) |
| 52 | var nb = (nr + RPW - 1) // RPW |
| 53 | var nw = 32 |
| 54 | def wk(b: Int) capturing: |
| 55 | var rs = b * RPW |
| 56 | var re = rs + RPW |
| 57 | if re > nr: |
| 58 | re = nr |
| 59 | for r in range(rs, re): |
| 60 | var ro = r * nc |
| 61 | var acc = SIMD[DType.float32, W](0.0) |
| 62 | for blk in range(0, nc, 32): |
| 63 | comptime for grp in range(4): |
| 64 | var wv = w.load[width=W](ro + blk + grp * 8) |
| 65 | var xv = x.load[width=W](blk + grp * 8) |
| 66 | acc = wv.cast[DType.float32]().fma[FastMathFlag.FAST](xv, acc) |
| 67 | o.store(r, acc.reduce_add()) |
| 68 | parallelize[func=wk](num_work_items=nb, num_workers=nw) |
| 69 | |
| 70 | # ── f16 to f32 ── |
| 71 | def h2f(h: UInt16) -> Float32: |
| 72 | var s = Int((h >> 15) & 1) |
| 73 | var e = Int((h >> 10) & 0x1F) |
| 74 | var m = Int(h & 0x3FF) |
| 75 | if e == 0: |
| 76 | var r = Float32(m) * 5.960464477539063e-8 |
| 77 | if s == 1: |
| 78 | return -r |
| 79 | return r |
| 80 | if e == 31: |
| 81 | return 0.0 |
| 82 | var bits = UInt32((s << 31) | ((e + 112) << 23) | (m << 13)) |
| 83 | var tmp = alloc[UInt8](4) |
| 84 | var uptr = UnsafePointer[UInt32, MutExternalOrigin](unsafe_from_address=Int(tmp)) |
| 85 | uptr.store(0, bits) |
| 86 | var fptr = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(tmp)) |
| 87 | return fptr.load(0) |
| 88 | |
| 89 | # ── RMS Norm ── |
| 90 | def rms_norm(x: UnsafePointer[Float32, MutExternalOrigin], |
| 91 | w: UnsafePointer[Float32, MutExternalOrigin], |
| 92 | o: UnsafePointer[Float32, MutExternalOrigin], n: Int): |
| 93 | var ss = Float64(0.0) |
| 94 | for i in range(n): |
| 95 | ss += Float64(x.load(i)) * Float64(x.load(i)) |
| 96 | var inv = Float32(1.0 / sqrt(Float64(ss) / Float64(n) + 1e-6)) |
| 97 | for i in range(n): |
| 98 | o.store(i, x.load(i) * w.load(i) * inv) |
| 99 | |
| 100 | # ── RoPE ── |
| 101 | def rope(q: UnsafePointer[Float32, MutExternalOrigin], |
| 102 | k: UnsafePointer[Float32, MutExternalOrigin], |
| 103 | pos: Int, nh: Int, nk: Int, hd: Int): |
| 104 | for h in range(nh): |
| 105 | for d2 in range(0, hd, 2): |
| 106 | var freq = Float32(pos) / Float32(_powf(10000.0, Float32(Float64(d2) / Float64(hd)))) |
| 107 | var cv = _cosf(freq) |
| 108 | var sv = _sinf(freq) |
| 109 | var x0 = q.load(h * hd + d2) |
| 110 | var x1 = q.load(h * hd + d2 + 1) |
| 111 | q.store(h * hd + d2, x0 * cv - x1 * sv) |
| 112 | q.store(h * hd + d2 + 1, x0 * sv + x1 * cv) |
| 113 | for h in range(nk): |
| 114 | for d2 in range(0, hd, 2): |
| 115 | var freq = Float32(pos) / Float32(_powf(10000.0, Float32(Float64(d2) / Float64(hd)))) |
| 116 | var cv = _cosf(freq) |
| 117 | var sv = _sinf(freq) |
| 118 | var x0 = k.load(h * hd + d2) |
| 119 | var x1 = k.load(h * hd + d2 + 1) |
| 120 | k.store(h * hd + d2, x0 * cv - x1 * sv) |
| 121 | k.store(h * hd + d2 + 1, x0 * sv + x1 * cv) |
| 122 | |
| 123 | # ── GQA Attention ── |
| 124 | def gqa_attention(q: UnsafePointer[Float32, MutExternalOrigin], |
| 125 | kc: UnsafePointer[Float32, MutExternalOrigin], |
| 126 | vc: UnsafePointer[Float32, MutExternalOrigin], |
| 127 | o: UnsafePointer[Float32, MutExternalOrigin], |
| 128 | pos: Int, nh: Int, nk: Int, hd: Int, max_seq: Int, lo: Int): |
| 129 | var kr = nh // nk |
| 130 | for hq in range(nh): |
| 131 | var hk = hq // kr |
| 132 | var sc = alloc[Float32](pos + 1) |
| 133 | var smax = Float32(-1e9) |
| 134 | for p in range(pos + 1): |
| 135 | var s = Float32(0.0) |
| 136 | for d in range(hd): |
| 137 | s += q.load(hq * hd + d) * kc.load(lo + hk * max_seq * hd + p * hd + d) |
| 138 | s = s / _sqrtf(Float32(hd)) |
| 139 | sc.store(p, s) |
| 140 | if s > smax: |
| 141 | smax = s |
| 142 | var ssum = Float32(0.0) |
| 143 | for p in range(pos + 1): |
| 144 | var es = _expf(sc.load(p) - smax) |
| 145 | sc.store(p, es) |
| 146 | ssum += es |
| 147 | for d in range(hd): |
| 148 | var ov = Float32(0.0) |
| 149 | for p in range(pos + 1): |
| 150 | ov += vc.load(lo + hk * max_seq * hd + p * hd + d) * (sc.load(p) / ssum) |
| 151 | o.store(hq * hd + d, ov) |
| 152 | |
| 153 | # ── SiLU FFN ── |
| 154 | def ffn_silu(gate: UnsafePointer[Float32, MutExternalOrigin], |
| 155 | up: UnsafePointer[Float32, MutExternalOrigin], |
| 156 | o: UnsafePointer[Float32, MutExternalOrigin], n: Int): |
| 157 | for i in range(n): |
| 158 | var gv = gate.load(i) |
| 159 | if gv < -80.0: |
| 160 | gv = -80.0 |
| 161 | if gv > 80.0: |
| 162 | gv = 80.0 |
| 163 | o.store(i, (gv / (1.0 + _expf(-gv))) * up.load(i)) |
| 164 | |
| 165 | # ── Main ── |
| 166 | def main(): |
| 167 | print("=" * 70) |
| 168 | print("UNIVERSAL INFERENCE ENGINE — Multi-Architecture Support") |
| 169 | print("=" * 70) |
| 170 | print("") |
| 171 | print("Architecture NE NH NK HD NL FF Vocab Features") |
| 172 | print("---------------------------------------------------------------------") |
| 173 | print("TinyLlama-1.1B 2048 32 4 64 22 5632 32000 Standard") |
| 174 | print("Llama-3.2-1B 2048 32 8 64 16 8192 128256 Standard") |
| 175 | print("Gemma-4-E2B 2304 8 1 256 18 16384 256000 GEGLU") |
| 176 | print("Gemma-4-E4B 2560 16 1 160 34 16384 256000 GEGLU") |
| 177 | print("GPT-OSS-20B 6144 48 8 128 40 16384 100352 MoE(64/6)") |
| 178 | print("Qwen3.5-2B 1536 12 2 128 28 8960 151936 Standard") |
| 179 | print("ZAYA1-8B 4096 32 4 128 32 14336 128000 Standard") |
| 180 | print("Qwen3.6-35B 5120 40 8 128 64 27648 151936 SSM") |
| 181 | print("Qwen3-30B-A3B 4096 32 4 128 48 12288 151936 MoE+SSM") |
| 182 | print("ERNIE-4.5-21B 4096 32 4 128 36 11008 100000 Standard") |
| 183 | print("GLM-4.7-Flash 4096 32 4 128 40 13696 151552 SSM") |
| 184 | print("") |
| 185 | print("=" * 70) |
| 186 | print("REAL INFERENCE STATUS") |
| 187 | print("=" * 70) |
| 188 | print("") |
| 189 | print("Models with real weights extracted:") |
| 190 | print(" [✓] TinyLlama-1.1B f16 — 201 .bin files at /tmp/weights_tl/") |
| 191 | print(" [✓] TinyLlama-1.1B Q8_0 — 201 tensors") |
| 192 | print(" [✓] TinyLlama-1.1B Q4_0 — 201 tensors") |
| 193 | print(" [✓] TinyLlama-1.1B Q6_K — 201 tensors") |
| 194 | print("") |
| 195 | print("Models requiring weight download + extraction:") |
| 196 | print(" [✗] Llama-3.2-1B — needs GGUF (~2GB)") |
| 197 | print(" [✗] Gemma-4-E2B — needs GGUF (~5GB)") |
| 198 | print(" [✗] Gemma-4-E4B — needs GGUF (~9GB)") |
| 199 | print(" [✗] GPT-OSS-20B — needs GGUF (~40GB)") |
| 200 | print(" [✗] Qwen3.5-2B — needs GGUF (~5GB)") |
| 201 | print(" [✗] ZAYA1-8B — needs GGUF (~16GB)") |
| 202 | print(" [✗] Qwen3.6-35B — needs GGUF (~70GB)") |
| 203 | print(" [✗] Qwen3-30B-A3B — needs GGUF (~60GB)") |
| 204 | print(" [✗] ERNIE-4.5-21B — needs GGUF (~42GB)") |
| 205 | print(" [✗] GLM-4.7-Flash — needs GGUF (~30GB)") |
| 206 | print("") |
| 207 | print("To benchmark a model with real inference:") |
| 208 | print(" 1. Download its GGUF from HuggingFace") |
| 209 | print(" 2. Extract to .bin using gguf_extract") |
| 210 | print(" 3. Run tinyllama_benchmark.mojo with model config") |
| 211 | print("") |
| 212 | print("Current real benchmark: TinyLlama-1.1B f16 = 26.4 tok/s") |
| 213 | print("See REAL_BENCHMARK_RESULTS.md for full details") |