pytorch_pfn_extras.handler.Logic#
- class pytorch_pfn_extras.handler.Logic(model_name='main', options=None)#
Bases:
BaseLogic
A set of methods that defines the training logic.
- Parameters:
model_name (str) – Name of the model. Default is
'main'
.options (dict, optional) –
The configuration options.
'backward_outputs'
(list of str):A list of names of outputs that require compution of the gradient.
'autocast'
(bool or dict):If
True
,torch.autocast
is enabled, using{"enabled": True, "device_type": "cuda"}
as autocast options. The default isFalse
which corresponds to the following options{"enabled": False, "device_type": "cuda"}
. If dict, options are passed totorch.autocast
.
'grad_scaler'
(torch.cuda.amp.GradScaler):A gradient scaler that outputs are applied to.
Methods
__init__
([model_name, options])A set of methods that defines the training logic.
consume_options
(options)A method to update options of Logic.
eval_step
(models, batch_idx, batch)A method for an evaluation step.
train_epoch_begin
(models, epoch, loader)A method called when starting a new epoch of training.
train_epoch_end
(models, epoch)A method called when completing an epoch of training.
train_step
(models, optimizers, batch_idx, batch)A method invokes the model forward and backward passes.
train_step_optimizers
(models, optimizers, ...)A method in charge of stepping the provided optimizers.
train_validation_begin
(models)A method called when starting a validation.
train_validation_end
(models)A method called when the validation completes.
- __init__(model_name='main', options=None)#
A set of methods that defines the training logic.
- Parameters:
model_name (str) – Name of the model. Default is
'main'
.options (dict, optional) –
The configuration options.
'backward_outputs'
(list of str):A list of names of outputs that require compution of the gradient.
'autocast'
(bool or dict):If
True
,torch.autocast
is enabled, using{"enabled": True, "device_type": "cuda"}
as autocast options. The default isFalse
which corresponds to the following options{"enabled": False, "device_type": "cuda"}
. If dict, options are passed totorch.autocast
.
'grad_scaler'
(torch.cuda.amp.GradScaler):A gradient scaler that outputs are applied to.
- Return type:
None
- consume_options(options)#
A method to update options of Logic.
Note that the given dict will be modified.
- Parameters:
options (dict) – Option key-values to be set.
- Return type:
None
- eval_step(models, batch_idx, batch)#
A method for an evaluation step.
- Parameters:
models (dict of torch.nn.Module) – The models.
batch_idx (int) – Number of steps already finished.
batch (torch.Tensor, list of torch.Tensor, dict of torch.Tensor) – Input tensors feeded to the model of the current step.
- Return type:
Any
- train_epoch_begin(models, epoch, loader)#
A method called when starting a new epoch of training.
- Parameters:
epoch (int) – Number of epochs already finished.
models (dict of torch.nn.Module) – The models.
loader (torch.utils.data.DataLoader) – The data loder.
- Return type:
None
- train_epoch_end(models, epoch)#
A method called when completing an epoch of training.
- Parameters:
epoch (int) – Number of epochs already finished.
models (dict of torch.nn.Module) – The models.
- Return type:
None
- train_step(models, optimizers, batch_idx, batch)#
A method invokes the model forward and backward passes.
Optimizing is left to train_step_optimizers since maybe the user would like to aggregate the gradients of several iterations.
- Parameters:
models (dict of torch.nn.Module) – The models.
optimizers (dict of torch.optim.Optimizer) – The optimizers.
batch_idx (int) – Number of training steps already finished.
batch (torch.Tensor, list of torch.Tensor, dict of torch.Tensor) – Input tensors feeded to the model of the current step.
- Return type:
Any
- train_step_optimizers(models, optimizers, batch_idx)#
A method in charge of stepping the provided optimizers.
Also a grad scaler will be used if defined.
- Parameters:
optimizers (dict of torch.optim.Optimizer) – The optimizers.
batch_idx (int) – Number of steps already finished.
models (Mapping[str, Module]) –
- Return type:
None
- train_validation_begin(models)#
A method called when starting a validation.
- Parameters:
models (dict of torch.nn.Module) – The models.
- Return type:
None
- train_validation_end(models)#
A method called when the validation completes.
- Parameters:
models (dict of torch.nn.Module) – The models.
- Return type:
None