-
Notifications
You must be signed in to change notification settings - Fork 3.6k
introduce default cluster environment for lightning-specific ddp #5915
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
2 commits
Select commit
Hold shift + click to select a range
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
71 changes: 71 additions & 0 deletions
71
pytorch_lightning/plugins/environments/lightning_environment.py
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. | ||
|
|
||
| import os | ||
| import socket | ||
| from typing import Optional | ||
|
|
||
| from pytorch_lightning.plugins.environments.cluster_environment import ClusterEnvironment | ||
|
|
||
|
|
||
| class LightningEnvironment(ClusterEnvironment): | ||
| """ | ||
| The default environment used by Lightning for a single node or free cluster (not managed). | ||
|
|
||
| The master process must be launched by the user and Lightning will spawn new | ||
| worker processes for distributed training, either in a single node or across multiple nodes. | ||
|
|
||
| If the master address and port are not provided, the default environment will choose them | ||
| automatically. It is recommended to use this default environment for single-node distributed | ||
| training as it provides the most convenient way to launch the training script. | ||
| """ | ||
|
|
||
| def __init__(self): | ||
| super().__init__() | ||
| self._master_port = None | ||
|
|
||
| def creates_children(self) -> bool: | ||
| return False | ||
|
|
||
| def master_address(self) -> str: | ||
| return os.environ.get("MASTER_ADDR", "127.0.0.1") | ||
|
|
||
| def master_port(self) -> int: | ||
| if self._master_port is None: | ||
| self._master_port = os.environ.get("MASTER_PORT", find_free_network_port()) | ||
| return int(self._master_port) | ||
|
|
||
| def world_size(self) -> Optional[int]: | ||
| return None | ||
|
|
||
| def local_rank(self) -> int: | ||
| return int(os.environ.get("LOCAL_RANK", 0)) | ||
|
|
||
| def node_rank(self) -> int: | ||
| group_rank = os.environ.get("GROUP_RANK", 0) | ||
| return int(os.environ.get("NODE_RANK", group_rank)) | ||
|
|
||
|
|
||
| def find_free_network_port() -> int: | ||
| """ | ||
| Finds a free port on localhost. | ||
| It is useful in single-node training when we don't want to connect to a real master node but | ||
| have to set the `MASTER_PORT` environment variable. | ||
| """ | ||
| s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
| s.bind(("", 0)) | ||
| s.listen(1) | ||
| port = s.getsockname()[1] | ||
| s.close() | ||
| return port |
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
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.