interface.py 2.63 KB
Newer Older
1
import enum
2
from typing import NamedTuple, Optional, Tuple, Union
3

4
5
import torch

6
7
8
9

class PlatformEnum(enum.Enum):
    CUDA = enum.auto()
    ROCM = enum.auto()
10
    TPU = enum.auto()
11
    CPU = enum.auto()
12
    UNSPECIFIED = enum.auto()
13
14


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class DeviceCapability(NamedTuple):
    major: int
    minor: int

    def as_version_str(self) -> str:
        return f"{self.major}.{self.minor}"

    def to_int(self) -> int:
        """
        Express device capability as an integer ``<major><minor>``.

        It is assumed that the minor version is always a single digit.
        """
        assert 0 <= self.minor < 10
        return self.major * 10 + self.minor


32
33
34
35
36
37
38
39
40
class Platform:
    _enum: PlatformEnum

    def is_cuda(self) -> bool:
        return self._enum == PlatformEnum.CUDA

    def is_rocm(self) -> bool:
        return self._enum == PlatformEnum.ROCM

41
42
43
    def is_tpu(self) -> bool:
        return self._enum == PlatformEnum.TPU

44
45
46
    def is_cpu(self) -> bool:
        return self._enum == PlatformEnum.CPU

47
48
49
50
51
52
53
54
55
56
    def is_cuda_alike(self) -> bool:
        """Stateless version of :func:`torch.cuda.is_available`."""
        return self._enum in (PlatformEnum.CUDA, PlatformEnum.ROCM)

    @classmethod
    def get_device_capability(
        cls,
        device_id: int = 0,
    ) -> Optional[DeviceCapability]:
        """Stateless version of :func:`torch.cuda.get_device_capability`."""
57
        return None
58

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    @classmethod
    def has_device_capability(
        cls,
        capability: Union[Tuple[int, int], int],
        device_id: int = 0,
    ) -> bool:
        """
        Test whether this platform is compatible with a device capability.

        The ``capability`` argument can either be:

        - A tuple ``(major, minor)``.
        - An integer ``<major><minor>``. (See :meth:`DeviceCapability.to_int`)
        """
        current_capability = cls.get_device_capability(device_id=device_id)
        if current_capability is None:
            return False

        if isinstance(capability, tuple):
            return current_capability >= capability

        return current_capability.to_int() >= capability

    @classmethod
    def get_device_name(cls, device_id: int = 0) -> str:
84
85
        raise NotImplementedError

86
87
    @classmethod
    def inference_mode(cls):
88
89
90
91
92
93
94
95
96
97
98
        """A device-specific wrapper of `torch.inference_mode`.

        This wrapper is recommended because some hardware backends such as TPU
        do not support `torch.inference_mode`. In such a case, they will fall
        back to `torch.no_grad` by overriding this method.
        """
        return torch.inference_mode(mode=True)


class UnspecifiedPlatform(Platform):
    _enum = PlatformEnum.UNSPECIFIED