__init__.py 2.39 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
46
47
48
49
50
51
52
53
is_xpu = False

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

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

61
62
63
64
65
66
67
is_neuron = False
try:
    import transformers_neuronx  # noqa: F401
    is_neuron = True
except ImportError:
    pass

68
69
70
71
72
73
74
is_openvino = False
try:
    from importlib.metadata import version
    is_openvino = "openvino" in version("vllm")
except Exception:
    pass

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

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