{"record":{"id":"814018cfcd445ff0","repo":"Lightning-AI/pytorch-lightning","slug":"when-using-multiple-models-deepspeed-please-pro","errorCode":null,"errorMessage":"When using multiple models + deepspeed, please provide the model used to perform the optimization: `self.backward(loss, model=model)`","messagePattern":"When using multiple models \\+ deepspeed, please provide the model used to perform the optimization: `self\\.backward\\(loss, model=model\\)`","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/lightning/fabric/fabric.py","lineNumber":512,"sourceCode":"            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,\n        module: Union[torch.nn.Module, _FabricModule],\n        optimizer: Union[Optimizer, _FabricOptimizer],","sourceCodeStart":494,"sourceCodeEnd":530,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/fabric.py#L494-L530","documentation":"With DeepSpeed, Fabric.backward(loss) without an explicit model needs to infer which DeepSpeedEngine to use. If more than one model was set up (self._models_setup > 1), the choice is ambiguous, so Fabric demands the target model be passed via the `model=` keyword.","triggerScenarios":"Calling fabric.setup()/setup_module() on two or more models with DeepSpeedStrategy and then calling fabric.backward(loss) without model=. Typical in multi-model setups (e.g. GAN, teacher-student, policy+critic) under deepspeed.","commonSituations":"Training adversarial or multi-network architectures with Fabric + DeepSpeed; splitting a pipeline into several wrapped modules.","solutions":["Pass the model whose loss you are backpropagating: fabric.backward(loss, model=model2)","Consolidate into a single nn.Module (e.g. a container holding both networks) and set that up once"],"exampleFix":"# before\nfabric.backward(loss)  # two models set up with deepspeed\n# after\nfabric.backward(loss, model=model2)","handlingStrategy":"validation","validationCode":"if fabric._models_setup > 1:\n    fabric.backward(loss, model=target_model)\nelse:\n    fabric.backward(loss)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["With DeepSpeed and more than one model, always pass model= explicitly","Wrap multi-network architectures in one container module"],"tags":["lightning","fabric","deepspeed","multi-model","backward"],"backgroundTag":"ambiguous-target-model","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}