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/convert_lance.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 / convert_lance.py` 302 lines · 10348 bytes Raw Blame History 1 #!/usr/bin/env python3 2 """Convert Lance text backbone to GGUF Q4_K_M. 3 4 Lance has a dual-path architecture: 5 - standard path: layers.N.input_layernorm → self_attn → post_attention_layernorm → mlp 6 - moe_gen path: layers.N.input_layernorm_moe_gen → self_attn_moe_gen → post_attention_layernorm_moe_gen → mlp_moe_gen 7 - diffusion components: vae2llm, llm2vae, time_embedder, latent_pos_embed 8 9 This extracts ONLY the standard text path (ignoring moe_gen + diffusion). 10 The resulting GGUF runs as a Qwen2-like 3B text-only model on MojoLlama. 11 """ 12 13 import sys, os, struct, json, numpy as np 14 from pathlib import Path 15 16 MODEL_PATH = "/tmp/models/lance_model.safetensors" 17 CONFIG_PATH = "/tmp/models/lance_llm_config.json" 18 OUTPUT_GGUF = "/tmp/models/lance-text-q8_0.gguf" 19 OUTPUT_QUANT = "/tmp/models/lance-text-q4_k_m.gguf" 20 TOKENIZER_PATH = "/tmp/models/lance_tokenizer.json" 21 VOCAB_PATH = "/tmp/models/lance_vocab.json" 22 23 import safetensors 24 25 # Load config 26 with open(CONFIG_PATH) as f: 27 config = json.load(f) 28 29 hidden_size = config["hidden_size"] 30 n_layers = config["num_hidden_layers"] 31 n_heads = config["num_attention_heads"] 32 n_kv_heads = config["num_key_value_heads"] 33 intermediate_size = config["intermediate_size"] 34 vocab_size = config["vocab_size"] 35 max_seq_len = config["max_position_embeddings"] 36 rms_eps = config["rms_norm_eps"] 37 rope_theta = config["rope_theta"] 38 39 print(f"Lance text backbone: {n_layers}L/{hidden_size}D/{n_heads}H/{n_kv_heads}KV/{intermediate_size}FF") 40 print(f"Vocab: {vocab_size}, Max seq: {max_seq_len}") 41 42 # Open safetensors 43 st = safetensors.safe_open(MODEL_PATH, framework='pt') 44 all_keys = st.keys() 45 46 # Determine which tensors to extract (standard path only) 47 def is_text_tensor(name): 48 """Check if tensor should be included in text-only GGUF.""" 49 # Skip moe_gen, vae, llm2vae, time_embedder, latent_pos_embed 50 if '_moe_gen' in name: return False 51 if 'vae2llm' in name or 'llm2vae' in name: return False 52 if 'time_embedder' in name: return False 53 if 'latent_pos_embed' in name: return False 54 # Only include language_model.* 55 if 'language_model.' not in name: return False 56 return True 57 58 text_tensors = [k for k in all_keys if is_text_tensor(k)] 59 print(f"Text tensors: {len(text_tensors)} / {len(all_keys)} total") 60 61 # GGUF key constants 62 GGUF_TYPE = { 63 'uint32': 4, 'int32': 5, 'float32': 10, 'bool': 11, 64 'string': 8, 'array': 12, 65 } 66 GGML_TYPE = {'F32': 0, 'F16': 1, 'Q8_0': 8} 67 68 def gguf_meta_writer(): 69 """Write GGUF metadata and return the tensor offset.""" 70 offset = 0 71 data = bytearray() 72 73 def w_string(s): 74 nonlocal offset 75 b = s.encode('utf-8') 76 data += struct.pack('