[Refactor] Derive is_reasoning_end from the engine grammar (#56200)

Signed-off-by: sfeng33 <4florafeng@gmail.com>
This commit is contained in:
Flora Feng
2026-09-15 10:07:58 +00:00
committed by GitHub
parent 142020c6c2
commit 24bfbd5d4d
13 changed files with 111 additions and 117 deletions
+7 -27
View File
@@ -293,10 +293,14 @@ def _gemma4_arg_converter(raw_args: str, partial: bool) -> str:
@functools.cache
def gemma4_config() -> ParserEngineConfig:
def gemma4_config(thinking: bool = False) -> ParserEngineConfig:
return ParserEngineConfig(
name="gemma4",
initial_state=ParserState.CONTENT,
# Decides a silent tail: the E-series HF templates write no marker
# with thinking off, and no template does after a tool response.
wait_for_reasoning=thinking,
turn_boundary_tokens=frozenset({"<|turn>", "<|tool_response>"}),
terminals={
"THINK_START": CHANNEL_START,
"THINK_END": CHANNEL_END,
@@ -417,11 +421,11 @@ class Gemma4Parser(ParserEngine):
**kwargs,
) -> None:
chat_kwargs = kwargs.get("chat_template_kwargs", {}) or {}
self._thinking_enabled = chat_kwargs.get("enable_thinking", False)
thinking = chat_kwargs.get("enable_thinking", False)
super().__init__(
tokenizer,
tools,
parser_engine_config=gemma4_config(),
parser_engine_config=gemma4_config(thinking=thinking),
**kwargs,
)
vocab = self.vocab
@@ -472,30 +476,6 @@ class Gemma4Parser(ParserEngine):
return delta_text, delta_token_ids
def is_reasoning_end(self, input_ids: list[int]) -> bool:
end_id = self._reasoning_end_token_id
start_id = self._reasoning_start_token_id
tool_call_id = self._tool_call_token_id
new_turn_id = self._new_turn_token_id
tool_response_id = self._tool_response_token_id
if end_id is not None and not input_ids:
return self.parser_engine_config.initial_state != ParserState.REASONING
for i in range(len(input_ids) - 1, -1, -1):
tid = input_ids[i]
if start_id is not None and tid == start_id:
return False
if tool_call_id is not None and tid == tool_call_id:
return True
if new_turn_id is not None and tid == new_turn_id:
return not self._thinking_enabled
if tool_response_id is not None and tid == tool_response_id:
return not self._thinking_enabled
if end_id is not None and tid == end_id:
return True
return True
def _prompt_ends_in_open_reasoning(self, prompt_token_ids: Sequence[int]) -> bool:
"""Whether the prompt tail is inside an open ``<|channel>`` block.