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

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