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 / test_loader.mojo

67 lines · 2108 bytes Raw Blame History
1# Test: load weights via C bridge
2from std import time
3
4@extern("load_weight")
5def _load_w(idx: Int64) abi("C") -> Int64: ...
6@extern("free_weight")
7def _free_w(p: Int64) abi("C") -> None: ...
8@extern("weight_size")
9def _w_size(idx: Int64) abi("C") -> Int64: ...
10@extern("read_u8")
11def _u8(a: Int64) abi("C") -> UInt8: ...
12@extern("read_f32")
13def _f32(a: Int64) abi("C") -> Float32: ...
14
15def main():
16 print("Testing C weight loader...")
17
18 var emb = _load_w(Int64(0))
19 var emb_sz = _w_size(Int64(0))
20 if emb == 0: print("FAIL"); return
21 print("emb: ptr=", emb, "size=", emb_sz, "bytes")
22
23 var meta = _load_w(Int64(1))
24 var meta_sz = _w_size(Int64(1))
25 print("meta: ptr=", meta, "size=", meta_sz, "bytes")
26
27 print("meta bytes:")
28 for i in range(Int64(meta_sz)):
29 var c = _u8(meta + Int64(i))
30 print(" ", c, end="")
31 print()
32 _free_w(meta)
33
34 # Layer 0 attn_norm: index 3
35 var norm = _load_w(Int64(3))
36 var norm_sz = _w_size(Int64(3))
37 print("\nattn_norm: size=", norm_sz, "first 4 f32:")
38 for i in range(4):
39 print(" [", i, "]:", _f32(norm + Int64(i * 4)))
40 _free_w(norm)
41
42 # q_weight (layer_0 files start at index 3)
43 # 0=attn_norm, 1=down_e, 2=down_info, 3=ffn_norm, 4=gate_e, 5=gate_info
44 # 6=k_info, 7=k_w, 8=o_info, 9=o_w, 10=q_info, 11=q_w
45 var qw = _load_w(Int64(3 + 11))
46 var qw_sz = _w_size(Int64(3 + 11))
47 print("\nq_weight: size=", qw_sz, "expected=", 4096 * 2880 // 32 * 22)
48 _free_w(qw)
49
50 # gate_exps (index 3 + 4 = 7)
51 var gw = _load_w(Int64(3 + 4))
52 var gw_sz = _w_size(Int64(3 + 4))
53 var per_exp = 2880 * 2880 // 32 * 17
54 print("gate_exps: size=", gw_sz, "per_exp=", per_exp, "experts=", gw_sz / per_exp)
55 _free_w(gw)
56
57 # router (index 3 + ... let me find it)
58 # Let's just loop and find all files
59 print("\nAll layer_0 files:")
60 for i in range(17):
61 var p = _load_w(Int64(3 + i))
62 var sz = _w_size(Int64(3 + i))
63 print(" [", i, "]: ptr=", p, "size=", sz)
64 _free_w(p)
65
66 _free_w(emb)
67 print("\nDone!")