-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
454 lines (362 loc) · 16.4 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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import torch
import torch.nn as nn
import torch.nn.functional as F
from tqdm import tqdm
import math
import threading
import time
from queue import Queue
import torch.cuda.amp as amp
from contextlib import ExitStack
from module import LocalLossBlock
from dataset import DATASET_CONFIGS, get_dataloader
from config import config
from loss import SmoothCrossEntropyLoss
from utils import move_model_state_dict, AverageMeter, get_error
QUEUE_SIZE = config.queue_size
def train_step(x, y, i, model, optimizer, scheduler=None, device='cuda:0',
mixed_precision=False, scaler=None):
x, y = x.to(device), y.to(device)
with ExitStack() as stack:
if mixed_precision:
stack.enter_context(amp.autocast())
out = model(x, isolate_grad=True)
losses = model.get_loss(y)
loss = sum(losses)
for j in range(len(optimizer)):
optimizer[j].zero_grad()
if mixed_precision:
scaler.scale(loss).backward()
for j in range(len(optimizer)):
scaler.step(optimizer[j])
if scheduler is not None:
scheduler[j].step(i+1)
scaler.update()
else:
loss.backward()
for j in range(len(optimizer)):
optimizer[j].step()
if scheduler is not None:
scheduler[j].step(i+1)
return [loss.item() for loss in losses]
def block_train_step(model, optimizer, data=None, target=None,
queue_in=None, queue_out=None, scheduler=None, device=None,
mixed_precision=False, scaler=None):
model.train()
# get item from queue
if data is None:
data, target = queue_in.get()
data, target = data.to(device), target.to(device)
with ExitStack() as stack:
if mixed_precision:
stack.enter_context(amp.autocast())
out = model(data)
if queue_out is not None:
queue_out.put((out.detach().clone(), target.detach().clone()))
loss = model[-1].get_loss(target)
del out # necessary?
del target # necessary?
optimizer.zero_grad()
if mixed_precision:
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
else:
loss.backward()
optimizer.step()
if scheduler is not None:
scheduler.step()
return loss.item()
def continuous_train_step(x, y, time_step, model, optimizer, alpha,
beta=None, categorical=False, scheduler=None, device='cuda:0'):
model.train()
x, y = x.to(device), y.to(device)
out = model(x)
alpha_softmax = F.softmax(alpha, dim=-1)
beta_softmax = F.softmax(beta, dim=-1) if beta is not None else None
dec_out_scale = 1/(alpha_softmax[:, 0].detach() + 1e-5)
dec_out_scale = torch.cat((dec_out_scale, torch.ones((1,), device=device)))
losses = model.get_loss(y, weights=beta_softmax, categorical=categorical, scale=dec_out_scale)
loss = losses[-1]
for i in range(len(losses)-2, -1, -1):
loss = alpha_softmax[i, 1] * loss + alpha_softmax[i, 0] * losses[i]
model.zero_grad()
loss.backward()
blocks = model.get_blocks()
for i in range(model.num_blocks-1, -1, -1):
block = blocks[i]
for parameter in block.parameters():
if parameter.grad is not None:
parameter.grad /= alpha_softmax.data[:i, 1].prod()
if isinstance(optimizer, list):
for opt in optimizer:
opt.step()
else:
optimizer.step()
if scheduler is not None:
if isinstance(scheduler, list):
for sc in scheduler:
sc.step(time_step+1)
else:
scheduler.step(time_step+1)
return [loss.item() for loss in losses]
class BlockThread(threading.Thread):
def __init__(self, idx, module, optimizer, scheduler, queue_in, queue_out,
device, train_iters=1e3, valid_freq=1000, do_validation=False,
mixed_precision=False, scaler=None):
threading.Thread.__init__(self)
self.idx = idx
self.module = module.to(device)
self.optimizer = optimizer
self.scheduler = scheduler
self.queue_in = queue_in
self.queue_out = queue_out
self.device = device
self.train_iters = train_iters
self.valid_freq = valid_freq
self.do_validation = do_validation
self.mixed_precision = mixed_precision
self.scaler = scaler
self.step = 0
self.paused = False
self.pause_cond = threading.Condition(threading.Lock())
def run(self):
while self.step < self.train_iters:
block_train_step(self.module, self.optimizer,
queue_in=self.queue_in,
queue_out=self.queue_out,
scheduler=self.scheduler,
device=self.device,
mixed_precision=self.mixed_precision,
scaler=self.scaler)
if self.do_validation and ((self.step + 1) % self.valid_freq == 0 or self.step == 0):
self.pause()
self.step += 1
while self.queue_out is not None and self.queue_out.qsize() > 0:
time.sleep(1)
def pause(self):
self.paused = True
self.pause_cond.acquire()
#should just resume the thread
def resume(self):
self.paused = False
self.pause_cond.notify()
self.pause_cond.release()
def is_paused(self):
return self.paused
def train(model, optimizer, train_loader, train_iters,
scheduler=None, device='cuda:0', valid_loader=None, valid_freq=100,
alpha=None, beta=None,
mixed_precision=False):
model.train()
pbar = tqdm(total=train_iters)
step = 0
time_per_iter = AverageMeter()
scaler = amp.GradScaler() if mixed_precision else None
if valid_loader:
best_valid_err1 = 1.
best_valid_err5 = 1.
best_valid_loss = math.inf
sd = None
start_time = time.time()
while True:
if config.classes_per_batch > 0 and step > config.classes_per_batch_until_iter:
train_loader = get_dataloader(train_loader.dataset, config.batch_size, shuffle=True)
for x, y in train_loader:
if config.eval_continuous:
train_losses = continuous_train_step(x, y, step, model, optimizer, alpha, beta,
False, scheduler, device)
else:
train_losses = train_step(x, y, step, model, optimizer, scheduler, device,
mixed_precision=mixed_precision,
scaler=scaler)
if valid_loader and (step + 1) % valid_freq == 0:
end_time = time.time()
time_per_iter.update((end_time - start_time) / valid_freq, valid_freq)
loss_avg, top1_err, top5_err = test(model, valid_loader, device, mixed_precision=mixed_precision)
model.train()
if top1_err < best_valid_err1:
sd = move_model_state_dict(model.state_dict(), 'cpu')
best_valid_err1 = top1_err
best_valid_err5 = top5_err
best_valid_loss = loss_avg
pbar.set_description('[Step {}] tloss: {:.2f}, vloss: {:.2f}, err@1: {:.2f}%, err@5: {:.2f}%, latency: {:.3f}s'.\
format(step, train_losses[-1], loss_avg, top1_err*100, top5_err*100, time_per_iter.avg),
True
)
start_time = time.time()
step += 1
pbar.update(1)
if step == train_iters:
if valid_loader:
pbar.set_description('[Final] loss: {:.3f}, err@1: {:.2f}%, err@5: {:.2f}%, latency: {:.3f}s'.\
format(best_valid_loss, best_valid_err1*100, best_valid_err5*100, time_per_iter.avg),
True)
model.load_state_dict(move_model_state_dict(sd, device))
pbar.close()
return move_model_state_dict(model.state_dict(), 'cpu')
def train_parallel(model, optimizers, train_loader, train_iters,
schedulers, devices, valid_loader=None, valid_freq=100,
mixed_precision=False):
'''Parallel training of local loss blocks'''
model.train()
modules = model.moduleList
block_mapping = model.mapping_from_header_index
scaler = [amp.GradScaler() for _ in range(model.num_blocks)] if mixed_precision else None
time_per_iter = AverageMeter()
if valid_loader:
best_valid_err1 = 1.
best_valid_err5 = 1.
best_valid_loss = math.inf
sd = None
threads = []
#queues = [Queue(1)] + [Queue(QUEUE_SIZE) for i in range(1, model.num_blocks)] + [None] # last queue_out is None
queues = [Queue(QUEUE_SIZE) for i in range(1, model.num_blocks)] + [None] # last queue_out is None
#for ix, block_module_indices in enumerate(block_mapping):
initial_block = nn.Sequential(*[modules[module_index] for module_index in block_mapping[0]]).to(devices[0])
for ix, block_module_indices in enumerate(block_mapping[1:], start=1):
block = nn.Sequential(*[modules[module_index] for module_index in block_module_indices])
#threads.append(BlockThread(ix, block, optimizers[ix], schedulers[ix], queues[ix], queues[ix+1], devices[ix],
# train_iters, valid_freq, valid_loader is not None))
threads.append(BlockThread(ix, block, optimizers[ix], schedulers[ix], queues[ix-1], queues[ix], devices[ix],
train_iters, valid_freq, valid_loader is not None,
mixed_precision, scaler[ix] if scaler is not None else None))
start_time = time.time()
for thread in threads:
thread.start()
step = 0
pbar = tqdm(total=train_iters)
#feed_queue = queues[0]
while True:
if config.classes_per_batch > 0 and step > config.classes_per_batch_until_iter:
train_loader = get_dataloader(train_loader.dataset, config.batch_size, shuffle=True)
for data, target in train_loader:
#feed_queue.put((data, target))
block_train_step(initial_block, optimizers[0], data, target,
queue_out=queues[0], device=devices[0],
scheduler=schedulers[0],
mixed_precision=mixed_precision, scaler=scaler[0] if scaler is not None else None)
if valid_loader and (step + 1) % valid_freq == 0:
# wait for all threads to pause
while not all(thread.is_paused() for thread in threads):
time.sleep(0.01)
end_time = time.time()
time_per_iter.update((end_time - start_time) / valid_freq, valid_freq)
# all threads paused, run validation
loss_avg, top1_err, top5_err = test_multigpu(model, valid_loader, devices, mixed_precision=mixed_precision)
model.train()
if top1_err < best_valid_err1:
sd = move_model_state_dict(model.state_dict(), 'cpu')
best_valid_err1 = top1_err
best_valid_err5 = top5_err
best_valid_loss = loss_avg
pbar.set_description('[Step {}] vloss: {:.2f}, err@1: {:.2f}%, err@5: {:.2f}%, latency: {:.3f}s'.\
format(step, loss_avg, top1_err*100, top5_err*100, time_per_iter.avg),
True)
for thread in threads:
thread.resume()
start_time = time.time()
step += 1
pbar.update(1)
if step == train_iters:
for thread in threads:
thread.join()
model.to('cpu')
if valid_loader:
pbar.set_description('[Final] loss: {:.3f}, err@1: {:.2f}%, err@5: {:.2f}%, latency: {:.3f}s'.\
format(best_valid_loss, best_valid_err1*100, best_valid_err5*100, time_per_iter.avg),
True)
model.load_state_dict(sd)
pbar.close()
return model.state_dict()
def test(model, test_loader, device='cuda:0', n_batches=0, mixed_precision=False):
''' Evaluate model on test set '''
num_classes = DATASET_CONFIGS[config.dataset]['classes']
model.eval()
loss_fn = SmoothCrossEntropyLoss(config.smoothing)
top1 = AverageMeter()
top5 = AverageMeter()
loss = AverageMeter()
# Loop test set
for i, (data, target) in enumerate(test_loader):
if n_batches > 0 and i == n_batches:
break
if config.cuda:
data, target = data.to(device), target.to(device)
with ExitStack() as stack:
stack.enter_context(torch.no_grad())
if mixed_precision:
stack.enter_context(amp.autocast())
output = model(data)
test_loss = loss_fn(output, target)
loss.update(test_loss.float().item(), target.size(0))
top1_err, top5_err = get_error(output.float(), target, (1, 5))
top1.update(top1_err.item(), target.size(0))
top5.update(top5_err.item(), target.size(0))
return loss.avg, top1.avg, top5.avg
def test_ensemble(model, test_loader, device='cuda:0', n_batches=0, num_ensemble=1, mixed_precision=False):
num_classes = DATASET_CONFIGS[config.dataset]['classes']
model.eval()
loss_fn = SmoothCrossEntropyLoss(config.smoothing)
top1 = AverageMeter()
top5 = AverageMeter()
loss = AverageMeter()
# Loop test set
for i, (data, target) in enumerate(test_loader):
if n_batches > 0 and i == n_batches:
break
if config.cuda:
data, target = data.to(device), target.to(device)
with ExitStack() as stack:
stack.enter_context(torch.no_grad())
if mixed_precision:
stack.enter_context(amp.autocast())
output = model(data)
test_loss = loss_fn(output, target)
outputs = []
for module in reversed(model.moduleList):
if isinstance(module, LocalLossBlock):
out = module.decoder(module.act)
outputs.append(F.log_softmax(out, dim=-1))
if len(outputs) == num_ensemble:
break
output = torch.stack(outputs, dim=0).sum(dim=0)
top1_err, top5_err = get_error(output.float(), target, (1, 5))
top1.update(top1_err.item(), target.size(0))
top5.update(top5_err.item(), target.size(0))
return loss.avg, top1.avg, top5.avg
def test_multigpu(model, test_loader, devices, n_batches=0, mixed_precision=False):
''' Evaluate model on test set on multiple GPUs'''
num_classes = DATASET_CONFIGS[config.dataset]['classes']
model.eval()
loss_fn = SmoothCrossEntropyLoss(config.smoothing)
top1 = AverageMeter()
top5 = AverageMeter()
loss = AverageMeter()
device = devices[0]
# Loop test set
for i, (data, target) in enumerate(test_loader):
if n_batches > 0 and i == n_batches:
break
if config.cuda:
data, target = data.to(devices[0]), target.to(devices[-1])
with ExitStack() as stack:
stack.enter_context(torch.no_grad())
if mixed_precision:
stack.enter_context(amp.autocast())
# multi-gpu version
x = data
for ix, block_module_indices in enumerate(model.mapping_from_header_index):
x = x.to(devices[ix])
for module_index in block_module_indices:
if isinstance(model.moduleList[module_index], LocalLossBlock):
x = model.moduleList[module_index](x, save_act=False)
else:
x = model.moduleList[module_index](x)
output = model.moduleList[-1].decoder(x)
test_loss = loss_fn(output, target)
loss.update(test_loss.float().item(), target.size(0))
top1_err, top5_err = get_error(output.float(), target, (1, 5))
top1.update(top1_err.item(), target.size(0))
top5.update(top5_err.item(), target.size(0))
return loss.avg, top1.avg, top5.avg