Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ def _read_requirements(filename: str) -> list[str]:
install_requires=get_requirements(),
ext_modules=ext_modules,
extras_require={},
data_files=[
# Install a .pth file so the torch compat shim runs at Python startup,
# before ``import vllm`` triggers env_override.py.
(".", ["vllm_gaudi_torch_compat.pth"]),
],
Comment on lines +65 to +69
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Installing the .pth via data_files=[('.', ...)] typically places it under the scheme's data directory (often sys.prefix), not the purelib/site-packages directory where Python actually processes .pth files at startup. If it doesn't land in site-packages, the runtime shim won't run and the original import vllm failure will persist.

Please adjust the installation approach so the .pth ends up in site-packages (or use an alternative startup mechanism that is reliably loaded from site-packages).

Suggested change
data_files=[
# Install a .pth file so the torch compat shim runs at Python startup,
# before ``import vllm`` triggers env_override.py.
(".", ["vllm_gaudi_torch_compat.pth"]),
],

Copilot uses AI. Check for mistakes.
entry_points={
"vllm.platform_plugins": ["hpu = vllm_gaudi:register"],
"vllm.general_plugins": [
Expand Down
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
"""Root-level conftest – ensures torch compatibility shims are applied
before any ``import vllm`` happens during the test session.
"""

import vllm_gaudi._torch_compat # noqa: F401 -- side-effect: patches GraphCaptureOutput alias
Comment on lines +2 to +6
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import executes vllm_gaudi/__init__.py before _torch_compat.py runs; __init__.py imports vllm_gaudi.platform which imports vllm (from vllm import envs). That means this conftest does not guarantee the shim is applied before any import vllm, contrary to the module docstring, and may fail in the same way the shim is trying to prevent.

To ensure ordering, import a shim that doesn't import the vllm_gaudi package (or make vllm_gaudi/__init__.py lazy) and then apply the patch from there.

Copilot uses AI. Check for mistakes.
42 changes: 42 additions & 0 deletions vllm_gaudi/_torch_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# SPDX-License-Identifier: Apache-2.0
"""Torch compatibility shim for Gaudi's custom PyTorch builds.

Gaudi's PyTorch build (2.9+hpu) cherry-picked the builtins fix from
upstream PyTorch (pytorch/177558), which renamed ``GraphCaptureOutput``
to ``CaptureOutput`` and removed the ``get_runtime_env`` method.

vLLM's ``env_override.py`` (guarded by ``not is_torch_equal_or_newer("2.12.0")``)
tries to import ``GraphCaptureOutput`` and patch its ``get_runtime_env``.
On Gaudi's build this block must be skipped because the fix is already applied.

We inject a stub ``GraphCaptureOutput`` class with a ``get_runtime_env``
class-method so that ``env_override.py`` can import and "patch" it without
error. The patched method is never actually called because the underlying
PyTorch code already contains the fix.

This module is loaded:
* In tests – via ``tests/conftest.py`` (runs before any ``import vllm``).
* At runtime – via a ``.pth`` file installed into site-packages so that
the shim is in place before *any* Python code imports ``vllm``.
"""

try:
import torch._dynamo.convert_frame as _cf

if not hasattr(_cf, "GraphCaptureOutput"):
# The Gaudi PyTorch build already has the builtins fix applied;
# create a stub so that env_override.py can import and monkey-patch
# it harmlessly.

class _GraphCaptureOutputStub:
"""Stub standing in for the removed GraphCaptureOutput class."""

def get_runtime_env(self): # type: ignore[override]
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring says the stub provides a get_runtime_env class-method, but the stub defines it as an instance method. Even if it's "never called", matching the documented/expected callable shape is safer because the upstream monkey-patch may treat it as a classmethod.

Define get_runtime_env as a @classmethod (or otherwise match the upstream signature) to avoid subtle incompatibilities.

Suggested change
def get_runtime_env(self): # type: ignore[override]
@classmethod
def get_runtime_env(cls): # type: ignore[override]

Copilot uses AI. Check for mistakes.
"""No-op — the real fix is already in this PyTorch build."""
return None

_cf.GraphCaptureOutput = _GraphCaptureOutputStub # type: ignore[attr-defined]
except Exception:
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This catches all exceptions and silently ignores them. Since this module is meant to protect imports, swallowing unexpected errors (e.g., API changes, AttributeError, RuntimeError during torch init) can make failures much harder to diagnose.

Prefer catching the specific expected failures (e.g., ImportError / ModuleNotFoundError) and, if you still want to proceed silently, consider at least logging at debug level for unexpected exception types.

Suggested change
except Exception:
except (ImportError, ModuleNotFoundError):

Copilot uses AI. Check for mistakes.
# If torch._dynamo.convert_frame is unavailable, there is nothing
# to patch – silently continue.
pass
2 changes: 1 addition & 1 deletion vllm_gaudi/models/deepseek_ocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from vllm.config import VllmConfig
from vllm.config.multimodal import BaseDummyOptions
from vllm.multimodal import MULTIMODAL_REGISTRY
from vllm.multimodal.inputs import MultiModalDataDict
from vllm.inputs import MultiModalDataDict
from vllm.model_executor.models.deepseek_ocr import (
DeepseekOCRForCausalLM,
DeepseekOCRMultiModalProcessor,
Expand Down
1 change: 1 addition & 0 deletions vllm_gaudi_torch_compat.pth
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import vllm_gaudi._torch_compat
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .pth imports vllm_gaudi._torch_compat, which first executes vllm_gaudi/__init__.py. That module imports vllm_gaudi.platform, and platform.py imports vllm (from vllm import envs), so this startup hook can end up importing vllm before the shim runs—defeating the intended ordering and potentially re-triggering the original env_override.py failure.

Consider changing the .pth to import a standalone shim module that does not import the vllm_gaudi package (or refactor vllm_gaudi/__init__.py to avoid importing platform at import-time) so the patch can be applied without pulling in vllm.

Suggested change
import vllm_gaudi._torch_compat
import vllm_gaudi_torch_compat

Copilot uses AI. Check for mistakes.
Loading