{"record":{"id":"ba1a91af08dbd85b","repo":"Lightning-AI/pytorch-lightning","slug":"you-have-overridden-the-lightningmodule-backward","errorCode":null,"errorMessage":"You have overridden the `LightningModule.backward` hook but it will be ignored since DeepSpeed handles the backward logic internally.","messagePattern":"You have overridden the `LightningModule\\.backward` hook but it will be ignored since DeepSpeed handles the backward logic internally\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/lightning/pytorch/plugins/precision/deepspeed.py","lineNumber":112,"sourceCode":"        self,\n        tensor: Tensor,\n        model: \"pl.LightningModule\",\n        optimizer: Optional[Steppable],\n        *args: Any,\n        **kwargs: Any,\n    ) -> None:\n        r\"\"\"Performs back-propagation using DeepSpeed's engine.\n\n        Args:\n            tensor: the loss tensor\n            model: the model to be optimized\n            optimizer: ignored for DeepSpeed\n            \\*args: additional positional arguments for the :meth:`deepspeed.DeepSpeedEngine.backward` call\n            \\**kwargs: additional keyword arguments for the :meth:`deepspeed.DeepSpeedEngine.backward` call\n\n        \"\"\"\n        if is_overridden(\"backward\", model):\n            warning_cache.warn(\n                \"You have overridden the `LightningModule.backward` hook but it will be ignored since DeepSpeed handles\"\n                \" the backward logic internally.\"\n            )\n        deepspeed_engine: deepspeed.DeepSpeedEngine = model.trainer.model\n        deepspeed_engine.backward(tensor, *args, **kwargs)\n\n    @override\n    def optimizer_step(  # type: ignore[override]\n        self,\n        optimizer: Steppable,\n        model: \"pl.LightningModule\",\n        closure: Callable[[], Any],\n        **kwargs: Any,\n    ) -> Any:\n        if isinstance(optimizer, LBFGS):\n            raise MisconfigurationException(\"DeepSpeed and the LBFGS optimizer are not compatible.\")\n        closure_result = closure()\n        self._after_closure(model, optimizer)","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/plugins/precision/deepspeed.py#L94-L130","documentation":"Warning (not exception) emitted by DeepSpeedPrecision's backward(): if the LightningModule overrides the backward hook, DeepSpeed will ignore it because the DeepSpeed engine performs its own backward pass with its internal loss scaling.","triggerScenarios":"Using Trainer(strategy='deepspeed', ...) with a LightningModule that defines def backward(self, loss): ...","commonSituations":"Porting a mixed-precision training script that manually calls loss.backward() or scales gradients in backward() to DeepSpeed.","solutions":["Remove the backward override when using DeepSpeed; rely on DeepSpeed's engine.backward()","Move custom loss scaling into training_step or a precision plugin","If you need custom backward logic, switch to a non-DeepSpeed strategy"],"exampleFix":"# before\nclass M(LLightningModule):\n    def backward(self, loss):\n        loss.backward()\n# after (with deepspeed strategy)\nclass M(LightningModule):\n    # no backward override; DeepSpeed handles it\n    pass","handlingStrategy":"validation","validationCode":"from lightning.pytorch.utilities.model_helpers import is_overridden\nassert not is_overridden('backward', model), 'remove backward override for deepspeed'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Don't override backward() when using DeepSpeed","Put loss manipulation in training_step instead"],"tags":["deepspeed","backward-hook","precision","lightning"],"backgroundTag":"hook-override-ignored","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}