{"record":{"id":"b8445741c5817c3f","repo":"Lightning-AI/pytorch-lightning","slug":"swa-with-swa-epoch-start-as-a-float-is-not-suppo","errorCode":null,"errorMessage":"SWA with `swa_epoch_start` as a float is not supported when `max_epochs=-1`. Please provide `swa_epoch_start` as an integer.","messagePattern":"SWA with `swa_epoch_start` as a float is not supported when `max_epochs=-1`\\. Please provide `swa_epoch_start` as an integer\\.","errorType":"exception","errorClass":"MisconfigurationException","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/callbacks/stochastic_weight_avg.py","lineNumber":169,"sourceCode":"    def setup(self, trainer: \"pl.Trainer\", pl_module: \"pl.LightningModule\", stage: str) -> None:\n        if isinstance(trainer.strategy, (FSDPStrategy, DeepSpeedStrategy)):\n            raise MisconfigurationException(\"SWA does not currently support sharded models.\")\n\n        # copy the model before moving it to accelerator device.\n        self._average_model = deepcopy(pl_module)\n\n    @override\n    def on_fit_start(self, trainer: \"pl.Trainer\", pl_module: \"pl.LightningModule\") -> None:\n        if len(trainer.optimizers) != 1:\n            raise MisconfigurationException(\"SWA currently works with 1 `optimizer`.\")\n\n        if len(trainer.lr_scheduler_configs) > 1:\n            raise MisconfigurationException(\"SWA currently not supported for more than 1 `lr_scheduler`.\")\n\n        assert trainer.max_epochs is not None\n        if isinstance(self._swa_epoch_start, float):\n            if trainer.max_epochs == -1:\n                raise MisconfigurationException(\n                    \"SWA with `swa_epoch_start` as a float is not supported when `max_epochs=-1`. \"\n                    \"Please provide `swa_epoch_start` as an integer.\"\n                )\n            self._swa_epoch_start = int(trainer.max_epochs * self._swa_epoch_start)\n\n        self._model_contains_batch_norm = self.pl_module_contains_batch_norm(pl_module)\n\n        self._max_epochs = trainer.max_epochs\n        if self._model_contains_batch_norm and trainer.max_epochs != -1:\n            # virtually increase max_epochs to perform batch norm update on latest epoch.\n            assert trainer.fit_loop.max_epochs is not None\n            trainer.fit_loop.max_epochs += 1\n\n        if self._scheduler_state is not None:\n            self._clear_schedulers(trainer)\n\n    @override\n    def on_train_epoch_start(self, trainer: \"pl.Trainer\", pl_module: \"pl.LightningModule\") -> None:","sourceCodeStart":151,"sourceCodeEnd":187,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/callbacks/stochastic_weight_avg.py#L151-L187","documentation":"When swa_epoch_start is a float (fraction of training), SWA needs trainer.max_epochs to convert it to an epoch number. With max_epochs=-1 (run until stopped), the total is unknown, so on_fit_start raises this MisconfigurationException.","triggerScenarios":"Trainer(max_epochs=-1) (or max_epochs=None defaulting to -1) combined with SWA(swa_epoch_start=0.75).","commonSituations":"Prototypes that rely on early stopping instead of a fixed epoch budget, then adding SWA with the default-ish float fraction.","solutions":["Pass an integer epoch: SWA(swa_epoch_start=100)","Set a fixed budget: Trainer(max_epochs=200) so the fraction can be resolved","Or remove SWA for open-ended training"],"exampleFix":"# before\ntrainer = Trainer(max_epochs=-1, callbacks=[SWA(swa_epoch_start=0.75)])\n# after\ntrainer = Trainer(max_epochs=200, callbacks=[SWA(swa_epoch_start=0.75)])\n# or: callbacks=[SWA(swa_epoch_start=150)]","handlingStrategy":"validation","validationCode":"def resolve_swa_start(start, max_epochs):\n    if isinstance(start, float):\n        assert max_epochs and max_epochs > 0, 'float swa_epoch_start requires fixed max_epochs'\n        return int(max_epochs * start)\n    return start\nswa = SWA(swa_epoch_start=resolve_swa_start(cfg.swa_start, cfg.max_epochs))","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always set Trainer(max_epochs=N) when using fractional swa_epoch_start","Convert fractions to ints yourself for open-ended training"],"tags":["swa","max-epochs","open-ended-training","callback"],"backgroundTag":"callback-requires-fixed-training-length","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}