Skip to content
Merged
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
4 changes: 2 additions & 2 deletions tests/compile/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
from contextlib import nullcontext
from copy import deepcopy

import depyf
from torch import fx
from torch._ops import OpOverload, OpOverloadPacket
from torch.fx._utils import lazy_format_graph_code

from vllm.compilation import depyf_compat
from vllm.compilation.passes.fx_utils import find_op_nodes
from vllm.compilation.passes.inductor_pass import (
InductorPass,
Expand Down Expand Up @@ -71,7 +71,7 @@ def __init__(self, *passes: InductorPass | Callable[[fx.Graph], None]):

if debug_dump_path := vllm_config.compile_debug_dump_path():
logger.debug("Dumping depyf output to %s", debug_dump_path)
self.debug_ctx = depyf.prepare_debug(debug_dump_path.as_posix())
self.debug_ctx = depyf_compat.prepare_debug(debug_dump_path.as_posix())
else:
self.debug_ctx = nullcontext()

Expand Down
28 changes: 17 additions & 11 deletions tests/compile/fullgraph/test_full_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,6 @@ def test_full_graph(
)
for model_info in models_list(keywords=["FP8-dynamic"])
]
+ [
# Test depyf integration works
(
CompilationConfig(
mode=CompilationMode.VLLM_COMPILE,
debug_dump_path=Path(tempfile.gettempdir()),
),
"facebook/opt-125m",
{},
),
]
+ [
# graph inductor partition
(
Expand Down Expand Up @@ -149,6 +138,23 @@ def test_custom_compile_config(
run_model(compilation_config, model, **model_kwargs)


@create_new_process_for_each_test()
def test_depyf_integration(disable_vllm_compile_cache):
"""Compile with depyf's debugging hooks installed.

``disable_vllm_compile_cache`` is what makes this a test rather than a
formality: depyf only ever sees a compilation that actually runs, so served
from a warm cache this exercises none of its hooks.
"""
run_model(
CompilationConfig(
mode=CompilationMode.VLLM_COMPILE,
debug_dump_path=Path(tempfile.gettempdir()),
),
"facebook/opt-125m",
)


def run_model(
compile_config: CompilationMode | CompilationConfig, model: str, **model_kwargs
):
Expand Down
84 changes: 84 additions & 0 deletions tests/compile/test_depyf_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Tests for the depyf ``load_by_key_path`` patch in compilation/depyf_compat.py.

depyf replaces ``torch._inductor.codecache.PyCodeCache.load_by_key_path`` with
a hook that dumps each generated kernel before loading it. That hook's
signature is frozen at ``(key, path, linemap, attrs)`` while Inductor also
passes ``set_sys_modules``, so the patch re-wraps it to forward keywords depyf
does not know about.

End-to-end coverage lives in
``tests/compile/fullgraph/test_full_graph.py::test_depyf_integration``, which
needs a GPU and a cold compile cache. These tests pin the same behaviour by
driving ``PyCodeCache`` directly, so they catch the incompatibility in seconds
on CPU -- which is where it should have been caught in the first place.
"""

import sys

import depyf.explain.enable_debugging as enable_debugging
import torch._inductor.codecache as codecache

from vllm.compilation import depyf_compat

# Captured at import, which pytest does before any test can install the patch.
PRISTINE_HOOK = enable_debugging.patched_load_by_key_path


class TestPatchedHook:
"""The hook has to survive the call *and* keep both sides' behaviour."""

def test_load_by_key_path_accepts_set_sys_modules(self, tmp_path):
with depyf_compat.prepare_debug(str(tmp_path)):
key, path = codecache.PyCodeCache.write("value = 1\n")
module = codecache.PyCodeCache.load_by_key_path(
key, path, set_sys_modules=False
)

assert module.value == 1

def test_set_sys_modules_is_forwarded_not_swallowed(self, tmp_path):
# Dropping the keyword would also make the call succeed, but it would
# re-register benchmark modules in sys.modules and bring back the
# exhaustion that pytorch#184285 fixed.
with depyf_compat.prepare_debug(str(tmp_path)):
key, path = codecache.PyCodeCache.write("value = 2\n")
module = codecache.PyCodeCache.load_by_key_path(
key, path, set_sys_modules=False
)
registered = module.__name__ in sys.modules

sys.modules.pop(module.__name__, None)
assert not registered

def test_depyf_still_dumps_the_generated_kernel(self, tmp_path):
# The patch reimplements depyf's hook body rather than wrapping it,
# so this is what catches us drifting away from what depyf does.
with depyf_compat.prepare_debug(str(tmp_path)):
key, path = codecache.PyCodeCache.write("value = 3\n")
codecache.PyCodeCache.load_by_key_path(key, path, set_sys_modules=False)

dumped = list(tmp_path.rglob("*.kernel_*.py"))
assert dumped, f"depyf wrote no kernel dump into {tmp_path}"
assert "value = 3" in dumped[0].read_text()


class TestPatchApplication:
def test_patch_is_still_needed(self):
assert not depyf_compat._hook_accepts_inductor_keywords(PRISTINE_HOOK), (
"depyf's load_by_key_path now accepts everything Inductor passes; "
"drop vllm/compilation/depyf_compat.py and this test"
)

def test_patch_is_skipped_when_unnecessary(self):
def accepts_anything(*args, **kwargs):
pass

assert depyf_compat._hook_accepts_inductor_keywords(accepts_anything)

def test_patch_is_idempotent(self, tmp_path):
with depyf_compat.prepare_debug(str(tmp_path)):
first = enable_debugging.patched_load_by_key_path
with depyf_compat.prepare_debug(str(tmp_path)):
assert enable_debugging.patched_load_by_key_path is first
90 changes: 90 additions & 0 deletions vllm/compilation/depyf_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Keep depyf usable against Inductor versions newer than its last release.

``depyf.prepare_debug`` replaces three torch functions with hooks of its own.
Two of them tolerate new arguments, but ``load_by_key_path`` has its signature
frozen at ``(key, path, linemap, attrs)``, and Inductor now also passes
``set_sys_modules`` (pytorch#184285). Every compilation that reaches an
autotune code block while depyf is active therefore dies with a ``TypeError``,
which surfaces as ``InductorError: Failed to run autotuning code block``.

depyf has hit this before -- ``linemap`` and ``attrs`` broke the same hook in
thuml/depyf#80 -- but 0.20.0 is both the pinned version and the last release
upstream published, so a version bump cannot resolve it. Patch the hook here
instead, following what depyf itself does for ``lazy_format_graph_code``:
accept ``**kwargs`` and forward them.

Route every depyf entry point through ``prepare_debug`` below so the patch is
never missed.
"""

import inspect
import os
from collections.abc import Callable
from contextlib import AbstractContextManager
from typing import Any

from vllm.logger import init_logger

logger = init_logger(__name__)

_patched = False


def _hook_accepts_inductor_keywords(hook: Callable[..., Any]) -> bool:
"""Whether depyf's hook already takes everything Inductor passes it.

Compares against the live ``PyCodeCache.load_by_key_path``, so this only
means anything before depyf swaps that out for the hook.
"""
import torch._inductor.codecache as codecache

accepted = inspect.signature(hook).parameters
if any(p.kind is inspect.Parameter.VAR_KEYWORD for p in accepted.values()):
return True
expected = inspect.signature(codecache.PyCodeCache.load_by_key_path).parameters
return expected.keys() <= accepted.keys()


def _patch_load_by_key_path() -> None:
"""Forward unrecognised keywords through depyf's ``load_by_key_path`` hook.

Mirrors depyf's own implementation -- dump the generated source alongside
the rest of the debug output, then defer to the function depyf displaced --
and adds ``**kwargs`` so the call survives arguments depyf never knew about.
"""
import depyf.explain.enable_debugging as enable_debugging
from depyf.explain.global_variables import data
from depyf.explain.utils import (
get_current_compiled_fn_name,
write_code_to_file_template,
)

def load_by_key_path(key, path, linemap=None, attrs=None, **kwargs):
with open(path) as f:
src = f.read()
func_name = get_current_compiled_fn_name()
dumped_path = write_code_to_file_template(
src, os.path.join(data["dump_src_dir"], func_name + ".kernel_%s.py")
)
unpatched = data["unpatched_load_by_key_path"]
return unpatched(key, dumped_path, linemap, attrs, **kwargs)

enable_debugging.patched_load_by_key_path = load_by_key_path


def prepare_debug(dump_src_dir: str) -> AbstractContextManager[None]:
"""``depyf.prepare_debug`` with the compatibility patches applied."""
global _patched
import depyf
import depyf.explain.enable_debugging as enable_debugging

if not _patched and not _hook_accepts_inductor_keywords(
enable_debugging.patched_load_by_key_path
):
logger.debug("Patching depyf's load_by_key_path hook for this Inductor")
_patch_load_by_key_path()
_patched = True

return depyf.prepare_debug(dump_src_dir)
4 changes: 2 additions & 2 deletions vllm/compilation/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ def monitor_torch_compile(
depyf_cm = None
path = vllm_config.compile_debug_dump_path()
if compilation_config.mode == CompilationMode.VLLM_COMPILE and path:
import depyf
from vllm.compilation import depyf_compat

path.mkdir(parents=True, exist_ok=True)
logger.debug("Dumping depyf output to %s", path)
depyf_cm = depyf.prepare_debug(path.as_posix())
depyf_cm = depyf_compat.prepare_debug(path.as_posix())
depyf_cm.__enter__()

try:
Expand Down
Loading