Files
wehub-resource-sync 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
chore: import upstream snapshot with attribution
2026-07-13 12:38:16 +08:00

292 lines
9.2 KiB
Python

from __future__ import annotations
from dataclasses import dataclass
from types import SimpleNamespace
from typing import List, Optional
import torch
from sglang.jit_kernel.kv_canary import consts
from sglang.jit_kernel.kv_canary.verify import CANARY_SLOT_BYTES, RealKvSource
from sglang.srt.kv_canary.buffer_group import CanaryBufferGroup, PoolKind
from sglang.srt.kv_canary.config import CanaryConfig, CanaryMode
from sglang.srt.kv_canary.pool_patcher.adapters.mha import attach_mha
from sglang.srt.kv_canary.pool_patcher.adapters.swa import attach_swa
from sglang.srt.kv_canary.pool_patcher.api import register_pool_attacher
from sglang.srt.mem_cache.radix_cache import RadixCache, TreeNode
from sglang.srt.model_executor.forward_batch_info import ForwardMode
DEFAULT_DEVICE: torch.device = torch.device("cuda")
@dataclass
class FakeMHAPool:
layer_num: int
k_buffer: List[torch.Tensor]
v_buffer: List[torch.Tensor]
page_size: int = 1
def get_contiguous_buf_infos(self):
ptrs = [b.data_ptr() for b in self.k_buffer] + [
b.data_ptr() for b in self.v_buffer
]
lens = [b.nbytes for b in self.k_buffer] + [b.nbytes for b in self.v_buffer]
item_lens = [b[0].nbytes * self.page_size for b in self.k_buffer] + [
b[0].nbytes * self.page_size for b in self.v_buffer
]
return ptrs, lens, item_lens
@dataclass
class FakeSwaSubPool:
k_buffer: List[torch.Tensor]
v_buffer: List[torch.Tensor]
@dataclass
class FakeSWAPool:
full_kv_pool: object
swa_kv_pool: object
full_to_swa_index_mapping: torch.Tensor
page_size: int = 1
def get_contiguous_buf_infos(self):
return _kv_buf_infos(
k_buffer=self.full_kv_pool.k_buffer,
v_buffer=self.full_kv_pool.v_buffer,
page_size=self.page_size,
)
def get_state_buf_infos(self):
return _kv_buf_infos(
k_buffer=self.swa_kv_pool.k_buffer,
v_buffer=self.swa_kv_pool.v_buffer,
page_size=self.page_size,
)
def _kv_buf_infos(
*,
k_buffer: List[torch.Tensor],
v_buffer: List[torch.Tensor],
page_size: int,
) -> tuple:
ptrs = [b.data_ptr() for b in k_buffer] + [b.data_ptr() for b in v_buffer]
lens = [b.nbytes for b in k_buffer] + [b.nbytes for b in v_buffer]
item_lens = [b[0].nbytes * page_size for b in k_buffer] + [
b[0].nbytes * page_size for b in v_buffer
]
return ptrs, lens, item_lens
def make_mha_pool(
device: torch.device = DEFAULT_DEVICE,
*,
num_slots: int = 16,
dim: int = 8,
layer_num: int = 2,
) -> FakeMHAPool:
k_layers = [
torch.zeros(num_slots, dim, dtype=torch.float16, device=device)
for _ in range(layer_num)
]
v_layers = [
torch.zeros(num_slots, dim, dtype=torch.float16, device=device)
for _ in range(layer_num)
]
return FakeMHAPool(layer_num=layer_num, k_buffer=k_layers, v_buffer=v_layers)
def make_swa_pool(
device: torch.device = DEFAULT_DEVICE,
*,
full_slots: int = 16,
swa_slots: int = 8,
dim: int = 8,
layer_num: int = 1,
) -> FakeSWAPool:
full = FakeSwaSubPool(
k_buffer=[
torch.zeros(full_slots, dim, dtype=torch.float16, device=device)
for _ in range(layer_num)
],
v_buffer=[
torch.zeros(full_slots, dim, dtype=torch.float16, device=device)
for _ in range(layer_num)
],
)
swa = FakeSwaSubPool(
k_buffer=[
torch.zeros(swa_slots, dim, dtype=torch.float16, device=device)
for _ in range(layer_num)
],
v_buffer=[
torch.zeros(swa_slots, dim, dtype=torch.float16, device=device)
for _ in range(layer_num)
],
)
lut = torch.full((full_slots + 1,), -1, dtype=torch.int64, device=device)
lut[:swa_slots] = torch.arange(swa_slots, dtype=torch.int64, device=device)
return FakeSWAPool(
full_kv_pool=full, swa_kv_pool=swa, full_to_swa_index_mapping=lut
)
def make_base_config() -> CanaryConfig:
return CanaryConfig(
mode=CanaryMode.RAISE,
ring_capacity=1024,
sweep_interval=0,
real_kv_hash_mode=consts.RealKvHashMode.NONE,
enable_write_input_assert=False,
enable_verify_token_assert=True,
stats_print_every_n_steps=100,
)
def make_req_to_token_pool(
device: torch.device = DEFAULT_DEVICE,
*,
max_reqs: int = 8,
max_seq_len: int = 32,
) -> SimpleNamespace:
req_to_token = torch.zeros(max_reqs, max_seq_len, dtype=torch.int32, device=device)
return SimpleNamespace(
req_to_token=req_to_token, size=max_reqs, max_context_len=max_seq_len
)
def make_forward_batch(
device: torch.device = DEFAULT_DEVICE,
*,
bs: int = 2,
seq_lens_list: tuple[int, ...] = (3, 4),
req_pool_indices: Optional[torch.Tensor] = None,
seq_lens: Optional[torch.Tensor] = None,
seq_lens_sum: Optional[int] = None,
extend_prefix_lens: Optional[torch.Tensor] = None,
extend_prefix_lens_cpu: Optional[list] = None,
extend_seq_lens: Optional[torch.Tensor] = None,
extend_seq_lens_cpu: Optional[list] = None,
is_extend: bool = False,
is_target_verify: bool = False,
is_draft_extend_v2: bool = False,
spec_info: Optional[object] = None,
input_ids: Optional[torch.Tensor] = None,
positions: Optional[torch.Tensor] = None,
out_cache_loc: Optional[torch.Tensor] = None,
num_token_non_padded_cpu: Optional[int] = None,
) -> SimpleNamespace:
seq_lens_default = list(seq_lens_list[:bs])
if req_pool_indices is None:
req_pool_indices = torch.tensor([1, 2][:bs], dtype=torch.int64, device=device)
if seq_lens is None:
seq_lens = torch.tensor(seq_lens_default, dtype=torch.int32, device=device)
if seq_lens_sum is None:
seq_lens_sum = int(sum(seq_lens_default))
if input_ids is None:
input_ids = torch.zeros(bs, dtype=torch.int32, device=device)
if positions is None:
# Default to decode-canonical: positions = seq_lens - 1 (one-token-per-req decode write
# at the post-bump tail). Plan input derives decode prefix_lens from positions
# directly, so the default must keep parity.
positions = (seq_lens.to(torch.int64) - 1).clamp(min=0).to(torch.int32)
if out_cache_loc is None:
out_cache_loc = torch.zeros(bs, dtype=torch.int32, device=device)
if is_extend:
forward_mode = ForwardMode.EXTEND
elif is_target_verify:
forward_mode = ForwardMode.TARGET_VERIFY
elif is_draft_extend_v2:
forward_mode = ForwardMode.DRAFT_EXTEND_V2
else:
forward_mode = ForwardMode.DECODE
return SimpleNamespace(
forward_mode=forward_mode,
batch_size=bs,
req_pool_indices=req_pool_indices,
seq_lens=seq_lens,
seq_lens_sum=seq_lens_sum,
extend_prefix_lens=extend_prefix_lens,
extend_prefix_lens_cpu=extend_prefix_lens_cpu,
extend_seq_lens=extend_seq_lens,
extend_seq_lens_cpu=extend_seq_lens_cpu,
spec_info=spec_info,
input_ids=input_ids,
positions=positions,
out_cache_loc=out_cache_loc,
num_token_non_padded_cpu=num_token_non_padded_cpu,
req_all_ids_flat=None,
req_all_ids_lens=None,
)
def make_buffer_group(
*,
device: torch.device = DEFAULT_DEVICE,
kind: PoolKind = PoolKind.FULL,
has_v: bool = True,
has_real_kv: bool = False,
real_kv_source: Optional[RealKvSource] = None,
swa_index_lut: Optional[torch.Tensor] = None,
num_slots: int = 4,
kv_token_id_vs_position_offset: int = 0,
) -> CanaryBufferGroup:
def _zero() -> torch.Tensor:
return torch.zeros(
num_slots, CANARY_SLOT_BYTES, dtype=torch.uint8, device=device
)
if has_real_kv:
source = real_kv_source or RealKvSource(
tensor=torch.zeros(num_slots, 16, dtype=torch.uint8, device=device),
page_size=1,
num_bytes_per_token=16,
read_bytes=16,
)
real_kv_sources = (source,)
else:
real_kv_sources = ()
return CanaryBufferGroup(
kind=kind,
k_head=_zero(),
k_tail=_zero(),
v_head=_zero() if has_v else None,
v_tail=_zero() if has_v else None,
real_kv_sources_k=real_kv_sources,
real_kv_sources_v=real_kv_sources if has_v else (),
swa_index_lut=swa_index_lut,
kv_token_id_vs_position_offset=kv_token_id_vs_position_offset,
)
def make_radix_cache(
slot_lists: List[List[int]], device: torch.device = DEFAULT_DEVICE
):
cache = RadixCache.__new__(RadixCache)
cache.device = device
cache.page_size = 1
cache.disable = False
root = TreeNode()
root.value = torch.tensor(
slot_lists[0] if slot_lists else [], dtype=torch.int32, device=device
)
cache.root_node = root
current = root
for child_slots in slot_lists[1:]:
child = TreeNode()
child.value = torch.tensor(child_slots, dtype=torch.int32, device=device)
child.parent = current
current.children[child.id] = child
current = child
return cache
register_pool_attacher(FakeMHAPool, attach_mha)
register_pool_attacher(FakeSWAPool, attach_swa)