Read code changes as Git diffs. Identifying strings are masked; binary files are listed by their full relative names.
sglang · 1 changed files
Base commit: 340db9201005fc64635bb25db1d2538f955761c3
diff --git a/python/sglang/srt/managers/scheduler.py b/python/sglang/srt/managers/scheduler.pyindex a9615a14c512345b2d63323ff3ebd2fe3d9a0dca..2c04b8673f252ef5379fbb72402a25981c97500e 100644--- a/python/sglang/srt/managers/scheduler.py+++ b/python/sglang/srt/managers/scheduler.py@@ -1734,6 +1734,15 @@ class Scheduler( if self._engine_paused: continue + # A pending prefill can produce the next cache page needed by the+ # FCFS head. Publish that result before planning, otherwise the+ # plan captures a stale radix miss and redundantly recomputes the+ # shared prefix even if overlap is disabled before launch.+ processed_last_batch_result = False+ if self.result_queue and self._has_inflight_prefix_dependency():+ pop_and_process()+ processed_last_batch_result = True+ # Get the next batch to run plan = self.get_next_batch_to_run( running_batch=self.running_batch, last_batch=self.last_batch@@ -1748,7 +1757,8 @@ class Scheduler( # If we do not need to overlap the current batch with the last batch, # we can process the last batch immediately. if disable_overlap_for_batch:- pop_and_process()+ if not processed_last_batch_result:+ pop_and_process() # Opportunistic flush at the disable_overlap sync boundary: # forward_stream is idle (prev forward drained, next not launched), # so `_flush`'s non-urgent guard compacts freely. Sync-free, best-effort.@@ -1769,7 +1779,10 @@ class Scheduler( # Process the last batch if self.last_batch:- if not disable_overlap_for_batch:+ if (+ not disable_overlap_for_batch+ and not processed_last_batch_result+ ): pop_and_process() elif batch is None: # When the server is idle, do self-check and re-init some states@@ -3064,6 +3077,45 @@ class Scheduler( res = min(res, self.req_to_token_pool.available_size()) return res + def _has_inflight_prefix_dependency(self) -> bool:+ """Return whether the FCFS head can reuse KV from the pending prefill.++ With overlap scheduling, the next batch is planned before the preceding+ prefill result is processed. Planning another prefill in that window can+ compute a shared prefix a second time because the radix entry has not been+ published yet. Let the producer advance to decode for one pass instead;+ its result is then processed and the waiting request is rematched normally.+ """+ last_batch = self.last_batch+ if (+ not self.enable_overlap+ or not self.waiting_queue+ or last_batch is None+ or not last_batch.forward_mode.is_extend()+ ):+ return False++ waiting_req = self.waiting_queue[0]+ cached_len = len(waiting_req.prefix_indices)+ required_len = (cached_len // self.page_size + 1) * self.page_size+ waiting_ids = waiting_req.origin_input_ids + waiting_req.output_ids+ if required_len >= len(waiting_ids):+ return False++ for producer_req in last_batch.reqs:+ if producer_req.extra_key != waiting_req.extra_key:+ continue+ producer_ids = producer_req.origin_input_ids + producer_req.output_ids+ if (+ required_len < len(producer_ids)+ and producer_req.extend_range is not None+ and producer_req.extend_range.start < required_len+ and producer_req.extend_range.end >= required_len+ and waiting_ids[:required_len] == producer_ids[:required_len]+ ):+ return True+ return False+ def get_new_batch_prefill(self, running_batch: ScheduleBatch) -> NextBatchPlan: prefill_delayer_single_pass = None if self.prefill_delayer: