"tests/models/language/pooling/test_embedding.py" did not exist on "e254497b66dcd87038969b0ad34d34425edfc5fe"
generate_argparse.py 7.38 KB
Newer Older
1
2
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
import importlib
4
5
6
7
8
9
10
import logging
import sys
from argparse import SUPPRESS, HelpFormatter
from pathlib import Path
from typing import Literal
from unittest.mock import MagicMock, patch

11
12
13
14
from pydantic_core import core_schema

logger = logging.getLogger("mkdocs")

15
16
17
18
19
20
21
ROOT_DIR = Path(__file__).parent.parent.parent.parent
ARGPARSE_DOC_DIR = ROOT_DIR / "docs/argparse"

sys.path.insert(0, str(ROOT_DIR))
sys.modules["vllm._C"] = MagicMock()


22
23
24
class PydanticMagicMock(MagicMock):
    """`MagicMock` that's able to generate pydantic-core schemas."""

25
26
27
28
29
    def __init__(self, *args, **kwargs):
        name = kwargs.pop("name", None)
        super().__init__(*args, **kwargs)
        self.__spec__ = importlib.machinery.ModuleSpec(name, None)

30
31
32
33
34
35
36
37
38
39
    def __get_pydantic_core_schema__(self, source_type, handler):
        return core_schema.any_schema()


def auto_mock(module, attr, max_mocks=50):
    """Function that automatically mocks missing modules during imports."""
    logger.info("Importing %s from %s", attr, module)
    for _ in range(max_mocks):
        try:
            # First treat attr as an attr, then as a submodule
40
            with patch("importlib.metadata.version", return_value="0.0.0"):
41
42
43
44
45
                return getattr(
                    importlib.import_module(module),
                    attr,
                    importlib.import_module(f"{module}.{attr}"),
                )
46
47
48
49
        except importlib.metadata.PackageNotFoundError as e:
            raise e
        except ModuleNotFoundError as e:
            logger.info("Mocking %s for argparse doc generation", e.name)
50
51
52
            sys.modules[e.name] = PydanticMagicMock(name=e.name)
        except Exception as e:
            logger.warning("Failed to import %s.%s: %s", module, attr, e)
53
54

    raise ImportError(
55
56
        f"Failed to import {module}.{attr} after mocking {max_mocks} imports"
    )
57
58


59
60
61
62
63
64
65
66
bench_latency = auto_mock("vllm.benchmarks", "latency")
bench_serve = auto_mock("vllm.benchmarks", "serve")
bench_sweep_plot = auto_mock("vllm.benchmarks.sweep.plot", "SweepPlotArgs")
bench_sweep_serve = auto_mock("vllm.benchmarks.sweep.serve", "SweepServeArgs")
bench_sweep_serve_sla = auto_mock(
    "vllm.benchmarks.sweep.serve_sla", "SweepServeSLAArgs"
)
bench_throughput = auto_mock("vllm.benchmarks", "throughput")
67
68
69
70
AsyncEngineArgs = auto_mock("vllm.engine.arg_utils", "AsyncEngineArgs")
EngineArgs = auto_mock("vllm.engine.arg_utils", "EngineArgs")
ChatCommand = auto_mock("vllm.entrypoints.cli.openai", "ChatCommand")
CompleteCommand = auto_mock("vllm.entrypoints.cli.openai", "CompleteCommand")
71
72
openai_cli_args = auto_mock("vllm.entrypoints.openai", "cli_args")
openai_run_batch = auto_mock("vllm.entrypoints.openai", "run_batch")
Cyrus Leung's avatar
Cyrus Leung committed
73
74
75
FlexibleArgumentParser = auto_mock(
    "vllm.utils.argparse_utils", "FlexibleArgumentParser"
)
76
77
78
79
80


class MarkdownFormatter(HelpFormatter):
    """Custom formatter that generates markdown for argument groups."""

81
    def __init__(self, prog, starting_heading_level=3):
82
        super().__init__(prog, max_help_position=float("inf"), width=float("inf"))
83
84
        self._section_heading_prefix = "#" * starting_heading_level
        self._argument_heading_prefix = "#" * (starting_heading_level + 1)
85
86
87
88
        self._markdown_output = []

    def start_section(self, heading):
        if heading not in {"positional arguments", "options"}:
89
90
            heading_md = f"\n{self._section_heading_prefix} {heading}\n\n"
            self._markdown_output.append(heading_md)
