[Scheduler] Preserve per-group eviction order in deferred block free (port of upstream #48532)

Repair: the port driver logged this patch as resolved but produced no
commit; the -3 merge dropped the coordinator code hunk. Reapplied to HEAD.

Co-Authored-By: Hinata <hinata@localhost>
Signed-off-by: MOVZX <movzx@yahoo.com>
This commit is contained in:
2026-10-09 20:13:04 +07:00
co-authored by Hinata
parent 7c0a916c5d
commit 0f72060d66
3 changed files with 19 additions and 9 deletions
+9 -4
View File
@@ -402,19 +402,24 @@ class KVCacheCoordinator(ABC):
"""Pop the request's bookkeeping from all single-type managers and """Pop the request's bookkeeping from all single-type managers and
return its blocks without returning them to the block pool. The return its blocks without returning them to the block pool. The
caller must eventually pass the returned blocks to caller must eventually pass the returned blocks to
`block_pool.free_blocks`, freeing them in reverse order (so that `block_pool.free_blocks` as-is: they are already ordered so that
tail blocks are evicted first). tail blocks are evicted first, within each KV cache group.
Args: Args:
request_id: The request ID. request_id: The request ID.
Returns: Returns:
The request's blocks in allocation order. The request's blocks in eviction order (each group's blocks
reversed, then concatenated group by group).
""" """
blocks: list[KVCacheBlock] = [] blocks: list[KVCacheBlock] = []
for manager in self.single_type_managers: for manager in self.single_type_managers:
blocks.extend(manager.pop_blocks_for_free(request_id)) # Reverse within each group so the concatenation is already in
# eviction order, matching `free()`, which frees each group in its
# own reversed `free_blocks()` call. Reversing the flat list
# instead would emit whole groups in the wrong order.
blocks.extend(reversed(manager.pop_blocks_for_free(request_id)))
return blocks return blocks
def get_num_common_prefix_blocks(self, running_request_id: str) -> list[int]: def get_num_common_prefix_blocks(self, running_request_id: str) -> list[int]:
+4 -2
View File
@@ -651,13 +651,15 @@ class KVCacheManager:
def pop_blocks_for_free(self, request: Request) -> list[KVCacheBlock]: def pop_blocks_for_free(self, request: Request) -> list[KVCacheBlock]:
"""Pop the request's bookkeeping and return its blocks without """Pop the request's bookkeeping and return its blocks without
returning them to the block pool. The caller must eventually free returning them to the block pool. The caller must eventually free
them in reverse order (so that tail blocks are evicted first). them as-is: they are already ordered so that tail blocks are
evicted first, within each KV cache group.
Args: Args:
request: The request to pop the blocks for. request: The request to pop the blocks for.
Returns: Returns:
The request's blocks in allocation order. The request's blocks in eviction order (each group's blocks
reversed, then concatenated group by group).
""" """
return self.coordinator.pop_blocks_for_free(request.request_id) return self.coordinator.pop_blocks_for_free(request.request_id)
+6 -3
View File
@@ -2831,7 +2831,9 @@ class Scheduler(SchedulerInterface):
if not self.defer_block_free or fence_seq <= self.processed_step_seq: if not self.defer_block_free or fence_seq <= self.processed_step_seq:
self.kv_cache_manager.block_pool.free_blocks(blocks) self.kv_cache_manager.block_pool.free_blocks(blocks)
return return
self.deferred_frees.append((fence_seq, blocks[::-1])) # The immediate path above frees these as-is, so enqueue them as-is:
# the drain no longer reverses.
self.deferred_frees.append((fence_seq, blocks))
def _drain_deferred_frees(self): def _drain_deferred_frees(self):
"""Return deferred blocks whose fence step has completed. """Return deferred blocks whose fence step has completed.
@@ -2845,8 +2847,9 @@ class Scheduler(SchedulerInterface):
if fence > self.processed_step_seq: if fence > self.processed_step_seq:
break break
_, blocks = self.deferred_frees.popleft() _, blocks = self.deferred_frees.popleft()
# Free in reverse order so that the tail blocks are evicted first. # Already in eviction order (reversed per KV cache group by
self.kv_cache_manager.block_pool.free_blocks(reversed(blocks)) # `pop_blocks_for_free`), so free as-is.
self.kv_cache_manager.block_pool.free_blocks(blocks)
def get_num_unfinished_requests(self) -> int: def get_num_unfinished_requests(self) -> int:
if self._pause_state == PauseState.PAUSED_ALL: if self._pause_state == PauseState.PAUSED_ALL: