Skip to content

LightningCLI does not overwrite optimizer defined in LightningModule #12759

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

Closed
Toekan opened this issue Apr 14, 2022 · 1 comment
Closed

LightningCLI does not overwrite optimizer defined in LightningModule #12759

Toekan opened this issue Apr 14, 2022 · 1 comment
Assignees
Labels
bug Something isn't working lightningcli pl.cli.LightningCLI waiting on author Waiting on user action, correction, or update

Comments

@Toekan
Copy link

Toekan commented Apr 14, 2022

🐛 Bug

Hi,

First of all, thanks for the great work creating pytorch lightning. Lately I've been using the LightningCLI functionality and it really cut down a lot of boilerplate code and made the CLI easier to use. So thanks for that!

Unfortunately I found one detail where I either don't understand the documentation well, or the behaviour is not as intended: the optimizer defined in configuration file does not seem to overwrite one defined in LightningModule. configure_optimizers.

To Reproduce

Initially I used a hardcoded optimizer and scheduler defined in my LightningModule, like:


class MyBoringModel(LightningModule):
  ...
 def configure_optimizers(self):
        optimizer = torch.optim.RMSprop(
            params=[p for p in self.model.parameters() if p.requires_grad],
            lr=self.hparams.initial_lr,
        )

        lr_scheduler = torch.optim.lr_scheduler.ExponentialLR(
            optimizer, gamma=self.hparams.scheduler_gamma
        )

        return [optimizer], [lr_scheduler]

Then I decided I wanted to change to a different optimizer, so I added the following to the config I pass in to the LightningCLI:

# my_config.yaml
...

optimizer:
  class_path: torch.optim.SGD
  init_args:
    lr: 0.0001
    momentum: 0.9

...

A dummy example of the script I run:

# cli.py

trainer_defaults = {
        "callbacks": [
            LearningRateMonitor(logging_interval="epoch"),
            # Default latest model checkpoint
            ModelCheckpoint(),
            # Best performing model checkpoint
            ModelCheckpoint(
                filename="best-epoch={epoch}-loss={total_loss/test_epoch:.3f}",
                monitor="total_loss/test_epoch",
                auto_insert_metric_name=False,
            ),
        ]
    }

cli = LightningCLI(
        model_class=MyBoringModel,
        datamodule_class=MyIrrelevantDataModule,
        trainer_defaults=trainer_defaults,
        run=True,
)

When running the code python cli.py fit --config my_config.yaml I get the following warning:

UserWarning: `MyBoringModel.configure_optimizers` will be overridden by `LightningCLI.configure_optimizers`. 

so I, incorrectly, assumed it would take the optimizer from the config, and either take the scheduler from my model's method, or no/a default scheduler. Instead, it seems to have just taken both the optimizer and the scheduler from my model's method. Is this intended? If so, should the documentation of LightningCLI regarding optimizers and schedulers be expanded to clarify this?

Small detail: the reason I assume the optimizer isn't being overwritten, is because LearningRateMonitor is still logging the learning rate of the optimize defined in the LightningModule, so I guess there is a chance the training is happening with the new optimizer, but the callback is logging the wrong optimizer.

Expected behavior

Overwrite the optimizer and scheduler from the LightningModule with whatever is provided from the config file, even if it's only the optimizer, or only the scheduler.

Environment

  • PyTorch Lightning Version: 1.6.0
  • PyTorch Version: 1.11.0
  • Python version: 3.8
  • OS (e.g., Linux): Linux
  • CUDA/cuDNN version: / (happens both cpu-only runs and gpu runs)
  • How you installed PyTorch (conda, pip, source): pip

Thanks!

cc @carmocca @mauvilsa

@Toekan Toekan added the needs triage Waiting to be triaged by maintainers label Apr 14, 2022
@awaelchli awaelchli added the lightningcli pl.cli.LightningCLI label Apr 15, 2022
@carmocca carmocca added bug Something isn't working and removed needs triage Waiting to be triaged by maintainers labels Apr 18, 2022
@carmocca carmocca self-assigned this May 3, 2022
@carmocca carmocca added this to the 1.6.x milestone May 3, 2022
@carmocca carmocca moved this to Todo in Frameworks Planning May 9, 2022
@carmocca
Copy link
Contributor

I am not able to reproduce the issue:

import torch

from pytorch_lightning.callbacks import LearningRateMonitor
from pytorch_lightning.utilities.cli import LightningCLI
from tests.helpers import BoringModel


class MyBoringModel(BoringModel):
    def configure_optimizers(self):
        optimizer = torch.optim.RMSprop(
            params=[p for p in self.layer.parameters() if p.requires_grad],
            lr=0.1,
        )

        lr_scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer, gamma=0.1)

        return [optimizer], [lr_scheduler]


lr_monitor = LearningRateMonitor(logging_interval="epoch")
cli = LightningCLI(model_class=MyBoringModel, run=True, trainer_defaults={"fast_dev_run": 1, "callbacks": lr_monitor})

print(lr_monitor.lrs)
print(cli.trainer.optimizers)
print(cli.trainer.lr_scheduler_configs)

Prints

{'lr-SGD': [0.0001]}                                                                                                                                                                                                                                                                                                                                                                      
[SGD (
Parameter Group 0
    dampening: 0
    foreach: None
    lr: 0.0001
    maximize: False
    momentum: 0.9
    nesterov: False
    weight_decay: 0
)]
[]

which are the values from the config, as expected.

@carmocca carmocca removed this from the pl:1.6.x milestone Jul 28, 2022
@carmocca carmocca added the waiting on author Waiting on user action, correction, or update label Jul 28, 2022
@carmocca carmocca closed this as not planned Won't fix, can't repro, duplicate, stale Aug 11, 2022
Repository owner moved this from Todo to Done in Frameworks Planning Aug 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working lightningcli pl.cli.LightningCLI waiting on author Waiting on user action, correction, or update
Projects
No open projects
Status: Done
Development

No branches or pull requests

3 participants