[XPU] Fix CUDA API shims breaking Torch Dynamo during AOT compile (#43092)
Signed-off-by: Łukasz Ślusarczyk <lukasz.slusarczyk@intel.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Kunshang Ji <kunshang.ji@intel.com>
This commit is contained in:
co-authored by
mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Kunshang Ji
parent
b71218107f
commit
78a04c208d
@@ -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 <function current_stream ...>
|
||||
|
||||
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()
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user