[Bugfix] Improve GLM4 MoE Reasoning Parser's is_reasoning_end Condition (#25355)

Signed-off-by: frankwang28 <frank.wbb@hotmail.com>
Signed-off-by: Frank Wang <41319051+frankwang28@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Chauncey <chaunceyjiang@gmail.com>
This commit is contained in:
Frank Wang
2025-09-26 11:54:00 -07:00
committed by GitHub
co-authored by gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Chauncey
parent b761df963c
commit 11aafd9886
2 changed files with 219 additions and 3 deletions
+16 -3
View File
@@ -30,6 +30,7 @@ class Glm4MoeModelReasoningParser(ReasoningParser):
super().__init__(tokenizer, *args, **kwargs)
self.think_start_token = "<think>"
self.think_end_token = "</think>"
self.assistant_token = "<|assistant|>"
if not self.model_tokenizer:
raise ValueError(
@@ -38,14 +39,26 @@ class Glm4MoeModelReasoningParser(ReasoningParser):
self.think_start_token_id = self.vocab.get(self.think_start_token)
self.think_end_token_id = self.vocab.get(self.think_end_token)
self.assistant_token_id = self.vocab.get(self.assistant_token)
if (self.think_start_token_id is None
or self.think_end_token_id is None):
or self.think_end_token_id is None
or self.assistant_token_id is None):
raise RuntimeError(
"Glm4MoeModel reasoning parser could not locate "
"think start/end tokens in the tokenizer!")
"think start/end or assistant tokens in the tokenizer!")
def is_reasoning_end(self, input_ids: list[int]) -> bool:
return self.think_end_token_id in input_ids
"""
GLM's chat template has <think></think> tokens after every
<|assistant|> token. Thus, we need to check if </think> is
after the most recent <|assistant|> token (if present).
"""
for token_id in input_ids[::-1]:
if token_id == self.think_end_token_id:
return True
elif token_id == self.assistant_token_id:
return False
return False
def extract_content_ids(self, input_ids: list[int]) -> list[int]:
"""