Repository navigation
[Scheduler] Refresh waiting requests' cached prefixes under FCFS-like policies with LRU eviction #41593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hnyls2002
merged 19 commits into
sgl-project:main
from
peilii:peili/refresh-waiting-prefix-fcfs
Oct 9, 2026
+191
−20
Merged
[Scheduler] Refresh waiting requests' cached prefixes under FCFS-like policies with LRU eviction #41593
Changes from 8 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
f2fe3d0
[Scheduler] Keep waiting requests' cached prefixes resident under cac…
peilii 3a79e2d
Document the 128 waiting-queue cap on the prefix refresh
peilii b788b80
Merge branch 'main' into peili/refresh-waiting-prefix-fcfs
nvpohanh a8d6d61
Merge branch 'main' into peili/refresh-waiting-prefix-fcfs
nvpohanh a2f2367
Refresh waiting prefixes on the device tree only and skip chunk caches
peilii 0d92e71
Merge remote-tracking branch 'fork/peili/refresh-waiting-prefix-fcfs'…
peilii 5708671
Give UnifiedRadixCache the device-only prefix refresh
peilii 998f9e9
Refresh waiting prefixes head-last at any depth, also through Streami…
peilii 80f7932
Build the request radix key in one place for the matcher and the refresh
peilii 8c898f6
Merge branch 'main' into peili/refresh-waiting-prefix-fcfs
nvpohanh f910570
Merge branch 'main' into peili/refresh-waiting-prefix-fcfs
nvpohanh 6767a7a
Merge branch 'main' into peili/refresh-waiting-prefix-fcfs
nvpohanh 4a3d7f1
Merge branch 'main' into peili/refresh-waiting-prefix-fcfs
hnyls2002 60bdb8c
[Scheduler] Refresh waiting prefixes after sorting, throttled and LRU…
hnyls2002 7a52058
[Scheduler] Address review: rename refresh_prefix, trim tests and com…
hnyls2002 8c5f848
[Scheduler] Rename the prefix touch API and share the queue bound
hnyls2002 893db7d
[Scheduler] Trim the comments on the waiting-prefix touch
hnyls2002 e5253fe
Merge remote-tracking branch 'origin/main' into peili/refresh-waiting…
hnyls2002 c42da2b
[Scheduler] Read device_prefix_len in the waiting-prefix touch test
hnyls2002 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
207 changes: 207 additions & 0 deletions
207
test/registered/unit/mem_cache/test_fcfs_waiting_prefix_refresh.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,207 @@ | ||
| import time | ||
| import unittest | ||
| import unittest.mock | ||
| from array import array | ||
| from dataclasses import replace | ||
|
|
||
| import torch | ||
|
|
||
| from sglang.srt.environ import envs | ||
| from sglang.srt.managers.schedule_batch import Req | ||
| from sglang.srt.managers.schedule_policy import ( | ||
| WAITING_PREFIX_REFRESH_MAX_QUEUE, | ||
| SchedulePolicy, | ||
| ) | ||
| from sglang.srt.mem_cache.allocator import TokenToKVPoolAllocator | ||
| from sglang.srt.mem_cache.base_prefix_cache import ( | ||
| EvictParams, | ||
| InsertParams, | ||
| MatchPrefixParams, | ||
| ) | ||
| from sglang.srt.mem_cache.cache_init_params import CacheInitParams | ||
| from sglang.srt.mem_cache.chunk_cache import ChunkCache | ||
| from sglang.srt.mem_cache.memory_pool import MHATokenToKVPool, ReqToTokenPool | ||
| from sglang.srt.mem_cache.radix_cache import RadixCache, RadixKey | ||
| from sglang.srt.mem_cache.unified_cache.components import ComponentType | ||
| from sglang.srt.mem_cache.unified_radix_cache import UnifiedRadixCache | ||
| from sglang.srt.runtime_context import publish, reset_context | ||
| from sglang.srt.sampling.sampling_params import SamplingParams | ||
| from sglang.srt.server_args import ServerArgs | ||
| from sglang.srt.session.streaming_session import StreamingSession | ||
| from sglang.test.ci.ci_register import register_cpu_ci | ||
| from sglang.test.test_utils import CustomTestCase | ||
|
|
||
| register_cpu_ci(est_time=10, suite="base-a-test-cpu") | ||
|
|
||
| OLDER_PREFIX = [1, 2, 3, 4] | ||
| NEWER_PREFIX = [5, 6, 7, 8] | ||
|
|
||
|
|
||
| class TestFcfsWaitingPrefixRefresh(CustomTestCase): | ||
| """Under FCFS an LRU eviction must not take the cached prefix of a request that | ||
| is waiting in the queue while a prefix no pending request needs is available.""" | ||
|
|
||
| def _make_cache(self, params): | ||
| return RadixCache(params) | ||
|
|
||
| def setUp(self): | ||
| reset_context() | ||
| self.addCleanup(reset_context) | ||
| publish(ServerArgs(model_path="dummy"), role="test") | ||
| torch.set_default_device(None) | ||
| kv_cache = MHATokenToKVPool( | ||
| size=16, | ||
| page_size=1, | ||
| dtype=torch.float16, | ||
| head_num=1, | ||
| head_dim=8, | ||
| layer_num=1, | ||
| device="cpu", | ||
| enable_memory_saver=False, | ||
| ) | ||
| allocator = TokenToKVPoolAllocator( | ||
| size=16, | ||
| dtype=torch.float16, | ||
| device="cpu", | ||
| kvcache=kv_cache, | ||
| need_sort=False, | ||
| ) | ||
| req_to_token_pool = ReqToTokenPool( | ||
| size=8, max_context_len=64, device="cpu", enable_memory_saver=False | ||
| ) | ||
| self.cache = self._make_cache( | ||
| CacheInitParams( | ||
| disable=False, | ||
| req_to_token_pool=req_to_token_pool, | ||
| token_to_kv_pool_allocator=allocator, | ||
| page_size=1, | ||
| eviction_policy="lru", | ||
| enable_kv_cache_events=False, | ||
| ) | ||
| ) | ||
| self.policy = self._make_policy(self.cache) | ||
| self._insert(OLDER_PREFIX, [10, 11, 12, 13]) | ||
| time.sleep(0.005) | ||
| self._insert(NEWER_PREFIX, [20, 21, 22, 23]) | ||
| time.sleep(0.005) | ||
| self.waiting = self._make_req(1, OLDER_PREFIX + [9]) | ||
|
|
||
| def _make_policy(self, cache): | ||
| return SchedulePolicy( | ||
| policy="fcfs", | ||
| tree_cache=cache, | ||
| enable_hierarchical_cache=False, | ||
| enable_priority_scheduling=False, | ||
| schedule_low_priority_values_first=False, | ||
| ) | ||
|
|
||
| def _make_req(self, rid, tokens): | ||
| return Req(rid, "", array("q", tokens), SamplingParams()) | ||
|
|
||
| def _insert(self, tokens, slots): | ||
| self.cache.insert( | ||
| InsertParams( | ||
| key=RadixKey(array("q", tokens)), | ||
| value=torch.tensor(slots, dtype=torch.int64), | ||
| ) | ||
| ) | ||
|
|
||
| def _matched_len(self, tokens): | ||
| result = self.cache.match_prefix( | ||
| MatchPrefixParams(key=RadixKey(array("q", tokens))) | ||
| ) | ||
| return len(result.device_indices) | ||
|
|
||
| def _assert_older_prefix_survives_one_eviction(self): | ||
| self.cache.evict(EvictParams(num_tokens=len(NEWER_PREFIX))) | ||
| self.assertEqual(self._matched_len(OLDER_PREFIX), len(OLDER_PREFIX)) | ||
| self.assertEqual(self._matched_len(NEWER_PREFIX), 0) | ||
|
|
||
| def test_refresh_keeps_the_waiting_request_prefix_resident(self): | ||
| with envs.SGLANG_ENABLE_WAITING_PREFIX_REFRESH.override(True): | ||
| self.policy.calc_priority([self.waiting]) | ||
| self._assert_older_prefix_survives_one_eviction() | ||
|
|
||
| def test_refresh_disabled_restores_plain_lru(self): | ||
| with envs.SGLANG_ENABLE_WAITING_PREFIX_REFRESH.override(False): | ||
| self.policy.calc_priority([self.waiting]) | ||
| self.cache.evict(EvictParams(num_tokens=len(OLDER_PREFIX))) | ||
| self.assertEqual(self._matched_len(OLDER_PREFIX), 0) | ||
| self.assertEqual(self._matched_len(NEWER_PREFIX), len(NEWER_PREFIX)) | ||
|
|
||
| def test_head_of_queue_is_refreshed_last(self): | ||
| # FCFS admits the head next, so when eviction must take a waiting prefix | ||
| # it has to be the one furthest from admission. | ||
| second = self._make_req(2, NEWER_PREFIX + [9]) | ||
| with envs.SGLANG_ENABLE_WAITING_PREFIX_REFRESH.override(True): | ||
| self.policy.calc_priority([self.waiting, second]) | ||
| self._assert_older_prefix_survives_one_eviction() | ||
|
|
||
| def test_deep_queue_still_refreshes_the_head(self): | ||
| fillers = [ | ||
| self._make_req(2 + i, [1000 + i]) | ||
| for i in range(WAITING_PREFIX_REFRESH_MAX_QUEUE) | ||
| ] | ||
| with envs.SGLANG_ENABLE_WAITING_PREFIX_REFRESH.override(True): | ||
| self.policy.calc_priority([self.waiting] + fillers) | ||
| self._assert_older_prefix_survives_one_eviction() | ||
|
|
||
| def test_refresh_never_calls_the_general_matcher(self): | ||
| # External-cache implementations (FlexKV, LMCache, hierarchical tiers) give | ||
| # match_prefix side effects: lookups enqueued, KV allocated or loaded per call. | ||
| calls = [] | ||
| original = type(self.cache).match_prefix | ||
|
|
||
| def spy(cache, params): | ||
| calls.append(params) | ||
| return original(cache, params) | ||
|
|
||
| with ( | ||
| unittest.mock.patch.object(type(self.cache), "match_prefix", spy), | ||
| envs.SGLANG_ENABLE_WAITING_PREFIX_REFRESH.override(True), | ||
| ): | ||
| self.policy.calc_priority([self.waiting]) | ||
| self.assertEqual(calls, []) | ||
| self._assert_older_prefix_survives_one_eviction() | ||
|
|
||
| def test_streaming_session_wrapper_forwards_the_refresh(self): | ||
| # --enable-streaming-session wraps caches without native session support; | ||
| # the wrapper inherits the base no-op unless it forwards explicitly. | ||
| policy = self._make_policy(StreamingSession(self.cache)) | ||
| with envs.SGLANG_ENABLE_WAITING_PREFIX_REFRESH.override(True): | ||
| policy.calc_priority([self.waiting]) | ||
| self._assert_older_prefix_survives_one_eviction() | ||
|
|
||
| def test_chunk_cache_skips_the_refresh(self): | ||
| # --disable-radix-cache deployments have no tree or LRU state to refresh; the | ||
| # scheduler hot path must not build keys or match for every waiting request. | ||
| chunk_cache = ChunkCache( | ||
| CacheInitParams( | ||
| disable=True, | ||
| req_to_token_pool=self.cache.req_to_token_pool, | ||
| token_to_kv_pool_allocator=self.cache.token_to_kv_pool_allocator, | ||
| page_size=1, | ||
| eviction_policy="lru", | ||
| enable_kv_cache_events=False, | ||
| ) | ||
| ) | ||
| policy = self._make_policy(chunk_cache) | ||
| with ( | ||
| unittest.mock.patch.object(ChunkCache, "match_prefix") as match_prefix, | ||
| unittest.mock.patch.object(ChunkCache, "refresh_device_prefix") as refresh, | ||
| envs.SGLANG_ENABLE_WAITING_PREFIX_REFRESH.override(True), | ||
| ): | ||
| policy.calc_priority([self.waiting]) | ||
| match_prefix.assert_not_called() | ||
| refresh.assert_not_called() | ||
|
|
||
|
|
||
| class TestFcfsWaitingPrefixRefreshUnified(TestFcfsWaitingPrefixRefresh): | ||
| """The same contract on UnifiedRadixCache, the default prefix cache.""" | ||
|
|
||
| def _make_cache(self, params): | ||
| return UnifiedRadixCache(replace(params, tree_components=(ComponentType.FULL,))) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.