{"record":{"id":"c8504d18c0249f16","repo":"Lightning-AI/pytorch-lightning","slug":"setup-optimizers-requires-at-least-one-optimizer","errorCode":null,"errorMessage":"`setup_optimizers` requires at least one optimizer as input.","messagePattern":"`setup_optimizers` requires at least one optimizer as input\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/lightning/fabric/fabric.py","lineNumber":1232,"sourceCode":"                \" 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.\")\n\n    def _validate_setup_optimizers(self, optimizers: Sequence[Optimizer]) -> None:\n        self._validate_launched()\n        if isinstance(self._strategy, (DeepSpeedStrategy, XLAStrategy)):\n            raise RuntimeError(\n                f\"The `{type(self._strategy).__name__}` requires the model and optimizer(s) to be set up jointly\"\n                \" through `.setup(model, optimizer, ...)`.\"\n            )\n\n        if not optimizers:\n            raise ValueError(\"`setup_optimizers` requires at least one optimizer as input.\")\n\n        if any(isinstance(opt, _FabricOptimizer) for opt in optimizers):\n            raise ValueError(\"An optimizer should be passed only once to the `setup_optimizers` method.\")\n\n        if any(_has_meta_device_parameters_or_buffers(optimizer) for optimizer in optimizers):\n            raise RuntimeError(\n                \"The optimizer has references to the model's meta-device parameters. Materializing them is\"\n                \" is currently not supported. Create the optimizer after setting up the model, then call\"\n                \" `fabric.setup_optimizers(optimizer)`.\"\n            )\n\n    def _validate_setup_dataloaders(self, dataloaders: Sequence[DataLoader]) -> None:\n        self._validate_launched()\n        if not dataloaders:\n            raise ValueError(\"`setup_dataloaders` requires at least one dataloader as input.\")\n\n        if any(isinstance(dl, _FabricDataLoader) for dl in dataloaders):\n            raise ValueError(\"A dataloader should be passed only once to the `setup_dataloaders` method.\")","sourceCodeStart":1214,"sourceCodeEnd":1250,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/fabric.py#L1214-L1250","documentation":"Raised by Fabric's `_validate_setup_optimizers` when `fabric.setup_optimizers()` is called with an empty sequence of optimizers. Lightning Fabric requires at least one optimizer because the whole point of the method is to wrap optimizers with `_FabricOptimizer` and wire them into the strategy.","triggerScenarios":"Calling `fabric.setup_optimizers()` with no arguments, or passing an empty list/tuple e.g. `fabric.setup_optimizers([])`, or programmatically building an optimizer list that ends up empty (e.g. no params matched a filter).","commonSituations":"Scripts that conditionally create optimizers and pass a possibly-empty list; porting a Trainer-based script to Fabric where optimizer creation was conditional; refactoring that accidentally drops the optimizer from the call.","solutions":["Pass at least one optimizer: `optimizer = torch.optim.Adam(model.parameters(), lr=1e-3); fabric.setup_optimizers(optimizer)`","If optimizers are built dynamically, assert the list is non-empty before calling setup_optimizers","If you meant to set up only the model, use `fabric.setup(model)` instead"],"exampleFix":"// before\nfabric.setup_optimizers(*optimizers)  # optimizers == []\n\n// after\nassert optimizers, 'at least one optimizer required'\nfabric.setup_optimizers(*optimizers)","handlingStrategy":"validation","validationCode":"assert optimizers and all(not isinstance(o, _FabricOptimizer) for o in optimizers), 'need >=1 raw optimizer'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Build optimizers with an assert on non-emptiness before setup","Keep optimizer creation unconditional in training scripts"],"tags":["pytorch-lightning","fabric","optimizer","validation"],"backgroundTag":"empty-argument-validation","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}