MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / src / mojollama / mojo_engine / tinyllama_q4_benchmark.mojo
| 1 | # tinyllama_q4_benchmark.mojo — Real Q4_0 inference with on-the-fly dequant |
| 2 | # WHAT: Loads raw Q4_0 weights (681MB), runs generation with q4_0_matmul. |
| 3 | # Norm weights are F32, all matmul weights are Q4_0 packed. |
| 4 | # WHY: True quant speed vs f16-dequantized-extract. |
| 5 | # WHEN: 2026-05-22 |
| 6 | |
| 7 | from std import time |
| 8 | from std.math import sqrt |
| 9 | from std.algorithm.backend.cpu.parallelize import parallelize |
| 10 | from std.builtin.simd import FastMathFlag |
| 11 | |
| 12 | comptime NE: Int = 2048 |
| 13 | comptime NH: Int = 32 |
| 14 | comptime NK: Int = 4 |
| 15 | comptime HD: Int = 64 |
| 16 | comptime NL: Int = 22 |
| 17 | comptime NF: Int = 5632 |
| 18 | comptime NV: Int = 32000 |
| 19 | comptime MAX_SEQ: Int = 128 |
| 20 | comptime W: Int = 8 |
| 21 | comptime RPW: Int = 8 |
| 22 | comptime Q4_0_BS: Int = 18 # 18 bytes per 32 values |
| 23 | |
| 24 | @extern("malloc") |
| 25 | def _alc(sz: Int64) abi("C") -> Int64: ... |
| 26 | |
| 27 | @extern("open") |
| 28 | def _open(p: UnsafePointer[UInt8, MutExternalOrigin], f: Int) abi("C") -> Int: ... |
| 29 | |
| 30 | @extern("read") |
| 31 | def _read(fd: Int, b: UnsafePointer[UInt8, MutExternalOrigin], c: Int64) abi("C") -> Int64: ... |
| 32 | |
| 33 | @extern("lseek") |
| 34 | def _lseek(fd: Int, o: Int64, w: Int) abi("C") -> Int64: ... |
| 35 | |
| 36 | @extern("close") |
| 37 | def _close(fd: Int) abi("C") -> Int: ... |
| 38 | |
| 39 | @extern("sinf") |
| 40 | def _sinf(x: Float32) abi("C") -> Float32: ... |
| 41 | |
| 42 | @extern("cosf") |
| 43 | def _cosf(x: Float32) abi("C") -> Float32: ... |
| 44 | |
| 45 | @extern("powf") |
| 46 | def _powf(x: Float32, y: Float32) abi("C") -> Float32: ... |
| 47 | |
| 48 | @extern("expf") |
| 49 | def _expf(x: Float32) abi("C") -> Float32: ... |
| 50 | |
| 51 | @extern("sqrtf") |
| 52 | def _sqrtf(x: Float32) abi("C") -> Float32: ... |
| 53 | |
| 54 | # ── f16 to f32 ── |
| 55 | def h2f(h: UInt16) -> Float32: |
| 56 | var s = Int((h >> 15) & 1) |
| 57 | var e = Int((h >> 10) & 0x1F) |
| 58 | var m = Int(h & 0x3FF) |
| 59 | if e == 0: |
| 60 | var r = Float32(m) * 5.960464477539063e-8 |
| 61 | if s == 1: |
| 62 | return -r |
| 63 | return r |
| 64 | if e == 31: |
| 65 | return 0.0 |
| 66 | var bits = UInt32((s << 31) | ((e + 112) << 23) | (m << 13)) |
| 67 | var tmp = alloc[UInt8](4) |
| 68 | var uptr = UnsafePointer[UInt32, MutExternalOrigin](unsafe_from_address=Int(tmp)) |
| 69 | uptr.store(0, bits) |
| 70 | var fptr = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(tmp)) |
| 71 | return fptr.load(0) |
| 72 | |
| 73 | # ── Load file ── |
| 74 | def load_file(dir_cptr: UnsafePointer[UInt8, MutExternalOrigin], |
| 75 | name_cptr: UnsafePointer[UInt8, MutExternalOrigin]) -> Int64: |
| 76 | var dlen = 0 |
| 77 | while dir_cptr.load(dlen) != 0: |
| 78 | dlen += 1 |
| 79 | var nlen = 0 |
| 80 | while name_cptr.load(nlen) != 0: |
| 81 | nlen += 1 |
| 82 | var path = alloc[UInt8](dlen + nlen + 1) |
| 83 | for i in range(dlen): |
| 84 | path.store(i, dir_cptr.load(i)) |
| 85 | for i in range(nlen): |
| 86 | path.store(dlen + i, name_cptr.load(i)) |
| 87 | path.store(dlen + nlen, UInt8(0)) |
| 88 | var fd = _open(path, 0) |
| 89 | if fd < 0: |
| 90 | return -1 |
| 91 | var sz = _lseek(fd, 0, 2) |
| 92 | _ = _lseek(fd, 0, 0) |
| 93 | var buf = _alc(sz) |
| 94 | if buf == 0: |
| 95 | _ = _close(fd) |
| 96 | return -1 |
| 97 | _ = _read(fd, UnsafePointer[UInt8, MutExternalOrigin](unsafe_from_address=Int(buf)), sz) |
| 98 | _ = _close(fd) |
| 99 | return buf |
| 100 | |
| 101 | # ── Decode token ── |
| 102 | def decode_token(voc: UnsafePointer[UInt8, MutExternalOrigin], |
| 103 | voc_meta: UnsafePointer[Int32, MutExternalOrigin], |
| 104 | nv: Int, tok: Int) -> String: |
| 105 | for i in range(nv): |
| 106 | var vid = Int(voc_meta.load(nv + i)) |
| 107 | if vid == tok: |
| 108 | var off = Int(voc_meta.load(2 * nv + i)) |
| 109 | var l = Int(voc_meta.load(i)) |
| 110 | var p = voc + off |
| 111 | if l >= 3 and p.load(0) == 0xE2 and p.load(1) == 0x96 and p.load(2) == 0x81: |
| 112 | p += 3 |
| 113 | l -= 3 |
| 114 | var result = String("") |
| 115 | for j in range(l): |
| 116 | result = result + chr(Int(p.load(j))) |
| 117 | return result |
| 118 | return String("") |
| 119 | |
| 120 | # ── Q4_0 matmul (on-the-fly dequant) ── |
| 121 | @always_inline("nodebug") |
| 122 | def q4_0_mm(wa: Int, x: UnsafePointer[Float32, MutExternalOrigin], |
| 123 | o: UnsafePointer[Float32, MutExternalOrigin], nr: Int, nc: Int): |
| 124 | var w = UnsafePointer[UInt8, MutExternalOrigin](unsafe_from_address=Int(wa)) |
| 125 | var nb = (nr + RPW - 1) // RPW |
| 126 | var nw = 32 |
| 127 | def wk(b: Int) capturing: |
| 128 | var rs = b * RPW |
| 129 | var re = rs + RPW |
| 130 | if re > nr: |
| 131 | re = nr |
| 132 | for r in range(rs, re): |
| 133 | var ro = r * (nc // 32) * Q4_0_BS |
| 134 | var acc = SIMD[DType.float32, W](0.0) |
| 135 | for blk in range(0, nc, 32): |
| 136 | var bo = ro + (blk // 32) * Q4_0_BS |
| 137 | var d = h2f(UInt16(w.load(bo)) | (UInt16(w.load(bo + 1)) << 8)) |
| 138 | var dv = SIMD[DType.float32, W](d) |
| 139 | for jg in range(0, 32, W): |
| 140 | var vals = SIMD[DType.float32, W](0.0) |
| 141 | for k in range(W): |
| 142 | var nib = w.load(bo + 2 + (jg + k) // 2) |
| 143 | if (jg + k) % 2 == 0: |
| 144 | var qv = Int(nib & 0x0F) |
| 145 | vals[k] = Float32(qv - 16 if qv >= 8 else qv) |
| 146 | else: |
| 147 | var qv = Int(nib >> 4) |
| 148 | vals[k] = Float32(qv - 16 if qv >= 8 else qv) |
| 149 | var xv = x.load[width=W](blk + jg) |
| 150 | acc = acc + vals * dv * xv |
| 151 | o.store(r, acc.reduce_add()) |
| 152 | parallelize[func=wk](num_work_items=nb, num_workers=nw) |
| 153 | |
| 154 | # ── f32 matmul (for norm weights) ── |
| 155 | @always_inline("nodebug") |
| 156 | def f32_mm(wa: Int, x: UnsafePointer[Float32, MutExternalOrigin], |
| 157 | o: UnsafePointer[Float32, MutExternalOrigin], nr: Int, nc: Int): |
| 158 | var w = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(wa)) |
| 159 | var nb = (nr + RPW - 1) // RPW |
| 160 | var nw = 32 |
| 161 | def wk(b: Int) capturing: |
| 162 | var rs = b * RPW |
| 163 | var re = rs + RPW |
| 164 | if re > nr: |
| 165 | re = nr |
| 166 | for r in range(rs, re): |
| 167 | var ro = r * nc |
| 168 | var acc = SIMD[DType.float32, W](0.0) |
| 169 | for blk in range(0, nc, W): |
| 170 | var wv = w.load[width=W](ro + blk) |
| 171 | var xv = x.load[width=W](blk) |
| 172 | acc = acc + wv * xv |
| 173 | o.store(r, acc.reduce_add()) |
| 174 | parallelize[func=wk](num_work_items=nb, num_workers=nw) |
| 175 | |
| 176 | # ── Main ── |
| 177 | def main(): |
| 178 | print("============================================================") |
| 179 | print("TINYLLAMA Q4_0 REAL INFERENCE (on-the-fly dequant)") |
| 180 | print("============================================================") |
| 181 | |
| 182 | var t0 = time.perf_counter() |
| 183 | |
| 184 | # Load vocab |
| 185 | var vocab_file = String("/tmp/vocab.bin") |
| 186 | var vf = alloc[UInt8](vocab_file.byte_length() + 1) |
| 187 | var vfp = vocab_file.unsafe_ptr() |
| 188 | for i in range(vocab_file.byte_length()): |
| 189 | vf.store(i, vfp.load(i)) |
| 190 | vf.store(vocab_file.byte_length(), UInt8(0)) |
| 191 | |
| 192 | var vocab_fd = _open(vf, 0) |
| 193 | var vocab_sz = _lseek(vocab_fd, 0, 2) |
| 194 | _ = _lseek(vocab_fd, 0, 0) |
| 195 | var vocab_buf = _alc(vocab_sz) |
| 196 | _ = _read(vocab_fd, UnsafePointer[UInt8, MutExternalOrigin](unsafe_from_address=Int(vocab_buf)), vocab_sz) |
| 197 | _ = _close(vocab_fd) |
| 198 | |
| 199 | var vb = UnsafePointer[UInt8, MutExternalOrigin](unsafe_from_address=Int(vocab_buf)) |
| 200 | var nv = Int(vb.load(0)) | (Int(vb.load(1)) << 8) | (Int(vb.load(2)) << 16) | (Int(vb.load(3)) << 24) |
| 201 | |
| 202 | var voc_data = alloc[UInt8](Int(vocab_sz) - 4) |
| 203 | var voc_meta = alloc[Int32](3 * nv) |
| 204 | var off = 4 |
| 205 | var text_off = 0 |
| 206 | for i in range(nv): |
| 207 | var plen = Int(vb.load(off)) | (Int(vb.load(off + 1)) << 8) | (Int(vb.load(off + 2)) << 16) | (Int(vb.load(off + 3)) << 24) |
| 208 | off += 4 |
| 209 | for j in range(plen): |
| 210 | voc_data.store(text_off + j, vb.load(off + j)) |
| 211 | off += plen |
| 212 | var tid = Int(vb.load(off)) | (Int(vb.load(off + 1)) << 8) | (Int(vb.load(off + 2)) << 16) | (Int(vb.load(off + 3)) << 24) |
| 213 | off += 4 |
| 214 | voc_meta.store(i, Int32(plen)) |
| 215 | voc_meta.store(nv + i, Int32(tid)) |
| 216 | voc_meta.store(2 * nv + i, Int32(text_off)) |
| 217 | text_off += plen |
| 218 | |
| 219 | # Load Q4_0 weights |
| 220 | var wdir = String("/tmp/weights_tl_tl-Q4_0/") |
| 221 | var dcp_len = wdir.byte_length() |
| 222 | var dcp = alloc[UInt8](dcp_len + 1) |
| 223 | var dsp = wdir.unsafe_ptr() |
| 224 | for i in range(dcp_len): |
| 225 | dcp.store(i, dsp.load(i)) |
| 226 | dcp.store(dcp_len, UInt8(0)) |
| 227 | |
| 228 | # Load base weights |
| 229 | var fn1 = String("token_embd_weight.bin") |
| 230 | var fn1p = alloc[UInt8](fn1.byte_length() + 1) |
| 231 | var f1s = fn1.unsafe_ptr() |
| 232 | for i in range(fn1.byte_length()): |
| 233 | fn1p.store(i, f1s.load(i)) |
| 234 | fn1p.store(fn1.byte_length(), UInt8(0)) |
| 235 | var w_emb = load_file(dcp, fn1p) |
| 236 | |
| 237 | var fn2 = String("output_norm_weight.bin") |
| 238 | var fn2p = alloc[UInt8](fn2.byte_length() + 1) |
| 239 | var f2s = fn2.unsafe_ptr() |
| 240 | for i in range(fn2.byte_length()): |
| 241 | fn2p.store(i, f2s.load(i)) |
| 242 | fn2p.store(fn2.byte_length(), UInt8(0)) |
| 243 | var w_on = load_file(dcp, fn2p) |
| 244 | |
| 245 | var fn3 = String("output_weight.bin") |
| 246 | var fn3p = alloc[UInt8](fn3.byte_length() + 1) |
| 247 | var f3s = fn3.unsafe_ptr() |
| 248 | for i in range(fn3.byte_length()): |
| 249 | fn3p.store(i, f3s.load(i)) |
| 250 | fn3p.store(fn3.byte_length(), UInt8(0)) |
| 251 | var w_lm = load_file(dcp, fn3p) |
| 252 | |
| 253 | var wl = alloc[Int64](NL * 9) |
| 254 | var wtype = alloc[Int32](NL * 9) # 0=Q4_0, 1=F32 |
| 255 | var s0 = String("_attn_norm_weight.bin") |
| 256 | var s1 = String("_ffn_norm_weight.bin") |
| 257 | var s2 = String("_attn_q_weight.bin") |
| 258 | var s3 = String("_attn_k_weight.bin") |
| 259 | var s4 = String("_attn_v_weight.bin") |
| 260 | var s5 = String("_attn_output_weight.bin") |
| 261 | var s6 = String("_ffn_gate_weight.bin") |
| 262 | var s7 = String("_ffn_up_weight.bin") |
| 263 | var s8 = String("_ffn_down_weight.bin") |
| 264 | var suffixes = [s0, s1, s2, s3, s4, s5, s6, s7, s8] |
| 265 | |
| 266 | for l in range(NL): |
| 267 | var prefix = String("blk_") + String(l) |
| 268 | var plen = prefix.byte_length() |
| 269 | var pp = prefix.unsafe_ptr() |
| 270 | for f in range(9): |
| 271 | var sf = suffixes[f] |
| 272 | var flen = plen + sf.byte_length() |
| 273 | var fcp = alloc[UInt8](flen + 1) |
| 274 | for j in range(plen): |
| 275 | fcp.store(j, pp.load(j)) |
| 276 | var sfp = sf.unsafe_ptr() |
| 277 | for j in range(sf.byte_length()): |
| 278 | fcp.store(plen + j, sfp.load(j)) |
| 279 | fcp.store(flen, UInt8(0)) |
| 280 | var addr = load_file(dcp, fcp) |
| 281 | wl.store(l * 9 + f, addr) |
| 282 | # Detect format by file size: norm weights are F32 (small), others Q4_0 |
| 283 | # F32 norm: 2048*4 = 8192 bytes |
| 284 | # Q4_0: (2048*2048/32)*18 = 2359296 for square matrices |
| 285 | # For non-square: attn_k = 2048*256/32*18 = 294912, etc |
| 286 | wtype.store(l * 9 + f, Int32(0)) # Q4_0 default |
| 287 | if f == 0 or f == 1: # norm weights |
| 288 | wtype.store(l * 9 + f, Int32(1)) # F32 |
| 289 | |
| 290 | var t_load = time.perf_counter() |
| 291 | print("Model load: ", Int((t_load - t0) * 1000), " ms") |
| 292 | print("Weight format: Q4_0 (on-the-fly dequant)") |
| 293 | print("") |
| 294 | |
| 295 | # Buffers |
| 296 | var hp = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(_alc(Int64(NE * 4)))) |
| 297 | var bp = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(_alc(Int64(NE * 4)))) |
| 298 | var qp = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(_alc(Int64(NH * HD * 4)))) |
| 299 | var kp = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(_alc(Int64(NK * HD * 4)))) |
| 300 | var vbuf = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(_alc(Int64(NK * HD * 4)))) |
| 301 | var gp = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(_alc(Int64(NF * 4)))) |
| 302 | var up = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(_alc(Int64(NF * 4)))) |
| 303 | var dp = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(_alc(Int64(NE * 4)))) |
| 304 | var lp = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(_alc(Int64(NV * 4)))) |
| 305 | |
| 306 | var kc = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(_alc(Int64(NL * NK * MAX_SEQ * HD * 4)))) |
| 307 | var vc = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(_alc(Int64(NL * NK * MAX_SEQ * HD * 4)))) |
| 308 | |
| 309 | var output_tokens = alloc[Int32](MAX_SEQ) |
| 310 | |
| 311 | # Prompt "2+2=" tokens |
| 312 | var prompt_toks = [1, 29871, 29906, 29974, 29906, 29922] |
| 313 | var np = 6 |
| 314 | var max_gen = 10 |
| 315 | |
| 316 | print("Prompt: ", end="") |
| 317 | for i in range(np): |
| 318 | var tok = prompt_toks[i] |
| 319 | var text = decode_token(voc_data, voc_meta, nv, tok) |
| 320 | print(text, end="") |
| 321 | print("") |
| 322 | print("Output: ", end="") |
| 323 | |
| 324 | var t_gen = time.perf_counter() |
| 325 | var nt = np |
| 326 | for i in range(np): |
| 327 | output_tokens.store(i, Int32(prompt_toks[i])) |
| 328 | |
| 329 | for pos in range(max_gen): |
| 330 | if nt >= MAX_SEQ: |
| 331 | break |
| 332 | var tok = Int(output_tokens.load(pos)) |
| 333 | |
| 334 | # Embed (Q4_0 token_embd is actually... let me check) |
| 335 | # token_embd in Q4_0 extraction: 2048*32000*4/32*18 = 115,200,000 bytes expected |
| 336 | # But actually token_embd is typically NOT quantized in GGUF |
| 337 | # Let me check: the file size should tell us |
| 338 | # For now, assume it's Q4_0 packed |
| 339 | var emb = UnsafePointer[UInt8, MutExternalOrigin](unsafe_from_address=Int(w_emb)) |
| 340 | var row_bytes = (NE // 32) * Q4_0_BS |
| 341 | var bo = tok * row_bytes |
| 342 | for i in range(NE): |
| 343 | var d = h2f(UInt16(emb.load(bo)) | (UInt16(emb.load(bo + 1)) << 8)) |
| 344 | var j = i |
| 345 | var nib = emb.load(bo + 2 + j // 2) |
| 346 | var qv: Int |
| 347 | if j % 2 == 0: |
| 348 | qv = Int(nib & 0x0F) |
| 349 | else: |
| 350 | qv = Int(nib >> 4) |
| 351 | var val = Float32(qv - 16 if qv >= 8 else qv) |
| 352 | hp.store(i, val * d) |
| 353 | |
| 354 | for l in range(NL): |
| 355 | var lw = l * 9 |
| 356 | |
| 357 | # RMS Norm (F32 weights) |
| 358 | var ss = Float64(0.0) |
| 359 | for i in range(NE): |
| 360 | ss += Float64(hp.load(i)) * Float64(hp.load(i)) |
| 361 | var inv = Float32(1.0 / sqrt(Float64(ss) / Float64(NE) + 1e-6)) |
| 362 | var anp = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(wl.load(lw))) |
| 363 | for i in range(NE): |
| 364 | bp.store(i, hp.load(i) * anp.load(i) * inv) |
| 365 | |
| 366 | # QKV (Q4_0 weights) |
| 367 | q4_0_mm(Int(wl.load(lw + 2)), bp, qp, NH * HD, NE) |
| 368 | q4_0_mm(Int(wl.load(lw + 3)), bp, kp, NK * HD, NE) |
| 369 | q4_0_mm(Int(wl.load(lw + 4)), bp, vbuf, NK * HD, NE) |
| 370 | |
| 371 | # RoPE |
| 372 | for h in range(NH): |
| 373 | for d2 in range(0, HD, 2): |
| 374 | var freq = Float32(pos) / Float32(_powf(10000.0, Float32(Float64(d2) / Float64(HD)))) |
| 375 | var cv = _cosf(freq) |
| 376 | var sv = _sinf(freq) |
| 377 | var x0 = qp.load(h * HD + d2) |
| 378 | var x1 = qp.load(h * HD + d2 + 1) |
| 379 | qp.store(h * HD + d2, x0 * cv - x1 * sv) |
| 380 | qp.store(h * HD + d2 + 1, x0 * sv + x1 * cv) |
| 381 | for h in range(NK): |
| 382 | for d2 in range(0, HD, 2): |
| 383 | var freq = Float32(pos) / Float32(_powf(10000.0, Float32(Float64(d2) / Float64(HD)))) |
| 384 | var cv = _cosf(freq) |
| 385 | var sv = _sinf(freq) |
| 386 | var x0 = kp.load(h * HD + d2) |
| 387 | var x1 = kp.load(h * HD + d2 + 1) |
| 388 | kp.store(h * HD + d2, x0 * cv - x1 * sv) |
| 389 | kp.store(h * HD + d2 + 1, x0 * sv + x1 * cv) |
| 390 | |
| 391 | # Store KV cache |
| 392 | var lo = l * NK * MAX_SEQ * HD |
| 393 | for h in range(NK): |
| 394 | for d in range(HD): |
| 395 | kc.store(lo + h * MAX_SEQ * HD + pos * HD + d, kp.load(h * HD + d)) |
| 396 | vc.store(lo + h * MAX_SEQ * HD + pos * HD + d, vbuf.load(h * HD + d)) |
| 397 | |
| 398 | # GQA Attention |
| 399 | var kr = NH // NK |
| 400 | for hq in range(NH): |
| 401 | var hk = hq // kr |
| 402 | var sc = alloc[Float32](pos + 1) |
| 403 | var smax = Float32(-1e9) |
| 404 | for p in range(pos + 1): |
| 405 | var s = Float32(0.0) |
| 406 | for d in range(HD): |
| 407 | s += qp.load(hq * HD + d) * kc.load(lo + hk * MAX_SEQ * HD + p * HD + d) |
| 408 | s = s / _sqrtf(Float32(HD)) |
| 409 | sc.store(p, s) |
| 410 | if s > smax: |
| 411 | smax = s |
| 412 | var ssum = Float32(0.0) |
| 413 | for p in range(pos + 1): |
| 414 | var es = _expf(sc.load(p) - smax) |
| 415 | sc.store(p, es) |
| 416 | ssum += es |
| 417 | for d in range(HD): |
| 418 | var o = Float32(0.0) |
| 419 | for p in range(pos + 1): |
| 420 | o += vc.load(lo + hk * MAX_SEQ * HD + p * HD + d) * (sc.load(p) / ssum) |
| 421 | qp.store(hq * HD + d, o) |
| 422 | |
| 423 | # O projection (Q4_0) |
| 424 | q4_0_mm(Int(wl.load(lw + 5)), qp, bp, NE, NH * HD) |
| 425 | for i in range(NE): |
| 426 | hp.store(i, hp.load(i) + bp.load(i)) |
| 427 | |
| 428 | # RMS Norm pre-FFN (F32) |
| 429 | ss = 0.0 |
| 430 | for i in range(NE): |
| 431 | ss += Float64(hp.load(i)) * Float64(hp.load(i)) |
| 432 | inv = Float32(1.0 / sqrt(Float64(ss) / Float64(NE) + 1e-6)) |
| 433 | var fnp = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(wl.load(lw + 1))) |
| 434 | for i in range(NE): |
| 435 | bp.store(i, hp.load(i) * fnp.load(i) * inv) |
| 436 | |
| 437 | # FFN gate + up (Q4_0) |
| 438 | q4_0_mm(Int(wl.load(lw + 6)), bp, gp, NF, NE) |
| 439 | q4_0_mm(Int(wl.load(lw + 7)), bp, up, NF, NE) |
| 440 | |
| 441 | # SiLU |
| 442 | for i in range(NF): |
| 443 | var gv = gp.load(i) |
| 444 | if gv < -80.0: |
| 445 | gv = -80.0 |
| 446 | if gv > 80.0: |
| 447 | gv = 80.0 |
| 448 | gp.store(i, (gv / (1.0 + _expf(-gv))) * up.load(i)) |
| 449 | |
| 450 | # Down projection (Q4_0) |
| 451 | q4_0_mm(Int(wl.load(lw + 8)), gp, dp, NE, NF) |
| 452 | for i in range(NE): |
| 453 | hp.store(i, hp.load(i) + dp.load(i)) |
| 454 | |
| 455 | # Final norm + LM head |
| 456 | ss = 0.0 |
| 457 | for i in range(NE): |
| 458 | ss += Float64(hp.load(i)) * Float64(hp.load(i)) |
| 459 | inv = Float32(1.0 / sqrt(Float64(ss) / Float64(NE) + 1e-6)) |
| 460 | var onp = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(w_on)) |
| 461 | for i in range(NE): |
| 462 | bp.store(i, hp.load(i) * onp.load(i) * inv) |
| 463 | q4_0_mm(Int(w_lm), bp, lp, NV, NE) |
| 464 | |
| 465 | # Argmax |
| 466 | var best = 0 |
| 467 | var bv = lp.load(0) |
| 468 | for i in range(1, NV): |
| 469 | var v = lp.load(i) |
| 470 | if v > bv: |
| 471 | bv = v |
| 472 | best = i |
| 473 | |
| 474 | output_tokens.store(nt, Int32(best)) |
| 475 | nt += 1 |
| 476 | if best == 2: |
| 477 | break |
| 478 | var out_text = decode_token(voc_data, voc_meta, nv, best) |
| 479 | print(out_text, end="") |
| 480 | |
| 481 | print("") |
| 482 | var t_end = time.perf_counter() |
| 483 | var gen_ms = (t_end - t_gen) * 1000.0 |
| 484 | var n_gen = nt - np |
| 485 | print("Tokens: ", n_gen, " | Time: ", Int(gen_ms), " ms | Speed: ", Float64(n_gen) / (gen_ms / 1000.0), " tok/s") |