---
title: MixedPrecision
description: Plugin for Automatic Mixed Precision (AMP) training with torch.autocast.
canonical: >-
  https://lightning.ai/docs/pytorch/stable/optional-api/api/lightning.pytorch.plugins.precision.MixedPrecision
---
# MixedPrecision {#mixedprecision}

### `class lightning.pytorch.plugins.precision.MixedPrecision(precision, device, scaler=None)` {#lightning.pytorch.plugins.precision.MixedPrecision}

Bases: [`Precision`](/docs/pytorch/2.6.6/optional-api/api/lightning.pytorch.plugins.precision.Precision.md#lightning.pytorch.plugins.precision.Precision "lightning.pytorch.plugins.precision.precision.Precision")

Plugin for Automatic Mixed Precision (AMP) training with `torch.autocast`.

Parameters:

*   **precision** ([`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'16-mixed'`, `'bf16-mixed'`\]) – Whether to use `torch.float16` (`'16-mixed'`) or `torch.bfloat16` (`'bf16-mixed'`).
    
*   **device** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)")) – The device for `torch.autocast`.
    
*   **scaler** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[`GradScaler`\]) – An optional [`torch.cuda.amp.GradScaler`](https://docs.pytorch.org/docs/stable/amp.html#torch.cuda.amp.GradScaler "(in PyTorch v2.15)") to use.
    

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

Clips the gradients.

Return type:

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

#### `forward_context()` {#lightning.pytorch.plugins.precision.MixedPrecision.forward_context}

Enable autocast context.

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)")\]

#### `load_state_dict(state_dict)` {#lightning.pytorch.plugins.precision.MixedPrecision.load_state_dict}

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

Parameters:

**state\_dict** ([`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict "(in Python v3.15)")\[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)"), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")\]) – the precision plugin state returned by `state_dict`.

Return type:

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

#### `optimizer_step(optimizer, model, closure, **kwargs)` {#lightning.pytorch.plugins.precision.MixedPrecision.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)")

#### `pre_backward(tensor, module)` {#lightning.pytorch.plugins.precision.MixedPrecision.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)")

#### `state_dict()` {#lightning.pytorch.plugins.precision.MixedPrecision.state_dict}

Called when saving a checkpoint, implement to generate precision plugin state\_dict.

Return type:

[`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict "(in Python v3.15)")\[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)"), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")\]

Returns:

A dictionary containing precision plugin state.

---

**Navigation**

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