[HiCache] Attribute buffer-mode storage hits against the joint device match (#41758)

Co-authored-by: merrymercy <merrymercy@users.noreply.github.com>
Co-authored-by: xiezhq-hermann <xiezhq-hermann@users.noreply.github.com>
This commit is contained in:
metamergebot
2026-09-30 11:46:30 -07:00
committed by GitHub
co-authored by merrymercy xiezhq-hermann
parent d26ec4690d
commit 9f9f9e702a
4 changed files with 83 additions and 18 deletions
@@ -897,10 +897,11 @@ class BufferModePipeline:
if not (req.host_hit_is_storage and req.host_loaded_length > 0):
self._clear_storage_hit(req)
return True
if len(req.prefix_indices) >= f.matched_len + f.num_tokens:
joint_len = len(req.prefix_indices)
if joint_len >= f.matched_len + f.num_tokens:
# The joint match already covers the staged span; a shorter FULL-only
# prefix would strand the slots recomputed below cache_protected_len.
self._resolve_device_covered(req, f)
self._resolve_device_covered(req)
return True
key = RadixKey(
f.key_tokens,
@@ -935,26 +936,25 @@ class BufferModePipeline:
if t.name == PoolName.SWA and t.host_indices is not None
)
if full_tokens == 0 and swa_tokens == 0:
self._resolve_device_covered(req, f)
self._resolve_device_covered(req)
return True
req.host_hit_length = full_tokens
req.swa_host_hit_length = swa_tokens
req.storage_hit_length = full_tokens
req.storage_hit_start = matched_len if full_tokens else None
# The device alone serves only this pass's joint match; the rest of the
# span, fetched FULL or resident FULL the aux tail unlocks, is storage's.
req.storage_hit_length = f.matched_len + f.num_tokens - joint_len
req.storage_hit_start = joint_len
req.host_hit_is_storage = True
req.staged_prefetch_plan = StagedPrefetchPlan(
f.operation_id, key, matched_len, full_tokens, swa_tokens
)
return True
def _resolve_device_covered(self, req: Req, f: _StagedPrefetch) -> None:
def _resolve_device_covered(self, req: Req) -> None:
req.host_hit_length = 0
req.swa_host_hit_length = 0
self._clear_storage_hit(req)
self._cache._resolve_storage_prefetch_tokens(
req.cache_request_handle, f.num_tokens, reason="device_covered"
)
self.release_staged_hold(req.cache_request_handle, reason=None)
self.release_staged_hold(req.cache_request_handle, reason="device_covered")
@staticmethod
def _clear_storage_hit(req: Req) -> None:
@@ -1162,8 +1162,8 @@ class BufferModePipeline:
del self.staged_prefetches[request]
self._staged_admission_defers.pop(request, None)
req.staged_prefetch_plan = None
cache._resolve_storage_prefetch_tokens(
request, trim_tokens, reason="device_covered"
cache._settle_storage_prefetch_hit(
request, credited_tokens=req.storage_hit_length
)
swa_dev = next(
@@ -2559,6 +2559,17 @@ class UnifiedRadixCache(BasePrefixCache):
unfulfilled, reason
)
def _settle_storage_prefetch_hit(
self, request: CacheRequestHandle, credited_tokens: int
) -> None:
"""At buffer-mode admission, hit tokens not credited to storage were
covered by the device's joint match; the rest resolve at the fill ack."""
remaining = self._storage_prefetch_hit_remaining_by_reqid.get(request)
if remaining is not None:
self._resolve_storage_prefetch_tokens(
request, remaining - credited_tokens, reason="device_covered"
)
def finish_storage_prefetch_admission(
self, request: CacheRequestHandle, fulfilled_tokens: int, reason: Optional[str]
) -> None:
@@ -2775,9 +2786,8 @@ class UnifiedRadixCache(BasePrefixCache):
)
self.ongoing_prefetch[request] = info
self.cache_controller.trim_prefetch_full_head(operation, trim_tokens)
self._resolve_storage_prefetch_tokens(
request, trim_tokens, reason="device_covered"
)
# Labeled at admission: FULL reusable only with the fetched aux tail is a
# storage hit there, not device_covered.
return info, hit_tokens - trim_tokens, original_hit_tokens
def revoke_pending_prefetch(self, request: CacheRequestHandle) -> None: