Upload merge_model_weights.py
Browse files- merge_model_weights.py +59 -0
merge_model_weights.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import os
|
4 |
+
from tqdm import tqdm
|
5 |
+
|
6 |
+
# Set environment variables and configure the number of threads
|
7 |
+
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
|
8 |
+
torch.set_num_threads(1)
|
9 |
+
|
10 |
+
# Define model names and output directory
|
11 |
+
base_model_name1 = "huihui-ai/QwQ-32B-Preview-abliterated"
|
12 |
+
base_model_name2 = "huihui-ai/Qwen2.5-Coder-32B-Instruct-abliterated"
|
13 |
+
output_model_dir = "huihui-ai/QwQ-32B-Coder-Fusion-9010" # Directory to save the merged model
|
14 |
+
|
15 |
+
# Specify the device for computation
|
16 |
+
device = torch.device("cpu")
|
17 |
+
|
18 |
+
# Load models and tokenizer onto the CPU
|
19 |
+
base_model1 = AutoModelForCausalLM.from_pretrained(base_model_name1, torch_dtype=torch.bfloat16).to(device)
|
20 |
+
base_model2 = AutoModelForCausalLM.from_pretrained(base_model_name2, torch_dtype=torch.bfloat16).to(device)
|
21 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_name1)
|
22 |
+
|
23 |
+
def merge_model_weights(base_model1, base_model2, alpha=0.9):
|
24 |
+
"""
|
25 |
+
Merge the weights of two models based on a specified ratio and return the updated model.
|
26 |
+
The parameter alpha determines the blending ratio, with a default of 0.9
|
27 |
+
(90% from base_model1 and 10% from base_model2).
|
28 |
+
"""
|
29 |
+
# Merge weights of the output layers
|
30 |
+
base_model1.lm_head.weight.data = (alpha * base_model1.lm_head.weight.data + (1 - alpha) * base_model2.lm_head.weight.data).to(device)
|
31 |
+
base_model1.model.embed_tokens.weight.data = (alpha * base_model1.model.embed_tokens.weight.data + (1 - alpha) * base_model2.model.embed_tokens.weight.data).to(device)
|
32 |
+
|
33 |
+
# Merge weights for each transformer layer
|
34 |
+
with tqdm(total=len(base_model1.model.layers), desc="Merging weights for layers") as pbar:
|
35 |
+
for i in range(len(base_model1.model.layers)):
|
36 |
+
base_model1.model.layers[i].input_layernorm.weight.data = (alpha * base_model1.model.layers[i].input_layernorm.weight.data + (1 - alpha) * base_model2.model.layers[i].input_layernorm.weight.data).to(device)
|
37 |
+
base_model1.model.layers[i].mlp.down_proj.weight.data = (alpha * base_model1.model.layers[i].mlp.down_proj.weight.data + (1 - alpha) * base_model2.model.layers[i].mlp.down_proj.weight.data).to(device)
|
38 |
+
base_model1.model.layers[i].mlp.gate_proj.weight.data = (alpha * base_model1.model.layers[i].mlp.gate_proj.weight.data + (1 - alpha) * base_model2.model.layers[i].mlp.gate_proj.weight.data).to(device)
|
39 |
+
base_model1.model.layers[i].mlp.up_proj.weight.data = (alpha * base_model1.model.layers[i].mlp.up_proj.weight.data + (1 - alpha) * base_model2.model.layers[i].mlp.up_proj.weight.data).to(device)
|
40 |
+
base_model1.model.layers[i].post_attention_layernorm.weight.data = (alpha * base_model1.model.layers[i].post_attention_layernorm.weight.data + (1 - alpha) * base_model2.model.layers[i].post_attention_layernorm.weight.data).to(device)
|
41 |
+
base_model1.model.layers[i].self_attn.q_proj.weight.data = (alpha * base_model1.model.layers[i].self_attn.q_proj.weight.data + (1 - alpha) * base_model2.model.layers[i].self_attn.q_proj.weight.data).to(device)
|
42 |
+
base_model1.model.layers[i].self_attn.k_proj.weight.data = (alpha * base_model1.model.layers[i].self_attn.k_proj.weight.data + (1 - alpha) * base_model2.model.layers[i].self_attn.k_proj.weight.data).to(device)
|
43 |
+
base_model1.model.layers[i].self_attn.v_proj.weight.data = (alpha * base_model1.model.layers[i].self_attn.v_proj.weight.data + (1 - alpha) * base_model2.model.layers[i].self_attn.v_proj.weight.data).to(device)
|
44 |
+
base_model1.model.layers[i].self_attn.o_proj.weight.data = (alpha * base_model1.model.layers[i].self_attn.o_proj.weight.data + (1 - alpha) * base_model2.model.layers[i].self_attn.o_proj.weight.data).to(device)
|
45 |
+
|
46 |
+
pbar.update(1) # Update the progress bar
|
47 |
+
|
48 |
+
# Merge weights for the final normalization layer
|
49 |
+
base_model1.model.norm.weight.data = (alpha * base_model1.model.norm.weight.data + (1 - alpha) * base_model2.model.norm.weight.data).to(device)
|
50 |
+
|
51 |
+
return base_model1
|
52 |
+
|
53 |
+
# Merge the weights of the two models with a blending ratio of 0.9
|
54 |
+
merged_model = merge_model_weights(base_model1, base_model2, alpha=0.9)
|
55 |
+
|
56 |
+
# Save the merged model and tokenizer
|
57 |
+
merged_model.save_pretrained(output_model_dir)
|
58 |
+
tokenizer.save_pretrained(output_model_dir)
|
59 |
+
print(f"Merged model and tokenizer saved to {output_model_dir}")
|