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

Easier configurability of callbacks that should always be present in LightningCLI #7964

Merged
merged 10 commits into from
Jun 16, 2021
Merged

Easier configurability of callbacks that should always be present in LightningCLI #7964

merged 10 commits into from
Jun 16, 2021

Conversation

mauvilsa
Copy link
Contributor

@mauvilsa mauvilsa commented Jun 14, 2021

What does this PR do?

Based on a discussion in slack it seemed convenient in LightningCLI to have callbacks that should always be present and be configurable. This only required a small change such that LightningArgumentParser.add_lightning_class_args also support callbacks and the callbacks added by this method be automatically added to the trainer. How to use it is explained in lightning_cli.rst.

Before submitting

  • Was this discussed/approved 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 update the CHANGELOG? (not for typos, docs, test updates, or internal minor changes/refactorings)

PR review

Anyone in the community is free to review the PR once the tests have passed.
Before you start reviewing make sure you have read Review guidelines. In short, see the following bullet-list:

  • 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

Did you have fun?

Make sure you had fun coding 🙃

@codecov
Copy link

codecov bot commented Jun 14, 2021

Codecov Report

Merging #7964 (91cef79) into master (024cf23) will decrease coverage by 5%.
The diff coverage is 100%.

@@           Coverage Diff           @@
##           master   #7964    +/-   ##
=======================================
- Coverage      92%     87%    -5%     
=======================================
  Files         203     203            
  Lines       13151   13156     +5     
=======================================
- Hits        12042   11412   -630     
- Misses       1109    1744   +635     

@mauvilsa
Copy link
Contributor Author

@jlperla

@awaelchli awaelchli added argparse (removed) Related to argument parsing (argparse, Hydra, ...) feature Is an improvement or enhancement labels Jun 14, 2021
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.

nice! this seems very useful

docs/source/common/lightning_cli.rst Outdated Show resolved Hide resolved
@awaelchli awaelchli added this to the v1.4 milestone Jun 14, 2021
Copy link
Contributor

@carmocca carmocca left a comment

Choose a reason for hiding this comment

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

Thanks a lot @mauvilsa

Can you also use this PR to mention that the config can be passed directly through CLI?

python script.py --config='{"trainer": {"callbacks": [{"class_path": "pytorch_lightning.callbacks.EarlyStopping", ...}]}}'

pytorch_lightning/utilities/cli.py Outdated Show resolved Hide resolved
docs/source/common/lightning_cli.rst Outdated Show resolved Hide resolved
@carmocca carmocca added the ready PRs ready to be merged label Jun 14, 2021
Copy link
Member

@ethanwharris ethanwharris left a comment

Choose a reason for hiding this comment

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

Nice! Minor comment

docs/source/common/lightning_cli.rst Outdated Show resolved Hide resolved
@jlperla
Copy link

jlperla commented Jun 14, 2021

This is amazing!

The only thing that is missing as far as I can tell is showing how to change the default arguments. You often want to set a particular default that is different from the class constructors. For example, in your problem you may want to swap out the EarlyStopping to have a specific monitor variable and then let the user tweak the others. Or maybe change the default patience, etc.

@@ -48,6 +48,7 @@ def __init__(self, *args, parse_as_dict: bool = True, **kwargs) -> None:
self.add_argument(
'--config', action=ActionConfigFile, help='Path to a configuration file in json or yaml format.'
)
self.callbacks = []

def add_lightning_class_args(
Copy link

Choose a reason for hiding this comment

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

What about adding in a place to override default arugments here? It would make sense for code locality. Then something like

def add_lightning_class_args(
        self,
        lightning_class: Union[Type[Trainer], Type[LightningModule], Type[LightningDataModule]],
        nested_key: str,
        class_defaults: Dict[str, Any] = None,
        subclass_mode: bool = False
    ) -> None:
        """
        Adds arguments from a lightning class to a nested key of the parser
        Args:
            lightning_class: Any subclass of {Trainer, LightningModule, LightningDataModule, Callback}.
            nested_key: Name of the nested namespace to store arguments.
            subclass_mode: Whether allow any subclass of the given class.
            class_defaults: Set to override class defaults
        """

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have added to the example in the docs an override of a default to illustrate how it could be done right now. Adding a class_defaults argument to add_lightning_class_args is a good idea. But this only makes sense if it works well with the subclass_mode option. Certainly can be done, but requires work and I can't do this today.

@mergify mergify bot removed the has conflicts label Jun 15, 2021
@carmocca carmocca enabled auto-merge (squash) June 15, 2021 10:52
@carmocca carmocca merged commit 0004216 into Lightning-AI:master Jun 16, 2021
@mauvilsa mauvilsa deleted the cli_configurable_callbacks branch June 16, 2021 06:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
argparse (removed) Related to argument parsing (argparse, Hydra, ...) feature Is an improvement or enhancement ready PRs ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants