Skip to content

Fix LearningRateMonitor crash for optimizers without weight decay - #22036

Open
RaphaelFakhri wants to merge 1 commit into
Lightning-AI:masterfrom
RaphaelFakhri:bugfix/lr-monitor-missing-weight-decay
Open

RaphaelFakhri wants to merge 1 commit into
Lightning-AI:masterfrom
RaphaelFakhri:bugfix/lr-monitor-missing-weight-decay

Conversation

@RaphaelFakhri

Copy link
Copy Markdown

What does this PR do?

LearningRateMonitor(log_weight_decay=True) raises KeyError: 'weight_decay' when the optimizer has no weight_decay key in its param groups, for example torch.optim.Rprop, torch.optim.LBFGS or torch.optim.SparseAdam.

This change reads the value with param_group.get("weight_decay", 0), which matches how log_momentum=True already logs 0 for optimizers without momentum. A param group without weight_decay applies no weight decay, so 0 is the accurate value.

The new test test_log_weight_decay_no_weight_decay_optimizer trains a BoringModel with Rprop. It fails with KeyError on master and passes with this change.

Fixes #22035

No breaking changes.

Before submitting
  • Was this discussed/agreed via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or minor internal changes/refactors)

PR review

Anyone in the community is welcome to review the PR.
Before you start reviewing, make sure you have read the review guidelines. In short, see the following bullet-list:

Reviewer checklist
  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

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.

LearningRateMonitor(log_weight_decay=True) crashes with KeyError for optimizers without weight decay

1 participant