-
Notifications
You must be signed in to change notification settings - Fork 70
/
opt_smooth_exporter.py
205 lines (159 loc) · 8.09 KB
/
opt_smooth_exporter.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
"""Script to export INT8 OPT models from smootquant to the TinyLLMEngine format.
Prerequisites: smoothquant (reference: https://github.com/mit-han-lab/smoothquant)
Usage:
python opt_smooth_exporter.py --model-name <model_name> --output-path <output_path>
Example command:
INT8:
python opt_smooth_exporter.py --model-name mit-han-lab/opt-1.3B-smoothquant --output-path models/OPT_1.3B
Supported model_name:
- opt-125m-smoothquant
- opt-1.3B-smoothquant
- opt-6.7B-smoothquant
FP32:
python opt_smooth_exporter.py --model_name facebook/opt-125m --output_path FP32/models/OPT_125m --no-int8_smooth
"""
import argparse
import os
import struct
import torch
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
@torch.no_grad()
def _export_Qmodel(model, prefix):
outpath = prefix
os.makedirs(outpath, exist_ok=True)
with open(os.path.join(f"{outpath}", "lm_head.bin"), "wb") as f:
f.write(model.lm_head._parameters["weight"].cpu().float().numpy().tobytes())
_export_Qdecoder(model.model.decoder, os.path.join(f"{outpath}", "decoder"))
@torch.no_grad()
def _export_model(model, prefix):
outpath = prefix
os.makedirs(outpath, exist_ok=True)
with open(os.path.join(f"{outpath}", "lm_head.bin"), "wb") as f:
f.write(model.lm_head._parameters["weight"].cpu().float().numpy().tobytes())
_export_decoder(model.model.decoder, os.path.join(f"{outpath}", "decoder"))
def _export_Qdecoder(decoder, prefix):
outpath = prefix
os.makedirs(outpath, exist_ok=True)
_export_embed_tokens(decoder.embed_tokens, os.path.join(f"{outpath}", "embed_tokens"))
_export_LayerNorm(decoder.final_layer_norm, os.path.join(f"{outpath}", "final_layer_norm"))
_export_embed_tokens(decoder.embed_positions, os.path.join(f"{outpath}", "embed_positions"))
idx = 0
for layer in decoder.layers:
_export_Qdecoder_layer(layer, os.path.join(f"{outpath}", f"layer{idx}"))
idx += 1
def _export_decoder(decoder, prefix):
outpath = prefix
os.makedirs(outpath, exist_ok=True)
_export_embed_tokens(decoder.embed_tokens, os.path.join(f"{outpath}", "embed_tokens"))
_export_LayerNorm(decoder.final_layer_norm, os.path.join(f"{outpath}", "final_layer_norm"))
_export_embed_tokens(decoder.embed_positions, os.path.join(f"{outpath}", "embed_positions"))
idx = 0
for layer in decoder.layers:
_export_decoder_layer(layer, os.path.join(f"{outpath}", f"layer{idx}"))
idx += 1
def _export_Qdecoder_layer(layer, prefix: str):
outpath = prefix
_export_Qattention_params(layer.self_attn, os.path.join(outpath, "self_attn"))
_export_LayerNormQ(layer.self_attn_layer_norm, os.path.join(outpath, "self_attn_layer_norm"))
_export_W8A8B8O8Linear(layer.fc1, os.path.join(outpath, "fc1"))
_export_W8A8BFP32OFP32Linear(layer.fc2, os.path.join(outpath, "fc2"))
_export_LayerNormQ(layer.final_layer_norm, os.path.join(outpath, "final_layer_norm"))
def _export_decoder_layer(layer, prefix: str):
outpath = prefix
_export_attention_params(layer.self_attn, os.path.join(outpath, "self_attn"))
_export_LayerNorm(layer.self_attn_layer_norm, os.path.join(outpath, "self_attn_layer_norm"))
_export_LinearFP(layer.fc1, os.path.join(outpath, "fc1"))
_export_LinearFP(layer.fc2, os.path.join(outpath, "fc2"))
_export_LayerNorm(layer.final_layer_norm, os.path.join(outpath, "final_layer_norm"))
def _export_Qattention_params(attn, prefix: str):
outpath = prefix
_export_BMM_S8T_S8N_F32T(attn.qk_bmm, os.path.join(outpath, "qk_bmm"))
_export_BMM_S8T_S8N_S8T(attn.pv_bmm, os.path.join(outpath, "pv_bmm"))
_export_W8A8B8O8Linear(attn.k_proj, os.path.join(outpath, "k_proj"))
_export_W8A8B8O8Linear(attn.v_proj, os.path.join(outpath, "v_proj"))
_export_W8A8B8O8Linear(attn.q_proj, os.path.join(outpath, "q_proj"))
_export_W8A8BFP32OFP32Linear(attn.out_proj, os.path.join(outpath, "out_proj"))
def _export_attention_params(attn, prefix: str):
outpath = prefix
os.makedirs(outpath, exist_ok=True)
with open(os.path.join(outpath, "scaling.bin"), "wb") as f:
f.write(bytearray(struct.pack("f", attn.scaling)))
_export_LinearFP(attn.k_proj, os.path.join(outpath, "k_proj"))
_export_LinearFP(attn.v_proj, os.path.join(outpath, "v_proj"))
_export_LinearFP(attn.q_proj, os.path.join(outpath, "q_proj"))
_export_LinearFP(attn.out_proj, os.path.join(outpath, "out_proj"))
def _export_embed_tokens(embed_tokens, prefix):
outpath = prefix
os.makedirs(outpath, exist_ok=True)
with open(os.path.join(f"{outpath}", "weight.bin"), "wb") as f:
f.write(embed_tokens.weight.cpu().float().numpy().tobytes())
def _export_BMM_S8T_S8N_F32T(op, prefix):
outpath = prefix
os.makedirs(outpath, exist_ok=True)
with open(os.path.join(f"{outpath}", "alpha.bin"), "wb") as f:
f.write(op.a.cpu().float().numpy().tobytes())
def _export_BMM_S8T_S8N_S8T(op, prefix):
outpath = prefix
os.makedirs(outpath, exist_ok=True)
with open(os.path.join(f"{outpath}", "alpha.bin"), "wb") as f:
f.write(op.a.cpu().float().numpy().tobytes())
def _export_W8A8B8O8Linear(op, prefix):
outpath = prefix
os.makedirs(outpath, exist_ok=True)
with open(os.path.join(f"{outpath}", "weight.bin"), "wb") as f:
f.write(op.weight.cpu().numpy().tobytes())
with open(os.path.join(f"{outpath}", "bias.bin"), "wb") as f:
f.write((op.bias.cpu().float() * (op.b.item() / op.a.item())).round().int().numpy().tobytes())
with open(os.path.join(f"{outpath}", "bias_int8.bin"), "wb") as f:
f.write((op.bias.cpu().numpy().tobytes()))
with open(os.path.join(f"{outpath}", "alpha.bin"), "wb") as f:
f.write(op.a.cpu().float().numpy().tobytes())
with open(os.path.join(f"{outpath}", "beta.bin"), "wb") as f:
f.write(op.b.cpu().float().numpy().tobytes())
def _export_W8A8BFP32OFP32Linear(op, prefix):
outpath = prefix
os.makedirs(outpath, exist_ok=True)
with open(os.path.join(f"{outpath}", "weight.bin"), "wb") as f:
f.write(op.weight.cpu().numpy().tobytes())
with open(os.path.join(f"{outpath}", "bias.bin"), "wb") as f:
f.write(op.bias.cpu().numpy().tobytes())
with open(os.path.join(f"{outpath}", "alpha.bin"), "wb") as f:
f.write(op.a.cpu().float().numpy().tobytes())
def _export_LayerNorm(op, prefix):
outpath = prefix
os.makedirs(outpath, exist_ok=True)
with open(os.path.join(f"{outpath}", "weight.bin"), "wb") as f:
f.write(op.weight.cpu().float().numpy().tobytes())
with open(os.path.join(f"{outpath}", "bias.bin"), "wb") as f:
f.write(op.bias.cpu().float().numpy().tobytes())
def _export_LayerNormQ(op, prefix):
outpath = prefix
os.makedirs(outpath, exist_ok=True)
with open(os.path.join(f"{outpath}", "weight.bin"), "wb") as f:
f.write(op.weight.cpu().float().numpy().tobytes())
with open(os.path.join(f"{outpath}", "bias.bin"), "wb") as f:
f.write(op.bias.cpu().float().numpy().tobytes())
def _export_LinearFP(op, prefix):
outpath = prefix
os.makedirs(outpath, exist_ok=True)
with open(os.path.join(f"{outpath}", "weight.bin"), "wb") as f:
f.write(op._parameters["weight"].cpu().float().numpy().tobytes())
with open(os.path.join(f"{outpath}", "bias.bin"), "wb") as f:
f.write(op._parameters["bias"].cpu().float().numpy().tobytes())
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--model_name", type=str, default="mit-han-lab/opt-1.3B-smoothquant")
parser.add_argument("--output_path", type=str)
parser.add_argument("--no-int8_smooth", dest="int8_smooth", action="store_false")
parser.set_defaults(int8_smooth=True)
args = parser.parse_args()
if args.int8_smooth:
from smoothquant.opt import Int8OPTForCausalLM
model_smoothquant = Int8OPTForCausalLM.from_pretrained(
args.model_name, torch_dtype=torch.float32, device_map="auto"
)
_export_Qmodel(model_smoothquant, args.output_path)
else:
from transformers import OPTForCausalLM
model = OPTForCausalLM.from_pretrained(args.model_name, torch_dtype=torch.float32)
_export_model(model, args.output_path)