-
Notifications
You must be signed in to change notification settings - Fork 26
/
finetune.py
338 lines (247 loc) · 12.2 KB
/
finetune.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import os
import json
import torch
from tqdm import tqdm
from multiprocessing import Pool
from pathlib import Path
from argparse import ArgumentParser
from torch.utils.data import DataLoader
from torch.cuda.amp import autocast
from pytorch_lightning import seed_everything
from utils import read_json, AverageMeterSet, Ranker
from optimization import create_optimizer_and_scheduler
from recformer import RecformerModel, RecformerForSeqRec, RecformerTokenizer, RecformerConfig
from collator import FinetuneDataCollatorWithPadding, EvalDataCollatorWithPadding
from dataloader import RecformerTrainDataset, RecformerEvalDataset
def load_data(args):
train = read_json(os.path.join(args.data_path, args.train_file), True)
val = read_json(os.path.join(args.data_path, args.dev_file), True)
test = read_json(os.path.join(args.data_path, args.test_file), True)
item_meta_dict = json.load(open(os.path.join(args.data_path, args.meta_file)))
item2id = read_json(os.path.join(args.data_path, args.item2id_file))
id2item = {v:k for k, v in item2id.items()}
item_meta_dict_filted = dict()
for k, v in item_meta_dict.items():
if k in item2id:
item_meta_dict_filted[k] = v
return train, val, test, item_meta_dict_filted, item2id, id2item
tokenizer_glb: RecformerTokenizer = None
def _par_tokenize_doc(doc):
item_id, item_attr = doc
input_ids, token_type_ids = tokenizer_glb.encode_item(item_attr)
return item_id, input_ids, token_type_ids
def encode_all_items(model: RecformerModel, tokenizer: RecformerTokenizer, tokenized_items, args):
model.eval()
items = sorted(list(tokenized_items.items()), key=lambda x: x[0])
items = [ele[1] for ele in items]
item_embeddings = []
with torch.no_grad():
for i in tqdm(range(0, len(items), args.batch_size), ncols=100, desc='Encode all items'):
item_batch = [[item] for item in items[i:i+args.batch_size]]
inputs = tokenizer.batch_encode(item_batch, encode_item=False)
for k, v in inputs.items():
inputs[k] = torch.LongTensor(v).to(args.device)
outputs = model(**inputs)
item_embeddings.append(outputs.pooler_output.detach())
item_embeddings = torch.cat(item_embeddings, dim=0)#.cpu()
return item_embeddings
def eval(model, dataloader, args):
model.eval()
ranker = Ranker(args.metric_ks)
average_meter_set = AverageMeterSet()
for batch, labels in tqdm(dataloader, ncols=100, desc='Evaluate'):
for k, v in batch.items():
batch[k] = v.to(args.device)
labels = labels.to(args.device)
with torch.no_grad():
scores = model(**batch)
res = ranker(scores, labels)
metrics = {}
for i, k in enumerate(args.metric_ks):
metrics["NDCG@%d" % k] = res[2*i]
metrics["Recall@%d" % k] = res[2*i+1]
metrics["MRR"] = res[-3]
metrics["AUC"] = res[-2]
for k, v in metrics.items():
average_meter_set.update(k, v)
average_metrics = average_meter_set.averages()
return average_metrics
def train_one_epoch(model, dataloader, optimizer, scheduler, scaler, args):
model.train()
for step, batch in enumerate(tqdm(dataloader, ncols=100, desc='Training')):
for k, v in batch.items():
batch[k] = v.to(args.device)
if args.fp16:
with autocast():
loss = model(**batch)
else:
loss = model(**batch)
if args.gradient_accumulation_steps > 1:
loss = loss / args.gradient_accumulation_steps
if args.fp16:
scaler.scale(loss).backward()
else:
loss.backward()
if (step + 1) % args.gradient_accumulation_steps == 0:
if args.fp16:
scale_before = scaler.get_scale()
scaler.step(optimizer)
scaler.update()
scale_after = scaler.get_scale()
optimizer_was_run = scale_before <= scale_after
optimizer.zero_grad()
if optimizer_was_run:
scheduler.step()
else:
scheduler.step() # Update learning rate schedule
optimizer.step()
optimizer.zero_grad()
def main():
parser = ArgumentParser()
# path and file
parser.add_argument('--pretrain_ckpt', type=str, default=None, required=True)
parser.add_argument('--data_path', type=str, default=None, required=True)
parser.add_argument('--output_dir', type=str, default='checkpoints')
parser.add_argument('--ckpt', type=str, default='best_model.bin')
parser.add_argument('--model_name_or_path', type=str, default='allenai/longformer-base-4096')
parser.add_argument('--train_file', type=str, default='train.json')
parser.add_argument('--dev_file', type=str, default='val.json')
parser.add_argument('--test_file', type=str, default='test.json')
parser.add_argument('--item2id_file', type=str, default='smap.json')
parser.add_argument('--meta_file', type=str, default='meta_data.json')
# data process
parser.add_argument('--preprocessing_num_workers', type=int, default=8, help="The number of processes to use for the preprocessing.")
parser.add_argument('--dataloader_num_workers', type=int, default=0)
# model
parser.add_argument('--temp', type=float, default=0.05, help="Temperature for softmax.")
# train
parser.add_argument('--num_train_epochs', type=int, default=16)
parser.add_argument('--gradient_accumulation_steps', type=int, default=8)
parser.add_argument('--finetune_negative_sample_size', type=int, default=1000)
parser.add_argument('--metric_ks', nargs='+', type=int, default=[10, 50], help='ks for Metric@k')
parser.add_argument('--batch_size', type=int, default=16)
parser.add_argument('--learning_rate', type=float, default=5e-5)
parser.add_argument('--weight_decay', type=float, default=0)
parser.add_argument('--warmup_steps', type=int, default=100)
parser.add_argument('--device', type=int, default=0)
parser.add_argument('--fp16', action='store_true')
parser.add_argument('--fix_word_embedding', action='store_true')
parser.add_argument('--verbose', type=int, default=3)
args = parser.parse_args()
print(args)
seed_everything(42)
args.device = torch.device('cuda:{}'.format(args.device)) if args.device>=0 else torch.device('cpu')
train, val, test, item_meta_dict, item2id, id2item = load_data(args)
config = RecformerConfig.from_pretrained(args.model_name_or_path)
config.max_attr_num = 3
config.max_attr_length = 32
config.max_item_embeddings = 51
config.attention_window = [64] * 12
config.max_token_num = 1024
config.item_num = len(item2id)
config.finetune_negative_sample_size = args.finetune_negative_sample_size
tokenizer = RecformerTokenizer.from_pretrained(args.model_name_or_path, config)
global tokenizer_glb
tokenizer_glb = tokenizer
path_corpus = Path(args.data_path)
dir_preprocess = path_corpus / 'preprocess'
dir_preprocess.mkdir(exist_ok=True)
path_output = Path(args.output_dir) / path_corpus.name
path_output.mkdir(exist_ok=True, parents=True)
path_ckpt = path_output / args.ckpt
path_tokenized_items = dir_preprocess / f'tokenized_items_{path_corpus.name}'
if path_tokenized_items.exists():
print(f'[Preprocessor] Use cache: {path_tokenized_items}')
else:
print(f'Loading attribute data {path_corpus}')
pool = Pool(processes=args.preprocessing_num_workers)
pool_func = pool.imap(func=_par_tokenize_doc, iterable=item_meta_dict.items())
doc_tuples = list(tqdm(pool_func, total=len(item_meta_dict), ncols=100, desc=f'[Tokenize] {path_corpus}'))
tokenized_items = {item2id[item_id]: [input_ids, token_type_ids] for item_id, input_ids, token_type_ids in doc_tuples}
pool.close()
pool.join()
torch.save(tokenized_items, path_tokenized_items)
tokenized_items = torch.load(path_tokenized_items)
print(f'Successfully load {len(tokenized_items)} tokenized items.')
finetune_data_collator = FinetuneDataCollatorWithPadding(tokenizer, tokenized_items)
eval_data_collator = EvalDataCollatorWithPadding(tokenizer, tokenized_items)
train_data = RecformerTrainDataset(train, collator=finetune_data_collator)
val_data = RecformerEvalDataset(train, val, test, mode='val', collator=eval_data_collator)
test_data = RecformerEvalDataset(train, val, test, mode='test', collator=eval_data_collator)
train_loader = DataLoader(train_data,
batch_size=args.batch_size,
shuffle=True,
collate_fn=train_data.collate_fn)
dev_loader = DataLoader(val_data,
batch_size=args.batch_size,
collate_fn=val_data.collate_fn)
test_loader = DataLoader(test_data,
batch_size=args.batch_size,
collate_fn=test_data.collate_fn)
model = RecformerForSeqRec(config)
pretrain_ckpt = torch.load(args.pretrain_ckpt)
model.load_state_dict(pretrain_ckpt, strict=False)
model.to(args.device)
if args.fix_word_embedding:
print('Fix word embeddings.')
for param in model.longformer.embeddings.word_embeddings.parameters():
param.requires_grad = False
path_item_embeddings = dir_preprocess / f'item_embeddings_{path_corpus.name}'
if path_item_embeddings.exists():
print(f'[Item Embeddings] Use cache: {path_tokenized_items}')
else:
print(f'Encoding items.')
item_embeddings = encode_all_items(model.longformer, tokenizer, tokenized_items, args)
torch.save(item_embeddings, path_item_embeddings)
item_embeddings = torch.load(path_item_embeddings)
model.init_item_embedding(item_embeddings)
model.to(args.device) # send item embeddings to device
num_train_optimization_steps = int(len(train_loader) / args.gradient_accumulation_steps) * args.num_train_epochs
optimizer, scheduler = create_optimizer_and_scheduler(model, num_train_optimization_steps, args)
if args.fp16:
scaler = torch.cuda.amp.GradScaler()
else:
scaler = None
test_metrics = eval(model, test_loader, args)
print(f'Test set: {test_metrics}')
best_target = float('-inf')
patient = 5
for epoch in range(args.num_train_epochs):
item_embeddings = encode_all_items(model.longformer, tokenizer, tokenized_items, args)
model.init_item_embedding(item_embeddings)
train_one_epoch(model, train_loader, optimizer, scheduler, scaler, args)
if (epoch + 1) % args.verbose == 0:
dev_metrics = eval(model, dev_loader, args)
print(f'Epoch: {epoch}. Dev set: {dev_metrics}')
if dev_metrics['NDCG@10'] > best_target:
print('Save the best model.')
best_target = dev_metrics['NDCG@10']
patient = 5
torch.save(model.state_dict(), path_ckpt)
else:
patient -= 1
if patient == 0:
break
print('Load best model in stage 1.')
model.load_state_dict(torch.load(path_ckpt))
patient = 3
for epoch in range(args.num_train_epochs):
train_one_epoch(model, train_loader, optimizer, scheduler, scaler, args)
if (epoch + 1) % args.verbose == 0:
dev_metrics = eval(model, dev_loader, args)
print(f'Epoch: {epoch}. Dev set: {dev_metrics}')
if dev_metrics['NDCG@10'] > best_target:
print('Save the best model.')
best_target = dev_metrics['NDCG@10']
patient = 3
torch.save(model.state_dict(), path_ckpt)
else:
patient -= 1
if patient == 0:
break
print('Test with the best checkpoint.')
model.load_state_dict(torch.load(path_ckpt))
test_metrics = eval(model, test_loader, args)
print(f'Test set: {test_metrics}')
if __name__ == "__main__":
main()