MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / src / mojollama / mojo_engine / test_mojo_kernels.mojo
| 1 | # MojoLlama Quant Kernels — Bench + Correctness Test |
| 2 | # WHAT: Verify all quant matmul types produce non-zero output. |
| 3 | # WHY: C library had type 1 (f16) as no-op → all zeros. Fix in Mojo kernels. |
| 4 | # WHEN: May 2026 — benchmark + correctness verification. |
| 5 | from std import time |
| 6 | from std.algorithm.backend.cpu.parallelize import parallelize |
| 7 | |
| 8 | # ── C bridge for pointer conversion ── |
| 9 | @extern("addr_to_f32") |
| 10 | def _af32(a: Int64) abi("C") -> UnsafePointer[Float32, MutExternalOrigin]: ... |
| 11 | @extern("addr_to_u8") |
| 12 | def _au8(a: Int64) abi("C") -> UnsafePointer[UInt8, MutExternalOrigin]: ... |
| 13 | @extern("mojo_alloc") |
| 14 | def _alc(sz: Int64) abi("C") -> Int64: ... |
| 15 | @extern("mojo_free") |
| 16 | def _free(p: Int64) abi("C") -> None: ... |
| 17 | @extern("load_weight") |
| 18 | def _lw(i: Int64) abi("C") -> Int64: ... |
| 19 | @extern("free_weight") |
| 20 | def _fw(p: Int64) abi("C") -> None: ... |
| 21 | @extern("mojo_set_threads") |
| 22 | def _st(n: Int) abi("C") -> None: ... |
| 23 | |
| 24 | # ── Quant constants ── |
| 25 | comptime Q5_0_BS: Int = 22 |
| 26 | comptime Q8_0_BS: Int = 34 |
| 27 | comptime Q4_K_BS: Int = 144 |
| 28 | comptime MXFP4_BS: Int = 17 |
| 29 | comptime W: Int = 8 |
| 30 | |
| 31 | def f16_to_f32(h: UInt16) -> Float32: |
| 32 | var s = UInt32(h >> 15); var e = UInt32((h >> 10) & 0x1F) |
| 33 | var m = UInt32(h & 0x3FF) |
| 34 | if e == 0: |
| 35 | if m == 0: return 0.0 |
| 36 | return Float32(Float64(m) * 5.960464477539063e-8) |
| 37 | if e == 31: return 0.0 |
| 38 | var r = Float32(m | 0x400) |
| 39 | var ei = Int(e) - 25 |
| 40 | if ei >= 0: |
| 41 | for _ in range(ei): r *= 2.0 |
| 42 | else: |
| 43 | for _ in range(-ei): r *= 0.5 |
| 44 | return -r if s != 0 else r |
| 45 | |
| 46 | # ── Q5_0 kernel ── |
| 47 | def q5_0_matmul(w: UnsafePointer[UInt8, MutExternalOrigin], |
| 48 | x: UnsafePointer[Float32, MutExternalOrigin], |
| 49 | o: UnsafePointer[Float32, MutExternalOrigin], |
| 50 | nr: Int, nc: Int): |
| 51 | var bpr = nc // 32 |
| 52 | def worker(r: Int) capturing -> None: |
| 53 | var ro = r * bpr * Q5_0_BS |
| 54 | var acc = SIMD[DType.float32, W](0.0) |
| 55 | for blk in range(bpr): |
| 56 | var bo = ro + blk * Q5_0_BS |
| 57 | var lo = UInt16(w.load(bo)); var hi = UInt16(w.load(bo + 1)) |
| 58 | var d = f16_to_f32(lo | (hi << 8)) |
| 59 | for ch in range(4): |
| 60 | var vals = SIMD[DType.float32, W](0.0) |
| 61 | for k in range(W): |
| 62 | var idx = ch * W + k |
| 63 | var p = w.load(bo + 6 + idx // 2) |
| 64 | var qh = w.load(bo + 2 + idx // 4) |
| 65 | var hs = 2 * (idx % 4) |
| 66 | var hb_u8 = (UInt8(qh) >> UInt8(hs)) & UInt8(1) |
| 67 | var nib = Int32(p >> 4) if idx % 2 == 1 else Int32(p & 0x0F) |
| 68 | if nib > 7: nib -= 16 |
| 69 | var v = nib + Int32(hb_u8) * 16 |
| 70 | if v > 15: v -= 32 |
| 71 | vals[k] = Float32(v) |
| 72 | var xb = blk * 32 + ch * W |
| 73 | var xv = x.load[width=W](xb) |
| 74 | acc = acc + vals * xv * d |
| 75 | o.store(r, acc.reduce_add()) |
| 76 | parallelize[func=worker](num_work_items=nr) |
| 77 | |
| 78 | # ── f16 kernel (the critical fix) ── |
| 79 | def f16_matmul(w: UnsafePointer[UInt8, MutExternalOrigin], |
| 80 | x: UnsafePointer[Float32, MutExternalOrigin], |
| 81 | o: UnsafePointer[Float32, MutExternalOrigin], |
| 82 | nr: Int, nc: Int): |
| 83 | def worker(r: Int) capturing -> None: |
| 84 | var acc = SIMD[DType.float32, W](0.0) |
| 85 | for bc in range(0, nc, W): |
| 86 | var vals = SIMD[DType.float32, W](0.0) |
| 87 | for k in range(W): |
| 88 | var wi = r * nc + bc + k |
| 89 | var lo = UInt16(w.load(wi * 2)); var hi = UInt16(w.load(wi * 2 + 1)) |
| 90 | vals[k] = f16_to_f32(lo | (hi << 8)) |
| 91 | var xv = x.load[width=W](bc) |
| 92 | acc = acc + vals * xv |
| 93 | o.store(r, acc.reduce_add()) |
| 94 | parallelize[func=worker](num_work_items=nr) |
| 95 | |
| 96 | def main(): |
| 97 | _st(64) |
| 98 | print("Mojo Quant Kernels — Bench + Correctness") |
| 99 | print("=========================================") |
| 100 | var ni = 2880; var nq = 4096 # Q dims |
| 101 | var t0 = time.perf_counter() |
| 102 | |
| 103 | # Load Q weight (layer 0, Q5_0) |
| 104 | var qw = _lw(Int64(4 + 12)) # layer_0 q_w at index 4+12 |
| 105 | var kw = _lw(Int64(4 + 7)) # layer_0 k_w |
| 106 | var vw = _lw(Int64(4 + 17)) # layer_0 v_w |
| 107 | var ow = _lw(Int64(4 + 10)) # layer_0 o_w |
| 108 | var emb = _lw(Int64(0)) # embedding (f16 LM head) |
| 109 | var xb = _alc(Int64(ni * 4)) |
| 110 | var qb = _alc(Int64(nq * 4)) |
| 111 | var ok = _alc(Int64(ni * 4)) |
| 112 | var lg = _alc(Int64(201088 * 4)) |
| 113 | |
| 114 | var xp = _af32(xb); var qp = _af32(qb); var op = _af32(ok); var lp = _af32(lg) |
| 115 | var wp = _au8(qw); var kp8 = _au8(kw); var vp8 = _au8(vw) |
| 116 | var op8 = _au8(ow); var ep8 = _au8(emb) |
| 117 | |
| 118 | # Fill input with known values |
| 119 | for i in range(ni): xp.store(i, Float32(Float64((i * 3) % 100 - 50) / 50.0)) |
| 120 | |
| 121 | # Test Q5_0 matmul (Q projection) |
| 122 | t0 = time.perf_counter() |
| 123 | q5_0_matmul(wp, xp, qp, nq, ni) |
| 124 | var t1 = time.perf_counter() |
| 125 | var ms_q5 = (t1 - t0) * 1000.0 |
| 126 | var non_zero = 1 |
| 127 | for i in range(min(nq, 64)): |
| 128 | if qp.load(i) != 0.0: non_zero = 0; break |
| 129 | # Actually check for non-zero: count how many are non-zero |
| 130 | var nz_count = 0 |
| 131 | for i in range(min(nq, 64)): |
| 132 | if qp.load(i) != 0.0: nz_count += 1 |
| 133 | print("Q5_0 matmul (", nq, "x", ni, "):", Int(ms_q5 * 1000), "us, non-zero:", nz_count, "/64") |
| 134 | print(" Q[0]:", qp.load(0), "Q[1]:", qp.load(1)) |
| 135 | if nz_count > 0: print(" ✓ Q5_0 produces non-zero output") |
| 136 | else: print(" ✗ Q5_0 all zeros!") |
| 137 | |
| 138 | # Test f16 matmul with real embedding weight (LM head) |
| 139 | print("Embedding LM head size: 201088 x", ni) |
| 140 | t0 = time.perf_counter() |
| 141 | f16_matmul(ep8, xp, lp, 201088, ni) |
| 142 | t1 = time.perf_counter() |
| 143 | ms_q5 = (t1 - t0) * 1000.0 |
| 144 | print("f16 LM head matmul: ", Int(ms_q5), "ms") |
| 145 | print(" Logit[0]:", lp.load(0), "Logit[1]:", lp.load(1)) |
| 146 | var lz = 1 |
| 147 | for i in range(min(100, 201088)): |
| 148 | if lp.load(i) != 0.0: lz = 0; break |
| 149 | if lz == 0: print(" ✓ f16 LM head produces non-zero output!") |
| 150 | else: print(" ✗ f16 LM head all zeros") |
| 151 | |
| 152 | # Cleanup |
| 153 | _fw(qw); _fw(kw); _fw(vw); _fw(ow); _fw(emb) |
| 154 | _free(xb); _free(qb); _free(ok); _free(lg) |