__init__.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. # mypy: allow-untyped-defs
  2. r"""
  3. This package introduces support for the XPU backend, specifically tailored for
  4. Intel GPU optimization.
  5. This package is lazily initialized, so you can always import it, and use
  6. :func:`is_available()` to determine if your system supports XPU.
  7. """
  8. import threading
  9. import traceback
  10. from functools import lru_cache
  11. from typing import Any, Callable, Optional, Union
  12. import torch
  13. import torch._C
  14. from torch import device as _device
  15. from torch._utils import _dummy_type, _LazySeedTracker
  16. from ._utils import _get_device_index
  17. from .streams import Event, Stream
  18. _initialized = False
  19. _tls = threading.local()
  20. _initialization_lock = threading.Lock()
  21. _queued_calls: list[
  22. tuple[Callable[[], None], list[str]]
  23. ] = [] # don't invoke these until initialization occurs
  24. _is_in_bad_fork = getattr(torch._C, "_xpu_isInBadFork", lambda: False)
  25. _device_t = Union[_device, str, int, None]
  26. _lazy_seed_tracker = _LazySeedTracker()
  27. default_generators: tuple[torch._C.Generator] = () # type: ignore[assignment]
  28. def _is_compiled() -> bool:
  29. r"""Return true if compile with XPU support."""
  30. return torch._C._has_xpu
  31. if _is_compiled():
  32. _XpuDeviceProperties = torch._C._XpuDeviceProperties
  33. _exchange_device = torch._C._xpu_exchangeDevice
  34. _maybe_exchange_device = torch._C._xpu_maybeExchangeDevice
  35. else:
  36. # Define dummy if PyTorch was compiled without XPU
  37. _XpuDeviceProperties = _dummy_type("_XpuDeviceProperties") # type: ignore[assignment, misc]
  38. def _exchange_device(device: int) -> int:
  39. raise NotImplementedError("PyTorch was compiled without XPU support")
  40. def _maybe_exchange_device(device: int) -> int:
  41. raise NotImplementedError("PyTorch was compiled without XPU support")
  42. @lru_cache(maxsize=1)
  43. def device_count() -> int:
  44. r"""Return the number of XPU device available."""
  45. if not _is_compiled():
  46. return 0
  47. return torch._C._xpu_getDeviceCount()
  48. def is_available() -> bool:
  49. r"""Return a bool indicating if XPU is currently available."""
  50. # This function never throws.
  51. return device_count() > 0
  52. def is_bf16_supported(including_emulation: bool = True) -> bool:
  53. r"""Return a bool indicating if the current XPU device supports dtype bfloat16."""
  54. if not is_available():
  55. return False
  56. return (
  57. including_emulation
  58. or torch.xpu.get_device_properties().has_bfloat16_conversions
  59. )
  60. def is_initialized():
  61. r"""Return whether PyTorch's XPU state has been initialized."""
  62. return _initialized and not _is_in_bad_fork()
  63. def _lazy_call(callable, **kwargs):
  64. if is_initialized():
  65. callable()
  66. else:
  67. global _lazy_seed_tracker
  68. if kwargs.get("seed_all", False):
  69. _lazy_seed_tracker.queue_seed_all(callable, traceback.format_stack())
  70. elif kwargs.get("seed", False):
  71. _lazy_seed_tracker.queue_seed(callable, traceback.format_stack())
  72. else:
  73. # Don't store the actual traceback to avoid memory cycle
  74. _queued_calls.append((callable, traceback.format_stack()))
  75. def init():
  76. r"""Initialize PyTorch's XPU state.
  77. This is a Python API about lazy initialization that avoids initializing
  78. XPU until the first time it is accessed. Does nothing if the XPU state is
  79. already initialized.
  80. """
  81. _lazy_init()
  82. def _lazy_init():
  83. global _initialized, _queued_calls
  84. if is_initialized() or hasattr(_tls, "is_initializing"):
  85. return
  86. with _initialization_lock:
  87. # This test was was protected via GIL. Double-check whether XPU has
  88. # already been initialized.
  89. if is_initialized():
  90. return
  91. # Stop promptly upon encountering a bad fork error.
  92. if _is_in_bad_fork():
  93. raise RuntimeError(
  94. "Cannot re-initialize XPU in forked subprocess. To use XPU with "
  95. "multiprocessing, you must use the 'spawn' start method"
  96. )
  97. if not _is_compiled():
  98. raise AssertionError("Torch not compiled with XPU enabled")
  99. # This function inits XPU backend and detects bad fork processing.
  100. torch._C._xpu_init()
  101. # Some of the queued calls may reentrantly call _lazy_init(); We need to
  102. # just return without initializing in that case.
  103. _tls.is_initializing = True
  104. _queued_calls.extend(calls for calls in _lazy_seed_tracker.get_calls() if calls)
  105. try:
  106. for queued_call, orig_traceback in _queued_calls:
  107. try:
  108. queued_call()
  109. except Exception as e:
  110. msg = (
  111. f"XPU call failed lazily at initialization with error: {str(e)}\n\n"
  112. f"XPU call was originally invoked at:\n\n{''.join(orig_traceback)}"
  113. )
  114. raise Exception(msg) from e # noqa: TRY002
  115. finally:
  116. delattr(_tls, "is_initializing")
  117. _initialized = True
  118. class _DeviceGuard:
  119. def __init__(self, index: int):
  120. self.idx = index
  121. self.prev_idx = -1
  122. def __enter__(self):
  123. self.prev_idx = torch.xpu._exchange_device(self.idx)
  124. def __exit__(self, type: Any, value: Any, traceback: Any):
  125. self.idx = torch.xpu._maybe_exchange_device(self.prev_idx)
  126. return False
  127. class device:
  128. r"""Context-manager that changes the selected device.
  129. Args:
  130. device (torch.device or int or str): device index to select. It's a no-op if
  131. this argument is a negative integer or ``None``.
  132. """
  133. def __init__(self, device: Any):
  134. self.idx = _get_device_index(device, optional=True)
  135. self.prev_idx = -1
  136. def __enter__(self):
  137. self.prev_idx = torch.xpu._exchange_device(self.idx)
  138. def __exit__(self, type: Any, value: Any, traceback: Any):
  139. self.idx = torch.xpu._maybe_exchange_device(self.prev_idx)
  140. return False
  141. class device_of(device):
  142. r"""Context-manager that changes the current device to that of given object.
  143. You can use both tensors and storages as arguments. If a given object is
  144. not allocated on a XPU, this is a no-op.
  145. Args:
  146. obj (Tensor or Storage): object allocated on the selected device.
  147. """
  148. def __init__(self, obj):
  149. idx = obj.get_device() if obj.is_xpu else -1
  150. super().__init__(idx)
  151. def set_device(device: _device_t) -> None:
  152. r"""Set the current device.
  153. Args:
  154. device (torch.device or int or str): selected device. This function is a
  155. no-op if this argument is negative.
  156. """
  157. _lazy_init()
  158. device = _get_device_index(device)
  159. if device >= 0:
  160. torch._C._xpu_setDevice(device)
  161. def get_device_name(device: Optional[_device_t] = None) -> str:
  162. r"""Get the name of a device.
  163. Args:
  164. device (torch.device or int or str, optional): device for which to
  165. return the name. This function is a no-op if this argument is a
  166. negative integer. It uses the current device, given by :func:`~torch.xpu.current_device`,
  167. if :attr:`device` is ``None`` (default).
  168. Returns:
  169. str: the name of the device
  170. """
  171. return get_device_properties(device).name
  172. @lru_cache(None)
  173. def get_device_capability(device: Optional[_device_t] = None) -> dict[str, Any]:
  174. r"""Get the xpu capability of a device.
  175. Args:
  176. device (torch.device or int or str, optional): device for which to
  177. return the device capability. This function is a no-op if this
  178. argument is a negative integer. It uses the current device, given by
  179. :func:`~torch.xpu.current_device`, if :attr:`device` is ``None``
  180. (default).
  181. Returns:
  182. Dict[str, Any]: the xpu capability dictionary of the device
  183. """
  184. props = get_device_properties(device)
  185. # Only keep attributes that are safe for dictionary serialization.
  186. serializable_types = (int, float, bool, str, type(None), list, tuple, dict)
  187. return {
  188. key: value
  189. for key in dir(props)
  190. if not key.startswith("__")
  191. and isinstance((value := getattr(props, key)), serializable_types)
  192. }
  193. def get_device_properties(device: Optional[_device_t] = None) -> _XpuDeviceProperties:
  194. r"""Get the properties of a device.
  195. Args:
  196. device (torch.device or int or str): device for which to return the
  197. properties of the device.
  198. Returns:
  199. _XpuDeviceProperties: the properties of the device
  200. """
  201. _lazy_init()
  202. device = _get_device_index(device, optional=True)
  203. return _get_device_properties(device) # type: ignore[name-defined] # noqa: F821
  204. def current_device() -> int:
  205. r"""Return the index of a currently selected device."""
  206. _lazy_init()
  207. return torch._C._xpu_getDevice()
  208. def _get_device(device: Union[int, str, torch.device]) -> torch.device:
  209. r"""Return the torch.device type object from the passed in device.
  210. Args:
  211. device (torch.device or int or str): selected device.
  212. """
  213. if isinstance(device, str):
  214. device = torch.device(device)
  215. elif isinstance(device, int):
  216. device = torch.device("xpu", device)
  217. return device
  218. class StreamContext:
  219. r"""Context-manager that selects a given stream.
  220. All XPU kernels queued within its context will be enqueued on a selected
  221. stream.
  222. Args:
  223. Stream (Stream): selected stream. This manager is a no-op if it's
  224. ``None``.
  225. .. note:: Streams are per-device.
  226. """
  227. cur_stream: Optional["torch.xpu.Stream"]
  228. def __init__(self, stream: Optional["torch.xpu.Stream"]):
  229. self.stream = stream
  230. self.idx = _get_device_index(None, True)
  231. if self.idx is None:
  232. self.idx = -1
  233. def __enter__(self):
  234. cur_stream = self.stream
  235. if cur_stream is None or self.idx == -1:
  236. return
  237. self.src_prev_stream = torch.xpu.current_stream(None)
  238. # If the stream is not on the current device, then set the current stream on the device
  239. if self.src_prev_stream.device != cur_stream.device:
  240. with device(cur_stream.device):
  241. self.dst_prev_stream = torch.xpu.current_stream(cur_stream.device)
  242. torch.xpu.set_stream(cur_stream)
  243. def __exit__(self, type: Any, value: Any, traceback: Any):
  244. cur_stream = self.stream
  245. if cur_stream is None or self.idx == -1:
  246. return
  247. # Reset the stream on the original device and destination device
  248. if self.src_prev_stream.device != cur_stream.device:
  249. torch.xpu.set_stream(self.dst_prev_stream)
  250. torch.xpu.set_stream(self.src_prev_stream)
  251. def stream(stream: Optional["torch.xpu.Stream"]) -> StreamContext:
  252. r"""Wrap around the Context-manager StreamContext that selects a given stream.
  253. Arguments:
  254. stream (Stream): selected stream. This manager is a no-op if it's ``None``.
  255. """
  256. return StreamContext(stream)
  257. def _set_stream_by_id(stream_id, device_index, device_type):
  258. r"""set stream specified by the stream id, device index and device type
  259. Args: stream_id (int): not visible to the user, used to assigned to the specific stream.
  260. device_index (int): selected device index.
  261. device_type (int): selected device type.
  262. """
  263. torch._C._xpu_setStream(
  264. stream_id=stream_id,
  265. device_index=device_index,
  266. device_type=device_type,
  267. )
  268. def set_stream(stream: Stream):
  269. r"""Set the current stream.This is a wrapper API to set the stream.
  270. Usage of this function is discouraged in favor of the ``stream``
  271. context manager.
  272. Args:
  273. stream (Stream): selected stream. This function is a no-op
  274. if this argument is ``None``.
  275. """
  276. if stream is None:
  277. return
  278. _lazy_init()
  279. _set_stream_by_id(
  280. stream_id=stream.stream_id,
  281. device_index=stream.device_index,
  282. device_type=stream.device_type,
  283. )
  284. def current_stream(device: Optional[_device_t] = None) -> Stream:
  285. r"""Return the currently selected :class:`Stream` for a given device.
  286. Args:
  287. device (torch.device or int, optional): selected device. Returns
  288. the currently selected :class:`Stream` for the current device, given
  289. by :func:`~torch.xpu.current_device`, if :attr:`device` is ``None``
  290. (default).
  291. """
  292. _lazy_init()
  293. streamdata = torch._C._xpu_getCurrentStream(
  294. _get_device_index(device, optional=True)
  295. )
  296. return Stream(
  297. stream_id=streamdata[0], device_index=streamdata[1], device_type=streamdata[2]
  298. )
  299. def get_stream_from_external(
  300. data_ptr: int, device: Optional[_device_t] = None
  301. ) -> Stream:
  302. r"""Return a :class:`Stream` from an external SYCL queue.
  303. This function is used to wrap SYCL queue created in other libraries in order
  304. to facilitate data exchange and multi-library interactions.
  305. .. note:: This function doesn't manage the queue life-cycle, it is the user
  306. responsibility to keep the referenced queue alive while this returned stream is
  307. being used. The different SYCL queue pointers will result in distinct
  308. :class:`Stream` objects, even if the SYCL queues they dereference are equivalent.
  309. Args:
  310. data_ptr(int): Integer representation of the `sycl::queue*` value passed externally.
  311. device(torch.device or int, optional): the device where the queue was originally created.
  312. It is the user responsibility to ensure the device is specified correctly.
  313. """
  314. _lazy_init()
  315. streamdata = torch._C._xpu_getStreamFromExternal(
  316. data_ptr, _get_device_index(device, optional=True)
  317. )
  318. return Stream(
  319. stream_id=streamdata[0], device_index=streamdata[1], device_type=streamdata[2]
  320. )
  321. def synchronize(device: _device_t = None) -> None:
  322. r"""Wait for all kernels in all streams on a XPU device to complete.
  323. Args:
  324. device (torch.device or int, optional): device for which to synchronize.
  325. It uses the current device, given by :func:`~torch.xpu.current_device`,
  326. if :attr:`device` is ``None`` (default).
  327. """
  328. _lazy_init()
  329. device = _get_device_index(device, optional=True)
  330. return torch._C._xpu_synchronize(device)
  331. def get_arch_list() -> list[str]:
  332. r"""Return list XPU architectures this library was compiled for."""
  333. if not _is_compiled():
  334. return []
  335. arch_flags = torch._C._xpu_getArchFlags()
  336. if arch_flags is None:
  337. return []
  338. return arch_flags.split()
  339. def get_gencode_flags() -> str:
  340. r"""Return XPU AOT(ahead-of-time) build flags this library was compiled with."""
  341. arch_list = get_arch_list()
  342. if len(arch_list) == 0:
  343. return ""
  344. return f"-device {','.join(arch for arch in arch_list)}"
  345. def _get_generator(device: torch.device) -> torch._C.Generator:
  346. r"""Return the XPU Generator object for the given device.
  347. Args:
  348. device (torch.device): selected device.
  349. """
  350. idx = device.index
  351. if idx is None:
  352. idx = current_device()
  353. return torch.xpu.default_generators[idx]
  354. def _set_rng_state_offset(
  355. offset: int, device: Union[int, str, torch.device] = "xpu"
  356. ) -> None:
  357. r"""Set the random number generator state offset of the specified GPU.
  358. Args:
  359. offset (int): The desired offset
  360. device (torch.device or int, optional): The device to set the RNG state.
  361. Default: ``'xpu'`` (i.e., ``torch.device('xpu')``, the current XPU device).
  362. """
  363. final_device = _get_device(device)
  364. def cb():
  365. default_generator = _get_generator(final_device)
  366. default_generator.set_offset(offset)
  367. _lazy_call(cb)
  368. def _get_rng_state_offset(device: Union[int, str, torch.device] = "xpu") -> int:
  369. r"""Return the random number generator state offset of the specified GPU.
  370. Args:
  371. device (torch.device or int, optional): The device to return the RNG state offset of.
  372. Default: ``'xpu'`` (i.e., ``torch.device('xpu')``, the current XPU device).
  373. .. warning::
  374. This function eagerly initializes XPU.
  375. """
  376. _lazy_init()
  377. final_device = _get_device(device)
  378. default_generator = _get_generator(final_device)
  379. return default_generator.get_offset()
  380. # import here to avoid circular import
  381. from .memory import (
  382. empty_cache,
  383. max_memory_allocated,
  384. max_memory_reserved,
  385. mem_get_info,
  386. memory_allocated,
  387. memory_reserved,
  388. memory_stats,
  389. memory_stats_as_nested_dict,
  390. reset_accumulated_memory_stats,
  391. reset_peak_memory_stats,
  392. )
  393. from .random import (
  394. get_rng_state,
  395. get_rng_state_all,
  396. initial_seed,
  397. manual_seed,
  398. manual_seed_all,
  399. seed,
  400. seed_all,
  401. set_rng_state,
  402. set_rng_state_all,
  403. )
  404. __all__ = [
  405. "Event",
  406. "Stream",
  407. "StreamContext",
  408. "current_device",
  409. "current_stream",
  410. "default_generators",
  411. "device",
  412. "device_of",
  413. "device_count",
  414. "empty_cache",
  415. "get_arch_list",
  416. "get_device_capability",
  417. "get_device_name",
  418. "get_device_properties",
  419. "get_gencode_flags",
  420. "get_rng_state",
  421. "get_rng_state_all",
  422. "get_stream_from_external",
  423. "init",
  424. "initial_seed",
  425. "is_available",
  426. "is_bf16_supported",
  427. "is_initialized",
  428. "manual_seed",
  429. "manual_seed_all",
  430. "max_memory_allocated",
  431. "max_memory_reserved",
  432. "mem_get_info",
  433. "memory_allocated",
  434. "memory_reserved",
  435. "memory_stats",
  436. "memory_stats_as_nested_dict",
  437. "reset_accumulated_memory_stats",
  438. "reset_peak_memory_stats",
  439. "seed",
  440. "seed_all",
  441. "set_device",
  442. "set_rng_state",
  443. "set_rng_state_all",
  444. "set_stream",
  445. "stream",
  446. "streams",
  447. "synchronize",
  448. ]