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] runtime context resource ids getter #26907

Merged
merged 9 commits into from
Jul 24, 2022
Merged
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
2 changes: 1 addition & 1 deletion python/ray/_private/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ def get_gpu_ids():
return assigned_ids


@Deprecated
@Deprecated(message="Use ray.get_runtime_context().assigned_resources instead.")
def get_resource_ids():
"""Get the IDs of the resources that are available to the worker.

Expand Down
15 changes: 14 additions & 1 deletion python/ray/runtime_context.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from typing import Dict, Any

import ray._private.worker
from ray._private.client_mode_hook import client_mode_hook
Expand All @@ -16,7 +17,7 @@ def __init__(self, worker):
assert worker is not None
self.worker = worker

def get(self):
def get(self) -> Dict[str, Any]:
"""Get a dictionary of the current context.

Returns:
Expand Down Expand Up @@ -160,6 +161,18 @@ def should_capture_child_tasks_in_placement_group(self):
"""
return self.worker.should_capture_child_tasks_in_placement_group

@property
def assigned_resources(self):
richardliaw marked this conversation as resolved.
Show resolved Hide resolved
richardliaw marked this conversation as resolved.
Show resolved Hide resolved
"""Get the assigned resources to this worker.

Returns:
A dictionary mapping the name of a resource to a list of pairs, where
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should return a dict mapping from resource name to count. Id of the resource is only meaningful for GPU and we already have a ray.worker.get_gpu_ids() for that purpose. Also feel we should copy get_gpu_ids() into runtime context as well for consistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I updated and added a docstring. Can you take a look?

I also won't address the get_gpu_ids() thing since we should probably have a broader discussion there (get_gpu_ids() is frequently used in multiple libraries?)

each pair consists of the ID of a resource and the fraction of that
resource reserved for this worker.
"""
self.worker.check_connected()
return self.worker.core_worker.resource_ids()

def get_runtime_env_string(self):
"""Get the runtime env string used for the current driver or worker.

Expand Down
2 changes: 1 addition & 1 deletion python/ray/tests/test_basic_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def g():
def f(block, accepted_resources):
true_resources = {
resource: value[0][1]
for resource, value in ray._private.worker.get_resource_ids().items()
for resource, value in ray.get_runtime_context().assigned_resources.items()
}
if block:
ray.get(g.remote())
Expand Down
6 changes: 3 additions & 3 deletions python/ray/tests/test_placement_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def test_placement_group_actor_resource_ids(ray_start_cluster, connect_to_client
@ray.remote(num_cpus=1)
class F:
def f(self):
return ray._private.worker.get_resource_ids()
return ray.get_runtime_context().assigned_resources

cluster = ray_start_cluster
num_nodes = 1
Expand All @@ -391,7 +391,7 @@ def f(self):
def test_placement_group_task_resource_ids(ray_start_cluster, connect_to_client):
@ray.remote(num_cpus=1)
def f():
return ray._private.worker.get_resource_ids()
return ray.get_runtime_context().assigned_resources

cluster = ray_start_cluster
num_nodes = 1
Expand Down Expand Up @@ -423,7 +423,7 @@ def f():
def test_placement_group_hang(ray_start_cluster, connect_to_client):
@ray.remote(num_cpus=1)
def f():
return ray._private.worker.get_resource_ids()
return ray.get_runtime_context().assigned_resources

cluster = ray_start_cluster
num_nodes = 1
Expand Down