-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtraining_utils.py
397 lines (360 loc) · 12.3 KB
/
training_utils.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import torch
from mlm_collator import MaskCollator
from tqdm import tqdm
import os
import wandb
### remove this and set this to your own project name!
wandb.init(project="compositionality", entity="shikharmurty")
from transformers import get_linear_schedule_with_warmup
from torch.optim import AdamW
from transformers.data.data_collator import DataCollatorWithPadding
from torch.utils.data import (
DataLoader,
RandomSampler,
SequentialSampler,
)
import collate
import wandb
from plot import CustomPlot
def get_grad_norm(model):
total_norm = 0
parameters = [
p for p in model.parameters() if p.grad is not None and p.requires_grad
]
for p in parameters:
param_norm = p.grad.detach().data.norm(2)
total_norm += param_norm.item() ** 2
total_norm = total_norm**0.5
return total_norm
def get_opt(lr, model):
if type(model) != torch.nn.Module:
model = model.model
no_decay = ["bias", "LayerNorm.weight"]
weight_decay = 0.0
adam_epsilon = 1e-7
optimizer_grouped_parameters = [
{
"params": [
p
for n, p in model.named_parameters()
if not any(nd in n for nd in no_decay)
],
"weight_decay": weight_decay,
},
{
"params": [
p
for n, p in model.named_parameters()
if any(nd in n for nd in no_decay)
],
"weight_decay": 0.0,
},
]
optimizer = AdamW(
optimizer_grouped_parameters,
lr=lr,
eps=adam_epsilon,
)
return optimizer
def get_scheduler(opt, t_total):
num_warmup_steps = 10000
scheduler = get_linear_schedule_with_warmup(
opt, num_warmup_steps=num_warmup_steps, num_training_steps=t_total
)
return scheduler
def eval_mlm(model_interface, val_datasets, best_accs, device, num_steps, collator):
def helper(validation):
model_interface.model.eval()
acc = 0
total = 0
with torch.no_grad():
for batch in tqdm(validation):
batch_gpu = {}
for key in batch:
batch_gpu[key] = batch[key].to(device)
res = model_interface(batch_gpu)
mask_logits = res.outputs
labels = batch_gpu["labels"].transpose(0, 1)
acc += (
(mask_logits.argmax(dim=-1) == labels)
* (batch_gpu["in"].transpose(0, 1) == collator.mask_token_id)
).sum()
total += (batch["in"] == collator.mask_token_id).sum()
return (acc / total).item()
eval_batch_size = 256
plots = {}
curr_accs = {}
for key, val_dataset in val_datasets.items():
validation = DataLoader(
val_dataset,
sampler=SequentialSampler(val_dataset),
batch_size=eval_batch_size,
collate_fn=collator,
)
curr_accs[key] = helper(validation)
best_accs = {key: max(curr_accs[key], best_accs[key]) for key in curr_accs}
plots.update({"best/{}": v for k, v in best_accs.items()})
plotting_util(plots, num_steps)
return best_accs, curr_accs
def eval_func_all(
args,
model_interface,
val_datasets,
tokenizer,
best_accs,
device,
num_steps,
eval_batch_size=None,
):
eval_batch_size = 256 if not eval_batch_size else eval_batch_size
if tokenizer is not None:
val_data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
else:
val_data_collator = collate.VarLengthCollate(tokenizer)
plots = {}
curr_accs = {}
ok_dict = {}
for key, val_dataset in val_datasets.items():
validation = DataLoader(
val_dataset,
sampler=SequentialSampler(val_dataset),
batch_size=eval_batch_size,
collate_fn=val_data_collator,
)
curr_accs[key], plots_curr, ok_all = eval_func_helper(
model_interface, validation, tokenizer, device, num_steps, key
)
plots.update({"{}/{}".format(key, k): v for k, v in plots_curr.items()})
ok_dict[key] = ok_all
best_accs = {key: max(curr_accs[key], best_accs[key]) for key in curr_accs}
plots.update({"best/{}": v for k, v in best_accs.items()})
plotting_util(plots, num_steps)
return best_accs, curr_accs, ok_all
def plotting_util(dict_of_elems, step):
wandbdict = {}
for k, v in dict_of_elems.items():
if isinstance(v, CustomPlot):
v = v.to_wandb()
if v is None:
continue
if isinstance(v, dict):
for k2, v2 in v.items():
wandbdict[k + "/" + k2] = v2
else:
wandbdict[k] = v
elif isinstance(v, (int, float)):
wandbdict[k] = v
else:
assert False, f"Invalid data type {type(v)}"
wandbdict["iteration"] = step
wandb.log(wandbdict)
def eval_func_helper(model_interface, validation, tokenizer, device, step, dkey):
model_interface.model.eval()
test = validation.dataset.start_test()
with torch.no_grad():
for batch in tqdm(validation):
batch_gpu = {}
for key in batch:
batch_gpu[key] = batch[key].to(device)
res = model_interface(batch_gpu)
outputs = model_interface.decode_outputs(res)
test.step(outputs, batch_gpu)
curr_acc = test.accuracy
return curr_acc, test.plot(), None
def eval_func(model, validation, tokenizer, best_acc, device):
def get_decoding_acc(outputs, labels):
acc = 0
for out, label in zip(outputs, labels):
dec_str = tokenizer.decode(out, skip_special_tokens=True)
label = [(l if l != -100 else tokenizer.pad_token_id) for l in label]
orig_str = tokenizer.decode(label, skip_special_tokens=True)
acc += dec_str == orig_str
return acc
curr_acc = 0
total = 0
if type(model) != torch.nn.Module:
model.model.eval()
else:
model.eval()
with torch.no_grad():
for batch in tqdm(validation):
batch_gpu = {}
for key in batch:
batch_gpu[key] = batch[key].to(device)
curr_acc += get_decoding_acc(
model.generate(batch_gpu["input_ids"]).cpu().tolist(),
batch["labels"].cpu().tolist(),
)
total += len(batch["labels"])
curr_acc /= 1.0 * total
print("Current Accuracy: {:.4f}".format(curr_acc))
if curr_acc > best_acc:
return curr_acc
else:
return best_acc
def eval_callback(
args,
model,
val_datasets,
tokenizer,
best_accs,
device,
num_steps,
train_data_collator,
):
if model.model.mode == "enc_dec":
best_accs, curr_accs, _ = eval_func_all(
args,
model,
val_datasets,
tokenizer,
best_accs,
device,
num_steps,
)
else:
best_accs, curr_accs = eval_mlm(
model,
val_datasets,
best_accs,
device,
num_steps,
train_data_collator,
)
return best_accs, curr_accs
def train_loop(
args,
model,
train_dataset,
val_datasets,
device,
save_dir,
tokenizer=None,
metric="acc",
in_vocab=None,
):
num_steps = 0
max_grad_norm = 1
train_batch_size = 32
accum_steps = 1
eval_every = 1000
max_steps = 100000
opt = get_opt(args.lr, model)
scheduler = get_scheduler(opt, max_steps)
if tokenizer is not None:
train_data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
elif model.model.mode == "enc_dec":
train_data_collator = collate.VarLengthCollate(tokenizer)
else:
train_data_collator = MaskCollator(
collate.VarLengthCollate(None), 0.2, in_vocab["<mask>"]
)
best_accs = {key: 0.0 for key in val_datasets}
while True:
train_dataloader = DataLoader(
train_dataset,
sampler=RandomSampler(train_dataset),
batch_size=train_batch_size,
collate_fn=train_data_collator,
)
total_train_sz = len(train_dataset)
if num_steps > max_steps:
break
with torch.enable_grad(), tqdm(total=total_train_sz) as progress_bar:
losses = []
for curr_batch_dict in train_dataloader:
if type(model) != torch.nn.Module:
model.model.train()
else:
model.train()
curr_batch_dict_gpu = {}
for key in curr_batch_dict:
curr_batch_dict_gpu[key] = curr_batch_dict[key].to(device)
loss_curr = model(curr_batch_dict_gpu).loss
progress_bar.update(curr_batch_dict["in"].shape[1])
losses.append(loss_curr.item())
loss_curr /= accum_steps
loss_curr.backward()
if len(losses) == accum_steps:
num_steps += 1
torch.nn.utils.clip_grad_norm_(
model.model.parameters(), max_grad_norm
)
progress_bar.set_postfix(
{"loss": sum(losses), "num_steps": num_steps}
)
grad_norm = get_grad_norm(model.model)
wandb.log(
{
"loss": sum(losses),
"grad_norm": grad_norm,
"iteration": num_steps,
}
)
opt.step()
scheduler.step()
model.model.zero_grad()
losses = []
if num_steps % eval_every == 0:
print("Evaluating at step {}".format(num_steps))
best_accs, curr_accs = eval_callback(
args,
model,
val_datasets,
tokenizer,
best_accs,
device,
num_steps,
train_data_collator
)
print(curr_accs)
if len(save_dir) > 0:
torch.save(
model.model.state_dict(),
os.path.join(
save_dir, "checkpoint_{}.pickle".format(num_steps)
),
)
if num_steps > max_steps:
break
if losses:
num_steps += 1
progress_bar.set_postfix({"loss": sum(losses), "num_steps": num_steps})
grad_norm = get_grad_norm(model.model)
wandb.log(
{
"loss": sum(losses),
"grad_norm": grad_norm,
"iteration": num_steps,
}
)
torch.nn.utils.clip_grad_norm_(model.model.parameters(), max_grad_norm)
opt.step()
scheduler.step()
model.model.zero_grad()
losses = []
if num_steps % eval_every == 0:
print("Evaluating at step {}".format(num_steps))
best_accs, curr_accs = eval_callback(
args,
model,
val_datasets,
tokenizer,
best_accs,
device,
num_steps,
train_data_collator
)
# save model
print(curr_accs)
if len(save_dir) > 0:
torch.save(
model.model.state_dict(),
os.path.join(
save_dir, "checkpoint_{}.pickle".format(num_steps)
),
)
if num_steps > max_steps:
break
print("Best Accuracies,", best_accs)
return