@tduncan / Nvidia-Optimization-Stack-VLLM Mirror · read-only / commits / 799ce45cc1
Fork 0

A high-throughput and memory-efficient inference and serving engine for LLMs

Mirrored from https://github.com/tduncan-lum/Nvidia-Optimization-Stack---VLLM.git

Synced Nov 13, 22:00 UTC

[Docs] Mock all imports for docs (#27873)

Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>

Harry Mellor committed Nov 1, 2025 at 10:02 UTC 799ce45cc160ffc0a3e1a0f958cc8e260b751808
3 files changed +49 -23
docs/mkdocs/hooks/generate_argparse.py
+47 -13
@@ -3,6 +3,7 @@
3 import importlib
4 import logging
5 import sys
6 +import traceback
7 from argparse import SUPPRESS, HelpFormatter
8 from pathlib import Path
9 from typing import Literal
@@ -16,7 +17,30 @@ ROOT_DIR = Path(__file__).parent.parent.parent.parent
17 ARGPARSE_DOC_DIR = ROOT_DIR / "docs/argparse"
18
19 sys.path.insert(0, str(ROOT_DIR))
20 +
21 +
22 +# Mock custom op code
23 +class MockCustomOp:
24 + @staticmethod
25 + def register(name):
26 + def decorator(cls):
27 + return cls
28 +
29 + return decorator
30 +
31 +
32 +noop = lambda *a, **k: None
33 sys.modules["vllm._C"] = MagicMock()
34 +sys.modules["vllm.model_executor.custom_op"] = MagicMock(CustomOp=MockCustomOp)
35 +sys.modules["vllm.utils.torch_utils"] = MagicMock(direct_register_custom_op=noop)
36 +
37 +# Mock any version checks by reading from compiled CI requirements
38 +with open(ROOT_DIR / "requirements/test.txt") as f:
39 + VERSIONS = dict(line.strip().split("==") for line in f if "==" in line)
40 +importlib.metadata.version = lambda name: VERSIONS.get(name) or "0.0.0"
41 +
42 +# Make torch.nn.Parameter safe to inherit from
43 +sys.modules["torch.nn"] = MagicMock(Parameter=object)
44
45
46 class PydanticMagicMock(MagicMock):
@@ -31,20 +55,17 @@ class PydanticMagicMock(MagicMock):
55 return core_schema.any_schema()
56
57
34 -def auto_mock(module, attr, max_mocks=50):
58 +def auto_mock(module, attr, max_mocks=100):
59 """Function that automatically mocks missing modules during imports."""
60 logger.info("Importing %s from %s", attr, module)
61 for _ in range(max_mocks):
62 try:
63 # First treat attr as an attr, then as a submodule
40 - with patch("importlib.metadata.version", return_value="0.0.0"):
41 - return getattr(
42 - importlib.import_module(module),
43 - attr,
44 - importlib.import_module(f"{module}.{attr}"),
45 - )
46 - except importlib.metadata.PackageNotFoundError as e:
47 - raise e
64 + return getattr(
65 + importlib.import_module(module),
66 + attr,
67 + importlib.import_module(f"{module}.{attr}"),
68 + )
69 except ModuleNotFoundError as e:
70 logger.info("Mocking %s for argparse doc generation", e.name)
71 sys.modules[e.name] = PydanticMagicMock(name=e.name)
@@ -139,10 +160,19 @@ def create_parser(add_cli_args, **kwargs) -> FlexibleArgumentParser:
160 Returns:
161 FlexibleArgumentParser: A parser with markdown formatting for the class.
162 """
142 - parser = FlexibleArgumentParser(add_json_tip=False)
143 - parser.formatter_class = MarkdownFormatter
144 - with patch("vllm.config.DeviceConfig.__post_init__"):
145 - _parser = add_cli_args(parser, **kwargs)
163 + try:
164 + parser = FlexibleArgumentParser(add_json_tip=False)
165 + parser.formatter_class = MarkdownFormatter
166 + with patch("vllm.config.DeviceConfig.__post_init__"):
167 + _parser = add_cli_args(parser, **kwargs)
168 + except ModuleNotFoundError as e:
169 + # Auto-mock runtime imports
170 + if tb_list := traceback.extract_tb(e.__traceback__):
171 + path = Path(tb_list[-1].filename).relative_to(ROOT_DIR)
172 + auto_mock(module=".".join(path.parent.parts), attr=path.stem)
173 + return create_parser(add_cli_args, **kwargs)
174 + else:
175 + raise e
176 # add_cli_args might be in-place so return parser if _parser is None
177 return _parser or parser
178
@@ -184,3 +214,7 @@ def on_startup(command: Literal["build", "gh-deploy", "serve"], dirty: bool):
214 with open(doc_path, "w", encoding="utf-8") as f:
215 f.write(super(type(parser), parser).format_help())
216 logger.info("Argparse generated: %s", doc_path.relative_to(ROOT_DIR))
217 +
218 +
219 +if __name__ == "__main__":
220 + on_startup("build", False)
requirements/docs.txt
-8
@@ -9,12 +9,4 @@ mkdocs-git-revision-date-localized-plugin
9 mkdocs-minify-plugin
10 regex
11 ruff
12 -
13 -# Required for argparse hook only
14 --f https://download.pytorch.org/whl/cpu
15 -cachetools
16 -cloudpickle
17 -py-cpuinfo
18 -msgspec
12 pydantic
20 -torch
vllm/utils/cache.py
+2 -2
@@ -3,7 +3,7 @@
3 from collections import UserDict
4 from collections.abc import Callable, Hashable, Iterator, KeysView, Mapping
5 from types import MappingProxyType
6 -from typing import Generic, NamedTuple, TypeVar, cast, overload
6 +from typing import NamedTuple, TypeVar, cast, overload
7
8 import cachetools
9
@@ -48,7 +48,7 @@ class CacheInfo(NamedTuple):
48 )
49
50
51 -class LRUCache(cachetools.LRUCache[_K, _V], Generic[_K, _V]):
51 +class LRUCache(cachetools.LRUCache[_K, _V]):
52 def __init__(self, capacity: float, getsizeof: Callable[[_V], float] | None = None):
53 super().__init__(capacity, getsizeof)
54