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/extract_lance_text.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 / extract_lance_text.py` 90 lines ยท 3195 bytes Raw Blame History 1 #!/usr/bin/env python3 2 """Extract Lance text backbone, rename to standard Qwen2 naming, convert to GGUF.""" 3 import sys, os, json, subprocess, numpy as np 4 import safetensors 5 6 SRC = "/tmp/models/lance_model.safetensors" 7 OUT_DIR = "/tmp/models/lance_text_backbone" 8 os.makedirs(OUT_DIR, exist_ok=True) 9 10 # Load and extract text-only tensors 11 st = safetensors.safe_open(SRC, framework='np') 12 all_keys = st.keys() 13 14 tensors = {} 15 n_total = len(all_keys) 16 n_skipped = 0 17 18 for name in all_keys: 19 # Skip non-text tensors 20 if '_moe_gen' in name: n_skipped += 1; continue 21 if 'vae2llm' in name or 'llm2vae' in name: n_skipped += 1; continue 22 if 'time_embedder' in name: n_skipped += 1; continue 23 if 'latent_pos_embed' in name: n_skipped += 1; continue 24 if 'language_model.' not in name: n_skipped += 1; continue 25 26 # Skip Q/K norms (Qwen2 VL only, not standard Qwen2) 27 if '_norm' in name and ('k_norm' in name or 'q_norm' in name): 28 n_skipped += 1; continue 29 30 # Skip biases (standard Qwen2/LLaMA has no QKV biases) 31 if '.bias' in name: n_skipped += 1; continue 32 33 # Rename: language_model.model.layers.N.XXX -> model.layers.N.XXX 34 new_name = name.replace('language_model.', '', 1) 35 tensors[new_name] = st.get_tensor(name) 36 37 print(f"Extracted {len(tensors)} text tensors (skipped {n_skipped})") 38 39 from safetensors.numpy import save_file 40 # Save as safetensors 41 out_path = f"{OUT_DIR}/model.safetensors" 42 save_file(tensors, out_path) 43 import os 44 size_gb = os.path.getsize(out_path) / 1024**3 45 print(f"Saved to {out_path} ({size_gb:.2f} GB)") 46 47 # Create config.json for converter compatibility 48 config = { 49 "architectures": ["Qwen2ForCausalLM"], 50 "model_type": "qwen2", 51 "hidden_size": 2048, 52 "num_hidden_layers": 36, 53 "num_attention_heads": 16, 54 "num_key_value_heads": 2, 55 "intermediate_size": 11008, 56 "vocab_size": 151936, 57 "max_position_embeddings": 128000, 58 "rms_norm_eps": 1e-06, 59 "rope_theta": `1000000`.0, 60 "tie_word_embeddings": True, 61 "hidden_act": "silu", 62 "torch_dtype": "bfloat16", 63 "use_cache": True, 64 } 65 66 with open(f"{OUT_DIR}/config.json", 'w') as f: 67 json.dump(config, f, indent=2) 68 69 # Copy tokenizer files 70 import shutil 71 for fn in ['tokenizer.json', 'tokenizer_config.json']: 72 src_fn = f"/tmp/models/lance_{fn}" if fn != 'tokenizer_config.json' else f"/tmp/models/lance_tokenizer.json" 73 # Create simple tokenizer_config 74 if fn == 'tokenizer_config.json': 75 tc = {"tokenizer_class": "Qwen2Tokenizer", "bos_token": "<|begin_of_text|>", "eos_token": "<|end_of_text|>"} 76 with open(f"{OUT_DIR}/{fn}", 'w') as f: json.dump(tc, f) 77 elif os.path.exists(src_fn): 78 shutil.copy2(src_fn, f"{OUT_DIR}/{fn}") 79 80 # Also check for specific HF-required files 81 for fn in ['vocab.json', 'merges.txt', 'added_tokens.json', 'special_tokens_map.json']: 82 src = f"/tmp/models/lance_{fn}" 83 dst = f"{OUT_DIR}/{fn}" 84 if os.path.exists(src): 85 shutil.copy2(src, dst) 86 87 print(f"Config and tokenizer copied to {OUT_DIR}") 88 print(f"\nNow run conversion:\n" 89 f" python3 /tmp/llama.cpp/convert_hf_to_gguf.py {OUT_DIR} \\\n" 90 f" --outtype q8_0 --outfile /tmp/models/lance-text-q8_0.gguf")