[Bugfix][Reasoning] kimi_k3: O(delta) reasoning-end check on the decode path (#50886)
Signed-off-by: abmfy <abmfy@icloud.com> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
385d4c084e
commit
adbf08d977
@@ -248,3 +248,98 @@ def test_adjust_request_keeps_xtml_markers_contiguous():
|
||||
assert adjusted.skip_special_tokens is False
|
||||
if hasattr(adjusted, "spaces_between_special_tokens"):
|
||||
assert adjusted.spaces_between_special_tokens is False
|
||||
|
||||
|
||||
OPEN_IDS = [1, 2, 3]
|
||||
CLOSE_IDS = [4, 2, 3]
|
||||
|
||||
|
||||
def _reference_is_reasoning_end(input_ids: list[int]) -> bool:
|
||||
"""Full-sequence reference for the streaming check to be measured against.
|
||||
|
||||
Two independent last-occurrence scans, i.e. the straightforward reading of
|
||||
"reasoning ended iff the newest think marker is a close marker".
|
||||
"""
|
||||
|
||||
def last(needle: list[int]) -> int:
|
||||
for i in range(len(input_ids) - len(needle), -1, -1):
|
||||
if input_ids[i : i + len(needle)] == needle:
|
||||
return i
|
||||
return -1
|
||||
|
||||
last_close, last_open = last(CLOSE_IDS), last(OPEN_IDS)
|
||||
if last_open == -1:
|
||||
return last_close != -1
|
||||
return last_close > last_open
|
||||
|
||||
|
||||
def test_is_reasoning_end_streaming_only_scans_the_step_window():
|
||||
"""The decode-step check must not re-derive the answer from the whole
|
||||
sequence: an already-closed think block earlier in the sequence is the
|
||||
engine's business (it latches `reasoning_ended`), not this call's."""
|
||||
parser = KimiK3ReasoningParser(DummyTokenizer())
|
||||
prompt = [*CLOSE_IDS, *OPEN_IDS, 9, 9, 9]
|
||||
|
||||
assert not parser.is_reasoning_end_streaming([*prompt, 7], [7])
|
||||
assert parser.is_reasoning_end_streaming([*prompt, *CLOSE_IDS], CLOSE_IDS)
|
||||
|
||||
|
||||
def test_is_reasoning_end_streaming_detects_marker_across_step_boundary():
|
||||
"""A 3-token marker can be split over decode steps; the check carries the
|
||||
preceding len(marker)-1 tokens so the final token still completes it."""
|
||||
parser = KimiK3ReasoningParser(DummyTokenizer())
|
||||
history = [*OPEN_IDS, 5, *CLOSE_IDS[:2]]
|
||||
|
||||
assert not parser.is_reasoning_end_streaming(history, [CLOSE_IDS[1]])
|
||||
assert parser.is_reasoning_end_streaming([*history, CLOSE_IDS[2]], [CLOSE_IDS[2]])
|
||||
|
||||
|
||||
def test_is_reasoning_end_streaming_reopened_block_is_not_ended():
|
||||
"""Kimi K3 may close and immediately reopen the think channel inside one
|
||||
window (speculative decoding lands several tokens per step). The newest
|
||||
marker wins, matching is_reasoning_end."""
|
||||
parser = KimiK3ReasoningParser(DummyTokenizer())
|
||||
window = [*CLOSE_IDS, *OPEN_IDS]
|
||||
|
||||
assert not parser.is_reasoning_end_streaming([*OPEN_IDS, 5, *window], window)
|
||||
|
||||
|
||||
def test_is_reasoning_end_streaming_accepts_an_iterator_delta():
|
||||
"""`should_advance` may hand over an islice rather than a list."""
|
||||
parser = KimiK3ReasoningParser(DummyTokenizer())
|
||||
full = [*OPEN_IDS, 5, *CLOSE_IDS]
|
||||
|
||||
assert parser.is_reasoning_end_streaming(full, iter(CLOSE_IDS))
|
||||
|
||||
|
||||
def test_is_reasoning_end_streaming_thinking_disabled():
|
||||
parser = KimiK3ReasoningParser(
|
||||
DummyTokenizer(), chat_template_kwargs={"thinking": False}
|
||||
)
|
||||
|
||||
assert parser.is_reasoning_end_streaming([1], [1])
|
||||
|
||||
|
||||
@pytest.mark.parametrize("seed", range(24))
|
||||
def test_reasoning_end_matches_reference_over_marker_dense_sequences(seed):
|
||||
"""Both the full-sequence check and the per-step check must agree with the
|
||||
two-scan reference, on sequences where the markers (which share the suffix
|
||||
[2, 3]) collide and overlap constantly."""
|
||||
import random
|
||||
|
||||
rnd = random.Random(seed)
|
||||
parser = KimiK3ReasoningParser(DummyTokenizer())
|
||||
|
||||
for _ in range(200):
|
||||
head = [rnd.choice([1, 2, 3, 4]) for _ in range(rnd.randrange(0, 14))]
|
||||
delta = [rnd.choice([1, 2, 3, 4]) for _ in range(rnd.randrange(1, 5))]
|
||||
full = [*head, *delta]
|
||||
|
||||
assert parser.is_reasoning_end(full) == _reference_is_reasoning_end(full)
|
||||
|
||||
# The engine only calls the streaming check while reasoning is still
|
||||
# open, so that is the only case it has to agree on.
|
||||
if not _reference_is_reasoning_end(head):
|
||||
assert parser.is_reasoning_end_streaming(
|
||||
full, delta
|
||||
) == _reference_is_reasoning_end(full), (head, delta)
|
||||
|
||||
Reference in New Issue
Block a user