Precision

class lightning.fabric.plugins.precision.Precision

Bases: object

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, *args, **kwargs)

Performs the actual backpropagation.

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

  • model (Optional[Module]) – The module that was involved in producing the tensor and whose parameters need the gradients

Return type:

None

convert_input(data)

Convert model inputs (forward) to the floating point precision type of this plugin.

This is a no-op in the base precision plugin, since we assume the data already has the desired type (default is torch.float32).

Return type:

Any

convert_module(module)

Convert the module parameters to the precision type this plugin handles.

This is optional and depends on the precision limitations during optimization.

Return type:

Module

convert_output(data)

Convert outputs to the floating point precision type expected after model’s forward.

This is a no-op in the base precision plugin, since we assume the data already has the desired type (default is torch.float32).

Return type:

Any

forward_context()

A contextmanager for managing model forward/training_step/evaluation_step/predict_step.

Return type:

AbstractContextManager

load_state_dict(state_dict)

Called when loading a checkpoint, implement to reload precision plugin state given precision plugin state_dict.

Parameters:

state_dict (dict[str, Any]) – the precision plugin state returned by state_dict.

Return type:

None

main_params(optimizer)

The main params of the model.

Returns the plain model params here. Maybe different in other precision plugins.

Return type:

Iterator[Parameter]

module_init_context()

Instantiate module parameters or tensors in the precision type this plugin handles.

This is optional and depends on the precision limitations during optimization.

Return type:

AbstractContextManager

optimizer_step(optimizer, **kwargs)

Hook to run the optimizer step.

Return type:

Any

post_backward(tensor, module)

Runs after precision plugin executes backward.

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

  • module (Optional[Module]) – The module that was involved in producing the tensor and whose parameters need the gradients

Return type:

Any

pre_backward(tensor, module)

Runs before precision plugin executes backward.

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

  • module (Optional[Module]) – The module that was involved in producing the tensor and whose parameters need the gradients

Return type:

Any

state_dict()

Called when saving a checkpoint, implement to generate precision plugin state_dict.

Return type:

dict[str, Any]

Returns:

A dictionary containing precision plugin state.

teardown()

This method is called to teardown the training process.

It is the right place to release memory and free other resources.

Return type:

None

tensor_init_context()

Controls how tensors get created (device, dtype).

Return type:

AbstractContextManager