Skip to content
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] Fix incorrect gpu ids if placement group bundle index specified #44385

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions python/ray/_private/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,13 @@ def get_gpu_ids() -> Union[List[int], List[str]]:
"""
worker = global_worker
worker.check_connected()
# respect placement_group_bundle_index if specified.
gpu_ids = worker.get_accelerator_ids_for_accelerator_resource(
ray_constants.GPU, f"^{ray_constants.GPU}_group_[0-9]+_[0-9A-Za-z]+$"
)
if not gpu_ids:
return gpu_ids

return worker.get_accelerator_ids_for_accelerator_resource(
ray_constants.GPU, f"^{ray_constants.GPU}_group_[0-9A-Za-z]+$"
)
Expand Down Expand Up @@ -2671,9 +2678,9 @@ def get(
port=None,
patch_stdstreams=False,
quiet=None,
breakpoint_uuid=debugger_breakpoint.decode()
if debugger_breakpoint
else None,
breakpoint_uuid=(
debugger_breakpoint.decode() if debugger_breakpoint else None
),
debugger_external=worker.ray_debugger_external,
)
rdb.set_trace(frame=frame)
Expand Down
8 changes: 7 additions & 1 deletion python/ray/runtime_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,16 @@ def get_accelerator_ids(self) -> Dict[str, List[str]]:
for (
accelerator_resource_name
) in ray._private.accelerators.get_all_accelerator_resource_names():
# respect placement_group_bundle_index if specified.
accelerator_ids = worker.get_accelerator_ids_for_accelerator_resource(
accelerator_resource_name,
f"^{accelerator_resource_name}_group_[0-9A-Za-z]+$",
f"^{accelerator_resource_name}_group_[0-9]+_[0-9A-Za-z]+$",
)
if not accelerator_ids:
accelerator_ids = worker.get_accelerator_ids_for_accelerator_resource(
accelerator_resource_name,
f"^{accelerator_resource_name}_group_[0-9A-Za-z]+$",
)
ids_dict[accelerator_resource_name] = [str(id) for id in accelerator_ids]
return ids_dict

Expand Down
44 changes: 44 additions & 0 deletions python/ray/tests/test_actor_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import ray
import ray.cluster_utils
from ray.util.placement_group import placement_group, PlacementGroupSchedulingStrategy


def test_actor_deletion_with_gpus(shutdown_only):
Expand Down Expand Up @@ -678,6 +679,49 @@ def get_cuda_visible_devices(self):
assert ray.get(actor.get_cuda_visible_devices.remote()) == "0,1"


def test_actor_cuda_visible_devices_placement_group_bundle_index(shutdown_only):
ray.init(num_cpus=4, num_gpus=2)

@ray.remote
class Actor:
def __init__(self) -> None:
self.gpu_ids = ray.get_gpu_ids()

def get_cuda_visible_devices(self):
gpu_ids = os.environ["CUDA_VISIBLE_DEVICES"]
assert int(gpu_ids) == self.gpu_ids[0]
return gpu_ids

bundles = [{"GPU": 1, "CPU": 2}] * 2
pg = placement_group(bundles)
pg.ready()

m1 = [
Actor.options(
num_gpus=0.1,
scheduling_strategy=PlacementGroupSchedulingStrategy(
placement_group=pg, placement_group_bundle_index=i
),
).remote()
for i in range(2)
]

m2 = [
Actor.options(
num_gpus=0.1,
scheduling_strategy=PlacementGroupSchedulingStrategy(
placement_group=pg, placement_group_bundle_index=i
),
).remote()
for i in range(2)
]

assert ray.get(m1[0].get_cuda_visible_devices.remote()) == "0"
assert ray.get(m1[1].get_cuda_visible_devices.remote()) == "1"
assert ray.get(m2[0].get_cuda_visible_devices.remote()) == "0"
assert ray.get(m2[1].get_cuda_visible_devices.remote()) == "1"


if __name__ == "__main__":
import pytest

Expand Down
Loading