Lightning-AI/pytorch-lightning · error · ValueError

`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

MixedPrecision with bf16-mixed does not use a gradient scaler (bfloat16 does not need loss scaling). Passing a non-None scaler alongside precision='bf16-mixed' raises this ValueError at construction.

Source

Thrown at src/lightning/fabric/plugins/precision/amp.py:55

    """

    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})`."
                " 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 ValueError(f"`precision='bf16-mixed'` does not use a scaler, found {scaler}.")
        self.device = device
        self.scaler = scaler

        self._desired_input_dtype = torch.bfloat16 if self.precision == "bf16-mixed" else torch.float16

    @override
    def forward_context(self) -> AbstractContextManager:
        return torch.autocast(self.device, dtype=self._desired_input_dtype)

    @override
    def convert_input(self, data: Any) -> Any:
        return apply_to_collection(data, function=_convert_fp_tensor, dtype=Tensor, dst_type=self._desired_input_dtype)

    @override
    def convert_output(self, data: Any) -> Any:
        return apply_to_collection(data, function=_convert_fp_tensor, dtype=Tensor, dst_type=torch.get_default_dtype())

    @override

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Remove the scaler argument when using bf16-mixed
  2. Keep the scaler only with '16-mixed'
  3. Conditionally create the scaler: only when precision == '16-mixed'

Example fix

# before
plugin = MixedPrecision(precision="bf16-mixed", scaler=torch.amp.GradScaler("cuda"))

# after
plugin = MixedPrecision(precision="bf16-mixed")  # no scaler for bf16
Defensive patterns

Strategy: validation

Validate before calling

precision = "bf16-mixed"
scaler = torch.amp.GradScaler("cuda") if precision == "16-mixed" else None
plugin = MixedPrecision(precision=precision, scaler=scaler)

Type guard

def scaler_allowed(precision: str) -> bool:
    return precision == "16-mixed"

Prevention

When it happens

Trigger: MixedPrecision(precision='bf16-mixed', scaler=torch.amp.GradScaler(...)) or fabric/plugins config that injects a scaler while bf16-mixed is selected.

Common situations: Reusing fp16 training code (which creates a GradScaler) when switching the precision string to bf16-mixed; copy-pasted scaler setup in a config file.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/d14bcc83909ed090. Report an issue: GitHub.