-
Notifications
You must be signed in to change notification settings - Fork 13
/
run.py
301 lines (258 loc) · 9.22 KB
/
run.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
import time
import torch
import torch.nn as nn
from bigptq import BRAGPTQ
from binary import Binarization
from modelutils import find_layers
def get_model(model):
import torch
def skip(*args, **kwargs):
pass
torch.nn.init.kaiming_uniform_ = skip
torch.nn.init.uniform_ = skip
torch.nn.init.normal_ = skip
if "opt" in model:
from transformers import OPTForCausalLM
model = OPTForCausalLM.from_pretrained(model, torch_dtype="auto")
model.seqlen = model.config.max_position_embeddings
elif "llama" in model:
from transformers import LlamaForCausalLM
model = LlamaForCausalLM.from_pretrained(model, torch_dtype="auto")
model.seqlen = 2048
return model
'''
The function is employed to calibrate and quantize models layer by layer.
'''
@torch.no_grad()
def quant_sequential(model, dataloader, dev):
print("Starting ...")
for name, module in model.named_modules():
module.global_name = args.model + name
use_cache = model.config.use_cache
model.config.use_cache = False
if "opt" in args.model:
layers = model.model.decoder.layers
model.model.decoder.embed_tokens = model.model.decoder.embed_tokens.to(dev)
model.model.decoder.embed_positions = model.model.decoder.embed_positions.to(
dev
)
if (
hasattr(model.model.decoder, "project_out")
and model.model.decoder.project_out
):
model.model.decoder.project_out = model.model.decoder.project_out.to(dev)
if (
hasattr(model.model.decoder, "project_in")
and model.model.decoder.project_in
):
model.model.decoder.project_in = model.model.decoder.project_in.to(dev)
elif "llama" in args.model:
layers = model.model.layers
model.model.embed_tokens = model.model.embed_tokens.to(dev)
model.model.norm = model.model.norm.to(dev)
layers[0] = layers[0].to(dev)
dtype = next(iter(model.parameters())).dtype
inps = torch.zeros(
(args.nsamples, model.seqlen, model.config.hidden_size), dtype=dtype, device=dev
)
cache = {"i": 0, "attention_mask": None}
class Catcher(nn.Module):
def __init__(self, module):
super().__init__()
self.module = module
def forward(self, inp, **kwargs):
inps[cache["i"]] = inp
cache["i"] += 1
cache["attention_mask"] = kwargs["attention_mask"]
raise ValueError
layers[0] = Catcher(layers[0])
for batch in dataloader:
try:
model(batch[0].to(dev))
except ValueError:
pass
layers[0] = layers[0].module
layers[0] = layers[0].cpu()
if "opt" in args.model:
model.model.decoder.embed_tokens = model.model.decoder.embed_tokens.cpu()
model.model.decoder.embed_positions = model.model.decoder.embed_positions.cpu()
if (
hasattr(model.model.decoder, "project_out")
and model.model.decoder.project_out
):
model.model.decoder.project_out = model.model.decoder.project_out.cpu()
if (
hasattr(model.model.decoder, "project_in")
and model.model.decoder.project_in
):
model.model.decoder.project_in = model.model.decoder.project_in.cpu()
elif "llama" in args.model:
model.model.embed_tokens = model.model.embed_tokens.cpu()
model.model.norm = model.model.norm.cpu()
torch.cuda.empty_cache()
outs = torch.zeros_like(inps)
attention_mask = cache["attention_mask"]
print("Ready.")
for i in range(len(layers)):
layer = layers[i].to(dev)
subset = find_layers(layer)
gptq = {}
for name in subset:
if (
not (args.minlayer <= i < args.maxlayer and args.quant_only in name)
) == (not args.invert):
continue
braq_quantizer = Binarization(
subset[name].weight,
method=args.low_quant_method,
groupsize=groupsize,
)
gptq[name] = BRAGPTQ(
subset[name],
braq_quantizer,
salient_metric=args.salient_metric,
disable_gptq=args.disable_gptq,
)
def add_batch(name):
def tmp(_, inp, out):
gptq[name].add_batch(inp[0].data, out.data)
return tmp
handles = []
for name in gptq:
handles.append(subset[name].register_forward_hook(add_batch(name)))
for j in range(args.nsamples):
outs[j] = layer(inps[j].unsqueeze(0), attention_mask=attention_mask)[0]
for h in handles:
h.remove()
for name in gptq:
print(i, name)
print("Quantizing ...")
info = gptq[name].fasterquant(
percdamp=args.percdamp,
blocksize=args.blocksize,
)
gptq[name].free()
for j in range(args.nsamples):
outs[j] = layer(inps[j].unsqueeze(0), attention_mask=attention_mask)[0]
layers[i] = layer.cpu()
del layer
del gptq
torch.cuda.empty_cache()
inps, outs = outs, inps
model.config.use_cache = use_cache
if __name__ == "__main__":
import argparse
from datautils import *
def list_of_ints(arg):
return list(map(int, arg.split(',')))
def list_of_floats(arg):
return list(map(float, arg.split(',')))
parser = argparse.ArgumentParser()
parser.add_argument(
"model", type=str, help="model to load; for example `huggyllama/llama-7b`."
)
parser.add_argument(
"dataset",
type=str,
choices=["wikitext2", "ptb", "c4"],
help="Where to extract calibration data from.",
)
parser.add_argument(
"low_quant_method",
type=str,
choices=["xnor", "sign", "no", "2bit", "4bit", "prune", "braq"],
help="quantization method; `xnor` is the method using XNOR to adapt hardware calculation; `prune` is the method used in sparseGPTQ; braq is the method used in BiLLM",
)
parser.add_argument("--load_quantized", action="store_true")
parser.add_argument(
"--seed", type=int, default=0, help="Seed for sampling the calibration data."
)
parser.add_argument(
"--nsamples", type=int, default=128, help="Number of calibration data samples."
)
parser.add_argument(
"--percdamp",
type=float,
default=0.01,
help="Percent of the average Hessian diagonal to use for dampening.",
)
parser.add_argument(
"--blocksize",
type=int,
default=128,
help="Blocksize to use for adaptive mask selection.",
)
parser.add_argument(
"--salient_metric",
type=str,
default="magnitude",
choices=["magnitude", "hessian"],
)
parser.add_argument(
"--device",
type=str,
default="cuda:0",
help="set the device to use for quantization.",
)
parser.add_argument(
"--disable_gptq",
action="store_true",
help="disable GPTQ for quantization.",
)
parser.add_argument(
"--minlayer", type=int, default=-1, help="Quant all layers with id >= this."
)
parser.add_argument(
"--maxlayer", type=int, default=1000, help="Quant all layers with id < this."
)
parser.add_argument(
"--quant_only",
type=str,
default="",
help="Quant only layers that contain this text.",
)
parser.add_argument("--invert", action="store_true", help="Invert subset.")
parser.add_argument(
"--save",
action="store_true",
)
parser.add_argument(
"--log_wandb", action="store_true", help="Whether to log to wandb."
)
args = parser.parse_args()
groupsize = args.blocksize
device = args.device
save_title = f"{args.model}_{args.dataset}_{args.low_quant_method}_{groupsize}_{args.salient_metric}"
save_file = "./output/" + save_title.replace("/", "_") + ".pt"
if args.load_quantized:
model = get_model(save_file)
model.eval()
else: # braq
model = get_model(args.model)
model.eval()
tick = time.time()
dataloader, testloader = get_loaders(
args.dataset,
nsamples=args.nsamples,
seed=args.seed,
model=args.model,
seqlen=model.seqlen,
)
quant_sequential(model, dataloader, device)
print("quantization time:", time.time() - tick, "s")
if args.save:
save_path = os.path.dirname(save_file)
if not os.path.exists(save_path):
os.makedirs(save_path)
model.save_pretrained(save_file)
for dataset in ["wikitext2", "ptb", "c4"]:
dataloader, testloader = get_loaders(
dataset, seed=args.seed, seqlen=model.seqlen, model=args.model
)
print(dataset)
if "opt" in args.model:
from eval_ppl_utils import opt_eval
opt_eval(model, testloader, device, dataset, args.log_wandb)
elif "llama" in args.model:
from eval_ppl_utils import llama_eval
llama_eval(model, testloader, device, dataset, args.log_wandb)