{"record":{"id":"6139dbf9e563f4a5","repo":"Lightning-AI/pytorch-lightning","slug":"no-models-were-set-up-for-backward-did-you-forget","errorCode":null,"errorMessage":"No models were set up for backward. Did you forget to call `fabric.setup()`?","messagePattern":"No models were set up for backward\\. Did you forget to call `fabric\\.setup\\(\\)`\\?","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"critical","filePath":"src/lightning/fabric/fabric.py","lineNumber":510,"sourceCode":"        Note:\n            When using ``strategy=\"deepspeed\"`` and multiple models were set up, it is required to pass in the\n            model as argument here.\n\n        Example::\n\n            loss = criterion(output, target)\n            fabric.backward(loss)\n\n            # With DeepSpeed and multiple models\n            fabric.backward(loss, model=model)\n\n        \"\"\"\n        module = model._forward_module if model is not None else model\n        module, _ = _unwrap_compiled(module)\n        if isinstance(self._strategy, DeepSpeedStrategy):\n            if model is None:\n                if self._models_setup == 0:\n                    raise RuntimeError(\"No models were set up for backward. Did you forget to call `fabric.setup()`?\")\n                if self._models_setup > 1:\n                    raise ValueError(\n                        \"When using multiple models + deepspeed, please provide the model used to perform\"\n                        \" the optimization: `self.backward(loss, model=model)`\"\n                    )\n                module = self._strategy.model\n            else:\n                # requires to attach the current `DeepSpeedEngine` for the `_FabricOptimizer.step` call.\n                self._strategy._deepspeed_engine = module\n\n        lightning.fabric.wrappers._in_fabric_backward = True\n        try:\n            self._strategy.backward(tensor, module, *args, **kwargs)\n        finally:\n            lightning.fabric.wrappers._in_fabric_backward = False\n\n    def clip_gradients(\n        self,","sourceCodeStart":492,"sourceCodeEnd":528,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/fabric.py#L492-L528","documentation":"Fabric.backward() with no `model=` argument requires exactly one model to have been set up when using DeepSpeed, because the DeepSpeedEngine must be attached to run backward. If zero models were set up, there is nothing to call backward on, so Fabric raises this RuntimeError.","triggerScenarios":"Calling fabric.backward(loss) before any fabric.setup(model) / fabric.setup_module(model) while the strategy is DeepSpeedStrategy. Common when refactorings move the backward call outside the setup flow or when setup is conditional.","commonSituations":"Porting a training loop to Fabric with deepspeed; calling backward on a raw loss computed from an un-wrapped module; reordering code so setup happens lazily.","solutions":["Call model = fabric.setup(model) (or fabric.setup_module) before fabric.backward(loss)","If using multiple models with DeepSpeed, pass the model explicitly: fabric.backward(loss, model=model)"],"exampleFix":"# before\nloss = model(x).sum()\nfabric.backward(loss)  # model never set up\n# after\nmodel, optimizer = fabric.setup(model, optimizer)\nloss = model(x).sum()\nfabric.backward(loss)","handlingStrategy":"validation","validationCode":"assert fabric._models_setup > 0, 'call fabric.setup(model) before fabric.backward(loss)'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always assign the return of fabric.setup and use that object for forward/backward","Set up model and optimizer in one place at loop start"],"tags":["lightning","fabric","deepspeed","backward","setup-order"],"backgroundTag":"model-not-initialized","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}