Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/MojoLlama/blob/main/src/mojollama/kernels/bench.mojo # MojoLlama Archived Mojo/MAX quantizer and serving research repository (read-only history) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `MojoLlama / src / mojollama / kernels / bench.mojo` 212 lines · 8189 bytes Raw Blame History 1 """Real Mojo SIMD benchmark — measures actual compute, not optimized-away loops.""" 2 from std.memory.unsafe_pointer import alloc 3 from std.math import sqrt 4 5 alias F32x8 = SIMD[DType.float32, 8] 6 alias U8x16 = SIMD[DType.uint8, 16] 7 8 # ─── F16→F32 ────────────────────────────────────────────────────────── 9 10 def f16_to_f32(h: UInt16) -> Float32: 11 from std.sys.info import CompilationTarget 12 from std.memory import bitcast 13 comptime if CompilationTarget.has_avx2(): 14 return Float32(bitcast[DType.float16](h)) 15 else: 16 var s = (UInt32(h) >> 15) & 1 17 var e = (UInt32(h) >> 10) & 0x1f 18 var m = UInt32(h) & 0x3ff 19 if e == 0: 20 if m == 0: return Float32(bitcast[DType.float32](s << 31)) 21 var mm = m; var c: UInt32 = 0 22 while mm > 0: mm >>= 1; c += 1 23 var sh = 24 - c 24 return Float32(bitcast[DType.float32]((s << 31) | ((UInt32(113 - sh)) << 23) | ((m << (sh + 13)) & 0x7fffff))) 25 if e == 31: return Float32(bitcast[DType.float32]((s << 31) | 0x7f800000 | (m << 13))) 26 return Float32(bitcast[DType.float32]((s << 31) | ((e + 112) << 23) | (m << 13))) 27 28 29 # ─── Q4_0 Matmul ────────────────────────────────────────────────────── 30 31 def q4_block_dot(scale: Float32, nibbles: U8x16, 32 x0: F32x8, x1: F32x8, x2: F32x8, x3: F32x8) -> Float32: 33 @parameter 34 def dg(s: Int, xv: F32x8) -> Float32: 35 var v = F32x8() 36 for j in range(8): 37 var bi = s + j // 2 38 var b = nibbles[bi] 39 v[j] = Float32(Int8(b & 15) - 8) if j % 2 == 0 else Float32(Int8((b >> 4) & 15) - 8) 40 return (v * scale * xv).reduce_add() 41 return dg(0, x0) + dg(4, x1) + dg(8, x2) + dg(12, x3) 42 43 44 def q4_matmul( 45 w: UnsafePointer[mut=True, type=UInt8, origin=_], 46 inp: UnsafePointer[mut=False, type=Float32, origin=_], 47 result: UnsafePointer[mut=True, type=Float32, origin=_], 48 n_rows: Int, n_cols: Int, checksum: UnsafePointer[mut=True, type=Float32, origin=_], 49 ): 50 var bpr = n_cols // 32 51 var total_chk: Float32 = 0.0 52 for row in range(n_rows): 53 var total: Float32 = 0.0 54 for blk in range(bpr): 55 var off = (row * bpr + blk) * 18 56 var lo = w.load(off) 57 var hi = w.load(off + 1) 58 var scale = f16_to_f32((UInt16(hi) << 8) | UInt16(lo)) 59 var nibs = w.load[width=16](off + 2) 60 var inp_off = blk * 32 61 var x0 = inp.load[width=8](inp_off) 62 var x1 = inp.load[width=8](inp_off + 8) 63 var x2 = inp.load[width=8](inp_off + 16) 64 var x3 = inp.load[width=8](inp_off + 24) 65 total += q4_block_dot(scale, nibs, x0, x1, x2, x3) 66 result.store(row, total) 67 total_chk += total 68 checksum.store(0, total_chk) 69 70 71 # ─── RMSNorm ─────────────────────────────────────────────────────────── 72 73 def rms_norm( 74 x: UnsafePointer[mut=False, type=Float32, origin=_], 75 w: UnsafePointer[mut=False, type=Float32, origin=_], 76 result: UnsafePointer[mut=True, type=Float32, origin=_], 77 n: Int, eps: Float32, checksum: UnsafePointer[mut=True, type=Float32, origin=_], 78 ): 79 var ss: Float32 = 0.0 80 for i in range(n): 81 var v = x.load(i) 82 ss += v * v 83 var inv_rms = Float32(1.0 / sqrt(Float64(ss) / Float64(n) + Float64(eps))) 84 var ck: Float32 = 0.0 85 for i in range(n): 86 var v = x.load(i) * w.load(i) * inv_rms 87 result.store(i, v) 88 ck += v 89 checksum.store(0, ck) 90 91 92 # ─── SiLU ────────────────────────────────────────────────────────────── 93 94 def fast_exp(x: Float32) -> Float32: 95 var v = 1.0 + x * 0.`000244140625` 96 for _ in range(12): v = v * v 97 return v 98 99 def silu_act( 100 x: UnsafePointer[mut=False, type=Float32, origin=_], 101 result: UnsafePointer[mut=True, type=Float32, origin=_], 102 n: Int, checksum: UnsafePointer[mut=True, type=Float32, origin=_], 103 ): 104 var ck: Float32 = 0.0 105 for i in range(n): 106 var v = x.load(i) / (1.0 + fast_exp(-x.load(i))) 107 result.store(i, v) 108 ck += v 109 checksum.store(0, ck) 110 111 112 # ─── Benchmark ───────────────────────────────────────────────────────── 113 114 def main() raises: 115 from python import Python 116 var tim = Python.import_module("time") 117 118 var n_embd: Int = 2048 119 var n_head: Int = 32 120 var n_kv_head: Int = 8 121 var n_ff: Int = 8192 122 var head_dim: Int = 64 123 var n_cols: Int = n_embd 124 var n_rows_q: Int = n_head * head_dim # 2048 125 var n_rows_ff: Int = n_ff # 8192 126 127 print("MojoLlama SIMD — Real Benchmarks") 128 print("Mojo 1.0.0b1, AVX2:", end="") 129 from std.sys.info import CompilationTarget 130 print("YES" if CompilationTarget.has_avx2() else "NO") 131 print() 132 133 # Allocate test data 134 var inp = alloc[Float32](n_embd) 135 var result = alloc[Float32](2048) 136 var norm_w = alloc[Float32](n_embd) 137 var chk = alloc[Float32](1) 138 139 for i in range(n_embd): 140 inp.store(i, Float32(0.5)) 141 norm_w.store(i, Float32(1.0)) 142 143 # Allocate Q4_0 weight buffer (simulated: all zeros → dequant to -8) 144 var n_blocks = n_rows_q * (n_cols // 32) 145 var w = alloc[UInt8](n_blocks * 18) 146 for i in range(n_blocks * 18): 147 w.store(i, UInt8(0)) # scale=0 (f16→0), nibbles=0 (dequant→-8) 148 149 # Real benchmark RMSNorm 150 print("=== RMSNorm (n=2048) ===") 151 var t0 = tim.time() 152 for iter in range(1000): 153 rms_norm(inp, norm_w, result, n_embd, 1e-5, chk) 154 var t1 = tim.time() 155 var rms_elapsed = t1 - t0 156 print(" 1000 iterations:", rms_elapsed, "s") 157 print(" per iteration:", rms_elapsed / 1000 * 1e6, "us") 158 print(" checksum:", chk.load(0)) 159 160 # Real benchmark SiLU 161 print("\n=== SiLU (n=2048) ===") 162 t0 = tim.time() 163 for iter in range(1000): 164 silu_act(inp, result, n_embd, chk) 165 t1 = tim.time() 166 var silu_elapsed = t1 - t0 167 print(" 1000 iterations:", silu_elapsed, "s") 168 print(" per iteration:", silu_elapsed / 1000 * 1e6, "us") 169 print(" checksum:", chk.load(0)) 170 171 # Real benchmark Q4_0 matmul (2048×2048) 172 print("\n=== Q4_0 Matmul (2048×2048) ===") 173 t0 = tim.time() 174 for iter in range(10): 175 q4_matmul(w, inp, result, n_rows_q, n_cols, chk) 176 t1 = tim.time() 177 var elapsed_qkv = t1 - t0 178 print(" 10 iterations:", elapsed_qkv, "s") 179 print(" per iteration:", elapsed_qkv / 10 * 1e3, "ms") 180 print(" checksum:", chk.load(0)) 181 182 # Benchmark Q4_0 matmul (8192×2048 - FFN size) 183 print("\n=== Q4_0 Matmul (8192×2048 - FFN) ===") 184 var w2 = alloc[UInt8](n_rows_ff * (n_cols // 32) * 18) 185 var result2 = alloc[Float32](n_rows_ff) 186 t0 = tim.time() 187 for iter in range(5): 188 q4_matmul(w2, inp, result2, n_rows_ff, n_cols, chk) 189 t1 = tim.time() 190 var ffn_elapsed = t1 - t0 191 print(" 5 iterations:", ffn_elapsed, "s") 192 print(" per iteration:", ffn_elapsed / 5 * 1e3, "ms") 193 print(" checksum:", chk.load(0)) 194 var ffn_matmul_time = ffn_elapsed / 5 195 var qkv_matmul_time = elapsed_qkv / 10 196 result2.free() 197 w2.free() 198 199 # Full forward estimate 200 print("\n=== Estimated Full Forward Pass ===") 201 # Per layer: 6 matmuls + 2 rmsnorm + 1 silu + attention 202 var layer_time = ffn_matmul_time + 5 * qkv_matmul_time + 2 * (rms_elapsed / 1000) + (silu_elapsed / 1000) 203 var fwd_time = layer_time * 16 204 print(" Estimated time per forward pass (16 layers):", fwd_time * 1e3, "ms") 205 print(" Estimated throughput:", 1.0 / fwd_time, "tok/s") 206 207 inp.free() 208 result.free() 209 norm_w.free() 210 w.free() 211 chk.free() 212 print("\nDone.")