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
262 lines
9.3 KiB
Python
262 lines
9.3 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
from dataclasses import dataclass
|
|
from typing import Optional
|
|
|
|
|
|
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
class KVCacheBuildResult:
|
|
is_hybrid_swa: bool
|
|
is_hybrid_ssm: bool
|
|
sliding_window_size: Optional[int]
|
|
full_tokens_per_layer: Optional[int]
|
|
swa_tokens_per_layer: Optional[int]
|
|
req_to_token_pool: object
|
|
token_to_kv_pool_allocator: object
|
|
disable_radix_cache: bool
|
|
tree_cache: object
|
|
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
from sglang.srt.configs.model_config import ModelImpl
|
|
from sglang.srt.environ import envs
|
|
from sglang.srt.managers.mm_utils import init_mm_embedding_cache
|
|
from sglang.srt.mem_cache.cache_init_params import CacheInitParams
|
|
from sglang.srt.mem_cache.registry import TreeCacheBuildContext, create_tree_cache
|
|
from sglang.srt.model_loader.utils import get_resolved_model_impl
|
|
from sglang.srt.runtime_context import get_parallel
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from torch.distributed import ProcessGroup
|
|
|
|
from sglang.srt.configs.model_config import ModelConfig
|
|
from sglang.srt.distributed.parallel_state import GroupCoordinator
|
|
from sglang.srt.distributed.parallel_state_wrapper import ParallelState
|
|
from sglang.srt.managers.tp_worker import BaseTpWorker
|
|
from sglang.srt.mem_cache.base_prefix_cache import BasePrefixCache
|
|
from sglang.srt.server_args import ServerArgs
|
|
from sglang.srt.speculative.spec_info import SpeculativeAlgorithm
|
|
|
|
|
|
def get_draft_kv_pool(
|
|
*,
|
|
draft_worker: BaseTpWorker,
|
|
spec_algorithm: SpeculativeAlgorithm,
|
|
server_args: ServerArgs,
|
|
):
|
|
"""Return the draft token-to-KV pool for the current draft worker,
|
|
or None when no draft KV pool is available."""
|
|
if draft_worker is None or spec_algorithm.is_ngram():
|
|
return None
|
|
|
|
# V2 workers nest the draft runner under `.draft_worker`.
|
|
if server_args.enable_multi_layer_eagle:
|
|
draft_runner = draft_worker.draft_worker.draft_runner_list[0]
|
|
else:
|
|
draft_runner = draft_worker.draft_worker.draft_runner
|
|
return draft_runner.token_to_kv_pool
|
|
|
|
|
|
def maybe_register_hicache_draft(
|
|
*,
|
|
tree_cache: BasePrefixCache,
|
|
draft_worker: BaseTpWorker,
|
|
spec_algorithm: SpeculativeAlgorithm,
|
|
server_args: ServerArgs,
|
|
enable_hierarchical_cache: bool,
|
|
page_size: int,
|
|
) -> None:
|
|
"""Register draft KV pool with HiCacheController for piggyback L2/L3 ops."""
|
|
if not enable_hierarchical_cache:
|
|
return
|
|
|
|
draft_kv_pool = get_draft_kv_pool(
|
|
draft_worker=draft_worker,
|
|
spec_algorithm=spec_algorithm,
|
|
server_args=server_args,
|
|
)
|
|
if draft_kv_pool is None:
|
|
return
|
|
|
|
from sglang.srt.mem_cache.memory_pool import (
|
|
HybridLinearKVPool,
|
|
MHATokenToKVPool,
|
|
MLATokenToKVPool,
|
|
)
|
|
from sglang.srt.mem_cache.memory_pool_host import MLATokenToKVPoolHost
|
|
from sglang.srt.mem_cache.pool_host.mha import get_mha_host_pool_cls
|
|
|
|
pool = draft_kv_pool
|
|
if isinstance(pool, HybridLinearKVPool):
|
|
pool = pool.full_kv_pool
|
|
|
|
# Create host pool for draft with the same slot count as the target host pool,
|
|
# so that host indices stay 1-to-1 between target and draft KV caches.
|
|
primary = tree_cache.cache_controller.mem_pool_host
|
|
kw = dict(
|
|
host_to_device_ratio=primary.size / pool.size,
|
|
host_size=0,
|
|
page_size=page_size,
|
|
layout=server_args.hicache_mem_layout,
|
|
allocator_type=server_args.hicache_storage_backend,
|
|
)
|
|
if isinstance(pool, MHATokenToKVPool):
|
|
draft_host_pool = get_mha_host_pool_cls(pool)(pool, **kw)
|
|
elif isinstance(pool, MLATokenToKVPool):
|
|
draft_host_pool = MLATokenToKVPoolHost(pool, **kw)
|
|
else:
|
|
logger.warning(
|
|
"Draft pool type %s not supported for HiCache, skipping.",
|
|
type(pool).__name__,
|
|
)
|
|
return
|
|
|
|
tree_cache.cache_controller.set_draft_kv_pool(pool, draft_host_pool)
|
|
|
|
|
|
def build_kv_cache(
|
|
*,
|
|
server_args: ServerArgs,
|
|
model_config: ModelConfig,
|
|
tp_worker: BaseTpWorker,
|
|
page_size: int,
|
|
spec_algorithm: SpeculativeAlgorithm,
|
|
attn_tp_cpu_group: ProcessGroup,
|
|
tp_cpu_group: ProcessGroup,
|
|
attn_cp_cpu_group: ProcessGroup,
|
|
enable_metrics: bool,
|
|
enable_kv_cache_events: bool,
|
|
ps: ParallelState,
|
|
tp_group: GroupCoordinator,
|
|
pp_group: GroupCoordinator,
|
|
enable_hierarchical_cache: bool,
|
|
) -> KVCacheBuildResult:
|
|
sliding_window_size: Optional[int] = None
|
|
full_tokens_per_layer: Optional[int] = None
|
|
swa_tokens_per_layer: Optional[int] = None
|
|
uses_transformers_backend = (
|
|
get_resolved_model_impl(model_config) == ModelImpl.TRANSFORMERS
|
|
)
|
|
|
|
# Hybrid memory pool
|
|
is_hybrid_swa = tp_worker.is_hybrid_swa
|
|
_spec = tp_worker.model_runner.linear_attn_model_spec
|
|
_registry_needs_mamba = _spec.uses_mamba_radix_cache if _spec is not None else False
|
|
is_hybrid_ssm = (
|
|
tp_worker.model_runner.hybrid_gdn_config is not None
|
|
or tp_worker.model_runner.mamba2_config is not None
|
|
or _registry_needs_mamba
|
|
or tp_worker.model_runner.kimi_linear_config is not None
|
|
or tp_worker.model_runner.hybrid_lightning_config is not None
|
|
)
|
|
|
|
sliding_window_size = None
|
|
if is_hybrid_swa:
|
|
sliding_window_size = tp_worker.sliding_window_size
|
|
full_tokens_per_layer, swa_tokens_per_layer = (
|
|
tp_worker.get_tokens_per_layer_info()
|
|
)
|
|
|
|
req_to_token_pool, token_to_kv_pool_allocator = tp_worker.get_memory_pool()
|
|
|
|
disable_radix_cache = server_args.disable_radix_cache or (
|
|
model_config.is_multimodal and uses_transformers_backend
|
|
)
|
|
if disable_radix_cache and not server_args.disable_radix_cache:
|
|
logger.warning(
|
|
"Radix cache is disabled for multimodal models with the "
|
|
"Transformers backend to avoid multimodal prefix-cache mismatches."
|
|
)
|
|
|
|
# Decode radix cache is unsupported with hybrid SWA/SSM models —
|
|
# these use specialized memory pools incompatible with the
|
|
# prefix-match-and-lock allocation path.
|
|
if (
|
|
server_args.disaggregation_decode_enable_radix_cache
|
|
and server_args.disaggregation_mode == "decode"
|
|
):
|
|
if is_hybrid_swa:
|
|
raise ValueError(
|
|
"--disaggregation-decode-enable-radix-cache is incompatible "
|
|
"with sliding window attention (SWA) models"
|
|
)
|
|
if is_hybrid_ssm:
|
|
raise ValueError(
|
|
"--disaggregation-decode-enable-radix-cache is incompatible "
|
|
"with Mamba/SSM models"
|
|
)
|
|
|
|
effective_chunked_prefill_size = server_args.chunked_prefill_size
|
|
if model_config.is_multimodal and uses_transformers_backend:
|
|
effective_chunked_prefill_size = None
|
|
|
|
params = CacheInitParams(
|
|
disable=disable_radix_cache,
|
|
req_to_token_pool=req_to_token_pool,
|
|
token_to_kv_pool_allocator=token_to_kv_pool_allocator,
|
|
# When dcp enabled, kv_pool_allocator.page_size is page_size * dcp_size.
|
|
# TreeCache.page_size should keep the same as allocator.page_size to
|
|
# avoid kv page eviction conflicts.
|
|
page_size=(
|
|
page_size
|
|
if not get_parallel().dcp_enabled
|
|
else token_to_kv_pool_allocator.page_size
|
|
),
|
|
is_eagle=spec_algorithm.is_eagle(),
|
|
tp_cache_group=(
|
|
attn_tp_cpu_group if server_args.enable_dp_attention else tp_cpu_group
|
|
),
|
|
attn_cp_cache_group=attn_cp_cpu_group,
|
|
attn_tp_cache_group=attn_tp_cpu_group,
|
|
pp_cache_group=pp_group.cpu_group,
|
|
eviction_policy=server_args.radix_eviction_policy,
|
|
enable_metrics=enable_metrics,
|
|
enable_kv_cache_events=enable_kv_cache_events,
|
|
enable_session_radix_cache=server_args.enable_session_radix_cache,
|
|
enable_mamba_extra_buffer=server_args.enable_mamba_extra_buffer(),
|
|
enable_mamba_extra_buffer_lazy=server_args.enable_mamba_extra_buffer_lazy(),
|
|
pp_rank=ps.pp_rank,
|
|
pp_size=ps.pp_size,
|
|
chunked_prefill_size=effective_chunked_prefill_size,
|
|
sliding_window_size=sliding_window_size,
|
|
)
|
|
|
|
tree_cache = create_tree_cache(
|
|
TreeCacheBuildContext(
|
|
server_args=server_args,
|
|
params=params,
|
|
is_hybrid_swa=is_hybrid_swa,
|
|
full_tokens_per_layer=full_tokens_per_layer,
|
|
is_hybrid_ssm=is_hybrid_ssm,
|
|
enable_hierarchical_cache=enable_hierarchical_cache,
|
|
disable_radix_cache=disable_radix_cache,
|
|
effective_chunked_prefill_size=effective_chunked_prefill_size,
|
|
tp_worker=tp_worker,
|
|
model_config=model_config,
|
|
tp_size=ps.tp_size,
|
|
tp_rank=ps.tp_rank,
|
|
tp_group=tp_group,
|
|
)
|
|
)
|
|
|
|
embedding_cache_size = envs.SGLANG_VLM_CACHE_SIZE_MB.get()
|
|
init_mm_embedding_cache(embedding_cache_size * 1024 * 1024)
|
|
|
|
return KVCacheBuildResult(
|
|
is_hybrid_swa=is_hybrid_swa,
|
|
is_hybrid_ssm=is_hybrid_ssm,
|
|
sliding_window_size=sliding_window_size,
|
|
full_tokens_per_layer=full_tokens_per_layer,
|
|
swa_tokens_per_layer=swa_tokens_per_layer,
|
|
req_to_token_pool=req_to_token_pool,
|
|
token_to_kv_pool_allocator=token_to_kv_pool_allocator,
|
|
disable_radix_cache=disable_radix_cache,
|
|
tree_cache=tree_cache,
|
|
)
|