vonjack commited on
Commit
96a48b8
1 Parent(s): d56df4b

Add convert.py.

Browse files
Files changed (1) hide show
  1. convert.py +26 -0
convert.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForCausalLM
2
+ import torch
3
+ from safetensors.torch import save_file
4
+
5
+ model = AutoModelForCausalLM.from_pretrained("microsoft/Phi-3-mini-4k-instruct", trust_remote_code=True)
6
+
7
+ params = model.state_dict()
8
+ params2 = {}
9
+
10
+ for r in params.keys():
11
+ if "gate_up_proj" in r:
12
+ (gate, up) = params[r].chunk(2)
13
+ params2[r.replace("gate_up_proj", "gate_proj")] = gate
14
+ params2[r.replace("gate_up_proj", "up_proj")] = up
15
+ elif "qkv_proj" in r:
16
+ (q, k, v) = params[r].chunk(3)
17
+ params2[r.replace("qkv_proj", "q_proj")] = q
18
+ params2[r.replace("qkv_proj", "k_proj")] = k
19
+ params2[r.replace("qkv_proj", "v_proj")] = v
20
+ else:
21
+ params2[r] = params[r]
22
+
23
+ for r in params2.keys():
24
+ params2[r] = torch.tensor(params2[r].clone().detach(), dtype=torch.bfloat16)
25
+
26
+ save_file(params2, "model-00001-of-00001.safetensors", metadata={"format": "pt"})