-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[CLI] Shorthand notation to instantiate callbacks [3/3] #8815
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
88 commits
Select commit
Hold shift + click to select a range
d7f00be
add registries
tchaton a07d305
simplify LightningCLI with defaults
tchaton ce39c47
cleanup
tchaton 3081475
update
tchaton 51f82d5
updates
tchaton 7197d6e
cleanup
tchaton 9a6e81e
update on comments
tchaton 41f5d78
update
tchaton 06e4999
cleanup
tchaton e91ea47
update on comments
tchaton 3f35ecd
Merge branch 'master' into lightning_cli_registries
tchaton 705c0bd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 78a4398
add docs
tchaton e96dc28
doc updates
tchaton 631aa72
update
tchaton 2fc3c0a
update
tchaton 43dd8b4
resolve comments
tchaton c6ae669
comment
tchaton 5c21b1c
add comment
tchaton b370deb
typo
tchaton f8e7ca7
update on comments
tchaton e428d2f
resolve bug
tchaton 3e97905
typo
tchaton 3b1bdb6
update
tchaton 0d1db29
resolve comments
tchaton 3d35c82
add unittesting
tchaton 4c0f960
resolve tests
tchaton d3a62ca
resolve comments
tchaton 39781a1
update on comments
tchaton 68c03de
doc updates
tchaton b01828b
update
tchaton d213c73
Merge branch 'master' into lightning_cli_registries
tchaton 5935ec4
update on comments
tchaton b6616f0
Merge branch 'lightning_cli_registries' of https://github.com/PyTorch…
tchaton 0d89423
Merge branch 'master' into lightning_cli_registries
carmocca 37fd679
Fix mypy
carmocca f16db3d
Revert unrelated change which had broken mypy
carmocca 572488c
Convert to staticmethod
carmocca 2fc4608
Replace context managers for functional static transformations
carmocca 9f383dc
Split tests
carmocca 2a7dfa8
Refactor optimizer tests
carmocca 423ab7b
Cleaning tests
carmocca 7c2e39e
Delete broken test
carmocca 048e159
Docs improvements
carmocca 86fce55
Docs improvements
carmocca 624b0d8
Restructure docs
carmocca 2cc0dc5
Docs for callbacks
carmocca f9b49fe
Add reload test when add_optimizer_args is added by the user
carmocca afcc4ba
Add failing config test - needs to be fixed
carmocca 9f41b88
Merge branch 'master' into lightning_cli_registries
carmocca 0ed4ae8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4dd0732
Use property
carmocca e0fae4f
Fixes after merge
carmocca 4f053bb
Merge branch 'master' into lightning_cli_registries
carmocca a22fdb3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 160b3f6
Update jsonargparse version
carmocca f185c2d
Use properties in registry
carmocca 803385c
Keep hacks together
carmocca 8eb8b05
Add FIXMEs
carmocca 9d84127
add_class_choices
carmocca 33ff2f4
Merge branch 'master' into lightning_cli_registries
carmocca cf82e1a
Remove contains registry. Avoid nested_key clash for optimizers and l…
carmocca b1cd083
Remove sanitize argv
carmocca 95d31a7
Better support for new callback format
carmocca 231e0ed
Avoid evaluating
carmocca 2af596f
Minor cleaning
carmocca 6add619
Mark argv as private
carmocca 525358a
Fix mypy
carmocca 84b8120
Fix mypy
carmocca 7e48c0e
Fix mypy
carmocca 40ce3c7
Merge branch 'master' into lightning_cli_registries
carmocca 3e77e8e
Support shorthand notation to instantiate optimizers and learning rat…
carmocca 1512a80
Update CHANGELOG
carmocca c6b86b1
Fix install
carmocca 6f1600c
Fix install
carmocca a3a791f
Use release
carmocca f67a90f
Merge branch 'feat/cli-shorthand-optimizers' into lightning_cli_regis…
carmocca fedae46
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ee7a068
Introduce set_choices
carmocca 6e67617
Undo change
carmocca e7f6d61
Replace add_class_choices with set_choices
carmocca 8e87359
Replace add_class_choices with set_choices
carmocca c74426b
Merge
carmocca 66cdb52
Docstrings
carmocca 9217304
Merge branch 'master' into lightning_cli_registries
carmocca 7b50401
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1406be9
Fix mypy
carmocca a000446
Undo change
carmocca File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# Copyright The PyTorch Lightning team. | ||
tchaton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import inspect | ||
from collections import UserDict | ||
from typing import Callable, List, Optional, Type | ||
|
||
import torch | ||
|
||
import pytorch_lightning as pl | ||
from pytorch_lightning.utilities.exceptions import MisconfigurationException | ||
|
||
|
||
class Registry(UserDict): | ||
tchaton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def __call__( | ||
self, | ||
cls: Optional[Type] = None, | ||
key: Optional[str] = None, | ||
override: bool = False, | ||
) -> Callable: | ||
""" | ||
Registers a plugin mapped to a name and with required metadata. | ||
|
||
Args: | ||
key : the name that identifies a plugin, e.g. "deepspeed_stage_3" | ||
value : plugin class | ||
""" | ||
tchaton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if key is None: | ||
key = cls.__name__ | ||
elif not isinstance(key, str): | ||
raise TypeError(f"`key` must be a str, found {key}") | ||
|
||
if key in self and not override: | ||
raise MisconfigurationException(f"'{key}' is already present in the registry. HINT: Use `override=True`.") | ||
|
||
def do_register(key, cls) -> Callable: | ||
self[key] = cls | ||
return cls | ||
|
||
do_register(key, cls) | ||
|
||
return do_register | ||
|
||
def register_package(self, module, base_cls: Type) -> None: | ||
for obj_name in dir(module): | ||
obj_cls = getattr(module, obj_name) | ||
if inspect.isclass(obj_cls) and issubclass(obj_cls, base_cls): | ||
self(cls=obj_cls) | ||
tchaton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def remove(self, name: str) -> None: | ||
"""Removes the registered plugin by name""" | ||
self.pop(name) | ||
tchaton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def available_objects(self) -> List: | ||
"""Returns a list of registered plugins""" | ||
return list(self.keys()) | ||
|
||
def __str__(self) -> str: | ||
return "Registered Plugins: {}".format(", ".join(self.keys())) | ||
|
||
|
||
CALLBACK_REGISTRIES = Registry() | ||
CALLBACK_REGISTRIES.register_package(pl.callbacks, pl.callbacks.Callback) | ||
|
||
OPTIMIZER_REGISTRIES = Registry() | ||
OPTIMIZER_REGISTRIES.register_package(torch.optim, torch.optim.Optimizer) | ||
|
||
SCHEDULER_REGISTRIES = Registry() | ||
SCHEDULER_REGISTRIES.register_package(torch.optim.lr_scheduler, torch.optim.lr_scheduler._LRScheduler) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.