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

373 lines
12 KiB
Python

from __future__ import annotations
import weakref
from abc import ABC, abstractmethod
from enum import Enum
from typing import (
TYPE_CHECKING,
Any,
Callable,
Optional,
OrderedDict,
Protocol,
Tuple,
TypeGuard,
Union,
runtime_checkable,
)
import torch
if TYPE_CHECKING:
from sglang.srt.batch_overlap.single_batch_overlap import CombineOverlapArgs
from sglang.srt.layers.moe.token_dispatcher import (
DeepEPLLCombineInput,
DeepEPLLDispatchOutput,
DeepEPNormalCombineInput,
DeepEPNormalDispatchOutput,
FlashinferCombineInput,
FlashinferDispatchOutput,
StandardCombineInput,
StandardDispatchOutput,
)
from sglang.srt.layers.moe.topk import TopKOutput
# ------------------------------ Dispatcher Hook -------------------------------------
class _RemovableDispatcherHandle:
next_id = 0 # Global counter for unique IDs
def __init__(self, hooks_dict: OrderedDict):
self.id = _RemovableDispatcherHandle.next_id
_RemovableDispatcherHandle.next_id += 1
self.weak_hooks_dict = weakref.ref(hooks_dict)
def remove(self):
hooks_dict = self.weak_hooks_dict()
if hooks_dict is not None and self.id in hooks_dict:
del hooks_dict[self.id]
class DispatcherBaseHooks:
def __init__(self):
self.hook_dict = OrderedDict[int, Callable]()
def register_hook(self, hook_fun: Callable) -> _RemovableDispatcherHandle:
handle = _RemovableDispatcherHandle(self.hook_dict)
self.hook_dict[handle.id] = hook_fun
return handle
def __call__(self, *args, **kwargs) -> Optional[Any]:
raise NotImplementedError("This method should be overridden by subclasses")
class _PreDispatchHooks(DispatcherBaseHooks):
def __call__(
self,
dispatcher: BaseDispatcher,
hidden_states: torch.Tensor,
topk_output: TopKOutput,
) -> Optional[Tuple[torch.Tensor, TopKOutput]]:
for hook_fun in self.hook_dict.values():
hook_output = hook_fun(dispatcher, hidden_states, topk_output)
if hook_output is not None:
hidden_states, topk_output = hook_output
return hidden_states, topk_output
class _PostDispatchHooks(DispatcherBaseHooks):
def __call__(
self, dispatcher: BaseDispatcher, dispatch_output: DispatchOutput
) -> Optional[DispatchOutput]:
for hook_fun in self.hook_dict.values():
hook_output = hook_fun(dispatcher, dispatch_output)
if hook_output is not None:
dispatch_output = hook_output
return dispatch_output
class _PreCombineHooks(DispatcherBaseHooks):
def __call__(
self, dispatcher: BaseDispatcher, combine_input: CombineInput
) -> Optional[CombineInput]:
for hook_fun in self.hook_dict.values():
hook_output = hook_fun(dispatcher, combine_input)
if hook_output is not None:
combine_input = hook_output
return combine_input
class _PostCombineHooks(DispatcherBaseHooks):
def __call__(
self, dispatcher: BaseDispatcher, hidden_states: torch.Tensor
) -> Optional[torch.Tensor]:
for hook_fun in self.hook_dict.values():
hook_output = hook_fun(dispatcher, hidden_states)
if hook_output is not None:
hidden_states = hook_output
return hidden_states
# ------------------------------ Dispatch Output -------------------------------------
class DispatchOutputChecker:
@staticmethod
def format_is_standard(
dispatch_output: DispatchOutput,
) -> TypeGuard[StandardDispatchOutput]:
return dispatch_output.format.is_standard()
@staticmethod
def format_is_triton_kernels(
dispatch_output: DispatchOutput,
) -> TypeGuard[StandardDispatchOutput]:
return dispatch_output.format.is_standard()
@staticmethod
def format_is_deepep_normal(
dispatch_output: DispatchOutput,
) -> TypeGuard[DeepEPNormalDispatchOutput]:
return dispatch_output.format.is_deepep_normal()
@staticmethod
def format_is_deepep_ll(
dispatch_output: DispatchOutput,
) -> TypeGuard[DeepEPLLDispatchOutput]:
return dispatch_output.format.is_deepep_ll()
@staticmethod
def format_is_deepep(
dispatch_output: DispatchOutput,
) -> TypeGuard[Union[DeepEPNormalDispatchOutput, DeepEPLLDispatchOutput]]:
return dispatch_output.format.is_deepep()
@staticmethod
def format_is_flashinfer(
dispatch_output: DispatchOutput,
) -> TypeGuard[FlashinferDispatchOutput]:
return dispatch_output.format.is_flashinfer()
class DispatchOutputFormat(Enum):
STANDARD = "standard"
DEEPEP_NORMAL = "deepep_normal"
DEEPEP_LL = "deepep_ll"
FLASHINFER = "flashinfer"
def is_standard(self) -> bool:
return self == DispatchOutputFormat.STANDARD
def is_deepep_normal(self) -> bool:
return self == DispatchOutputFormat.DEEPEP_NORMAL
def is_deepep_ll(self) -> bool:
return self == DispatchOutputFormat.DEEPEP_LL
def is_deepep(self) -> bool:
return self in [
DispatchOutputFormat.DEEPEP_NORMAL,
DispatchOutputFormat.DEEPEP_LL,
]
def is_flashinfer(self) -> bool:
return self == DispatchOutputFormat.FLASHINFER
@runtime_checkable
class DispatchOutput(Protocol):
"""Protocol for dispatch outputs in different formats."""
hidden_states: torch.Tensor
@property
def format(self) -> DispatchOutputFormat: ...
# ------------------------------ Combine Input -------------------------------------
class CombineInputChecker:
@staticmethod
def format_is_standard(
combine_input: CombineInput,
) -> TypeGuard[StandardCombineInput]:
return combine_input.format == CombineInputFormat.STANDARD
@staticmethod
def format_is_deepep_normal(
combine_input: CombineInput,
) -> TypeGuard[DeepEPNormalCombineInput]:
return combine_input.format == CombineInputFormat.DEEPEP_NORMAL
@staticmethod
def format_is_deepep_ll(
combine_input: CombineInput,
) -> TypeGuard[DeepEPLLCombineInput]:
return combine_input.format == CombineInputFormat.DEEPEP_LL
@staticmethod
def format_is_deepep(
combine_input: CombineInput,
) -> TypeGuard[Union[DeepEPNormalCombineInput, DeepEPLLCombineInput]]:
return combine_input.format in [
CombineInputFormat.DEEPEP_NORMAL,
CombineInputFormat.DEEPEP_LL,
]
@staticmethod
def format_is_flashinfer(
combine_input: CombineInput,
) -> TypeGuard[FlashinferCombineInput]:
return combine_input.format == CombineInputFormat.FLASHINFER
class CombineInputFormat(Enum):
STANDARD = "standard"
DEEPEP_NORMAL = "deepep_normal"
DEEPEP_LL = "deepep_ll"
FLASHINFER = "flashinfer"
@runtime_checkable
class CombineInput(Protocol):
"""Protocol for combine inputs in different formats."""
# TODO: add hidden_states to the protocol
@property
def format(self) -> CombineInputFormat: ...
# ------------------------------ Base Dispatcher -------------------------------------
class BaseDispatcherConfig(ABC):
"""Base class for dispatcher configs."""
pass
class BaseDispatcher(ABC):
"""Base class for dispatchers."""
def __init__(self):
self.quant_config: dict = {}
# Overlap args
self.overlap_args: Optional[CombineOverlapArgs] = None
self.meta_overlap_args: Optional[dict] = None
# Hooks
self._pre_dispatch_hooks: Optional[_PreDispatchHooks] = None
self._post_dispatch_hooks: Optional[_PostDispatchHooks] = None
self._pre_combine_hooks: Optional[_PreCombineHooks] = None
self._post_combine_hooks: Optional[_PostCombineHooks] = None
self._original_dispatch_func: Optional[Callable] = None
self._original_combine_func: Optional[Callable] = None
@abstractmethod
def dispatch(
self, hidden_states: torch.Tensor, topk_output: TopKOutput
) -> DispatchOutput:
pass
def _dispatch_with_hook(
self, hidden_states: torch.Tensor, topk_output: TopKOutput
) -> DispatchOutput:
if self._pre_dispatch_hooks is not None:
hidden_states, topk_output = self._pre_dispatch_hooks(
self, hidden_states, topk_output
)
dispatch_output = self._original_dispatch_func(
hidden_states=hidden_states, topk_output=topk_output
)
if self._post_dispatch_hooks is not None:
dispatch_output = self._post_dispatch_hooks(self, dispatch_output)
return dispatch_output
def _override_dispatch_func(self) -> None:
if self._original_dispatch_func is None:
self._original_dispatch_func = self.dispatch
self.dispatch = self._dispatch_with_hook
@abstractmethod
def combine(self, combine_input: CombineInput) -> torch.Tensor:
pass
def _combine_with_hook(self, combine_input: CombineInput) -> torch.Tensor:
if self._pre_combine_hooks is not None:
combine_input = self._pre_combine_hooks(self, combine_input)
hidden_states = self._original_combine_func(combine_input=combine_input)
if self._post_combine_hooks is not None:
hidden_states = self._post_combine_hooks(self, hidden_states)
return hidden_states
def _override_combine_func(self) -> None:
if self._original_combine_func is None:
self._original_combine_func = self.combine
self.combine = self._combine_with_hook
def register_pre_dispatch_hook(
self,
hook: Callable[
[BaseDispatcher, torch.Tensor, TopKOutput],
Optional[Tuple[torch.Tensor, TopKOutput]],
],
) -> _RemovableDispatcherHandle:
if self._pre_dispatch_hooks is None:
self._pre_dispatch_hooks = _PreDispatchHooks()
self._override_dispatch_func()
handle = self._pre_dispatch_hooks.register_hook(hook)
return handle
def register_post_dispatch_hook(
self, hook: Callable[[BaseDispatcher, DispatchOutput], Optional[DispatchOutput]]
) -> _RemovableDispatcherHandle:
if self._post_dispatch_hooks is None:
self._post_dispatch_hooks = _PostDispatchHooks()
self._override_dispatch_func()
handle = self._post_dispatch_hooks.register_hook(hook)
return handle
def register_pre_combine_hook(
self, hook: Callable[[BaseDispatcher, CombineInput], Optional[CombineInput]]
) -> _RemovableDispatcherHandle:
if self._pre_combine_hooks is None:
self._pre_combine_hooks = _PreCombineHooks()
self._override_combine_func()
handle = self._pre_combine_hooks.register_hook(hook)
return handle
def register_post_combine_hook(
self, hook: Callable[[BaseDispatcher, torch.Tensor], Optional[torch.Tensor]]
) -> _RemovableDispatcherHandle:
if self._post_combine_hooks is None:
self._post_combine_hooks = _PostCombineHooks()
self._override_combine_func()
handle = self._post_combine_hooks.register_hook(hook)
return handle
def set_quant_config(self, quant_config: dict) -> None:
self.quant_config = quant_config
def set_overlap_args(
self, combine_overlap_args: CombineOverlapArgs, meta_overlap_args: dict
) -> None:
self.overlap_args = combine_overlap_args
self.meta_overlap_args = meta_overlap_args
def clear_overlap_args(self) -> None:
self.overlap_args = None
self.meta_overlap_args = None