---
title: Precision
description: >-
  Base class for all plugins handling the precision-specific parts of the
  training.
canonical: >-
  https://lightning.ai/docs/pytorch/stable/optional-api/api/lightning.pytorch.plugins.precision.Precision
---
# Precision {#precision}

### `class lightning.pytorch.plugins.precision.Precision` {#lightning.pytorch.plugins.precision.Precision}

Bases: [`Precision`](/docs/fabric/stable/api/generated/lightning.fabric.plugins.precision.Precision#lightning.fabric.plugins.precision.Precision "(in lightning v2.7.0.dev)"), [`CheckpointHooks`](/docs/pytorch/2.6.6/optional-api/api/lightning.pytorch.core.hooks.CheckpointHooks.md#lightning.pytorch.core.hooks.CheckpointHooks "lightning.pytorch.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, *args, **kwargs)` {#lightning.pytorch.plugins.precision.Precision.backward}

Performs the actual backpropagation.

Parameters:

*   **tensor** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – the loss value obtained from the closure
    
*   **model** ([`LightningModule`](/docs/pytorch/2.6.6/optional-api/api/lightning.pytorch.core.LightningModule.md#lightning.pytorch.core.LightningModule "lightning.pytorch.core.module.LightningModule")) – the model to be optimized
    
*   **optimizer** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[`Steppable`\]) – current optimizer being used. `None` if using manual optimization
    
*   **\*args** ([`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")) – Positional arguments intended for the actual function that performs the backward, like [`backward()`](https://docs.pytorch.org/docs/stable/generated/torch.Tensor.backward.html#torch.Tensor.backward "(in PyTorch v2.15)").
    
*   **\*\*kwargs** ([`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")) – Keyword arguments for the same purpose as `*args`.
    

Return type:

[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")

#### `clip_grad_by_norm(optimizer, clip_val)` {#lightning.pytorch.plugins.precision.Precision.clip_grad_by_norm}

Clip gradients by norm.

Return type:

[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")

#### `clip_grad_by_value(optimizer, clip_val)` {#lightning.pytorch.plugins.precision.Precision.clip_grad_by_value}

Clip gradients by value.

Return type:

[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")

#### `clip_gradients(optimizer, clip_val=0.0, gradient_clip_algorithm=GradClipAlgorithmType.NORM)` {#lightning.pytorch.plugins.precision.Precision.clip_gradients}

Clips the gradients.

Return type:

[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")

#### `connect(model, optimizers, lr_schedulers)` {#lightning.pytorch.plugins.precision.Precision.connect}

Connects this plugin to the accelerator and the training process.

Return type:

[`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple "(in Python v3.15)")\[[`Module`](https://docs.pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module "(in PyTorch v2.15)"), [`list`](https://docs.python.org/3/builtins/stdtypes.html#list "(in Python v3.15)")\[[`Optimizer`](https://docs.pytorch.org/docs/stable/optim.html#torch.optim.Optimizer "(in PyTorch v2.15)")\], [`list`](https://docs.python.org/3/builtins/stdtypes.html#list "(in Python v3.15)")\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")\]\]

#### `optimizer_step(optimizer, model, closure, **kwargs)` {#lightning.pytorch.plugins.precision.Precision.optimizer_step}

Hook to run the optimizer step.

Return type:

[`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")

#### `post_backward(tensor, module)` {#lightning.pytorch.plugins.precision.Precision.post_backward}

Runs after precision plugin executes backward.

Parameters:

*   **tensor** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – The tensor that will be used for backpropagation
    
*   **module** ([`LightningModule`](/docs/pytorch/2.6.6/optional-api/api/lightning.pytorch.core.LightningModule.md#lightning.pytorch.core.LightningModule "lightning.pytorch.core.module.LightningModule")) – The module that was involved in producing the tensor and whose parameters need the gradients
    

Return type:

[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")

#### `pre_backward(tensor, module)` {#lightning.pytorch.plugins.precision.Precision.pre_backward}

Runs before precision plugin executes backward.

Parameters:

*   **tensor** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – The tensor that will be used for backpropagation
    
*   **module** ([`LightningModule`](/docs/pytorch/2.6.6/optional-api/api/lightning.pytorch.core.LightningModule.md#lightning.pytorch.core.LightningModule "lightning.pytorch.core.module.LightningModule")) – The module that was involved in producing the tensor and whose parameters need the gradients
    

Return type:

[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")

#### `predict_step_context()` {#lightning.pytorch.plugins.precision.Precision.predict_step_context}

A contextmanager for the predict step.

Return type:

[`Generator`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Generator "(in Python v3.15)")\[[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)"), [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)"), [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")\]

#### `test_step_context()` {#lightning.pytorch.plugins.precision.Precision.test_step_context}

A contextmanager for the test step.

Return type:

[`Generator`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Generator "(in Python v3.15)")\[[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)"), [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)"), [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")\]

#### `train_step_context()` {#lightning.pytorch.plugins.precision.Precision.train_step_context}

A contextmanager for the training step.

Return type:

[`Generator`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Generator "(in Python v3.15)")\[[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)"), [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)"), [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")\]

#### `val_step_context()` {#lightning.pytorch.plugins.precision.Precision.val_step_context}

A contextmanager for the validation step.

Return type:

[`Generator`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Generator "(in Python v3.15)")\[[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)"), [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)"), [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")\]

---

**Navigation**

- Parent: [Api](/docs/pytorch/2.6.6/optional-api/api.md)
