{"record":{"id":"d5cd731cc278f8ed","repo":"Lightning-AI/pytorch-lightning","slug":"you-need-to-set-up-the-model-first-before-you-can","errorCode":null,"errorMessage":"You need to set up the model first before you can call `fabric.no_backward_sync()`: `model = fabric.setup(model, ...)`","messagePattern":"You need to set up the model first before you can call `fabric\\.no_backward_sync\\(\\)`: `model = fabric\\.setup\\(model, \\.\\.\\.\\)`","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/lightning/fabric/fabric.py","lineNumber":771,"sourceCode":"            becomes a no-op. For single-device strategies, it is always a no-op.\n\n        Example::\n\n            # Accumulate gradients over 8 batches\n            for batch_idx, batch in enumerate(dataloader):\n                with fabric.no_backward_sync(model, enabled=(batch_idx % 8 != 0)):\n                    output = model(batch)\n                    loss = criterion(output, target)\n                    fabric.backward(loss)\n\n                if batch_idx % 8 == 0:\n                    optimizer.step()\n                    optimizer.zero_grad()\n\n        \"\"\"\n        module, _ = _unwrap_compiled(module)\n        if not isinstance(module, _FabricModule):\n            raise TypeError(\n                \"You need to set up the model first before you can call `fabric.no_backward_sync()`:\"\n                \" `model = fabric.setup(model, ...)`\"\n            )\n        if isinstance(self._strategy, (SingleDeviceStrategy, XLAStrategy)):\n            return nullcontext()\n        if self._strategy._backward_sync_control is None:\n            rank_zero_warn(\n                f\"The `{self._strategy.__class__.__name__}` does not support skipping the gradient synchronization.\"\n                f\" Remove `.no_backward_sync()` from your code or choose a different strategy.\",\n                category=PossibleUserWarning,\n            )\n            return nullcontext()\n\n        forward_module, _ = _unwrap_compiled(module._forward_module)\n        return self._strategy._backward_sync_control.no_backward_sync(forward_module, enabled)\n\n    def sharded_model(self) -> AbstractContextManager:\n        r\"\"\"Instantiate a model under this context manager to prepare it for model-parallel sharding.","sourceCodeStart":753,"sourceCodeEnd":789,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/fabric.py#L753-L789","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"# before\nmodel = MyModel()\nfabric.setup(model)\nwith fabric.no_backward_sync(model):  # raw module\n    ...\n# after\nmodel = MyModel()\nmodel = fabric.setup(model)\nwith fabric.no_backward_sync(model):  # wrapped _FabricModule\n    ...","handlingStrategy":"type-guard","validationCode":"from lightning.fabric.wrappers import _FabricModule\nassert isinstance(model, _FabricModule), 'setup the model first'","typeGuard":"from lightning.fabric.wrappers import _FabricModule\ndef is_setup(model):\n    return isinstance(model, _FabricModule)","tryCatchPattern":null,"preventionTips":["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"],"tags":["lightning","fabric","no-backward-sync","ddp","setup-order"],"backgroundTag":"unwrapped-module-passed","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}