-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquantize.py
69 lines (54 loc) · 1.61 KB
/
quantize.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
from transformers import AutoTokenizer
from auto_gptq import AutoGPTQForCausalLM, BaseQuantizeConfig
import torch
import time
import sys
import random
from datasets import load_dataset
if torch.cuda.is_available():
print("CUDA available, continuing...")
else:
print("This script is only meant to be used with CUDA, enable CUDA and re-run it")
sys.exit(0)
base_model_path = "path/to/base"
quant_mode_path = "path/to/quant"
seed = 0
random.seed(seed)
quantize_dataset = []
n_samples = 128
seqlen = 2048
chunk_size = 100
tokenizer = AutoTokenizer.from_pretrained(
base_model_path,
trust_remote_code=True,
device_map = "cuda"
)
quantize_config = BaseQuantizeConfig(
bits=4,
group_size=128
)
model = AutoGPTQForCausalLM.from_pretrained(
base_model_path,
quantize_config,
trust_remote_code=True,
)
paths = []
data = load_dataset("json", data_files=paths, split="train")
for _ in range(n_samples):
i = random.randint(0, data.num_rows - chunk_size - 1)
chunk = "".join(data["text"][i:i+chunk_size])
token_data = tokenizer(chunk, return_tensors="pt")
inp = token_data.input_ids[:, :seqlen]
attention_mask = torch.ones_like(inp)
quantize_dataset.append({"input_ids": inp, "attention_mask": attention_mask})
print("Starting quantization...")
model.to("cuda:0")
start = time.perf_counter()
model.quantize(quantize_dataset, batch_size=10)
end = time.perf_counter()
print(f"Quantization time (s): {end - start}")
print("Saving model...")
model.save_quantized(
quant_model_path,
use_safetensors=True
)