From de153cf76a7d705a0acfaea5feb69a706ec0aa14 Mon Sep 17 00:00:00 2001 From: fzyzcjy <5236035+fzyzcjy@users.noreply.github.com> Date: Mon, 1 Dec 2025 15:30:13 +0800 Subject: [PATCH] Fix speculative decoding error when retracting (#14180) --- python/sglang/srt/managers/schedule_batch.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/python/sglang/srt/managers/schedule_batch.py b/python/sglang/srt/managers/schedule_batch.py index a11e8fc7e..aaa0a00b2 100644 --- a/python/sglang/srt/managers/schedule_batch.py +++ b/python/sglang/srt/managers/schedule_batch.py @@ -1528,6 +1528,18 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin): ) if page_size == 1: return len(requests) + + if not self.spec_algorithm.is_none(): + # A loose bound that err towards safety + server_args = get_global_server_args() + thresh = server_args.speculative_num_draft_tokens + ( + (server_args.speculative_eagle_topk or 1) + * (server_args.speculative_num_steps or 1) + ) + return sum( + 1 for req in requests if ((req.seqlen + thresh) % page_size) <= thresh + ) + # In the decoding phase, the length of a request's KV cache should be # the total length of the request minus 1 return (