[Bugfix] Fix and add tests for GptOss reasoning parser (#28000)
Signed-off-by: Benjamin Chislett <bchislett@nvidia.com>
This commit is contained in:
@@ -67,18 +67,35 @@ class GptOssReasoningParser(ReasoningParser):
|
||||
|
||||
def __init__(self, tokenizer: PreTrainedTokenizerBase, *args, **kwargs):
|
||||
super().__init__(tokenizer, *args, **kwargs)
|
||||
self.reasoning_end_token_ids = self.model_tokenizer.encode(
|
||||
"<|start|>assistant<|channel|>final<|message|>"
|
||||
# The model can output some special tokens between "final" and "<|message|>"
|
||||
# So we need to look for both sequences to determine the end of reasoning.
|
||||
self.reasoning_end_token_ids_prefix = self.model_tokenizer.encode(
|
||||
"<|channel|>final"
|
||||
)
|
||||
self.reasoning_end_token_ids_suffix = self.model_tokenizer.encode("<|message|>")
|
||||
self.reasoning_max_num_between_tokens = 20
|
||||
|
||||
def is_reasoning_end(self, input_ids: list[int]) -> bool:
|
||||
end_token_ids = self.reasoning_end_token_ids
|
||||
assert len(end_token_ids) > 0, "reasoning_end_token_ids is empty"
|
||||
end_token_ids_prefix = self.reasoning_end_token_ids_prefix
|
||||
end_token_ids_suffix = self.reasoning_end_token_ids_suffix
|
||||
assert len(end_token_ids_prefix) > 0, "reasoning_end_token_ids_prefix is empty"
|
||||
assert len(end_token_ids_suffix) > 0, "reasoning_end_token_ids_suffix is empty"
|
||||
# Check if the end sequence is present in the input_ids.
|
||||
# We search from the end of input_ids to find the last match.
|
||||
for i in range(len(input_ids) - len(end_token_ids), -1, -1):
|
||||
if input_ids[i : i + len(end_token_ids)] == end_token_ids:
|
||||
return True
|
||||
for i in range(len(input_ids) - len(end_token_ids_prefix), -1, -1):
|
||||
if input_ids[i : i + len(end_token_ids_prefix)] == end_token_ids_prefix:
|
||||
# We have found the prefix, now we look for the suffix after the prefix.
|
||||
suffix_start = i + len(end_token_ids_prefix)
|
||||
for j in range(
|
||||
suffix_start, len(input_ids) - len(end_token_ids_suffix) + 1
|
||||
):
|
||||
if j - suffix_start >= self.reasoning_max_num_between_tokens:
|
||||
break
|
||||
if (
|
||||
input_ids[j : j + len(end_token_ids_suffix)]
|
||||
== end_token_ids_suffix
|
||||
):
|
||||
return True
|
||||
return False
|
||||
|
||||
def extract_content_ids(self, input_ids: list[int]) -> list[int]:
|
||||
|
||||
Reference in New Issue
Block a user