[Parser] Fix: correct parser frontend handling of reasoning end and boundary tokens (#56635)
Signed-off-by: DEV TYAGI <devtyagi3909@gmail.com>
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""Reasoning-boundary tests for the GLM-4.7 parser.
|
||||
|
||||
GLM can transition straight from reasoning into a tool call without ever
|
||||
emitting ``</think>``, so ``<tool_call>`` is an implicit reasoning
|
||||
terminator. These cover that path plus the multi-turn prompts where an
|
||||
earlier turn's markers must not be read as the current turn's state.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
from tests.parser.engine.conftest import make_mock_tokenizer
|
||||
from vllm.parser.glm47_moe import (
|
||||
THINK_END,
|
||||
THINK_START,
|
||||
TOOL_CALL_END,
|
||||
TOOL_CALL_START,
|
||||
Glm47MoeParser,
|
||||
)
|
||||
|
||||
THINK_S, THINK_E = 1000, 1001
|
||||
TOOL_S, TOOL_E = 1002, 1003
|
||||
ASSISTANT, OBSERVATION, USER = 1004, 1005, 1006
|
||||
TEXT = 42 # stand-in for an ordinary reasoning/content token
|
||||
|
||||
VOCAB = {
|
||||
THINK_START: THINK_S,
|
||||
THINK_END: THINK_E,
|
||||
TOOL_CALL_START: TOOL_S,
|
||||
TOOL_CALL_END: TOOL_E,
|
||||
"<|assistant|>": ASSISTANT,
|
||||
"<|observation|>": OBSERVATION,
|
||||
"<|user|>": USER,
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def parser():
|
||||
return Glm47MoeParser(make_mock_tokenizer(VOCAB))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def no_thinking_parser():
|
||||
return Glm47MoeParser(
|
||||
make_mock_tokenizer(VOCAB),
|
||||
chat_template_kwargs={"thinking": False},
|
||||
)
|
||||
|
||||
|
||||
class TestIsReasoningEnd:
|
||||
def test_open_reasoning(self, parser):
|
||||
assert not parser.is_reasoning_end([THINK_S, TEXT])
|
||||
|
||||
def test_think_end(self, parser):
|
||||
assert parser.is_reasoning_end([THINK_S, TEXT, THINK_E, TEXT])
|
||||
|
||||
def test_tool_call_without_think_end(self, parser):
|
||||
"""Reasoning -> tool call with no ``</think>`` still ends reasoning."""
|
||||
assert parser.is_reasoning_end([THINK_S, TEXT, TOOL_S, TEXT])
|
||||
|
||||
def test_previous_turn_tool_call_ignored(self, parser):
|
||||
"""A finished tool call from an earlier turn says nothing about the
|
||||
turn currently being generated."""
|
||||
prompt = [THINK_S, TEXT, TOOL_S, TEXT, TOOL_E, OBSERVATION, TEXT, ASSISTANT]
|
||||
assert not parser.is_reasoning_end(prompt)
|
||||
|
||||
def test_previous_turn_think_end_ignored(self, parser):
|
||||
prompt = [THINK_S, TEXT, THINK_E, TEXT, USER, TEXT, ASSISTANT]
|
||||
assert not parser.is_reasoning_end(prompt)
|
||||
|
||||
def test_empty_input(self, parser):
|
||||
assert not parser.is_reasoning_end([])
|
||||
|
||||
def test_thinking_disabled(self, no_thinking_parser):
|
||||
assert no_thinking_parser.is_reasoning_end([THINK_S, TEXT])
|
||||
|
||||
|
||||
class TestExtractContentIds:
|
||||
def test_think_end_wins_over_later_tool_call(self, parser):
|
||||
"""Content between ``</think>`` and a tool call must survive."""
|
||||
ids = [THINK_S, TEXT, THINK_E, 20, 21, TOOL_S, 30, TOOL_E]
|
||||
assert parser.extract_content_ids(ids) == [20, 21, TOOL_S, 30, TOOL_E]
|
||||
|
||||
def test_every_tool_call_after_think_end_kept(self, parser):
|
||||
ids = [THINK_E, 20, TOOL_S, 30, TOOL_E, 21, TOOL_S, 31, TOOL_E]
|
||||
assert parser.extract_content_ids(ids) == ids[1:]
|
||||
|
||||
def test_falls_back_to_tool_call(self, parser):
|
||||
"""Without ``</think>``, content starts at the opener itself so the
|
||||
tool parser still receives a well-formed call."""
|
||||
ids = [THINK_S, TEXT, TOOL_S, 30, TOOL_E]
|
||||
assert parser.extract_content_ids(ids) == [TOOL_S, 30, TOOL_E]
|
||||
|
||||
def test_falls_back_to_first_tool_call_of_turn(self, parser):
|
||||
ids = [THINK_S, TEXT, TOOL_S, 30, TOOL_E, TOOL_S, 31, TOOL_E]
|
||||
assert parser.extract_content_ids(ids) == ids[2:]
|
||||
|
||||
def test_previous_turn_tool_call_ignored(self, parser):
|
||||
ids = [TOOL_S, 30, TOOL_E, OBSERVATION, TEXT, ASSISTANT, THINK_S, TEXT]
|
||||
assert parser.extract_content_ids(ids) == ids
|
||||
|
||||
def test_no_markers_returns_input_ids(self, parser):
|
||||
assert parser.extract_content_ids([20, 21]) == [20, 21]
|
||||
|
||||
def test_thinking_disabled(self, no_thinking_parser):
|
||||
ids = [THINK_S, TEXT, TOOL_S]
|
||||
assert no_thinking_parser.extract_content_ids(ids) == ids
|
||||
@@ -672,11 +672,30 @@ class ParserEngine(Parser):
|
||||
return not wait_for_reasoning
|
||||
|
||||
def extract_content_ids(self, input_ids: list[int]) -> list[int]:
|
||||
config = self.parser_engine_config
|
||||
wait_for_reasoning = config.wait_for_reasoning
|
||||
if wait_for_reasoning is None:
|
||||
wait_for_reasoning = config.initial_state is ParserState.REASONING
|
||||
if not wait_for_reasoning:
|
||||
return input_ids
|
||||
|
||||
end_id = self._reasoning_end_token_id
|
||||
if end_id is not None:
|
||||
for i in range(len(input_ids) - 1, -1, -1):
|
||||
if input_ids[i] == end_id:
|
||||
return input_ids[i + 1 :]
|
||||
|
||||
end_ids = self._reasoning_end_token_ids
|
||||
if end_ids:
|
||||
turn_start = 0
|
||||
boundary_ids = self._turn_boundary_token_ids
|
||||
for i in range(len(input_ids) - 1, -1, -1):
|
||||
if input_ids[i] in boundary_ids:
|
||||
turn_start = i + 1
|
||||
break
|
||||
for i in range(turn_start, len(input_ids)):
|
||||
if input_ids[i] in end_ids:
|
||||
return input_ids[i:]
|
||||
return input_ids
|
||||
|
||||
def get_streaming_fallback_content(
|
||||
|
||||
@@ -41,6 +41,13 @@ ARG_KEY_END = "</arg_key>"
|
||||
ARG_VALUE_START = "<arg_value>"
|
||||
ARG_VALUE_END = "</arg_value>"
|
||||
|
||||
# Special tokens that delimit conversation turns in a rendered GLM prompt.
|
||||
# Reasoning markers belonging to earlier turns must not be mistaken for the
|
||||
# state of the turn currently being generated.
|
||||
GLM_TURN_BOUNDARIES = frozenset(
|
||||
("<|system|>", "<|user|>", "<|assistant|>", "<|observation|>")
|
||||
)
|
||||
|
||||
_ARG_RE = re.compile(
|
||||
r"<arg_key>(?P<key>.*?)</arg_key>\s*"
|
||||
r"<arg_value>(?P<value>.*?)</arg_value>",
|
||||
@@ -123,7 +130,6 @@ def glm47_moe_config(thinking: bool = True) -> ParserEngineConfig:
|
||||
return ParserEngineConfig(
|
||||
name="glm47_moe",
|
||||
initial_state=ParserState.REASONING if thinking else ParserState.CONTENT,
|
||||
wait_for_reasoning=thinking,
|
||||
terminals={
|
||||
**reasoning_terminals,
|
||||
"TOOL_START": TOOL_CALL_START,
|
||||
@@ -166,6 +172,7 @@ def glm47_moe_config(thinking: bool = True) -> ParserEngineConfig:
|
||||
),
|
||||
**arg_tag_transitions,
|
||||
},
|
||||
turn_boundary_tokens=GLM_TURN_BOUNDARIES,
|
||||
arg_converter=_glm47_arg_converter,
|
||||
stream_arg_deltas=True,
|
||||
tool_args_json=False,
|
||||
@@ -207,11 +214,6 @@ class Glm47MoeParser(ParserEngine):
|
||||
self._tool_slots[idx].name = self._tool_slots[idx].name.strip()
|
||||
super()._handle_tool_end(event, deltas)
|
||||
|
||||
def extract_content_ids(self, input_ids: list[int]) -> list[int]:
|
||||
if not self.thinking_enabled:
|
||||
return input_ids
|
||||
return super().extract_content_ids(input_ids)
|
||||
|
||||
def extract_reasoning(
|
||||
self,
|
||||
model_output: str,
|
||||
|
||||
Reference in New Issue
Block a user