|
| 1 | +from abc import ABC, abstractmethod |
| 2 | +from typing import Dict, Callable, Any, Optional, List |
| 3 | +from dataclasses import dataclass |
| 4 | + |
| 5 | +@dataclass |
| 6 | +class BaseParams: |
| 7 | + cpus_per_worker: int = 1 |
| 8 | + use_gpu: bool = False |
| 9 | + gpus_per_worker: Optional[int] = None |
| 10 | + def __post_init__(self): |
| 11 | + if self.gpus_per_worker and not self.use_gpu: |
| 12 | + raise ValueError("gpus_per_worker is set, but use_gpu is False. " |
| 13 | + "use_gpu must be True if gpus_per_worker is " |
| 14 | + "set. ") |
| 15 | + if self.use_gpu and isinstance(self.gpus_per_worker, |
| 16 | + int) and self.gpus_per_worker < 1: |
| 17 | + raise ValueError( |
| 18 | + f"gpus_per_worker must be >= 1: Got {self.gpus_per_worker}.") |
| 19 | + self.gpus_per_worker = self.gpus_per_worker or int(self.use_gpu) |
| 20 | + |
| 21 | + |
| 22 | +class Adapter(ABC): |
| 23 | + """Adapter for executing Ray calls for various types(e.g. static and elastic) |
| 24 | + Horovod jobs. |
| 25 | + """ |
| 26 | + @abstractmethod |
| 27 | + def start(self, |
| 28 | + executable_cls: type = None, |
| 29 | + executable_args: Optional[List] = None, |
| 30 | + executable_kwargs: Optional[Dict] = None, |
| 31 | + extra_env_vars: Optional[Dict] = None): |
| 32 | + """Starts the Adapter |
| 33 | +
|
| 34 | + Args: |
| 35 | + executable_cls (type): The class that will be created within |
| 36 | + an actor (BaseHorovodWorker). This will allow Horovod |
| 37 | + to establish its connections and set env vars. |
| 38 | + executable_args (List): Arguments to be passed into the |
| 39 | + worker class upon initialization. |
| 40 | + executable_kwargs (Dict): Keyword arguments to be passed into the |
| 41 | + worker class upon initialization. |
| 42 | + extra_env_vars (Dict): Environment variables to be set |
| 43 | + on the actors (worker processes) before initialization. |
| 44 | + """ |
| 45 | + raise NotImplementedError("Method must be implemented in a subclass") |
| 46 | + |
| 47 | + @abstractmethod |
| 48 | + def execute(self, fn: Callable[["executable_cls"], Any], |
| 49 | + callbacks: Optional[List[Callable]] = None) -> List[Any]: |
| 50 | + """Executes the provided function on all workers. |
| 51 | +
|
| 52 | + Args: |
| 53 | + fn: Target function to be invoked on every object. |
| 54 | + callbacks: List of callables. Each callback must either |
| 55 | + be a callable function or a class that implements __call__. |
| 56 | + Every callback will be invoked on every value logged |
| 57 | + by the rank 0 worker. |
| 58 | +
|
| 59 | + Returns: |
| 60 | + Deserialized return values from the target function. |
| 61 | + """ |
| 62 | + raise NotImplementedError("Method must be implemented in a subclass") |
| 63 | + |
| 64 | + @abstractmethod |
| 65 | + def run(self, |
| 66 | + fn: Callable[[Any], Any], |
| 67 | + args: Optional[List] = None, |
| 68 | + kwargs: Optional[Dict] = None, |
| 69 | + callbacks: Optional[List[Callable]] = None) -> List[Any]: |
| 70 | + """Executes the provided function on all workers. |
| 71 | +
|
| 72 | + Args: |
| 73 | + fn: Target function that can be executed with arbitrary |
| 74 | + args and keyword arguments. |
| 75 | + args: List of arguments to be passed into the target function. |
| 76 | + kwargs: Dictionary of keyword arguments to be |
| 77 | + passed into the target function. |
| 78 | + callbacks: List of callables. Each callback must either |
| 79 | + be a callable function or a class that implements __call__. |
| 80 | + Every callback will be invoked on every value logged |
| 81 | + by the rank 0 worker. |
| 82 | +
|
| 83 | + Returns: |
| 84 | + Deserialized return values from the target function. |
| 85 | + """ |
| 86 | + raise NotImplementedError("Method must be implemented in a subclass") |
| 87 | + |
| 88 | + @abstractmethod |
| 89 | + def run_remote(self, |
| 90 | + fn: Callable[[Any], Any], |
| 91 | + args: Optional[List] = None, |
| 92 | + kwargs: Optional[Dict] = None, |
| 93 | + callbacks: Optional[List[Callable]] = None): |
| 94 | + |
| 95 | + """Executes the provided function on all workers. |
| 96 | +
|
| 97 | + Args: |
| 98 | + fn: Target function that can be executed with arbitrary |
| 99 | + args and keyword arguments. |
| 100 | + args: List of arguments to be passed into the target function. |
| 101 | + kwargs: Dictionary of keyword arguments to be |
| 102 | + passed into the target function. |
| 103 | +
|
| 104 | + Returns: |
| 105 | + list: List of ObjectRefs that you can run `ray.get` on to |
| 106 | + retrieve values. |
| 107 | + """ |
| 108 | + raise NotImplementedError("Method must be implemented in a subclass") |
| 109 | + |
| 110 | + @abstractmethod |
| 111 | + def execute_single(self, |
| 112 | + fn: Callable[["executable_cls"], Any]) -> List[Any]: |
| 113 | + """Executes the provided function on the rank 0 worker (chief). |
| 114 | +
|
| 115 | + Args: |
| 116 | + fn: Target function to be invoked on the chief object. |
| 117 | +
|
| 118 | + Returns: |
| 119 | + Deserialized return values from the target function. |
| 120 | + """ |
| 121 | + raise NotImplementedError("Method must be implemented in a subclass") |
| 122 | + |
| 123 | + @abstractmethod |
| 124 | + def shutdown(self): |
| 125 | + """Destroys the adapter.""" |
| 126 | + raise NotImplementedError("Method must be implemented in a subclass") |
0 commit comments