-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Ability to initialize distributed backend outside deepspeed runtime #608
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 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
663f00f
refactor distributed init (relates to old PR #276)
jeffra f857fef
remove dead code
jeffra c248980
fix import errors
jeffra d8700f7
fix another import
jeffra 33ec22a
another fix, runnable now
jeffra 0587ae8
fixes for unit test backend
jeffra af901ea
update DSE
jeffra f066482
update mpi documentation
jeffra 5a30339
formatting
jeffra af733a9
fix warnings in cluster install script
jeffra 3ec4f25
add verbose flag
jeffra ffc67df
more verbose logs
jeffra 2df5aa9
switch unit test backend to use deepspeed.init_dist
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
Submodule DeepSpeedExamples
updated
from abb270 to 78d69c
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,8 @@ | ||
| ''' | ||
| Copyright 2020 The Microsoft DeepSpeed Team | ||
| ''' | ||
|
|
||
| ############################################# | ||
| # Torch distributed constants | ||
| ############################################# | ||
| TORCH_DISTRIBUTED_DEFAULT_PORT = 29500 |
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 |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| from deepspeed.utils.logging import logger, log_dist | ||
| from .logging import logger, log_dist | ||
| from .distributed import init_distributed | ||
| from deepspeed.runtime.dataloader import RepeatingLoader |
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,122 @@ | ||
| ''' | ||
| Copyright 2020 The Microsoft DeepSpeed Team | ||
| ''' | ||
| import os | ||
| import torch | ||
|
|
||
| from .logging import logger | ||
| from ..constants import TORCH_DISTRIBUTED_DEFAULT_PORT | ||
|
|
||
|
|
||
| def init_distributed(dist_backend="nccl", | ||
| auto_mpi_discovery=True, | ||
| distributed_port=TORCH_DISTRIBUTED_DEFAULT_PORT): | ||
| """ | ||
| Initialize torch.distributed backend, potentially performing MPI discovery if needed | ||
| Arguments: | ||
| dist_backend (str): torch distributed backend, e.g., nccl, mpi, gloo | ||
| auto_mpi_discovery (bool): if distributed environment variables are not set, attempt to discover them from MPI | ||
| distributed_port (int, optional): torch distributed backend port | ||
| """ | ||
|
|
||
| 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..." | ||
| ) | ||
| if in_aml() and not in_dlts(): | ||
| patch_aml_env_for_torch_nccl_backend() | ||
| else: | ||
| mpi_discovery(distributed_port) | ||
|
|
||
| if not torch.distributed.is_initialized(): | ||
| logger.info( | ||
| "Initializing torch distributed with backend: {}".format(dist_backend)) | ||
| torch.distributed.init_process_group(backend=dist_backend) | ||
|
|
||
|
|
||
| def mpi_discovery(distributed_port=TORCH_DISTRIBUTED_DEFAULT_PORT): | ||
| """ | ||
| Discovery MPI environment via mpi4py 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'] = str(distributed_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'])) | ||
|
|
||
| if torch.distributed.is_initialized(): | ||
| assert dist.get_rank() == rank, "MPI rank {} does not match torch rank {}".format(rank, dist.get_rank()) | ||
| assert dist.get_world_size() == world_size, "MPI world size {} does not match torch world size {}".format( | ||
| world_size, dist.get_world_size()) | ||
|
|
||
|
|
||
| def in_aml(): | ||
| # Are we running inside an Azure Machine Learning (AML) environment? | ||
| return 'AZUREML_EXPERIMENT_ID' in os.environ | ||
|
|
||
|
|
||
| def in_dlts(): | ||
| # Are we running on a DLTS cluster? | ||
| return 'DLTS_JOB_ID' in os.environ | ||
|
|
||
|
|
||
| def patch_aml_env_for_torch_nccl_backend(master_port=6105, verbose=True): | ||
| """Helper routine to get and set environment variables. | ||
| This is adapted from Azure ML's documentation available from: | ||
| https://azure.github.io/azureml-web/docs/cheatsheet/distributed-training/#environment-variables-from-openmpi | ||
| """ | ||
| os.environ["RANK"] = os.environ["OMPI_COMM_WORLD_RANK"] | ||
| os.environ["WORLD_SIZE"] = os.environ["OMPI_COMM_WORLD_SIZE"] | ||
| single_node = int(os.environ["OMPI_COMM_WORLD_LOCAL_SIZE"]) == int( | ||
| os.environ["WORLD_SIZE"]) | ||
|
|
||
| if not single_node: | ||
| master_node_params = os.environ["AZ_BATCH_MASTER_NODE"].split(":") | ||
| os.environ["MASTER_ADDR"] = master_node_params[0] | ||
| # Do not overwrite master port with that defined in AZ_BATCH_MASTER_NODE | ||
| if "MASTER_PORT" not in os.environ: | ||
| os.environ["MASTER_PORT"] = str(master_port) | ||
| else: | ||
| os.environ["MASTER_ADDR"] = os.environ["AZ_BATCHAI_MPI_MASTER_NODE"] | ||
| os.environ["MASTER_PORT"] = "54965" | ||
| logger.info("NCCL_SOCKET_IFNAME original value = {}".format( | ||
|
jeffra marked this conversation as resolved.
Outdated
|
||
| os.environ["NCCL_SOCKET_IFNAME"])) | ||
|
|
||
| os.environ["NCCL_SOCKET_IFNAME"] = "^docker0,lo" | ||
| os.environ['LOCAL_RANK'] = os.environ["OMPI_COMM_WORLD_LOCAL_RANK"] | ||
|
|
||
| if verbose: | ||
| logger.info( | ||
| "Discovered AzureML 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.
Uh oh!
There was an error while loading. Please reload this page.