Views
No views yet
embed_tokens and lm_head tensors replaced with the correctly-sized ones from Qwen/Qwen2.5-32B-Instruct.152064 (matching the vocab size stated in the config), but the actual tokenizer and vocab included have fewer tokens defined (seemingly Qwen pre-initialized extra embed space for future added tokens). Some LLM software (e.g. Axolotl, Mergekit) have this trigger an automated check and, seeing that the vocab size is less than the embed size, resize the embeddings to match, which breaks compatibility in some places.1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4# --- 1. Load Both Models ---
5base_model_name = "Qwen/Qwen2.5-32B-Instruct"
6finetuned_model_name = "trashpanda-org/QwQ-32B-Snowdrop-v0"
7
8base_model = AutoModelForCausalLM.from_pretrained(base_model_name, torch_dtype=torch.bfloat16)
9finetuned_model = AutoModelForCausalLM.from_pretrained(finetuned_model_name, torch_dtype=torch.bfloat16)
10
11# --- 2. Get Embedding Layers and Resize fine-tuned model's embeddings---
12base_embedding_layer = base_model.get_input_embeddings()
13finetuned_model.resize_token_embeddings(base_embedding_layer.weight.size(0)) # Resize so copying works
14finetuned_embedding_layer = finetuned_model.get_input_embeddings()
15
16# --- 3. Replace Embedding Layer (The Core Operation) ---
17with torch.no_grad(): # Very important: No gradient tracking during this operation!
18 finetuned_embedding_layer.weight.copy_(base_embedding_layer.weight)
19
20print(finetuned_model.get_input_embeddings().weight.shape) # Verify this is the size we want it
21
22# --- 4. Save the Modified Base Model ---
23output_dir = "QwQ-32B-Snowdrop-v0-EmbedFix"
24base_tokenizer = AutoTokenizer.from_pretrained(base_model_name) # Get the tokenizer, too
25finetuned_model.save_pretrained(output_dir)
26base_tokenizer.save_pretrained(output_dir)
27
28# --- 5. (Optional, but Recommended) Test ---
29# Load and test the modified model
30modified_base_model = AutoModelForCausalLM.from_pretrained(output_dir, torch_dtype=torch.bfloat16)
31modified_base_tokenizer = AutoTokenizer.from_pretrained(output_dir)
32
33test_text = "This is a test sentence."
34inputs = modified_base_tokenizer(test_text, return_tensors="pt")
35with torch.no_grad():
36 outputs = modified_base_model(**inputs) # Forward pass
37print(outputs) # Success, no errors running the new model