+6








35870d55ac
Co-authored-by: Baizhou Zhang <sobereddiezhang@gmail.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Co-authored-by: fzyzcjy <ch271828n@outlook.com> Co-authored-by: ispobock <ispobaoke@gmail.com> Co-authored-by: Zhiqiang Xie <xiezhq@stanford.edu> Co-authored-by: yueming-yuan <yym022502@gmail.com> Co-authored-by: DarkSharpness <2040703891@qq.com> Co-authored-by: Yuhao Yang <47235274+yhyang201@users.noreply.github.com> Co-authored-by: yhyang201 <yhyang201@users.noreply.github.com> Co-authored-by: yhyang201 <yhyang201@gmail.com> Co-authored-by: Qiaolin Yu <90088090+qiaolin-yu@users.noreply.github.com> Co-authored-by: Ethan (Yusheng) Su <11704492+yushengsu-thu@users.noreply.github.com> Co-authored-by: Mingyi <27337995+wisclmy0611@users.noreply.github.com> Co-authored-by: Cheng Wan <54331508+ch-wan@users.noreply.github.com> Co-authored-by: Yihao Wang <42559837+againstentropy@users.noreply.github.com>
83 lines
2.2 KiB
Python
83 lines
2.2 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import TYPE_CHECKING, Tuple
|
|
|
|
import torch
|
|
|
|
from sglang.jit_kernel.utils import cache_once, load_jit
|
|
|
|
if TYPE_CHECKING:
|
|
from tvm_ffi.module import Module
|
|
|
|
|
|
_SCORING_FUNC_MAP = {
|
|
"sigmoid": 0,
|
|
"sqrtsoftplus": 1,
|
|
}
|
|
|
|
|
|
@cache_once
|
|
def _jit_moe_fused_gate_module() -> Module:
|
|
return load_jit(
|
|
"moe_fused_gate",
|
|
cuda_files=["moe/moe_fused_gate.cuh"],
|
|
cuda_wrappers=[("moe_fused_gate", "MoEFusedGateKernel::run")],
|
|
)
|
|
|
|
|
|
@cache_once
|
|
def can_use_moe_fused_gate() -> bool:
|
|
logger = logging.getLogger(__name__)
|
|
try:
|
|
_jit_moe_fused_gate_module()
|
|
return True
|
|
except Exception as e:
|
|
logger.warning(f"Failed to load JIT MoE fused gate kernel: {e}")
|
|
return False
|
|
|
|
|
|
def moe_fused_gate(
|
|
input: torch.Tensor,
|
|
bias: torch.Tensor,
|
|
topk: int,
|
|
scoring_func: str = "sigmoid",
|
|
num_fused_shared_experts: int = 0,
|
|
renormalize: bool = True,
|
|
routed_scaling_factor: float = 1.0,
|
|
apply_routed_scaling_factor_on_output: bool = False,
|
|
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
scoring_func_int = _SCORING_FUNC_MAP.get(scoring_func.lower())
|
|
assert (
|
|
scoring_func_int is not None
|
|
), f"Unknown scoring_func '{scoring_func}', must be one of {list(_SCORING_FUNC_MAP.keys())}"
|
|
|
|
assert input.dtype == torch.float32, "input must be float32"
|
|
assert bias.dtype == torch.float32, "bias must be float32"
|
|
assert input.ndim == 2, "input must be 2D"
|
|
assert bias.ndim == 1, "bias must be 1D"
|
|
assert input.size(1) == bias.size(0), "input and bias must have same num_experts"
|
|
assert topk > num_fused_shared_experts, "topk must be > num_fused_shared_experts"
|
|
|
|
num_rows, _ = input.shape
|
|
device = input.device
|
|
|
|
output = torch.empty(num_rows, topk, dtype=torch.float32, device=device)
|
|
indices = torch.empty(num_rows, topk, dtype=torch.int32, device=device)
|
|
|
|
module = _jit_moe_fused_gate_module()
|
|
module.moe_fused_gate(
|
|
input,
|
|
bias,
|
|
output,
|
|
indices,
|
|
topk,
|
|
scoring_func_int,
|
|
num_fused_shared_experts,
|
|
renormalize,
|
|
routed_scaling_factor,
|
|
apply_routed_scaling_factor_on_output,
|
|
)
|
|
|
|
return output, indices
|