Fix token leak with logprob_start_len=0 in streaming sessions (#20557)

This commit is contained in:
Leon Gao
2026-03-19 15:37:27 -07:00
committed by GitHub
parent 62361e8fd5
commit 63c38aba5e
2 changed files with 157 additions and 1 deletions

View File

@@ -181,8 +181,26 @@ class SessionAwareCache(BasePrefixCache):
slot.restore_to_req(req)
max_prefix_len = len(params.key.token_ids)
# For streaming sessions, ignore the logprob_start_len truncation on
# the key and reuse the full committed KV from the slot. The key may
# have been truncated (e.g. to length 0 when logprob_start_len=0), but
# the session slot already holds those KV tokens — skipping them would
# orphan allocated memory. Use fill_ids length (the actual input for
# this turn) as the upper bound instead of the truncated key.
# Also clamp logprob_start_len so the scheduler doesn't expect logprobs
# for tokens that are already in the session's committed KV.
input_len = (
len(req.fill_ids) if req.fill_ids is not None else len(params.key.token_ids)
)
max_prefix_len = max(input_len - 1, 0)
prefix_len = min(req.kv_committed_len, max_prefix_len)
if (
req.return_logprob
and req.logprob_start_len >= 0
and req.logprob_start_len < prefix_len
):
req.logprob_start_len = prefix_len
device_indices = self.req_to_token_pool.req_to_token[
req.req_pool_idx, :prefix_len
].to(dtype=torch.int64)