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
254 lines
8.7 KiB
Python
254 lines
8.7 KiB
Python
from __future__ import annotations
|
|
|
|
from abc import ABC
|
|
from typing import TYPE_CHECKING, Optional
|
|
|
|
import torch
|
|
|
|
from sglang.kernel_api_logging import debug_kernel_api
|
|
from sglang.srt.utils.common import is_npu
|
|
|
|
if TYPE_CHECKING:
|
|
from sglang.srt.layers.attention.dsa.dsa_indexer import BaseIndexerMetadata
|
|
from sglang.srt.layers.radix_attention import RadixAttention
|
|
from sglang.srt.model_executor.forward_batch_info import ForwardBatch
|
|
from sglang.srt.speculative.spec_info import SpecInput
|
|
|
|
|
|
class AttentionBackend(ABC):
|
|
"""The base class of attention backends.
|
|
|
|
Forward-data init contract (3 methods):
|
|
|
|
- ``init_forward_metadata(fb)`` — eager entry point. Default is a wrapper
|
|
that calls ``_out_graph(fb)`` then ``_in_graph(fb)``. Backends may
|
|
override to keep an independent eager body.
|
|
- ``init_forward_metadata_out_graph(fb, in_capture=False)`` — per-iter
|
|
metadata prep, runs outside ``with graph.capture():``. Capture
|
|
sites pass ``in_capture=True``; replay/eager use the default
|
|
``False``. Backends read ``in_capture`` only when capture / replay
|
|
bodies diverge.
|
|
- ``init_forward_metadata_in_graph(fb)`` — graph-recordable static-shape
|
|
GPU op, runs inside ``with graph.capture():`` at capture time and
|
|
is auto-replayed by ``graph.replay()``. Default is no-op.
|
|
|
|
The legacy ``init_forward_metadata_capture_cuda_graph`` and
|
|
``init_forward_metadata_replay_cuda_graph`` overrides are fully
|
|
deprecated and removed from the ABC: out-of-tree backends overriding
|
|
those must migrate to ``init_forward_metadata_out_graph(fb, in_capture)``.
|
|
"""
|
|
|
|
# Resolved per-mode backend names, stamped by ModelRunner.init_attention_backend
|
|
prefill_attention_backend_str: Optional[str] = None
|
|
decode_attention_backend_str: Optional[str] = None
|
|
|
|
supports_ragged_verify_graph: bool = False
|
|
|
|
def init_forward_metadata(self, forward_batch: ForwardBatch):
|
|
"""Eager entry point. Default = ``_out_graph(fb) + _in_graph(fb)``.
|
|
|
|
Backends may override to keep an independent eager body.
|
|
"""
|
|
self.init_forward_metadata_out_graph(forward_batch)
|
|
self.init_forward_metadata_in_graph(forward_batch)
|
|
|
|
def init_forward_metadata_out_graph(
|
|
self,
|
|
forward_batch: ForwardBatch,
|
|
in_capture: bool = False,
|
|
):
|
|
"""Per-iter metadata prep — runs outside ``with graph.capture():``.
|
|
|
|
Called at:
|
|
* capture: before ``with graph.capture():`` (caller passes
|
|
``in_capture=True``).
|
|
* replay: before ``graph.replay()`` (``in_capture=False``).
|
|
* eager: via :py:meth:`init_forward_metadata` default wrapper
|
|
(``in_capture=False``).
|
|
|
|
Backends read ``in_capture`` only when capture / replay bodies
|
|
diverge (e.g., snapshot metadata, swap buffer pointers, install
|
|
temp workspace). Host op / dynamic-shape / non-graph-recordable
|
|
logic lives here.
|
|
|
|
Default: no-op.
|
|
"""
|
|
|
|
def init_forward_metadata_in_graph(self, forward_batch: ForwardBatch):
|
|
"""Graph-recordable static-shape GPU op.
|
|
|
|
Runs inside ``with graph.capture():`` at capture time; recorded
|
|
ops auto-execute at replay via ``graph.replay()``.
|
|
|
|
Lint contract for overrides: body must NOT call ``.item()`` /
|
|
``.cpu()`` / ``.tolist()`` / dynamic-shape ``torch.empty()``.
|
|
Such ops belong in :py:meth:`init_forward_metadata_out_graph`; they
|
|
cannot be recorded into a cuda graph.
|
|
|
|
Default: no-op.
|
|
"""
|
|
|
|
# Opt out only when this backend never reads seq_lens_cpu / seq_lens_sum.
|
|
needs_cpu_seq_lens: bool = True
|
|
|
|
# Most attention backends can rebuild and replace forward metadata before
|
|
# every forward. BCG capture is different: some backends expose metadata
|
|
# tensors to kernels across graph breaks, so the captured graph depends on
|
|
# those tensor addresses. Such backends opt in here, create the metadata
|
|
# object during capture, and refresh its dynamic fields before each replay.
|
|
use_captured_forward_metadata_for_breakable_cuda_graph: bool = False
|
|
|
|
def init_cuda_graph_state(self, max_bs: int, max_num_tokens: int):
|
|
"""Init the global shared states for cuda graph."""
|
|
raise NotImplementedError()
|
|
|
|
def init_forward_metadata_for_breakable_cuda_graph_capture(
|
|
self,
|
|
forward_batch: ForwardBatch,
|
|
):
|
|
"""Create forward metadata whose tensor addresses will be graph-captured."""
|
|
raise NotImplementedError()
|
|
|
|
def prepare_forward_metadata_for_breakable_cuda_graph_replay(
|
|
self,
|
|
capture_metadata,
|
|
forward_batch: ForwardBatch,
|
|
*,
|
|
static_forward_batch: Optional[ForwardBatch] = None,
|
|
) -> None:
|
|
"""Refresh captured metadata for the current batch before BCG replay.
|
|
|
|
Implementations should update ``capture_metadata`` in place where graph
|
|
address stability is required, assign any safe per-replay objects, and
|
|
make the backend's active ``forward_metadata`` point to the captured
|
|
metadata object.
|
|
"""
|
|
raise NotImplementedError()
|
|
|
|
def get_cuda_graph_seq_len_fill_value(self):
|
|
"""Get the fill value for padded seq lens. Typically, it is 0 or 1."""
|
|
raise NotImplementedError()
|
|
|
|
def on_after_cuda_graph_warmup(self):
|
|
"""Hook between cuda graph warmup pass and the actual capture.
|
|
|
|
Override to undo state that warmup mutated or eagerly advanced
|
|
(e.g. dirty metadata buffers, raw->full upgrades) before capture
|
|
freezes the kernel pointers.
|
|
"""
|
|
pass
|
|
|
|
def get_verify_buffers_to_fill_after_draft(self):
|
|
"""
|
|
Return buffers of verify attention kernels that needs to be filled after draft.
|
|
|
|
Typically, these are tree mask and position buffers.
|
|
"""
|
|
return [None, None]
|
|
|
|
def update_verify_buffers_to_fill_after_draft(
|
|
self, spec_info: SpecInput, cuda_graph_bs: Optional[int]
|
|
):
|
|
"""
|
|
Update the buffers returned by get_verify_fill_after_draft_buffers if needed.
|
|
|
|
Here, we need to redo the computation of all metadata of the attention backend
|
|
that depends on tree mask and position buffers.
|
|
"""
|
|
raise NotImplementedError()
|
|
|
|
@debug_kernel_api
|
|
def forward(
|
|
self,
|
|
q: torch.Tensor,
|
|
k: torch.Tensor,
|
|
v: torch.Tensor,
|
|
layer: RadixAttention,
|
|
forward_batch: ForwardBatch,
|
|
save_kv_cache: bool = True,
|
|
**kwargs,
|
|
):
|
|
"""Run forward on an attention layer."""
|
|
if forward_batch.forward_mode.is_idle():
|
|
return q.new_empty(q.shape[0], layer.tp_q_head_num * layer.v_head_dim)
|
|
elif forward_batch.forward_mode.is_decode():
|
|
return self.forward_decode(
|
|
q,
|
|
k,
|
|
v,
|
|
layer,
|
|
forward_batch,
|
|
save_kv_cache=save_kv_cache,
|
|
**kwargs,
|
|
)
|
|
elif forward_batch.forward_mode.is_mixed() and is_npu():
|
|
return self.forward_mixed(
|
|
q,
|
|
k,
|
|
v,
|
|
layer,
|
|
forward_batch,
|
|
save_kv_cache=save_kv_cache,
|
|
**kwargs,
|
|
)
|
|
else:
|
|
return self.forward_extend(
|
|
q,
|
|
k,
|
|
v,
|
|
layer,
|
|
forward_batch,
|
|
save_kv_cache=save_kv_cache,
|
|
**kwargs,
|
|
)
|
|
|
|
def forward_decode(
|
|
self,
|
|
q: torch.Tensor,
|
|
k: torch.Tensor,
|
|
v: torch.Tensor,
|
|
layer: RadixAttention,
|
|
forward_batch: ForwardBatch,
|
|
save_kv_cache: bool = True,
|
|
**kwargs,
|
|
):
|
|
"""Run a forward for decode."""
|
|
raise NotImplementedError()
|
|
|
|
def forward_extend(
|
|
self,
|
|
q: torch.Tensor,
|
|
k: torch.Tensor,
|
|
v: torch.Tensor,
|
|
layer: RadixAttention,
|
|
forward_batch: ForwardBatch,
|
|
save_kv_cache: bool = True,
|
|
**kwargs,
|
|
):
|
|
"""Run a forward for extend."""
|
|
raise NotImplementedError()
|
|
|
|
def forward_mixed(
|
|
self,
|
|
q: torch.Tensor,
|
|
k: torch.Tensor,
|
|
v: torch.Tensor,
|
|
layer: RadixAttention,
|
|
forward_batch: ForwardBatch,
|
|
save_kv_cache: bool = True,
|
|
):
|
|
"""Run a forward for mix."""
|
|
raise NotImplementedError()
|
|
|
|
def support_triton(self):
|
|
"""Check if the current backend supports triton."""
|
|
return True
|
|
|
|
def get_indexer_metadata(
|
|
self,
|
|
layer_id: int,
|
|
forward_batch: ForwardBatch,
|
|
) -> Optional[BaseIndexerMetadata]:
|
|
"""Get the indexer metadata. None means don't support indexer."""
|
|
return None
|