2024-06-08 18:08:21 +00:00
|
|
|
# mypy: allow-untyped-defs
|
[dynamo][stream]support device-agnostic stream in dynamo and capture stream/event method in fx graph (#108312)
This PR implements 2 things:
1. support the device agnostic stream and runtime APIs captured by the dynamo.
2. support the stream methods(include the event) captured by the dynamo.
Here are details for 1st.
Previously the stream captured in dynamo was tightly bind to CUDA. Here we implement a global singleton container named `StreamMethodContainer` for different backends to register their associated stream methods to dynamo. When import the backend’s product, the stream operations can be registered directly by calling
```
device_stream_method = {'current_stream': method_1,
'create_stream_context': method_2,
'set_stream': method_3,
'set_stream_by_id': method_4}
torch._dynamo.stream.register_stream_method(device_name, device_stream_method)
```
Stream methods need to be passed in this API according to the precise semantics represented by the dict key in `device_stream_method`. After register, these methods can be used by dynamo to capture the stream operations in users’ script, for example, get the current stream or set the specific stream. Additionally, the wrapped stream variable and the stream context variable are changed to be the device-agnostic, the proxy functions of these variables are assigned by the associated methods in the container. All of this are illustrated in the below. Below is a illustration.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/108312
Approved by: https://github.com/jansel, https://github.com/jgong5
2023-10-22 13:22:58 +00:00
|
|
|
import inspect
|
2023-11-08 17:58:55 +00:00
|
|
|
from typing import Any, Callable, Dict, Iterable, Optional, Tuple, Type, Union
|
2023-09-24 07:49:16 +00:00
|
|
|
|
|
|
|
|
import torch
|
[dynamo][stream]support device-agnostic stream in dynamo and capture stream/event method in fx graph (#108312)
This PR implements 2 things:
1. support the device agnostic stream and runtime APIs captured by the dynamo.
2. support the stream methods(include the event) captured by the dynamo.
Here are details for 1st.
Previously the stream captured in dynamo was tightly bind to CUDA. Here we implement a global singleton container named `StreamMethodContainer` for different backends to register their associated stream methods to dynamo. When import the backend’s product, the stream operations can be registered directly by calling
```
device_stream_method = {'current_stream': method_1,
'create_stream_context': method_2,
'set_stream': method_3,
'set_stream_by_id': method_4}
torch._dynamo.stream.register_stream_method(device_name, device_stream_method)
```
Stream methods need to be passed in this API according to the precise semantics represented by the dict key in `device_stream_method`. After register, these methods can be used by dynamo to capture the stream operations in users’ script, for example, get the current stream or set the specific stream. Additionally, the wrapped stream variable and the stream context variable are changed to be the device-agnostic, the proxy functions of these variables are assigned by the associated methods in the container. All of this are illustrated in the below. Below is a illustration.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/108312
Approved by: https://github.com/jansel, https://github.com/jgong5
2023-10-22 13:22:58 +00:00
|
|
|
from torch._streambase import _EventBase, _StreamBase
|
2023-09-24 07:49:16 +00:00
|
|
|
|
2024-07-31 13:58:25 +00:00
|
|
|
|
2023-11-08 17:58:55 +00:00
|
|
|
get_cuda_stream: Optional[Callable[[int], int]]
|
2023-09-24 07:49:16 +00:00
|
|
|
if torch.cuda._is_compiled():
|
|
|
|
|
from torch._C import _cuda_getCurrentRawStream as get_cuda_stream
|
|
|
|
|
else:
|
|
|
|
|
get_cuda_stream = None
|
|
|
|
|
|
|
|
|
|
_device_t = Union[torch.device, str, int, None]
|
|
|
|
|
|
|
|
|
|
# Recording the device properties in the main process but used in worker process.
|
|
|
|
|
caching_worker_device_properties: Dict[str, Any] = {}
|
|
|
|
|
caching_worker_current_devices: Dict[str, int] = {}
|
|
|
|
|
|
|
|
|
|
|
[dynamo][stream]support device-agnostic stream in dynamo and capture stream/event method in fx graph (#108312)
This PR implements 2 things:
1. support the device agnostic stream and runtime APIs captured by the dynamo.
2. support the stream methods(include the event) captured by the dynamo.
Here are details for 1st.
Previously the stream captured in dynamo was tightly bind to CUDA. Here we implement a global singleton container named `StreamMethodContainer` for different backends to register their associated stream methods to dynamo. When import the backend’s product, the stream operations can be registered directly by calling
```
device_stream_method = {'current_stream': method_1,
'create_stream_context': method_2,
'set_stream': method_3,
'set_stream_by_id': method_4}
torch._dynamo.stream.register_stream_method(device_name, device_stream_method)
```
Stream methods need to be passed in this API according to the precise semantics represented by the dict key in `device_stream_method`. After register, these methods can be used by dynamo to capture the stream operations in users’ script, for example, get the current stream or set the specific stream. Additionally, the wrapped stream variable and the stream context variable are changed to be the device-agnostic, the proxy functions of these variables are assigned by the associated methods in the container. All of this are illustrated in the below. Below is a illustration.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/108312
Approved by: https://github.com/jansel, https://github.com/jgong5
2023-10-22 13:22:58 +00:00
|
|
|
class DeviceInterfaceMeta(type):
|
|
|
|
|
def __new__(metacls, *args, **kwargs):
|
|
|
|
|
class_member = args[2]
|
|
|
|
|
if "Event" in class_member:
|
|
|
|
|
assert inspect.isclass(class_member["Event"]) and issubclass(
|
|
|
|
|
class_member["Event"], _EventBase
|
|
|
|
|
), "DeviceInterface member Event should be inherit from _EventBase"
|
|
|
|
|
if "Stream" in class_member:
|
|
|
|
|
assert inspect.isclass(class_member["Stream"]) and issubclass(
|
|
|
|
|
class_member["Stream"], _StreamBase
|
|
|
|
|
), "DeviceInterface member Stream should be inherit from _StreamBase"
|
|
|
|
|
return super().__new__(metacls, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DeviceInterface(metaclass=DeviceInterfaceMeta):
|
2023-09-24 07:49:16 +00:00
|
|
|
"""
|
|
|
|
|
This is a simple device runtime interface for Inductor. It enables custom
|
|
|
|
|
backends to be integrated with Inductor in a device-agnostic semantic.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
class device:
|
|
|
|
|
def __new__(cls, device: _device_t):
|
2024-04-17 19:29:30 +00:00
|
|
|
raise NotImplementedError
|
2023-09-24 07:49:16 +00:00
|
|
|
|
|
|
|
|
class Worker:
|
|
|
|
|
"""
|
|
|
|
|
Worker API to query device properties that will work in multi processing
|
|
|
|
|
workers that cannot use the GPU APIs (due to processing fork() and
|
|
|
|
|
initialization time issues). Properties are recorded in the main process
|
|
|
|
|
before we fork the workers.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def set_device(device: int):
|
2024-04-17 19:29:30 +00:00
|
|
|
raise NotImplementedError
|
2023-09-24 07:49:16 +00:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def current_device() -> int:
|
2024-04-17 19:29:30 +00:00
|
|
|
raise NotImplementedError
|
2023-09-24 07:49:16 +00:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_device_properties(device: _device_t = None):
|
2024-04-17 19:29:30 +00:00
|
|
|
raise NotImplementedError
|
2023-09-24 07:49:16 +00:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def current_device():
|
2024-04-17 19:29:30 +00:00
|
|
|
raise NotImplementedError
|
2023-09-24 07:49:16 +00:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def set_device(device: _device_t):
|
2024-04-17 19:29:30 +00:00
|
|
|
raise NotImplementedError
|
2023-09-24 07:49:16 +00:00
|
|
|
|
2024-04-12 18:21:27 +00:00
|
|
|
@staticmethod
|
|
|
|
|
def maybe_exchange_device(device: int) -> int:
|
2024-04-17 19:29:30 +00:00
|
|
|
raise NotImplementedError
|
2024-04-12 18:21:27 +00:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def exchange_device(device: int) -> int:
|
2024-04-17 19:29:30 +00:00
|
|
|
raise NotImplementedError
|
2024-04-12 18:21:27 +00:00
|
|
|
|
2023-09-24 07:49:16 +00:00
|
|
|
@staticmethod
|
|
|
|
|
def device_count():
|
2024-04-17 19:29:30 +00:00
|
|
|
raise NotImplementedError
|
2023-09-24 07:49:16 +00:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def is_available() -> bool:
|
2024-04-17 19:29:30 +00:00
|
|
|
raise NotImplementedError
|
2023-09-24 07:49:16 +00:00
|
|
|
|
[dynamo][stream]support device-agnostic stream in dynamo and capture stream/event method in fx graph (#108312)
This PR implements 2 things:
1. support the device agnostic stream and runtime APIs captured by the dynamo.
2. support the stream methods(include the event) captured by the dynamo.
Here are details for 1st.
Previously the stream captured in dynamo was tightly bind to CUDA. Here we implement a global singleton container named `StreamMethodContainer` for different backends to register their associated stream methods to dynamo. When import the backend’s product, the stream operations can be registered directly by calling
```
device_stream_method = {'current_stream': method_1,
'create_stream_context': method_2,
'set_stream': method_3,
'set_stream_by_id': method_4}
torch._dynamo.stream.register_stream_method(device_name, device_stream_method)
```
Stream methods need to be passed in this API according to the precise semantics represented by the dict key in `device_stream_method`. After register, these methods can be used by dynamo to capture the stream operations in users’ script, for example, get the current stream or set the specific stream. Additionally, the wrapped stream variable and the stream context variable are changed to be the device-agnostic, the proxy functions of these variables are assigned by the associated methods in the container. All of this are illustrated in the below. Below is a illustration.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/108312
Approved by: https://github.com/jansel, https://github.com/jgong5
2023-10-22 13:22:58 +00:00
|
|
|
@staticmethod
|
|
|
|
|
def stream(stream: torch.Stream):
|
2024-04-17 19:29:30 +00:00
|
|
|
raise NotImplementedError
|
[dynamo][stream]support device-agnostic stream in dynamo and capture stream/event method in fx graph (#108312)
This PR implements 2 things:
1. support the device agnostic stream and runtime APIs captured by the dynamo.
2. support the stream methods(include the event) captured by the dynamo.
Here are details for 1st.
Previously the stream captured in dynamo was tightly bind to CUDA. Here we implement a global singleton container named `StreamMethodContainer` for different backends to register their associated stream methods to dynamo. When import the backend’s product, the stream operations can be registered directly by calling
```
device_stream_method = {'current_stream': method_1,
'create_stream_context': method_2,
'set_stream': method_3,
'set_stream_by_id': method_4}
torch._dynamo.stream.register_stream_method(device_name, device_stream_method)
```
Stream methods need to be passed in this API according to the precise semantics represented by the dict key in `device_stream_method`. After register, these methods can be used by dynamo to capture the stream operations in users’ script, for example, get the current stream or set the specific stream. Additionally, the wrapped stream variable and the stream context variable are changed to be the device-agnostic, the proxy functions of these variables are assigned by the associated methods in the container. All of this are illustrated in the below. Below is a illustration.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/108312
Approved by: https://github.com/jansel, https://github.com/jgong5
2023-10-22 13:22:58 +00:00
|
|
|
|
2023-09-24 07:49:16 +00:00
|
|
|
@staticmethod
|
|
|
|
|
def current_stream():
|
2024-04-17 19:29:30 +00:00
|
|
|
raise NotImplementedError
|
2023-09-24 07:49:16 +00:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def set_stream(stream: torch.Stream):
|
2024-04-17 19:29:30 +00:00
|
|
|
raise NotImplementedError
|
2023-09-24 07:49:16 +00:00
|
|
|
|
[dynamo][stream]support device-agnostic stream in dynamo and capture stream/event method in fx graph (#108312)
This PR implements 2 things:
1. support the device agnostic stream and runtime APIs captured by the dynamo.
2. support the stream methods(include the event) captured by the dynamo.
Here are details for 1st.
Previously the stream captured in dynamo was tightly bind to CUDA. Here we implement a global singleton container named `StreamMethodContainer` for different backends to register their associated stream methods to dynamo. When import the backend’s product, the stream operations can be registered directly by calling
```
device_stream_method = {'current_stream': method_1,
'create_stream_context': method_2,
'set_stream': method_3,
'set_stream_by_id': method_4}
torch._dynamo.stream.register_stream_method(device_name, device_stream_method)
```
Stream methods need to be passed in this API according to the precise semantics represented by the dict key in `device_stream_method`. After register, these methods can be used by dynamo to capture the stream operations in users’ script, for example, get the current stream or set the specific stream. Additionally, the wrapped stream variable and the stream context variable are changed to be the device-agnostic, the proxy functions of these variables are assigned by the associated methods in the container. All of this are illustrated in the below. Below is a illustration.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/108312
Approved by: https://github.com/jansel, https://github.com/jgong5
2023-10-22 13:22:58 +00:00
|
|
|
@staticmethod
|
|
|
|
|
def _set_stream_by_id(stream_id: int, device_index: int, device_type: int):
|
2024-04-17 19:29:30 +00:00
|
|
|
raise NotImplementedError
|
[dynamo][stream]support device-agnostic stream in dynamo and capture stream/event method in fx graph (#108312)
This PR implements 2 things:
1. support the device agnostic stream and runtime APIs captured by the dynamo.
2. support the stream methods(include the event) captured by the dynamo.
Here are details for 1st.
Previously the stream captured in dynamo was tightly bind to CUDA. Here we implement a global singleton container named `StreamMethodContainer` for different backends to register their associated stream methods to dynamo. When import the backend’s product, the stream operations can be registered directly by calling
```
device_stream_method = {'current_stream': method_1,
'create_stream_context': method_2,
'set_stream': method_3,
'set_stream_by_id': method_4}
torch._dynamo.stream.register_stream_method(device_name, device_stream_method)
```
Stream methods need to be passed in this API according to the precise semantics represented by the dict key in `device_stream_method`. After register, these methods can be used by dynamo to capture the stream operations in users’ script, for example, get the current stream or set the specific stream. Additionally, the wrapped stream variable and the stream context variable are changed to be the device-agnostic, the proxy functions of these variables are assigned by the associated methods in the container. All of this are illustrated in the below. Below is a illustration.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/108312
Approved by: https://github.com/jansel, https://github.com/jgong5
2023-10-22 13:22:58 +00:00
|
|
|
|
2023-09-24 07:49:16 +00:00
|
|
|
@staticmethod
|
|
|
|
|
def get_raw_stream():
|
2024-04-17 19:29:30 +00:00
|
|
|
raise NotImplementedError
|
2023-09-24 07:49:16 +00:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def synchronize(device: _device_t = None):
|
2024-04-17 19:29:30 +00:00
|
|
|
raise NotImplementedError
|
2023-09-24 07:49:16 +00:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_device_properties(device: _device_t = None):
|
2024-04-17 19:29:30 +00:00
|
|
|
raise NotImplementedError
|
2023-09-24 07:49:16 +00:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_compute_capability(device: _device_t = None):
|
2024-04-17 19:29:30 +00:00
|
|
|
raise NotImplementedError
|
2023-09-24 07:49:16 +00:00
|
|
|
|
|
|
|
|
|
2024-04-12 18:21:27 +00:00
|
|
|
class DeviceGuard:
|
|
|
|
|
"""
|
|
|
|
|
This class provides a context manager for device switching. This is a stripped
|
|
|
|
|
down version of torch.{device_name}.device.
|
|
|
|
|
|
|
|
|
|
The context manager changes the current device to the given device index
|
|
|
|
|
on entering the context and restores the original device on exiting.
|
|
|
|
|
The device is switched using the provided device interface.
|
|
|
|
|
"""
|
|
|
|
|
|
2024-08-01 15:53:32 +00:00
|
|
|
def __init__(
|
|
|
|
|
self, device_interface: Type[DeviceInterface], index: Optional[int]
|
|
|
|
|
) -> None:
|
2024-04-12 18:21:27 +00:00
|
|
|
self.device_interface = device_interface
|
|
|
|
|
self.idx = index
|
|
|
|
|
self.prev_idx = -1
|
|
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
|
if self.idx is not None:
|
|
|
|
|
self.prev_idx = self.device_interface.exchange_device(self.idx)
|
|
|
|
|
|
|
|
|
|
def __exit__(self, type: Any, value: Any, traceback: Any):
|
|
|
|
|
if self.idx is not None:
|
|
|
|
|
self.idx = self.device_interface.maybe_exchange_device(self.prev_idx)
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
2023-09-24 07:49:16 +00:00
|
|
|
class CudaInterface(DeviceInterface):
|
|
|
|
|
device = torch.cuda.device
|
|
|
|
|
|
[dynamo][stream]support device-agnostic stream in dynamo and capture stream/event method in fx graph (#108312)
This PR implements 2 things:
1. support the device agnostic stream and runtime APIs captured by the dynamo.
2. support the stream methods(include the event) captured by the dynamo.
Here are details for 1st.
Previously the stream captured in dynamo was tightly bind to CUDA. Here we implement a global singleton container named `StreamMethodContainer` for different backends to register their associated stream methods to dynamo. When import the backend’s product, the stream operations can be registered directly by calling
```
device_stream_method = {'current_stream': method_1,
'create_stream_context': method_2,
'set_stream': method_3,
'set_stream_by_id': method_4}
torch._dynamo.stream.register_stream_method(device_name, device_stream_method)
```
Stream methods need to be passed in this API according to the precise semantics represented by the dict key in `device_stream_method`. After register, these methods can be used by dynamo to capture the stream operations in users’ script, for example, get the current stream or set the specific stream. Additionally, the wrapped stream variable and the stream context variable are changed to be the device-agnostic, the proxy functions of these variables are assigned by the associated methods in the container. All of this are illustrated in the below. Below is a illustration.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/108312
Approved by: https://github.com/jansel, https://github.com/jgong5
2023-10-22 13:22:58 +00:00
|
|
|
# register Event and Stream class into the backend interface
|
|
|
|
|
# make sure Event and Stream are implemented and inherited from the _EventBase and _StreamBase
|
|
|
|
|
Event = torch.cuda.Event
|
|
|
|
|
Stream = torch.cuda.Stream
|
|
|
|
|
|
2023-09-24 07:49:16 +00:00
|
|
|
class Worker:
|
|
|
|
|
@staticmethod
|
|
|
|
|
def set_device(device: int):
|
|
|
|
|
caching_worker_current_devices["cuda"] = device
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def current_device() -> int:
|
|
|
|
|
if "cuda" in caching_worker_current_devices:
|
|
|
|
|
return caching_worker_current_devices["cuda"]
|
|
|
|
|
return torch.cuda.current_device()
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_device_properties(device: _device_t = None):
|
|
|
|
|
if device is not None:
|
|
|
|
|
if isinstance(device, str):
|
|
|
|
|
device = torch.device(device)
|
|
|
|
|
assert device.type == "cuda"
|
|
|
|
|
if isinstance(device, torch.device):
|
|
|
|
|
device = device.index
|
|
|
|
|
if device is None:
|
|
|
|
|
device = CudaInterface.Worker.current_device()
|
|
|
|
|
|
|
|
|
|
if "cuda" not in caching_worker_device_properties:
|
|
|
|
|
device_prop = [
|
|
|
|
|
torch.cuda.get_device_properties(i)
|
|
|
|
|
for i in range(torch.cuda.device_count())
|
|
|
|
|
]
|
|
|
|
|
caching_worker_device_properties["cuda"] = device_prop
|
|
|
|
|
|
|
|
|
|
return caching_worker_device_properties["cuda"][device]
|
|
|
|
|
|
|
|
|
|
current_device = staticmethod(torch.cuda.current_device)
|
|
|
|
|
set_device = staticmethod(torch.cuda.set_device)
|
|
|
|
|
device_count = staticmethod(torch.cuda.device_count)
|
2023-11-08 17:58:55 +00:00
|
|
|
stream = staticmethod(torch.cuda.stream) # type: ignore[assignment]
|
2023-09-24 07:49:16 +00:00
|
|
|
current_stream = staticmethod(torch.cuda.current_stream)
|
2023-11-08 17:58:55 +00:00
|
|
|
set_stream = staticmethod(torch.cuda.set_stream) # type: ignore[assignment]
|
|
|
|
|
_set_stream_by_id = staticmethod(torch.cuda._set_stream_by_id) # type: ignore[assignment]
|
2023-09-24 07:49:16 +00:00
|
|
|
synchronize = staticmethod(torch.cuda.synchronize)
|
2023-11-08 17:58:55 +00:00
|
|
|
get_device_properties = staticmethod(torch.cuda.get_device_properties) # type: ignore[assignment]
|
|
|
|
|
get_raw_stream = staticmethod(get_cuda_stream) # type: ignore[arg-type]
|
2024-04-12 18:21:27 +00:00
|
|
|
exchange_device = staticmethod(torch.cuda._exchange_device) # type: ignore[arg-type]
|
|
|
|
|
maybe_exchange_device = staticmethod(torch.cuda._maybe_exchange_device) # type: ignore[arg-type]
|
2023-09-24 07:49:16 +00:00
|
|
|
|
|
|
|
|
# Can be mock patched by @patch decorator.
|
|
|
|
|
@staticmethod
|
|
|
|
|
def is_available() -> bool:
|
|
|
|
|
return torch.cuda.is_available()
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_compute_capability(device: _device_t = None):
|
2024-04-25 20:44:27 +00:00
|
|
|
if torch.version.hip is None:
|
|
|
|
|
major, min = torch.cuda.get_device_capability(device)
|
|
|
|
|
return major * 10 + min
|
|
|
|
|
else:
|
|
|
|
|
return torch.cuda.get_device_properties(device).gcnArchName.split(":", 1)[0]
|
2023-09-24 07:49:16 +00:00
|
|
|
|
|
|
|
|
|
2024-04-02 15:55:48 +00:00
|
|
|
get_xpu_stream: Optional[Callable[[int], int]]
|
|
|
|
|
if torch.xpu._is_compiled():
|
|
|
|
|
from torch._C import _xpu_getCurrentRawStream as get_xpu_stream
|
|
|
|
|
else:
|
|
|
|
|
get_xpu_stream = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class XpuInterface(DeviceInterface):
|
|
|
|
|
device = torch.xpu.device
|
|
|
|
|
Event = torch.xpu.Event
|
|
|
|
|
Stream = torch.xpu.Stream
|
|
|
|
|
|
|
|
|
|
class Worker:
|
|
|
|
|
@staticmethod
|
|
|
|
|
def set_device(device: int):
|
|
|
|
|
caching_worker_current_devices["xpu"] = device
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def current_device() -> int:
|
|
|
|
|
if "xpu" in caching_worker_current_devices:
|
|
|
|
|
return caching_worker_current_devices["xpu"]
|
|
|
|
|
return torch.xpu.current_device()
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_device_properties(device: _device_t = None):
|
|
|
|
|
if device is not None:
|
|
|
|
|
if isinstance(device, str):
|
|
|
|
|
device = torch.device(device)
|
|
|
|
|
assert device.type == "xpu"
|
|
|
|
|
if isinstance(device, torch.device):
|
|
|
|
|
device = device.index
|
|
|
|
|
if device is None:
|
|
|
|
|
device = XpuInterface.Worker.current_device()
|
|
|
|
|
|
|
|
|
|
if "xpu" not in caching_worker_device_properties:
|
|
|
|
|
device_prop = [
|
|
|
|
|
torch.xpu.get_device_properties(i)
|
|
|
|
|
for i in range(torch.xpu.device_count())
|
|
|
|
|
]
|
|
|
|
|
caching_worker_device_properties["xpu"] = device_prop
|
|
|
|
|
|
|
|
|
|
return caching_worker_device_properties["xpu"][device]
|
|
|
|
|
|
|
|
|
|
current_device = staticmethod(torch.xpu.current_device)
|
|
|
|
|
set_device = staticmethod(torch.xpu.set_device)
|
|
|
|
|
device_count = staticmethod(torch.xpu.device_count)
|
|
|
|
|
stream = staticmethod(torch.xpu.stream) # type: ignore[assignment]
|
|
|
|
|
current_stream = staticmethod(torch.xpu.current_stream)
|
|
|
|
|
set_stream = staticmethod(torch.xpu.set_stream) # type: ignore[assignment]
|
|
|
|
|
_set_stream_by_id = staticmethod(torch.xpu._set_stream_by_id) # type: ignore[assignment]
|
|
|
|
|
synchronize = staticmethod(torch.xpu.synchronize)
|
|
|
|
|
get_device_properties = staticmethod(torch.xpu.get_device_properties) # type: ignore[assignment]
|
|
|
|
|
get_raw_stream = staticmethod(get_xpu_stream) # type: ignore[arg-type]
|
2024-04-12 18:21:27 +00:00
|
|
|
exchange_device = staticmethod(torch.xpu._exchange_device) # type: ignore[arg-type]
|
|
|
|
|
maybe_exchange_device = staticmethod(torch.xpu._maybe_exchange_device) # type: ignore[arg-type]
|
2024-04-02 15:55:48 +00:00
|
|
|
|
|
|
|
|
# Can be mock patched by @patch decorator.
|
|
|
|
|
@staticmethod
|
|
|
|
|
def is_available() -> bool:
|
|
|
|
|
return torch.xpu.is_available()
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_compute_capability(device: _device_t = None):
|
|
|
|
|
cc = torch.xpu.get_device_capability(device)
|
|
|
|
|
return cc
|
|
|
|
|
|
|
|
|
|
|
2023-11-08 17:58:55 +00:00
|
|
|
device_interfaces: Dict[str, Type[DeviceInterface]] = {}
|
2024-03-29 17:22:14 +00:00
|
|
|
_device_initialized = False
|
2023-09-24 07:49:16 +00:00
|
|
|
|
|
|
|
|
|
2024-01-12 19:03:02 +00:00
|
|
|
def register_interface_for_device(
|
|
|
|
|
device: Union[str, torch.device], device_interface: Type[DeviceInterface]
|
|
|
|
|
):
|
|
|
|
|
if isinstance(device, torch.device):
|
|
|
|
|
device = str(device)
|
2023-09-24 07:49:16 +00:00
|
|
|
device_interfaces[device] = device_interface
|
|
|
|
|
|
|
|
|
|
|
2024-01-12 19:03:02 +00:00
|
|
|
def get_interface_for_device(device: Union[str, torch.device]) -> Type[DeviceInterface]:
|
|
|
|
|
if isinstance(device, torch.device):
|
|
|
|
|
device = str(device)
|
2024-03-29 17:22:14 +00:00
|
|
|
if not _device_initialized:
|
|
|
|
|
init_device_reg()
|
2023-11-08 17:58:55 +00:00
|
|
|
if device in device_interfaces:
|
|
|
|
|
return device_interfaces[device]
|
|
|
|
|
raise NotImplementedError(f"No interface for device {device}")
|
2023-09-24 07:49:16 +00:00
|
|
|
|
|
|
|
|
|
2023-11-08 17:58:55 +00:00
|
|
|
def get_registered_device_interfaces() -> Iterable[Tuple[str, Type[DeviceInterface]]]:
|
2024-03-29 17:22:14 +00:00
|
|
|
if not _device_initialized:
|
|
|
|
|
init_device_reg()
|
2023-09-24 07:49:16 +00:00
|
|
|
return device_interfaces.items()
|
|
|
|
|
|
|
|
|
|
|
2024-03-29 17:22:14 +00:00
|
|
|
def init_device_reg():
|
|
|
|
|
global _device_initialized
|
|
|
|
|
register_interface_for_device("cuda", CudaInterface)
|
|
|
|
|
for i in range(torch.cuda.device_count()):
|
|
|
|
|
register_interface_for_device(f"cuda:{i}", CudaInterface)
|
2024-04-02 15:55:48 +00:00
|
|
|
|
|
|
|
|
register_interface_for_device("xpu", XpuInterface)
|
|
|
|
|
for i in range(torch.xpu.device_count()):
|
|
|
|
|
register_interface_for_device(f"xpu:{i}", XpuInterface)
|
|
|
|
|
|
2024-03-29 17:22:14 +00:00
|
|
|
_device_initialized = True
|