Lightning-AI/pytorch-lightning · error · ValueError
`Passed `{type(self).__name__}(precision={precision!r})`. Pr
Error message
`Passed `{type(self).__name__}(precision={precision!r})`. Precision must be '16-mixed' or 'bf16-mixed'. What it means
Raised by the MixedPrecisionPlugin constructor when the precision argument is anything other than the literals '16-mixed' or 'bf16-mixed'. The AMP plugin only wraps these two mixed-precision modes; full 32-bit or true 16-bit training use different precision plugin classes.
Source
Thrown at src/lightning/pytorch/plugins/precision/amp.py:66
class MixedPrecision(Precision):
"""Plugin for Automatic Mixed Precision (AMP) training with ``torch.autocast``.
Args:
precision: Whether to use ``torch.float16`` (``'16-mixed'``) or ``torch.bfloat16`` (``'bf16-mixed'``).
device: The device for ``torch.autocast``.
scaler: An optional :class:`torch.cuda.amp.GradScaler` to use.
"""
def __init__(
self,
precision: Literal["16-mixed", "bf16-mixed"],
device: str,
scaler: Optional["torch.amp.GradScaler"] = None,
) -> None:
if precision not in ("16-mixed", "bf16-mixed"):
raise ValueError(
f"`Passed `{type(self).__name__}(precision={precision!r})`."
f" Precision must be '16-mixed' or 'bf16-mixed'."
)
self.precision = precision
if scaler is None and self.precision == "16-mixed":
scaler = torch.amp.GradScaler(device=device)
if scaler is not None and self.precision == "bf16-mixed":
raise MisconfigurationException(f"`precision='bf16-mixed'` does not use a scaler, found {scaler}.")
self.device = device
self.scaler = scaler
@override
def pre_backward(self, tensor: Tensor, module: "pl.LightningModule") -> Tensor: # type: ignore[override]
if self.scaler is not None:
tensor = self.scaler.scale(tensor)
return super().pre_backward(tensor, module)
View on GitHub (pinned to 9fed5c27d2)
Solutions
- Use the new string literals: '16-mixed' or 'bf16-mixed'
- Update Trainer calls: `Trainer(precision='16-mixed')` instead of `precision=16`
- If you need a different precision scheme, use the corresponding plugin class (e.g. DoublePrecisionPlugin, Precision)
Example fix
# before (Lightning 1.x style) trainer = pl.Trainer(precision=16) # or plugin = MixedPrecisionPlugin(precision=16, device='cuda') # after trainer = pl.Trainer(precision='16-mixed') plugin = MixedPrecisionPlugin(precision='16-mixed', device='cuda')
Defensive patterns
Strategy: type-guard
Validate before calling
VALID = ('16-mixed', 'bf16-mixed')
precision = trainer_config.get('precision', '32-true')
if precision in VALID:
plugin = MixedPrecisionPlugin(precision=precision, device='cuda')
# else use default Precision plugin for '32-true' etc. Type guard
def is_mixed_precision_literal(p) -> bool:
return p in ('16-mixed', 'bf16-mixed') Prevention
- Migrate legacy precision=16/'bf16' values to the '16-mixed'/'bf16-mixed' literals on upgrade to Lightning 2.x
- Validate the precision field in config schemas against the allowed enum
When it happens
Trigger: Instantiating amp plugins with legacy values like precision=16 or precision='bf16' (pre-2.0 naming); passing '32-true', 16, or 'fp16' to MixedPrecisionPlugin; configs migrated from Lightning 1.x using integer precision.
Common situations: Upgrading from Lightning 1.x where `Trainer(precision=16)` was valid; writing custom plugins that hardcode old precision strings; YAML configs with stale precision values.
Related errors
- Passed `{type(self).__name__}(precision={precision!r})`. Pre
- `precision='bf16-mixed'` does not use a scaler, found {scale
- `precision='bf16-mixed'` does not use a scaler, found {scale
- AMP and the LBFGS optimizer are not compatible.
- Gradient clipping is not implemented for optimizers handling
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/a4dc517a6e9a1c1c.
Report an issue: GitHub.