From 78a04c208d1f6fad27a949aa1a7f53ae638e86e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20=C5=9Alusarczyk?= Date: Mon, 6 Jul 2026 02:29:20 +0200 Subject: [PATCH] [XPU] Fix CUDA API shims breaking Torch Dynamo during AOT compile (#43092) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Łukasz Ślusarczyk Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Kunshang Ji --- tests/v1/worker/test_xpu_model_runner.py | 46 ++++++++++++++++++++++++ vllm/v1/worker/xpu_model_runner.py | 17 +++++---- 2 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 tests/v1/worker/test_xpu_model_runner.py diff --git a/tests/v1/worker/test_xpu_model_runner.py b/tests/v1/worker/test_xpu_model_runner.py new file mode 100644 index 0000000000..5ddf490c9b --- /dev/null +++ b/tests/v1/worker/test_xpu_model_runner.py @@ -0,0 +1,46 @@ +# SPDX-License-Identifier: Apache-2.0 +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project + +"""Unit tests for ``vllm.v1.worker.xpu_model_runner`` (XPU worker / CUDA shims).""" + +import pytest +import torch +from torch._dynamo.variables.torch import TorchInGraphFunctionVariable + +from vllm.v1.worker.xpu_model_runner import _torch_cuda_wrapper + +# XPU-only: needs distinct torch.cuda vs torch.xpu current_stream symbols. +pytestmark = pytest.mark.skipif( + not hasattr(torch, "xpu") or not hasattr(torch.xpu, "current_stream"), + reason="torch.xpu.current_stream is required", +) + + +# Child process: patched torch.cuda must not leak to other tests in the session. +@pytest.mark.forked +def test_torch_cuda_wrapper_allows_dynamo_handler_registration() -> None: + """Guard against XPU CUDA shim breaking Torch Dynamo during AOT compile. + + Before the fix, ``_torch_cuda_wrapper`` assigned + ``torch.cuda.current_stream = torch.xpu.current_stream`` (same function object). + On the first AOT/profile run, Dynamo builds its in-graph handler table and + registers ``torch.cuda.current_stream`` and ``torch.xpu.current_stream`` + separately; duplicate identity triggers:: + + AssertionError: Handler already registered for + + That surfaced as EngineCore failing in ``profile_run`` / ``_get_handlers()``. + The fix uses distinct shim callables so both can be registered. + + This test replays the post-init state (wrapper applied, patches left on + ``torch.cuda``) and checks that Dynamo's real ``_get_handlers()`` succeeds. + """ + # Same entry point as XPUModelRunner.__init__ (patches persist after exit). + with _torch_cuda_wrapper(): + pass + + # Fresh handler table build, as on first torch.compile / AOT in the worker. + # Registers torch.cuda.current_stream and torch.xpu.current_stream separately; + # if they are the same object (pre-fix alias), raises Handler already registered. + TorchInGraphFunctionVariable._get_handlers.cache_clear() + TorchInGraphFunctionVariable._get_handlers() diff --git a/vllm/v1/worker/xpu_model_runner.py b/vllm/v1/worker/xpu_model_runner.py index 6cdca994da..52647afa0a 100644 --- a/vllm/v1/worker/xpu_model_runner.py +++ b/vllm/v1/worker/xpu_model_runner.py @@ -1,6 +1,7 @@ # SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from contextlib import contextmanager +from functools import partial import torch @@ -40,14 +41,16 @@ class XPUModelRunnerV2(GPUModelRunnerV2): @contextmanager def _torch_cuda_wrapper(): - # replace cuda APIs with xpu APIs, this should work by default + # Replace cuda APIs with xpu APIs. Each callable gets its own functools.partial + # so it is not the same object as torch.xpu.* (Torch Dynamo _get_handlers() + # asserts on duplicate registration when cuda aliases xpu directly). 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 - torch.cuda.set_stream = torch.xpu.set_stream + torch.cuda.default_stream = partial(torch.xpu.current_stream) + torch.cuda.current_stream = partial(torch.xpu.current_stream) + torch.cuda.stream = partial(torch.xpu.stream) + torch.cuda.set_stream = partial(torch.xpu.set_stream) if supports_xpu_graph(): - torch.cuda.graph = torch.xpu.graph + torch.cuda.graph = partial(torch.xpu.graph) torch.cuda.CUDAGraph = torch.xpu.XPUGraph - torch.cuda.graph_pool_handle = torch.xpu.graph_pool_handle + torch.cuda.graph_pool_handle = partial(torch.xpu.graph_pool_handle) yield