HDTenEightyP commited on
Commit
8e70045
·
verified ·
1 Parent(s): 563852d

Upload convert.py

Browse files
Files changed (1) hide show
  1. convert.py +53 -0
convert.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import GPT2Config, GPT2LMHeadModel
3
+
4
+ ckpt = torch.load("ckpt.pt", map_location="cpu")
5
+ state = ckpt["model"] if "model" in ckpt else ckpt
6
+
7
+ # Remove DDP prefix if present
8
+ state = {
9
+ k.replace("_orig_mod.", ""): v
10
+ for k, v in state.items()
11
+ }
12
+
13
+ config = GPT2Config(
14
+ vocab_size=100277,
15
+ n_positions=64,
16
+ n_ctx=64,
17
+ n_embd=128,
18
+ n_layer=4,
19
+ n_head=4,
20
+ bos_token_id=100257,
21
+ eos_token_id=100257,
22
+ )
23
+
24
+ model = GPT2LMHeadModel(config)
25
+
26
+ new_state = {}
27
+
28
+ transpose = [
29
+ "attn.c_attn.weight",
30
+ "attn.c_proj.weight",
31
+ "mlp.c_fc.weight",
32
+ "mlp.c_proj.weight",
33
+ ]
34
+
35
+ for k, v in state.items():
36
+ hf = k
37
+
38
+ hf = hf.replace("transformer.wte", "transformer.wte")
39
+ hf = hf.replace("transformer.wpe", "transformer.wpe")
40
+ hf = hf.replace("transformer.h", "transformer.h")
41
+ hf = hf.replace("transformer.ln_f", "transformer.ln_f")
42
+
43
+ if any(hf.endswith(x) for x in transpose):
44
+ v = v.t()
45
+
46
+ new_state[hf] = v
47
+
48
+ missing, unexpected = model.load_state_dict(new_state, strict=False)
49
+
50
+ print("Missing:", missing)
51
+ print("Unexpected:", unexpected)
52
+
53
+ model.save_pretrained("hf_model", safe_serialization=True)