-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtrain_decoder.py
216 lines (179 loc) · 6.85 KB
/
train_decoder.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
206
207
208
209
210
211
212
213
214
215
216
import argparse
import os
import gin
import torch
import wandb
from accelerate import Accelerator
from data.processed import ItemData
from data.processed import RecDataset
from data.processed import SeqData
from data.utils import batch_to
from data.utils import cycle
from data.utils import next_batch
from evaluate.metrics import TopKAccumulator
from modules.model import DecoderRetrievalModel
from modules.scheduler.inv_sqrt import InverseSquareRootScheduler
from modules.tokenizer.semids import SemanticIdTokenizer
from modules.utils import parse_config
from torch.optim import AdamW
from torch.utils.data import BatchSampler
from torch.utils.data import DataLoader
from torch.utils.data import RandomSampler
from tqdm import tqdm
@gin.configurable
def train(
iterations=500000,
batch_size=64,
learning_rate=0.001,
weight_decay=0.01,
max_grad_norm=1,
dataset_folder="dataset/ml-1m",
save_dir_root="out/",
dataset=RecDataset.ML_1M,
pretrained_rqvae_path=None,
split_batches=True,
amp=False,
wandb_logging=False,
force_dataset_process=False,
mixed_precision_type="fp16",
gradient_accumulate_every=1,
save_model_every=1000000,
eval_every=10000,
vae_input_dim=18,
vae_embed_dim=16,
vae_hidden_dims=[18, 18],
vae_codebook_size=32,
vae_codebook_normalize=False,
vae_sim_vq=False,
vae_n_cat_feats=18,
vae_n_layers=3,
decoder_embed_dim=64,
attn_dropout=False,
attn_heads=8,
attn_embed_dim=64,
attn_layers=4,
dataset_split="beauty"
):
if wandb_logging:
params = locals()
accelerator = Accelerator(
split_batches=split_batches,
mixed_precision=mixed_precision_type if amp else 'no'
)
device = accelerator.device
if wandb_logging and accelerator.is_main_process:
wandb.login()
run = wandb.init(
project="gen-retrieval-decoder-training",
config=params
)
movie_dataset = ItemData(
root=dataset_folder,
dataset=dataset,
force_process=force_dataset_process,
split=dataset_split
)
train_dataset = SeqData(root=dataset_folder, dataset=dataset, is_train=True, split=dataset_split)
eval_dataset = SeqData(root=dataset_folder, dataset=dataset, is_train=False, split=dataset_split)
train_sampler = BatchSampler(RandomSampler(train_dataset), batch_size, drop_last=True)
train_dataloader = DataLoader(train_dataset, batch_size=None, sampler=train_sampler, collate_fn=lambda batch: batch)
train_dataloader = cycle(train_dataloader)
eval_dataloader = DataLoader(eval_dataset, batch_size=batch_size, shuffle=True)
train_dataloader, eval_dataloader = accelerator.prepare(
train_dataloader, eval_dataloader
)
tokenizer = SemanticIdTokenizer(
input_dim=vae_input_dim,
hidden_dims=vae_hidden_dims,
output_dim=vae_embed_dim,
codebook_size=vae_codebook_size,
n_layers=vae_n_layers,
n_cat_feats=vae_n_cat_feats,
rqvae_weights_path=pretrained_rqvae_path,
rqvae_codebook_normalize=vae_codebook_normalize,
rqvae_sim_vq=vae_sim_vq
)
tokenizer = accelerator.prepare(tokenizer)
tokenizer.precompute_corpus_ids(movie_dataset)
# import pdb; pdb.set_trace()
model = DecoderRetrievalModel(
embedding_dim=decoder_embed_dim,
attn_dim=attn_embed_dim,
dropout=attn_dropout,
num_heads=attn_heads,
n_layers=attn_layers,
num_embeddings=vae_codebook_size,
inference_verifier_fn=lambda x: tokenizer.exists_prefix(x),
sem_id_dim=tokenizer.sem_ids_dim,
max_pos=train_dataset.max_seq_len*tokenizer.sem_ids_dim
)
optimizer = AdamW(
params=model.parameters(),
lr=learning_rate,
weight_decay=weight_decay
)
lr_scheduler = InverseSquareRootScheduler(
optimizer=optimizer,
warmup_steps=10000
)
model, optimizer, lr_scheduler = accelerator.prepare(
model, optimizer, lr_scheduler
)
metrics_accumulator = TopKAccumulator(ks=[1, 5, 10])
num_params = sum(p.numel() for p in model.parameters())
print(f"Device: {device}, Num Parameters: {num_params}")
with tqdm(initial=0, total=iterations,
disable=not accelerator.is_main_process) as pbar:
for iter in range(iterations):
model.train()
total_loss = 0
optimizer.zero_grad()
for _ in range(gradient_accumulate_every):
data = next_batch(train_dataloader, device)
tokenized_data = tokenizer(data)
with accelerator.autocast():
loss = model(tokenized_data).loss
loss = loss / gradient_accumulate_every
total_loss += loss
accelerator.backward(total_loss)
pbar.set_description(f'loss: {total_loss.item():.4f}')
accelerator.wait_for_everyone()
accelerator.clip_grad_norm_(model.parameters(), max_grad_norm)
optimizer.step()
lr_scheduler.step()
accelerator.wait_for_everyone()
if (iter+1) % eval_every == 0:
model.eval()
with tqdm(eval_dataloader, desc=f'Eval {iter+1}', disable=not accelerator.is_main_process) as pbar_eval:
for batch in pbar_eval:
data = batch_to(batch, device)
tokenized_data = tokenizer(data)
generated = model.generate_next_sem_id(tokenized_data, top_k=True)
actual, top_k = tokenized_data.sem_ids_fut, generated.sem_ids
metrics_accumulator.accumulate(actual=actual, top_k=top_k)
eval_metrics = metrics_accumulator.reduce()
print(eval_metrics)
if accelerator.is_main_process and wandb_logging:
wandb.log(eval_metrics)
metrics_accumulator.reset()
if accelerator.is_main_process:
if (iter+1) % save_model_every == 0 or iter+1 == iterations:
state = {
"iter": iter,
"model": model.state_dict(),
"optimizer": optimizer.state_dict()
}
if not os.path.exists(save_dir_root):
os.makedirs(save_dir_root)
torch.save(state, save_dir_root + f"checkpoint_{iter}.pt")
if wandb_logging:
wandb.log({
"learning_rate": optimizer.param_groups[0]["lr"],
"total_loss": total_loss.cpu().item(),
})
pbar.update(1)
if wandb_logging:
wandb.finish()
if __name__ == "__main__":
parse_config()
train()