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
286 lines
9.2 KiB
Python
286 lines
9.2 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import TYPE_CHECKING, Any, Dict, Optional
|
|
|
|
import torch
|
|
|
|
from sglang.srt.environ import envs
|
|
from sglang.srt.layers import deep_gemm_wrapper
|
|
from sglang.srt.layers.moe import (
|
|
get_deepep_mode,
|
|
get_moe_a2a_backend,
|
|
get_moe_runner_backend,
|
|
)
|
|
from sglang.srt.layers.moe.fused_moe_triton.layer import (
|
|
FusedMoE,
|
|
moe_forward_piecewise_cuda_graph_impl,
|
|
)
|
|
from sglang.srt.layers.moe.token_dispatcher.deepep import (
|
|
DeepEPLLCombineInput,
|
|
DeepEPNormalCombineInput,
|
|
)
|
|
from sglang.srt.layers.moe.topk import TopKOutput, TopKOutputChecker
|
|
from sglang.srt.layers.quantization.base_config import QuantizationConfig
|
|
from sglang.srt.layers.quantization.fp8 import Fp8Config
|
|
from sglang.srt.layers.quantization.fp8_kernel import is_fp8_fnuz
|
|
from sglang.srt.layers.quantization.w4afp8 import W4AFp8Config, W4AFp8MoEMethod
|
|
from sglang.srt.model_executor.runner_backend_utils.tc_piecewise_cuda_graph import (
|
|
is_in_tc_piecewise_cuda_graph,
|
|
)
|
|
from sglang.srt.utils import get_bool_env_var, is_hip, is_npu
|
|
|
|
if TYPE_CHECKING:
|
|
from sglang.srt.layers.moe.token_dispatcher import (
|
|
DeepEPLLDispatchOutput,
|
|
DeepEPNormalDispatchOutput,
|
|
DispatchOutput,
|
|
)
|
|
|
|
_is_hip = is_hip()
|
|
_is_npu = is_npu()
|
|
_is_fp8_fnuz = is_fp8_fnuz()
|
|
_use_aiter = get_bool_env_var("SGLANG_USE_AITER") and _is_hip
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class DeepEPMoE(FusedMoE):
|
|
"""
|
|
MoE Expert Parallel Impl based on DeepEP (https://github.com/deepseek-ai/DeepEP/tree/main)
|
|
Mooncake EP shares the same class, as they expose the same interface.
|
|
"""
|
|
|
|
_has_printed = False
|
|
|
|
def __init__(
|
|
self,
|
|
num_experts: int,
|
|
top_k: int,
|
|
hidden_size: int,
|
|
intermediate_size: int,
|
|
layer_id: int,
|
|
num_fused_shared_experts: int = 0,
|
|
params_dtype: Optional[torch.dtype] = None,
|
|
quant_config: Optional[QuantizationConfig] = None,
|
|
prefix: str = "",
|
|
activation: str = "silu",
|
|
routed_scaling_factor: Optional[float] = None,
|
|
**kwargs,
|
|
):
|
|
super().__init__(
|
|
num_experts=num_experts,
|
|
top_k=top_k,
|
|
hidden_size=hidden_size,
|
|
intermediate_size=intermediate_size,
|
|
layer_id=layer_id,
|
|
num_fused_shared_experts=num_fused_shared_experts,
|
|
params_dtype=params_dtype,
|
|
quant_config=quant_config,
|
|
prefix=prefix,
|
|
activation=activation,
|
|
routed_scaling_factor=routed_scaling_factor,
|
|
**kwargs,
|
|
)
|
|
if _use_aiter:
|
|
self.deprecate_flag = True
|
|
elif _is_npu:
|
|
self.deprecate_flag = True
|
|
elif deep_gemm_wrapper.ENABLE_JIT_DEEPGEMM and isinstance(
|
|
quant_config, Fp8Config
|
|
):
|
|
self.deprecate_flag = True
|
|
elif (
|
|
deep_gemm_wrapper.ENABLE_JIT_DEEPGEMM
|
|
and envs.SGLANG_DEEPEP_BF16_DISPATCH.get()
|
|
):
|
|
self.deprecate_flag = True
|
|
elif (
|
|
get_moe_runner_backend().is_flashinfer_cutedsl()
|
|
and quant_config is not None
|
|
and quant_config.get_name() in ("modelopt_fp4", "modelopt_mixed")
|
|
):
|
|
self.deprecate_flag = True
|
|
elif (
|
|
quant_config is None
|
|
and self.w13_weight.dtype == torch.bfloat16
|
|
and get_moe_runner_backend().is_deep_gemm()
|
|
and get_moe_a2a_backend().is_deepep()
|
|
and get_deepep_mode().enable_low_latency()
|
|
and not _is_npu
|
|
and not _is_hip
|
|
):
|
|
assert (
|
|
deep_gemm_wrapper.ENABLE_JIT_DEEPGEMM
|
|
), "Unquantized DeepEP low-latency MoE requires DeepGEMM BF16"
|
|
self.deprecate_flag = True
|
|
else:
|
|
self.deprecate_flag = False
|
|
|
|
if self.deprecate_flag:
|
|
return
|
|
|
|
if isinstance(quant_config, Fp8Config):
|
|
self.use_block_quant = getattr(self.quant_method, "block_quant", False)
|
|
self.use_fp8_w8a8 = True
|
|
self.fp8_dtype = torch.float8_e4m3fn
|
|
self.use_w4afp8 = False
|
|
elif isinstance(quant_config, W4AFp8Config):
|
|
self.use_w4afp8 = True
|
|
self.use_fp8_w8a8 = False
|
|
self.use_block_quant = False
|
|
else:
|
|
self.use_w4afp8 = False
|
|
self.use_fp8_w8a8 = False
|
|
self.use_block_quant = False
|
|
|
|
self.deepep_mode = get_deepep_mode()
|
|
if (
|
|
self.deepep_mode.enable_low_latency()
|
|
and not _is_npu
|
|
and not _is_hip
|
|
and quant_config is not None
|
|
):
|
|
# AMD HIP and NPU support low_latency DeepEP without DeepGEMM.
|
|
assert (
|
|
deep_gemm_wrapper.ENABLE_JIT_DEEPGEMM
|
|
), f"DeepEP {self.deepep_mode} mode requires deep_gemm"
|
|
|
|
def forward(
|
|
self,
|
|
hidden_states: torch.Tensor,
|
|
topk_output: TopKOutput,
|
|
):
|
|
if is_in_tc_piecewise_cuda_graph():
|
|
assert TopKOutputChecker.format_is_standard(
|
|
topk_output
|
|
), "Only standard topk output is supported for piecewise cuda graph"
|
|
return moe_forward_piecewise_cuda_graph_impl(
|
|
hidden_states,
|
|
topk_output.topk_weights,
|
|
topk_output.topk_ids,
|
|
topk_output.router_logits,
|
|
self.layer_id,
|
|
)
|
|
else:
|
|
return self.forward_impl(hidden_states, topk_output)
|
|
|
|
def forward_impl(
|
|
self,
|
|
hidden_states: torch.Tensor,
|
|
topk_output: TopKOutput,
|
|
):
|
|
|
|
if self.deprecate_flag:
|
|
return super().forward_impl(
|
|
hidden_states,
|
|
topk_output,
|
|
)
|
|
|
|
dispatch_output = self.dispatcher.dispatch(
|
|
hidden_states=hidden_states, topk_output=topk_output
|
|
)
|
|
combine_input = self.run_moe_core(dispatch_output)
|
|
return self.dispatcher.combine(combine_input=combine_input)
|
|
|
|
def dispatch(
|
|
self,
|
|
hidden_states: torch.Tensor,
|
|
topk_output: TopKOutput,
|
|
):
|
|
return self.dispatcher.dispatch(
|
|
hidden_states=hidden_states,
|
|
topk_output=topk_output,
|
|
)
|
|
|
|
def run_moe_core(
|
|
self,
|
|
dispatch_output: DispatchOutput,
|
|
):
|
|
|
|
if self.deprecate_flag:
|
|
return super().run_moe_core(dispatch_output)
|
|
|
|
from sglang.srt.layers.moe.token_dispatcher import DispatchOutputChecker
|
|
|
|
if DispatchOutputChecker.format_is_deepep_normal(dispatch_output):
|
|
if self.quant_config is None:
|
|
raise NotImplementedError(
|
|
"Unquantized DeepEP MoE currently supports low_latency mode only"
|
|
)
|
|
elif self.use_w4afp8:
|
|
output = self.forward_cutlass_w4afp8(dispatch_output)
|
|
else:
|
|
assert False, "forward_deepgemm_contiguous is deprecated"
|
|
elif DispatchOutputChecker.format_is_deepep_ll(dispatch_output):
|
|
if self.use_w4afp8:
|
|
output = self.forward_cutlass_w4afp8_masked(dispatch_output)
|
|
else:
|
|
assert False, "forward_deepgemm_masked is deprecated"
|
|
|
|
combine_input_wrapper = (
|
|
DeepEPNormalCombineInput
|
|
if DispatchOutputChecker.format_is_deepep_normal(dispatch_output)
|
|
else DeepEPLLCombineInput
|
|
)
|
|
|
|
return combine_input_wrapper(
|
|
hidden_states=output,
|
|
topk_ids=dispatch_output.topk_ids,
|
|
topk_weights=dispatch_output.topk_weights,
|
|
)
|
|
|
|
def combine(
|
|
self,
|
|
hidden_states: torch.Tensor,
|
|
topk_ids: torch.Tensor,
|
|
topk_weights: torch.Tensor,
|
|
overlap_args: Optional[Dict[str, Any]] = None,
|
|
):
|
|
return self.dispatcher.combine(
|
|
hidden_states=hidden_states,
|
|
topk_ids=topk_ids,
|
|
topk_weights=topk_weights,
|
|
overlap_args=overlap_args,
|
|
)
|
|
|
|
def forward_cutlass_w4afp8(
|
|
self,
|
|
dispatch_output: DeepEPNormalDispatchOutput,
|
|
):
|
|
assert self.moe_runner_config.activation == "silu"
|
|
assert isinstance(self.quant_method, W4AFp8MoEMethod)
|
|
return self.quant_method.apply_deepep_normal(
|
|
layer=self,
|
|
dispatch_output=dispatch_output,
|
|
)
|
|
|
|
def forward_cutlass_w4afp8_masked(
|
|
self,
|
|
dispatch_output: DeepEPLLDispatchOutput,
|
|
):
|
|
assert self.moe_runner_config.activation == "silu"
|
|
assert isinstance(self.quant_method, W4AFp8MoEMethod)
|
|
return self.quant_method.apply_deepep_ll(
|
|
layer=self,
|
|
dispatch_output=dispatch_output,
|
|
)
|
|
|
|
|
|
def get_moe_impl_class(quant_config: Optional[QuantizationConfig]):
|
|
# [TODO] kk, temporary solution
|
|
if (
|
|
get_moe_a2a_backend().is_mori()
|
|
or get_moe_a2a_backend().is_deepep()
|
|
or get_moe_a2a_backend().is_mooncake()
|
|
or get_moe_a2a_backend().is_nixl()
|
|
):
|
|
return DeepEPMoE
|
|
if get_moe_a2a_backend().is_ascend_fuseep():
|
|
# ascend_fuseep bypasses dispatch/combine inside FusedMoE.forward
|
|
# (see forward_fuseep in hardware_backend/npu/moe/fuseep.py).
|
|
return FusedMoE
|
|
|
|
return FusedMoE
|