{"record":{"id":"66fc00be7a4d4543","repo":"Lightning-AI/pytorch-lightning","slug":"a-model-should-be-passed-only-once-to-the-setup","errorCode":null,"errorMessage":"A model should be passed only once to the `setup` method.","messagePattern":"A model should be passed only once to the `setup` method\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/lightning/fabric/fabric.py","lineNumber":1203,"sourceCode":"        if is_overridden(\"run\", self, Fabric) and _is_using_cli():\n            raise TypeError(\n                \"Overriding `Fabric.run()` and launching from the CLI is not allowed. Run the script normally,\"\n                \" or change your code to directly call `fabric = Fabric(...); fabric.setup(...)` etc.\"\n            )\n        # wrap the run method, so we can inject setup logic or spawn processes for the user\n        setattr(self, \"run\", partial(self._wrap_and_launch, self.run))\n\n    def _validate_launched(self) -> None:\n        if not self._launched and not isinstance(self._strategy, (SingleDeviceStrategy, DataParallelStrategy)):\n            raise RuntimeError(\n                \"To use Fabric with more than one device, you must call `.launch()` or use the CLI:\"\n                \" `fabric run --help`.\"\n            )\n\n    def _validate_setup(self, module: nn.Module, optimizers: Sequence[Optimizer]) -> None:\n        self._validate_launched()\n        if isinstance(module, _FabricModule):\n            raise ValueError(\"A model should be passed only once to the `setup` method.\")\n\n        if any(isinstance(opt, _FabricOptimizer) for opt in optimizers):\n            raise ValueError(\"An optimizer should be passed only once to the `setup` method.\")\n\n        if isinstance(self._strategy, FSDPStrategy) and any(\n            _has_meta_device_parameters_or_buffers(optimizer) for optimizer in optimizers\n        ):\n            raise RuntimeError(\n                \"The optimizer has references to the model's meta-device parameters. Materializing them is\"\n                \" is currently not supported unless you to set up the model and optimizer(s) separately.\"\n                \" Create and set up the model first through `model = fabric.setup_module(model)`. Then create the\"\n                \" optimizer and set it up: `optimizer = fabric.setup_optimizers(optimizer)`.\"\n            )\n\n    def _validate_setup_module(self, module: nn.Module) -> None:\n        self._validate_launched()\n        if isinstance(module, _FabricModule):\n            raise ValueError(\"A model should be passed only once to the `setup_module` method.\")","sourceCodeStart":1185,"sourceCodeEnd":1221,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/fabric.py#L1185-L1221","documentation":"fabric.setup() wraps a model into a _FabricModule; passing an already-wrapped module again means double setup (re-wrapping, re-sharding, duplicated hooks). _validate_setup detects this via isinstance(module, _FabricModule) and raises ValueError.","triggerScenarios":"model = fabric.setup(model) followed by another fabric.setup(model, optimizer) — e.g. in a loop, in a re-entered function, or when setup responsibilities are split across helpers that both call setup.","commonSituations":"Refactoring so setup runs in two places; retry logic that re-runs setup; calling setup on the return of setup_module.","solutions":["Trace where the module was first wrapped and call setup only there","Use the returned reference once: model, optimizer = fabric.setup(model, optimizer); for later optimizers use fabric.setup_optimizers(optimizer)"],"exampleFix":"# before\nmodel = fabric.setup(model)\nmodel = fabric.setup(model, optimizer)  # second wrap\n# after\nmodel, optimizer = fabric.setup(model, optimizer)  # once","handlingStrategy":"type-guard","validationCode":"from lightning.fabric.wrappers import _FabricModule\nassert not isinstance(model, _FabricModule), 'already set up'","typeGuard":"from lightning.fabric.wrappers import _FabricModule\ndef needs_setup(m):\n    return not isinstance(m, _FabricModule)","tryCatchPattern":null,"preventionTips":["Assign and reuse the wrapped reference; keep setup calls at the top of the training entry point"],"tags":["lightning","fabric","setup","double-setup"],"backgroundTag":"double-initialization","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}