Type hicache transfer hook kwargs in unified cache (#27071)

This commit is contained in:
Ke Bao
2026-06-03 18:57:25 +08:00
committed by GitHub
parent d7013b6537
commit 44d4a25a07
4 changed files with 67 additions and 19 deletions
@@ -1,7 +1,7 @@
from __future__ import annotations from __future__ import annotations
import heapq import heapq
from typing import TYPE_CHECKING, Callable, Optional from typing import TYPE_CHECKING, Callable, Optional, Sequence
import torch import torch
@@ -9,10 +9,15 @@ from sglang.srt.mem_cache.base_prefix_cache import (
DecLockRefParams, DecLockRefParams,
EvictParams, EvictParams,
IncLockRefResult, IncLockRefResult,
InsertResult,
MatchPrefixParams, MatchPrefixParams,
MatchResult, MatchResult,
) )
from sglang.srt.mem_cache.hicache_storage import PoolName, PoolTransfer from sglang.srt.mem_cache.hicache_storage import (
PoolName,
PoolTransfer,
PoolTransferResult,
)
from sglang.srt.mem_cache.unified_cache_components.tree_component import ( from sglang.srt.mem_cache.unified_cache_components.tree_component import (
CacheTransferPhase, CacheTransferPhase,
ComponentType, ComponentType,
@@ -21,6 +26,7 @@ from sglang.srt.mem_cache.unified_cache_components.tree_component import (
) )
if TYPE_CHECKING: if TYPE_CHECKING:
from sglang.srt.managers.schedule_batch import Req
from sglang.srt.mem_cache.unified_radix_cache import ( from sglang.srt.mem_cache.unified_radix_cache import (
UnifiedTreeNode, UnifiedTreeNode,
) )
@@ -246,7 +252,14 @@ class FullComponent(TreeComponent):
# ---- HiCache Hooks ---- # ---- HiCache Hooks ----
def build_hicache_transfers( def build_hicache_transfers(
self, node: UnifiedTreeNode, phase: CacheTransferPhase, **kw self,
node: UnifiedTreeNode,
phase: CacheTransferPhase,
*,
req: Optional[Req] = None,
token_ids: Optional[Sequence[int]] = None,
prefetch_tokens: int = 0,
last_hash: Optional[str] = None,
) -> Optional[list[PoolTransfer]]: ) -> Optional[list[PoolTransfer]]:
ct = self.component_type ct = self.component_type
@@ -290,7 +303,9 @@ class FullComponent(TreeComponent):
node: UnifiedTreeNode, node: UnifiedTreeNode,
phase: CacheTransferPhase, phase: CacheTransferPhase,
transfers: list[PoolTransfer] = (), transfers: list[PoolTransfer] = (),
**kw, *,
insert_result: Optional[InsertResult] = None,
pool_storage_result: Optional[PoolTransferResult] = None,
) -> None: ) -> None:
ct = self.component_type ct = self.component_type
@@ -1,6 +1,6 @@
from __future__ import annotations from __future__ import annotations
from typing import TYPE_CHECKING, Callable, Optional from typing import TYPE_CHECKING, Callable, Optional, Sequence
import torch import torch
@@ -13,7 +13,12 @@ from sglang.srt.mem_cache.base_prefix_cache import (
MatchPrefixParams, MatchPrefixParams,
MatchResult, MatchResult,
) )
from sglang.srt.mem_cache.hicache_storage import PoolHitPolicy, PoolName, PoolTransfer from sglang.srt.mem_cache.hicache_storage import (
PoolHitPolicy,
PoolName,
PoolTransfer,
PoolTransferResult,
)
from sglang.srt.mem_cache.unified_cache_components.tree_component import ( from sglang.srt.mem_cache.unified_cache_components.tree_component import (
CacheTransferPhase, CacheTransferPhase,
ComponentType, ComponentType,
@@ -369,7 +374,14 @@ class MambaComponent(TreeComponent):
# ---- HiCache Hooks ---- # ---- HiCache Hooks ----
def build_hicache_transfers( def build_hicache_transfers(
self, node: UnifiedTreeNode, phase: CacheTransferPhase, **kw self,
node: UnifiedTreeNode,
phase: CacheTransferPhase,
*,
req: Optional[Req] = None,
token_ids: Optional[Sequence[int]] = None,
prefetch_tokens: int = 0,
last_hash: Optional[str] = None,
) -> Optional[list[PoolTransfer]]: ) -> Optional[list[PoolTransfer]]:
ct = self.component_type ct = self.component_type
@@ -385,7 +397,6 @@ class MambaComponent(TreeComponent):
] ]
if phase == CacheTransferPhase.LOAD_BACK: if phase == CacheTransferPhase.LOAD_BACK:
req = kw.get("req")
transfers: list[PoolTransfer] = [] transfers: list[PoolTransfer] = []
cd = node.component_data[ct] cd = node.component_data[ct]
@@ -458,7 +469,9 @@ class MambaComponent(TreeComponent):
node: UnifiedTreeNode, node: UnifiedTreeNode,
phase: CacheTransferPhase, phase: CacheTransferPhase,
transfers: list[PoolTransfer] = (), transfers: list[PoolTransfer] = (),
**kw, *,
insert_result: Optional[InsertResult] = None,
pool_storage_result: Optional[PoolTransferResult] = None,
) -> None: ) -> None:
ct = self.component_type ct = self.component_type
@@ -488,8 +501,6 @@ class MambaComponent(TreeComponent):
return return
transfer = transfers[0] transfer = transfers[0]
host_indices = transfer.host_indices host_indices = transfer.host_indices
insert_result = kw.get("insert_result")
pool_storage_result = kw.get("pool_storage_result")
loaded = ( loaded = (
pool_storage_result is not None pool_storage_result is not None
and pool_storage_result.extra_pool_hit_pages.get(PoolName.MAMBA, 0) >= 1 and pool_storage_result.extra_pool_hit_pages.get(PoolName.MAMBA, 0) >= 1
@@ -1,6 +1,6 @@
from __future__ import annotations from __future__ import annotations
from typing import TYPE_CHECKING, Callable, Optional from typing import TYPE_CHECKING, Callable, Optional, Sequence
import torch import torch
@@ -13,7 +13,11 @@ from sglang.srt.mem_cache.base_prefix_cache import (
MatchPrefixParams, MatchPrefixParams,
MatchResult, MatchResult,
) )
from sglang.srt.mem_cache.hicache_storage import PoolName, PoolTransfer from sglang.srt.mem_cache.hicache_storage import (
PoolName,
PoolTransfer,
PoolTransferResult,
)
from sglang.srt.mem_cache.unified_cache_components.tree_component import ( from sglang.srt.mem_cache.unified_cache_components.tree_component import (
BASE_COMPONENT_TYPE, BASE_COMPONENT_TYPE,
CacheTransferPhase, CacheTransferPhase,
@@ -483,7 +487,14 @@ class SWAComponent(TreeComponent):
# ---- HiCache Hooks ---- # ---- HiCache Hooks ----
def build_hicache_transfers( def build_hicache_transfers(
self, node: UnifiedTreeNode, phase: CacheTransferPhase, **kw self,
node: UnifiedTreeNode,
phase: CacheTransferPhase,
*,
req: Optional[Req] = None,
token_ids: Optional[Sequence[int]] = None,
prefetch_tokens: int = 0,
last_hash: Optional[str] = None,
) -> Optional[list[PoolTransfer]]: ) -> Optional[list[PoolTransfer]]:
ct = self.component_type ct = self.component_type
@@ -542,7 +553,9 @@ class SWAComponent(TreeComponent):
node: UnifiedTreeNode, node: UnifiedTreeNode,
phase: CacheTransferPhase, phase: CacheTransferPhase,
transfers: list[PoolTransfer] = (), transfers: list[PoolTransfer] = (),
**kw, *,
insert_result: Optional[InsertResult] = None,
pool_storage_result: Optional[PoolTransferResult] = None,
) -> None: ) -> None:
ct = self.component_type ct = self.component_type
@@ -3,7 +3,7 @@ from __future__ import annotations
import dataclasses import dataclasses
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from enum import Enum, IntFlag from enum import Enum, IntFlag
from typing import TYPE_CHECKING, Any, Callable, Optional from typing import TYPE_CHECKING, Any, Callable, Optional, Sequence
import torch import torch
from numpy import float64 from numpy import float64
@@ -17,7 +17,7 @@ from sglang.srt.mem_cache.base_prefix_cache import (
MatchPrefixParams, MatchPrefixParams,
MatchResult, MatchResult,
) )
from sglang.srt.mem_cache.hicache_storage import PoolTransfer from sglang.srt.mem_cache.hicache_storage import PoolTransfer, PoolTransferResult
if TYPE_CHECKING: if TYPE_CHECKING:
from sglang.srt.managers.schedule_batch import Req from sglang.srt.managers.schedule_batch import Req
@@ -383,7 +383,14 @@ class TreeComponent(ABC):
# ---- HiCache Hooks ---- # ---- HiCache Hooks ----
def build_hicache_transfers( def build_hicache_transfers(
self, node: UnifiedTreeNode, phase: CacheTransferPhase, **kw self,
node: UnifiedTreeNode,
phase: CacheTransferPhase,
*,
req: Optional[Req] = None,
token_ids: Optional[Sequence[int]] = None,
prefetch_tokens: int = 0,
last_hash: Optional[str] = None,
) -> Optional[list[PoolTransfer]]: ) -> Optional[list[PoolTransfer]]:
"""Build transfer descriptors for this component in the given phase. """Build transfer descriptors for this component in the given phase.
Returns None if the component has nothing to transfer.""" Returns None if the component has nothing to transfer."""
@@ -394,7 +401,9 @@ class TreeComponent(ABC):
node: UnifiedTreeNode, node: UnifiedTreeNode,
phase: CacheTransferPhase, phase: CacheTransferPhase,
transfers: list[PoolTransfer] = (), transfers: list[PoolTransfer] = (),
**kw, *,
insert_result: Optional[InsertResult] = None,
pool_storage_result: Optional[PoolTransferResult] = None,
) -> None: ) -> None:
"""Post-transfer bookkeeping: store host indices, update LRU, etc.""" """Post-transfer bookkeeping: store host indices, update LRU, etc."""
pass pass