{"record":{"id":"d14bcc83909ed090","repo":"Lightning-AI/pytorch-lightning","slug":"precision-bf16-mixed-does-not-use-a-scaler-fo","errorCode":null,"errorMessage":"`precision='bf16-mixed'` does not use a scaler, found {scaler}.","messagePattern":"`precision='bf16-mixed'` does not use a scaler, found (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/lightning/fabric/plugins/precision/amp.py","lineNumber":55,"sourceCode":"    \"\"\"\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                \" 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 ValueError(f\"`precision='bf16-mixed'` does not use a scaler, found {scaler}.\")\n        self.device = device\n        self.scaler = scaler\n\n        self._desired_input_dtype = torch.bfloat16 if self.precision == \"bf16-mixed\" else torch.float16\n\n    @override\n    def forward_context(self) -> AbstractContextManager:\n        return torch.autocast(self.device, dtype=self._desired_input_dtype)\n\n    @override\n    def convert_input(self, data: Any) -> Any:\n        return apply_to_collection(data, function=_convert_fp_tensor, dtype=Tensor, dst_type=self._desired_input_dtype)\n\n    @override\n    def convert_output(self, data: Any) -> Any:\n        return apply_to_collection(data, function=_convert_fp_tensor, dtype=Tensor, dst_type=torch.get_default_dtype())\n\n    @override","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/plugins/precision/amp.py#L37-L73","documentation":"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.","triggerScenarios":"MixedPrecision(precision='bf16-mixed', scaler=torch.amp.GradScaler(...)) or fabric/plugins config that injects a scaler while bf16-mixed is selected.","commonSituations":"Reusing fp16 training code (which creates a GradScaler) when switching the precision string to bf16-mixed; copy-pasted scaler setup in a config file.","solutions":["Remove the scaler argument when using bf16-mixed","Keep the scaler only with '16-mixed'","Conditionally create the scaler: only when precision == '16-mixed'"],"exampleFix":"# before\nplugin = MixedPrecision(precision=\"bf16-mixed\", scaler=torch.amp.GradScaler(\"cuda\"))\n\n# after\nplugin = MixedPrecision(precision=\"bf16-mixed\")  # no scaler for bf16","handlingStrategy":"validation","validationCode":"precision = \"bf16-mixed\"\nscaler = torch.amp.GradScaler(\"cuda\") if precision == \"16-mixed\" else None\nplugin = MixedPrecision(precision=precision, scaler=scaler)","typeGuard":"def scaler_allowed(precision: str) -> bool:\n    return precision == \"16-mixed\"","tryCatchPattern":null,"preventionTips":["Only construct a GradScaler when precision == '16-mixed'","Centralize precision/scaler wiring in one config function"],"tags":["amp","precision","gradscaler","pytorch-lightning"],"backgroundTag":"incompatible-configuration","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}