Skip to content
Open
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
3 changes: 3 additions & 0 deletions docs/features/reasoning_outputs.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ If `thinking_token_budget` is not specified, no explicit reasoning limit is appl
!!! note
`reasoning_end_str` can include a transition phrase before the reasoning end token. For example, setting `reasoning_end_str` to `"I have to give the solution based on the reasoning directly now.</think>"` instructs the model to emit that phrase when the budget is exhausted, making the reasoning termination more natural.

!!! note
Both strings must be spelled the way the model *generates* them, because they are matched against output token ids. If the chat template ends the generation prompt in the middle of a line, the first generated token carries the leading whitespace (` to` is not the same token as `to`), and a spelling that differs from the model's is never matched, so the budget silently never applies.

### Online Serving

```bash
Expand Down
110 changes: 110 additions & 0 deletions tests/reasoning/test_muse_glimmer_reasoning_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project

"""Thinking-budget enablement tests for MuseGlimmer.

MuseGlimmer has no `<think>`/`</think>` pair: reasoning is a channel, opened by
naming `self` as the recipient (` to=self<|message|>`) and closed by `<|eom|>`.
Without declared boundary strings, `ReasoningConfig.initialize_token_ids`
returns early, `reasoning_config.enabled` stays False, and any request that
sets `thinking_token_budget` is rejected with HTTP 400.

A stub tokenizer keeps these checkpoint-free; the framing markers are the
special tokens they are in the real vocabulary.
"""

import pytest

from vllm.reasoning.muse_glimmer_reasoning_parser import MuseGlimmerReasoningParser

_SPECIALS = ("<|message|>", "<|eom|>", "<|eot|>", "<|start|>")


class _StubTokenizer:
"""Longest-match tokenizer over the framing markers plus single characters."""

def __init__(self, specials=_SPECIALS):
pieces = list(specials) + [chr(c) for c in range(32, 127)]
self._id_to_text = pieces
self._text_to_id = {text: i for i, text in enumerate(pieces)}
self._ordered = sorted(pieces, key=len, reverse=True)

def get_vocab(self):
return dict(self._text_to_id)

def encode(self, text, add_special_tokens=False):
ids = []
pos = 0
while pos < len(text):
for piece in self._ordered:
if text.startswith(piece, pos):
ids.append(self._text_to_id[piece])
pos += len(piece)
break
else:
raise AssertionError(f"unencodable text at {pos}: {text[pos:]!r}")
return ids

def decode(self, token_ids, **kwargs):
return "".join(self._id_to_text[i] for i in token_ids)


@pytest.fixture
def tok():
return _StubTokenizer()


@pytest.fixture
def parser(tok):
return MuseGlimmerReasoningParser(tok)


def test_boundary_strings_are_what_the_model_generates(parser):
# The leading space matters: the generation prompt ends after
# `<|start|>assistant`, so the model emits ` to`, a different token from
# `to`, and the budget matcher compares token ids by exact slice.
assert parser.reasoning_start_str == " to=self<|message|>"
# The end string is the closing marker only. The full transition
# `<|eom|><|start|>assistant to=user<|message|>` would decide the next
# recipient, which on a tool turn belongs to the model.
assert parser.reasoning_end_str == "<|eom|>"


def test_reasoning_config_enables_the_budget(tok, monkeypatch):
"""`thinking_token_budget` must be enabled, with forced == natural end."""
from vllm.config import reasoning as reasoning_module
from vllm.config.reasoning import ReasoningConfig

monkeypatch.setattr(
reasoning_module, "cached_tokenizer_from_config", lambda model_config: tok
)
config = ReasoningConfig(reasoning_parser="muse_glimmer")
config.initialize_token_ids(model_config=None)

assert config.enabled, "a thinking_token_budget request would get HTTP 400"
assert config.reasoning_start_token_ids == tok.encode(" to=self<|message|>")
assert config.reasoning_end_token_ids == tok.encode("<|eom|>")
assert config.natural_reasoning_end_token_ids == tok.encode("<|eom|>")


def test_cli_reasoning_end_str_still_overrides(tok, monkeypatch):
"""A user-provided end string must win over the parser's declaration."""
from vllm.config import reasoning as reasoning_module
from vllm.config.reasoning import ReasoningConfig

monkeypatch.setattr(
reasoning_module, "cached_tokenizer_from_config", lambda model_config: tok
)
config = ReasoningConfig(
reasoning_parser="muse_glimmer",
reasoning_start_str=" to=self<|message|>",
reasoning_end_str="<|eom|><|start|>assistant to=user<|message|>",
)
config.initialize_token_ids(model_config=None)

assert config.enabled
assert config.reasoning_end_token_ids == tok.encode(
"<|eom|><|start|>assistant to=user<|message|>"
)
# The parser's own declaration remains the natural end.
assert config.natural_reasoning_end_token_ids == tok.encode("<|eom|>")
32 changes: 32 additions & 0 deletions vllm/reasoning/muse_glimmer_reasoning_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
_EOT = "<|eot|>"
_FUNCTION_CALLS_OPEN = "<atem:function_calls>"
_REASONING_OPEN = "to=self<|message|>"
# What the model GENERATES to open its reasoning channel, leading space and all.
# The chat template renders the assistant header as
# ``<|start|>assistant to=self<|message|>`` and ends the generation prompt after
# ``<|start|>assistant``, so the space belongs to the first generated token: the
# model emits `` to``, not ``to``, and the two are different ids.
# ``thinking_token_budget`` matches these ids against the output by exact slice
# and silently never fires if the spelling differs.
_GENERATED_REASONING_OPEN = " to=self<|message|>"
_ASSISTANT_TURN_OPEN = "<|start|>assistant"
# A channel header: ``to=<recipient><|message|>`` where recipient is ``self``
# (reasoning), ``user`` (final answer) or ``<tool>[.<fn>]`` (tool call).
Expand Down Expand Up @@ -135,6 +143,30 @@ def adjust_request(
request.skip_special_tokens = False
return request

@property
def reasoning_start_str(self) -> str:
"""Opens a reasoning message; see ``_GENERATED_REASONING_OPEN``.

Declaring this and ``reasoning_end_str`` is what lets
``ReasoningConfig.initialize_token_ids`` enable
``thinking_token_budget`` for MuseGlimmer. Without them the config
returns early, ``reasoning_config.enabled`` stays False, and any
request carrying a budget is rejected with HTTP 400.
"""
return _GENERATED_REASONING_OPEN

@property
def reasoning_end_str(self) -> str:
"""The marker that ends a reasoning message, and only that.

Deliberately NOT the full transition
``<|eom|><|start|>assistant to=user<|message|>``: that string does not
just end reasoning, it decides the next recipient, and forcing it on a
tool turn sends the answer to the user instead of the tool. Forcing the
bare marker leaves the recipient to the model.
"""
return _EOM

def is_reasoning_end(self, input_ids: Sequence[int]) -> bool:
"""Whether the model has left reasoning and opened a TOOL channel.
A ``to=user`` answer is NOT a reason to leave the reasoning phase -- this
Expand Down
Loading