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.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.mojo` 53 lines · 1831 bytes Raw Blame History 1 # MojoLlama Mojo Engine — clean Python bridge 2 # Systematic port: Python ops that can't be ported (numpy, GGUF) 3 # are wrapped via interop. Engine API is pure Mojo. 4 from std.prelude import * 5 from std.python import Python, PythonObject 6 from std.time import perf_counter 7 8 struct Engine: 9 var eng: PythonObject 10 var N: Int 11 var L: Int 12 var NH: Int 13 var V: Int 14 var n_threads: Int32 15 16 fn __init__(inout self, path: String, threads: Int32): 17 try: 18 self.n_threads = threads 19 var np = Python.import_module("numpy") 20 var sys = Python.import_module("sys") 21 sys.path.insert(0, "/onedev-workspace/work/src/mojollama/kernels") 22 23 var te = Python.import_module("turbo_engine_v7_moe") 24 self.eng = te.TurboEngineV7MoE(path, Int(threads)) 25 self.N = Int(self.eng.n_embd.__index__()) 26 self.L = Int(self.eng.n_layers.__index__()) 27 self.NH = Int(self.eng.n_head.__index__()) 28 self.V = Int(self.eng.vocab_size.__index__()) 29 30 fn forward(inout self, token: Int) -> PythonObject: 31 try: 32 return self.eng.forward(Python.list(token)) 33 34 fn bench(inout self, n: Int) raises -> Float64: 35 var t0 = perf_counter() 36 for i in range(n): 37 var _ = self.forward(i % 1000) 38 var t1 = perf_counter() 39 return Float64(n) / (t1 - t0) 40 41 fn main() raises: 42 print("MojoLlama Mojo Engine — Bridge API") 43 print("====================================") 44 45 var e = Engine("/tmp/models/gpt-oss-20b-Q4_K_M.gguf", 8) 46 print("Engine: ", e.L, "L, ", e.N, "D, ", e.V, "vocab") 47 48 var np = Python.import_module("numpy") 49 var l0 = e.forward(0) 50 print("Forward: ", "OK" if not np.any(np.isnan(l0)) else "NaN") 51 52 var tok_s = e.bench(10) 53 print("Throughput: ", tok_s, " tok/s")