chore: import upstream snapshot with attribution
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
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
This commit is contained in:
@@ -0,0 +1,240 @@
|
||||
"""AOT kernel selection and decode-context helpers for the MLX backend."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from dataclasses import dataclass, field
|
||||
from typing import TYPE_CHECKING, Any, Callable, Optional
|
||||
|
||||
import mlx.core as mx
|
||||
|
||||
from sglang.srt.environ import envs
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from sglang.srt.hardware_backend.mlx.kv_cache.attention_kv_cache import (
|
||||
ContiguousAttentionKVCache,
|
||||
)
|
||||
|
||||
|
||||
def _load_metal_rope_pool_fused():
|
||||
try:
|
||||
from sgl_kernel import metal
|
||||
except ImportError as exc:
|
||||
raise ImportError(
|
||||
"sgl_kernel.metal is not importable. Install sgl-kernel in the "
|
||||
"active environment before enabling SGLANG_MLX_USE_CUSTOM_ROPE."
|
||||
) from exc
|
||||
|
||||
import_error = getattr(metal, "_IMPORT_ERROR", None)
|
||||
if getattr(metal, "_metal", None) is None or import_error is not None:
|
||||
reason = f" Reason: {import_error}." if import_error is not None else ""
|
||||
raise ImportError(
|
||||
"sgl_kernel.metal is importable, but the native Metal extension "
|
||||
f"or metallib is not available.{reason} Install the Metal kernels "
|
||||
"with `uv run sgl-kernel/setup_metal.py install` from the SGLang "
|
||||
"repo root in the active environment."
|
||||
) from import_error
|
||||
return metal.rope_pool_fused
|
||||
|
||||
|
||||
@dataclass
|
||||
class MlxAOTRoPEKernel:
|
||||
base: float = 0.0
|
||||
config: dict[str, Any] = field(default_factory=dict)
|
||||
rope_pool_fused: Optional[Any] = None
|
||||
|
||||
@property
|
||||
def enabled(self) -> bool:
|
||||
return (
|
||||
self.base > 0.0 and bool(self.config) and self.rope_pool_fused is not None
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class MlxAOTKernelBuildInputs:
|
||||
sample_attn: Any
|
||||
n_kv_heads: int
|
||||
head_dim: int
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class MlxAOTKernelSpec:
|
||||
name: str
|
||||
kernel_attr: str
|
||||
is_enabled: Callable[[], bool]
|
||||
build: Callable[[MlxAOTKernelBuildInputs], Any]
|
||||
|
||||
|
||||
@dataclass
|
||||
class MlxAOTKernelSet:
|
||||
rope: MlxAOTRoPEKernel = field(default_factory=MlxAOTRoPEKernel)
|
||||
selected_kernel_names: tuple[str, ...] = ()
|
||||
|
||||
|
||||
class MlxAOTKernelRegistry:
|
||||
"""Registry for optional MLX AOT kernels.
|
||||
|
||||
Each spec owns one kernel field on ``MlxAOTKernelSet``. The registry is the
|
||||
only place that checks kernel enablement policy and model support.
|
||||
"""
|
||||
|
||||
def __init__(self, specs: tuple[MlxAOTKernelSpec, ...]):
|
||||
self._specs = specs
|
||||
|
||||
@property
|
||||
def registered_kernel_names(self) -> tuple[str, ...]:
|
||||
return tuple(spec.name for spec in self._specs)
|
||||
|
||||
def build_kernel_set(
|
||||
self,
|
||||
*,
|
||||
sample_attn: Any,
|
||||
n_kv_heads: int,
|
||||
head_dim: int,
|
||||
) -> MlxAOTKernelSet:
|
||||
inputs = MlxAOTKernelBuildInputs(
|
||||
sample_attn=sample_attn,
|
||||
n_kv_heads=n_kv_heads,
|
||||
head_dim=head_dim,
|
||||
)
|
||||
kernel_set = MlxAOTKernelSet()
|
||||
selected_kernel_names = []
|
||||
for spec in self._specs:
|
||||
if not spec.is_enabled():
|
||||
continue
|
||||
kernel = spec.build(inputs)
|
||||
if getattr(kernel, "enabled", False):
|
||||
if not hasattr(kernel_set, spec.kernel_attr):
|
||||
raise ValueError(
|
||||
f"AOT kernel {spec.name} targets unknown kernel-set "
|
||||
f"attribute {spec.kernel_attr}"
|
||||
)
|
||||
setattr(kernel_set, spec.kernel_attr, kernel)
|
||||
selected_kernel_names.append(spec.name)
|
||||
kernel_set.selected_kernel_names = tuple(selected_kernel_names)
|
||||
if kernel_set.selected_kernel_names:
|
||||
logger.info(
|
||||
"MLX AOT kernels selected: %s",
|
||||
", ".join(kernel_set.selected_kernel_names),
|
||||
)
|
||||
return kernel_set
|
||||
|
||||
|
||||
def _build_rope_kernel(inputs: MlxAOTKernelBuildInputs) -> MlxAOTRoPEKernel:
|
||||
from sglang.srt.hardware_backend.mlx.kv_cache.attention_contract import (
|
||||
get_num_heads,
|
||||
)
|
||||
|
||||
sample_attn = getattr(inputs.sample_attn, "_inner", inputs.sample_attn)
|
||||
rope = getattr(sample_attn, "rope", None)
|
||||
if rope is None or getattr(rope, "traditional", False):
|
||||
return MlxAOTRoPEKernel()
|
||||
|
||||
rope_dim = int(getattr(rope, "dims", 0))
|
||||
if rope_dim == 0:
|
||||
return MlxAOTRoPEKernel()
|
||||
if rope_dim != inputs.head_dim:
|
||||
# AOT kernel currently requires rope_dim == head_dim.
|
||||
return MlxAOTRoPEKernel()
|
||||
|
||||
base = float(getattr(rope, "base", 10000.0))
|
||||
num_qo_heads = get_num_heads(sample_attn)
|
||||
if num_qo_heads is None:
|
||||
return MlxAOTRoPEKernel()
|
||||
config = {
|
||||
"head_dim": int(inputs.head_dim),
|
||||
"rope_dim": rope_dim,
|
||||
"num_qo_heads": int(num_qo_heads),
|
||||
"num_kv_heads": int(inputs.n_kv_heads),
|
||||
}
|
||||
try:
|
||||
rope_pool_fused = _load_metal_rope_pool_fused()
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.info(
|
||||
"AOT Metal RoPE kernel not available (%s) - falling back to "
|
||||
"mx.fast.rope.",
|
||||
exc,
|
||||
)
|
||||
return MlxAOTRoPEKernel()
|
||||
|
||||
logger.info(
|
||||
f"AOT Metal RoPE kernel ENABLED: head_dim={inputs.head_dim}, "
|
||||
f"n_heads={config['num_qo_heads']}, n_kv={config['num_kv_heads']}, "
|
||||
f"base={base}"
|
||||
)
|
||||
return MlxAOTRoPEKernel(
|
||||
base=base,
|
||||
config=config,
|
||||
rope_pool_fused=rope_pool_fused,
|
||||
)
|
||||
|
||||
|
||||
MLX_AOT_KERNEL_REGISTRY = MlxAOTKernelRegistry(
|
||||
specs=(
|
||||
MlxAOTKernelSpec(
|
||||
name="metal_rope_pool_fused",
|
||||
kernel_attr="rope",
|
||||
is_enabled=lambda: envs.SGLANG_MLX_USE_CUSTOM_ROPE.get(),
|
||||
build=_build_rope_kernel,
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class MlxAOTRoPEContext:
|
||||
kernel: MlxAOTRoPEKernel
|
||||
kv_pool: Any
|
||||
new_token_slots: Optional[mx.array] = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class MlxAOTKernelContext:
|
||||
rope: Optional[MlxAOTRoPEContext] = None
|
||||
|
||||
@classmethod
|
||||
def from_decode(
|
||||
cls,
|
||||
*,
|
||||
aot_kernels: MlxAOTKernelSet,
|
||||
kv_pool: Any | None,
|
||||
req_ids: list[str],
|
||||
req_pool_idx: dict[str, int],
|
||||
req_to_token_pool: Any | None,
|
||||
layer_caches: list[list[ContiguousAttentionKVCache]],
|
||||
) -> MlxAOTKernelContext:
|
||||
"""Build optional AOT context for one batched decode step."""
|
||||
if not aot_kernels.rope.enabled or kv_pool is None:
|
||||
return cls()
|
||||
|
||||
new_token_slots = None
|
||||
if req_to_token_pool is not None:
|
||||
try:
|
||||
slot_ids = []
|
||||
for req_idx, req_id in enumerate(req_ids):
|
||||
pool_idx = req_pool_idx.get(req_id)
|
||||
if pool_idx is None:
|
||||
raise KeyError(req_id)
|
||||
slot = int(
|
||||
req_to_token_pool.req_to_token[
|
||||
pool_idx, layer_caches[0][req_idx].offset
|
||||
].item()
|
||||
)
|
||||
slot_ids.append(slot)
|
||||
new_token_slots = mx.array(slot_ids, dtype=mx.int32)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.warning(
|
||||
"AOT RoPE: failed to resolve new-token slots (%s); "
|
||||
"falling back to RoPE-only for this decode step",
|
||||
exc,
|
||||
)
|
||||
|
||||
return cls(
|
||||
rope=MlxAOTRoPEContext(
|
||||
kernel=aot_kernels.rope,
|
||||
kv_pool=kv_pool,
|
||||
new_token_slots=new_token_slots,
|
||||
)
|
||||
)
|
||||
Reference in New Issue
Block a user