Lightning-AI/pytorch-lightning · error · MisconfigurationException
`precision='bf16-mixed'` does not use a scaler, found {scale
Error message
`precision='bf16-mixed'` does not use a scaler, found {scaler}. What it means
MixedPrecisionPlugin (the AMP precision plugin) was constructed with a GradScaler while precision is 'bf16-mixed'. Bfloat16 training has a much wider dynamic range than fp16, so loss scaling is unnecessary, and Lightning rejects the scaler to prevent silently mis-scaled gradients. Pass scaler=None (or omit it) when using bf16.
Source
Thrown at src/lightning/pytorch/plugins/precision/amp.py:75
"""
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)
@override
def optimizer_step( # type: ignore[override]
self,
optimizer: Optimizable,
model: "pl.LightningModule",
closure: Callable[[], Any],
**kwargs: Any,
) -> Any:
if self.scaler is None:View on GitHub (pinned to 9fed5c27d2)
Solutions
- Remove the scaler argument when using precision='bf16-mixed'
- If you need a scaler, switch precision to '16-mixed'
- Build the scaler conditionally: only create one when precision == '16-mixed'
Example fix
# before
plugin = MixedPrecisionPlugin(precision='bf16-mixed', scaler=torch.amp.GradScaler('cuda'))
# after
plugin = MixedPrecisionPlugin(precision='bf16-mixed', scaler=None) Defensive patterns
Strategy: validation
Validate before calling
from lightning.pytorch.plugins import MixedPrecisionPlugin
def make_plugin(precision, scaler=None):
if precision == '16-mixed' and scaler is None:
scaler = torch.amp.GradScaler('cuda')
if precision != '16-mixed':
scaler = None # bf16/fp32 never take a scaler
return MixedPrecisionPlugin(precision=precision, scaler=scaler) Type guard
def is_scaler_compatible_precision(precision: str) -> bool:
return precision == '16-mixed' Prevention
- Never hardcode a GradScaler in shared configs; derive it from the precision string
- Treat 'bf16-mixed' as scaler-free by design, not as an error to work around
When it happens
Trigger: Instantiating MixedPrecisionPlugin(precision='bf16-mixed', scaler=torch.amp.GradScaler(...)) or a custom plugin subclass; also when reusing an fp16 ('16-mixed') plugin config after switching Trainer(precision='bf16-mixed').
Common situations: Migrating a working 16-mixed AMP setup to bf16-mixed without removing the scaler; copy-pasted plugin configs from older Lightning versions (<2.0) where scaler+bf16 was tolerated; programmatically swapping precision strings while keeping a scaler object.
Related errors
- `precision='bf16-mixed'` does not use a scaler, found {scale
- `Passed `{type(self).__name__}(precision={precision!r})`. Pr
- AMP and the LBFGS optimizer are not compatible.
- The current optimizer, {type(optimizer).__qualname__}, does
- `precision={precision!r}` does not use a scaler, found {scal
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/36f50db3ba8bbb8e.
Report an issue: GitHub.