-
Notifications
You must be signed in to change notification settings - Fork 24.1k
Fix lr_scheduler
unexpectedly calls step()
when init argument last_epoch is larger than -1
#149312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/149312
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Active SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: ✅ No FailuresAs of commit 538d5e0 with merge base 01f226b ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
424ac56
to
7c5e79a
Compare
@pytorchbot rebase -b main |
@pytorchbot started a rebase job onto refs/remotes/origin/main. Check the current status here |
…t_epoch is larger than -1
Successfully rebased |
7c5e79a
to
538d5e0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not look like the right approach. If the discrepancy is for ExponentialLR between get_lr and _get_closed_form_lr, I'd expect the fix to be local there. Could you explain your approach a little bit?
optim2 = torch.optim.AdamW(model.parameters()) | ||
optim2.load_state_dict(optim.state_dict()) | ||
sch2 = LRClass(optim2, last_epoch=1) | ||
self.assertEqual(optim.param_groups[0]["lr"], optim2.param_groups[0]["lr"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not the same comparison as the repro--we should be comparing that the closed form lr is the same as the params group lr?
@@ -724,7 +738,7 @@ def get_lr(self): | |||
"""Compute the learning rate of each parameter group.""" | |||
_warn_get_lr_called_within_step(self) | |||
|
|||
if self.last_epoch == 0: | |||
if self._is_initial: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if self._is_initial: | |
// when loading from a checkpoint, we don't want _initial_step (called from the constructor) to update the lr | |
// one more step ahead of itself. | |
if self._is_initial: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh actually, I see what you're doing now. Sorry I was confused yesterday. I'm willing to accept this fix if you update the test case.
It would also be good to include a comment about why we prefer the _is_initial
.
@@ -134,7 +135,8 @@ def wrapper(*args, **kwargs): | |||
def _initial_step(self): | |||
"""Initialize step counts and perform a step.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As someone who has looked into LRScheduler more than I've been able to, have you seen a good reason why we need to call .step() from the constructor?
Fixes #102261
Changes
_is_initial
to replaceself.last_epoch == 0
condition to judge whetherlr
should be initial valueExponentialLR
checkpoint usecaseTest Result