Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def fused_moe_triton_api(
):
topk_op = TopK(
top_k=topk,
layer_id=0,
renormalize=False,
use_grouped_topk=False,
output_format=TopKOutputFormat.TRITON_KERNEL,
Expand Down
4 changes: 3 additions & 1 deletion python/sglang/srt/layers/moe/topk.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,8 @@ class TopK(BaseFusedOp):
"""
Parameters:
--top_k: The all number of top experts selected per token, including the fused shared expert(s).
--layer_id: global index of the owning layer. Required: routed-experts capture and
per-layer EPLB state are keyed by it.
--num_fused_shared_experts: num of shared experts, can be activate both in TP or EP mode.
--routed_scaling_factor: the scaling factor for routed experts in topk_weights.
--fused_shared_experts_scaling_factor: scaling factor applied to the fused shared experts'
Expand All @@ -547,7 +549,7 @@ def __init__(
self,
top_k: int,
*,
layer_id: Optional[int] = None,
layer_id: int,
use_grouped_topk: bool = False,
topk_group: Optional[int] = None,
num_expert_group: Optional[int] = None,
Expand Down
3 changes: 3 additions & 0 deletions python/sglang/srt/models/afmoe.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def _custom_routing_function(
def __init__(
self,
config: PretrainedConfig,
layer_id: int,
quant_config: Optional[QuantizationConfig] = None,
prefix: str = "",
):
Expand Down Expand Up @@ -234,6 +235,7 @@ def __init__(
renormalize = self.route_norm if self.score_func == "sigmoid" else False
self.topk = TopK(
top_k=self.top_k,
layer_id=layer_id,
renormalize=renormalize,
use_grouped_topk=self.use_grouped_topk,
num_expert_group=self.n_group if self.use_grouped_topk else None,
Expand Down Expand Up @@ -455,6 +457,7 @@ def __init__(
if use_moe:
self.mlp = AfmoeMoE(
config=config,
layer_id=layer_id,
quant_config=quant_config,
prefix=add_prefix("mlp", prefix),
)
Expand Down
1 change: 1 addition & 0 deletions python/sglang/srt/models/bailing_moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ def __init__(

self.topk = TopK(
top_k=self.top_k,
layer_id=self.layer_id,
renormalize=self.norm_topk_prob,
use_grouped_topk=self.use_grouped_topk,
num_expert_group=self.num_expert_group,
Expand Down
1 change: 1 addition & 0 deletions python/sglang/srt/models/bailing_moe_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ def __init__(

self.topk = TopK(
top_k=self.top_k,
layer_id=self.layer_id,
use_grouped_topk=self.use_grouped_topk,
renormalize=self.norm_expert_prob,
num_expert_group=self.num_expert_group,
Expand Down
4 changes: 3 additions & 1 deletion python/sglang/srt/models/dbrx.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class DbrxExperts(nn.Module):
def __init__(
self,
config: DbrxConfig,
layer_id: int,
quant_config: Optional[QuantizationConfig] = None,
params_dtype: Optional[torch.dtype] = None,
prefix: str = "",
Expand All @@ -115,6 +116,7 @@ def __init__(
self.router = DbrxRouter(config, self.params_dtype)
self.topk = TopK(
self.top_k,
layer_id=layer_id,
renormalize=True,
)
self.moe_runner_config = MoeRunnerConfig(inplace=True)
Expand Down Expand Up @@ -336,7 +338,7 @@ def __init__(
quant_config=quant_config,
prefix=add_prefix("norm_attn_norm", prefix),
)
self.ffn = DbrxExperts(config, quant_config=quant_config)
self.ffn = DbrxExperts(config, layer_id, quant_config=quant_config)

def forward(
self,
Expand Down
3 changes: 3 additions & 0 deletions python/sglang/srt/models/deepseek.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class DeepseekMoE(nn.Module):
def __init__(
self,
config: PretrainedConfig,
layer_id: int,
quant_config: Optional[QuantizationConfig] = None,
prefix: str = "",
):
Expand All @@ -125,6 +126,7 @@ def __init__(
)
self.topk = TopK(
top_k=self.top_k,
layer_id=layer_id,
renormalize=config.norm_topk_prob,
)
self.experts = nn.ModuleList(
Expand Down Expand Up @@ -339,6 +341,7 @@ def __init__(
):
self.mlp = DeepseekMoE(
config=config,
layer_id=layer_id,
quant_config=quant_config,
prefix=add_prefix("mlp", prefix),
)
Expand Down
2 changes: 2 additions & 0 deletions python/sglang/srt/models/ernie45_moe_vl.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ def __init__(

self.text_experts_topk = TopK(
top_k=config.moe_k,
layer_id=self.layer_id,
renormalize=True,
use_grouped_topk=False,
correction_bias=self.e_score_correction_bias[0],
Expand Down Expand Up @@ -237,6 +238,7 @@ def __init__(

self.vision_experts_topk = TopK(
top_k=config.moe_k,
layer_id=self.layer_id,
renormalize=True,
use_grouped_topk=False,
correction_bias=self.e_score_correction_bias[1],
Expand Down
1 change: 1 addition & 0 deletions python/sglang/srt/models/exaone_moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ def __init__(

self.topk = TopK(
top_k=config.num_experts_per_tok,
layer_id=self.layer_id,
renormalize=config.norm_topk_prob,
use_grouped_topk=True,
num_expert_group=config.n_group,
Expand Down
1 change: 1 addition & 0 deletions python/sglang/srt/models/granitemoe.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ def __init__(

self.topk = TopK(
top_k=top_k,
layer_id=layer_id,
renormalize=True,
)

Expand Down
1 change: 1 addition & 0 deletions python/sglang/srt/models/hunyuan_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def __init__(
)
self.topk = TopK(
top_k=config.num_experts_per_tok,
layer_id=layer_id,
use_grouped_topk=True,
num_expert_group=1,
topk_group=1,
Expand Down
1 change: 1 addition & 0 deletions python/sglang/srt/models/kimi_k3.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ def __init__(

self.topk = TopK(
top_k=config.num_experts_per_token,
layer_id=self.layer_idx,
renormalize=moe_renormalize,
use_grouped_topk=True,
num_expert_group=config.num_expert_group,
Expand Down
1 change: 1 addition & 0 deletions python/sglang/srt/models/kimi_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def __init__(

self.topk = TopK(
top_k=config.num_experts_per_token,
layer_id=self.layer_idx,
renormalize=moe_renormalize,
use_grouped_topk=True,
num_expert_group=config.num_expert_group,
Expand Down
1 change: 1 addition & 0 deletions python/sglang/srt/models/llada2.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ def __init__(

self.topk = TopK(
top_k=self.top_k,
layer_id=self.layer_id,
renormalize=self.norm_topk_prob,
use_grouped_topk=self.use_grouped_topk,
num_expert_group=self.num_expert_group,
Expand Down
1 change: 1 addition & 0 deletions python/sglang/srt/models/llama4.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def __init__(

self.topk = TopK(
top_k=self.top_k,
layer_id=layer_id,
renormalize=False,
custom_routing_function=Llama4MoE.custom_routing_function,
)
Expand Down
1 change: 1 addition & 0 deletions python/sglang/srt/models/mimo_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ def __init__(

self.topk = TopK(
top_k=config.num_experts_per_tok,
layer_id=self.layer_id,
renormalize=config.norm_topk_prob,
use_grouped_topk=True,
num_expert_group=config.n_group,
Expand Down
1 change: 1 addition & 0 deletions python/sglang/srt/models/minimax_m2.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ def __init__(
)
self.topk = TopK(
top_k=config.num_experts_per_tok,
layer_id=layer_id,
renormalize=True,
scoring_func=config.scoring_func,
correction_bias=self.e_score_correction_bias,
Expand Down
1 change: 1 addition & 0 deletions python/sglang/srt/models/mixtral.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def __init__(

self.topk = TopK(
top_k=top_k,
layer_id=layer_id,
renormalize=True,
)

Expand Down
1 change: 1 addition & 0 deletions python/sglang/srt/models/nemotron_h.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ def __init__(
)
self.topk = TopK(
top_k=config.num_experts_per_tok,
layer_id=layer_idx,
use_grouped_topk=True,
topk_group=config.topk_group,
num_expert_group=config.n_group,
Expand Down
1 change: 1 addition & 0 deletions python/sglang/srt/models/olmoe.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def __init__(

self.topk = TopK(
top_k=top_k,
layer_id=layer_id,
renormalize=False,
)

Expand Down
1 change: 1 addition & 0 deletions python/sglang/srt/models/phimoe.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ def __init__(

self.topk = TopK(
top_k=top_k,
layer_id=layer_id,
renormalize=False,
custom_routing_function=phimoe_routing_function,
)
Expand Down
3 changes: 3 additions & 0 deletions python/sglang/srt/models/xverse_moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class XverseMoE(nn.Module):
def __init__(
self,
config: PretrainedConfig,
layer_id: int,
quant_config: Optional[QuantizationConfig] = None,
prefix: str = "",
):
Expand Down Expand Up @@ -134,6 +135,7 @@ def __init__(
)
self.topk = TopK(
top_k=self.top_k,
layer_id=layer_id,
renormalize=getattr(self.config, "norm_topk_prob", False),
)

Expand Down Expand Up @@ -305,6 +307,7 @@ def __init__(
if config.num_experts is not None:
self.mlp = XverseMoE(
config=config,
layer_id=layer_id,
quant_config=quant_config,
prefix=add_prefix("mlp", prefix),
)
Expand Down
1 change: 1 addition & 0 deletions test/registered/moe/test_triton_fused_moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def _test_case(self, m, n, k, e, topk, dtype):

topk_op = TopK(
top_k=topk,
layer_id=0,
renormalize=False,
use_grouped_topk=False,
)
Expand Down
Loading