|
| 1 | +r""" |
| 2 | +This package introduces support for the current :ref:`accelerator<accelerators>` in python. |
| 3 | +""" |
| 4 | + |
| 5 | +import torch |
| 6 | + |
| 7 | +from ._utils import _device_t, _get_device_index |
| 8 | + |
| 9 | + |
| 10 | +def device_count() -> int: |
| 11 | + r"""Return the number of current :ref:`accelerator<accelerators>` available. |
| 12 | +
|
| 13 | + Returns: |
| 14 | + int: the number of the current :ref:`accelerator<accelerators>` available. |
| 15 | + If there is no available accelerators, return 0. |
| 16 | + """ |
| 17 | + return torch._C._accelerator_deviceCount() |
| 18 | + |
| 19 | + |
| 20 | +def is_available() -> bool: |
| 21 | + r"""Check if there is an available :ref:`accelerator<accelerators>`. |
| 22 | +
|
| 23 | + Returns: |
| 24 | + bool: A boolean indicating if there is an available :ref:`accelerator<accelerators>`. |
| 25 | +
|
| 26 | + Example:: |
| 27 | +
|
| 28 | + >>> assert torch.accelerator.is_available() "No available accelerators detected." |
| 29 | + """ |
| 30 | + return device_count() > 0 |
| 31 | + |
| 32 | + |
| 33 | +def current_accelerator() -> torch.device: |
| 34 | + r"""Return the device of the current :ref:`accelerator<accelerators>`. |
| 35 | +
|
| 36 | + Returns: |
| 37 | + torch.device: return the current accelerator as :class:`torch.device`. |
| 38 | +
|
| 39 | + .. note:: The index of the returned :class:`torch.device` will be ``None``, please use |
| 40 | + :func:`torch.accelerator.current_device_idx` to know the current index being used. |
| 41 | + And ensure to use :func:`torch.accelerator.is_available` to check if there is an available |
| 42 | + accelerator. If there is no available accelerator, this function will raise an exception. |
| 43 | +
|
| 44 | + Example:: |
| 45 | +
|
| 46 | + >>> # xdoctest: |
| 47 | + >>> if torch.accelerator.is_available(): |
| 48 | + >>> current_device = torch.accelerator.current_accelerator() |
| 49 | + >>> else: |
| 50 | + >>> current_device = torch.device("cpu") |
| 51 | + >>> if current_device.type == 'cuda': |
| 52 | + >>> is_half_supported = torch.cuda.has_half |
| 53 | + >>> elif current_device.type == 'xpu': |
| 54 | + >>> is_half_supported = torch.xpu.get_device_properties().has_fp16 |
| 55 | + >>> elif current_device.type == 'cpu': |
| 56 | + >>> is_half_supported = True |
| 57 | + """ |
| 58 | + return torch._C._accelerator_getAccelerator() |
| 59 | + |
| 60 | + |
| 61 | +def current_device_idx() -> int: |
| 62 | + r"""Return the index of a currently selected device for the current :ref:`accelerator<accelerators>`. |
| 63 | +
|
| 64 | + Returns: |
| 65 | + int: the index of a currently selected device. |
| 66 | + """ |
| 67 | + return torch._C._accelerator_getDeviceIndex() |
| 68 | + |
| 69 | + |
| 70 | +def set_device_idx(device: _device_t, /) -> None: |
| 71 | + r"""Set the current device index to a given device. |
| 72 | +
|
| 73 | + Args: |
| 74 | + device (:class:`torch.device`, str, int): a given device that must match the current |
| 75 | + :ref:`accelerator<accelerators>` device type. |
| 76 | +
|
| 77 | + .. note:: This function is a no-op if this device index is negative. |
| 78 | + """ |
| 79 | + device_index = _get_device_index(device) |
| 80 | + torch._C._accelerator_setDeviceIndex(device_index) |
| 81 | + |
| 82 | + |
| 83 | +def current_stream(device: _device_t = None, /) -> torch.Stream: |
| 84 | + r"""Return the currently selected stream for a given device. |
| 85 | +
|
| 86 | + Args: |
| 87 | + device (:class:`torch.device`, str, int, optional): a given device that must match the current |
| 88 | + :ref:`accelerator<accelerators>` device type. If not given, |
| 89 | + use :func:`torch.accelerator.current_device_idx` by default. |
| 90 | +
|
| 91 | + Returns: |
| 92 | + torch.Stream: the currently selected stream for a given device. |
| 93 | + """ |
| 94 | + device_index = _get_device_index(device, True) |
| 95 | + return torch._C._accelerator_getStream(device_index) |
| 96 | + |
| 97 | + |
| 98 | +def set_stream(stream: torch.Stream) -> None: |
| 99 | + r"""Set the current stream to a given stream. |
| 100 | +
|
| 101 | + Args: |
| 102 | + stream (torch.Stream): a given stream that must match the current :ref:`accelerator<accelerators>` device type. |
| 103 | +
|
| 104 | + .. note:: This function will set the current device index to the device index of the given stream. |
| 105 | + """ |
| 106 | + torch._C._accelerator_setStream(stream) |
| 107 | + |
| 108 | + |
| 109 | +def synchronize(device: _device_t = None, /) -> None: |
| 110 | + r"""Wait for all kernels in all streams on the given device to complete. |
| 111 | +
|
| 112 | + Args: |
| 113 | + device (:class:`torch.device`, str, int, optional): device for which to synchronize. It must match |
| 114 | + the current :ref:`accelerator<accelerators>` device type. If not given, |
| 115 | + use :func:`torch.accelerator.current_device_idx` by default. |
| 116 | +
|
| 117 | + .. note:: This function is a no-op if the current :ref:`accelerator<accelerators>` is not initialized. |
| 118 | +
|
| 119 | + Example:: |
| 120 | +
|
| 121 | + >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA) |
| 122 | + >>> assert torch.accelerator.is_available() "No available accelerators detected." |
| 123 | + >>> start_event = torch.Event(enable_timing=True) |
| 124 | + >>> end_event = torch.Event(enable_timing=True) |
| 125 | + >>> start_event.record() |
| 126 | + >>> tensor = torch.randn(100, device=torch.accelerator.current_accelerator()) |
| 127 | + >>> sum = torch.sum(tensor) |
| 128 | + >>> end_event.record() |
| 129 | + >>> torch.accelerator.synchronize() |
| 130 | + >>> elapsed_time_ms = start_event.elapsed_time(end_event) |
| 131 | + """ |
| 132 | + device_index = _get_device_index(device, True) |
| 133 | + torch._C._accelerator_synchronizeDevice(device_index) |
| 134 | + |
| 135 | + |
| 136 | +__all__ = [ |
| 137 | + "current_accelerator", |
| 138 | + "current_device_idx", |
| 139 | + "current_stream", |
| 140 | + "device_count", |
| 141 | + "is_available", |
| 142 | + "set_device_idx", |
| 143 | + "set_stream", |
| 144 | + "synchronize", |
| 145 | +] |
0 commit comments