__init__.py 1.5 KB
Newer Older
1
from .interface import Platform, PlatformEnum, UnspecifiedPlatform
2

3
current_platform: Platform
4

5
6
7
8
9
10
# NOTE: we don't use `torch.version.cuda` / `torch.version.hip` because
# they only indicate the build configuration, not the runtime environment.
# For example, people can install a cuda build of pytorch but run on tpu.

is_tpu = False
try:
11
12
13
14
    # While it's technically possible to install libtpu on a non-TPU machine,
    # this is a very uncommon scenario. Therefore, we assume that libtpu is
    # installed if and only if the machine has TPUs.
    import libtpu  # noqa: F401
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    is_tpu = True
except Exception:
    pass

is_cuda = False

try:
    import pynvml
    pynvml.nvmlInit()
    try:
        if pynvml.nvmlDeviceGetCount() > 0:
            is_cuda = True
    finally:
        pynvml.nvmlShutdown()
except Exception:
    pass

is_rocm = False

34
try:
35
36
37
38
39
40
41
42
43
    import amdsmi
    amdsmi.amdsmi_init()
    try:
        if len(amdsmi.amdsmi_get_processor_handles()) > 0:
            is_rocm = True
    finally:
        amdsmi.amdsmi_shut_down()
except Exception:
    pass
44

45
if is_tpu:
46
47
48
49
    # people might install pytorch built with cuda but run on tpu
    # so we need to check tpu first
    from .tpu import TpuPlatform
    current_platform = TpuPlatform()
50
elif is_cuda:
51
52
    from .cuda import CudaPlatform
    current_platform = CudaPlatform()
53
elif is_rocm:
54
55
56
    from .rocm import RocmPlatform
    current_platform = RocmPlatform()
else:
57
    current_platform = UnspecifiedPlatform()
58
59

__all__ = ['Platform', 'PlatformEnum', 'current_platform']