-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
parallel_requests.py
258 lines (226 loc) · 10.4 KB
/
parallel_requests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import logging
from collections import defaultdict
from typing import Any, Callable, Dict, List, Optional, Union
import ray
from ray.actor import ActorHandle
logger = logging.getLogger(__name__)
class AsyncRequestsManager:
"""A manager for asynchronous requests to actors.
Args:
workers: A list of ray remote workers to operate on. These workers must have an
``apply`` method which takes a function and a list of arguments to that
function.
max_remote_requests_in_flight_per_worker: The maximum number of remote
requests that can be in flight per actor. Any requests made to the pool
that cannot be scheduled because the
max_remote_requests_in_flight_per_worker per actor has been reached will
be queued.
ray_wait_timeout_s: The maximum amount of time to wait for inflight requests
to be done and ready when calling
AsyncRequestsManager.get_ready_results().
Example:
>>> import time # doctest: +SKIP
>>> import ray # doctest: +SKIP
>>> from ray.rllib.execution.parallel_requests import AsyncRequestsManager # doctest: +SKIP # noqa
>>>
>>> @ray.remote # doctest: +SKIP
... class MyActor: # doctest: +SKIP
... def apply(self, fn, *args: List[Any], **kwargs: Dict[str, Any]) -> Any: # doctest: +SKIP # noqa
... return fn(*args, **kwargs) # doctest: +SKIP
...
... def task(self, a: int, b: int) -> Any: # doctest: +SKIP
... time.sleep(0.5) # doctest: +SKIP
... return a + b # doctest: +SKIP
>>>
>>> workers = [MyActor.remote() for _ in range(3)] # doctest: +SKIP
>>> manager = AsyncRequestsManager(workers, # doctest: +SKIP
... max_remote_requests_in_flight_per_worker=2) # doctest: +SKIP # noqa
>>> manager.call(lambda worker, a, b: worker.task(a, b), fn_args=[1, 2]) # doctest: +SKIP # noqa
>>> print(manager.get_ready()) # doctest: +SKIP
>>> manager.call(lambda worker, a, b: worker.task(a, b), # doctest: +SKIP
... fn_kwargs={"a": 1, "b": 2}) # doctest: +SKIP
>>> time.sleep(2) # Wait for the tasks to finish. # doctest: +SKIP
>>> print(manager.get_ready()) # doctest: +SKIP
"""
def __init__(
self,
workers: List[ActorHandle],
max_remote_requests_in_flight_per_worker: int = 2,
ray_wait_timeout_s: Optional[float] = 0.0,
return_object_refs: bool = False,
):
self._ray_wait_timeout_s = ray_wait_timeout_s
self._return_object_refs = return_object_refs
self._max_remote_requests_in_flight = max_remote_requests_in_flight_per_worker
self._pending_to_actor = {}
self._pending_remotes = []
self._remote_requests_in_flight = defaultdict(set)
self._all_workers = (
list(workers) if not isinstance(workers, list) else workers.copy()
)
self._curr_actor_ptr = 0
def call(
self,
remote_fn: Callable,
*,
actor: ActorHandle = None,
fn_args: List[Any] = None,
fn_kwargs: Dict[str, Any] = None,
) -> bool:
"""Call remote function on any available Actor or - if provided - on `actor`.
Args:
remote_fn: The remote function to call. The function must have a signature
of: [RolloutWorker, args, kwargs] and return Any.
actor: The actor to call the remote function on.
fn_args: The arguments to pass to the remote function.
fn_kwargs: The keyword arguments to pass to the remote function.
Returns:
True if the remoted_fn was scheduled on an actor. False if it was unable
to be scheduled.
Raises:
ValueError: If actor has not been added to the manager.
ValueError: If there are no actors available to submit a request to.
"""
if actor and actor not in self._all_workers:
raise ValueError(
f"Actor {actor} has not been added to the manager."
f" You must call manager.add_worker(actor) first "
f"before submitting requests to actor."
)
if fn_args is None:
fn_args = []
if fn_kwargs is None:
fn_kwargs = {}
def actor_available(a):
return (
len(self._remote_requests_in_flight[a])
< self._max_remote_requests_in_flight
)
num_workers = len(self._all_workers)
if not actor: # If no actor is specified, use a random actor.
for _ in range(num_workers):
if actor_available(self._all_workers[self._curr_actor_ptr]):
actor = self._all_workers[self._curr_actor_ptr]
self._curr_actor_ptr = (self._curr_actor_ptr + 1) % num_workers
break
self._curr_actor_ptr = (self._curr_actor_ptr + 1) % num_workers
if not actor: # No actors available to schedule the request on.
return False
else:
if not actor_available(actor):
return False
req = actor.apply.remote(remote_fn, *fn_args, **fn_kwargs)
self._remote_requests_in_flight[actor].add(req)
self._pending_to_actor[req] = actor
self._pending_remotes.append(req)
return True
def call_on_all_available(
self,
remote_fn: Callable,
*,
fn_args: List[Any] = None,
fn_kwargs: Dict[str, Any] = None,
) -> int:
"""Call `remote_fn` on all available workers.
Available workers are those that have less than the maximum requests currently
in-flight. The max. requests is set via the constructor's
`max_remote_requests_in_flight_per_worker` argument.
Args:
remote_fn: The remote function to call. The function must have a signature
of: [RolloutWorker, args, kwargs] and return Any.
fn_args: The arguments to pass to the remote function.
fn_kwargs: The keyword arguments to pass to the remote function.
Returns:
The number of remote calls of remote_fn that were able to be launched.
"""
num_launched = 0
for worker in self._all_workers:
launched = self.call(
remote_fn, actor=worker, fn_args=fn_args, fn_kwargs=fn_kwargs
)
num_launched += int(launched)
return num_launched
def get_ready(self) -> Dict[ActorHandle, List[Any]]:
"""Get results that are ready to be returned.
Returns:
A dictionary of actor handles to lists of returns from tasks that were
previously submitted to this actor pool that are now ready to be returned.
If self._return_object_refs is True, return only the object store
references, not the actual return values.
"""
ready_requests_dict = defaultdict(list)
ready_requests, self._pending_remotes = ray.wait(
self._pending_remotes,
timeout=self._ray_wait_timeout_s,
num_returns=len(self._pending_remotes),
)
if not self._return_object_refs:
objs = ray.get(ready_requests)
else:
objs = ready_requests
for req, obj in zip(ready_requests, objs):
actor = self._pending_to_actor[req]
self._remote_requests_in_flight[actor].remove(req)
ready_requests_dict[actor].append(obj)
del self._pending_to_actor[req]
return dict(ready_requests_dict)
def add_workers(self, new_workers: Union[List[ActorHandle], ActorHandle]) -> None:
"""Add a new worker to the manager
Args:
new_workers: The actors to add
"""
if isinstance(new_workers, ActorHandle):
new_workers = [new_workers]
for new_worker in new_workers:
if new_worker not in self._all_workers:
self._all_workers.append(new_worker)
def remove_workers(
self,
workers: Union[List[ActorHandle], ActorHandle],
remove_in_flight_requests: bool = False,
) -> None:
"""Make workers unschedulable and remove them from this manager.
Args:
workers: The actors to remove.
remove_in_flight_requests: If True, will remove the actor completely from
this manager, even all of its in-flight requests. Useful for removing
a worker after some detected failure.
"""
if isinstance(workers, ActorHandle):
workers = [workers]
workers_to_remove = set(workers)
self._all_workers[:] = [
el for el in self._all_workers if el not in workers_to_remove
]
if self._all_workers and (self._curr_actor_ptr >= len(self._all_workers)):
# Move current pointer to the new tail of the list.
self._curr_actor_ptr = len(self._all_workers) - 1
elif not self._all_workers:
self._curr_actor_ptr = 0
# Remove all in-flight requests as well such that nothing related to the
# removed workers remains in this manager.
if remove_in_flight_requests is True:
for worker in workers_to_remove:
# Get all remote requests currently in-flight for this worker ..
if worker in self._remote_requests_in_flight:
# .. and remove all of them from our internal maps/lists.
reqs = self._remote_requests_in_flight[worker]
del self._remote_requests_in_flight[worker]
for req in reqs:
del self._pending_to_actor[req]
if req in self._pending_remotes:
self._pending_remotes.remove(req)
def get_manager_statistics(self) -> Dict[str, Any]:
"""Get statistics about the the manager
Some of the statistics include the number of actors that are available,
the number of pending inflight requests, and the number of pending requests
to be scheduled on the available actors.
Returns:
A dictionary of statistics about the manager.
"""
return {
"num_pending_inflight_requests": len(self._pending_remotes),
}
@property
def workers(self):
return frozenset(self._all_workers)