xpu.py 4.47 KB
Newer Older
1
from typing import TYPE_CHECKING, Optional
2

3
4
import torch

5
6
7
8
from vllm.logger import init_logger

from .interface import DeviceCapability, Platform, PlatformEnum, _Backend

9
10
11
12
13
if TYPE_CHECKING:
    from vllm.config import VllmConfig
else:
    VllmConfig = None

14
logger = init_logger(__name__)
15
16
17
18


class XPUPlatform(Platform):
    _enum = PlatformEnum.XPU
19
    device_name: str = "xpu"
20
    device_type: str = "xpu"
21
    dispatch_key: str = "XPU"
22
23
24
    # Intel XPU's device key is "GPU" for Ray.
    # see https://github.com/ray-project/ray/blob/6a5eb5865eeb9ccf058a79b44f107e327e360673/python/ray/_private/accelerators/intel_gpu.py#L20 # noqa: E501
    ray_device_key: str = "GPU"
25
    device_control_env_var: str = "ONEAPI_DEVICE_SELECTOR"
26

27
    @classmethod
28
29
30
    def get_attn_backend_cls(cls, selected_backend: _Backend, head_size: int,
                             dtype: torch.dtype, kv_cache_dtype: Optional[str],
                             block_size: int, use_v1: bool) -> str:
31
32
        if selected_backend != _Backend.IPEX:
            logger.info("Cannot use %s backend on XPU.", selected_backend)
33
34
        logger.info("Using IPEX attention backend.")
        return "vllm.attention.backends.ipex_attn.IpexAttnBackend"
35

36
37
    @staticmethod
    def get_device_capability(device_id: int = 0) -> DeviceCapability:
38
39
40
        major, minor, *_ = torch.xpu.get_device_capability(
            device_id)['version'].split('.')
        return DeviceCapability(major=int(major), minor=int(minor))
41
42
43
44

    @staticmethod
    def get_device_name(device_id: int = 0) -> str:
        return torch.xpu.get_device_name(device_id)
45
46
47
48
49

    @classmethod
    def get_device_total_memory(cls, device_id: int = 0) -> int:
        device_props = torch.xpu.get_device_properties(device_id)
        return device_props.total_memory
50

51
52
53
54
    @classmethod
    def is_async_output_supported(cls, enforce_eager: Optional[bool]) -> bool:
        return True

55
56
57
    @staticmethod
    def inference_mode():
        return torch.no_grad()
58
59
60

    @classmethod
    def check_and_update_config(cls, vllm_config: VllmConfig) -> None:
61
62
63
64
        cache_config = vllm_config.cache_config
        if cache_config and cache_config.block_size is None:
            cache_config.block_size = 16

65
66
67
68
69
70
71
72
73
74
75
        # check and update model config
        model_config = vllm_config.model_config
        if model_config.dtype == torch.bfloat16:
            logger.warning(
                "bfloat16 is not fully supported on XPU, casting to float16.")
            model_config.dtype = torch.float16
        if not model_config.enforce_eager:
            logger.warning(
                "CUDA graph is not supported on XPU, fallback to the eager "
                "mode.")
            model_config.enforce_eager = True
76

77
78
79
80
        if vllm_config.speculative_config is not None:
            raise NotImplementedError(
                "XPU does not support speculative decoding")

81
82
83
        if vllm_config.device_config is not None:
            assert vllm_config.device_config.device_type == "xpu"

84
85
        # check and update parallel config
        parallel_config = vllm_config.parallel_config
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
        if parallel_config.worker_cls == "auto":
            parallel_config.worker_cls = "vllm.worker.xpu_worker.XPUWorker"

        if parallel_config.distributed_executor_backend is None:
            parallel_config.distributed_executor_backend = "ray"
        elif parallel_config.distributed_executor_backend == "mp":
            # FIXME(kunshang):
            # spawn needs calling `if __name__ == '__main__':``
            # fork is not supported for xpu start new process.
            logger.error(
                "Both start methods (spawn and fork) have issue "
                "on XPU if you use mp backend, setting it to ray instead.")
            parallel_config.distributed_executor_backend = "ray"

        elif parallel_config.distributed_executor_backend != "ray":
101
102
103
104
105
            logger.warning(
                "%s is not supported on XPU, fallback to ray distributed"
                " executor backend.",
                parallel_config.distributed_executor_backend)
            parallel_config.distributed_executor_backend = "ray"
106
107
108
109
110

    @classmethod
    def is_pin_memory_available(cls):
        logger.warning("Pin memory is not supported on XPU.")
        return False
111
112
113
114
115
116
117

    @classmethod
    def get_current_memory_usage(cls,
                                 device: Optional[torch.types.Device] = None
                                 ) -> float:
        torch.xpu.reset_peak_memory_stats(device)
        return torch.xpu.max_memory_allocated(device)