__init__.py 2.8 KB
Newer Older
1
from .interface import _Backend  # noqa: F401
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:
36
37
38
39
40
41
42
43
44
    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
45

46
47
48
49
50
51
52
is_hpu = False
try:
    from importlib import util
    is_hpu = util.find_spec('habana_frameworks') is not None
except Exception:
    pass

53
54
55
is_xpu = False

try:
56
57
58
    # installed IPEX if the machine has XPUs.
    import intel_extension_for_pytorch  # noqa: F401
    import oneccl_bindings_for_pytorch  # noqa: F401
59
60
61
62
63
64
    import torch
    if hasattr(torch, 'xpu') and torch.xpu.is_available():
        is_xpu = True
except Exception:
    pass

65
66
67
68
69
70
71
is_cpu = False
try:
    from importlib.metadata import version
    is_cpu = "cpu" in version("vllm")
except Exception:
    pass

72
73
74
75
76
77
78
is_neuron = False
try:
    import transformers_neuronx  # noqa: F401
    is_neuron = True
except ImportError:
    pass

79
80
81
82
83
84
85
is_openvino = False
try:
    from importlib.metadata import version
    is_openvino = "openvino" in version("vllm")
except Exception:
    pass

86
if is_tpu:
87
88
89
90
    # 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()
91
elif is_cuda:
92
93
    from .cuda import CudaPlatform
    current_platform = CudaPlatform()
94
elif is_rocm:
95
96
    from .rocm import RocmPlatform
    current_platform = RocmPlatform()
97
98
99
elif is_hpu:
    from .hpu import HpuPlatform
    current_platform = HpuPlatform()
100
101
102
elif is_xpu:
    from .xpu import XPUPlatform
    current_platform = XPUPlatform()
103
104
105
elif is_cpu:
    from .cpu import CpuPlatform
    current_platform = CpuPlatform()
106
107
108
elif is_neuron:
    from .neuron import NeuronPlatform
    current_platform = NeuronPlatform()
109
110
111
elif is_openvino:
    from .openvino import OpenVinoPlatform
    current_platform = OpenVinoPlatform()
112
else:
113
    current_platform = UnspecifiedPlatform()
114
115

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