__init__.py 2.23 KB
Newer Older
zhuwenwen's avatar
zhuwenwen committed
1
import torch
2
from .interface import Platform, PlatformEnum, UnspecifiedPlatform
3

4
current_platform: Platform
5

6
7
8
9
10
11
# 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:
12
13
14
15
    # 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
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    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

35
try:
zhuwenwen's avatar
zhuwenwen committed
36
37
38
39
40
41
42
43
44
    if torch.version.hip is not None:
        is_rocm = True
    # import amdsmi
    # amdsmi.amdsmi_init()
    # try:
    #     if len(amdsmi.amdsmi_get_processor_handles()) > 0:
    #         is_rocm = True
    # finally:
    #     amdsmi.amdsmi_shut_down()
45
46
except Exception:
    pass
47

48
49
50
51
52
53
54
55
56
is_xpu = False

try:
    import torch
    if hasattr(torch, 'xpu') and torch.xpu.is_available():
        is_xpu = True
except Exception:
    pass

57
58
59
60
61
62
63
is_cpu = False
try:
    from importlib.metadata import version
    is_cpu = "cpu" in version("vllm")
except Exception:
    pass

64
65
66
67
68
69
70
is_neuron = False
try:
    import transformers_neuronx  # noqa: F401
    is_neuron = True
except ImportError:
    pass

71
if is_tpu:
72
73
74
75
    # 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()
76
elif is_cuda:
77
78
    from .cuda import CudaPlatform
    current_platform = CudaPlatform()
79
elif is_rocm:
80
81
    from .rocm import RocmPlatform
    current_platform = RocmPlatform()
82
83
84
elif is_xpu:
    from .xpu import XPUPlatform
    current_platform = XPUPlatform()
85
86
87
elif is_cpu:
    from .cpu import CpuPlatform
    current_platform = CpuPlatform()
88
89
90
elif is_neuron:
    from .neuron import NeuronPlatform
    current_platform = NeuronPlatform()
91
else:
92
    current_platform = UnspecifiedPlatform()
93
94

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