-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquantize_by_autogptq.py
97 lines (70 loc) · 2.98 KB
/
quantize_by_autogptq.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
from transformers import AutoTokenizer
from auto_gptq import AutoGPTQForCausalLM, BaseQuantizeConfig
# from loguru import logger
import json
import tqdm
import random
import logging
logging.basicConfig(
format="%(asctime)s %(levelname)s [%(name)s] %(message)s",
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S",
) # auto-gptq logger
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"--pretrained_model_dir", type=str, default="/mnt/data/models/Llama-2-7b-hf/"
)
parser.add_argument("--quantized_model_dir", type=str, default="./quantized")
parser.add_argument("--data", type=str, default="../data/processed/train_v1.json")
parser.add_argument("--bits", type=int, default=4)
parser.add_argument("--group_size", type=int, default=128)
parser.add_argument("--trust_remote_code", action="store_true")
parser.add_argument("--cache_examples_on_gpu", action="store_true")
parser.add_argument("--desc_act", action="store_true")
parser.add_argument("--seed", type=int, default=42)
parser.add_argument("--num_samples", type=int, default=4096)
args = parser.parse_args()
print(args)
random.seed(args.seed)
# logger.info("loading tokenizer")
tokenizer = AutoTokenizer.from_pretrained(
args.pretrained_model_dir, use_fast=True, trust_remote_code=args.trust_remote_code
)
data = args.data
with open(data, "r") as f:
data = json.load(f)
examples = [d["text"] for d in data]
logging.info(f"loaded {len(examples)} examples")
examples = random.sample(examples, args.num_samples)
tokenized_examples = []
for e in tqdm.tqdm(examples):
tokenized_examples += [tokenizer(e, return_tensors="pt")]
# logger.info(f"loaded {len(tokenized_examples)} examples")
quantize_config = BaseQuantizeConfig(
bits=args.bits, # quantize model to 4-bit
group_size=args.group_size, # it is recommended to set the value to 128
desc_act=args.desc_act, # set to False can significantly speed up inference but the perplexity may slightly bad
)
# logger.info(f"quantize config: {quantize_config}")
# load un-quantized model, by default, the model will always be loaded into CPU memory
model = AutoGPTQForCausalLM.from_pretrained(
args.pretrained_model_dir,
quantize_config,
device_map={"": "cuda:0"},
trust_remote_code=args.trust_remote_code,
)
# print model parameters
for name, param in model.named_parameters():
print(name, param.shape, param.device)
# quantize model, the examples should be list of dict whose keys can only be "input_ids" and "attention_mask"
# use mini-batch to quantize model
model.quantize(tokenized_examples, batch_size=1, cache_examples_on_gpu=args.cache_examples_on_gpu)
# # save quantized model
# model.save_quantized(args.quantized_model_dir)
# save quantized model using safetensors
# model.save_quantized(args.quantized_model_dir, use_safetensors=True)
# save the tokenizer and model using save_pretrained
model.save_pretrained(args.quantized_model_dir)
tokenizer.save_pretrained(args.quantized_model_dir)
print("saved model to", args.quantized_model_dir)