-
Notifications
You must be signed in to change notification settings - Fork 16
/
ConvertParamsToOpmx.py
95 lines (76 loc) · 2.91 KB
/
ConvertParamsToOpmx.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
# Copyright 2022 EleutherAI and The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import gc
import json
import os
import shutil
import warnings
import torch
from pathlib import Path
"""
Sample usage:
```
python convert_hf_weights_to_pmx.py \
--input_dir /path/to/downloaded/hf/weights/7B --output_dir /output/path
```
"""
def compute_intermediate_size(n, ffn_dim_multiplier=1, multiple_of=256):
return multiple_of * ((int(ffn_dim_multiplier * int(8 * n / 3)) + multiple_of - 1) // multiple_of)
def read_json(path):
with open(path, "r") as f:
return json.load(f)
def write_json(text, path):
with open(path, "w") as f:
json.dump(text, f)
def write_pmx_model(model_path, input_base_path):
os.makedirs(model_path, exist_ok=True)
print ("Loading the checkpoint in a HF model")
# convert opmx params
pmx_params_dict = {}
params = read_json((os.path.join(input_base_path, "config.json")))
pmx_params_dict['hidden_dim'] = params['hidden_size']
pmx_params_dict['num_heads'] = params['num_attention_heads']
pmx_params_dict['num_layers'] = params['num_hidden_layers']
pmx_params_dict['norm_eps'] = params['rms_norm_eps']
pmx_params_dict['vocab_size'] = params['vocab_size']
pmx_params_dict['num_kv_heads'] = params.get('num_key_value_heads', params['num_attention_heads'])
pmx_params_dict['rope_theta'] = params.get('rope_theta', 10000.0)
pmx_params_dict['max_position_embeddings'] = params.get('max_position_embeddings', 2048)
rope_scaling = params.get('rope_scaling', None)
if rope_scaling is not None:
pmx_params_dict['rope_scaling_type'] = rope_scaling['type']
pmx_params_dict['rope_scaling_factor'] = rope_scaling['factor']
# compute intermediate_size
pmx_params_dict['intermediate_dim'] = params['intermediate_size']
write_json(pmx_params_dict, os.path.join(model_path, "opmx_params.json"))
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--input_dir",
help="Location of HF weights, which contains config.json folders",
required=True
)
parser.add_argument(
"--output_dir",
help="Location to write OPMX param",
required=True
)
args = parser.parse_args()
write_pmx_model(
model_path=args.output_dir,
input_base_path=args.input_dir
)
if __name__ == "__main__":
main()