Skip to content

Fix switch_optimizer callback#608

Merged
GiovanniCanali merged 1 commit into
mathLab:devfrom
GiovanniCanali:fix_switch_optimizer
Jul 25, 2025
Merged

Fix switch_optimizer callback#608
GiovanniCanali merged 1 commit into
mathLab:devfrom
GiovanniCanali:fix_switch_optimizer

Conversation

@GiovanniCanali

@GiovanniCanali GiovanniCanali commented Jul 23, 2025

Copy link
Copy Markdown
Collaborator

Description

This PR fixes #607

Issue summary, for reference: While solver.optimizer was successfully updated, trainer.strategy.optimizers was not. As a result, PyTorch Lightning continued using the original optimizer during training, effectively ignoring the switch.

Checklist

  • Code follows the project’s Code Style Guidelines
  • Tests have been added or updated
  • Documentation has been updated if necessary
  • Pull request is linked to an open issue

@GiovanniCanali GiovanniCanali self-assigned this Jul 23, 2025
@GiovanniCanali GiovanniCanali added bug Something isn't working high priority Higher priority pr-to-fix Label for PR that needs modification labels Jul 23, 2025
@GiovanniCanali GiovanniCanali linked an issue Jul 23, 2025 that may be closed by this pull request
@GiovanniCanali GiovanniCanali added pr-to-review Label for PR that are ready to been reviewed and removed pr-to-fix Label for PR that needs modification labels Jul 23, 2025
@GiovanniCanali
GiovanniCanali marked this pull request as ready for review July 23, 2025 15:57
if trainer.current_epoch == self._epoch_switch:
optims = []

# Hook the new optimizers to the model parameters

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Maybe we could do a setter in the solver class? This way we will not forget in the future

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @dario-coscia,
Adding a setter in the solver class feels a bit redundant with configure_optimizers, in my opinion. It’s definitely a useful idea, but it might require more invasive changes.
Since we’re already planning to refactor the solver logic, we could revisit this at that time instead.
If that sounds good to you, I’d go ahead and merge this PR for now.

Comment thread pina/callback/optimizer_callback.py Outdated
@dario-coscia

Copy link
Copy Markdown
Collaborator

@GiovanniCanali I think you can rebase and merge

@GiovanniCanali
GiovanniCanali merged commit 1ed1491 into mathLab:dev Jul 25, 2025
18 of 19 checks passed
@GiovanniCanali
GiovanniCanali deleted the fix_switch_optimizer branch July 25, 2025 14:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working high priority Higher priority pr-to-review Label for PR that are ready to been reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected behaviour with SwitchOptimizer

4 participants