-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathexecutors.py
More file actions
511 lines (443 loc) · 17.6 KB
/
executors.py
File metadata and controls
511 lines (443 loc) · 17.6 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
from __future__ import annotations
import asyncio
import contextlib
import logging
import signal
import threading
import time
import traceback
from contextlib import contextmanager
from enum import Enum
from typing import (
Any,
Callable,
Coroutine,
Generator,
List,
Optional,
Protocol,
Sequence,
Tuple,
Union,
)
from tqdm.auto import tqdm
from arize.experimental.datasets.experiments.evaluators.exceptions import (
ArizeException,
)
logger = logging.getLogger(__name__)
class Unset:
pass
_unset = Unset()
class ExecutionStatus(Enum):
DID_NOT_RUN = "DID NOT RUN"
COMPLETED = "COMPLETED"
COMPLETED_WITH_RETRIES = "COMPLETED WITH RETRIES"
FAILED = "FAILED"
class ExecutionDetails:
def __init__(self) -> None:
self.exceptions: List[Exception] = []
self.status = ExecutionStatus.DID_NOT_RUN
self.execution_seconds: float = 0
def fail(self) -> None:
self.status = ExecutionStatus.FAILED
def complete(self) -> None:
if self.exceptions:
self.status = ExecutionStatus.COMPLETED_WITH_RETRIES
else:
self.status = ExecutionStatus.COMPLETED
def log_exception(self, exc: Exception) -> None:
self.exceptions.append(exc)
def log_runtime(self, start_time: float) -> None:
self.execution_seconds += time.time() - start_time
class Executor(Protocol):
def run(
self, inputs: Sequence[Any]
) -> Tuple[List[Any], List[ExecutionDetails]]: ...
class AsyncExecutor(Executor):
"""
A class that provides asynchronous execution of tasks using a producer-consumer pattern.
An async interface is provided by the `execute` method, which returns a coroutine, and a sync
interface is provided by the `run` method.
Args:
generation_fn (Callable[[Any], Coroutine[Any, Any, Any]]): A coroutine function that
generates tasks to be executed.
concurrency (int, optional): The number of concurrent consumers. Defaults to 3.
tqdm_bar_format (Optional[str], optional): The format string for the progress bar.
Defaults to None.
max_retries (int, optional): The maximum number of times to retry on exceptions.
Defaults to 10.
exit_on_error (bool, optional): Whether to exit execution on the first encountered error.
Defaults to True.
fallback_return_value (Union[Unset, Any], optional): The fallback return value for tasks
that encounter errors. Defaults to _unset.
termination_signal (signal.Signals, optional): The signal handled to terminate the executor.
timeout (float, optional): The timeout in seconds for each task execution. Defaults to 120.
"""
def __init__(
self,
generation_fn: Callable[[Any], Coroutine[Any, Any, Any]],
concurrency: int = 3,
tqdm_bar_format: Optional[str] = None,
max_retries: int = 10,
exit_on_error: bool = True,
fallback_return_value: Union[Unset, Any] = _unset,
termination_signal: signal.Signals = signal.SIGINT,
timeout: float = 120,
):
self.generate = generation_fn
self.fallback_return_value = fallback_return_value
self.concurrency = concurrency
self.tqdm_bar_format = tqdm_bar_format
self.max_retries = max_retries
self.exit_on_error = exit_on_error
self.base_priority = 0
self.termination_signal = termination_signal
self.timeout = timeout
async def producer(
self,
inputs: Sequence[Any],
queue: asyncio.PriorityQueue[Tuple[int, Any]],
max_fill: int,
done_producing: asyncio.Event,
termination_signal: asyncio.Event,
) -> None:
try:
for index, input in enumerate(inputs):
if termination_signal.is_set():
break
while queue.qsize() >= max_fill:
# keep room in the queue for requeues
await asyncio.sleep(1)
await queue.put((self.base_priority, (index, input)))
finally:
done_producing.set()
async def consumer(
self,
outputs: List[Any],
execution_details: List[ExecutionDetails],
queue: asyncio.PriorityQueue[Tuple[int, Any]],
done_producing: asyncio.Event,
termination_event: asyncio.Event,
progress_bar: tqdm[Any],
) -> None:
termination_event_watcher = None
while True:
marked_done = False
try:
priority, item = await asyncio.wait_for(queue.get(), timeout=1)
except asyncio.TimeoutError:
if done_producing.is_set() and queue.empty():
break
continue
if termination_event.is_set():
# discard any remaining items in the queue
queue.task_done()
marked_done = True
continue
index, payload = item
try:
task_start_time = time.time()
generate_task = asyncio.create_task(self.generate(payload))
termination_event_watcher = asyncio.create_task(
termination_event.wait()
)
done, pending = await asyncio.wait(
[generate_task, termination_event_watcher],
timeout=self.timeout,
return_when=asyncio.FIRST_COMPLETED,
)
if generate_task in done:
outputs[index] = generate_task.result()
execution_details[index].complete()
execution_details[index].log_runtime(task_start_time)
progress_bar.update()
elif termination_event.is_set():
# discard the pending task and remaining items in the queue
if not generate_task.done():
generate_task.cancel()
# Handle the cancellation exception
with contextlib.suppress(asyncio.CancelledError):
# allow any cleanup to finish for the cancelled task
await generate_task
queue.task_done()
marked_done = True
continue
else:
tqdm.write("Worker timeout, requeuing")
# task timeouts are requeued at the same priority
await queue.put((priority, item))
execution_details[index].log_runtime(task_start_time)
except Exception as exc:
execution_details[index].log_exception(exc)
execution_details[index].log_runtime(task_start_time)
is_arize_exception = isinstance(exc, ArizeException)
if (
retry_count := abs(priority)
) < self.max_retries and not is_arize_exception:
tqdm.write(
f"Exception in worker on attempt {retry_count + 1}: raised {repr(exc)}"
)
tqdm.write("Requeuing...")
await queue.put((priority - 1, item))
else:
execution_details[index].fail()
tqdm.write(
f"Retries exhausted after {retry_count + 1} attempts: {traceback.format_exc()}"
)
if self.exit_on_error:
termination_event.set()
else:
progress_bar.update()
finally:
if not marked_done:
queue.task_done()
if (
termination_event_watcher
and not termination_event_watcher.done()
):
termination_event_watcher.cancel()
async def execute(
self, inputs: Sequence[Any]
) -> Tuple[List[Any], List[ExecutionDetails]]:
termination_event = asyncio.Event()
def termination_handler(signum: int, frame: Any) -> None:
termination_event.set()
tqdm.write(
"Process was interrupted. The return value will be incomplete..."
)
original_handler = signal.signal(
self.termination_signal, termination_handler
)
outputs = [self.fallback_return_value] * len(inputs)
execution_details = [ExecutionDetails() for _ in range(len(inputs))]
progress_bar = tqdm(total=len(inputs), bar_format=self.tqdm_bar_format)
max_queue_size = (
5 * self.concurrency
) # limit the queue to bound memory usage
max_fill = max_queue_size - (
2 * self.concurrency
) # ensure there is always room to requeue
queue: asyncio.PriorityQueue[Tuple[int, Any]] = asyncio.PriorityQueue(
maxsize=max_queue_size
)
done_producing = asyncio.Event()
producer = asyncio.create_task(
self.producer(
inputs, queue, max_fill, done_producing, termination_event
)
)
consumers = [
asyncio.create_task(
self.consumer(
outputs,
execution_details,
queue,
done_producing,
termination_event,
progress_bar,
)
)
for _ in range(self.concurrency)
]
await asyncio.gather(producer, *consumers)
join_task = asyncio.create_task(queue.join())
termination_event_watcher = asyncio.create_task(
termination_event.wait()
)
done, pending = await asyncio.wait(
[join_task, termination_event_watcher],
return_when=asyncio.FIRST_COMPLETED,
)
if termination_event_watcher in done:
# Cancel all tasks
if not join_task.done():
join_task.cancel()
if not producer.done():
producer.cancel()
for task in consumers:
if not task.done():
task.cancel()
if not termination_event_watcher.done():
termination_event_watcher.cancel()
# reset the SIGTERM handler
signal.signal(
self.termination_signal, original_handler
) # reset the SIGTERM handler
return outputs, execution_details
def run(
self, inputs: Sequence[Any]
) -> Tuple[List[Any], List[ExecutionDetails]]:
return asyncio.run(self.execute(inputs))
class SyncExecutor(Executor):
"""
Synchronous executor for generating outputs from inputs using a given generation function.
Args:
generation_fn (Callable[[Any], Any]): The generation function that takes an input and
returns an output.
tqdm_bar_format (Optional[str], optional): The format string for the progress bar. Defaults
to None.
max_retries (int, optional): The maximum number of times to retry on exceptions. Defaults to
10.
exit_on_error (bool, optional): Whether to exit execution on the first encountered error.
Defaults to True.
fallback_return_value (Union[Unset, Any], optional): The fallback return value for tasks
that encounter errors. Defaults to _unset.
"""
def __init__(
self,
generation_fn: Callable[[Any], Any],
tqdm_bar_format: Optional[str] = None,
max_retries: int = 10,
exit_on_error: bool = True,
fallback_return_value: Union[Unset, Any] = _unset,
termination_signal: Optional[signal.Signals] = signal.SIGINT,
):
self.generate = generation_fn
self.fallback_return_value = fallback_return_value
self.tqdm_bar_format = tqdm_bar_format
self.max_retries = max_retries
self.exit_on_error = exit_on_error
self.termination_signal = termination_signal
self._TERMINATE = False
def _signal_handler(self, signum: int, frame: Any) -> None:
tqdm.write(
"Process was interrupted. The return value will be incomplete..."
)
self._TERMINATE = True
@contextmanager
def _executor_signal_handling(
self, signum: Optional[int]
) -> Generator[None, None, None]:
original_handler = None
if signum is not None:
original_handler = signal.signal(signum, self._signal_handler)
try:
yield
finally:
signal.signal(signum, original_handler)
else:
yield
def run(self, inputs: Sequence[Any]) -> Tuple[List[Any], List[Any]]:
with self._executor_signal_handling(self.termination_signal):
outputs = [self.fallback_return_value] * len(inputs)
execution_details: List[ExecutionDetails] = [
ExecutionDetails() for _ in range(len(inputs))
]
progress_bar = tqdm(
total=len(inputs), bar_format=self.tqdm_bar_format
)
for index, input in enumerate(inputs):
task_start_time = time.time()
try:
for attempt in range(self.max_retries + 1):
if self._TERMINATE:
return outputs, execution_details
try:
result = self.generate(input)
outputs[index] = result
execution_details[index].complete()
progress_bar.update()
break
except Exception as exc:
execution_details[index].log_exception(exc)
is_arize_exception = isinstance(exc, ArizeException)
if (
attempt >= self.max_retries
or is_arize_exception
):
raise exc
else:
tqdm.write(
f"Exception in worker on attempt {attempt + 1}: {exc}"
)
tqdm.write("Retrying...")
except Exception as exc:
execution_details[index].fail()
tqdm.write(
f"Retries exhausted after {attempt + 1} attempts: {exc}"
)
if self.exit_on_error:
return outputs, execution_details
else:
progress_bar.update()
finally:
execution_details[index].log_runtime(task_start_time)
return outputs, execution_details
def get_executor_on_sync_context(
sync_fn: Callable[[Any], Any],
async_fn: Callable[[Any], Coroutine[Any, Any, Any]],
run_sync: bool = False,
concurrency: int = 3,
tqdm_bar_format: Optional[str] = None,
max_retries: int = 10,
exit_on_error: bool = True,
fallback_return_value: Union[Unset, Any] = _unset,
timeout: float = 120,
) -> Executor:
if threading.current_thread() is not threading.main_thread():
# run evals synchronously if not in the main thread
if run_sync is False:
logger.warning(
"Async evals execution is not supported in non-main threads. Falling back to sync."
)
return SyncExecutor(
sync_fn,
tqdm_bar_format=tqdm_bar_format,
exit_on_error=exit_on_error,
max_retries=max_retries,
fallback_return_value=fallback_return_value,
termination_signal=None,
)
if run_sync is True:
return SyncExecutor(
sync_fn,
tqdm_bar_format=tqdm_bar_format,
max_retries=max_retries,
exit_on_error=exit_on_error,
fallback_return_value=fallback_return_value,
)
if _running_event_loop_exists():
if getattr(asyncio, "_nest_patched", False):
return AsyncExecutor(
async_fn,
concurrency=concurrency,
tqdm_bar_format=tqdm_bar_format,
max_retries=max_retries,
exit_on_error=exit_on_error,
fallback_return_value=fallback_return_value,
timeout=timeout,
)
else:
logger.warning(
"🐌!! If running inside a notebook, patching the event loop with "
"nest_asyncio will allow asynchronous eval submission, and is significantly "
"faster. To patch the event loop, run `nest_asyncio.apply()`."
)
return SyncExecutor(
sync_fn,
tqdm_bar_format=tqdm_bar_format,
max_retries=max_retries,
exit_on_error=exit_on_error,
fallback_return_value=fallback_return_value,
)
else:
return AsyncExecutor(
async_fn,
concurrency=concurrency,
tqdm_bar_format=tqdm_bar_format,
max_retries=max_retries,
exit_on_error=exit_on_error,
fallback_return_value=fallback_return_value,
timeout=timeout,
)
def _running_event_loop_exists() -> bool:
"""
Checks for a running event loop.
Returns:
bool: True if a running event loop exists, False otherwise.
"""
try:
asyncio.get_running_loop()
return True
except RuntimeError:
return False