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

166 lines
9.5 KiB
Python

from __future__ import annotations
from typing import Optional
import torch
from sglang.jit_kernel.kv_canary.plan.entries_kernel import (
launch_plan_entries_kernel,
)
from sglang.jit_kernel.kv_canary.plan.offsets_kernel import (
_PLAN_BS_BLOCK_SIZE,
launch_plan_offsets_kernel,
)
from sglang.jit_kernel.kv_canary.verify import VerifyPlan
from sglang.jit_kernel.kv_canary.write import WritePlan
def launch_canary_plan_kernels(
*,
verify_plan_out: VerifyPlan,
write_plan_out: WritePlan,
req_pool_indices: torch.Tensor,
prefix_lens: torch.Tensor,
extend_seq_lens: torch.Tensor,
req_to_token: torch.Tensor,
swa_window_size: int,
full_to_swa_index_mapping: Optional[torch.Tensor],
verify_capacity: int,
req_to_verify_expected_tokens: Optional[torch.Tensor],
req_to_verify_expected_tokens_valid_lens: Optional[torch.Tensor],
kv_token_id_vs_position_offset: int,
) -> None:
"""Fill verify_plan_out + write_plan_out from normalized canary plan inputs.
For each req r with req_pool_indices[r] != 0 (0 = padding sentinel):
- **Verify entries**: one per pos in [window_start, prefix_lens[r]), where window_start = max(0,
prefix_lens[r] - swa_window_size) if SWA else 0. slot_idx = req_to_token[req_pool_indices[r], pos]
(SWA-translated via full_to_swa_index_mapping if non-None); prev_slot_idx =
req_to_token[req_pool_indices[r], pos-1] for pos > 0, else -1. (SWA windows do NOT reset the chain —
the writer chains across the entire prefix; sweep verify within an SWA window dereferences the real
predecessor for chain-link reconstruction.) Expected-token gather: when
``req_to_verify_expected_tokens`` is supplied, ``expected_input_id =
req_to_verify_expected_tokens[rp, pos + kv_token_id_vs_position_offset]`` when ``0 <= pos +
kv_token_id_vs_position_offset < req_to_verify_expected_tokens_valid_lens[r]``, else the ``-1``
sentinel (which the verify kernel treats as "skip token-id check").
- **Write metadata** (when extend_seq_lens[r] > 0): contribute extend_seq_lens[r] to the per-req
write count (for write_offsets cumsum). Per-req chain seed = req_to_token[req_pool_indices[r],
prefix_lens[r]-1] (SWA-translated), or -1 if prefix_lens[r] == 0. Per-token write data
(input_ids / positions / out_cache_loc) is NOT materialized here — launch_canary_write_kernel
reads it directly from ForwardBatch via write_offsets.
Args:
verify_plan_out: Pre-allocated VerifyPlan; filled in-place.
write_plan_out: Pre-allocated WritePlan; filled in-place.
req_pool_indices: Per-row ReqToTokenPool row index, shape [bs], int64. 0 is the padding sentinel.
prefix_lens: Per-req prefix length already written before this step, shape [bs], int64.
extend_seq_lens: Per-req tokens being written this step, shape [bs], int64.
req_to_token: ReqToTokenPool.req_to_token; full-pool slot index table, shape [max_reqs, max_seq_len],
int32.
swa_window_size: 0 for the FULL canary group; positive window length for the SWA group.
full_to_swa_index_mapping: SWA LUT, shape [full_pool_size + 1], int64, or None. Required (non-None) iff
swa_window_size > 0. Used to translate verify slot indices and chain-seed slot indices at plan time.
Loaded element-typed via Triton ``tl.load``; intermediate translated slot values are int64 inside the
kernel and stored in the int64 plan schema.
verify_capacity: Length of verify_plan_out.verify_*; on overflow the offsets kernel clears
verify_enable and plan_entries skips the scatter.
req_to_verify_expected_tokens: Optional source-of-truth token pool, shape [max_reqs, max_context_len],
int32. When supplied, the plan kernel gathers expected_input_id for each verify entry from
``[rp, pos + kv_token_id_vs_position_offset]``; when None, every entry gets the ``-1`` sentinel.
req_to_verify_expected_tokens_valid_lens: Per-req snapshot length on ``req_to_verify_expected_tokens``,
shape [bs], int64. Required iff ``req_to_verify_expected_tokens`` is set. Reads past
``valid_lens[r]`` skip the gather (emit ``-1``) — this is what makes the plan kernel correct in the
presence of EAGLE draft / verify positions written past the committed history, and across pool
rows recycled from a longer previous owner whose stale tail still lives at high indices.
kv_token_id_vs_position_offset: Per-buffer-group logical-position offset applied to ``pos`` before
indexing ``req_to_verify_expected_tokens``. 0 for target pools; +1 for EAGLE draft.
Implementation:
- Two sub-kernels launched in sequence:
1. Triton ``_plan_offsets_kernel`` (1-D grid ``(1,)``, single program over all ``bs`` reqs):
reads req_pool_indices[r], prefix_lens[r], extend_seq_lens[r] for each r; computes
verify_count = (prefix_lens - window_start) and write_count = extend_seq_lens (both 0 if rp == 0
padding); gathers seed_slot_full = req_to_token[rp, prefix_lens - 1] (or -1 if prefix_lens == 0),
SWA-translates seed_slot via full_to_swa_index_mapping[seed_slot_full] if non-None; runs
block-level cumsum (``tl.cumsum``) to produce verify_offsets[_PLAN_BS_BLOCK_SIZE + 1] and
write_plan_out.write_offsets[write_req_capacity + 1] in-place; scatters write_seed slots; writes
scalar totals ``verify_plan_out.verify_num_valid`` and ``write_plan_out.write_num_valid_reqs``.
2. CUDA ``plan_entries_persistent_kernel`` (1-D persistent grid sized to ``num_sms *
kBlocksPerSm`` blocks of ``kBlockSize`` threads), wrapped by Python
``launch_plan_entries_kernel``: each thread grid-strides over ``tid ∈ [0, total_verify)``,
locates its owning req via ``find_req_id`` (binary search on verify_offsets), computes
out_position = window_start[req_id] + (tid - verify_offsets[req_id]), gathers slot =
req_to_token[rp, out_position] (SWA-translated when ``HAS_SWA_LUT``), prev_slot =
req_to_token[rp, out_position - 1] when out_position > 0 (also translated) else -1, and
scatters (slot, position, prev_slot) into verify_plan_out at flat index tid.
- All output tensors are addressed at addresses baked into the cuda-graph capture.
Calling contract:
- Pure side-effect; no host work, no D2H.
- Safe in cuda-graph capture; caller refills all input tensors in-place before replay.
- The wrapper launches the plan sub-kernels needed to fill both plans end-to-end.
- Padding rows contribute zero entries.
Pinned by Python reference
:func:`sglang.jit_kernel.kv_canary.plan_ref.launch_canary_plan_kernels_torch_reference`; both the Triton
offsets kernel and the CUDA JIT entries kernel must match byte-for-byte.
"""
bs = int(req_pool_indices.shape[0])
if bs > _PLAN_BS_BLOCK_SIZE:
raise ValueError(
f"kv-canary: launch_canary_plan_kernels supports at most bs={_PLAN_BS_BLOCK_SIZE} reqs per launch, "
f"got bs={bs}. Bump _PLAN_BS_BLOCK_SIZE if real workloads need this."
)
if swa_window_size > 0 and full_to_swa_index_mapping is None:
raise ValueError(
"kv-canary: launch_canary_plan_kernels requires full_to_swa_index_mapping when swa_window_size > 0"
)
device = verify_plan_out.verify_slot_indices.device
verify_offsets_scratch = torch.empty(
_PLAN_BS_BLOCK_SIZE + 1, dtype=torch.int64, device=device
)
plan_verify_capacity = int(verify_plan_out.verify_slot_indices.shape[0])
if verify_capacity != plan_verify_capacity:
raise ValueError(
f"kv-canary: launch_canary_plan_kernels verify_capacity={verify_capacity} does not match "
f"verify_plan_out.verify_slot_indices.shape[0]={plan_verify_capacity}"
)
write_plan_out.write_offsets.zero_()
launch_plan_offsets_kernel(
req_pool_indices=req_pool_indices,
prefix_lens=prefix_lens,
extend_seq_lens=extend_seq_lens,
req_to_token=req_to_token,
full_to_swa_index_mapping=full_to_swa_index_mapping,
out_verify_offsets_scratch=verify_offsets_scratch,
out_write_offsets=write_plan_out.write_offsets,
out_write_seed_slot_indices=write_plan_out.write_seed_slot_indices,
out_verify_num_valid=verify_plan_out.verify_num_valid,
out_verify_enable=verify_plan_out.enable,
out_write_num_valid_reqs=write_plan_out.write_num_valid_reqs,
swa_window_size=int(swa_window_size),
verify_capacity=verify_capacity,
)
launch_plan_entries_kernel(
req_pool_indices=req_pool_indices,
prefix_lens=prefix_lens,
req_to_token=req_to_token,
full_to_swa_index_mapping=full_to_swa_index_mapping,
verify_offsets_scratch=verify_offsets_scratch,
verify_enable=verify_plan_out.enable,
req_to_verify_expected_tokens=req_to_verify_expected_tokens,
req_to_verify_expected_tokens_valid_lens=req_to_verify_expected_tokens_valid_lens,
out_verify_slot_indices=verify_plan_out.verify_slot_indices,
out_verify_expected_tokens=verify_plan_out.verify_expected_tokens,
out_verify_expected_positions=verify_plan_out.verify_expected_positions,
out_verify_prev_slot_indices=verify_plan_out.verify_prev_slot_indices,
kv_token_id_vs_position_offset=int(kv_token_id_vs_position_offset),
swa_window_size=int(swa_window_size),
)