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/mojo_engine/mojo_engine_v6_final.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 / mojo_engine / mojo_engine_v6_final.mojo` 107 lines · 3677 bytes Raw Blame History 1 # Mojo v6 Final — links with read_helpers.o 2 from std.prelude import * 3 from std import time 4 5 @extern("read_u8") 6 fn c_read_u8(addr: Int64) -> UInt8: ... 7 8 @extern("read_f32") 9 fn c_read_f32(addr: Int64) -> Float32: ... 10 11 @extern("load_file_size") 12 fn c_load_file_size(path: String) -> Int64: ... 13 14 @extern("load_file_data") 15 fn c_load_file_data(path: String) -> Int64: ... 16 17 @extern("free_buf") 18 fn c_free_buf(ptr: Int64): ... 19 20 fn main(): 21 print("MojoLlama v6 Final") 22 print("==================") 23 24 # Load meta file 25 var sz = c_load_file_size("/tmp/mojo_weights/gpt-oss/meta.bin") 26 var meta = c_load_file_data("/tmp/mojo_weights/gpt-oss/meta.bin") 27 if meta != 0 and sz > 0: 28 print("meta: ", sz, " bytes") 29 print("First 60: ", end="") 30 for i in range(min(60, Int(sz))): 31 var c = c_read_u8(meta + Int64(i)) 32 print(chr(Int(c)) if c >= 32 and c <= 126 else ".", end="") 33 print() 34 c_free_buf(meta) 35 36 # Load embedding 37 var esz = c_load_file_size("/tmp/mojo_weights/gpt-oss/emb.bin") 38 var emb = c_load_file_data("/tmp/mojo_weights/gpt-oss/emb.bin") 39 if emb != 0 and esz > 0: 40 var n_floats = Int(esz / 4) 41 print("emb: ", esz, " bytes = ", n_floats, " floats") 42 print("emb[0:5]: ", end="") 43 for i in range(min(5, n_floats)): 44 print(c_read_f32(emb + Int64(i * 4)), " ", end="") 45 print() 46 c_free_buf(emb) 47 48 # MXFP4 matmul benchmark 49 var N = 2880 50 var n_rows = 256 51 var bpr = N / 32 52 comptime MXFP4_BS: Int = 17 53 var total_w = n_rows * bpr * MXFP4_BS 54 55 var W = List[UInt8](capacity=total_w) 56 for i in range(total_w): 57 W.append(UInt8((i * 7 + 13) & 0xFF)) 58 59 var x = List[Float32](capacity=N) 60 for i in range(N): 61 x.append(Float32(Float64((i * 3) % 100 - 50) / 50.0)) 62 63 var result: Float32 = 0.0 64 for row in range(4): 65 var off = row * bpr * MXFP4_BS 66 for blk in range(bpr): 67 var ebyte = W[off + blk * MXFP4_BS + 16] 68 var sf: Float32 = 0.0 69 if ebyte != 0 and ebyte < 255: 70 var e = Int(ebyte) - 127; sf = 1.0 71 if e >= 0: 72 for _ in range(e): sf *= 2.0 73 else: 74 for _ in range(-e): sf *= 0.5 75 elif ebyte >= 255: sf = 1e20 76 for j in range(16): 77 var p = W[off + blk * MXFP4_BS + j] 78 var lo = Int32(p & 0x0F) 79 if lo > 7: lo -= 16 80 var hi = Int32(p >> 4) 81 if hi > 7: hi -= 16 82 result += Float32(lo * Int32(x[blk * 32 + j * 2]) + hi * Int32(x[blk * 32 + j * 2 + 1])) * sf 83 84 var t0 = time.perf_counter() 85 for row in range(n_rows): 86 var off = row * bpr * MXFP4_BS 87 for blk in range(bpr): 88 var ebyte = W[off + blk * MXFP4_BS + 16] 89 var sf: Float32 = 0.0 90 if ebyte != 0 and ebyte < 255: 91 var e = Int(ebyte) - 127; sf = 1.0 92 if e >= 0: 93 for _ in range(e): sf *= 2.0 94 else: 95 for _ in range(-e): sf *= 0.5 96 elif ebyte >= 255: sf = 1e20 97 for j in range(16): 98 var p = W[off + blk * MXFP4_BS + j] 99 var lo = Int32(p & 0x0F) 100 if lo > 7: lo -= 16 101 var hi = Int32(p >> 4) 102 if hi > 7: hi -= 16 103 result += Float32(lo * Int32(x[blk * 32 + j * 2]) + hi * Int32(x[blk * 32 + j * 2 + 1])) * sf 104 var t1 = time.perf_counter() 105 106 print("Result: ", result) 107 print("Rows/s: ", Int(Float64(n_rows) / (t1 - t0)))