Shortcuts

PrecisionPlugin

class pytorch_lightning.plugins.precision.PrecisionPlugin[source]

Bases: lightning_lite.plugins.precision.precision.Precision, pytorch_lightning.core.hooks.CheckpointHooks

Base class for all plugins handling the precision-specific parts of the training.

The class attribute precision must be overwritten in child classes. The default value reflects fp32 training.

backward(tensor, model, optimizer, optimizer_idx, *args, **kwargs)[source]

Performs the actual backpropagation.

Parameters:
  • tensor (Tensor) – the loss value obtained from the closure

  • model (LightningModule) – the model to be optimized

  • optimizer (Optional[Steppable]) – current optimizer being used. None if using manual optimization

  • optimizer_idx (Optional[int]) – the index of the current optimizer. None if using manual optimization

  • *args (Any) – Positional arguments intended for the actual function that performs the backward, like backward().

  • **kwargs (Any) – Keyword arguments for the same purpose as *args.

Return type:

None

clip_grad_by_norm(optimizer, clip_val)[source]

Clip gradients by norm.

Return type:

None

clip_grad_by_value(optimizer, clip_val)[source]

Clip gradients by value.

Return type:

None

clip_gradients(optimizer, clip_val=0.0, gradient_clip_algorithm=GradClipAlgorithmType.NORM)[source]

Clips the gradients.

Return type:

None

connect(model, optimizers, lr_schedulers)[source]

Connects this plugin to the accelerator and the training process.

Return type:

Tuple[Module, List[Optimizer], List[Any]]

dispatch(trainer)[source]

Hook to do something when Strategy.dispatch() gets called.

Return type:

None

optimizer_step(optimizer, model, optimizer_idx, closure, **kwargs)[source]

Hook to run the optimizer step.

Return type:

Any

post_backward(tensor, module)[source]

Runs after precision plugin executes backward.

Parameters:
  • tensor (Tensor) – The tensor that will be used for backpropagation

  • module (LightningModule) – The module that was involved in producing the tensor and whose parameters need the gradients

Return type:

Tensor

pre_backward(tensor, module)[source]

Runs before precision plugin executes backward.

Parameters:
  • tensor (Tensor) – The tensor that will be used for backpropagation

  • module (LightningModule) – The module that was involved in producing the tensor and whose parameters need the gradients

Return type:

Tensor

predict_step_context()[source]

A contextmanager for the predict step.

Return type:

Generator[None, None, None]

test_step_context()[source]

A contextmanager for the test step.

Return type:

Generator[None, None, None]

train_step_context()[source]

A contextmanager for the training step.

Return type:

Generator[None, None, None]

val_step_context()[source]

A contextmanager for the validation step.

Return type:

Generator[None, None, None]