Lightning-AI/pytorch-lightning · error · ValueError

Passed `{type(self).__name__}(precision={precision!r})`. Pre

Error message

Passed `{type(self).__name__}(precision={precision!r})`. Precision must be '16-mixed' or 'bf16-mixed'.

What it means

MixedPrecision.__init__ validates its precision argument: only the literal strings '16-mixed' and 'bf16-mixed' are accepted. Any other value (e.g. 'float16', 16, 'bf16') raises this ValueError.

Source

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

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})`."
                " 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)

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Use precision='16-mixed' (fp16) or precision='bf16-mixed' (bf16)
  2. For full (non-mixed) 16-bit precision use the MixedPrecisionLite/other precision plugins or pass a dtype instead of this plugin
  3. Check the installed Lightning docs for accepted precision strings for your version

Example fix

# before
fabric = Fabric(precision="bf16")  # or "mixed"

# after
fabric = Fabric(precision="bf16-mixed")
Defensive patterns

Strategy: validation

Validate before calling

from typing import Literal
MixedPrecisionValue = Literal["16-mixed", "bf16-mixed"]

def check_precision(p: str) -> MixedPrecisionValue:
    assert p in ("16-mixed", "bf16-mixed"), f"invalid precision {p!r}"
    return p

Type guard

from typing import Literal, TypeGuard
MixedPrecisionValue = Literal["16-mixed", "bf16-mixed"]
def is_mixed_precision_value(p: str) -> TypeGuard[MixedPrecisionValue]:
    return p in ("16-mixed", "bf16-mixed")

Prevention

When it happens

Trigger: Constructing MixedPrecision(precision=...) with anything other than '16-mixed' or 'bf16-mixed'; often from Fabric(precision=...) which forwards the value.

Common situations: Migrating from older Lightning where precision was 'mixed'/'bf16', or passing raw dtypes/numbers like 16, 'fp16', 'bf16' instead of the mixed-precision literal names.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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