-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquantization_az.py
206 lines (161 loc) · 7.09 KB
/
quantization_az.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import torch
import torch.nn as nn
from torchvision import datasets
from torch.utils.data import DataLoader, Dataset
from vector_quantize_pytorch import ResidualVQ
import os
import numpy as np
from transformers import set_seed, HfArgumentParser
from utils import json_load, weight_init
from tqdm import trange
from dataclasses import dataclass, field, asdict
from typing import Optional, List, Dict, Tuple
@dataclass
class TrainingArgs:
lr: float = 1e-3
wd: float = 1e-3
epochs: int = 1000
batch_size: int = 512
seed: int = 42
# item: bs 64, alpha 0.25, codebook_size 256, lr 1e-3, wd 1e-4
# user: bs 512, alpha 1.0, codebook_size 300, lr 1e-3, wd 1e-3
@dataclass
class QuantArgs:
dim: int = 32
num_quantizers: int = 3
codebook_size: int = 7000
kmeans_init: bool = True
kmeans_iters: int = 500
sample_codes: bool = False
alpha: float = 1.0
embed_dir: str = "outputs_llm"
output_dir: str = "outputs_quant"
embed_type: str = "item"
dataset: str = "az-books"
dim_dnn: Tuple = tuple([512, 256])
def __post_init__(self):
self.embeddings = np.load(f"outputs_llm/amz-books/{self.embed_type}.npy")
print("LLM embeddings: ", self.embeddings.shape)
embed_dim = self.embeddings.shape[1]
self.dim_dnn = tuple([embed_dim, *self.dim_dnn, self.dim])
class EmbeddingDataset(Dataset):
def __init__(self, embeddings):
super().__init__()
self.embeddings = embeddings
def __len__(self):
return len(self.embeddings)
def __getitem__(self, index):
return torch.tensor(self.embeddings[index]).to(torch.float32)
class RQAutoEncoder(nn.Module):
def __init__(self, config: QuantArgs):
super().__init__()
self.encoder = nn.Sequential()
self.decoder = nn.Sequential()
for in_dim, out_dim in zip(config.dim_dnn[:-1], config.dim_dnn[1:]):
self.encoder.append(nn.Linear(in_dim, out_dim))
self.encoder.append(nn.ReLU())
self.encoder.pop(-1)
for in_dim, out_dim in zip(config.dim_dnn[::-1][:-1], config.dim_dnn[::-1][1:]):
self.decoder.append(nn.Linear(in_dim, out_dim))
self.decoder.append(nn.ReLU())
self.decoder.pop(-1)
self.rq_layer = ResidualVQ(
dim = config.dim,
codebook_size = config.codebook_size,
num_quantizers = config.num_quantizers,
kmeans_init = config.kmeans_init,
kmeans_iters = config.kmeans_iters,
stochastic_sample_codes = config.sample_codes,
sample_codebook_temp = 0.1
)
self.post_init()
def forward(self, x):
x = self.encoder(x)
x, indices, commit_loss = self.rq_layer(x)
x = self.decoder(x)
return x, indices, commit_loss
def post_init(self):
self.encoder.apply(weight_init)
self.decoder.apply(weight_init)
if __name__ == "__main__":
parser = HfArgumentParser((QuantArgs, TrainingArgs))
quant_args, training_args = parser.parse_args_into_dataclasses()
set_seed(training_args.seed)
device = "cuda" if torch.cuda.is_available() else "cpu"
# Construct data and dataloader
print("Constructing dataset.")
embed_dataset = EmbeddingDataset(quant_args.embeddings)
train_dataloader = DataLoader(dataset=embed_dataset, batch_size=training_args.batch_size, shuffle=True, num_workers=4)
eval_dataloader = DataLoader(dataset=embed_dataset, batch_size=training_args.batch_size, shuffle=False, num_workers=4)
# Train
print("Constructing model.")
model = RQAutoEncoder(quant_args).to(device)
optimizer = torch.optim.AdamW(model.parameters(), lr=training_args.lr, weight_decay=training_args.wd)
print("Training.")
for epoch in (pbar := trange(training_args.epochs)):
total_rec_loss = []
total_cmt_loss = []
total_indices = []
model.train()
for x in train_dataloader:
x = x.to(device)
x_rec, indices, cmt_loss = model(x)
rec_loss = (x_rec - x).abs().mean()
cmt_loss = cmt_loss.sum()
loss = rec_loss + quant_args.alpha * cmt_loss
total_rec_loss.append(rec_loss.item())
total_cmt_loss.append(cmt_loss.item())
total_indices.append(indices.detach().cpu())
optimizer.zero_grad()
loss.backward()
optimizer.step()
model.eval()
with torch.no_grad():
total_indices = torch.cat(total_indices, dim=0).t()
active_ratio = []
active_min_count = []
active_max_count = []
for c, idx in enumerate(total_indices):
uniques, counts = torch.unique(idx, return_counts=True)
# if (epoch + 1) % 100 == 0:
# for elem, cnt in zip(uniques.detach().cpu(), counts.detach().cpu()):
# print(f"Codebook {c}, idx {elem}, count {cnt}", flush=True)
min_count = counts.min().item()
max_count = counts.max().item()
active_ratio.append(uniques.numel()/quant_args.codebook_size)
active_min_count.append(min_count)
active_max_count.append(max_count)
assert len(active_ratio) == quant_args.num_quantizers
pbar.set_description(f"Epoch {epoch}")
pbar.set_postfix({
"rec loss": f"{np.mean(total_rec_loss):.4f}",
"cmt loss": f"{np.mean(total_cmt_loss):.4f}",
"active ratio": ", ".join(map(lambda x: f"{100*x:.3f}%", active_ratio)),
"min": ", ".join(map(lambda x: f"{x}", active_min_count)),
"max": ", ".join(map(lambda x: f"{x}", active_max_count))
})
# Save quantization indices and embeddings
model.eval()
rec_embeds, total_indices = [], []
import time
start_time = time.time()
with torch.no_grad():
for x in eval_dataloader:
x = x.to(device)
x_rec, indices, _ = model(x)
rec_embeds.append(x_rec.detach().cpu().numpy())
total_indices.append(indices.detach().cpu().numpy())
rec_embeds = np.concatenate(rec_embeds, axis=0)
total_indices = np.concatenate(total_indices, axis=0)
codebooks = model.rq_layer.codebooks.detach().cpu().numpy()
end_time = time.time()
print(f"Time taken: {end_time - start_time:.2f}s")
print("rec_embeds:", rec_embeds.shape)
print("total indices:", total_indices.shape)
print("codebooks", codebooks.shape)
sub_dir = f"{quant_args.output_dir}/{quant_args.dataset}/{quant_args.embed_type}"
os.makedirs(sub_dir, exist_ok=True)
np.save(f"{sub_dir}/rec_embeds_cbNum{quant_args.num_quantizers}_cbSize{quant_args.codebook_size}.npy", rec_embeds)
np.save(f"{sub_dir}/indices_cbNum{quant_args.num_quantizers}_cbSize{quant_args.codebook_size}.npy", total_indices)
np.save(f"{sub_dir}/codebooks_cbNum{quant_args.num_quantizers}_cbSize{quant_args.codebook_size}.npy", codebooks)
torch.save(model.state_dict(), f"{quant_args.output_dir}/{quant_args.embed_type}.pth")