{"record":{"id":"a4dc517a6e9a1c1c","repo":"Lightning-AI/pytorch-lightning","slug":"passed-type-self-name-precision-precisio","errorCode":null,"errorMessage":"`Passed `{type(self).__name__}(precision={precision!r})`. Precision must be '16-mixed' or 'bf16-mixed'.","messagePattern":"`Passed `(.+?)\\(precision=(.+?)\\)`\\. Precision must be '16-mixed' or 'bf16-mixed'\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/plugins/precision/amp.py","lineNumber":66,"sourceCode":"\nclass MixedPrecision(Precision):\n    \"\"\"Plugin for Automatic Mixed Precision (AMP) training with ``torch.autocast``.\n\n    Args:\n        precision: Whether to use ``torch.float16`` (``'16-mixed'``) or ``torch.bfloat16`` (``'bf16-mixed'``).\n        device: The device for ``torch.autocast``.\n        scaler: An optional :class:`torch.cuda.amp.GradScaler` to use.\n\n    \"\"\"\n\n    def __init__(\n        self,\n        precision: Literal[\"16-mixed\", \"bf16-mixed\"],\n        device: str,\n        scaler: Optional[\"torch.amp.GradScaler\"] = None,\n    ) -> None:\n        if precision not in (\"16-mixed\", \"bf16-mixed\"):\n            raise ValueError(\n                f\"`Passed `{type(self).__name__}(precision={precision!r})`.\"\n                f\" Precision must be '16-mixed' or 'bf16-mixed'.\"\n            )\n\n        self.precision = precision\n        if scaler is None and self.precision == \"16-mixed\":\n            scaler = torch.amp.GradScaler(device=device)\n        if scaler is not None and self.precision == \"bf16-mixed\":\n            raise MisconfigurationException(f\"`precision='bf16-mixed'` does not use a scaler, found {scaler}.\")\n        self.device = device\n        self.scaler = scaler\n\n    @override\n    def pre_backward(self, tensor: Tensor, module: \"pl.LightningModule\") -> Tensor:  # type: ignore[override]\n        if self.scaler is not None:\n            tensor = self.scaler.scale(tensor)\n        return super().pre_backward(tensor, module)\n","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/plugins/precision/amp.py#L48-L84","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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)"],"exampleFix":"# before (Lightning 1.x style)\ntrainer = pl.Trainer(precision=16)\n# or\nplugin = MixedPrecisionPlugin(precision=16, device='cuda')\n\n# after\ntrainer = pl.Trainer(precision='16-mixed')\nplugin = MixedPrecisionPlugin(precision='16-mixed', device='cuda')","handlingStrategy":"type-guard","validationCode":"VALID = ('16-mixed', 'bf16-mixed')\nprecision = trainer_config.get('precision', '32-true')\nif precision in VALID:\n    plugin = MixedPrecisionPlugin(precision=precision, device='cuda')\n# else use default Precision plugin for '32-true' etc.","typeGuard":"def is_mixed_precision_literal(p) -> bool:\n    return p in ('16-mixed', 'bf16-mixed')","tryCatchPattern":null,"preventionTips":["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"],"tags":["pytorch-lightning","precision","amp","mixed-precision","version-migration"],"backgroundTag":"invalid-precision-config","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}