diff --git a/python/sglang/srt/mem_cache/unified_radix_cache.py b/python/sglang/srt/mem_cache/unified_radix_cache.py index ee3087eecf..82be213ea2 100644 --- a/python/sglang/srt/mem_cache/unified_radix_cache.py +++ b/python/sglang/srt/mem_cache/unified_radix_cache.py @@ -2708,6 +2708,9 @@ class UnifiedRadixCache(BasePrefixCache): return beliefs = self.storage_existence_cache verdicts = operation.query_pool_hit_pages + # No rank-agreed KV query evidence; leave beliefs unchanged. + if PoolName.KV not in verdicts: + return folded_pages = operation.storage_hit_count // self.page_size def verdict(pool: PoolName) -> tuple[int, bool]: @@ -2852,7 +2855,6 @@ class UnifiedRadixCache(BasePrefixCache): info = self.ongoing_prefetch.get(request) if info is None: return - self._invalidate_absent_from_hit_query(info.operation) # Every revoke path runs before the bounce alloc, so buffer mode # holds no occupancy here; post-alloc aborts go through # release_aborted_request instead. @@ -3013,13 +3015,11 @@ class UnifiedRadixCache(BasePrefixCache): break parked.popleft() for operation in _drain_queue(cc.prefetch_hit_queue, n_storage_hit): + self._invalidate_absent_from_hit_query(operation) request = operation.handle hit_tokens = operation.storage_hit_count info = self.ongoing_prefetch.get(request) if info is None: - # Request already aborted/cleaned up; still flush the - # query's absent-hash feedback. - self._invalidate_absent_from_hit_query(operation) if hit_tokens > 0: self.discard_storage_prefetch_accounting(request) continue @@ -3053,7 +3053,6 @@ class UnifiedRadixCache(BasePrefixCache): self.storage_prefetch_retries.poll_miss(request.rid) self.revoke_pending_prefetch(request) continue - self._invalidate_absent_from_hit_query(operation) self._account_prefetch_outcome(operation, revoked=False) # A parked hit keeps its turn: a newer hit must not take the # staging or anchor budget the parked head is waiting for. diff --git a/test/registered/unit/mem_cache/test_buffer_mode_sidecar.py b/test/registered/unit/mem_cache/test_buffer_mode_sidecar.py index 297d348065..f34bdbca02 100644 --- a/test/registered/unit/mem_cache/test_buffer_mode_sidecar.py +++ b/test/registered/unit/mem_cache/test_buffer_mode_sidecar.py @@ -49,6 +49,48 @@ register_cpu_ci(est_time=8, suite="base-a-test-cpu") class TestBufferModeSidecar(unittest.TestCase): + def test_cancelled_query_distinguishes_skipped_query_from_miss(self): + for peer_kv_pages in (-1, 0): + with self.subTest(peer_kv_pages=peer_kv_pages): + + def reduce(values, *_): + peer = torch.full_like(values, -1) + peer[0] = 0 + peer[1 + list(PoolName).index(PoolName.KV)] = peer_kv_pages + values.copy_(torch.minimum(values, peer)) + + controller = SimpleNamespace( + page_size=2, + storage_backend=SimpleNamespace(batch_exists=lambda *_: 2), + prefetch_hits_sync_groups=[], + _all_reduce=reduce, + ) + operation = PrefetchOperation( + CacheRequestHandle("cancelled", 0), list(range(4)) + ) + _, hit_tokens = HybridCacheController._storage_hit_query( + controller, operation + ) + operation.mark_terminate() + operation.storage_hit_count = ( + HybridCacheController._sync_prefetch_hit_query( + controller, operation, hit_tokens + ) + ) + cache = UnifiedRadixCache.__new__(UnifiedRadixCache) + cache.host_memory_mode = "buffer_only" + cache.tree_core = SimpleNamespace(page_size=2) + cache.components = {} + cache.storage_existence_cache = StorageExistenceCache() + beliefs = cache.storage_existence_cache.pool(PoolName.KV) + chain = operation.all_hash_values + beliefs.add(chain) + cache._invalidate_absent_from_hit_query(operation) + self.assertEqual( + [beliefs.contains(h) for h in chain], + [peer_kv_pages == -1] * len(chain), + ) + @staticmethod def _swa_component(): return SimpleNamespace( diff --git a/test/registered/unit/mem_cache/test_storage_prefetch_lifecycle.py b/test/registered/unit/mem_cache/test_storage_prefetch_lifecycle.py index 6489db3e11..c49f4446fc 100644 --- a/test/registered/unit/mem_cache/test_storage_prefetch_lifecycle.py +++ b/test/registered/unit/mem_cache/test_storage_prefetch_lifecycle.py @@ -167,6 +167,7 @@ def _terminated_query(cache, rid, hit_tokens): handle=handle, storage_hit_count=hit_tokens, stats_requested_tokens=8, + all_hash_values=None, is_terminated=lambda: True, ) cache.ongoing_prefetch[handle] = _OngoingPrefetch(