-
Notifications
You must be signed in to change notification settings - Fork 4
/
asvd.py
181 lines (165 loc) · 4.99 KB
/
asvd.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
import argparse
import torch
import os
from transformers import AutoModelForCausalLM, AutoTokenizer, OPTForCausalLM
from transformers.models.opt.configuration_opt import OPTConfig
from evaluate_utils import evaluate_model
from datautils import get_calib_data
from act_aware_utils import calib_input_distribution, calib_fisher_info
from sensitivity import calib_sensitivity_ppl, calib_sensitivity_stable_rank
from quantization import rtn_quant_sequential
from binary_search import binary_search_truncation_rank
import numpy as np
def main(args):
# setting random seed of numpy and torch
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
torch.backends.cudnn.deterministic = True
# Load model
model_id = args.model_id
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
model_id, device_map="auto", torch_dtype=torch.float16, trust_remote_code=True
)
# if "llama" in model_id or "opt" in model_id:
# model = model.to_bettertransformer()
# sensitivity calibration
calib_loader = get_calib_data(args.calib_dataset, tokenizer, model_id, 256)
if "fisher" in args.scaling_method:
calib_fisher_info(model, calib_loader, args.use_cache)
if "abs" in args.scaling_method:
calib_input_distribution(
model, calib_loader, args.scaling_method, args.use_cache
)
if args.sensitivity_metric == "ppl":
sensitivity = calib_sensitivity_ppl(model, calib_loader, args, args.use_cache)
elif args.sensitivity_metric == "stable_rank":
sensitivity = calib_sensitivity_stable_rank(
model, calib_loader, args, args.use_cache
)
# search best truncation rank for each layer
binary_search_truncation_rank(model, sensitivity, calib_loader, args)
# quantization
if args.weight_quant != "none":
if args.weight_quant == "rtn_int8":
rtn_quant_sequential(model, 8)
elif args.weight_quant == "rtn_int6":
rtn_quant_sequential(model, 6)
# evaluate
result = evaluate_model(
model,
tokenizer,
args.model_id,
"mmlu" if args.eval_mmlu else "",
eval_ppl="wikitext2,ptb",
limit=-1,
)
print(result)
if not os.path.exists("output"):
os.makedirs("output")
with open("output/result.txt", "a+") as f:
f.write(f"{args}\n")
f.write(f"{result}\n")
# finished
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--model_id",
type=str,
default="facebook/opt-1.3b",
help="Pretrained model ID",
)
parser.add_argument(
"--ppl_target",
type=float,
default=-1,
help="target ppl",
)
parser.add_argument(
"--param_ratio_target",
type=float,
default=-1,
help="target param ratio",
)
parser.add_argument(
"--act_aware",
action="store_true",
help="use act aware svd (ASVD)",
)
parser.add_argument(
"--alpha",
type=float,
default=0.5,
help="hyper-parameter alpha for ASVD",
)
parser.add_argument(
"--n_calib_samples",
type=int,
default=32,
help="number of samples used for calibration",
)
parser.add_argument(
"--calib_dataset",
type=str,
default="wikitext2",
choices=["wikitext2", "c4", "ptb", "alpaca"],
help="calibration dataset",
)
parser.add_argument(
"--scaling_method",
type=str,
default="abs_mean",
choices=["abs_mean", "abs_max", "fisher", "fisher_abs_mean"],
help="scaling method",
)
parser.add_argument(
"--sensitivity_metric",
type=str,
default="ppl",
choices=["ppl", "stable_rank"],
help="search metric",
)
parser.add_argument(
"--use_cache",
action="store_true",
help="use cached calibration results",
)
parser.add_argument(
"--weight_quant",
type=str,
default="none",
choices=["none", "rtn_int8", "rtn_int6"],
help="weight quantization method",
)
parser.add_argument(
"--eval_mmlu",
action="store_true",
help="evaluate mmlu",
)
parser.add_argument(
"--sigma_fuse",
type=str,
default="UV",
help="sigma fuse method",
choices=["U", "V", "UV"],
)
parser.add_argument(
"--seed",
type=int,
default=233,
help="random seed, which can significantly affect the calibration results",
)
parser.add_argument(
"--compress_kv_cache",
action="store_true",
help="compress kv cache by asvd for k_proj and v_proj",
)
parser.add_argument(
"--kv_cache_ratio_target",
type=float,
default=-1,
help="kv cache ratio",
)
args = parser.parse_args()
main(args)