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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# mypy: allow-untyped-defs
|
| 2 |
+
r"""
|
| 3 |
+
This package enables an interface for accessing MTIA backend in python
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import threading
|
| 7 |
+
import warnings
|
| 8 |
+
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
|
| 9 |
+
|
| 10 |
+
import torch
|
| 11 |
+
from torch import device as _device, Tensor
|
| 12 |
+
from torch._utils import _dummy_type, _LazySeedTracker, classproperty
|
| 13 |
+
from torch.types import Device
|
| 14 |
+
|
| 15 |
+
from ._utils import _get_device_index
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
_device_t = Union[_device, str, int]
|
| 19 |
+
|
| 20 |
+
# torch.mtia.Event/Stream is alias of torch.Event/Stream
|
| 21 |
+
Event = torch.Event
|
| 22 |
+
Stream = torch.Stream
|
| 23 |
+
|
| 24 |
+
_initialized = False
|
| 25 |
+
_queued_calls: List[
|
| 26 |
+
Tuple[Callable[[], None], List[str]]
|
| 27 |
+
] = [] # don't invoke these until initialization occurs
|
| 28 |
+
_tls = threading.local()
|
| 29 |
+
_initialization_lock = threading.Lock()
|
| 30 |
+
_lazy_seed_tracker = _LazySeedTracker()
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def init():
|
| 34 |
+
_lazy_init()
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def is_initialized():
|
| 38 |
+
r"""Return whether PyTorch's MTIA state has been initialized."""
|
| 39 |
+
return _initialized and not _is_in_bad_fork()
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def _is_in_bad_fork() -> bool:
|
| 43 |
+
return torch._C._mtia_isInBadFork()
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def _lazy_init() -> None:
|
| 47 |
+
global _initialized, _queued_calls
|
| 48 |
+
if is_initialized() or hasattr(_tls, "is_initializing"):
|
| 49 |
+
return
|
| 50 |
+
with _initialization_lock:
|
| 51 |
+
# We be double-checking locking, boys! This is OK because
|
| 52 |
+
# the above test was GIL protected anyway. The inner test
|
| 53 |
+
# is for when a thread blocked on some other thread which was
|
| 54 |
+
# doing the initialization; when they get the lock, they will
|
| 55 |
+
# find there is nothing left to do.
|
| 56 |
+
if is_initialized():
|
| 57 |
+
return
|
| 58 |
+
# It is important to prevent other threads from entering _lazy_init
|
| 59 |
+
# immediately, while we are still guaranteed to have the GIL, because some
|
| 60 |
+
# of the C calls we make below will release the GIL
|
| 61 |
+
if _is_in_bad_fork():
|
| 62 |
+
raise RuntimeError(
|
| 63 |
+
"Cannot re-initialize MTIA in forked subprocess. To use MTIA with "
|
| 64 |
+
"multiprocessing, you must use the 'spawn' start method"
|
| 65 |
+
)
|
| 66 |
+
if not _is_compiled():
|
| 67 |
+
raise AssertionError(
|
| 68 |
+
"Torch not compiled with MTIA enabled. "
|
| 69 |
+
"Ensure you have `import mtia.host_runtime.torch_mtia` in your python "
|
| 70 |
+
"src file and include `//mtia/host_runtime/torch_mtia:torch_mtia` as "
|
| 71 |
+
"your target dependency!"
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
torch._C._mtia_init()
|
| 75 |
+
# Some of the queued calls may reentrantly call _lazy_init();
|
| 76 |
+
# we need to just return without initializing in that case.
|
| 77 |
+
# However, we must not let any *other* threads in!
|
| 78 |
+
_tls.is_initializing = True
|
| 79 |
+
|
| 80 |
+
_queued_calls.extend(calls for calls in _lazy_seed_tracker.get_calls() if calls)
|
| 81 |
+
|
| 82 |
+
try:
|
| 83 |
+
for queued_call, orig_traceback in _queued_calls:
|
| 84 |
+
try:
|
| 85 |
+
queued_call()
|
| 86 |
+
except Exception as e:
|
| 87 |
+
msg = (
|
| 88 |
+
f"MTIA call failed lazily at initialization with error: {str(e)}\n\n"
|
| 89 |
+
f"MTIA call was originally invoked at:\n\n{''.join(orig_traceback)}"
|
| 90 |
+
)
|
| 91 |
+
raise DeferredMtiaCallError(msg) from e
|
| 92 |
+
finally:
|
| 93 |
+
delattr(_tls, "is_initializing")
|
| 94 |
+
_initialized = True
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
class DeferredMtiaCallError(Exception):
|
| 98 |
+
pass
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def _is_compiled() -> bool:
|
| 102 |
+
r"""Return true if compiled with MTIA support."""
|
| 103 |
+
return torch._C._mtia_isBuilt()
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def is_available() -> bool:
|
| 107 |
+
r"""Return true if MTIA device is available"""
|
| 108 |
+
if not _is_compiled():
|
| 109 |
+
return False
|
| 110 |
+
# MTIA has to init devices first to know if there is any devices available.
|
| 111 |
+
return device_count() > 0
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def synchronize(device: Optional[_device_t] = None) -> None:
|
| 115 |
+
r"""Waits for all jobs in all streams on a MTIA device to complete."""
|
| 116 |
+
with torch.mtia.device(device):
|
| 117 |
+
return torch._C._mtia_deviceSynchronize()
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
def device_count() -> int:
|
| 121 |
+
r"""Return the number of MTIA devices available."""
|
| 122 |
+
return torch._C._accelerator_hooks_device_count()
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
def current_device() -> int:
|
| 126 |
+
r"""Return the index of a currently selected device."""
|
| 127 |
+
return torch._C._accelerator_hooks_get_current_device()
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
def current_stream(device: Optional[_device_t] = None) -> Stream:
|
| 131 |
+
r"""Return the currently selected :class:`Stream` for a given device.
|
| 132 |
+
|
| 133 |
+
Args:
|
| 134 |
+
device (torch.device or int, optional): selected device. Returns
|
| 135 |
+
the currently selected :class:`Stream` for the current device, given
|
| 136 |
+
by :func:`~torch.mtia.current_device`, if :attr:`device` is ``None``
|
| 137 |
+
(default).
|
| 138 |
+
"""
|
| 139 |
+
return torch._C._mtia_getCurrentStream(_get_device_index(device, optional=True))
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
def default_stream(device: Optional[_device_t] = None) -> Stream:
|
| 143 |
+
r"""Return the default :class:`Stream` for a given device.
|
| 144 |
+
|
| 145 |
+
Args:
|
| 146 |
+
device (torch.device or int, optional): selected device. Returns
|
| 147 |
+
the default :class:`Stream` for the current device, given by
|
| 148 |
+
:func:`~torch.mtia.current_device`, if :attr:`device` is ``None``
|
| 149 |
+
(default).
|
| 150 |
+
"""
|
| 151 |
+
return torch._C._mtia_getDefaultStream(_get_device_index(device, optional=True))
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
def get_device_capability(device: Optional[_device_t] = None) -> Tuple[int, int]:
|
| 155 |
+
r"""Return capability of a given device as a tuple of (major version, minor version).
|
| 156 |
+
|
| 157 |
+
Args:
|
| 158 |
+
device (torch.device or int, optional) selected device. Returns
|
| 159 |
+
statistics for the current device, given by current_device(),
|
| 160 |
+
if device is None (default).
|
| 161 |
+
"""
|
| 162 |
+
return torch._C._mtia_getDeviceCapability(_get_device_index(device, optional=True))
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
def empty_cache() -> None:
|
| 166 |
+
r"""Empty the MTIA device cache."""
|
| 167 |
+
return torch._C._mtia_emptyCache()
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
def set_stream(stream: Stream):
|
| 171 |
+
r"""Set the current stream.This is a wrapper API to set the stream.
|
| 172 |
+
Usage of this function is discouraged in favor of the ``stream``
|
| 173 |
+
context manager.
|
| 174 |
+
|
| 175 |
+
Args:
|
| 176 |
+
stream (Stream): selected stream. This function is a no-op
|
| 177 |
+
if this argument is ``None``.
|
| 178 |
+
"""
|
| 179 |
+
if stream is None:
|
| 180 |
+
return
|
| 181 |
+
torch._C._mtia_setCurrentStream(stream)
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
def set_device(device: _device_t) -> None:
|
| 185 |
+
r"""Set the current device.
|
| 186 |
+
|
| 187 |
+
Args:
|
| 188 |
+
device (torch.device or int): selected device. This function is a no-op
|
| 189 |
+
if this argument is negative.
|
| 190 |
+
"""
|
| 191 |
+
device = _get_device_index(device)
|
| 192 |
+
if device >= 0:
|
| 193 |
+
torch._C._accelerator_hooks_set_current_device(device)
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
class device:
|
| 197 |
+
r"""Context-manager that changes the selected device.
|
| 198 |
+
|
| 199 |
+
Args:
|
| 200 |
+
device (torch.device or int): device index to select. It's a no-op if
|
| 201 |
+
this argument is a negative integer or ``None``.
|
| 202 |
+
"""
|
| 203 |
+
|
| 204 |
+
def __init__(self, device: Any):
|
| 205 |
+
self.idx = _get_device_index(device, optional=True)
|
| 206 |
+
self.prev_idx = -1
|
| 207 |
+
|
| 208 |
+
def __enter__(self):
|
| 209 |
+
self.prev_idx = torch._C._accelerator_hooks_maybe_exchange_device(self.idx)
|
| 210 |
+
|
| 211 |
+
def __exit__(self, type: Any, value: Any, traceback: Any):
|
| 212 |
+
self.idx = torch._C._accelerator_hooks_maybe_exchange_device(self.prev_idx)
|
| 213 |
+
return False
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
class StreamContext:
|
| 217 |
+
r"""Context-manager that selects a given stream.
|
| 218 |
+
|
| 219 |
+
All MTIA kernels queued within its context will be enqueued on a selected
|
| 220 |
+
stream.
|
| 221 |
+
|
| 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 |
+
|
| 228 |
+
cur_stream: Optional["torch.mtia.Stream"]
|
| 229 |
+
|
| 230 |
+
def __init__(self, stream: Optional["torch.mtia.Stream"]):
|
| 231 |
+
self.cur_stream = None
|
| 232 |
+
self.stream = stream
|
| 233 |
+
self.idx = _get_device_index(None, True)
|
| 234 |
+
if not torch.jit.is_scripting():
|
| 235 |
+
if self.idx is None:
|
| 236 |
+
self.idx = -1
|
| 237 |
+
|
| 238 |
+
self.src_prev_stream = (
|
| 239 |
+
None if not torch.jit.is_scripting() else torch.mtia.default_stream(None)
|
| 240 |
+
)
|
| 241 |
+
self.dst_prev_stream = (
|
| 242 |
+
None if not torch.jit.is_scripting() else torch.mtia.default_stream(None)
|
| 243 |
+
)
|
| 244 |
+
|
| 245 |
+
def __enter__(self):
|
| 246 |
+
# Local cur_stream variable for type refinement
|
| 247 |
+
cur_stream = self.stream
|
| 248 |
+
# Return if stream is None or MTIA device not available
|
| 249 |
+
if cur_stream is None or self.idx == -1:
|
| 250 |
+
return
|
| 251 |
+
self.src_prev_stream = torch.mtia.current_stream(None)
|
| 252 |
+
|
| 253 |
+
# If the stream is not on the current device, then
|
| 254 |
+
# set the current stream on the device
|
| 255 |
+
if self.src_prev_stream.device != cur_stream.device:
|
| 256 |
+
with device(cur_stream.device):
|
| 257 |
+
self.dst_prev_stream = torch.mtia.current_stream(cur_stream.device)
|
| 258 |
+
torch.mtia.set_stream(cur_stream)
|
| 259 |
+
|
| 260 |
+
def __exit__(self, type: Any, value: Any, traceback: Any):
|
| 261 |
+
# Local cur_stream variable for type refinement
|
| 262 |
+
cur_stream = self.stream
|
| 263 |
+
# If stream is None or no MTIA device available, return
|
| 264 |
+
if cur_stream is None or self.idx == -1:
|
| 265 |
+
return
|
| 266 |
+
|
| 267 |
+
# Reset the stream on the original device
|
| 268 |
+
# and destination device
|
| 269 |
+
if self.src_prev_stream.device != cur_stream.device: # type: ignore[union-attr]
|
| 270 |
+
torch.mtia.set_stream(self.dst_prev_stream) # type: ignore[arg-type]
|
| 271 |
+
torch.mtia.set_stream(self.src_prev_stream) # type: ignore[arg-type]
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
def stream(stream: Optional["torch.mtia.Stream"]) -> StreamContext:
|
| 275 |
+
r"""Wrap around the Context-manager StreamContext that selects a given stream.
|
| 276 |
+
|
| 277 |
+
Arguments:
|
| 278 |
+
stream (Stream): selected stream. This manager is a no-op if it's
|
| 279 |
+
``None``.
|
| 280 |
+
..Note:: In eager mode stream is of type Stream class while in JIT it doesn't support torch.mtia.stream
|
| 281 |
+
"""
|
| 282 |
+
return StreamContext(stream)
|
| 283 |
+
|
| 284 |
+
|
| 285 |
+
def get_rng_state(device: Union[int, str, torch.device] = "mtia") -> Tensor:
|
| 286 |
+
r"""Returns the random number generator state as a ByteTensor.
|
| 287 |
+
|
| 288 |
+
Args:
|
| 289 |
+
device (torch.device or int, optional): The device to return the RNG state of.
|
| 290 |
+
Default: ``'mtia'`` (i.e., ``torch.device('mtia')``, the current mtia device).
|
| 291 |
+
"""
|
| 292 |
+
warnings.warn(
|
| 293 |
+
"get_rng_state is not implemented in torch.mtia",
|
| 294 |
+
UserWarning,
|
| 295 |
+
stacklevel=2,
|
| 296 |
+
)
|
| 297 |
+
return torch.zeros([1], dtype=torch.uint8, device=device)
|
| 298 |
+
|
| 299 |
+
|
| 300 |
+
def set_rng_state(
|
| 301 |
+
new_state: Tensor, device: Union[int, str, torch.device] = "mtia"
|
| 302 |
+
) -> None:
|
| 303 |
+
r"""Sets the random number generator state.
|
| 304 |
+
|
| 305 |
+
Args:
|
| 306 |
+
new_state (torch.ByteTensor): The desired state
|
| 307 |
+
device (torch.device or int, optional): The device to set the RNG state.
|
| 308 |
+
Default: ``'mtia'`` (i.e., ``torch.device('mtia')``, the current mtia device).
|
| 309 |
+
"""
|
| 310 |
+
warnings.warn(
|
| 311 |
+
"set_rng_state is not implemented in torch.mtia",
|
| 312 |
+
UserWarning,
|
| 313 |
+
stacklevel=2,
|
| 314 |
+
)
|
| 315 |
+
|
| 316 |
+
|
| 317 |
+
from .memory import * # noqa: F403
|
| 318 |
+
|
| 319 |
+
|
| 320 |
+
__all__ = [
|
| 321 |
+
"init",
|
| 322 |
+
"is_available",
|
| 323 |
+
"is_initialized",
|
| 324 |
+
"synchronize",
|
| 325 |
+
"device_count",
|
| 326 |
+
"current_device",
|
| 327 |
+
"current_stream",
|
| 328 |
+
"default_stream",
|
| 329 |
+
"memory_stats",
|
| 330 |
+
"max_memory_allocated",
|
| 331 |
+
"get_device_capability",
|
| 332 |
+
"empty_cache",
|
| 333 |
+
"set_device",
|
| 334 |
+
"set_stream",
|
| 335 |
+
"stream",
|
| 336 |
+
"device",
|
| 337 |
+
"set_rng_state",
|
| 338 |
+
"get_rng_state",
|
| 339 |
+
]
|