-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Support distributed init outside runtime init #276
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
47e170e
deepspeed distributed with mpi discovery
jeffra 4e36fc3
remove env var
jeffra 434d616
remove import
jeffra 51b88b0
add warning about dist_init_required
jeffra 309bbb8
fixes
jeffra b510f11
add all torch distributed environ vars
jeffra e0e616f
allow ability to turn off mpi discovery and change name
jeffra 4e82529
Merge branch 'master' into jeffra/ds_dist_init
jeffra 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,63 @@ | ||
| import os | ||
| import torch | ||
| from deepspeed.pt.log_utils import logger | ||
| from deepspeed.pt.deepspeed_constants import TORCH_DISTRIBUTED_DEFAULT_PORT | ||
|
|
||
|
|
||
| def init_distributed(dist_backend="nccl", auto_mpi_discovery=True): | ||
| """ | ||
| Initialize torch.distributed backend, potentially performing MPI discovery if needed | ||
|
|
||
| Arguments: | ||
| dist_backend: torch distributed backend, e.g., nccl, mpi, gloo | ||
| auto_mpi_discovery: if distributed environment variables are not set, attempt to discover them from MPI | ||
| """ | ||
| required_env = ["RANK", "WORLD_SIZE", "MASTER_ADDR", "MASTER_PORT", "LOCAL_RANK"] | ||
| if auto_mpi_discovery and not all(map(lambda v: v in os.environ, required_env)): | ||
| logger.info( | ||
| "Not using the DeepSpeed or torch.distributed launchers, attempting to detect MPI environment..." | ||
| ) | ||
| mpi_discovery() | ||
|
|
||
| if not torch.distributed.is_initialized(): | ||
| logger.info("Initializing torch distributed with backend: {}".format( | ||
| self.dist_backend)) | ||
| torch.distributed.init_process_group(backend=self.dist_backend) | ||
|
|
||
|
|
||
| def mpi_discovery(): | ||
| """ | ||
| Discovery MPI environment and map to relevant torch.distributed state | ||
| """ | ||
| from mpi4py import MPI | ||
| import subprocess | ||
|
|
||
| comm = MPI.COMM_WORLD | ||
| rank = comm.Get_rank() | ||
| world_size = comm.Get_size() | ||
|
|
||
| master_addr = None | ||
| if rank == 0: | ||
| hostname_cmd = ["hostname -I"] | ||
| result = subprocess.check_output(hostname_cmd, shell=True) | ||
| master_addr = result.decode('utf-8').split()[0] | ||
| master_addr = comm.bcast(master_addr, root=0) | ||
|
|
||
| # Determine local rank by assuming hostnames are unique | ||
| proc_name = MPI.Get_processor_name() | ||
| all_procs = comm.allgather(proc_name) | ||
| local_rank = sum([i == proc_name for i in all_procs[:rank]]) | ||
|
|
||
| os.environ['RANK'] = str(rank) | ||
| os.environ['WORLD_SIZE'] = str(world_size) | ||
| os.environ['LOCAL_RANK'] = str(local_rank) | ||
| os.environ['MASTER_ADDR'] = master_addr | ||
| os.environ['MASTER_PORT'] = TORCH_DISTRIBUTED_DEFAULT_PORT | ||
|
|
||
| logger.info( | ||
| "Discovered MPI settings of world_rank={}, local_rank={}, world_size={}, master_addr={}, master_port={}" | ||
| .format(os.environ['RANK'], | ||
| os.environ['LOCAL_RANK'], | ||
| os.environ['WORLD_SIZE'], | ||
| os.environ['MASTER_ADDR'], | ||
| os.environ['MASTER_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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move this message to where we set self.local_rank in line 125?