-
Notifications
You must be signed in to change notification settings - Fork 0
/
awareness.py
110 lines (90 loc) · 3.34 KB
/
awareness.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
import logging
from transformers.models.mixtral.modeling_mixtral import MixtralForCausalLM
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoConfig
from gptq import GPTQ
from expert_weight import layerwise_experts_weights_frequencies, layerwise_quant
from data.build import build_calib_loader
from quant.QLinear import *
logger = logging.getLogger(__name__)
def get_model():
import torch
def skip(*args, **kwargs):
pass
torch.nn.init.kaiming_uniform_ = skip
torch.nn.init.uniform_ = skip
torch.nn.init.normal_ = skip
model = AutoModelForCausalLM.from_pretrained(args.model, device_map='auto',torch_dtype=torch.float16, attn_implementation="flash_attention_2" if args.use_flash_attention_2 else None)
assert isinstance(
model, MixtralForCausalLM), 'Successfully loaded `Mixtral` model!'
model.seqlen = 2048
return model
if __name__ == "__main__":
import argparse
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 `Mixtral 8*7b`."
)
parser.add_argument(
"--calibration",
type=str,
choices=["math", "c4"],
help="Where to extract calibration data from.",
)
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(
"--batch_size",
type=int,
default=1,
help="batch size."
)
parser.add_argument(
"--attn_implementation",
type=str, required=False, default="eager",
choices=["eager", "sdpa", "flash_attention_2"],
help="attention implementation that the model works with",
)
parser.add_argument(
"--multigpu",
action="store_true",
)
parser.add_argument(
"--use_flash_attention_2", action="store_true", help="Whether to use flash_attention2 for inference."
)
parser.add_argument(
'--r', type=int, default=7, help='Number of experts to preserve'
)
args = parser.parse_args()
print(f'Arguments: {args}')
######## get the moe activated factors ########
model = get_model()
model.eval()
for param in model.parameters():
param.requires_grad = False
tokenizer = AutoTokenizer.from_pretrained(args.model)
batch_size = 8
num_workers = 4
calib_loader = build_calib_loader(args.calibration, tokenizer, model.seqlen,
args.nsamples, batch_size, num_workers, args.seed)
model = layerwise_experts_weights_frequencies(model, calib_loader, args)
del model
######## get the moe quant loss ########
model = get_model()
model.eval()
for param in model.parameters():
param.requires_grad = False
tokenizer = AutoTokenizer.from_pretrained(args.model)
batch_size = 8
num_workers = 4
calib_loader = build_calib_loader(args.calibration, tokenizer, model.seqlen,
args.nsamples, batch_size, num_workers, args.seed)
model = layerwise_quant(model, calib_loader, args)
del model