"examples/backends/vllm/launch/agg_omni.sh" did not exist on "d22ca523107bd5a45bbed81ce2ffa1c55d518d2d"
abs_reasoning_parsers.py 11.3 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

4
import importlib
5
import os
6
from abc import abstractmethod
7
from collections.abc import Callable, Iterable, Sequence
8
from functools import cached_property
9
from typing import TYPE_CHECKING, cast
10

11
from vllm.entrypoints.mcp.tool_server import ToolServer
12
from vllm.logger import init_logger
13
from vllm.utils.collection_utils import is_list_of
14
from vllm.utils.import_utils import import_from_path
15

16
if TYPE_CHECKING:
17
18
19
    from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionRequest
    from vllm.entrypoints.openai.engine.protocol import DeltaMessage
    from vllm.entrypoints.openai.responses.protocol import ResponsesRequest
20
    from vllm.tokenizers import TokenizerLike
21

22
23
24
25
26
logger = init_logger(__name__)


class ReasoningParser:
    """
27
    Abstract reasoning parser class that should not be used directly.
28
29
30
31
32
    Provided and methods should be used in derived classes.

    It is used to extract reasoning content from the model output.
    """

33
    def __init__(self, tokenizer: "TokenizerLike", *args, **kwargs):
34
35
36
        self.model_tokenizer = tokenizer

    @cached_property
37
    def vocab(self) -> dict[str, int]:
38
39
40
41
        # NOTE: Only PreTrainedTokenizerFast is guaranteed to have .vocab
        # whereas all tokenizers have .get_vocab()
        return self.model_tokenizer.get_vocab()

42
    @abstractmethod
43
    def is_reasoning_end(self, input_ids: Sequence[int]) -> bool:
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
        """
        Check if the reasoning content ends in the input_ids.

        It is used in structured engines like `xgrammar` to check if the
        reasoning content ends in the model output.

        Parameters:
        input_ids: list[int]
            The input_ids of the model output.

        Returns:
        bool
            True if the reasoning content ends in the input_ids.
        """

59
    def is_reasoning_end_streaming(
60
        self, input_ids: Sequence[int], delta_ids: Iterable[int]
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    ) -> bool:
        """
        Check if the reasoning content ends in the input_ids on a
        decode step.

        It is used in structured engines like `xgrammar` to check if the
        reasoning content ends in the model output during a decode step.
        `input_ids` the entire model output and `delta_ids` are the last few
        computed tokens of the model output (like during a decode step).

        Parameters:
        input_ids: list[int]
            The entire model output.
        delta_ids: list[int]
            The last few computed tokens of the model output at the current decode step.

        Returns:
        bool
            True if the reasoning content ends in the `delta_ids` on a
            decode step.
        """
        return self.is_reasoning_end(input_ids)

84
85
86
87
88
89
90
91
92
93
94
95
    @abstractmethod
    def extract_content_ids(self, input_ids: list[int]) -> list[int]:
        """
        Extract content token ids from the input_ids.
        Parameters:
        input_ids: list[int]
            The input_ids of the model output.
        Returns:
        list[int]
            The extracted content from the input_ids.
        """

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    def count_reasoning_tokens(self, token_ids: Sequence[int]) -> int:
        """Count the number of reasoning tokens in a sequence.

        Text-based reasoning models typically wrap their chain-of-thought
        between special start/end tokens (e.g., ``<think> ... </think>``).
        Implementations that support reasoning token counting should override
        this method. The default implementation returns ``0`` so existing
        parsers remain unchanged unless they explicitly opt in.

        Args:
            token_ids: Sequence of generated token ids (excluding prompt).

        Returns:
            int: Number of tokens that belong to reasoning content.
        """

        # By default, assume the parser cannot detect reasoning spans.
        return 0

115
    @abstractmethod
116
    def extract_reasoning(
117
118
        self,
        model_output: str,
119
        request: "ChatCompletionRequest | ResponsesRequest",
120
    ) -> tuple[str | None, str | None]:
121
122
123
124
125
126
127
        """
        Extract reasoning content from a complete model-generated string.

        Used for non-streaming responses where we have the entire model response
        available before sending to the client.

        Parameters:
128
129
            model_output: The model-generated string to extract reasoning content from.
            request: The request object that was used to generate the model_output.
130
131
132
133
134

        Returns:
            A tuple containing the reasoning content and the content.
        """

135
    @abstractmethod
136
    def extract_reasoning_streaming(
137
138
139
140
141
142
143
        self,
        previous_text: str,
        current_text: str,
        delta_text: str,
        previous_token_ids: Sequence[int],
        current_token_ids: Sequence[int],
        delta_token_ids: Sequence[int],
144
    ) -> "DeltaMessage | None":
145
146
147
148
149
150
151
        """
        Instance method that should be implemented for extracting reasoning
        from an incomplete response; for use when handling reasoning calls and
        streaming. Has to be an instance method because  it requires state -
        the current tokens/diffs, but also the information about what has
        previously been parsed and extracted (see constructor)
        """
152

153
154
155
156
157
158
    def adjust_request(
        self, request: "ChatCompletionRequest | ResponsesRequest"
    ) -> "ChatCompletionRequest | ResponsesRequest":
        """Adjust request parameters; override in subclasses as needed."""
        return request

159
160
161
162
    def prepare_structured_tag(
        self,
        original_tag: str | None,
        tool_server: ToolServer | None,
Ning Xie's avatar
Ning Xie committed
163
    ) -> str | None:
164
165
166
167
168
169
        """
        Instance method that is implemented for preparing the structured tag
        Otherwise, None is returned
        """
        return None

