From 5cc0d25a0d47be2242697238d460355a6b7d993f Mon Sep 17 00:00:00 2001 From: Liangsheng Yin Date: Fri, 31 Oct 2025 14:03:27 +0800 Subject: [PATCH] Add trait for `BasePrefixCache` (#12436) --- .../sglang/srt/mem_cache/base_prefix_cache.py | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/python/sglang/srt/mem_cache/base_prefix_cache.py b/python/sglang/srt/mem_cache/base_prefix_cache.py index fb85497c3..be6968e8b 100644 --- a/python/sglang/srt/mem_cache/base_prefix_cache.py +++ b/python/sglang/srt/mem_cache/base_prefix_cache.py @@ -1,12 +1,31 @@ +from __future__ import annotations + from abc import ABC, abstractmethod -from typing import TYPE_CHECKING, Any, NamedTuple, Optional, Tuple +from typing import ( + TYPE_CHECKING, + Any, + NamedTuple, + Optional, + Protocol, + Tuple, + runtime_checkable, +) import torch +from sglang.srt.mem_cache.allocator import BaseTokenToKVPoolAllocator +from sglang.srt.mem_cache.memory_pool import ReqToTokenPool + if TYPE_CHECKING: from sglang.srt.managers.schedule_batch import Req -else: - Req = Any # Placeholder for Req type when not type checking + + +@runtime_checkable +class PrefixCacheTrait(Protocol): + req_to_token_pool: ReqToTokenPool + token_to_kv_pool_allocator: BaseTokenToKVPoolAllocator + page_size: int + disable: bool class MatchResult(NamedTuple): @@ -28,7 +47,7 @@ class MatchResult(NamedTuple): host_hit_length: int = 0 -class BasePrefixCache(ABC): +class BasePrefixCache(ABC, PrefixCacheTrait): """Cache can be indexed by either rid or key.""" @abstractmethod