-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
[core][distributed] fix ray worker rank assignment #6235
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 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 |
---|---|---|
|
@@ -134,11 +134,32 @@ def _init_workers_ray(self, placement_group: "PlacementGroup", | |
worker_node_and_gpu_ids = self._run_workers("get_node_and_gpu_ids", | ||
use_dummy_driver=True) | ||
|
||
node_workers = defaultdict(list) | ||
node_gpus = defaultdict(list) | ||
|
||
for i, (node_id, gpu_ids) in enumerate(worker_node_and_gpu_ids): | ||
node_workers[node_id].append(i) | ||
# the order in `worker_node_and_gpu_ids` does not necessarily match | ||
# the machine boundaries. We need to make sure that workers in the | ||
# same node are assigned consecutive ranks. | ||
# examples: | ||
# [('852a09a13c7503ef126d7c828454c741494b1be33a8627a5206604d9', [0]), ('dfaad7adfdae57a694cc74490db45bd112c9f31243523e43ddc2e7f0', [0]), ('dfaad7adfdae57a694cc74490db45bd112c9f31243523e43ddc2e7f0', [1]), ('dfaad7adfdae57a694cc74490db45bd112c9f31243523e43ddc2e7f0', [2]), ('dfaad7adfdae57a694cc74490db45bd112c9f31243523e43ddc2e7f0', [3]), ('852a09a13c7503ef126d7c828454c741494b1be33a8627a5206604d9', [1]), ('852a09a13c7503ef126d7c828454c741494b1be33a8627a5206604d9', [2]), ('852a09a13c7503ef126d7c828454c741494b1be33a8627a5206604d9', [3])] # noqa | ||
|
||
# initialize worker ranks with -1 (unassigned) | ||
worker_ranks = [-1 for x in worker_node_and_gpu_ids] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can do this simply by using python sort/sorted, but this can be very unreadable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wish ray can sort by node & GPU id for me :( but it cannot |
||
current_rank = 0 | ||
while -1 in worker_ranks: | ||
# whenever we find an unassigned worker, find the node | ||
index = worker_ranks.index(-1) | ||
current_node_id = worker_node_and_gpu_ids[index][0] | ||
# assign ranks to all workers in the same node | ||
for i, (node_id, _) in enumerate(worker_node_and_gpu_ids): | ||
if node_id == current_node_id: | ||
worker_ranks[i] = current_rank | ||
current_rank += 1 | ||
# with the above example, worker_ranks will be [0, 4, 5, 6, 7, 1, 2, 3] | ||
|
||
node_workers = defaultdict(list) # node id -> list of worker ranks | ||
node_gpus = defaultdict(list) # node id -> list of gpu ids | ||
|
||
for worker_rank, (node_id, gpu_ids) in zip(worker_ranks, | ||
worker_node_and_gpu_ids): | ||
node_workers[node_id].append(worker_rank) | ||
# `gpu_ids` can be a list of strings or integers. | ||
# convert them to integers for consistency. | ||
# NOTE: gpu_ids can be larger than 9 (e.g. 16 GPUs), | ||
|
@@ -184,7 +205,8 @@ def _init_workers_ray(self, placement_group: "PlacementGroup", | |
local_rank=node_workers[node_id].index(rank), | ||
rank=rank, | ||
distributed_init_method=distributed_init_method, | ||
) for rank, (node_id, _) in enumerate(worker_node_and_gpu_ids) | ||
) for rank, (node_id, | ||
_) in zip(worker_ranks, worker_node_and_gpu_ids) | ||
] | ||
self._run_workers("init_worker", all_kwargs=init_worker_all_kwargs) | ||
|
||
|
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.
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.
Are we guaranteed that the GPU IDs for a given node will be in strictly increasing order?
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.
I don't think so. this is not needed, either. the
CUDA_VISIBLE_DEVICES
is",".join(map(str, node_gpus[node_id]))
, and local rank isnode_workers[node_id].index(rank)
, as long as these two are consistent, it should be fine.node_gpus[node_id]
: list of gpu ids for each worker in this nodenode_workers[node_id]
: list of worker ranks for each worker in this node