-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Enable auto parameters tying for TPUs #9525
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 all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
dac83cf
Enable auto parameters tying for TPUs
kaushikb11 bc45547
Update plugins
kaushikb11 9644d3d
Update decorators
kaushikb11 d4ba103
Update tests
kaushikb11 7ad20c1
Update tests
kaushikb11 3fdb3ee
Update typing
kaushikb11 7a1a921
Address reviews
kaushikb11 a5cb251
Update pytorch_lightning/core/decorators.py
kaushikb11 adf3f96
Update tests/accelerators/test_tpu_backend.py
kaushikb11 4247483
Address deprecation concerns
kaushikb11 f9c196f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 24b4c8d
Scrap the decorator logic
kaushikb11 00a7de8
Update tpu spawn plugin
kaushikb11 aa8f7df
Update decorators
kaushikb11 5e4ec17
Update tests
kaushikb11 82bf7d3
Update tests
kaushikb11 8f7ecbc
Address reviews
kaushikb11 22d5556
Merge branch 'master' into feat/enable_auto_tying__for_tpus
kaushikb11 63b03b1
Update deprecation msg
kaushikb11 68775ed
Add ParameterSharingModule
kaushikb11 2dd878a
Update changelog
kaushikb11 78b787d
Fix mypy
kaushikb11 8f7e828
Fix test
kaushikb11 c8ec0c3
Fix for tpu spawn
kaushikb11 4d6435b
Update CHANGELOG.md
kaushikb11 4031cf9
Update pytorch_lightning/core/decorators.py
kaushikb11 8f02d16
Update pytorch_lightning/trainer/configuration_validator.py
kaushikb11 942e3a8
Update pytorch_lightning/utilities/parameter_tying.py
kaushikb11 4e1df7e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0117bea
Address reviews
kaushikb11 f17cf25
Merge branch 'feat/enable_auto_tying__for_tpus' of https://github.com…
kaushikb11 d83d7ad
Fix test
kaushikb11 6ad1dd9
Update
kaushikb11 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
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
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
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,71 @@ | ||
| # Copyright The PyTorch Lightning team. | ||
| # | ||
| # 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. | ||
| """Utilities for automatic parameters tying. | ||
|
|
||
| Reference: | ||
| https://github.com/pytorch/fairseq/blob/1f7ef9ed1e1061f8c7f88f8b94c7186834398690/fairseq/trainer.py#L110-L118 | ||
| """ | ||
| from typing import Dict, List, Optional | ||
|
|
||
| from torch import nn | ||
| from torch.nn import Parameter | ||
|
|
||
|
|
||
| def find_shared_parameters(module: nn.Module) -> List[str]: | ||
| """Returns a list of names of shared parameters set in the module.""" | ||
| return _find_shared_parameters(module) | ||
|
|
||
|
|
||
| def _find_shared_parameters(module: nn.Module, tied_parameters: Optional[Dict] = None, prefix: str = "") -> List[str]: | ||
| if tied_parameters is None: | ||
| first_call = True | ||
| tied_parameters = {} | ||
| else: | ||
| first_call = False | ||
| for name, param in module._parameters.items(): | ||
| param_prefix = prefix + ("." if prefix else "") + name | ||
| if param is None: | ||
| continue | ||
| if param not in tied_parameters: | ||
| tied_parameters[param] = [] | ||
| tied_parameters[param].append(param_prefix) | ||
| for name, m in module._modules.items(): | ||
| if m is None: | ||
| continue | ||
| submodule_prefix = prefix + ("." if prefix else "") + name | ||
| _find_shared_parameters(m, tied_parameters, submodule_prefix) | ||
| if first_call: | ||
| return [x for x in tied_parameters.values() if len(x) > 1] | ||
|
|
||
|
|
||
| def set_shared_parameters(module: nn.Module, shared_params: list) -> nn.Module: | ||
| for shared_param in shared_params: | ||
| ref = _get_module_by_path(module, shared_param[0]) | ||
| for path in shared_param[1:]: | ||
| _set_module_by_path(module, path, ref) | ||
| return module | ||
|
|
||
|
|
||
| def _get_module_by_path(module: nn.Module, path: str) -> nn.Module: | ||
| path = path.split(".") | ||
| for name in path: | ||
| module = getattr(module, name) | ||
kaushikb11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return module | ||
|
|
||
|
|
||
| def _set_module_by_path(module: nn.Module, path: str, value: Parameter) -> None: | ||
| path = path.split(".") | ||
| for name in path[:-1]: | ||
| module = getattr(module, name) | ||
| setattr(module, path[-1], value) | ||
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
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.