forked from epfml/landmark-attention
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbase.py
executable file
·136 lines (110 loc) · 5.63 KB
/
base.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
# Copyright 2023 Amirkeivan Mohtashami, Martin Jaggi
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from contextlib import nullcontext
import torch
import torch.nn.functional as F
import wandb
import time
import copy
import traceback
from .utils import get_batch, save_checkpoint
@torch.no_grad()
def eval(model, data_tensor, sequence_length, batch_size, device='cpu', max_num_batches=24, ctx=nullcontext()):
assert model.training == False
loss_list_val, acc_list = [], []
for _ in range(max_num_batches):
x, y = get_batch(data_tensor, sequence_length, batch_size, device=device)
with ctx:
outputs = model(x, targets=y, get_logits=True)
val_loss = outputs['loss']
loss_list_val.append(val_loss)
acc_list.append((outputs['logits'].argmax(-1) == y).float().mean())
val_acc = torch.stack(acc_list).mean().item()
val_loss = torch.stack(loss_list_val).mean().item()
val_perplexity = 2.71828 ** val_loss
return val_acc, val_loss, val_perplexity
def train_base(model, opt, data, scheduler, iterations, acc_steps, batch_size, sequence_length, eval_freq, ckpt_path, distributed_backend, extra_args):
device_type = 'cuda' if 'cuda' in str(extra_args.device) else 'cpu'
type_ctx = nullcontext() if device_type == 'cpu' else torch.amp.autocast(
device_type=device_type, dtype=extra_args.dtype) # extra_args.dtype)
itr, substep, best_val_loss, text_table = 0, 0, float('inf'), None # best_val_loss not used atm, early stopping not recommended but possible
stats = {'train_loss': [], 'val_loss': [], 'val_pp': [], 'val_acc': []}
num_substeps_per_epoch = len(data['train']) // (batch_size * sequence_length)
if not extra_args.no_compile:
print(f"Compiling model ...")
import torch._dynamo as torchdynamo
torchdynamo.config.guard_nn_modules = True
model = torch.compile(model) # requires pytorch 2.0+
model.train()
t0 = time.time()
while itr < iterations:
for microstep_idx in range(acc_steps): # gradient accumulation
x, y = get_batch(data['train'], sequence_length, batch_size, device=extra_args.device)
with type_ctx:
with distributed_backend.get_context_for_microstep_forward(model=model, microstep_idx=microstep_idx, gradient_accumulation_steps=acc_steps):
if getattr(distributed_backend.get_raw_model(model), "needs_iter", False):
outputs = model(x, targets=y, iter=itr)
else:
outputs = model(x, targets=y)
loss = outputs['loss']
loss.backward()
substep += 1
opt.step()
scheduler.step()
opt.zero_grad(set_to_none=True)
itr += 1
if itr % eval_freq == 0 or itr == iterations: # from here it's only evaluation code, all the training is above
if distributed_backend.is_master_process():
t1 = time.time()
dt = t1 - t0
epoch = substep//num_substeps_per_epoch
model.eval()
train_loss = loss.detach().cpu().item()
current_lr = scheduler.get_last_lr()[0] if scheduler is not None else extra_args.lr
val_acc, val_loss, val_perplexity = eval(model, data['val'], sequence_length, batch_size,
extra_args.device, max_num_batches=24, ctx=type_ctx)
print_string = f"{epoch}/{itr} [train] loss={train_loss:.3f} [val] loss={val_loss:.3f}, pp={val_perplexity:.2f}, acc={val_acc:3f}"
print_string += f" [time per itr] {dt*1000/eval_freq:.2f}ms"
if scheduler is not None:
print_string += f" [lr] {current_lr:.5f}"
print(print_string)
if extra_args.wandb:
wandb.log({
"iter": itr,
"train/loss": train_loss,
"val/loss": val_loss,
"val/perplexity": val_perplexity,
"val/acc": val_acc,
"lr": current_lr,
})
model.train()
t0 = time.time()
if distributed_backend.is_master_process():
if extra_args.save_checkpoint_freq is not None and itr % extra_args.save_checkpoint_freq == 0:
print(f"saving checkpoint to {ckpt_path}/ckpt_{itr}.pt")
save_checkpoint(distributed_backend=distributed_backend,
model=model,
opt=opt,
scheduler=scheduler,
itr=itr,
ckpt_path=f"{ckpt_path}/ckpt_{itr}.pt")
if distributed_backend.is_master_process():
print(f"saving checkpoint to {ckpt_path}")
save_checkpoint(distributed_backend=distributed_backend,
model=model,
opt=opt,
scheduler=scheduler,
itr=itr,
ckpt_path=f"{ckpt_path}/ckpt.pt")
return stats