91
92
93
94
95
96
97
98
99
100
101
102
103

    def end_section(self):
        pass

    def add_text(self, text):
        if text:
            self._markdown_output.append(f"{text.strip()}\n\n")

    def add_usage(self, usage, actions, groups, prefix=None):
        pass

    def add_arguments(self, actions):
        for action in actions:
104
            if len(action.option_strings) == 0 or "--help" in action.option_strings:
105
                continue
106

107
            option_strings = f"`{'`, `'.join(action.option_strings)}`"
108
109
            heading_md = f"{self._argument_heading_prefix} {option_strings}\n\n"
            self._markdown_output.append(heading_md)
110
111

            if choices := action.choices:
112
113
114
115
116
                choices = f"`{'`, `'.join(str(c) for c in choices)}`"
                self._markdown_output.append(f"Possible choices: {choices}\n\n")
            elif (metavar := action.metavar) and isinstance(metavar, (list, tuple)):
                metavar = f"`{'`, `'.join(str(m) for m in metavar)}`"
                self._markdown_output.append(f"Possible choices: {metavar}\n\n")
117

118
119
            if action.help:
                self._markdown_output.append(f"{action.help}\n\n")
120
121

            if (default := action.default) != SUPPRESS:
122
123
124
                # Make empty string defaults visible
                if default == "":
                    default = '""'
125
126
127
128
129
130
131
                self._markdown_output.append(f"Default: `{default}`\n\n")

    def format_help(self):
        """Return the formatted help as markdown."""
        return "".join(self._markdown_output)


132
def create_parser(add_cli_args, **kwargs) -> FlexibleArgumentParser:
133
    """Create a parser for the given class with markdown formatting.
134

135
136
137
138
139
140
141
    Args:
        cls: The class to create a parser for
        **kwargs: Additional keyword arguments to pass to `cls.add_cli_args`.

    Returns:
        FlexibleArgumentParser: A parser with markdown formatting for the class.
    """
142
    parser = FlexibleArgumentParser(add_json_tip=False)
143
144
    parser.formatter_class = MarkdownFormatter
    with patch("vllm.config.DeviceConfig.__post_init__"):
145
146
147
        _parser = add_cli_args(parser, **kwargs)
    # add_cli_args might be in-place so return parser if _parser is None
    return _parser or parser
148
149


150
151
152
153
154
155
156
157
158
159
160
def on_startup(command: Literal["build", "gh-deploy", "serve"], dirty: bool):
    logger.info("Generating argparse documentation")
    logger.debug("Root directory: %s", ROOT_DIR.resolve())
    logger.debug("Output directory: %s", ARGPARSE_DOC_DIR.resolve())

    # Create the ARGPARSE_DOC_DIR if it doesn't exist
    if not ARGPARSE_DOC_DIR.exists():
        ARGPARSE_DOC_DIR.mkdir(parents=True)

    # Create parsers to document
    parsers = {
161
        # Engine args
162
163
164
165
        "engine_args": create_parser(EngineArgs.add_cli_args),
        "async_engine_args": create_parser(
            AsyncEngineArgs.add_cli_args, async_args_only=True
        ),
166
167
        # CLI
        "serve": create_parser(openai_cli_args.make_arg_parser),
168
169
        "chat": create_parser(ChatCommand.add_cli_args),
        "complete": create_parser(CompleteCommand.add_cli_args),
170
171
172
173
174
175
176
177
        "run-batch": create_parser(openai_run_batch.make_arg_parser),
        # Benchmark CLI
        "bench_latency": create_parser(bench_latency.add_cli_args),
        "bench_serve": create_parser(bench_serve.add_cli_args),
        "bench_sweep_plot": create_parser(bench_sweep_plot.add_cli_args),
        "bench_sweep_serve": create_parser(bench_sweep_serve.add_cli_args),
        "bench_sweep_serve_sla": create_parser(bench_sweep_serve_sla.add_cli_args),
        "bench_throughput": create_parser(bench_throughput.add_cli_args),
178
179
180
181
182
    }

    # Generate documentation for each parser
    for stem, parser in parsers.items():
        doc_path = ARGPARSE_DOC_DIR / f"{stem}.md"
183
184
        # Specify encoding for building on Windows
        with open(doc_path, "w", encoding="utf-8") as f:
185
            f.write(super(type(parser), parser).format_help())
186
        logger.info("Argparse generated: %s", doc_path.relative_to(ROOT_DIR))