Avoid CP HiCache owner-lane planning stalls during load-back

CP HiCache load-back eviction planning previously recomputed per-owner page counts from node token tensors while scanning evictable leaves. Under shared-KV pressure this can put scheduler-side planning onto an expensive tensor-padding path and stall before load_back can complete.

This stores per-CP-size owner page counts on CP HiCache metadata and uses that CPU metadata for backed/resident CP nodes. Streaming abort handling also accepts int-like status codes so abort responses do not crash on .name/.value access. Temporary debug runbooks remain ignored.

Unnecessary prefill hot-path timing logs were removed before commit; owner-lane eviction now keeps warning-level output for slow planning, insufficient eviction, or remaining deficits only.

Constraint: CP shared-KV cache residency is page-owner based and already records page owners in CpHiCacheNodeMetadata.
Rejected: Keep verbose prefill/owner-lane timing logs | they proved the issue but add hot-path noise after validation.
Confidence: medium
Scope-risk: moderate
Directive: Do not reintroduce tensor-derived owner counting on CP HiCache backed nodes without measuring scheduler CPU/GPU sync cost.
Tested: python -m py_compile python/sglang/srt/mem_cache/hiradix_cache.py python/sglang/srt/entrypoints/openai/serving_base.py python/sglang/srt/entrypoints/openai/serving_chat.py python/sglang/srt/entrypoints/openai/serving_completions.py test/registered/unit/mem_cache/test_cp_hicache_load_back_owner_lanes.py
Tested: git diff --check
Not-tested: Local pytest collection is blocked by missing starlette dependency.
Not-tested: Full ETE after log cleanup; previous pre-cleanup ETE replay reached 136098.82 prompt tok/s without killing prefill.
This commit is contained in:
laoyao0822
2026-06-09 06:55:39 +08:00
parent 50fde834ae
commit 81eb138a26
6 changed files with 144 additions and 11 deletions
+1
View File
@@ -283,3 +283,4 @@ tai-kernel/
# Local CP shared-KV/GSM8K debug ledger # Local CP shared-KV/GSM8K debug ledger
docs/advanced_features/nsa_prefill_cp_gsm8k_cachehit_temp_findings.md docs/advanced_features/nsa_prefill_cp_gsm8k_cachehit_temp_findings.md
docs/advanced_features/*_temp*.md
@@ -4,6 +4,7 @@ import json
import logging import logging
import uuid import uuid
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from http import HTTPStatus
from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Union from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Union
import orjson import orjson
@@ -37,6 +38,23 @@ class OpenAIServingBase(ABC):
else None else None
) )
@staticmethod
def _streaming_http_error_type_and_status(status_code) -> tuple[str, int]:
if isinstance(status_code, HTTPStatus):
return status_code.name, status_code.value
try:
status = HTTPStatus(int(status_code))
return status.name, status.value
except Exception:
logger.warning(
"Invalid streaming abort status_code=%r; using INTERNAL_SERVER_ERROR",
status_code,
)
return (
HTTPStatus.INTERNAL_SERVER_ERROR.name,
HTTPStatus.INTERNAL_SERVER_ERROR.value,
)
def _parse_model_parameter(self, model: str) -> Tuple[str, Optional[str]]: def _parse_model_parameter(self, model: str) -> Tuple[str, Optional[str]]:
"""Parse 'base-model:adapter-name' syntax to extract LoRA adapter. """Parse 'base-model:adapter-name' syntax to extract LoRA adapter.
@@ -813,10 +813,13 @@ class OpenAIServingChat(OpenAIServingBase):
code = finish_reason.get( code = finish_reason.get(
"status_code", HTTPStatus.INTERNAL_SERVER_ERROR "status_code", HTTPStatus.INTERNAL_SERVER_ERROR
) )
err_type, status_code = (
self._streaming_http_error_type_and_status(code)
)
error = self.create_streaming_error_response( error = self.create_streaming_error_response(
finish_reason.get("message", "Generation aborted."), finish_reason.get("message", "Generation aborted."),
code.name, err_type,
code.value, status_code,
) )
yield f"data: {error}\n\n" yield f"data: {error}\n\n"
break break
@@ -274,10 +274,13 @@ class OpenAIServingCompletion(OpenAIServingBase):
code = finish_reason.get( code = finish_reason.get(
"status_code", HTTPStatus.INTERNAL_SERVER_ERROR "status_code", HTTPStatus.INTERNAL_SERVER_ERROR
) )
err_type, status_code = self._streaming_http_error_type_and_status(
code
)
error = self.create_streaming_error_response( error = self.create_streaming_error_response(
finish_reason.get("message", "Generation aborted."), finish_reason.get("message", "Generation aborted."),
code.name, err_type,
code.value, status_code,
) )
yield f"data: {error}\n\n" yield f"data: {error}\n\n"
break break
+96 -7
View File
@@ -8,7 +8,7 @@ import math
import os import os
import threading import threading
import time import time
from dataclasses import dataclass from dataclasses import dataclass, field
from queue import Empty from queue import Empty
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple from typing import TYPE_CHECKING, Dict, List, Optional, Tuple
@@ -177,6 +177,9 @@ class CpHiCacheNodeMetadata:
# page-aligned invariant by using logical_len as the physical length. # page-aligned invariant by using logical_len as the physical length.
padded_len: Optional[int] = None padded_len: Optional[int] = None
draft_host_indices: Optional[torch.Tensor] = None draft_host_indices: Optional[torch.Tensor] = None
_owner_page_counts_cache: Dict[int, Tuple[int, ...]] = field(
default_factory=dict, init=False, repr=False
)
def __post_init__(self): def __post_init__(self):
if self.logical_len < 0: if self.logical_len < 0:
@@ -245,6 +248,28 @@ class CpHiCacheNodeMetadata:
"owned_positions must be sorted and strictly increasing" "owned_positions must be sorted and strictly increasing"
) )
def owner_page_counts(self, cp_size: int) -> Tuple[int, ...]:
cp_size = int(cp_size)
if cp_size <= 0:
raise ValueError(f"cp_size must be positive, got {cp_size}")
cached = self._owner_page_counts_cache.get(cp_size)
if cached is not None:
return cached
counts = [0 for _ in range(cp_size)]
for owner in self.page_owners.tolist():
owner = int(owner)
if owner < 0:
continue
if owner >= cp_size:
raise RuntimeError(
"CP HiCache metadata owner exceeds CP size: "
f"owner={owner} cp_size={cp_size}"
)
counts[owner] += 1
result = tuple(counts)
self._owner_page_counts_cache[cp_size] = result
return result
@property @property
def valid_len(self) -> int: def valid_len(self) -> int:
return self.logical_len return self.logical_len
@@ -1112,6 +1137,10 @@ class HiRadixCache(RadixCache):
def _cp_load_back_node_owner_page_counts( def _cp_load_back_node_owner_page_counts(
self, node: TreeNode, cp_size: int self, node: TreeNode, cp_size: int
) -> Tuple[int, ...]: ) -> Tuple[int, ...]:
metadata = getattr(node, "cp_hicache", None)
if metadata is not None:
return metadata.owner_page_counts(cp_size)
value = getattr(node, "value", None) value = getattr(node, "value", None)
if value is None or int(value.numel()) == 0: if value is None or int(value.numel()) == 0:
return tuple(0 for _ in range(cp_size)) return tuple(0 for _ in range(cp_size))
@@ -1158,18 +1187,41 @@ class HiRadixCache(RadixCache):
def _plan_cp_load_back_owner_lane_evictions( def _plan_cp_load_back_owner_lane_evictions(
self, plan: CpLoadBackPlan self, plan: CpLoadBackPlan
) -> CpLoadBackEvictionPlan: ) -> CpLoadBackEvictionPlan:
plan_start_time = time.perf_counter()
last_progress_time = plan_start_time
deficits = [max(0, int(v)) for v in plan.deficit_by_owner] deficits = [max(0, int(v)) for v in plan.deficit_by_owner]
cp_size = len(deficits) cp_size = len(deficits)
planned_freed = [0 for _ in range(cp_size)] planned_freed = [0 for _ in range(cp_size)]
victims: List[TreeNode] = [] victims: List[TreeNode] = []
planned_evicted_nodes = set() planned_evicted_nodes = set()
candidate_nodes = set(getattr(self, "evictable_leaves", set())) candidate_nodes = set(getattr(self, "evictable_leaves", set()))
initial_candidate_count = len(candidate_nodes)
iteration = 0
while any(v > 0 for v in deficits): while any(v > 0 for v in deficits):
iteration += 1
best_node = None best_node = None
best_counts = None best_counts = None
best_score = None best_score = None
scanned_candidates = 0
for node in list(candidate_nodes): for node in list(candidate_nodes):
scanned_candidates += 1
if (scanned_candidates & 1023) == 0:
now = time.perf_counter()
if now - last_progress_time >= 5.0:
logger.warning(
"[HiCache-load] slow CP owner-lane eviction planning scan: "
"iteration=%d scanned=%d candidates=%d victims=%d "
"deficits=%s planned_freed=%s elapsed_ms=%.3f",
iteration,
scanned_candidates,
len(candidate_nodes),
len(victims),
deficits,
planned_freed,
(now - plan_start_time) * 1000.0,
)
last_progress_time = now
if node in planned_evicted_nodes: if node in planned_evicted_nodes:
continue continue
if not self._cp_device_node_is_load_back_victim_after_plan( if not self._cp_device_node_is_load_back_victim_after_plan(
@@ -1223,6 +1275,22 @@ class HiRadixCache(RadixCache):
break break
ancestor = getattr(ancestor, "parent", None) ancestor = getattr(ancestor, "parent", None)
plan_elapsed_ms = (time.perf_counter() - plan_start_time) * 1000.0
if plan_elapsed_ms >= 1000.0 or any(v > 0 for v in deficits):
logger.warning(
"[HiCache-load] CP owner-lane eviction planning done: "
"elapsed_ms=%.3f initial_candidates=%d remaining_candidates=%d "
"iterations=%d victims=%d original_deficit_by_owner=%s "
"planned_freed_by_owner=%s remaining_deficit_by_owner=%s",
plan_elapsed_ms,
initial_candidate_count,
len(candidate_nodes),
iteration,
len(victims),
plan.deficit_by_owner,
planned_freed,
deficits,
)
return CpLoadBackEvictionPlan( return CpLoadBackEvictionPlan(
victims=tuple(victims), victims=tuple(victims),
planned_freed_by_owner=tuple(planned_freed), planned_freed_by_owner=tuple(planned_freed),
@@ -1232,7 +1300,20 @@ class HiRadixCache(RadixCache):
def _evict_cp_load_back_owner_lanes( def _evict_cp_load_back_owner_lanes(
self, plan: CpLoadBackPlan, *, node_id: int self, plan: CpLoadBackPlan, *, node_id: int
) -> CpLoadBackPlan: ) -> CpLoadBackPlan:
evict_start_time = time.perf_counter()
eviction_plan = self._plan_cp_load_back_owner_lane_evictions(plan) eviction_plan = self._plan_cp_load_back_owner_lane_evictions(plan)
plan_elapsed_ms = (time.perf_counter() - evict_start_time) * 1000.0
if plan_elapsed_ms >= 1000.0:
logger.warning(
"[HiCache-load] CP owner-lane eviction plan ready: node_id=%d "
"elapsed_ms=%.3f victims=%d planned_freed_by_owner=%s "
"remaining_deficit_by_owner=%s",
node_id,
plan_elapsed_ms,
len(eviction_plan.victims),
eviction_plan.planned_freed_by_owner,
eviction_plan.remaining_deficit_by_owner,
)
if any(v > 0 for v in eviction_plan.remaining_deficit_by_owner): if any(v > 0 for v in eviction_plan.remaining_deficit_by_owner):
logger.warning( logger.warning(
"[CP_HICACHE_FALLBACK][cp_load_back_owner_lane_eviction_insufficient] " "[CP_HICACHE_FALLBACK][cp_load_back_owner_lane_eviction_insufficient] "
@@ -1300,13 +1381,21 @@ class HiRadixCache(RadixCache):
num_evicted += self._evict_backuped(victim) num_evicted += self._evict_backuped(victim)
refreshed = self._refresh_cp_load_back_plan(plan) refreshed = self._refresh_cp_load_back_plan(plan)
logger.debug( evict_elapsed_ms = (time.perf_counter() - evict_start_time) * 1000.0
"[HiCache-load] owner-lane device eviction before CP load-back: " log_fn = (
"node_id=%d victims=%s num_evicted=%d required_by_owner=%s " logger.warning
"before_available_by_owner=%s before_deficit_by_owner=%s " if evict_elapsed_ms >= 1000.0
"after_available_by_owner=%s after_deficit_by_owner=%s " or any(v > 0 for v in refreshed.deficit_by_owner)
"planned_freed_by_owner=%s", else logger.debug
)
log_fn(
"[HiCache-load] owner-lane device eviction before CP load-back done: "
"node_id=%d elapsed_ms=%.3f victims=%s num_evicted=%d "
"required_by_owner=%s before_available_by_owner=%s "
"before_deficit_by_owner=%s after_available_by_owner=%s "
"after_deficit_by_owner=%s planned_freed_by_owner=%s",
node_id, node_id,
evict_elapsed_ms,
[getattr(node, "id", None) for node in eviction_plan.victims], [getattr(node, "id", None) for node in eviction_plan.victims],
num_evicted, num_evicted,
plan.required_by_owner, plan.required_by_owner,
@@ -319,6 +319,25 @@ class TestCpHiCacheLoadBackOwnerLanes(CustomTestCase):
self.assertEqual(counts, (1, 1, 0, 0)) self.assertEqual(counts, (1, 1, 0, 0))
def test_device_victim_owner_counts_use_cp_metadata_without_tensor_padding(self):
allocator = _make_allocator(page_size=4, cp_size=4)
cache = _make_cache(allocator)
node = _make_node(14, 140, [0, 0, 2], value=torch.arange(4, 16, dtype=torch.int64))
import sglang.srt.mem_cache.hiradix_cache as hiradix_cache
old_pad = hiradix_cache.pad_token_locs_to_page_boundary
try:
hiradix_cache.pad_token_locs_to_page_boundary = lambda *args, **kwargs: (_ for _ in ()).throw(
AssertionError("metadata-backed counts must not touch tensor padding")
)
counts = cache._cp_load_back_node_owner_page_counts(node, cp_size=4)
finally:
hiradix_cache.pad_token_locs_to_page_boundary = old_pad
self.assertEqual(counts, (2, 0, 1, 0))
self.assertIs(counts, node.cp_hicache.owner_page_counts(4))
def test_load_back_plan_fails_closed_without_cp_metadata(self): def test_load_back_plan_fails_closed_without_cp_metadata(self):
allocator = _make_allocator() allocator = _make_allocator()
cache = _make_cache(allocator) cache = _make_cache(allocator)