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 / mojollama_bench_quantcomp.mojo

158 lines · 7132 bytes Raw Blame History
1# MojoLlama Quant Comparison — Measure each quant's matmul speed
2# WHAT: For each GGUF quant format, allocates cold-cache weights in that
3# format, measures actual f16 matmul throughput, reports tok/s.
4# WHY: Pure Mojo quant benchmark — measures real decode speed.
5# WHEN: May 2026.
6from std import time, math
7from std.math import sqrt, exp
8from std.algorithm.backend.cpu.parallelize import parallelize
9from std.builtin.simd import FastMathFlag
10
11comptime NE: Int = 2048; comptime NH: Int = 32; comptime NK: Int = 4
12comptime HD: Int = 64; comptime NL: Int = 22; comptime FF: Int = 5632
13comptime NV: Int = 32000; comptime NW: Int = 24; comptime RPW: Int = 32; comptime W: Int = 8
14
15comptime Q4_0_BS: Int = 18
16
17@extern("malloc")
18def _alc(sz: Int64) abi("C") -> Int64: ...
19
20# ═══ f16 matmul (baseline) ═══
21def bench_f16(nr: Int, nc: Int, cold_pool: Int64, pool_off: Int64) -> Float64:
22 var w = UnsafePointer[Float16, MutExternalOrigin](unsafe_from_address=Int(cold_pool + pool_off))
23 var x = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(cold_pool + 256*1048576))
24 var o = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(cold_pool + 256*1048576 + 4096*4))
25 var bpr = nc // 32; var nb = (nr + RPW - 1) // RPW
26 var t0 = time.perf_counter()
27 def wk(b: Int) capturing -> None:
28 var rs = b * RPW; var re = rs + RPW
29 if re > nr: re = nr
30 for r in range(rs, re):
31 var ro = r * nc; var acc = SIMD[DType.float32, W](0.0)
32 for blk in range(bpr):
33 comptime for grp in range(4):
34 var w16 = w.load[width=W](ro + blk * 32 + grp * 8)
35 var wf32 = w16.cast[DType.float32]()
36 var xv = x.load[width=W](blk * 32 + grp * 8)
37 acc = wf32.fma[FastMathFlag.FAST](xv, acc)
38 o.store(r, acc.reduce_add())
39 parallelize[func=wk](num_work_items=nb, num_workers=NW)
40 return (time.perf_counter() - t0) * 1000.0
41
42# ═══ Q4_0 matmul ═══
43def bench_q4_0(nr: Int, nc: Int, cold_pool: Int64, pool_off: Int64) -> Float64:
44 var w = UnsafePointer[UInt8, MutExternalOrigin](unsafe_from_address=Int(cold_pool + pool_off))
45 var x = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(cold_pool + 256*1048576))
46 var o = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(cold_pool + 256*1048576 + 4096*4))
47 var bpr = nc // 32
48 var t0 = time.perf_counter()
49 def wk(r: Int) capturing -> None:
50 var ro = r * bpr * Q4_0_BS; var acc: Float32 = 0.0
51 for blk in range(bpr):
52 var bo = ro + blk * Q4_0_BS
53 var lo16 = UInt16(w.load(bo)); var hi16 = UInt16(w.load(bo+1))
54 var s = UInt32(hi16 >> 15); var e = UInt32((hi16 >> 10) & 0x1F); var m = UInt32(hi16 & 0x3FF)
55 var d: Float32 = 0.0
56 if e == 0: d = 0.0 if m == 0 else Float32(Float64(m) * 5.960464477539063e-8)
57 elif e < 31:
58 d = Float32(m | 0x400); var ei = Int(e) - 25
59 if ei >= 0:
60 for _ in range(ei): d *= 2.0
61 else:
62 for _ in range(-ei): d *= 0.5
63 d = -d if s != 0 else d
64 for j in range(32):
65 var nib = Int32(w.load(bo + 2 + j//2))
66 if j % 2 == 0: nib = nib & 0x0F
67 else: nib = nib >> 4
68 if nib > 7: nib -= 16
69 acc += Float32(nib) * d * x.load(blk * 32 + j)
70 o.store(r, acc)
71 parallelize[func=wk](num_work_items=nr, num_workers=NW)
72 return (time.perf_counter() - t0) * 1000.0
73
74# ═══ MXFP4 matmul ═══
75comptime MXFP4_BYTES: Int = 17
76def bench_mxfp4(nr: Int, nc: Int, cold_pool: Int64, pool_off: Int64) -> Float64:
77 var w = UnsafePointer[UInt8, MutExternalOrigin](unsafe_from_address=Int(cold_pool + pool_off))
78 var x = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(cold_pool + 256*1048576))
79 var o = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(cold_pool + 256*1048576 + 4096*4))
80 var bpr = nc // 32
81 var t0 = time.perf_counter()
82 def wk(r: Int) capturing -> None:
83 var ro = r * bpr * MXFP4_BYTES; var acc: Float32 = 0.0
84 for blk in range(bpr):
85 var bo = ro + blk * MXFP4_BYTES
86 var eb = w.load(bo + 16)
87 var sf: Float32 = 0.0
88 if eb != 0:
89 if eb < 255:
90 var ei = Int(eb) - 127
91 sf = 1.0
92 if ei >= 0:
93 for _ in range(ei): sf *= 2.0
94 else:
95 for _ in range(-ei): sf *= 0.5
96 else: sf = 1e20
97 var ai: Int32 = 0
98 for j in range(16):
99 var p = w.load(bo + j)
100 var lo = Int32(p & 0x0F)
101 if lo > 7: lo -= 16
102 var hi = Int32(p >> 4)
103 if hi > 7: hi -= 16
104 ai += lo * Int32(x.load(blk*32 + j*2)) + hi * Int32(x.load(blk*32 + j*2+1))
105 acc += Float32(ai) * sf
106 o.store(r, acc)
107 parallelize[func=wk](num_work_items=nr, num_workers=NW)
108 return (time.perf_counter() - t0) * 1000.0
109
110# ═══ Fill helpers ═══
111def fill_pool(pool: Int64, sz: Int64):
112 var p = UnsafePointer[UInt8, MutExternalOrigin](unsafe_from_address=Int(pool))
113 for i in range(Int(sz)):
114 p.store(i, UInt8((i * 7 + (i >> 4) * 13 + (i >> 8) * 17) & 0xFF))
115
116def main():
117 # Cold-cache pool: 256MB + buffer space
118 var pool = _alc(Int64(300 * 1048576))
119 fill_pool(pool, Int64(300 * 1048576))
120
121 # Input buffers within pool
122 var x_addr = pool + 256 * 1048576
123 var x = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(x_addr))
124 for i in range(NE): x.store(i, Float32(i % 100 - 50) * 0.01)
125
126 # Benchmark each quant format on key matmul shapes
127 # Use QKV (nq × NE = 1024×2048) as the representative matmul
128 var nq = NH * HD # 1024
129
130 print("{")
131 print("\"quant_benchmarks\": [")
132
133 var first = True
134
135 # f16: 2 bytes/value
136 var off = Int64(0)
137 var t = bench_f16(nq, NE, pool, off)
138 var tok_s = 1000.0 / (t * Float64(NL * 7))
139 if not first: print(",")
140 first = False
141 print(" {\"quant\":\"f16\",\"ms\":" + String(Float64(t)) + ",\"est_tok_s\":" + String(Float64(tok_s)) + "}")
142
143 # Q4_0: 18 bytes/32 values = 0.5625 bytes/value
144 off += Int64(nq) * Int64(NE) * 2 # skip f16 weights
145 t = bench_q4_0(nq, NE, pool, off)
146 tok_s = 1000.0 / (t * Float64(NL * 7))
147 if not first: print(",")
148 print(" {\"quant\":\"Q4_0\",\"ms\":" + String(Float64(t)) + ",\"est_tok_s\":" + String(Float64(tok_s)) + "}")
149
150 # MXFP4: 17 bytes/32 values = 0.53125 bytes/value
151 off += Int64(nq) * (Int64(NE) // 32) * Q4_0_BS
152 t = bench_mxfp4(nq, NE, pool, off)
153 tok_s = 1000.0 / (t * Float64(NL * 7))
154 if not first: print(",")
155 print(" {\"quant\":\"MXFP4\",\"ms\":" + String(Float64(t)) + ",\"est_tok_s\":" + String(Float64(tok_s)) + "}")
156
157 print("")
158 print("]}")