Lightning-AI/pytorch-lightning · error · ValueError
`precision={precision!r}` does not use a scaler, found {scal
Error message
`precision={precision!r}` does not use a scaler, found {scaler}. What it means
FSDPMixedPrecisionPlugin received an explicit ShardedGradScaler while precision is anything other than '16-mixed'. Only fp16 mixed precision needs loss scaling; bf16/fp32 modes must be constructed without a scaler, so the plugin raises to prevent a mis-scaled FSDP run.
Source
Thrown at src/lightning/pytorch/plugins/precision/fsdp.py:64
Raises:
ValueError:
If unsupported ``precision`` is provided.
"""
def __init__(self, precision: _PRECISION_INPUT, scaler: Optional["ShardedGradScaler"] = None) -> None:
supported_precision = get_args(_PRECISION_INPUT)
if precision not in supported_precision:
raise ValueError(
f"`precision={precision!r})` is not supported in FSDP."
f" `precision` must be one of: {supported_precision}."
)
from torch.distributed.fsdp.sharded_grad_scaler import ShardedGradScaler
if scaler is not None and self.precision != "16-mixed":
raise ValueError(f"`precision={precision!r}` does not use a scaler, found {scaler}.")
self.scaler = ShardedGradScaler() if scaler is None and precision == "16-mixed" else None
self.precision = precision
precision_to_type = {
"bf16-mixed": torch.float32,
"16-mixed": torch.float32,
"bf16-true": torch.bfloat16,
"16-true": torch.float16,
"32-true": torch.float32,
}
self._desired_input_dtype = precision_to_type[self.precision]
@override
def convert_module(self, module: Module) -> Module:
if "true" in self.precision:
return module.to(dtype=self._desired_input_dtype)
return moduleView on GitHub (pinned to 9fed5c27d2)
Solutions
- Pass scaler=None (or omit it) when precision is not '16-mixed'
- Keep the scaler only for precision='16-mixed'
- Construct the scaler conditionally based on the precision string
Example fix
# before plugin = FSDPMixedPrecisionPlugin(precision='bf16-mixed', scaler=ShardedGradScaler()) # after plugin = FSDPMixedPrecisionPlugin(precision='bf16-mixed', scaler=None)
Defensive patterns
Strategy: validation
Validate before calling
def make_fsdp_plugin(precision, scaler=None):
if precision != '16-mixed':
scaler = None # only fp16 mixed precision uses ShardedGradScaler
return FSDPMixedPrecisionPlugin(precision=precision, scaler=scaler) Type guard
def scaler_allowed_for(precision: str) -> bool:
return precision == '16-mixed' Prevention
- Never carry a ShardedGradScaler across precision changes in FSDP configs
- Regenerate the whole plugin object when precision changes instead of mutating fields
When it happens
Trigger: FSDPMixedPrecisionPlugin(precision='bf16-mixed', scaler=ShardedGradScaler()) or precision='32-true' with a scaler; any non-'16-mixed' precision combined with a non-None scaler argument.
Common situations: Switching an FSDP fp16 recipe to bf16-mixed but keeping the ShardedGradScaler in the config; templated plugin construction that always passes a scaler; older examples that shipped with explicit scalers.
Related errors
- `precision='bf16-mixed'` does not use a scaler, found {scale
- Was unable to infer precision type, received {self.precision
- The optimizer has references to the model's meta-device para
- `precision={precision!r})` is not supported in FSDP. `precis
- `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/183f59b057486f63.
Report an issue: GitHub.