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

Support CLI shorthand natively #12614

Merged
merged 10 commits into from
May 3, 2022
Merged

Support CLI shorthand natively #12614

merged 10 commits into from
May 3, 2022

Conversation

mauvilsa
Copy link
Contributor

@mauvilsa mauvilsa commented Apr 5, 2022

What does this PR do?

Uses a new version of jsonargparse which implements for subclasses the support for short command line arguments and specifying only the class name instead of the full class path. This allows to remove the placeholder LightningArgumentParser._convert_argv_issue_84.

The idea with the 4.6.0 version of jsonargparse is that all subclasses for the given base classes can be specified by name. No need to explicitly register them. The only requirement is that the module where the subclass is defined, is imported prior to parsing. For example in the case of --lr_scheduler two base classes are given torch.optim.lr_scheduler.{_LRScheduler,ReduceLROnPlateau}. All subclasses in the same module are already known by the parser, so for instance --lr_scheduler ExponentialLR would already work. One nice benefit of this is that many other parameters get the same support without needing to implement a register for it, e.g. --trainer.accelerator. And also works for user defined parameters, e.g. a module with signature def __init__(self, encoder: EncoderBase, decoder: DecoderBase):.

This does not introduce any breaking change, though it does make the registries a bit misleading. The CLIs might support more classes than what the registry has. Also using a decorator, e.g. @MODEL_REGISTRY, makes no difference. The module where a class is defined has to be imported for the decorator to register it. But since it is imported, the parser already knows about this class and supports it without having to register it.

Until _convert_argv_issue_85 is removed, still there is a need to register the callbacks. But assuming that this is done, there is a general question. Should the registries be deprecated?

Fixes #12464

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?
  • [n/a] 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

Anyone in the community is welcome to review the PR.
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 🙃

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.

Until _convert_argv_issue_85 is removed, still there is a need to register the callbacks. But assuming that this is done, there is a general question. Should the registries be deprecated?

If we have no use for them, yes.

pytorch_lightning/utilities/cli.py Show resolved Hide resolved
@carmocca carmocca added refactor lightningcli pl.cli.LightningCLI labels Apr 6, 2022
@mauvilsa mauvilsa marked this pull request as ready for review April 12, 2022 04:06
- Only accept ReduceLROnPlateau defined in cli module.
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.

This is great!

Follow-ups would be:

  • Deprecate registries
  • Deprecate LightningCLI(auto_registry=...)
  • Rewrite docs

pytorch_lightning/utilities/cli.py Show resolved Hide resolved
pytorch_lightning/utilities/cli.py Show resolved Hide resolved
@carmocca carmocca changed the title Remove LightningArgumentParser._convert_argv_issue_84 Support CLI shorthand natively Apr 13, 2022
@mauvilsa
Copy link
Contributor Author

This is missing the milestone. I guess it would be 1.7, right?

@carmocca carmocca added this to the 1.7 milestone Apr 26, 2022
@mergify mergify bot removed the has conflicts label May 2, 2022
@carmocca carmocca self-assigned this May 2, 2022
@carmocca carmocca added the community This PR is from the community label May 2, 2022
@rohitgr7
Copy link
Contributor

rohitgr7 commented May 2, 2022

hey @mauvilsa!
mind check the failing tests?

@carmocca carmocca mentioned this pull request May 2, 2022
8 tasks
@mauvilsa
Copy link
Contributor Author

mauvilsa commented May 2, 2022

hey @mauvilsa! mind check the failing tests?

This is why I implemented mock_subclasses, see https://github.com/mauvilsa/pytorch-lightning/blob/cli-remove-hack-84/tests/utilities/test_cli.py#L65-L73. Should be fixed by just adding the mock in that test.

@carmocca carmocca enabled auto-merge (squash) May 2, 2022 15:45
@mergify mergify bot added the ready PRs ready to be merged label May 3, 2022
@carmocca carmocca merged commit 1c25ab8 into Lightning-AI:master May 3, 2022
@mauvilsa mauvilsa deleted the cli-remove-hack-84 branch May 3, 2022 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community This PR is from the community lightningcli pl.cli.LightningCLI ready PRs ready to be merged refactor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[CLI] Cannot pass init_args to override config file
5 participants