MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / src / mojollama / mojo_engine / dump_weights.py
| 1 | # MojoLlama Weight Dumper — exports GGUF weights as raw binary |
| 2 | # One-time use per model. Output: directory of .bin files that |
| 3 | # the Mojo engine loads directly (no Python dependency at runtime). |
| 4 | import sys, os, struct, numpy as np |
| 5 | sys.path.insert(0, '/onedev-workspace/work/src/mojollama/kernels') |
| 6 | from turbo_engine_v7_moe import TurboEngineV7MoE |
| 7 | |
| 8 | def dump_model(model_path, out_dir): |
| 9 | os.makedirs(out_dir, exist_ok=True) |
| 10 | e = TurboEngineV7MoE(model_path, n_threads=8) |
| 11 | |
| 12 | # Dump metadata |
| 13 | meta = { |
| 14 | 'n_embd': e.n_embd, 'n_layers': e.n_layers, |
| 15 | 'n_head': e.n_head, 'n_kv_head': e.n_kv_head, |
| 16 | 'head_dim': e.head_dim, 'vocab_size': e.vocab_size, |
| 17 | 'n_ff': e.n_ff, 'n_experts': e.n_experts, |
| 18 | 'n_experts_per_tok': e.n_experts_per_tok, |
| 19 | 'n_ff_expert': e.n_ff_expert, |
| 20 | 'is_moe': e.is_moe, 'eps': e.eps, |
| 21 | } |
| 22 | with open(f'{out_dir}/meta.bin', 'wb') as f: |
| 23 | for k, v in meta.items(): |
| 24 | f.write(f'{k}={v}\n'.encode()) |
| 25 | |
| 26 | # Dump embedding |
| 27 | e.emb.astype(np.float32).tofile(f'{out_dir}/emb.bin') |
| 28 | |
| 29 | # Dump output norm |
| 30 | e._out_norm_w.astype(np.float32).tofile(f'{out_dir}/out_norm.bin') |
| 31 | |
| 32 | # Dump per-layer weights |
| 33 | for i in range(e.n_layers): |
| 34 | lw = e._layers[i] |
| 35 | ld = f'{out_dir}/layer_{i}' |
| 36 | os.makedirs(ld, exist_ok=True) |
| 37 | |
| 38 | # Attention norm |
| 39 | lw.attn_norm_w.astype(np.float32).tofile(f'{ld}/attn_norm.bin') |
| 40 | |
| 41 | # Q projection (quantized) |
| 42 | if hasattr(lw, 'attn_q_raw') and lw.attn_q_raw is not None: |
| 43 | np.array([lw.attn_q_nr.value, lw.attn_q_nc.value, lw.attn_q_qt.value], |
| 44 | dtype=np.int32).tofile(f'{ld}/q_info.bin') |
| 45 | raw = lw.attn_q_raw |
| 46 | if hasattr(raw, 'tobytes'): |
| 47 | raw.tofile(f'{ld}/q_weight.bin') |
| 48 | |
| 49 | # K projection |
| 50 | if hasattr(lw, 'attn_k_raw') and lw.attn_k_raw is not None: |
| 51 | np.array([lw.attn_k_nr.value, lw.attn_k_nc.value, lw.attn_k_qt.value], |
| 52 | dtype=np.int32).tofile(f'{ld}/k_info.bin') |
| 53 | raw = lw.attn_k_raw |
| 54 | if hasattr(raw, 'tobytes'): |
| 55 | raw.tofile(f'{ld}/k_weight.bin') |
| 56 | |
| 57 | # Output projection |
| 58 | if hasattr(lw, 'attn_out_raw') and lw.attn_out_raw is not None: |
| 59 | np.array([lw.attn_out_nr.value, lw.attn_out_nc.value, lw.attn_out_qt.value], |
| 60 | dtype=np.int32).tofile(f'{ld}/o_info.bin') |
| 61 | raw = lw.attn_out_raw |
| 62 | if hasattr(raw, 'tobytes'): |
| 63 | raw.tofile(f'{ld}/o_weight.bin') |
| 64 | |
| 65 | # Q norm (Gemma4) |
| 66 | if lw.has_q_norm: |
| 67 | lw.q_norm_w.astype(np.float32).tofile(f'{ld}/q_norm.bin') |
| 68 | if lw.has_k_norm: |
| 69 | lw.k_norm_w.astype(np.float32).tofile(f'{ld}/k_norm.bin') |
| 70 | |
| 71 | # FFN norms |
| 72 | if lw.ffn_norm_w is not None: |
| 73 | lw.ffn_norm_w.astype(np.float32).tofile(f'{ld}/ffn_norm.bin') |
| 74 | |
| 75 | # MoE weights |
| 76 | me = e._moe_layers[i] |
| 77 | if me.router_f32 is not None: |
| 78 | me.router_f32.astype(np.float32).tofile(f'{ld}/router.bin') |
| 79 | |
| 80 | # Expert weights (MXFP4) |
| 81 | for name, attr in [('gate', 'gate_raw'), ('up', 'up_raw'), ('down', 'down_raw')]: |
| 82 | raw_list = getattr(me, attr, None) |
| 83 | if raw_list and len(raw_list) > 0: |
| 84 | # First expert's raw data |
| 85 | raw0 = raw_list[0] |
| 86 | if hasattr(raw0, 'tobytes') or hasattr(raw0, 'tobytes'): |
| 87 | data = np.frombuffer(raw0, dtype=np.uint8) if hasattr(raw0, 'tobytes') else raw0 |
| 88 | # Get per-expert stride |
| 89 | nr = getattr(me, f'{name}_nr', 0) |
| 90 | nc = getattr(me, f'{name}_nc', 0) |
| 91 | qt = getattr(me, f'{name}_qt', 0) |
| 92 | if hasattr(nr, 'value'): nr = nr.value |
| 93 | if hasattr(nc, 'value'): nc = nc.value |
| 94 | if hasattr(qt, 'value'): qt = qt.value |
| 95 | np.array([len(raw_list), nr, nc, qt], dtype=np.int32).tofile(f'{ld}/{name}_info.bin') |
| 96 | # Dump all experts concatenated |
| 97 | all_exp = np.concatenate([np.frombuffer(r, dtype=np.uint8) for r in raw_list]) |
| 98 | all_exp.tofile(f'{ld}/{name}_exps.bin') |
| 99 | |
| 100 | print(f'Dumped to {out_dir}') |
| 101 | print(f' Layers: {e.n_layers}') |
| 102 | print(f' Embedding: {e.emb.shape}') |
| 103 | print(f' Output norm: {e._out_norm_w.shape}') |
| 104 | |
| 105 | if __name__ == '__main__': |
| 106 | model = sys.argv[1] if len(sys.argv) > 1 else '/tmp/models/gpt-oss-20b-Q4_K_M.gguf' |
| 107 | out = sys.argv[2] if len(sys.argv) > 2 else '/tmp/mojo_weights/gpt-oss' |
| 108 | dump_model(model, out) |