[Bugfix][Reasoning] Count Kimi K3 reasoning tokens (#58372)
Signed-off-by: Elvir Crncevic <elvircrn@gmail.com> Signed-off-by: Flora Feng <4florafeng@gmail.com> Co-authored-by: Flora Feng <4florafeng@gmail.com>
This commit is contained in:
co-authored by
Flora Feng
parent
974cb65155
commit
9bf44c4013
@@ -134,6 +134,9 @@ class KimiK3ReasoningParser(ReasoningParser):
|
||||
self._think_close_ids = tokenizer.encode(
|
||||
self._think_close, add_special_tokens=False
|
||||
)
|
||||
self._response_open_ids = tokenizer.encode(
|
||||
self._response_open, add_special_tokens=False
|
||||
)
|
||||
self._last_streaming_delta_token_ids: tuple[int, ...] | None = None
|
||||
self._last_streaming_content_token_ids: list[int] | None = None
|
||||
|
||||
@@ -211,6 +214,43 @@ class KimiK3ReasoningParser(ReasoningParser):
|
||||
return cached_content_ids
|
||||
return self._extract_content_ids(input_ids)
|
||||
|
||||
def count_reasoning_tokens(self, token_ids: Sequence[int]) -> int:
|
||||
if not self._thinking_enabled:
|
||||
return 0
|
||||
think_open = self._think_open_ids
|
||||
think_close = self._think_close_ids
|
||||
response_open = self._response_open_ids
|
||||
first_ids = {m[0] for m in (think_open, think_close, response_open) if m}
|
||||
n = len(token_ids)
|
||||
count = 0
|
||||
in_reasoning = True
|
||||
seen_open = False
|
||||
i = 0
|
||||
while i < n:
|
||||
head = token_ids[i]
|
||||
if head in first_ids:
|
||||
if i + len(think_open) <= n and _match_at(token_ids, i, think_open):
|
||||
in_reasoning = True
|
||||
seen_open = True
|
||||
i += len(think_open)
|
||||
continue
|
||||
if i + len(think_close) <= n and _match_at(token_ids, i, think_close):
|
||||
in_reasoning = False
|
||||
i += len(think_close)
|
||||
continue
|
||||
if (
|
||||
not seen_open
|
||||
and i + len(response_open) <= n
|
||||
and _match_at(token_ids, i, response_open)
|
||||
):
|
||||
in_reasoning = False
|
||||
i += len(response_open)
|
||||
continue
|
||||
if in_reasoning:
|
||||
count += 1
|
||||
i += 1
|
||||
return count
|
||||
|
||||
def _strip_content_wrapper(self, text: str) -> str:
|
||||
"""Strip ``<|open|>response<|sep|>…<|close|>response<|sep|>`` wrapper and
|
||||
``<|close|>message<|sep|>`` from *text*.
|
||||
|
||||
Reference in New Issue
Block a user