-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
316 lines (273 loc) · 15.2 KB
/
main.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
import argparse
import os
import numpy as np
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
from importlib.metadata import version
from lib.prune import prune_wanda, prune_magnitude, prune_sparsegpt, prune_ablate, check_sparsity, find_layers, prune_admm
from lib.eval import eval_ppl, eval_zero_shot
print('torch', version('torch'))
print('transformers', version('transformers'))
print('accelerate', version('accelerate'))
print('# of gpus: ', torch.cuda.device_count())
def get_llm(model_name, cache_dir="llm_weights"):
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float16,
cache_dir=cache_dir,
low_cpu_mem_usage=True,
device_map="auto"
)
model.seqlen = model.config.max_position_embeddings
return model
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str, help='LLaMA model')
parser.add_argument('--seed', type=int, default=0, help='Seed for sampling the calibration data.')
parser.add_argument('--sparsity_ratio', type=float, default=0, help='Sparsity level')
parser.add_argument("--sparsity_type", type=str, choices=["unstructured", "4:8", "2:4", "1:4"])
parser.add_argument("--prune_method", type=str, choices=["magnitude", "wanda", "sparsegpt",
"ablate_mag_seq", "ablate_wanda_seq", "ablate_mag_iter", "ablate_wanda_iter", "search", "prune_admm"])
parser.add_argument("--cache_dir", default="llm_weights", type=str )
parser.add_argument('--use_variant', action="store_true", help="whether to use the wanda variant described in the appendix")
parser.add_argument('--save', type=str, default=None, help='Path to save results.')
parser.add_argument('--save_model', type=str, default=None, help='Path to save the pruned model.')
parser.add_argument("--eval_zero_shot", action="store_true")
parser.add_argument('--mlp_sparsity', type=float, default=0, help='Sparsity level')
parser.add_argument('--atten_sparsity', type=float, default=0, help='Sparsity level')
parser.add_argument('--split', action=argparse.BooleanOptionalAction, default=False,
help='''split the sparsity''')
parser.add_argument('--owl', action=argparse.BooleanOptionalAction, default=False,
help='''split the sparsity''')
parser.add_argument('--temperature', type=float, default=0, help='')
parser.add_argument(
'--Hyper_m',
type=float,
default=3, )
parser.add_argument(
"--Lamda",
default=0.08,
type=float,
help="Lamda",
)
##QuaRot args
supported_datasets = ['wikitext2', 'ptb', 'c4']
##
parser.add_argument('--eval_dataset', type=str, default='wikitext2',
help='Dataset for Evaluation (default: wikitext2)', choices=supported_datasets,)
parser.add_argument('--hf_token', type=str, default=None)
parser.add_argument('--bsz', type=int, default=32,
help='Batch-size for PPL evaluation (default:32)')
parser.add_argument('--prune', action=argparse.BooleanOptionalAction, default=False)
# Rotation Arguments
parser.add_argument('--rotate', action=argparse.BooleanOptionalAction, default=False,
help='''Rotate the moodel. This will include online rotation for down-projection and
out-projection. Note that this does not apply rotation to the K/Q and they will be rotated
if we want to quantize the Keys''')
parser.add_argument('--rotate_mode', type=str, default='hadamard', choices=['hadamard', 'random'])
parser.add_argument('--rotation_seed', type=int, default=-1,
help='Random Seed for generating random matrix!!')
parser.add_argument('--fp32_had', action=argparse.BooleanOptionalAction, default=False,
help='Apply Hadamard rotation in FP32 (default: False)')
# Activation Quantization Arguments
parser.add_argument('--a_bits', type=int, default=16,
help='''Number of bits for inputs of the Linear layers. This will be
for all the linear layers in the model (including down-projection and out-projection)''')
parser.add_argument('--a_groupsize', type=int, default=-1,
help='Groupsize for activation quantization. Note that this should be the same as w_groupsize')
parser.add_argument('--a_asym', action=argparse.BooleanOptionalAction, default=False,
help='ASymmetric Activation quantization (default: False)')
parser.add_argument('--a_clip_ratio', type=float, default=1.0,
help='Clip ratio for activation quantization. new_max = max * clip_ratio')
# Weight Quantization Arguments
parser.add_argument('--w_bits', type=int, default=16,
help='Number of bits for weights of the Linear layers')
parser.add_argument('--w_groupsize', type=int, default=-1,
help='Groupsize for weight quantization. Note that this should be the same as a_groupsize')
parser.add_argument('--w_asym', action=argparse.BooleanOptionalAction, default=False,
help='ASymmetric weight quantization (default: False)')
parser.add_argument('--w_rtn', action=argparse.BooleanOptionalAction, default=False,
help='Quantize the weights using RtN. If the w_bits < 16 and this flag is not set, we use GPTQ')
parser.add_argument('--w_clip', action=argparse.BooleanOptionalAction, default=False,
help='''Clipping the weight quantization!
We do not support arguments for clipping and we find the best clip ratio during the weight quantization''')
parser.add_argument('--nsamples', type=int, default=128,
help='Number of calibration data samples for GPTQ.')
parser.add_argument('--cal_dataset', type=str, default='wikitext2',
help='calibration data samples for GPTQ.', choices=supported_datasets)
parser.add_argument('--percdamp', type=float, default=.01,
help='Percent of the average Hessian diagonal to use for dampening.')
parser.add_argument('--act_order', action=argparse.BooleanOptionalAction, default=False,
help='act-order in GPTQ')
# General Quantization Arguments
parser.add_argument('--int8_down_proj', action=argparse.BooleanOptionalAction, default=False,
help='Use INT8 for Down Projection! If this set, both weights and activations of this layer will be in INT8')
# KV-Cache Quantization Arguments
parser.add_argument('--v_bits', type=int, default=16,
help='''Number of bits for V-cache quantization.
Note that quantizing the V-cache does not need any other rotation''')
parser.add_argument('--v_groupsize', type=int, default=-1)
parser.add_argument('--v_asym', action=argparse.BooleanOptionalAction, default=False,
help='ASymmetric V-cache quantization')
parser.add_argument('--v_clip_ratio', type=float, default=1.0,
help='Clip ratio for v-cache quantization. new_max = max * clip_ratio')
parser.add_argument('--k_bits', type=int, default=16,
help='''Number of bits for K-cache quantization.
Note that quantizing the K-cache needs another rotation for the keys/queries''')
parser.add_argument('--k_groupsize', type=int, default=-1)
parser.add_argument('--k_asym', action=argparse.BooleanOptionalAction, default=False,
help='ASymmetric K-cache quantization')
parser.add_argument('--k_pre_rope', action=argparse.BooleanOptionalAction, default=False,
help='Pre-RoPE quantization for K-cache (not Supported yet!)')
parser.add_argument('--k_clip_ratio', type=float, default=1.0,
help='Clip ratio for k-cache quantization. new_max = max * clip_ratio')
# Save/Load Quantized Model Arguments
parser.add_argument('--load_qmodel_path', type=str, default=None,
help='Load the quantized model from the specified path!')
parser.add_argument('--save_qmodel_path', type=str, default=None,
help='Save the quantized model to the specified path!')
# WandB Arguments
parser.add_argument('--wandb', action=argparse.BooleanOptionalAction, default=False)
parser.add_argument('--wandb_id', type=str, default=None)
parser.add_argument('--wandb_project', type=str, default=None)
#Experiments Arguments
parser.add_argument('--save_name', type=str, default=None, help='The path to save experiment data, '
'including quantized models, dumped layer inputs, etc. The data will be saved in experiments/[model]/save_name. Default: [datetime].')
parser.add_argument('--capture_layer_io', action=argparse.BooleanOptionalAction, default=False,
help='Capture the input and output of the specified decoder layer and dump into a file')
parser.add_argument('--layer_idx', type=int, default=10, help='Which decoder layer to capture')
# LM Eval Arguments
parser.add_argument("--lm_eval", action="store_true", help="Evaluate the model on LM Eval tasks.")
parser.add_argument(
'--tasks',
nargs='+',
default=["piqa", "hellaswag", "arc_easy", "arc_challenge", "winogrande", "lambada"],
)
parser.add_argument('--lm_eval_batch_size', type=int, default=128, help='Batch size for evaluating with lm eval harness.')
parser.add_argument(
"--distribute",
action="store_true",
help="Distribute the model on multiple GPUs for evaluation.",
)
parser.add_argument(
'--act_sort_metric', type=str, default='hessian', choices=['abs_mean', 'hessian'],
help='The metric used to sort the activations.'
)
parser.add_argument(
'--keeper', type=int, default=0,
help='Group size to keep outliers.'
)
parser.add_argument(
'--keeper_precision', type=int, default=0, choices=[0, 1, 2, 3],
help='Precision to keep outliers. 0 for FP16; 1 for E5M2; 2 for E4M3; 3 for INT8 Quant.'
)
parser.add_argument(
'--reorder', action='store_true',
help='Whether to keep salient weight unquantized.'
)
parser.add_argument(
'--static', action='store_true',
help='Whether to perform static quantization (For activtions). Default is dynamic. (Deprecated in Atom)'
)
parser.add_argument(
'--exponential', action='store_true',
help='Whether to use exponent-only for weight quantization.'
)
parser.add_argument(
'--a_sym', action='store_true',
help='Whether to perform symmetric quantization. Default is asymmetric.'
)
parser.add_argument(
'--w_sym', action='store_true',
help='Whether to perform symmetric quantization. Default is asymmetric.'
)
parser.add_argument(
'--act_group_size', type=int, default=0, choices=[0, 64, 128, 256, 384, 768],
help='Group size when quantizing activations. Using 128 as default quantization group.'
)
parser.add_argument(
'--abits', type=int, default=16, choices=[2, 3, 4, 8, 16],
help='#bits to use for quantizing activation; use 16 for evaluating base model.'
)
parser.add_argument(
'--kv_cache', action='store_true',
help='Whether to quant KV_Cache'
)
parser.add_argument(
'--tiling', type=int, default=0, choices=[0, 16],
help='Tile-wise quantization granularity (Deprecated in Atom).'
)
parser.add_argument(
'--w_clip_ratio', type=float, default=1.0,
help='Clip ratio for weight quantization. new_max = max * clip_ratio'
)
parser.add_argument(
'--quant_type', type=str, default='int', choices=['int', 'fp'],
help='Determine the mapped data format by quant_type + n_bits. e.g. int8, fp4.'
)
parser.add_argument(
'--kv_clip_ratio', type=float, default=1.0,
help='Clip ratio for kv cache quantization. new_max = max * clip_ratio'
)
args = parser.parse_args()
# Setting seeds for reproducibility
np.random.seed(args.seed)
torch.random.manual_seed(args.seed)
# Handling n:m sparsity
prune_n, prune_m = 0, 0
if args.sparsity_type != "unstructured":
if args.sparsity_type == "1:4":
assert args.sparsity_ratio == 0.25
else:
assert args.sparsity_ratio == 0.5, "sparsity ratio must be 0.5 for structured N:M sparsity"
prune_n, prune_m = map(int, args.sparsity_type.split(":"))
model_name = args.model.split("/")[-1]
print(f"loading llm model {args.model}")
model = get_llm(args.model, args.cache_dir)
model.eval()
tokenizer = AutoTokenizer.from_pretrained(args.model, use_fast=False)
device = torch.device("cuda:0")
if "30b" in args.model or "65b" in args.model: # for 30b and 65b we use device_map to load onto multiple A6000 GPUs, thus the processing here.
device = model.hf_device_map["lm_head"]
print("use device ", device)
if args.sparsity_ratio != 0:
print("pruning starts")
if args.prune_method == "wanda":
prune_wanda(args, model, tokenizer, device, prune_n=prune_n, prune_m=prune_m)
elif args.prune_method == "magnitude":
prune_magnitude(args, model, tokenizer, device, prune_n=prune_n, prune_m=prune_m)
elif args.prune_method == "sparsegpt":
prune_sparsegpt(args, model, tokenizer, device, prune_n=prune_n, prune_m=prune_m)
elif "ablate" in args.prune_method:
prune_ablate(args, model, tokenizer, device, prune_n=prune_n, prune_m=prune_m)
elif "admm" in args.prune_method:
prune_admm(args, model, tokenizer, device, prune_n=prune_n, prune_m=prune_m)
################################################################
print("*"*30)
sparsity_ratio = check_sparsity(model)
print(f"sparsity sanity check {sparsity_ratio:.4f}")
print("*"*30)
################################################################
ppl_test = eval_ppl(args, model, tokenizer, device)
print(f"wikitext perplexity {ppl_test}")
if not os.path.exists(args.save):
os.makedirs(args.save)
save_filepath = os.path.join(args.save, f"log_{args.prune_method}.txt")
with open(save_filepath, "w") as f:
print("method\tactual_sparsity\tppl_test", file=f, flush=True)
print(f"{args.prune_method}\t{sparsity_ratio:.4f}\t{ppl_test:.4f}", file=f, flush=True)
if args.eval_zero_shot:
accelerate=False
if "30b" in args.model or "65b" in args.model or "70b" in args.model:
accelerate=True
task_list = ["boolq", "rte","hellaswag","winogrande", "arc_easy","arc_challenge", "openbookqa"]
num_shot = 0
results = eval_zero_shot(args.model, model, tokenizer, task_list, num_shot, accelerate)
print("********************************")
print("zero_shot evaluation results")
print(results)
if args.save_model:
model.save_pretrained(args.save_model)
tokenizer.save_pretrained(args.save_model)
if __name__ == '__main__':
main()