class BeamSparseMoeBlock(nn.Module):
"""Grouped sigmoid ``noaux_tc`` MoE block."""
def __init__(self, vllm_config: VllmConfig, prefix: str = "") -> None:
super().__init__()
config = vllm_config.model_config.hf_config
parallel_config = vllm_config.parallel_config
quant_config = vllm_config.quant_config
self.gate = GateLinear(
config.hidden_size,
config.num_experts,
bias=False,
params_dtype=torch.float32,
out_dtype=torch.float32,
prefix=f"{prefix}.gate",
)
e_score_correction_bias = nn.Parameter(
torch.zeros(config.num_experts, dtype=torch.float32)
)
self.gate.register_parameter("e_score_correction_bias", e_score_correction_bias)
self.experts = FusedMoEFactory(
gate=self.gate,
num_experts=config.num_experts,
top_k=config.num_experts_per_tok,
hidden_size=config.hidden_size,
intermediate_size=config.moe_intermediate_size,
renormalize=config.norm_topk_prob,
quant_config=quant_config,
use_grouped_topk=(
config.n_group is not None and config.topk_group is not None
),
num_expert_group=config.n_group,
topk_group=config.topk_group,
scoring_func="sigmoid",
e_score_correction_bias=e_score_correction_bias,
routed_scaling_factor=config.routed_scaling_factor,
apply_routed_scale_to_output=True,
router_logits_dtype=self.gate.out_dtype,
enable_eplb=parallel_config.enable_eplb,
num_redundant_experts=parallel_config.eplb_config.num_redundant_experts,
prefix=f"{prefix}.experts",
)
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
return self.experts(hidden_states=hidden_states, router_logits=hidden_states)