Skip to content

Commit 4052b4c

Browse files
committed
[py_connector] make connector unit tests runnable without torch
The open-source CI image has no torch/triton/orjson/zmq/requests, so the three pure-logic py_tests failed at import time. vllm_stubs now registers stand-ins for missing third-party deps (real modules always win), the connector uses typing.TYPE_CHECKING instead of typing_extensions, and the GPU-only Triton kernel test is tagged manual like the other GPU suites.
1 parent f909e82 commit 4052b4c

3 files changed

Lines changed: 38 additions & 6 deletions

File tree

kv_cache_manager/py_connector/test/kernel/BUILD

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ py_test(
88
],
99
main = "test_strided_gather_scatter.py",
1010
tags = [
11-
"no-remote-exec",
12-
"gpu", # requires 1 GPU
1311
"exclusive", # GPU tests run serially to avoid CUDA contention
12+
"gpu", # requires 1 GPU
13+
"manual", # needs torch/triton + GPU; not runnable in open-source CI
14+
"no-remote-exec",
1415
],
1516
deps = [
1617
"//kv_cache_manager/py_connector/kernel",

kv_cache_manager/py_connector/test/vllm_stubs.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
"""Shared test stubs: make ``v1_connector`` importable without vLLM/CUDA/pybind.
22
3-
``v1_connector`` imports vLLM and the compiled ``kvcm_py_client`` at module
3+
``v1_connector`` imports vLLM, the compiled ``kvcm_py_client`` and several
4+
third-party runtime deps (torch, triton, orjson, zmq, requests) at module
45
level. For pure-logic unit tests we register lightweight stand-ins in
56
``sys.modules`` *before* the first import, then build connector instances via
67
``__new__`` with only the attributes the code under test reads. No production
7-
module is modified.
8+
module is modified; real modules are preferred whenever they are importable
9+
(e.g. on a dev machine with a full vLLM venv).
810
"""
911

12+
import importlib.util
13+
import json
1014
import sys
1115
import types
1216
from typing import Optional
@@ -21,7 +25,35 @@ def _module(name: str) -> types.ModuleType:
2125
return mod
2226

2327

28+
def _importable(name: str) -> bool:
29+
try:
30+
return importlib.util.find_spec(name) is not None
31+
except (ImportError, ValueError):
32+
return False
33+
34+
35+
def _stub_third_party():
36+
"""Register stand-ins for third-party deps missing from the environment
37+
(the open-source CI runs these tests without torch/triton/orjson/zmq/
38+
requests installed). Real modules always win."""
39+
# Pure-attribute deps: a MagicMock module is enough because the pure-logic
40+
# tests never execute tensor/socket/http work at module import time.
41+
for name in ("torch", "triton", "triton.language", "zmq", "requests"):
42+
if name not in sys.modules and not _importable(name):
43+
sys.modules[name] = MagicMock(__name__=name)
44+
45+
# orjson is used functionally (CoordinateMsgSerializer round trips), so
46+
# the stand-in must actually (de)serialize; stdlib json handles the
47+
# dataclass payloads via __dict__.
48+
if "orjson" not in sys.modules and not _importable("orjson"):
49+
orjson = _module("orjson")
50+
orjson.dumps = lambda obj: json.dumps(
51+
obj, default=lambda o: o.__dict__).encode()
52+
orjson.loads = json.loads
53+
54+
2455
def _install_stubs():
56+
_stub_third_party()
2557
existing = sys.modules.get("vllm")
2658
if existing is not None:
2759
# Either our stub is already in place or the real vLLM is importable;

kv_cache_manager/py_connector/vllm/v1_connector.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
from kv_cache_manager.client.pybind import kvcm_py_client
3333

3434
import torch
35-
import typing_extensions
3635
from vllm.config import VllmConfig
3736
from vllm.distributed import get_tensor_model_parallel_rank
3837
from vllm.distributed.kv_transfer.kv_connector.v1.base import (
@@ -67,7 +66,7 @@
6766
from kv_cache_manager.py_connector.vllm.location_query_manager import LocationQueryManager
6867
from kv_cache_manager.py_connector.vllm.data_transfer import MultiResult, DataTransferManager, _get_device_module
6968

70-
if typing_extensions.TYPE_CHECKING:
69+
if typing.TYPE_CHECKING:
7170
from vllm.forward_context import ForwardContext
7271
from vllm.attention import AttentionMetadata
7372
from vllm.v1.request import Request

0 commit comments

Comments
 (0)