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

Fix LightningCLI saving hyperparameters breaking change #20068

Merged
merged 4 commits into from
Jul 11, 2024

Conversation

mauvilsa
Copy link
Contributor

@mauvilsa mauvilsa commented Jul 10, 2024

What does this PR do?

Changes the LightningCLI's load_from_checkpoint support such that the hparams are not saved with the class_path and init_args structure, since this constitutes a breaking change which we failed to notice in #18105.

Fixes #19977

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

📚 Documentation preview 📚: https://pytorch-lightning--20068.org.readthedocs.build/en/20068/

Copy link
Contributor

@adamjstewart adamjstewart left a comment

Choose a reason for hiding this comment

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

Thanks for the quick fix @mauvilsa. This seems very specific to init_args and ignores dict_kwargs. I also seem to be missing many class parameters with default values that are not listed in the YAML file, and/or optional values that default to None.

EDIT: It's possible no special handling is needed for dict_kwargs.

@adamjstewart
Copy link
Contributor

adamjstewart commented Jul 10, 2024

Steps to reproduce are identical to #19977 (comment), but replace model.py with:

from lightning.pytorch import LightningModule


class Model(LightningModule):
    def __init__(self, learning_rate, step_size=None):
        super().__init__()
        self.save_hyperparameters()
        assert 'learning_rate' in self.hparams
        assert 'step_size' in self.hparams

    def training_step(*args):
        pass

    def configure_optimizers(*args):
        pass

The code will fail because step_size is not defined in self.hparams. Note that this code does not fail if the default value is 2 instead of None.

@mauvilsa
Copy link
Contributor Author

I also seem to be missing many class parameters with default values that are not listed in the YAML file, and/or optional values that default to None.

Indeed, there was a bug.

It's possible no special handling is needed for dict_kwargs.

It is needed. It is now fixed and I added a new unit test based on your code snippet which includes dict_kwargs.

@adamjstewart please check again.

Copy link
Contributor

@adamjstewart adamjstewart left a comment

Choose a reason for hiding this comment

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

Passes all TorchGeo tests now, thanks for the quick fix!

Copy link
Contributor

@awaelchli awaelchli left a comment

Choose a reason for hiding this comment

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

Thank you @mauvilsa, very appreciated!

src/lightning/pytorch/cli.py Show resolved Hide resolved
@awaelchli awaelchli added bug Something isn't working lightningcli pl.cli.LightningCLI labels Jul 10, 2024
@awaelchli awaelchli added this to the 2.3.x milestone Jul 10, 2024
@awaelchli awaelchli merged commit 96b75df into Lightning-AI:master Jul 11, 2024
69 of 70 checks passed
@awaelchli
Copy link
Contributor

Thanks @mauvilsa @adamjstewart for the help here 🚀

@mauvilsa mauvilsa deleted the fix-cli-save-hyperparameters branch July 11, 2024 11:36
Copy link

codecov bot commented Oct 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82%. Comparing base (50af052) to head (6df8678).
Report is 63 commits behind head on master.

❗ There is a different number of reports uploaded between BASE (50af052) and HEAD (6df8678). Click for more details.

HEAD has 380 uploads less than BASE
Flag BASE (50af052) HEAD (6df8678)
cpu 106 21
lightning_fabric 19 0
pytest 59 2
python3.11 41 3
lightning 74 16
python3.8 36 6
python3.10 29 12
gpu 5 2
pytorch2.1 35 13
pytest-full 52 21
pytorch2.2 8 4
pytorch2.3 9 4
pytorch_lightning 18 7
Additional details and impacted files
@@            Coverage Diff            @@
##           master   #20068     +/-   ##
=========================================
- Coverage      90%      82%     -9%     
=========================================
  Files         266      263      -3     
  Lines       22942    22891     -51     
=========================================
- Hits        20722    18670   -2052     
- Misses       2220     4221   +2001     

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 pl Generic label for PyTorch Lightning package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

With yaml config file for LightningCLI, self.save_hyperparameters() behavior abnormal
3 participants