170
171

class ReasoningParserManager:
172
173
174
175
176
177
178
179
180
181
182
183
    """
    Central registry for ReasoningParser implementations.

    Supports two registration modes:
      - Eager registration via `register_module`
      - Lazy registration via `register_lazy_module`

    Each reasoning parser must inherit from `ReasoningParser`.
    """

    reasoning_parsers: dict[str, type[ReasoningParser]] = {}
    lazy_parsers: dict[str, tuple[str, str]] = {}  # name -> (module_path, class_name)
184
185

    @classmethod
186
    def get_reasoning_parser(cls, name: str) -> type[ReasoningParser]:
187
        """
188
189
190
191
        Retrieve a registered or lazily registered ReasoningParser class.

        If the parser is lazily registered, it will be imported and cached
        on first access.
192

193
194
        Raises:
            KeyError: if no parser is found under the given name.
195
196
197
198
        """
        if name in cls.reasoning_parsers:
            return cls.reasoning_parsers[name]

199
200
201
        if name in cls.lazy_parsers:
            return cls._load_lazy_parser(name)

202
203
204
205
        registered = ", ".join(cls.list_registered())
        raise KeyError(
            f"Reasoning parser '{name}' not found. Available parsers: {registered}"
        )
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233

    @classmethod
    def list_registered(cls) -> list[str]:
        """Return names of all eagerly and lazily registered reasoning parsers."""
        return sorted(set(cls.reasoning_parsers.keys()) | set(cls.lazy_parsers.keys()))

    @classmethod
    def _load_lazy_parser(cls, name: str) -> type[ReasoningParser]:
        """Import and register a lazily loaded reasoning parser."""
        module_path, class_name = cls.lazy_parsers[name]
        try:
            mod = importlib.import_module(module_path)
            parser_cls = getattr(mod, class_name)
            if not issubclass(parser_cls, ReasoningParser):
                raise TypeError(
                    f"{class_name} in {module_path} is not a ReasoningParser subclass."
                )

            cls.reasoning_parsers[name] = parser_cls  # cache
            return parser_cls
        except Exception as e:
            logger.exception(
                "Failed to import lazy reasoning parser '%s' from %s: %s",
                name,
                module_path,
                e,
            )
            raise
234
235

    @classmethod
236
237
    def _register_module(
        cls,
238
        module: type[ReasoningParser],
239
        module_name: str | list[str] | None = None,
240
241
        force: bool = True,
    ) -> None:
242
        """Register a ReasoningParser class immediately."""
243
        if not issubclass(module, ReasoningParser):
244
245
246
            raise TypeError(
                f"module must be subclass of ReasoningParser, but got {type(module)}"
            )
247

248
        if module_name is None:
249
250
251
252
253
254
255
256
257
            module_names = [module.__name__]
        elif isinstance(module_name, str):
            module_names = [module_name]
        elif is_list_of(module_name, str):
            module_names = module_name
        else:
            raise TypeError("module_name must be str, list[str], or None.")

        for name in module_names:
258
            if not force and name in cls.reasoning_parsers:
259
260
                existed = cls.reasoning_parsers[name]
                raise KeyError(f"{name} is already registered at {existed.__module__}")
261
262
            cls.reasoning_parsers[name] = module

263
264
265
266
267
268
269
270
271
272
273
274
275
276
    @classmethod
    def register_lazy_module(cls, name: str, module_path: str, class_name: str) -> None:
        """
        Register a lazy module mapping for delayed import.

        Example:
            ReasoningParserManager.register_lazy_module(
                name="qwen3",
                module_path="vllm.reasoning.parsers.qwen3_reasoning_parser",
                class_name="Qwen3ReasoningParser",
            )
        """
        cls.lazy_parsers[name] = (module_path, class_name)

277
278
    @classmethod
    def register_module(
279
        cls,
280
        name: str | list[str] | None = None,
281
        force: bool = True,
282
283
284
285
        module: type[ReasoningParser] | None = None,
    ) -> (
        type[ReasoningParser] | Callable[[type[ReasoningParser]], type[ReasoningParser]]
    ):
286
287
        """
        Register module with the given name or name list. it can be used as a
288
        decoder(with module as None) or normal function(with module as not
289
290
291
292
293
        None).
        """
        if not isinstance(force, bool):
            raise TypeError(f"force must be a boolean, but got {type(force)}")

294
        # Immediate registration (explicit call)
295
296
297
298
        if module is not None:
            cls._register_module(module=module, module_name=name, force=force)
            return module

299
300
301
302
303
304
305
306
        # Decorator usage
        def _decorator(obj: type[ReasoningParser]) -> type[ReasoningParser]:
            module_path = obj.__module__
            class_name = obj.__name__

            if isinstance(name, str):
                names = [name]
            elif is_list_of(name, str):
307
                names = cast(list[str], name)
308
309
310
311
312
313
314
            else:
                names = [class_name]

            for n in names:
                cls.lazy_parsers[n] = (module_path, class_name)

            return obj
315

316
        return _decorator
317
318
319
320

    @classmethod
    def import_reasoning_parser(cls, plugin_path: str) -> None:
        """
321
        Import a user-defined reasoning parser by the path
322
323
324
325
326
327
328
        of the reasoning parser define file.
        """
        module_name = os.path.splitext(os.path.basename(plugin_path))[0]

        try:
            import_from_path(module_name, plugin_path)
        except Exception:
329
330
331
            logger.exception(
                "Failed to load module '%s' from %s.", module_name, plugin_path
            )
332
            return