xpu.py 1.79 KB
Newer Older
1
2
from typing import TYPE_CHECKING

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
19


class XPUPlatform(Platform):
    _enum = PlatformEnum.XPU

20
21
22
23
24
25
    @classmethod
    def get_default_attn_backend(cls, selected_backend: _Backend) -> _Backend:
        if selected_backend != _Backend.IPEX:
            logger.info("Cannot use %s backend on XPU.", selected_backend)
        return _Backend.IPEX

26
27
    @staticmethod
    def get_device_capability(device_id: int = 0) -> DeviceCapability:
28
29
30
        major, minor, *_ = torch.xpu.get_device_capability(
            device_id)['version'].split('.')
        return DeviceCapability(major=int(major), minor=int(minor))
31
32
33
34

    @staticmethod
    def get_device_name(device_id: int = 0) -> str:
        return torch.xpu.get_device_name(device_id)
35
36
37
38
39

    @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
40
41
42
43

    @staticmethod
    def inference_mode():
        return torch.no_grad()
44
45
46
47
48
49
50
51
52
53
54
55
56
57

    @classmethod
    def check_and_update_config(cls, vllm_config: VllmConfig) -> None:
        # 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