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 / 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.
4from std.prelude import *
5from std.python import Python, PythonObject
6from std.time import perf_counter
7
8struct 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
41fn 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")