2020-10-22 19:58:05 +00:00
|
|
|
from torch.cuda.amp import GradScaler, autocast
|
|
|
|
|
2020-08-22 14:24:34 +00:00
|
|
|
from utils.loss_accumulator import LossAccumulator
|
|
|
|
from torch.nn import Module
|
|
|
|
import logging
|
2020-09-29 04:09:42 +00:00
|
|
|
from models.steps.losses import create_loss
|
2020-08-22 14:24:34 +00:00
|
|
|
import torch
|
|
|
|
from collections import OrderedDict
|
|
|
|
from .injectors import create_injector
|
2020-09-20 03:47:34 +00:00
|
|
|
from utils.util import recursively_detach
|
2020-08-12 14:45:23 +00:00
|
|
|
|
2020-08-22 14:24:34 +00:00
|
|
|
logger = logging.getLogger('base')
|
2020-08-12 14:45:23 +00:00
|
|
|
|
|
|
|
|
2020-08-22 14:24:34 +00:00
|
|
|
# Defines the expected API for a single training step
|
|
|
|
class ConfigurableStep(Module):
|
|
|
|
|
|
|
|
def __init__(self, opt_step, env):
|
|
|
|
super(ConfigurableStep, self).__init__()
|
|
|
|
|
|
|
|
self.step_opt = opt_step
|
|
|
|
self.env = env
|
|
|
|
self.opt = env['opt']
|
|
|
|
self.gen_outputs = opt_step['generator_outputs']
|
|
|
|
self.loss_accumulator = LossAccumulator()
|
2020-08-23 23:22:34 +00:00
|
|
|
self.optimizers = None
|
2020-10-22 19:58:05 +00:00
|
|
|
self.scaler = GradScaler(enabled=self.opt['fp16'])
|
2020-10-22 20:39:19 +00:00
|
|
|
self.grads_generated = False
|
2020-08-22 14:24:34 +00:00
|
|
|
|
|
|
|
self.injectors = []
|
|
|
|
if 'injectors' in self.step_opt.keys():
|
2020-09-29 22:59:44 +00:00
|
|
|
injector_names = []
|
2020-08-22 14:24:34 +00:00
|
|
|
for inj_name, injector in self.step_opt['injectors'].items():
|
2020-09-29 22:59:44 +00:00
|
|
|
assert inj_name not in injector_names # Repeated names are always an error case.
|
|
|
|
injector_names.append(inj_name)
|
2020-08-22 14:24:34 +00:00
|
|
|
self.injectors.append(create_injector(injector, env))
|
|
|
|
|
|
|
|
losses = []
|
|
|
|
self.weights = {}
|
2020-08-31 15:41:48 +00:00
|
|
|
if 'losses' in self.step_opt.keys():
|
|
|
|
for loss_name, loss in self.step_opt['losses'].items():
|
2020-09-29 22:59:44 +00:00
|
|
|
assert loss_name not in self.weights.keys() # Repeated names are always an error case.
|
2020-09-29 04:09:42 +00:00
|
|
|
losses.append((loss_name, create_loss(loss, env)))
|
2020-08-31 15:41:48 +00:00
|
|
|
self.weights[loss_name] = loss['weight']
|
2020-08-22 14:24:34 +00:00
|
|
|
self.losses = OrderedDict(losses)
|
|
|
|
|
2020-09-12 04:57:06 +00:00
|
|
|
def get_network_for_name(self, name):
|
|
|
|
return self.env['generators'][name] if name in self.env['generators'].keys() \
|
|
|
|
else self.env['discriminators'][name]
|
|
|
|
|
2020-08-22 14:24:34 +00:00
|
|
|
# Subclasses should override this to define individual optimizers. They should all go into self.optimizers.
|
|
|
|
# This default implementation defines a single optimizer for all Generator parameters.
|
2020-08-23 23:22:34 +00:00
|
|
|
# Must be called after networks are initialized and wrapped.
|
2020-08-22 14:24:34 +00:00
|
|
|
def define_optimizers(self):
|
2020-09-12 04:57:06 +00:00
|
|
|
training = self.step_opt['training']
|
|
|
|
if isinstance(training, list):
|
|
|
|
self.training_net = [self.get_network_for_name(t) for t in training]
|
|
|
|
opt_configs = [self.step_opt['optimizer_params'][t] for t in training]
|
|
|
|
nets = self.training_net
|
|
|
|
else:
|
|
|
|
self.training_net = self.get_network_for_name(training)
|
|
|
|
# When only training one network, optimizer params can just embedded in the step params.
|
|
|
|
if 'optimizer_params' not in self.step_opt.keys():
|
|
|
|
opt_configs = [self.step_opt]
|
2020-08-22 14:24:34 +00:00
|
|
|
else:
|
2020-09-12 04:57:06 +00:00
|
|
|
opt_configs = [self.step_opt['optimizer_params']]
|
|
|
|
nets = [self.training_net]
|
|
|
|
self.optimizers = []
|
|
|
|
for net, opt_config in zip(nets, opt_configs):
|
|
|
|
optim_params = []
|
|
|
|
for k, v in net.named_parameters(): # can optimize for a part of the model
|
|
|
|
if v.requires_grad:
|
|
|
|
optim_params.append(v)
|
|
|
|
else:
|
|
|
|
if self.env['rank'] <= 0:
|
|
|
|
logger.warning('Params [{:s}] will not optimize.'.format(k))
|
|
|
|
|
|
|
|
if 'optimizer' not in self.step_opt.keys() or self.step_opt['optimizer'] == 'adam':
|
|
|
|
opt = torch.optim.Adam(optim_params, lr=opt_config['lr'],
|
|
|
|
weight_decay=opt_config['weight_decay'],
|
|
|
|
betas=(opt_config['beta1'], opt_config['beta2']))
|
|
|
|
elif self.step_opt['optimizer'] == 'novograd':
|
|
|
|
opt = NovoGrad(optim_params, lr=opt_config['lr'], weight_decay=opt_config['weight_decay'],
|
|
|
|
betas=(opt_config['beta1'], opt_config['beta2']))
|
2020-09-22 23:03:22 +00:00
|
|
|
opt._config = opt_config # This is a bit seedy, but we will need these configs later.
|
2020-09-12 04:57:06 +00:00
|
|
|
self.optimizers.append(opt)
|
2020-08-12 14:45:23 +00:00
|
|
|
|
|
|
|
# Returns all optimizers used in this step.
|
|
|
|
def get_optimizers(self):
|
2020-08-22 14:24:34 +00:00
|
|
|
assert self.optimizers is not None
|
|
|
|
return self.optimizers
|
2020-08-12 14:45:23 +00:00
|
|
|
|
|
|
|
# Returns optimizers which are opting in for default LR scheduling.
|
|
|
|
def get_optimizers_with_default_scheduler(self):
|
2020-08-22 14:24:34 +00:00
|
|
|
assert self.optimizers is not None
|
|
|
|
return self.optimizers
|
2020-08-12 14:45:23 +00:00
|
|
|
|
|
|
|
# Returns the names of the networks this step will train. Other networks will be frozen.
|
|
|
|
def get_networks_trained(self):
|
2020-09-12 04:57:06 +00:00
|
|
|
if isinstance(self.step_opt['training'], list):
|
|
|
|
return self.step_opt['training']
|
|
|
|
else:
|
|
|
|
return [self.step_opt['training']]
|
2020-08-22 14:24:34 +00:00
|
|
|
|
2020-09-29 04:09:42 +00:00
|
|
|
def get_training_network_name(self):
|
|
|
|
if isinstance(self.step_opt['training'], list):
|
|
|
|
return self.step_opt['training'][0]
|
|
|
|
else:
|
|
|
|
return self.step_opt['training']
|
|
|
|
|
2020-08-22 14:24:34 +00:00
|
|
|
# Performs all forward and backward passes for this step given an input state. All input states are lists of
|
|
|
|
# chunked tensors. Use grad_accum_step to dereference these steps. Should return a dict of tensors that later
|
|
|
|
# steps might use. These tensors are automatically detached and accumulated into chunks.
|
2020-09-05 13:59:45 +00:00
|
|
|
def do_forward_backward(self, state, grad_accum_step, amp_loss_id, train=True):
|
2020-08-22 14:24:34 +00:00
|
|
|
new_state = {}
|
2020-08-12 14:45:23 +00:00
|
|
|
|
2020-08-22 14:24:34 +00:00
|
|
|
# Prepare a de-chunked state dict which will be used for the injectors & losses.
|
|
|
|
local_state = {}
|
|
|
|
for k, v in state.items():
|
|
|
|
local_state[k] = v[grad_accum_step]
|
|
|
|
local_state.update(new_state)
|
2020-09-18 03:05:32 +00:00
|
|
|
local_state['train_nets'] = str(self.get_networks_trained())
|
2020-08-12 14:45:23 +00:00
|
|
|
|
2020-10-22 19:58:05 +00:00
|
|
|
# Some losses compute backward() internally. Accommodate this by stashing the amp_loss_id in env.
|
2020-09-25 22:38:23 +00:00
|
|
|
self.env['amp_loss_id'] = amp_loss_id
|
|
|
|
self.env['current_step_optimizers'] = self.optimizers
|
2020-09-27 03:25:32 +00:00
|
|
|
self.env['training'] = train
|
2020-09-25 22:38:23 +00:00
|
|
|
|
2020-08-22 14:24:34 +00:00
|
|
|
# Inject in any extra dependencies.
|
2020-10-22 20:39:19 +00:00
|
|
|
for inj in self.injectors:
|
|
|
|
# Don't do injections tagged with eval unless we are not in train mode.
|
|
|
|
if train and 'eval' in inj.opt.keys() and inj.opt['eval']:
|
|
|
|
continue
|
|
|
|
# Likewise, don't do injections tagged with train unless we are not in eval.
|
|
|
|
if not train and 'train' in inj.opt.keys() and inj.opt['train']:
|
|
|
|
continue
|
|
|
|
# Don't do injections tagged with 'after' or 'before' when we are out of spec.
|
|
|
|
if 'after' in inj.opt.keys() and self.env['step'] < inj.opt['after'] or \
|
|
|
|
'before' in inj.opt.keys() and self.env['step'] > inj.opt['before']:
|
|
|
|
continue
|
|
|
|
injected = inj(local_state)
|
|
|
|
local_state.update(injected)
|
|
|
|
new_state.update(injected)
|
2020-08-22 14:24:34 +00:00
|
|
|
|
2020-09-09 02:36:27 +00:00
|
|
|
if train and len(self.losses) > 0:
|
2020-08-22 19:08:33 +00:00
|
|
|
# Finally, compute the losses.
|
|
|
|
total_loss = 0
|
|
|
|
for loss_name, loss in self.losses.items():
|
2020-09-09 02:36:27 +00:00
|
|
|
# Some losses only activate after a set number of steps. For example, proto-discriminator losses can
|
|
|
|
# be very disruptive to a generator.
|
|
|
|
if 'after' in loss.opt.keys() and loss.opt['after'] > self.env['step']:
|
|
|
|
continue
|
2020-08-22 19:08:33 +00:00
|
|
|
l = loss(self.training_net, local_state)
|
|
|
|
total_loss += l * self.weights[loss_name]
|
2020-08-23 23:22:34 +00:00
|
|
|
# Record metrics.
|
2020-10-03 17:03:55 +00:00
|
|
|
if isinstance(l, torch.Tensor):
|
|
|
|
self.loss_accumulator.add_loss(loss_name, l)
|
2020-08-23 23:22:34 +00:00
|
|
|
for n, v in loss.extra_metrics():
|
|
|
|
self.loss_accumulator.add_loss("%s_%s" % (loss_name, n), v)
|
2020-10-13 16:07:49 +00:00
|
|
|
loss.clear_metrics()
|
2020-10-03 03:06:10 +00:00
|
|
|
|
2020-10-03 02:54:06 +00:00
|
|
|
# In some cases, the loss could not be set (e.g. all losses have 'after'
|
2020-10-03 03:06:10 +00:00
|
|
|
if isinstance(total_loss, torch.Tensor):
|
2020-10-03 02:55:33 +00:00
|
|
|
self.loss_accumulator.add_loss("%s_total" % (self.get_training_network_name(),), total_loss)
|
|
|
|
# Scale the loss down by the accumulation factor.
|
|
|
|
total_loss = total_loss / self.env['mega_batch_factor']
|
|
|
|
|
2020-10-03 02:54:06 +00:00
|
|
|
# Get dem grads!
|
2020-10-22 20:39:19 +00:00
|
|
|
self.scaler.scale(total_loss).backward()
|
|
|
|
self.grads_generated = True
|
2020-08-22 14:24:34 +00:00
|
|
|
|
2020-08-23 23:22:34 +00:00
|
|
|
# Detach all state variables. Within the step, gradients can flow. Once these variables leave the step
|
|
|
|
# we must release the gradients.
|
2020-09-20 03:47:34 +00:00
|
|
|
new_state = recursively_detach(new_state)
|
2020-08-22 14:24:34 +00:00
|
|
|
return new_state
|
|
|
|
|
|
|
|
# Performs the optimizer step after all gradient accumulation is completed. Default implementation simply steps()
|
|
|
|
# all self.optimizers.
|
2020-08-12 14:45:23 +00:00
|
|
|
def do_step(self):
|
2020-10-22 20:39:19 +00:00
|
|
|
if not self.grads_generated:
|
|
|
|
return
|
|
|
|
self.grads_generated = False
|
2020-08-22 14:24:34 +00:00
|
|
|
for opt in self.optimizers:
|
2020-09-22 23:03:22 +00:00
|
|
|
# Optimizers can be opted out in the early stages of training.
|
|
|
|
after = opt._config['after'] if 'after' in opt._config.keys() else 0
|
|
|
|
if self.env['step'] < after:
|
|
|
|
continue
|
|
|
|
before = opt._config['before'] if 'before' in opt._config.keys() else -1
|
|
|
|
if before != -1 and self.env['step'] > before:
|
|
|
|
continue
|
2020-10-22 19:58:05 +00:00
|
|
|
self.scaler.step(opt)
|
|
|
|
self.scaler.update()
|
2020-08-22 14:24:34 +00:00
|
|
|
|
|
|
|
def get_metrics(self):
|
2020-08-22 19:08:33 +00:00
|
|
|
return self.loss_accumulator.as_dict()
|