-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathgenerate_act_scales.py
65 lines (52 loc) · 1.94 KB
/
generate_act_scales.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
import torch
import os
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
)
import argparse
from smoothquant.calibration import get_act_scales
def build_model_and_tokenizer(model_name):
tokenizer = AutoTokenizer.from_pretrained(model_name, model_max_length=512)
kwargs = {"torch_dtype": torch.float16, "device_map": "sequential"}
model = AutoModelForCausalLM.from_pretrained(model_name, **kwargs)
return model, tokenizer
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--model-name", type=str, default="facebook/opt-1.3b", help="model name"
)
parser.add_argument(
"--output-path",
type=str,
default="act_scales/opt-1.3b.pt",
help="where to save the act scales",
)
parser.add_argument(
"--dataset-path",
type=str,
default="dataset/val.jsonl.zst",
help="location of the calibration dataset, we use the validation set of the Pile dataset",
)
parser.add_argument("--num-samples", type=int, default=512)
parser.add_argument("--seq-len", type=int, default=512)
args = parser.parse_args()
return args
@torch.no_grad()
def main():
args = parse_args()
model, tokenizer = build_model_and_tokenizer(args.model_name)
if not os.path.exists(args.dataset_path):
print(f"Cannot find the dataset at {args.dataset_path}")
print("Please download the Pile dataset and put the validation set at the path")
print(
"You can download the validation dataset of the Pile at https://huggingface.co/datasets/mit-han-lab/pile-val-backup/resolve/main/val.jsonl.zst"
)
raise FileNotFoundError
act_scales = get_act_scales(
model, tokenizer, args.dataset_path, args.num_samples, args.seq_len
)
os.makedirs(os.path.dirname(args.output_path), exist_ok=True)
torch.save(act_scales, args.output_path)
if __name__ == "__main__":
main()