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

[CLI] Shorthand notation to instantiate callbacks [3/3] #8815

Merged
merged 88 commits into from
Sep 17, 2021

Conversation

tchaton
Copy link
Contributor

@tchaton tchaton commented Aug 9, 2021

What does this PR do?

Part of #7250
Implements the feature request in jsonargparse: omni-us/jsonargparse#85

co-author: @carmocca

  • Add support for multiple callbacks to be provided.
  • Automatically merge callbacks if provided with lists and class_paths.
python script.py fit --trainer.callbacks=LearningRateMonitor --trainer.callbacks.logging_interval=epoch --trainer.callbacks.log_momentum=True --trainer.callbacks=ModelCheckpoint --trainer.callbacks.monitor=loss

Does your PR introduce any breaking changes? If yes, please list them.

None

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 list all the breaking changes introduced by this pull request?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or internal minor changes/refactorings)

PR review

  • 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

@tchaton tchaton self-assigned this Aug 9, 2021
@tchaton tchaton added this to the v1.5 milestone Aug 9, 2021
@tchaton tchaton added the feature Is an improvement or enhancement label Aug 9, 2021
@codecov
Copy link

codecov bot commented Aug 9, 2021

Codecov Report

Merging #8815 (afcc4ba) into master (f87b2ef) will decrease coverage by 49%.
The diff coverage is 34%.

@@           Coverage Diff            @@
##           master   #8815     +/-   ##
========================================
- Coverage      93%     43%    -49%     
========================================
  Files         175     175             
  Lines       14402   14543    +141     
========================================
- Hits        13342    6319   -7023     
- Misses       1060    8224   +7164     

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.

Can you add to the post the following?:

  • Motivation
  • What's new
  • What was changed

pytorch_lightning/utilities/cli.py Outdated Show resolved Hide resolved
pytorch_lightning/utilities/cli.py Outdated Show resolved Hide resolved
pytorch_lightning/utilities/cli.py Outdated Show resolved Hide resolved
pytorch_lightning/utilities/cli.py Outdated Show resolved Hide resolved
@tchaton tchaton requested a review from carmocca August 10, 2021 12:18
pytorch_lightning/utilities/cli_registries.py Outdated Show resolved Hide resolved
pytorch_lightning/utilities/cli_registries.py Outdated Show resolved Hide resolved
pytorch_lightning/utilities/cli_registries.py Outdated Show resolved Hide resolved
pytorch_lightning/utilities/cli_registries.py Outdated Show resolved Hide resolved
pytorch_lightning/utilities/cli_registries.py Outdated Show resolved Hide resolved
tests/utilities/test_cli.py Outdated Show resolved Hide resolved
tests/utilities/test_cli.py Outdated Show resolved Hide resolved
tests/utilities/test_cli.py Outdated Show resolved Hide resolved
pytorch_lightning/utilities/cli.py Outdated Show resolved Hide resolved
@pep8speaks
Copy link

pep8speaks commented Aug 10, 2021

Hello @tchaton! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2021-08-12 07:21:22 UTC

@tchaton
Copy link
Contributor Author

tchaton commented Aug 10, 2021

User can registered their own callbacks. Somehow it doesn't work within the tests.

from unittest import mock
from pytorch_lightning import Callback
from pytorch_lightning.utilities.cli import CALLBACK_REGISTRIES, LightningCLI
from tests.helpers import BoringModel


class CustomCallback(Callback):
    pass

CALLBACK_REGISTRIES(cls=CustomCallback, override=True)

with mock.patch("sys.argv", ["any.py", "--trainer.callbacks=[{'class_path': test.CustomCallback}]"]):
    cli = LightningCLI(BoringModel, trainer_defaults=dict(fast_dev_run=True), run=False)
    callbacks = [c for c in cli.trainer.callbacks if isinstance(c, CustomCallback)]
    assert len(callbacks) == 1

@tchaton tchaton marked this pull request as ready for review August 10, 2021 14:50
@carmocca carmocca changed the base branch from master to feat/cli-shorthand-optimizers September 16, 2021 17:24
@carmocca carmocca changed the title [feat] LightningCLI simplification through registries [CLI] Shorthand notation to instantiate callbacks [3/3] Sep 16, 2021
@carmocca carmocca added callback argparse (removed) Related to argument parsing (argparse, Hydra, ...) and removed has conflicts labels Sep 16, 2021
Base automatically changed from feat/cli-shorthand-optimizers to master September 17, 2021 17:00
@carmocca carmocca marked this pull request as ready for review September 17, 2021 17:07
@mergify mergify bot added the ready PRs ready to be merged label Sep 17, 2021
@carmocca carmocca enabled auto-merge (squash) September 17, 2021 17:13
@carmocca carmocca merged commit 1bb5fcc into master Sep 17, 2021
@carmocca carmocca deleted the lightning_cli_registries branch September 17, 2021 17:54
SeanNaren pushed a commit that referenced this pull request Sep 22, 2021
Co-authored-by: Carlos Mocholi <carlossmocholi@gmail.com>
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, ...) callback 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.

10 participants