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

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

Bases: [`Precision`](/docs/fabric/2.6.6/full-api-reference/api/generated/lightning.fabric.plugins.precision.Precision.md#lightning.fabric.plugins.precision.Precision "lightning.fabric.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.
    

#### `backward(tensor, model, *args, **kwargs)` {#lightning.fabric.plugins.precision.MixedPrecision.backward}

Performs the actual backpropagation.

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
    
*   **model** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`Module`](https://docs.pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module "(in PyTorch v2.15)")\]) – The module that was involved in producing the tensor and whose parameters need the gradients
    

Return type:

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

#### `convert_input(data)` {#lightning.fabric.plugins.precision.MixedPrecision.convert_input}

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`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")

#### `convert_output(data)` {#lightning.fabric.plugins.precision.MixedPrecision.convert_output}

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`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")

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

A contextmanager for managing model forward/training\_step/evaluation\_step/predict\_step.

Return type:

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

#### `load_state_dict(state_dict)` {#lightning.fabric.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, **kwargs)` {#lightning.fabric.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)")

#### `state_dict()` {#lightning.fabric.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: [Generated](/docs/fabric/2.6.6/full-api-reference/api/generated.md)
