Lightning-AI/pytorch-lightning · error · MisconfigurationException

SWA with `swa_epoch_start` as a float is not supported when

Error message

SWA with `swa_epoch_start` as a float is not supported when `max_epochs=-1`. Please provide `swa_epoch_start` as an integer.

What it means

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.

Source

Thrown at src/lightning/pytorch/callbacks/stochastic_weight_avg.py:169

    def setup(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", stage: str) -> None:
        if isinstance(trainer.strategy, (FSDPStrategy, DeepSpeedStrategy)):
            raise MisconfigurationException("SWA does not currently support sharded models.")

        # copy the model before moving it to accelerator device.
        self._average_model = deepcopy(pl_module)

    @override
    def on_fit_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
        if len(trainer.optimizers) != 1:
            raise MisconfigurationException("SWA currently works with 1 `optimizer`.")

        if len(trainer.lr_scheduler_configs) > 1:
            raise MisconfigurationException("SWA currently not supported for more than 1 `lr_scheduler`.")

        assert trainer.max_epochs is not None
        if isinstance(self._swa_epoch_start, float):
            if trainer.max_epochs == -1:
                raise MisconfigurationException(
                    "SWA with `swa_epoch_start` as a float is not supported when `max_epochs=-1`. "
                    "Please provide `swa_epoch_start` as an integer."
                )
            self._swa_epoch_start = int(trainer.max_epochs * self._swa_epoch_start)

        self._model_contains_batch_norm = self.pl_module_contains_batch_norm(pl_module)

        self._max_epochs = trainer.max_epochs
        if self._model_contains_batch_norm and trainer.max_epochs != -1:
            # virtually increase max_epochs to perform batch norm update on latest epoch.
            assert trainer.fit_loop.max_epochs is not None
            trainer.fit_loop.max_epochs += 1

        if self._scheduler_state is not None:
            self._clear_schedulers(trainer)

    @override
    def on_train_epoch_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Pass an integer epoch: SWA(swa_epoch_start=100)
  2. Set a fixed budget: Trainer(max_epochs=200) so the fraction can be resolved
  3. Or remove SWA for open-ended training

Example fix

# before
trainer = Trainer(max_epochs=-1, callbacks=[SWA(swa_epoch_start=0.75)])
# after
trainer = Trainer(max_epochs=200, callbacks=[SWA(swa_epoch_start=0.75)])
# or: callbacks=[SWA(swa_epoch_start=150)]
Defensive patterns

Strategy: validation

Validate before calling

def resolve_swa_start(start, max_epochs):
    if isinstance(start, float):
        assert max_epochs and max_epochs > 0, 'float swa_epoch_start requires fixed max_epochs'
        return int(max_epochs * start)
    return start
swa = SWA(swa_epoch_start=resolve_swa_start(cfg.swa_start, cfg.max_epochs))

Prevention

When it happens

Trigger: Trainer(max_epochs=-1) (or max_epochs=None defaulting to -1) combined with SWA(swa_epoch_start=0.75).

Common situations: Prototypes that rely on early stopping instead of a fixed epoch budget, then adding SWA with the default-ish float fraction.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/b8445741c5817c3f. Report an issue: GitHub.