|
| 1 | +import logging |
| 2 | +import traceback |
| 3 | + |
| 4 | +from orion.core.utils.module_import import ImportOptional |
| 5 | +from orion.executor.base import ( |
| 6 | + AsyncException, |
| 7 | + AsyncResult, |
| 8 | + BaseExecutor, |
| 9 | + ExecutorClosed, |
| 10 | + Future, |
| 11 | +) |
| 12 | + |
| 13 | +with ImportOptional("ray") as import_optional: |
| 14 | + import ray |
| 15 | + |
| 16 | +HAS_RAY = not import_optional.failed |
| 17 | + |
| 18 | +logger = logging.getLogger(__name__) |
| 19 | + |
| 20 | + |
| 21 | +class _Future(Future): |
| 22 | + def __init__(self, future): |
| 23 | + self.future = future |
| 24 | + self.exception = None |
| 25 | + |
| 26 | + def get(self, timeout=None): |
| 27 | + if self.exception: |
| 28 | + raise self.exception |
| 29 | + try: |
| 30 | + return ray.get(self.future, timeout=timeout) |
| 31 | + except ray.exceptions.GetTimeoutError as e: |
| 32 | + raise TimeoutError() from e |
| 33 | + |
| 34 | + def wait(self, timeout=None): |
| 35 | + try: |
| 36 | + ray.get(self.future, timeout=timeout) |
| 37 | + except ray.exceptions.GetTimeoutError: |
| 38 | + pass |
| 39 | + except Exception as e: |
| 40 | + self.exception = e |
| 41 | + |
| 42 | + def ready(self): |
| 43 | + obj_ready = ray.wait([self.future]) |
| 44 | + return len(obj_ready[0]) == 1 |
| 45 | + |
| 46 | + def successful(self): |
| 47 | + # Python 3.6 raise assertion error |
| 48 | + if not self.ready(): |
| 49 | + raise ValueError() |
| 50 | + |
| 51 | + return self.future.successful() |
| 52 | + |
| 53 | + |
| 54 | +class Ray(BaseExecutor): |
| 55 | + def __init__( |
| 56 | + self, |
| 57 | + n_workers=-1, |
| 58 | + **config, |
| 59 | + ): |
| 60 | + super().__init__(n_workers=n_workers) |
| 61 | + self.initialized = False |
| 62 | + if not HAS_RAY: |
| 63 | + raise ImportError("Ray must be installed to use Ray executor.") |
| 64 | + self.config = config |
| 65 | + |
| 66 | + if not ray.is_initialized(): |
| 67 | + ray.init(**self.config) |
| 68 | + self.initialized = True |
| 69 | + logger.debug("Ray was initiated with runtime_env : %s", **config) |
| 70 | + |
| 71 | + def close(self): |
| 72 | + if self.initialized: |
| 73 | + self.initialized = False |
| 74 | + ray.shutdown() |
| 75 | + |
| 76 | + def __del__(self): |
| 77 | + self.close() |
| 78 | + |
| 79 | + def __enter__(self): |
| 80 | + return self |
| 81 | + |
| 82 | + def submit(self, function, *args, **kwargs): |
| 83 | + if not ray.is_initialized(): |
| 84 | + raise ExecutorClosed() |
| 85 | + |
| 86 | + remote_g = ray.remote(function) |
| 87 | + return _Future(remote_g.remote(*args, **kwargs)) |
| 88 | + |
| 89 | + def wait(self, futures): |
| 90 | + return [future.get() for future in futures] |
| 91 | + |
| 92 | + def async_get(self, futures, timeout=None): |
| 93 | + results = [] |
| 94 | + tobe_deleted = [] |
| 95 | + for i, future in enumerate(futures): |
| 96 | + if timeout and i == 0: |
| 97 | + future.wait(timeout) |
| 98 | + |
| 99 | + if future.ready(): |
| 100 | + try: |
| 101 | + results.append(AsyncResult(future, future.get())) |
| 102 | + except Exception as err: |
| 103 | + results.append(AsyncException(future, err, traceback.format_exc())) |
| 104 | + |
| 105 | + tobe_deleted.append(future) |
| 106 | + for future in tobe_deleted: |
| 107 | + futures.remove(future) |
| 108 | + |
| 109 | + return results |
| 110 | + |
| 111 | + def __exit__(self, exc_type, exc_value, traceback): |
| 112 | + self.close() |
| 113 | + super().__exit__(exc_type, exc_value, traceback) |
0 commit comments