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 / kernels / mojo_engine_v5.mojo

111 lines · 3836 bytes Raw Blame History
1# MojoLlama Mojo Engine — Pure Mojo Matmul Kernel
2# No Python, no ctypes — pure Mojo SIMD for MXFP4 × Q8_0
3from std.prelude import *
4from std import time
5
6# MXFP4 block: 16 bytes quants + 1 byte exponent = 17 bytes
7comptime MXFP4_BLOCK: Int = 17
8# Q8_0 block: 2 bytes f16 + 32 bytes i8 = 34 bytes
9comptime Q8_0_BLOCK: Int = 34
10
11# ── Helpers ────────────────────────────────────────────────
12fn sext4(v: UInt8) -> Int32:
13 if v >= 8:
14 return Int32(v) - 16
15 return Int32(v)
16
17fn f16_to_f32(h: UInt16) -> Float32:
18 var sign = UInt32(h >> 15)
19 var exp = UInt32((h >> 10) & 0x1F)
20 var mant = UInt32(h & 0x3FF)
21 if exp == 0:
22 if mant == 0:
23 return 0.0
24 var m = Float64(mant) * 5.960464477539063e-8
25 return Float32(m)
26 elif exp == 31:
27 return 0.0
28 var m = Float32(mant | 0x400)
29 var e = Int(exp) - 25
30 var result = m
31 if e >= 0:
32 for _ in range(e):
33 result *= 2.0
34 else:
35 for _ in range(-e):
36 result *= 0.5
37 return -result if sign != 0 else result
38
39# ── Row-dot using List (no raw pointer arithmetic) ────────
40fn dot_row_gptoss(w: List[UInt8], x: List[UInt8], bpr: Int, row: Int) -> Float32:
41 var acc: Float32 = 0.0
42 var row_off = row * bpr * MXFP4_BLOCK
43 for blk in range(bpr):
44 var acc_blk: Int32 = 0
45 var w_off = row_off + blk * MXFP4_BLOCK
46 var x_off = blk * Q8_0_BLOCK
47 var ebyte = w[w_off + 16]
48 var d_lo = x[x_off]
49 var d_hi = x[x_off + 1]
50 var q8d = f16_to_f32(UInt16(d_lo) | (UInt16(d_hi) << 8))
51 # MXFP4 E8M0 exponent decode: value = 2^(ebyte - 127)
52 # Special: ebyte=0 → 0, ebyte=255 → limit to 1e20
53 var sf_val: Float32 = 0.0
54 if ebyte != 0 and ebyte < 255:
55 var sf_exp = Int(ebyte) - 127
56 sf_val = 1.0
57 if sf_exp >= 0:
58 for _ in range(sf_exp):
59 sf_val *= 2.0
60 else:
61 for _ in range(-sf_exp):
62 sf_val *= 0.5
63 elif ebyte >= 255:
64 sf_val = 1e20
65 var sf = sf_val * q8d
66 if sf > 1e20: sf = 1e20
67 if sf < -1e20: sf = -1e20
68 for j in range(16):
69 var p = w[w_off + j]
70 var lo = sext4(p & 0x0F)
71 var hi = sext4(p >> 4)
72 var ql = Int32(Int8(x[x_off + 2 + j * 2]))
73 var qh = Int32(Int8(x[x_off + 2 + j * 2 + 1]))
74 acc_blk += lo * ql + hi * qh
75 acc += Float32(acc_blk) * sf
76 return acc
77
78# ── Benchmark ─────────────────────────────────────────────
79fn main():
80 var N = 2880 # GPT-OSS n_embd
81 var n_rows = 256
82 var bpr = N / 32
83
84 # Fill weight buffer
85 var total_w = n_rows * bpr * MXFP4_BLOCK
86 var w = List[UInt8](capacity=total_w)
87 for i in range(total_w):
88 w.append(UInt8((i * 7 + 13) & 0xFF))
89
90 # Fill Q8 input
91 var q8_total = bpr * Q8_0_BLOCK
92 var q = List[UInt8](capacity=q8_total)
93 for i in range(q8_total):
94 q.append(UInt8((i * 3 + 7) & 0xFF))
95
96 # Warmup (use Python for sanity check — no, don't use Python)
97 # Pure Mojo warmup + benchmark
98 var result: Float32 = 0.0
99 for row in range(4):
100 result += dot_row_gptoss(w, q, bpr, row)
101
102 var t0 = time.perf_counter()
103 for row in range(n_rows):
104 result += dot_row_gptoss(w, q, bpr, row)
105 var t1 = time.perf_counter()
106
107 print("Pure Mojo Matmul")
108 print(" Dims: ", N, " rows: ", n_rows)
109 print(" Result: ", result)
110 print(" Time: ", (t1 - t0) * 1000000, " us")
111 print(" Rows/s: ", Float64(n_rows) / (t1 - t0))