Skip to content
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

Scheduler should be initialized after Apex is configured #841

Closed
alexandrebone opened this issue Feb 14, 2020 · 7 comments · Fixed by #1873
Closed

Scheduler should be initialized after Apex is configured #841

alexandrebone opened this issue Feb 14, 2020 · 7 comments · Fixed by #1873
Labels
bug Something isn't working good first issue Good for newcomers help wanted Open to be worked on

Comments

@alexandrebone
Copy link

Description

Using jointly a scheduler and apex-amp throws a UserWarning:

Seems like optimizer.step() has been overridden after learning rate scheduler initialization. Please, make sure to call optimizer.step() before lr_scheduler.step().

As suggested here (https://discuss.pytorch.org/t/cyclic-learning-rate-how-to-use/53796), the scheduler should be initialized after Apex is configured.

To reproduce

Run the MNIST example with use_amp=True.

Expected behavior

No scheduler-related user warning.

Workaround

The following hack seems to work:

    def on_train_start(self):
        self.trainer.lr_schedulers = [optim.lr_scheduler.CosineAnnealingLR(self.trainer.optimizers[0], T_max=10)]
@alexandrebone alexandrebone added the bug Something isn't working label Feb 14, 2020
@Borda
Copy link
Member

Borda commented Feb 22, 2020

Good catch, @alexandrebone mind you send a PR with the fix? 🤖

@Borda Borda added good first issue Good for newcomers help wanted Open to be worked on need fix labels Feb 22, 2020
@stale
Copy link

stale bot commented May 6, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the won't fix This will not be worked on label May 6, 2020
@stale stale bot closed this as completed May 15, 2020
@dbpprt
Copy link
Contributor

dbpprt commented May 15, 2020

Any news on this? This bug is still active.

@Borda
Copy link
Member

Borda commented May 15, 2020

@SkafteNicki pls ^^

@Borda Borda reopened this May 15, 2020
@stale stale bot removed the won't fix This will not be worked on label May 15, 2020
@SkafteNicki
Copy link
Member

I will take a look at this. It will not be easy since we construct optimizers and schedulers at the same time currently. But maybe there is a workaround.

@SkafteNicki
Copy link
Member

The problem seems to be that the scheduler equips the optimizer.step() method
with a number of fields, that are lost after the optimizer is passed to apex. These
can be reconstructed by calling init method of base scheduler class (which all
schedulers inherence from). Here is working example ('_with_counter' is one field
set by the scheduler) that prevents the UserWarning:

import torch

optimizer = torch.optim.Adam(torch.nn.Linear(10,10).parameters(), lr=0.1)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, 10, 0.1)

print(hasattr(scheduler.optimizer.step, '_with_counter'))  # >>> True

# This mimics what apex is doing internally
old_step = optimizer.step
def new_step(self, closure=None):
    retval = old_step()
    return retval        
optimizer.step = new_step

print(hasattr(scheduler.optimizer.step, '_with_counter'))  # >>> False

# Call the base lr scheduler class to re-equip the optimizer with 
# fields important for the scheduler 
scheduler.__class__.__mro__[-2].__init__(scheduler, optimizer)

print(hasattr(scheduler.optimizer.step, '_with_counter'))  # >>> True

optimizer.step()
scheduler.step()

@Borda is this fine?

@Borda
Copy link
Member

Borda commented May 18, 2020

it looks reasonable to me...
cc: @PyTorchLightning/core-contributors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Open to be worked on
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants