forked from MurtyShikhar/structural-grokking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraining_utils.py
315 lines (285 loc) · 9.67 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
import torch
import sequence
from tqdm import tqdm
import os
import wandb
### NOTE: change this to your own wandb project and entity!
wandb.init(project="structural-grokking", 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_lm(model_interface, val_datasets, best_accs, device, num_steps, collator):
def helper(validation):
model_interface.model.eval()
loss_curr = 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, normalize=True)
loss_curr += res.loss.cpu().numpy()
total += 1
return loss_curr / total
eval_batch_size = 32
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)
plots["curr-{}-ppl".format(key)] = curr_accs[key]
best_accs = {key: min(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 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(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,
):
assert model.model.mode == "lm"
best_accs, curr_accs = eval_lm(
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,
callback_fn=None,
):
num_steps = 0
max_grad_norm = 1
train_batch_size = 8
accum_steps = 1
eval_every = 10000
max_steps = 2000000
opt = get_opt(args.lr, model)
scheduler = get_scheduler(opt, max_steps)
if tokenizer is not None:
train_data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
else:
train_data_collator = collate.VarLengthCollate(tokenizer)
best_ppl = {key: 10000.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_ppl, curr_ppl = eval_callback(
args,
model,
val_datasets,
tokenizer,
best_ppl,
device,
num_steps,
train_data_collator,
)
print(curr_ppl)
if callback_fn is not None:
val_score = callback_fn("val")
test_score = callback_fn("test")
print(val_score, test_score)
wandbdict = {
"iteration": num_steps,
"val_aux": val_score,
"test_aux": test_score,
}
wandb.log(wandbdict)
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 > max_steps:
break
print("Best Perplexities,", best_ppl)
return