diff --git a/python/sglang/srt/disaggregation/decode.py b/python/sglang/srt/disaggregation/decode.py index 6595f678ad..58ec809320 100644 --- a/python/sglang/srt/disaggregation/decode.py +++ b/python/sglang/srt/disaggregation/decode.py @@ -96,6 +96,7 @@ from sglang.srt.mem_cache.common import ( page_align_floor, release_kv_cache, restore_kv_cache, + suffix_page_indices, ) from sglang.srt.mem_cache.deepseek_v4_memory_pool import DeepSeekV4TokenToKVPool from sglang.srt.mem_cache.hicache_storage import PoolName, PoolTransfer @@ -1659,6 +1660,18 @@ class DecodePreallocQueue(DecodeHiCachePreallocMixin): device_page_size = self.token_to_kv_pool.page_size return kv_to_page_indices(kv_indices_full, device_page_size) + def _dsa_pages_payload(): + # Mirror of prefill _dsa_pages_payload: the cached prefix keeps + # its own indexer rows, and rows still being restored by + # HiCache are not written until the restore commits. + return suffix_page_indices( + self.req_to_token_pool.req_to_token, + decode_req.req.kv.req_pool_idx, + total_prefix_len, + seq_len, + self.token_to_kv_pool.page_size, + ) + def _dsa_tail_payload(): return get_dsa_tail_state_indices( self.token_to_kv_pool, @@ -1699,7 +1712,7 @@ class DecodePreallocQueue(DecodeHiCachePreallocMixin): StateType.QSA_PENDING: _qsa_pending_payload, StateType.QSA_COMPRESSED: _full_kv_pages_payload, StateType.SWA: _swa_payload, - StateType.DSA: _full_kv_pages_payload, + StateType.DSA: _dsa_pages_payload, StateType.DSA_TAIL: _dsa_tail_payload, StateType.MINIMAX_INDEX_K: _full_kv_pages_payload, StateType.MINIMAX_DENSE_KV: _full_kv_pages_payload, diff --git a/python/sglang/srt/disaggregation/prefill.py b/python/sglang/srt/disaggregation/prefill.py index da0aed989f..0f6d8d2d22 100644 --- a/python/sglang/srt/disaggregation/prefill.py +++ b/python/sglang/srt/disaggregation/prefill.py @@ -78,6 +78,7 @@ from sglang.srt.mem_cache.common import ( kv_to_page_indices, kv_to_page_num, release_kv_cache, + suffix_page_indices, ) from sglang.srt.mem_cache.deepseek_v4_memory_pool import DeepSeekV4TokenToKVPool from sglang.srt.mem_cache.memory_pool import HybridLinearKVPool @@ -1549,6 +1550,19 @@ class SchedulerDisaggregationPrefillMixin: ] return kv_to_page_indices(kv_indices_full, page_size) + def _dsa_pages_payload(): + # Decode already holds the indexer rows of the prefix it cached + # (decode_prefix_len, page-aligned), and its rows for a prefix + # still being restored point at no page yet: send only the + # suffix, positionally matching decode's _dsa_pages_payload. + return suffix_page_indices( + self.req_to_token_pool.req_to_token, + req.kv.req_pool_idx, + req.disagg_decode_prefix_len, + seq_len, + page_size, + ) + def _dsa_tail_payload(): return get_dsa_tail_state_indices( self.token_to_kv_pool_allocator.get_kvcache(), @@ -1588,7 +1602,7 @@ class SchedulerDisaggregationPrefillMixin: StateType.QSA_PENDING: _qsa_pending_payload, StateType.QSA_COMPRESSED: _full_kv_pages_payload, StateType.SWA: _swa_payload, - StateType.DSA: _full_kv_pages_payload, + StateType.DSA: _dsa_pages_payload, StateType.DSA_TAIL: _dsa_tail_payload, StateType.MINIMAX_INDEX_K: _full_kv_pages_payload, StateType.MINIMAX_DENSE_KV: _full_kv_pages_payload, diff --git a/python/sglang/srt/mem_cache/common.py b/python/sglang/srt/mem_cache/common.py index a1d714b15d..6c0061ca96 100644 --- a/python/sglang/srt/mem_cache/common.py +++ b/python/sglang/srt/mem_cache/common.py @@ -47,6 +47,22 @@ def kv_to_page_indices(kv_indices: torch.Tensor, page_size: int) -> np.ndarray: return (kv_indices[::page_size] // page_size).cpu().numpy() +def suffix_page_indices( + req_to_token: torch.Tensor, + req_pool_idx: int, + prefix_len: int, + seq_len: int, + page_size: int, +) -> np.ndarray: + """Pages of a request's tokens [prefix_len, seq_len). + + PD prefill and decode both call this with the same page-aligned prefix + (decode_prefix_len), so their lists pair up page by page even though their + rows point at different physical pages. + """ + return kv_to_page_indices(req_to_token[req_pool_idx, prefix_len:seq_len], page_size) + + def kv_to_page_num(num_kv_indices: int, page_size: int): return (num_kv_indices + page_size - 1) // page_size