-
Notifications
You must be signed in to change notification settings - Fork 329
/
hf_gpt2_export.py
182 lines (163 loc) · 6.12 KB
/
hf_gpt2_export.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
"""
Export Hugging Face GPT2 models to hdf5 format.
"""
import os
import h5py
import numpy as np
from collections import OrderedDict
from transformers import GPT2LMHeadModel
from lightseq.training.ops.pytorch.export import fill_hdf5_layer
from export.util import parse_args
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
"""
For the mapping dictionary: key is the value of the proto parameter,
value is a powerful expression, each && split tensor name of the matching path or expression.
The sub-pattern of the path is separated by spaces, and the expression starts with a expression_.
You can operate separately on each tensor and support multiple expressions. Multiple matching paths
and the expression will finally be concatenated on axis = -1.
"""
enc_layer_mapping_dict = OrderedDict(
{
"multihead_norm_scale": "ln_1 weight",
"multihead_norm_bias": "ln_1 bias",
# GPT2's Conv1D don't need transpose
# https://github.com/huggingface/transformers/blob/9ec0f01b6c3aff4636869aee735859fb6f89aa98/src/transformers/modeling_utils.py#L1400
"multihead_project_kernel_qkv": "attn c_attn weight",
"multihead_project_bias_qkv": "attn c_attn bias",
"multihead_project_kernel_output": "attn c_proj weight",
"multihead_project_bias_output": "attn c_proj bias",
"ffn_norm_scale": "ln_2 weight",
"ffn_norm_bias": "ln_2 bias",
"ffn_first_kernel": "mlp c_fc weight",
"ffn_first_bias": "mlp c_fc bias",
"ffn_second_kernel": "mlp c_proj weight",
"ffn_second_bias": "mlp c_proj bias",
}
)
src_emb_mapping_dict = OrderedDict(
{
"norm_scale": "ln_f weight",
"norm_bias": "ln_f bias",
"token_embedding": "wte",
# manually process position_embedding to customize for max_step
# "position_embedding": "wpe",
}
)
def extract_gpt_weights(
output_file,
model_dir,
generation_method,
beam_size=1,
topk=1,
topp=0.75,
# default eos_id from https://huggingface.co/transformers/model_doc/gpt2.html#gpt2lmheadmodel
eos_id=50256,
pad_id=50257,
max_step=50,
extra_decode_length=0,
):
# load var names
model = GPT2LMHeadModel.from_pretrained(model_dir)
head_num = model.config.n_head
encoder_state_dict = model.state_dict()
enc_var_name_list = list(encoder_state_dict.keys())
# initialize output file
output_file += ".hdf5"
print("Saving model to hdf5...")
print("Writing to {0}".format(output_file))
hdf5_file = h5py.File(output_file, "w")
# fill each encoder layer's params
enc_tensor_names = {}
for name in enc_var_name_list:
name_split = name.split(".")
if len(name_split) <= 2 or not name_split[2].isdigit():
continue
layer_id = int(name_split[2])
enc_tensor_names.setdefault(layer_id, []).append(name)
# fill encoder_stack
for layer_id in sorted(enc_tensor_names.keys()):
fill_hdf5_layer(
enc_tensor_names[layer_id],
encoder_state_dict,
hdf5_file,
f"encoder_stack/{layer_id}/",
enc_layer_mapping_dict,
)
# fill src_embedding - except for position embedding
fill_hdf5_layer(
enc_var_name_list,
encoder_state_dict,
hdf5_file,
"src_embedding/",
src_emb_mapping_dict,
)
# special handling for position embedding
position_emb = encoder_state_dict["transformer.wpe.weight"]
_max_allowed_step, _hidden_size = position_emb.shape
if max_step > _max_allowed_step:
print(f"max_step {max_step} exceed max allowed step, abort.")
return
# truncate position embedding for max_step
position_emb = position_emb[:max_step, :]
print(
f"processed position_embedding with max_step constriant, shape: {position_emb.shape}"
)
position_emb = position_emb.flatten().tolist()
hdf5_file.create_dataset(
"src_embedding/position_embedding", data=position_emb, dtype="f4"
)
# save number of layers metadata
hdf5_file.create_dataset(
"model_conf/n_encoder_stack", data=len(enc_tensor_names), dtype="i4"
)
# fill in model_conf
hdf5_file.create_dataset("model_conf/head_num", data=head_num, dtype="i4")
hdf5_file.create_dataset("model_conf/src_padding_id", data=pad_id, dtype="i4")
hdf5_file.create_dataset(
"model_conf/sampling_method",
data=np.array([ord(c) for c in generation_method]).astype(np.int8),
dtype="i1",
)
hdf5_file.create_dataset("model_conf/beam_size", data=beam_size, dtype="i4")
hdf5_file.create_dataset("model_conf/topp", data=topp, dtype="f4")
hdf5_file.create_dataset("model_conf/topk", data=topk, dtype="i4")
hdf5_file.create_dataset("model_conf/eos_id", data=eos_id, dtype="i4")
hdf5_file.create_dataset(
"model_conf/extra_decode_length", data=extra_decode_length, dtype="i4"
)
hdf5_file.close()
# read-in again to double check
hdf5_file = h5py.File(output_file, "r")
def _print_pair(key, value):
if key == "sampling_method":
value = "".join(map(chr, value[()]))
else:
value = value[()]
print(f"{key}: {value}")
list(map(lambda x: _print_pair(*x), hdf5_file["model_conf"].items()))
if __name__ == "__main__":
args = parse_args()
if args.generation_method not in ["topk", "topp", "ppl", "beam_search"]:
args.generation_method = "topk"
output_lightseq_model_name = "lightseq_gpt2_base" # or "lightseq_gpt2_large"
input_huggingface_gpt_model = "gpt2" # or "gpt2-large"
topk = 1
topp = 0.75
beam_size = 4
# default eos_id from https://huggingface.co/transformers/model_doc/gpt2.html#gpt2lmheadmodel
eos_id = 50256
pad_id = 50257
max_step = 50
extra_decode_length = 0 # use positive length to avtivate it
extract_gpt_weights(
output_lightseq_model_name,
input_huggingface_gpt_model,
generation_method=args.generation_method,
beam_size=beam_size,
topk=topk,
topp=topp,
eos_id=eos_id,
pad_id=pad_id,
max_step=max_step,
extra_decode_length=extra_decode_length,
)