94057c3d3e
PR Test (NPU) / check-changes (push) Has been cancelled
PR Test (NPU) / pr-gate (push) Has been cancelled
PR Test (NPU) / set-image-config (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-4-npu-a3 (push) Has been cancelled
PR Test (NPU) / stage-b-test-16-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-1-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-2-npu-a3 (push) Has been cancelled
PR Test (Arm64) / pr-gate (push) Has been cancelled
PR Test (Arm64) / check-changes (push) Has been cancelled
PR Test (Arm64) / build-test (push) Has been cancelled
PR Test (sgl-router) / gate (push) Has been cancelled
PR Test (sgl-router) / tier-1 — lint (push) Has been cancelled
PR Test (sgl-router) / tier-2 — build + test (push) Has been cancelled
PR Test (sgl-router) / tier-3 — docker (placeholder) (push) Has been cancelled
PR Test (sgl-router) / tier-3 — k8s integration (push) Has been cancelled
PR Test (sgl-router) / tier-3 — e2e (push) Has been cancelled
PR Test (sgl-router) / finish (push) Has been cancelled
PR Test (NPU) / single-node-poc (map[name:qwen3_6_27b_w8a8_1p_in64k_out1k_50ms runner:linux-aarch64-a3-2 test_case:test/registered/ascend/performance/qwen3_6_27b/test_npu_qwen3_6_27b_w8a8_1p_in64k_out1k_50ms.py test_type:perf]) (push) Has been cancelled
PR Test (NPU) / pr-test-npu-finish (push) Has been cancelled
PR Test (Xeon) / pr-gate (push) Has been cancelled
PR Test (Xeon) / check-changes (push) Has been cancelled
PR Test (Xeon) / build-test (, xeon-gnr, base-b-test-cpu) (push) Has been cancelled
PR Test (XPU) / check-changes (push) Has been cancelled
PR Test (XPU) / pr-gate (push) Has been cancelled
PR Test (XPU) / stage-a-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / wait-for-stage-a (push) Has been cancelled
PR Test (XPU) / stage-b-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / finish (push) Has been cancelled
CI Model Inventory / build-inventory (push) Has been cancelled
Lint / lint (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Compilation Check (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Manual Policy (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Request Processing (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Summary (push) Has been cancelled
PR Test (SMG) / build-wheel (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on windows (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (x86_64 - auto) (push) Has been cancelled
PR Test (SMG) / python-unit-tests (push) Has been cancelled
PR Test (SMG) / unit-tests (push) Has been cancelled
PR Test (SMG) / benchmarks (push) Has been cancelled
PR Test (SMG) / chat-completions (push) Has been cancelled
PR Test (SMG) / chat-completions-4gpu (push) Has been cancelled
PR Test (SMG) / e2e (push) Has been cancelled
PR Test (SMG) / docker-build-test (push) Has been cancelled
PR Test (SMG) / k8s-integration (push) Has been cancelled
PR Test (SMG) / finish (push) Has been cancelled
PR Test (SMG) / summarize-benchmarks (push) Has been cancelled
Release SGLang Model Gateway Docker Image / publish (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Build SDist (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Upload to PyPI (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (aarch64, 12.9, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (x86_64, 12.9, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu129 (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (aarch64, 13.0, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (x86_64, 13.0, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu130 (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 700) (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 720) (push) Has been cancelled
Release SGLang Kernels / release-rocm700 (push) Has been cancelled
Release SGLang Kernels / release-rocm720 (push) Has been cancelled
Release SGLang Kernels / build-musa43 (43, 3.10) (push) Has been cancelled
Release SGLang Kernels / release-musa43 (push) Has been cancelled
170 lines
5.7 KiB
Python
170 lines
5.7 KiB
Python
from __future__ import annotations
|
|
|
|
"""Cache for chunked prefill, used when RadixCache is disabled."""
|
|
|
|
import logging
|
|
from typing import TYPE_CHECKING, Any, Optional
|
|
|
|
import torch
|
|
|
|
from sglang.srt.mem_cache.allocator.hisparse import (
|
|
DeepSeekV4HiSparseTokenToKVPoolAllocator,
|
|
)
|
|
from sglang.srt.mem_cache.allocator.swa import SWATokenToKVPoolAllocator
|
|
from sglang.srt.mem_cache.base_prefix_cache import (
|
|
BasePrefixCache,
|
|
DecLockRefParams,
|
|
DecLockRefResult,
|
|
EvictParams,
|
|
EvictResult,
|
|
IncLockRefResult,
|
|
InsertParams,
|
|
InsertResult,
|
|
MatchPrefixParams,
|
|
MatchResult,
|
|
)
|
|
|
|
if TYPE_CHECKING:
|
|
from sglang.srt.managers.schedule_batch import Req
|
|
from sglang.srt.mem_cache.cache_init_params import CacheInitParams
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class ChunkCache(BasePrefixCache):
|
|
"""
|
|
ChunkCache is used when radix cache is disabled.
|
|
|
|
That includes standard chunked-prefill setups and the decode side of P/D
|
|
disaggregation when decode radix cache is not enabled.
|
|
"""
|
|
|
|
def __init__(self, params: CacheInitParams):
|
|
self.req_to_token_pool = params.req_to_token_pool
|
|
self.token_to_kv_pool_allocator = params.token_to_kv_pool_allocator
|
|
self.page_size = params.page_size
|
|
if self.token_to_kv_pool_allocator:
|
|
self.device = self.token_to_kv_pool_allocator.device
|
|
else:
|
|
self.device = torch.device("cpu")
|
|
|
|
self.protected_size_ = 0
|
|
|
|
def is_chunk_cache(self) -> bool:
|
|
return True
|
|
|
|
# NOTE (csy): this is to determine if a cache has prefix matching feature.
|
|
# Chunk cache always return True to indicate no prefix matching.
|
|
# TODO (csy): Using a prefix cache trait to replace this
|
|
@property
|
|
def disable(self):
|
|
return True
|
|
|
|
def reset(self):
|
|
pass
|
|
|
|
def match_prefix(self, params: MatchPrefixParams) -> MatchResult:
|
|
return MatchResult(
|
|
device_indices=torch.empty((0,), dtype=torch.int64),
|
|
last_device_node=None,
|
|
last_host_node=None,
|
|
best_match_node=None,
|
|
)
|
|
|
|
def insert(self, params: InsertParams) -> InsertResult:
|
|
# ChunkCache does not support prefix caching, so insert is a no-op
|
|
return InsertResult(prefix_len=0)
|
|
|
|
def cache_finished_req(self, req: Req, is_insert: bool = True):
|
|
kv_committed_len = req.pop_committed_kv_cache()
|
|
# For decode server: if req.output_ids is empty, we want to free all req.origin_input_ids
|
|
kv_indices = self.req_to_token_pool.req_to_token[
|
|
req.req_pool_idx, :kv_committed_len
|
|
]
|
|
self.token_to_kv_pool_allocator.free(kv_indices)
|
|
|
|
def cache_unfinished_req(self, req: Req, chunked=False):
|
|
kv_indices = self.req_to_token_pool.req_to_token[
|
|
req.req_pool_idx, : req.extend_range.end
|
|
]
|
|
# `req.prefix_indices` will be used in `PrefillAdder::add_chunked_req` later
|
|
req.prefix_indices = kv_indices.to(dtype=torch.int64, copy=True)
|
|
|
|
def evict(self, params: EvictParams) -> EvictResult:
|
|
return EvictResult()
|
|
|
|
def inc_lock_ref(self, node: Any) -> IncLockRefResult:
|
|
return IncLockRefResult(delta=0)
|
|
|
|
def dec_lock_ref(
|
|
self, node: Any, params: Optional[DecLockRefParams] = None
|
|
) -> DecLockRefResult:
|
|
return DecLockRefResult(delta=0)
|
|
|
|
def protected_size(self):
|
|
# NOTE: no protected size in chunk cache. Chunk cache's eviction is the same with request's lifecycle.
|
|
return 0
|
|
|
|
def pretty_print(self):
|
|
return ""
|
|
|
|
|
|
class SWAChunkCache(ChunkCache):
|
|
"""ChunkCache with support for sliding window attention."""
|
|
|
|
def __init__(self, params: CacheInitParams):
|
|
# DeepSeek V4 HiSparse wraps SWATokenToKVPoolAllocator and exposes the same API.
|
|
assert isinstance(
|
|
params.token_to_kv_pool_allocator,
|
|
(
|
|
SWATokenToKVPoolAllocator,
|
|
DeepSeekV4HiSparseTokenToKVPoolAllocator,
|
|
),
|
|
)
|
|
super().__init__(params)
|
|
|
|
self.sliding_window_size = params.sliding_window_size
|
|
self.chunked_prefill_size = params.chunked_prefill_size
|
|
|
|
def supports_swa(self) -> bool:
|
|
assert (
|
|
self.sliding_window_size is not None
|
|
), "sliding_window_size must be set for SWAChunkCache"
|
|
return True
|
|
|
|
def evict(self, params: EvictParams) -> EvictResult:
|
|
return EvictResult()
|
|
|
|
|
|
class PureSWAChunkCache(SWAChunkCache):
|
|
"""ChunkCache for all-SWA models (no full attention layers).
|
|
|
|
For hybrid models, full_to_swa_index_mapping prevents SWA double-free.
|
|
All-SWA models lack this mapping, so on request completion we must
|
|
explicitly skip the range already freed by ``free_swa_out_of_window_slots``
|
|
(a.k.a. _evict_swa) during decode.
|
|
|
|
``req.swa_evict_floor`` only protects the prompt/image KV while the request
|
|
is active. ChunkCache does not retain finished prefixes, so the protected
|
|
prefix is released here when the request finishes.
|
|
"""
|
|
|
|
def cache_finished_req(self, req: Req, is_insert: bool = True):
|
|
kv_committed_len = req.pop_committed_kv_cache()
|
|
kv_indices = self.req_to_token_pool.req_to_token[
|
|
req.req_pool_idx, :kv_committed_len
|
|
]
|
|
evict_floor = req.swa_evict_floor
|
|
evicted_seqlen = req.swa_evicted_seqlen
|
|
if evicted_seqlen > evict_floor:
|
|
parts = []
|
|
if evict_floor > 0:
|
|
parts.append(kv_indices[:evict_floor])
|
|
if evicted_seqlen < kv_committed_len:
|
|
parts.append(kv_indices[evicted_seqlen:kv_committed_len])
|
|
if parts:
|
|
self.token_to_kv_pool_allocator.free(torch.cat(parts))
|
|
else:
|
|
self.token_to_kv_pool_allocator.free(kv_indices)
|