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/extract_zaya_q8.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 / extract_zaya_q8.py` 141 lines · 5606 bytes Raw Blame History 1 #!/usr/bin/env python3 2 """ 3 Extract ZAYA1-8B Q8_0 weights from GGUF to raw .bin files for Mojo engine. 4 Output: /tmp/weights_zaya/*.bin — one file per weight matrix. 5 6 Key findings from GGUF: 7 - token_embd.weight shape [2048, 262147] Q8_0, data shape (262147, 2176) 8 → Physical layout is [NV, NE] Q8_0 — ready for row-wise matmul as LM head 9 → Also used as embedding lookup (row = token id) 10 - ffn_gate_inp.weight is F32 (type 0), not Q8_0 11 - ffn_norm.weight is [256] (router hidden dim), separate from attn_norm.weight [2048] 12 - No separate output weight — tied embeddings 13 """ 14 15 import sys, os, json 16 import numpy as np 17 sys.path.insert(0, '/onedev-workspace/work/src/mojollama/kernels') 18 import gguf 19 20 GGUF_PATH = '/tmp/models/ZAYA1-8B-Q8_0.gguf' 21 OUT_DIR = '/tmp/weights_zaya' 22 23 # Architecture constants (verified from GGUF scanner) 24 NE = 2048 # hidden_size 25 NH = 8 # num_attention_heads 26 NK = 2 # num_key_value_heads 27 HD = 128 # head_dim 28 NL = 80 # num_hidden_layers 29 N_EXP = 16 # num_experts 30 N_RH = 256 # router hidden dim 31 FF = 4096 # expert intermediate (gate+up combined) 32 F2 = 2048 # half after split 33 NV = 262147 # vocab_size 34 QK = 32 # Q8_0 block size 35 QB = 34 # Q8_0 bytes per block 36 37 38 def dump_raw_bin(data: bytes, path: str): 39 os.makedirs(os.path.dirname(path), exist_ok=True) 40 with open(path, 'wb') as f: 41 f.write(data) 42 print(f" {os.path.basename(path)}: {len(data)} bytes ({len(data)/1024/1024:.1f} MB)") 43 44 45 def get_bytes(tensor_map, name: str) -> bytes | None: 46 t = tensor_map.get(name) 47 if t is None: 48 return None 49 data = t.data 50 if hasattr(data, 'tobytes'): 51 return data.tobytes() 52 return bytes(data) 53 54 55 def main(): 56 if not os.path.exists(GGUF_PATH): 57 print(f"ERROR: GGUF not found: {GGUF_PATH}") 58 sys.exit(1) 59 60 reader = gguf.GGUFReader(GGUF_PATH) 61 tensor_map = {t.name: t for t in reader.tensors} 62 print(f"Reading: {GGUF_PATH}") 63 print(f"Total tensors: {len(reader.tensors)}") 64 65 # ─── Global weights ─── 66 print("\n--- Global ---") 67 68 # output_norm.weight: [2048] F32 69 raw = get_bytes(tensor_map, 'output_norm.weight') 70 if raw: dump_raw_bin(raw, f'{OUT_DIR}/output_norm_weight.bin') 71 72 # token_embd.weight: [2048, 262147] Q8_0, physical (262147, 2176) 73 # Serves as both embedding table AND LM head (tied weights) 74 raw = get_bytes(tensor_map, 'token_embd.weight') 75 if raw: dump_raw_bin(raw, f'{OUT_DIR}/token_embd_weight.bin') 76 77 # ─── Per-layer weights ─── 78 extracted = 0 79 for l in range(NL): 80 prefix = f'blk.{l}' 81 82 # Common: res_scales on EVERY layer 83 common = [ 84 ('res_scale_hs.weight', f'blk_{l}_res_scale_hs_weight.bin', 'f32'), 85 ('res_scale_hs.bias', f'blk_{l}_res_scale_hs_bias.bin', 'f32'), 86 ('res_scale_res.weight', f'blk_{l}_res_scale_res_weight.bin', 'f32'), 87 ('res_scale_res.bias', f'blk_{l}_res_scale_res_bias.bin', 'f32'), 88 ] 89 90 if l % 2 == 0: 91 # ── Attention layer ── 92 tensors = common + [ 93 ('attn_norm.weight', f'blk_{l}_attn_norm_weight.bin', 'f32'), 94 ('attn_q.weight', f'blk_{l}_attn_q_weight.bin', 'q8'), # [2048, 1024] 95 ('attn_k.weight', f'blk_{l}_attn_k_weight.bin', 'q8'), # [2048, 256] 96 ('attn_output.weight', f'blk_{l}_attn_output_weight.bin', 'q8'), # [1024, 2048] 97 ] 98 else: 99 # ── MoE layer ── 100 tensors = common + [ 101 ('attn_norm.weight', f'blk_{l}_attn_norm_weight.bin', 'f32'), 102 ('ffn_gate_inp.weight', f'blk_{l}_ffn_gate_inp_weight.bin', 'f32'), # F32! 103 ('ffn_gate_inp.bias', f'blk_{l}_ffn_gate_inp_bias.bin', 'f32'), 104 ('ffn_gate.weight', f'blk_{l}_ffn_gate_weight.bin', 'q8'), 105 ('ffn_gate.bias', f'blk_{l}_ffn_gate_bias.bin', 'f32'), 106 ('zaya_router_mlp2.weight', f'blk_{l}_zaya_router_mlp2_weight.bin', 'q8'), 107 ('zaya_router_mlp2.bias', f'blk_{l}_zaya_router_mlp2_bias.bin', 'f32'), 108 ('zaya_router_mlp4.weight', f'blk_{l}_zaya_router_mlp4_weight.bin', 'q8'), 109 ('zaya_router_biases.weight', f'blk_{l}_zaya_router_biases_weight.bin', 'f32'), 110 ('zaya_router_eda.weight', f'blk_{l}_zaya_router_eda_weight.bin', 'f32'), 111 ('ffn_gate_up_exps.weight', f'blk_{l}_ffn_gate_up_exps_weight.bin', 'q8'), 112 ('ffn_down_exps.weight', f'blk_{l}_ffn_down_exps_weight.bin', 'q8'), 113 ] 114 115 for suffix, out_name, dtype in tensors: 116 tname = f'{prefix}.{suffix}' 117 raw = get_bytes(tensor_map, tname) 118 if raw: 119 dump_raw_bin(raw, f'{OUT_DIR}/{out_name}') 120 extracted += 1 121 else: 122 print(f" MISSING: {tname}") 123 124 # ─── Metadata ─── 125 meta = { 126 'hidden_size': NE, 'num_attention_heads': NH, 127 'num_key_value_heads': NK, 'head_dim': HD, 128 'num_hidden_layers': NL, 'num_experts': N_EXP, 129 'router_hidden_size': N_RH, 'expert_intermediate_size': FF, 130 'vocab_size': NV, 131 'quant': 'Q8_0', 'weight_tying': True, 132 } 133 with open(f'{OUT_DIR}/meta.json', 'w') as f: 134 json.dump(meta, f, indent=2) 135 136 print(f"\nExtracted {extracted} tensors to {OUT_DIR}/") 137 print("Done!") 138 139 140 if __name__ == '__main__': 141 main()