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

Disable lr_scheduler.step() in manual optimization #6825

Merged
merged 24 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Changed `PyTorchProfiler` to use `torch.autograd.profiler.record_function` to record functions ([#6349](https://github.com/PyTorchLightning/pytorch-lightning/pull/6349))


- Disabled `lr_schdeulder.step()` in manual optimization ([#6825](https://github.com/PyTorchLightning/pytorch-lightning/pull/6825))
akihironitta marked this conversation as resolved.
Show resolved Hide resolved


### Deprecated

- `period` has been deprecated in favor of `every_n_val_epochs` in the `ModelCheckpoint` callback ([#6146](https://github.com/PyTorchLightning/pytorch-lightning/pull/6146))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def update_learning_rates(self, interval: str, monitor_metrics=None):
interval: either 'epoch' or 'step'.
monitor_metrics: dict of possible values to monitor
"""
if not self.trainer.lr_schedulers:
if not self.trainer.lr_schedulers or not self.trainer.train_loop.automatic_optimization:
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's update the doc with this change + add a warning if we detect the scheduler was on step or epoch, but never triggered.

Copy link
Contributor Author

@akihironitta akihironitta Apr 7, 2021

Choose a reason for hiding this comment

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

@tchaton I updated the docs and added a warning when scheduler keys (e.g. "interval", "frequency", "monitor"), which are invalid in manual optimization, are provided. Would you mind having a look at the changes?

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks good. Could we add in the doc how to use the scheduler for interval=epoch.

 def training_step(self, batch, batch_idx)

     # do optimization

      if self.trainer.is_last_batch: # is_last_batch doesn't exist for the trainer, could be added.
             self.lr_schedulers[0].step()

Copy link
Contributor Author

@akihironitta akihironitta Apr 8, 2021

Choose a reason for hiding this comment

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

@tchaton I added self.trainer.is_last_batch and updated the docs. Could you have a look again?

return

for scheduler_idx, lr_scheduler in enumerate(self.trainer.lr_schedulers):
Expand Down
38 changes: 38 additions & 0 deletions tests/trainer/optimization/test_manual_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -1147,3 +1147,41 @@ def dis_closure():
@RunIf(min_gpus=2, special=True)
def test_step_with_optimizer_closure_with_different_frequencies_ddp_with_toggle_model(tmpdir):
train_manual_optimization(tmpdir, "ddp", model_cls=TestManualOptimizationDDPModelToggleModel)


def test_lr_scheduler_step_not_called(tmpdir):
"""
Test `lr_scheduler.step()` is not called in manual optimization.
"""
class TestModel(BoringModel):
def __init__(self):
super().__init__()
self.automatic_optimization = False
akihironitta marked this conversation as resolved.
Show resolved Hide resolved

def training_step(self, batch, batch_idx):
opt = self.optimizers()

output = self(batch)
loss = self.loss(batch, output)

opt.zero_grad()
self.manual_backward(loss)
opt.step()

model = TestModel()
model.training_step_end = None
model.training_epoch_end = None

trainer = Trainer(
max_epochs=1,
default_root_dir=tmpdir,
fast_dev_run=2,
)

with patch("torch.optim.lr_scheduler.StepLR.step") as lr_step:
trainer.fit(model)

# If a lr scheduler inherits `torch.optim.lr_scheduler._LRScheduler`,
# `.step()` is called once during its instantiation.
# Thus, the call count should be 1.
assert lr_step.call_count == 1