This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from PrefectHQ/resources_context_manager
Add resources context manager to set CPUs/GPUs for task
- Loading branch information
Showing
5 changed files
with
134 additions
and
8 deletions.
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
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
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
""" | ||
Contexts to manage Ray clusters and tasks. | ||
""" | ||
|
||
from contextlib import contextmanager | ||
from typing import Any, Dict | ||
|
||
from prefect.context import ContextModel, ContextVar | ||
from pydantic import Field | ||
|
||
|
||
class RemoteOptionsContext(ContextModel): | ||
""" | ||
The context for Ray remote_options management. | ||
Attributes: | ||
current_remote_options: A set of current remote_options in the context. | ||
""" | ||
|
||
current_remote_options: Dict[str, Any] = Field(default_factory=dict) | ||
|
||
@classmethod | ||
def get(cls) -> "RemoteOptionsContext": | ||
""" | ||
Return an empty `RemoteOptionsContext` | ||
instead of `None` if no context exists. | ||
""" | ||
return cls.__var__.get(RemoteOptionsContext()) | ||
|
||
__var__ = ContextVar("remote_options") | ||
|
||
|
||
@contextmanager | ||
def remote_options(**new_remote_options: Dict[str, Any]) -> Dict[str, Any]: | ||
""" | ||
Context manager to add keyword arguments to Ray `@remote` calls | ||
for task runs. If contexts are nested, new options are merged with options | ||
in the outer context. If a key is present in both, the new option will be used. | ||
Yields: | ||
The current set of remote options. | ||
Examples: | ||
Use 4 CPUs and 2 GPUs for the `process` task: | ||
```python | ||
from prefect import flow, task | ||
from prefect_ray.task_runners import RayTaskRunner | ||
from prefect_ray.context import remote_options | ||
@task | ||
def process(x): | ||
return x + 1 | ||
@flow(task_runner=RayTaskRunner()) | ||
def my_flow(): | ||
# equivalent to setting @ray.remote(num_cpus=4, num_gpus=2) | ||
with remote_options(num_cpus=4, num_gpus=2): | ||
process.submit(42) | ||
``` | ||
""" | ||
current_remote_options = RemoteOptionsContext.get().current_remote_options | ||
with RemoteOptionsContext( | ||
current_remote_options={**current_remote_options, **new_remote_options} | ||
): | ||
yield |
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
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from prefect_ray.context import RemoteOptionsContext, remote_options | ||
|
||
|
||
def test_remote_options_context(): | ||
input_remote_options = {"num_cpus": 4} | ||
current_remote_options = RemoteOptionsContext( | ||
current_remote_options=input_remote_options | ||
).current_remote_options | ||
assert current_remote_options == input_remote_options | ||
|
||
|
||
def test_remote_options_context_get(): | ||
current_remote_options = RemoteOptionsContext.get().current_remote_options | ||
assert current_remote_options == {} | ||
|
||
|
||
def test_remote_options(): | ||
with remote_options(num_cpus=4, num_gpus=None): | ||
current_remote_options = RemoteOptionsContext.get().current_remote_options | ||
assert current_remote_options == {"num_cpus": 4, "num_gpus": None} | ||
|
||
|
||
def test_remote_options_empty(): | ||
with remote_options(): | ||
current_remote_options = RemoteOptionsContext.get().current_remote_options | ||
assert current_remote_options == {} | ||
|
||
|
||
def test_remote_options_override(): | ||
with remote_options(num_cpus=2, num_gpus=1): | ||
with remote_options(num_cpus=4, num_gpus=2): | ||
current_remote_options = RemoteOptionsContext.get().current_remote_options | ||
assert current_remote_options == {"num_cpus": 4, "num_gpus": 2} |