__init__.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. # isort: skip_file
  2. from ray._private import log # isort: skip # noqa: F401
  3. import logging
  4. import os
  5. import sys
  6. from typing import TYPE_CHECKING
  7. log.generate_logging_config()
  8. logger = logging.getLogger(__name__)
  9. def _configure_system():
  10. import os
  11. import platform
  12. import sys
  13. """Wraps system configuration to avoid 'leaking' variables into ray."""
  14. # Sanity check pickle5 if it has been installed.
  15. if "pickle5" in sys.modules:
  16. if sys.version_info >= (3, 8):
  17. logger.warning(
  18. "Package pickle5 becomes unnecessary in Python 3.8 and above. "
  19. "Its presence may confuse libraries including Ray. "
  20. "Please uninstall the package."
  21. )
  22. import importlib.metadata
  23. try:
  24. version_str = importlib.metadata.version("pickle5")
  25. version = tuple(int(n) for n in version_str.split("."))
  26. if version < (0, 0, 10):
  27. logger.warning(
  28. "Although not used by Ray, a version of pickle5 that leaks memory "
  29. "is found in the environment. Please run 'pip install pickle5 -U' "
  30. "to upgrade."
  31. )
  32. except importlib.metadata.PackageNotFoundError:
  33. logger.warning(
  34. "You are using the 'pickle5' module, but "
  35. "the exact version is unknown (possibly carried as "
  36. "an internal component by another module). Please "
  37. "make sure you are using pickle5 >= 0.0.10 because "
  38. "previous versions may leak memory."
  39. )
  40. # Importing psutil. Must be before ray._raylet is
  41. # initialized.
  42. thirdparty_files = os.path.join(
  43. os.path.abspath(os.path.dirname(__file__)), "thirdparty_files"
  44. )
  45. sys.path.insert(0, thirdparty_files)
  46. if (
  47. platform.system() == "Linux"
  48. and "Microsoft".lower() in platform.release().lower()
  49. ):
  50. from ray._private import compat # noqa: E402
  51. compat.patch_psutil()
  52. # Expose ray ABI symbols which may be dependent by other shared
  53. # libraries such as _streaming.so. See BUILD.bazel:_raylet
  54. python_shared_lib_suffix = ".so" if sys.platform != "win32" else ".pyd"
  55. so_path = os.path.join(
  56. os.path.dirname(__file__), "_raylet" + python_shared_lib_suffix
  57. )
  58. if os.path.exists(so_path):
  59. import ctypes
  60. from ctypes import CDLL
  61. CDLL(so_path, ctypes.RTLD_GLOBAL)
  62. _configure_system()
  63. # Delete configuration function.
  64. del _configure_system
  65. from ray import _version # noqa: E402
  66. __commit__ = _version.commit
  67. __version__ = _version.version
  68. import ray._raylet # noqa: E402
  69. from ray._raylet import ( # noqa: E402,F401
  70. ActorClassID,
  71. ActorID,
  72. NodeID,
  73. Config as _Config,
  74. JobID,
  75. WorkerID,
  76. FunctionID,
  77. ObjectID,
  78. ObjectRef,
  79. ObjectRefGenerator,
  80. DynamicObjectRefGenerator,
  81. TaskID,
  82. UniqueID,
  83. Language,
  84. PlacementGroupID,
  85. ClusterID,
  86. )
  87. _config = _Config()
  88. from ray._private.state import ( # noqa: E402,F401
  89. nodes,
  90. timeline,
  91. cluster_resources,
  92. available_resources,
  93. )
  94. from ray._private.worker import ( # noqa: E402,F401
  95. LOCAL_MODE,
  96. SCRIPT_MODE,
  97. WORKER_MODE,
  98. RESTORE_WORKER_MODE,
  99. SPILL_WORKER_MODE,
  100. cancel,
  101. get,
  102. get_actor,
  103. get_gpu_ids,
  104. init,
  105. is_initialized,
  106. put,
  107. kill,
  108. remote,
  109. shutdown,
  110. wait,
  111. )
  112. from ray._private.ray_logging.logging_config import LoggingConfig # noqa: E402
  113. # We import ray.actor because some code is run in actor.py which initializes
  114. # some functions in the worker.
  115. import ray.actor # noqa: E402,F401
  116. from ray.actor import method # noqa: E402,F401
  117. # TODO(qwang): We should remove this exporting in Ray2.0.
  118. from ray.cross_language import java_function, java_actor_class # noqa: E402,F401
  119. from ray.runtime_context import get_runtime_context # noqa: E402,F401
  120. from ray import internal # noqa: E402,F401
  121. from ray import util # noqa: E402,F401
  122. from ray import _private # noqa: E402,F401
  123. # We import ClientBuilder so that modules can inherit from `ray.ClientBuilder`.
  124. from ray.client_builder import client, ClientBuilder # noqa: E402,F401
  125. class _DeprecationWrapper:
  126. def __init__(self, name, real_worker):
  127. self._name = name
  128. self._real_worker = real_worker
  129. self._warned = set()
  130. def __getattr__(self, attr):
  131. value = getattr(self._real_worker, attr)
  132. if attr not in self._warned:
  133. self._warned.add(attr)
  134. logger.warning(
  135. f"DeprecationWarning: `ray.{self._name}.{attr}` is a private "
  136. "attribute and access will be removed in a future Ray version."
  137. )
  138. return value
  139. # TODO(ekl) remove this entirely after 3rd party libraries are all migrated.
  140. worker = _DeprecationWrapper("worker", ray._private.worker)
  141. ray_constants = _DeprecationWrapper("ray_constants", ray._private.ray_constants)
  142. serialization = _DeprecationWrapper("serialization", ray._private.serialization)
  143. state = _DeprecationWrapper("state", ray._private.state)
  144. # Pulic Ray APIs
  145. __all__ = [
  146. "__version__",
  147. "_config",
  148. "get_runtime_context",
  149. "autoscaler",
  150. "available_resources",
  151. "cancel",
  152. "client",
  153. "ClientBuilder",
  154. "cluster_resources",
  155. "get",
  156. "get_actor",
  157. "get_gpu_ids",
  158. "init",
  159. "is_initialized",
  160. "java_actor_class",
  161. "java_function",
  162. "cpp_function",
  163. "kill",
  164. "Language",
  165. "method",
  166. "nodes",
  167. "put",
  168. "remote",
  169. "shutdown",
  170. "timeline",
  171. "wait",
  172. "LOCAL_MODE",
  173. "SCRIPT_MODE",
  174. "WORKER_MODE",
  175. "LoggingConfig",
  176. ]
  177. # Public APIs that should automatically trigger ray.init().
  178. AUTO_INIT_APIS = {
  179. "cancel",
  180. "get",
  181. "get_actor",
  182. "get_gpu_ids",
  183. "kill",
  184. "put",
  185. "wait",
  186. "get_runtime_context",
  187. }
  188. # Public APIs that should not automatically trigger ray.init().
  189. NON_AUTO_INIT_APIS = {
  190. "ClientBuilder",
  191. "LOCAL_MODE",
  192. "Language",
  193. "SCRIPT_MODE",
  194. "WORKER_MODE",
  195. "__version__",
  196. "_config",
  197. "autoscaler",
  198. "available_resources",
  199. "client",
  200. "cluster_resources",
  201. "cpp_function",
  202. "init",
  203. "is_initialized",
  204. "java_actor_class",
  205. "java_function",
  206. "method",
  207. "nodes",
  208. "remote",
  209. "shutdown",
  210. "timeline",
  211. "LoggingConfig",
  212. }
  213. assert set(__all__) == AUTO_INIT_APIS | NON_AUTO_INIT_APIS
  214. from ray._private.auto_init_hook import wrap_auto_init_for_all_apis # noqa: E402
  215. wrap_auto_init_for_all_apis(AUTO_INIT_APIS)
  216. del wrap_auto_init_for_all_apis
  217. # Subpackages
  218. __all__ += [
  219. "actor",
  220. "autoscaler",
  221. "data",
  222. "internal",
  223. "util",
  224. "widgets",
  225. "workflow",
  226. ]
  227. # ID types
  228. __all__ += [
  229. "ActorClassID",
  230. "ActorID",
  231. "NodeID",
  232. "JobID",
  233. "WorkerID",
  234. "FunctionID",
  235. "ObjectID",
  236. "ObjectRef",
  237. "ObjectRefGenerator",
  238. "DynamicObjectRefGenerator",
  239. "TaskID",
  240. "UniqueID",
  241. "PlacementGroupID",
  242. ]
  243. # Delay importing of expensive, isolated subpackages. Note that for proper type
  244. # checking support these imports must be kept in sync between type checking and
  245. # runtime behavior.
  246. if TYPE_CHECKING:
  247. from ray import autoscaler
  248. from ray import data
  249. from ray import workflow
  250. else:
  251. def __getattr__(name: str):
  252. import importlib
  253. if name in ["data", "workflow", "autoscaler"]:
  254. return importlib.import_module("." + name, __name__)
  255. raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
  256. del os
  257. del logging
  258. del sys
  259. del TYPE_CHECKING