Lightning-AI/pytorch-lightning · error · TypeError
You need to set up the model first before you can call `fabr
Error message
You need to set up the model first before you can call `fabric.no_backward_sync()`: `model = fabric.setup(model, ...)`
What it means
fabric.no_backward_sync(model) requires the model to be a wrapped _FabricModule, because the context manager needs access to the strategy's backward-sync control on the wrapped module. Passing a raw nn.Module (never set up) fails the isinstance check and raises TypeError with remediation instructions.
Source
Thrown at src/lightning/fabric/fabric.py:771
becomes a no-op. For single-device strategies, it is always a no-op.
Example::
# Accumulate gradients over 8 batches
for batch_idx, batch in enumerate(dataloader):
with fabric.no_backward_sync(model, enabled=(batch_idx % 8 != 0)):
output = model(batch)
loss = criterion(output, target)
fabric.backward(loss)
if batch_idx % 8 == 0:
optimizer.step()
optimizer.zero_grad()
"""
module, _ = _unwrap_compiled(module)
if not isinstance(module, _FabricModule):
raise TypeError(
"You need to set up the model first before you can call `fabric.no_backward_sync()`:"
" `model = fabric.setup(model, ...)`"
)
if isinstance(self._strategy, (SingleDeviceStrategy, XLAStrategy)):
return nullcontext()
if self._strategy._backward_sync_control is None:
rank_zero_warn(
f"The `{self._strategy.__class__.__name__}` does not support skipping the gradient synchronization."
f" Remove `.no_backward_sync()` from your code or choose a different strategy.",
category=PossibleUserWarning,
)
return nullcontext()
forward_module, _ = _unwrap_compiled(module._forward_module)
return self._strategy._backward_sync_control.no_backward_sync(forward_module, enabled)
def sharded_model(self) -> AbstractContextManager:
r"""Instantiate a model under this context manager to prepare it for model-parallel sharding.View on GitHub (pinned to 9fed5c27d2)
Solutions
- Use the return value of setup: model = fabric.setup(model), then fabric.no_backward_sync(model)
- Note: on single-device or XLA strategies this context is a nullcontext anyway — the call is only meaningful for DDP/FSDP
Example fix
# before
model = MyModel()
fabric.setup(model)
with fabric.no_backward_sync(model): # raw module
...
# after
model = MyModel()
model = fabric.setup(model)
with fabric.no_backward_sync(model): # wrapped _FabricModule
... Defensive patterns
Strategy: type-guard
Validate before calling
from lightning.fabric.wrappers import _FabricModule assert isinstance(model, _FabricModule), 'setup the model first'
Type guard
from lightning.fabric.wrappers import _FabricModule
def is_setup(model):
return isinstance(model, _FabricModule) Prevention
- Shadow the original variable: model = fabric.setup(model) so stale raw references can't be used
- Skip the call on single-device strategies where it's a nullcontext anyway
When it happens
Trigger: Calling fabric.no_backward_sync(model) with the original unwrapped module instead of the object returned by fabric.setup(model)/setup_module(model). Also happens after _FabricModule.unwrap() when re-wrapping was forgotten.
Common situations: Using gradient accumulation with no_sync optimization; keeping references to the pre-setup model around and passing the stale reference; unwrapping for checkpointing then continuing training.
Related errors
- No models were set up for backward. Did you forget to call `
- To use Fabric with more than one device, you must call `.lau
- Received multiple values for {', '.join(duplicated_plugin_ke
- Received both `precision={precision_input}` and `plugins={se
- accelerator set through both strategy class and accelerator
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/d5cd731cc278f8ed.
Report an issue: GitHub.