Skip to content

[BugFix] optim["interval"] is silently ignored: schedulers step once per batch regardless - #513

Open
SolidusAbi wants to merge 3 commits into
galilai-group:mainfrom
SolidusAbi:interval
Open

SolidusAbi wants to merge 3 commits into
galilai-group:mainfrom
SolidusAbi:interval

Conversation

@SolidusAbi

@SolidusAbi SolidusAbi commented Dec 1, 2026 •

Copy link
Copy Markdown

Description

Module documents interval ("step" or "epoch") in optim, but ignores it during training. Manual optimization (automatic_optimization = False) disables Lightning's automatic scheduler stepping, and training_step manually called scheduler.step() every batch—so "epoch" acted just like "step".

As a result, the README example (interval: epoch + CosineAnnealingLR(T_max=trainer.max_epochs)) runs a full cycle every $2 * T_{max}$ batches instead of once over the whole run. The average learning rate is roughly half the target, and logging every 50 steps samples the extremes in a sawtooth pattern that looks convincing enough to hide the bug.

This PR makes sp.Module handle scheduler stepping for both step and epoch.

What changed

  • Module stores the configured interval per optimizer in _optimizer_intervals, mirroring _optimizer_frequencies: the flat single-optimizer config, the multi-optimizer {name: {...}} form, and the on_train_start fallback for callback
    optimizers (which keep the "step" default).
  • training_step steps only interval="step" schedulers, as before.
  • New on_train_epoch_end steps interval="epoch" schedulers once per epoch.

Tests

New stable_pretraining/tests/unit/test_optimizer_interval.py: 9 CPU-only unit tests on toy data using a CosineAnnealingLR whose step is wrapped to count calls.

  • interval="step" -> n_batches * n_epochs scheduler steps (flat, named and two-optimizer configs)
  • interval="epoch" -> exactly n_epochs steps, one per epoch
  • two optimizers with different intervals ("step" + "epoch") -> each follows its own, which also pins the index -> name mapping between optimizers() and lr_schedulers()
  • frequency > 1 does not swallow the end-of-epoch step
  • end-to-end: the final LR matches a reference cosine schedule stepped n_epochs times

Verified the tests fail without the fix (5 of 9) and pass with it.

Checklist

  • I have read the Contributing document.
  • The documentation is up-to-date with the changes I made (check build artifacts).
  • All tests passed, and additional code has been covered with new tests.
  • I have added the PR to the RELEASES.rst file.

Abian Hernandez-Guedes added 2 commits December 1, 2026 16:11
With automatic_optimization=False Lightning never steps schedulers, so "interval" was dead config and every scheduler stepped per batch.

Epoch-interval schedulers now step once per epoch in on_train_epoch_end (independent of frequency); step-interval ones stay in training_step.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant