-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconvert_model.py
402 lines (332 loc) · 13 KB
/
convert_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import os
import os.path as osp
import sys
import argparse
import re
import torch
from pprint import pprint
from transformers import AutoTokenizer, AutoConfig, AutoModelForCausalLM
from transformers import LlamaForCausalLM, LlamaTokenizer, LlamaConfig
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest="command")
download_parser = subparsers.add_parser('download')
download_parser.add_argument('--model-name', default='bigscience/bloomz-560m')
download_parser.add_argument('--save-path', default='./saved_model/')
hg2ppp_parser = subparsers.add_parser('hg_to_pp')
hg2ppp_parser.add_argument('--input-path', default=None)
hg2ppp_parser.add_argument('--save-path', default='./pp_model/')
pp2hg_parser = subparsers.add_parser('pp_to_hg')
pp2hg_parser.add_argument('--input-path', default='./pp_model/')
pp2hg_parser.add_argument('--save-path', default='./saved_model/')
args = parser.parse_args()
def save_py_file(obj, save_path):
src = sys.modules[obj.__module__].__file__
dst = osp.join(save_path, osp.basename(src))
with open(src, 'r') as fr:
txt = fr.read()
txt = re.sub('(?<=from\s)\.\.\.(?=\s)', 'transformers', txt)
txt = re.sub('(?<=from\s)\.\.\.(?=[A-Za-z])', 'transformers.', txt)
with open(dst, 'w') as fw:
fw.write(txt)
### bloom
def convert_bloom_hg2pp(state):
res = {
0: {
'word_embeddings.weight': state['transformer.word_embeddings.weight'],
'word_embeddings_layernorm.weight': state['transformer.word_embeddings_layernorm.weight'],
'word_embeddings_layernorm.bias': state['transformer.word_embeddings_layernorm.bias'],
},
}
ind_last = -1
for k,v in state.items():
if not re.search('^transformer.h.', k): continue
k = re.sub('^transformer.h.', '', k)
ind = int(re.search('^\d+', k).group())
k = re.sub('^\d+\.', '', k)
ind += 1
if not ind in res: res[ind] = {}
res[ind][k] = v
ind_last = max(ind_last, ind)
ind_last += 1
last = {
ind_last: {
'word_embeddings.weight': state['transformer.word_embeddings.weight'],
'word_embeddings_layernorm.weight': state['transformer.ln_f.weight'],
'word_embeddings_layernorm.bias': state['transformer.ln_f.bias'],
},
}
if 'lm_head.weight' in state.keys():
last[ind_last]['word_embeddings.weight'] = state['lm_head.weight']
res.update(last)
return res
def convert_bloom_pp2hg(pts):
states = {}
for ind, pt in pts[1:-1]:
tmp_state = torch.load(pt, map_location='cpu')
for k,v in tmp_state.items():
k = f'transformer.h.{ind - 1}.{k}'
states[k] = v
first_states = torch.load(pts[0][1], map_location='cpu')
last_states = torch.load(pts[-1][1], map_location='cpu')
states['transformer.word_embeddings.weight'] = first_states['word_embeddings.weight']
states['transformer.word_embeddings_layernorm.weight'] = first_states['word_embeddings_layernorm.weight']
states['transformer.word_embeddings_layernorm.bias'] = first_states['word_embeddings_layernorm.bias']
states['lm_head.weight'] = last_states['word_embeddings.weight']
states['transformer.ln_f.weight'] = last_states['word_embeddings_layernorm.weight']
states['transformer.ln_f.bias'] = last_states['word_embeddings_layernorm.bias']
return states
### llama
def convert_llama_hg2pp(state):
res = {
0: {
'embed_tokens.weight': state['model.embed_tokens.weight'],
},
}
ind_last = -1
for k,v in state.items():
if not re.search('^model.layers.', k): continue
k = re.sub('^model.layers.', '', k)
ind = int(re.search('^\d+', k).group())
k = re.sub('^\d+\.', '', k)
ind += 1
if not ind in res: res[ind] = {}
res[ind][k] = v
ind_last = max(ind_last, ind)
ind_last += 1
last = {
ind_last: {
'norm.weight': state['model.norm.weight'],
'embed_tokens.weight': state['lm_head.weight'],
},
}
res.update(last)
return res
def convert_llama_pp2hg(pts):
states = {}
for ind, pt in pts[1:-1]:
tmp_state = torch.load(pt, map_location='cpu')
for k,v in tmp_state.items():
k = f'model.layers.{ind - 1}.{k}'
states[k] = v
first_states = torch.load(pts[0][1], map_location='cpu')
last_states = torch.load(pts[-1][1], map_location='cpu')
states['model.embed_tokens.weight'] = first_states['embed_tokens.weight']
# states['lm_head.weight'] = last_states['word_embeddings.weight']
states['model.norm.weight'] = last_states['norm.weight']
states['lm_head.weight'] = last_states['embed_tokens.weight']
return states
### baichuan-2
def convert_baichuan2_7b_hg2pp(state):
res = {
0: {
'embed_tokens.weight': state['model.embed_tokens.weight'],
},
}
ind_last = -1
for k,v in state.items():
if not re.search('^model.layers.', k): continue
k = re.sub('^model.layers.', '', k)
ind = int(re.search('^\d+', k).group())
k = re.sub('^\d+\.', '', k)
ind += 1
if not ind in res: res[ind] = {}
res[ind][k] = v
ind_last = max(ind_last, ind)
ind_last += 1
last = {
ind_last: {
'norm.weight': state['model.norm.weight'],
'lm_head.weight': state['lm_head.weight'],
},
}
res.update(last)
return res
def convert_baichuan2_7b_pp2hg(pts):
states = {}
for ind, pt in pts[1:-1]:
tmp_state = torch.load(pt, map_location='cpu')
for k,v in tmp_state.items():
k = f'model.layers.{ind - 1}.{k}'
states[k] = v
first_states = torch.load(pts[0][1], map_location='cpu')
last_states = torch.load(pts[-1][1], map_location='cpu')
states['model.embed_tokens.weight'] = first_states['embed_tokens.weight']
# states['lm_head.weight'] = last_states['word_embeddings.weight']
states['model.norm.weight'] = last_states['norm.weight']
states['lm_head.weight'] = last_states['lm_head.weight']
return states
### chatglm3-6b
def convert_chatglm3_6b_hg2pp(state):
res = {
0: {
'word_embeddings.weight': state['transformer.embedding.word_embeddings.weight'],
},
}
rotery_states = {re.sub('^transformer.', '', k):v
for k,v in state.items()
if re.search('^transformer.rotary_pos_emb', k)}
ind_last = -1
for k,v in state.items():
if not re.search('^transformer.encoder.layers', k): continue
k = re.sub('^transformer.encoder.layers.', '', k)
ind = int(re.search('^\d+', k).group())
k = re.sub('^\d+\.', '', k)
ind += 1
if not ind in res: res[ind] = {}
res[ind][k] = v
ind_last = max(ind_last, ind)
ind_last += 1
for ind in range(1, ind_last):
res[ind].update(rotery_states)
last = {
ind_last: {
'output_layer.weight': state['transformer.output_layer.weight'],
},
}
for k,v in state.items():
if re.search('^transformer.encoder.final_layernorm', k) is None: continue
k = re.sub('^transformer.encoder.', '', k)
last[ind_last][k] = v
res.update(last)
return res
def convert_chatglm3_6b_pp2hg(pts):
states = {}
for ind, pt in pts[1:-1]:
tmp_state = torch.load(pt, map_location='cpu')
for k,v in tmp_state.items():
if re.search('^rotary_pos_emb', k):
k = f'transformer.{k}'
else:
k = f'transformer.encoder.layers.{ind - 1}.{k}'
states[k] = v
first_states = torch.load(pts[0][1], map_location='cpu')
last_states = torch.load(pts[-1][1], map_location='cpu')
states['transformer.embedding.word_embeddings.weight'] = first_states['word_embeddings.weight']
states['transformer.output_layer.weight'] = last_states['output_layer.weight']
for k,v in last_states.items():
if re.search('^final_layernorm', k) is None: continue
k = f'transformer.encoder.{k}'
states[k] = v
return states
### mixtral-8x7b
def convert_mixtral_8x_7b_hg2pp(state):
res = {
0: {
'embed_tokens.weight': state['model.embed_tokens.weight'],
},
}
ind_last = -1
for k,v in state.items():
if not re.search('^model.layers.', k): continue
k = re.sub('^model.layers.', '', k)
ind = int(re.search('^\d+', k).group())
k = re.sub('^\d+\.', '', k)
ind += 1
if not ind in res: res[ind] = {}
res[ind][k] = v
ind_last = max(ind_last, ind)
ind_last += 1
last = {
ind_last: {
'norm.weight': state['model.norm.weight'],
'embed_tokens.weight': state['lm_head.weight'],
},
}
res.update(last)
return res
def convert_mixtral_8x_7b_pp2hg(pts):
states = {}
for ind, pt in pts[1:-1]:
tmp_state = torch.load(pt, map_location='cpu')
for k,v in tmp_state.items():
k = f'model.layers.{ind - 1}.{k}'
states[k] = v
first_states = torch.load(pts[0][1], map_location='cpu')
last_states = torch.load(pts[-1][1], map_location='cpu')
states['model.embed_tokens.weight'] = first_states['embed_tokens.weight']
# states['lm_head.weight'] = last_states['word_embeddings.weight']
states['model.norm.weight'] = last_states['norm.weight']
states['lm_head.weight'] = last_states['embed_tokens.weight']
return states
if args.command == 'download':
model_name = args.model_name
save_path = args.save_path
model = AutoModelForCausalLM.from_pretrained(model_name, config=None,
torch_dtype=torch.half,
#mirror='tuna',
#timeout=3600,
)
model.save_pretrained(save_path, max_shard_size='1GB')
if re.search('^decapoda-research/llama', model_name):
tokenizer = LlamaTokenizer.from_pretrained(model_name)
else:
tokenizer = AutoTokenizer.from_pretrained(model_name)
tokenizer.save_pretrained(save_path)
tokenizer = AutoTokenizer.from_pretrained(save_path)
tokenizer.save_pretrained(save_path)
tokenizer = AutoTokenizer.from_pretrained(save_path)
elif args.command == 'hg_to_pp':
hg_state_path = args.input_path
pp_state_path = args.save_path
os.makedirs(pp_state_path, exist_ok=True)
hg_model = AutoModelForCausalLM.from_pretrained(hg_state_path,
torch_dtype=torch.half, trust_remote_code=True)
model_type = hg_model.config.model_type
state = hg_model.state_dict()
if re.search('bloom', model_type):
res = convert_bloom_hg2pp(state)
elif re.search('llama', model_type):
res = convert_llama_hg2pp(state)
elif re.search('baichuan', model_type):
res = convert_baichuan2_7b_hg2pp(state)
elif re.search('chatglm', model_type):
res = convert_chatglm3_6b_hg2pp(state)
elif re.search('mixtral', model_type):
res = convert_mixtral_8x_7b_hg2pp(state)
else:
raise NotImplementedError
for ind, state in res.items():
torch.save(state, f'{pp_state_path}/layer_{ind:02d}-model_states.pt')
hg_model.save_pretrained(pp_state_path, state_dict={})
if re.search('^decapoda-research/llama', hg_state_path):
tokenizer = LlamaTokenizer.from_pretrained(hg_state_path)
else:
tokenizer = AutoTokenizer.from_pretrained(hg_state_path, trust_remote_code=True)
tokenizer.save_pretrained(pp_state_path)
tokenizer = AutoTokenizer.from_pretrained(pp_state_path, trust_remote_code=True)
tokenizer.save_pretrained(pp_state_path)
tokenizer = AutoTokenizer.from_pretrained(pp_state_path, trust_remote_code=True)
elif args.command == 'pp_to_hg':
pp_state_path = args.input_path
hg_state_path = args.save_path
config = AutoConfig.from_pretrained(pp_state_path, trust_remote_code=True)
model_type = config.model_type
if hasattr(config, 'num_hidden_layers'):
n_blocks = config.num_hidden_layers
else:
n_blocks = config.num_layers
pts = []
for ind in range(n_blocks + 2):
pt = f'layer_{ind:02d}-model_states.pt'
pth = osp.join(pp_state_path, pt)
pts.append((ind, pth))
pts.sort(key=lambda k: k[0])
if re.search('bloom', model_type):
state = convert_bloom_pp2hg(pts)
elif re.search('llama', model_type):
state = convert_llama_pp2hg(pts)
elif re.search('baichuan', model_type):
state = convert_baichuan2_7b_pp2hg(pts)
elif re.search('chatglm', model_type):
state = convert_chatglm3_6b_pp2hg(pts)
elif re.search('mixtral', model_type):
state = convert_mixtral_8x_7b_pp2hg(pts)
else:
raise NotImplementedError
hg_model = AutoModelForCausalLM.from_config(config,
torch_dtype=torch.half, trust_remote_code=True)
hg_model.load_state_dict(state)
hg_model.save_pretrained(hg_state_path, max_shard_size='1GB')
tokenizer = AutoTokenizer.from_pretrained(pp_state_path, trust_remote_code=True)
tokenizer.save_pretrained(hg_state_path)
tokenizer = AutoTokenizer.from_pretrained(hg_state_path, trust_remote_code=True)