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.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.mojo` 100 lines · 3211 bytes Raw Blame History 1 # Mojo v6 — use fgetc for byte-by-byte file reading 2 from std.prelude import * 3 from std import time 4 5 @extern("fopen") 6 fn c_fopen(path: String, mode: String) -> Int64: ... 7 8 @extern("fclose") 9 fn c_fclose(fp: Int64) -> Int32: ... 10 11 @extern("fgetc") 12 fn c_fgetc(fp: Int64) -> Int32: ... 13 14 fn load_file(path: String) -> List[UInt8]: 15 var fp = c_fopen(path, "rb") 16 if fp == 0: return List[UInt8]() 17 var result = List[UInt8]() 18 while True: 19 var c = c_fgetc(fp) 20 if c < 0: break 21 result.append(UInt8(c)) 22 c_fclose(fp) 23 return result^ 24 25 fn main() raises: 26 print("MojoLlama v6") 27 print("============") 28 29 var meta = load_file("/tmp/mojo_weights/gpt-oss/meta.bin") 30 print("meta: ", len(meta), " bytes") 31 if len(meta) > 0: 32 for i in range(min(60, len(meta))): 33 var c = meta[i] 34 print(chr(Int(c)) if c >= 32 and c <= 126 else ".", end="") 35 print() 36 37 # Pure Mojo matmul benchmark 38 var N = 2880 39 var n_rows = 256 40 var bpr = N / 32 41 comptime MXFP4_BS: Int = 17 42 var total_w = n_rows * bpr * MXFP4_BS 43 44 var W = List[UInt8](capacity=total_w) 45 for i in range(total_w): 46 W.append(UInt8((i * 7 + 13) & 0xFF)) 47 48 var x = List[Float32](capacity=N) 49 for i in range(N): 50 x.append(Float32(Float64((i * 3) % 100 - 50) / 50.0)) 51 52 var result: Float32 = 0.0 53 for row in range(4): 54 var off = row * bpr * MXFP4_BS 55 for blk in range(bpr): 56 var wb = off + blk * MXFP4_BS 57 var ebyte = W[wb + 16] 58 var sf: Float32 = 0.0 59 if ebyte != 0 and ebyte < 255: 60 var e = Int(ebyte) - 127 61 sf = 1.0 62 if e >= 0: 63 for _ in range(e): sf *= 2.0 64 else: 65 for _ in range(-e): sf *= 0.5 66 elif ebyte >= 255: sf = 1e20 67 for j in range(16): 68 var p = W[wb + j] 69 var lo = Int32(p & 0x0F) 70 if lo > 7: lo -= 16 71 var hi = Int32(p >> 4) 72 if hi > 7: hi -= 16 73 result += Float32(lo * Int32(x[blk * 32 + j * 2]) + hi * Int32(x[blk * 32 + j * 2 + 1])) * sf 74 75 var t0 = time.perf_counter() 76 for row in range(n_rows): 77 var off = row * bpr * MXFP4_BS 78 for blk in range(bpr): 79 var wb = off + blk * MXFP4_BS 80 var ebyte = W[wb + 16] 81 var sf: Float32 = 0.0 82 if ebyte != 0 and ebyte < 255: 83 var e = Int(ebyte) - 127 84 sf = 1.0 85 if e >= 0: 86 for _ in range(e): sf *= 2.0 87 else: 88 for _ in range(-e): sf *= 0.5 89 elif ebyte >= 255: sf = 1e20 90 for j in range(16): 91 var p = W[wb + j] 92 var lo = Int32(p & 0x0F) 93 if lo > 7: lo -= 16 94 var hi = Int32(p >> 4) 95 if hi > 7: hi -= 16 96 result += Float32(lo * Int32(x[blk * 32 + j * 2]) + hi * Int32(x[blk * 32 + j * 2 + 1])) * sf 97 var t1 = time.perf_counter() 98 99 print("Result: ", result) 100 print("Rows/s: ", Int(Float64(n_rows) / (t1 - t0)))