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/dump_weights_v2.py # 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 / dump_weights_v2.py` 115 lines · 4587 bytes Raw Blame History 1 #!/usr/bin/env python3 2 """MojoLlama Weight Dumper v2 — dumps quantized weight arrays from raw_weights dict. 3 Usage: python3 dump_weights.py [model.gguf] [out_dir]""" 4 import sys, os, numpy as np 5 sys.path.insert(0, '/onedev-workspace/work/src/mojollama/kernels') 6 sys.path.insert(0, '/onedev-workspace/work/src/mojollama') 7 from turbo_engine_v7_moe import TurboEngineV7MoE 8 9 model = sys.argv[1] if len(sys.argv) > 1 else '/tmp/models/gpt-oss-20b-Q4_K_M.gguf' 10 out = sys.argv[2] if len(sys.argv) > 2 else '/tmp/mojo_weights/gpt-oss' 11 e = TurboEngineV7MoE(model, n_threads=8) 12 rw = e.raw_weights 13 V = int(e.vocab_size) 14 L = int(e.n_layers) 15 N = int(e.n_embd) 16 17 # Output norm 18 e._out_norm_w.astype(np.float32).tofile(f'{out}/out_norm.bin') 19 print(f"out_norm: {e._out_norm_w.shape}") 20 21 # Per-layer 22 for i in range(L): 23 ld = f'{out}/layer_{i}' 24 os.makedirs(ld, exist_ok=True) 25 26 # Norms 27 e._layers[i].attn_norm_w.astype(np.float32).tofile(f'{ld}/attn_norm.bin') 28 e._layers[i].ffn_norm_w.astype(np.float32).tofile(f'{ld}/ffn_norm.bin') 29 30 # Q — from raw_weights dict 31 q_key = f'blk.{i}.attn_q.weight' 32 if q_key in rw: 33 data = rw[q_key] 34 data.tofile(f'{ld}/q_weight.bin') 35 lw = e._layers[i] 36 nr = int(getattr(lw.attn_q_nr, 'value', lw.attn_q_nr)) 37 nc = int(getattr(lw.attn_q_nc, 'value', lw.attn_q_nc)) 38 qt_val = lw.attn_q_qt 39 if isinstance(qt_val, bytes): 40 qt_val = int.from_bytes(qt_val, 'little') 41 else: 42 qt_val = int(getattr(qt_val, 'value', qt_val)) 43 np.array([nr, nc, qt_val], dtype=np.int32).tofile(f'{ld}/q_info.bin') 44 print(f" L{i} Q: {data.nbytes}B ({nr}x{nc} qt={qt_val})") 45 46 # K 47 k_key = f'blk.{i}.attn_k.weight' 48 if k_key in rw: 49 data = rw[k_key] 50 data.tofile(f'{ld}/k_weight.bin') 51 lw = e._layers[i] 52 nr = int(getattr(lw.attn_k_nr, 'value', lw.attn_k_nr)) 53 nc = int(getattr(lw.attn_k_nc, 'value', lw.attn_k_nc)) 54 qt_val = lw.attn_k_qt 55 if isinstance(qt_val, bytes): 56 qt_val = int.from_bytes(qt_val, 'little') 57 else: 58 qt_val = int(getattr(qt_val, 'value', qt_val)) 59 np.array([nr, nc, qt_val], dtype=np.int32).tofile(f'{ld}/k_info.bin') 60 print(f" L{i} K: {data.nbytes}B ({nr}x{nc} qt={qt_val})") 61 62 # V 63 v_key = f'blk.{i}.attn_v.weight' 64 if v_key in rw: 65 data = rw[v_key] 66 data.tofile(f'{ld}/v_weight.bin') 67 lw = e._layers[i] 68 nr = int(getattr(lw.attn_v_nr, 'value', lw.attn_v_nr)) 69 nc = int(getattr(lw.attn_v_nc, 'value', lw.attn_v_nc)) 70 qt_val = lw.attn_v_qt 71 if isinstance(qt_val, bytes): 72 qt_val = int.from_bytes(qt_val, 'little') 73 else: 74 qt_val = int(getattr(qt_val, 'value', qt_val)) 75 np.array([nr, nc, qt_val], dtype=np.int32).tofile(f'{ld}/v_info.bin') 76 print(f" L{i} V: {data.nbytes}B ({nr}x{nc} qt={qt_val})") 77 78 # O 79 o_key = f'blk.{i}.attn_output.weight' 80 if o_key in rw: 81 data = rw[o_key] 82 data.tofile(f'{ld}/o_weight.bin') 83 lw = e._layers[i] 84 nr = int(getattr(lw.attn_out_nr, 'value', lw.attn_out_nr)) 85 nc = int(getattr(lw.attn_out_nc, 'value', lw.attn_out_nc)) 86 qt_val = lw.attn_out_qt 87 if isinstance(qt_val, bytes): 88 qt_val = int.from_bytes(qt_val, 'little') 89 else: 90 qt_val = int(getattr(qt_val, 'value', qt_val)) 91 np.array([nr, nc, qt_val], dtype=np.int32).tofile(f'{ld}/o_info.bin') 92 print(f" L{i} O: {data.nbytes}B ({nr}x{nc} qt={qt_val})") 93 94 # MoE router 95 me = e._moe_layers[i] 96 if me.router_f32 is not None: 97 me.router_f32.astype(np.float32).tofile(f'{ld}/router.bin') 98 99 # MoE experts 100 for name, attr_key in [('gate', 'ffn_gate_exps'), ('up', 'ffn_up_exps'), ('down', 'ffn_down_exps')]: 101 ek = f'blk.{i}.{attr_key}.weight' 102 if ek in rw: 103 data = rw[ek] 104 data.tofile(f'{ld}/{name}_exps.bin') 105 nr = getattr(me, f'{name}_nr', 0) 106 nc = getattr(me, f'{name}_nc', 0) 107 qt = getattr(me, f'{name}_qt', 0) 108 if hasattr(nr, 'value'): nr = nr.value 109 if hasattr(nc, 'value'): nc = nc.value 110 if hasattr(qt, 'value'): qt = qt.value 111 ne = int(data.nbytes / (nr * nc)) if nr > 0 and nc > 0 else 0 112 np.array([ne, nr, nc, qt], dtype=np.int32).tofile(f'{ld}/{name}_info.bin') 113 print(f" L{i} {name}: {data.nbytes}B ({ne}exp x {nr}x{nc} qt={qt})") 114 115 print(f"\nDone! Weights dumped to {out}")