Lightning-AI/pytorch-lightning · critical · RuntimeError

No models were set up for backward. Did you forget to call `

Error message

No models were set up for backward. Did you forget to call `fabric.setup()`?

What it means

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.

Source

Thrown at src/lightning/fabric/fabric.py:510

        Note:
            When using ``strategy="deepspeed"`` and multiple models were set up, it is required to pass in the
            model as argument here.

        Example::

            loss = criterion(output, target)
            fabric.backward(loss)

            # With DeepSpeed and multiple models
            fabric.backward(loss, model=model)

        """
        module = model._forward_module if model is not None else model
        module, _ = _unwrap_compiled(module)
        if isinstance(self._strategy, DeepSpeedStrategy):
            if model is None:
                if self._models_setup == 0:
                    raise RuntimeError("No models were set up for backward. Did you forget to call `fabric.setup()`?")
                if self._models_setup > 1:
                    raise ValueError(
                        "When using multiple models + deepspeed, please provide the model used to perform"
                        " the optimization: `self.backward(loss, model=model)`"
                    )
                module = self._strategy.model
            else:
                # requires to attach the current `DeepSpeedEngine` for the `_FabricOptimizer.step` call.
                self._strategy._deepspeed_engine = module

        lightning.fabric.wrappers._in_fabric_backward = True
        try:
            self._strategy.backward(tensor, module, *args, **kwargs)
        finally:
            lightning.fabric.wrappers._in_fabric_backward = False

    def clip_gradients(
        self,

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Call model = fabric.setup(model) (or fabric.setup_module) before fabric.backward(loss)
  2. If using multiple models with DeepSpeed, pass the model explicitly: fabric.backward(loss, model=model)

Example fix

# before
loss = model(x).sum()
fabric.backward(loss)  # model never set up
# after
model, optimizer = fabric.setup(model, optimizer)
loss = model(x).sum()
fabric.backward(loss)
Defensive patterns

Strategy: validation

Validate before calling

assert fabric._models_setup > 0, 'call fabric.setup(model) before fabric.backward(loss)'

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/6139dbf9e563f4a5. Report an issue: GitHub.