-
Notifications
You must be signed in to change notification settings - Fork 42
/
train.py
172 lines (153 loc) · 6.42 KB
/
train.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
import os
import shutil
import argparse
import torch
import torch.utils.tensorboard
from torch.nn.utils import clip_grad_norm_
from torch.utils.data import DataLoader
from tqdm.auto import tqdm
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
from diffab.datasets import get_dataset
from diffab.models import get_model
from diffab.utils.misc import *
from diffab.utils.data import *
from diffab.utils.train import *
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('config', type=str)
parser.add_argument('--logdir', type=str, default='./logs')
parser.add_argument('--debug', action='store_true', default=False)
parser.add_argument('--device', type=str, default='cuda')
parser.add_argument('--num_workers', type=int, default=8)
parser.add_argument('--tag', type=str, default='')
parser.add_argument('--resume', type=str, default=None)
parser.add_argument('--finetune', type=str, default=None)
args = parser.parse_args()
# Load configs
config, config_name = load_config(args.config)
seed_all(config.train.seed)
# Logging
if args.debug:
logger = get_logger('train', None)
writer = BlackHole()
else:
if args.resume:
log_dir = os.path.dirname(os.path.dirname(args.resume))
else:
log_dir = get_new_log_dir(args.logdir, prefix=config_name, tag=args.tag)
ckpt_dir = os.path.join(log_dir, 'checkpoints')
if not os.path.exists(ckpt_dir): os.makedirs(ckpt_dir)
logger = get_logger('train', log_dir)
writer = torch.utils.tensorboard.SummaryWriter(log_dir)
tensorboard_trace_handler = torch.profiler.tensorboard_trace_handler(log_dir)
if not os.path.exists(os.path.join(log_dir, os.path.basename(args.config))):
shutil.copyfile(args.config, os.path.join(log_dir, os.path.basename(args.config)))
logger.info(args)
logger.info(config)
# Data
logger.info('Loading dataset...')
train_dataset = get_dataset(config.dataset.train)
val_dataset = get_dataset(config.dataset.val)
train_iterator = inf_iterator(DataLoader(
train_dataset,
batch_size=config.train.batch_size,
collate_fn=PaddingCollate(),
shuffle=True,
num_workers=args.num_workers
))
val_loader = DataLoader(val_dataset, batch_size=config.train.batch_size, collate_fn=PaddingCollate(), shuffle=False, num_workers=args.num_workers)
logger.info('Train %d | Val %d' % (len(train_dataset), len(val_dataset)))
# Model
logger.info('Building model...')
model = get_model(config.model).to(args.device)
logger.info('Number of parameters: %d' % count_parameters(model))
# Optimizer & scheduler
optimizer = get_optimizer(config.train.optimizer, model)
scheduler = get_scheduler(config.train.scheduler, optimizer)
optimizer.zero_grad()
it_first = 1
# Resume
if args.resume is not None or args.finetune is not None:
ckpt_path = args.resume if args.resume is not None else args.finetune
logger.info('Resuming from checkpoint: %s' % ckpt_path)
ckpt = torch.load(ckpt_path, map_location=args.device)
it_first = ckpt['iteration'] # + 1
model.load_state_dict(ckpt['model'])
logger.info('Resuming optimizer states...')
optimizer.load_state_dict(ckpt['optimizer'])
logger.info('Resuming scheduler states...')
scheduler.load_state_dict(ckpt['scheduler'])
# Train
def train(it):
time_start = current_milli_time()
model.train()
# Prepare data
batch = recursive_to(next(train_iterator), args.device)
# Forward
# if args.debug: torch.set_anomaly_enabled(True)
loss_dict = model(batch)
loss = sum_weighted_losses(loss_dict, config.train.loss_weights)
loss_dict['overall'] = loss
time_forward_end = current_milli_time()
# Backward
loss.backward()
orig_grad_norm = clip_grad_norm_(model.parameters(), config.train.max_grad_norm)
optimizer.step()
optimizer.zero_grad()
time_backward_end = current_milli_time()
# Logging
log_losses(loss_dict, it, 'train', logger, writer, others={
'grad': orig_grad_norm,
'lr': optimizer.param_groups[0]['lr'],
'time_forward': (time_forward_end - time_start) / 1000,
'time_backward': (time_backward_end - time_forward_end) / 1000,
})
if not torch.isfinite(loss):
logger.error('NaN or Inf detected.')
torch.save({
'config': config,
'model': model.state_dict(),
'optimizer': optimizer.state_dict(),
'scheduler': scheduler.state_dict(),
'iteration': it,
'batch': recursive_to(batch, 'cpu'),
}, os.path.join(log_dir, 'checkpoint_nan_%d.pt' % it))
raise KeyboardInterrupt()
# Validate
def validate(it):
loss_tape = ValidationLossTape()
with torch.no_grad():
model.eval()
for i, batch in enumerate(tqdm(val_loader, desc='Validate', dynamic_ncols=True)):
# Prepare data
batch = recursive_to(batch, args.device)
# Forward
loss_dict = model(batch)
loss = sum_weighted_losses(loss_dict, config.train.loss_weights)
loss_dict['overall'] = loss
loss_tape.update(loss_dict, 1)
avg_loss = loss_tape.log(it, logger, writer, 'val')
# Trigger scheduler
if config.train.scheduler.type == 'plateau':
scheduler.step(avg_loss)
else:
scheduler.step()
return avg_loss
try:
for it in range(it_first, config.train.max_iters + 1):
train(it)
if it % config.train.val_freq == 0:
avg_val_loss = validate(it)
if not args.debug:
ckpt_path = os.path.join(ckpt_dir, '%d.pt' % it)
torch.save({
'config': config,
'model': model.state_dict(),
'optimizer': optimizer.state_dict(),
'scheduler': scheduler.state_dict(),
'iteration': it,
'avg_val_loss': avg_val_loss,
}, ckpt_path)
except KeyboardInterrupt:
logger.info('Terminating...')