2025-06-27 00:27:18 +08:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2025-07-04 15:40:42 +08:00
|
|
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
2025-08-27 15:27:14 +08:00
|
|
|
from contextlib import contextmanager
|
2025-06-27 00:27:18 +08:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
|
|
import torch
|
|
|
|
|
|
|
|
|
|
from vllm.config import VllmConfig
|
|
|
|
|
from vllm.logger import init_logger
|
2026-02-25 14:22:52 +08:00
|
|
|
from vllm.utils.torch_utils import supports_xpu_graph
|
2026-03-06 01:19:18 +08:00
|
|
|
from vllm.v1.worker.gpu.model_runner import (
|
|
|
|
|
GPUModelRunner as GPUModelRunnerV2,
|
|
|
|
|
)
|
2025-06-27 00:27:18 +08:00
|
|
|
from vllm.v1.worker.gpu_model_runner import GPUModelRunner
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
logger = init_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class XPUModelRunner(GPUModelRunner):
|
|
|
|
|
"""A model runner for XPU devices."""
|
|
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
vllm_config: VllmConfig,
|
|
|
|
|
device: torch.device,
|
|
|
|
|
):
|
2025-08-27 15:27:14 +08:00
|
|
|
with _torch_cuda_wrapper():
|
|
|
|
|
super().__init__(vllm_config, device)
|
2025-06-27 00:27:18 +08:00
|
|
|
# FIXME: To be verified.
|
|
|
|
|
self.cascade_attn_enabled = False
|
|
|
|
|
|
2025-08-27 15:27:14 +08:00
|
|
|
|
2026-03-06 01:19:18 +08:00
|
|
|
class XPUModelRunnerV2(GPUModelRunnerV2):
|
|
|
|
|
"""A model runner for XPU devices."""
|
|
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
vllm_config: VllmConfig,
|
|
|
|
|
device: torch.device,
|
|
|
|
|
):
|
|
|
|
|
with _torch_cuda_wrapper():
|
|
|
|
|
super().__init__(vllm_config, device)
|
|
|
|
|
|
|
|
|
|
|
2025-08-27 15:27:14 +08:00
|
|
|
@contextmanager
|
|
|
|
|
def _torch_cuda_wrapper():
|
|
|
|
|
try:
|
2025-09-17 14:45:25 +08:00
|
|
|
# replace cuda APIs with xpu APIs, this should work by default
|
|
|
|
|
torch.cuda.Stream = torch.xpu.Stream
|
|
|
|
|
torch.cuda.default_stream = torch.xpu.current_stream
|
|
|
|
|
torch.cuda.current_stream = torch.xpu.current_stream
|
|
|
|
|
torch.cuda.stream = torch.xpu.stream
|
2026-02-25 14:22:52 +08:00
|
|
|
torch.cuda.mem_get_info = torch.xpu.mem_get_info
|
2026-03-06 01:19:18 +08:00
|
|
|
torch.cuda.Event = torch.Event
|
|
|
|
|
torch.cuda.set_stream = torch.xpu.set_stream
|
2026-02-25 14:22:52 +08:00
|
|
|
if supports_xpu_graph():
|
|
|
|
|
torch.cuda.graph = torch.xpu.graph
|
|
|
|
|
torch.cuda.CUDAGraph = torch.xpu.XPUGraph
|
2026-03-06 01:19:18 +08:00
|
|
|
torch.cuda.graph_pool_handle = torch.xpu.graph_pool_handle
|
2025-08-27 15:27:14 +08:00
|
|
|
yield
|
|
|
|
|
finally:
|
2025-11-19 03:34:36 +08:00
|
|
|
pass
|