Shared expert
×One SwiGLU MLP of 2,048 hidden features, applied to every position.
All 5 steps
- in
- [B batch, 1 position, 8192 d]
- out
- [B batch, 1 position, 8192 d]
Parameters50,331,648 × 23 layers
50,331,648 parameters per instance, across its learned components
| Component | Calculation | Parameters |
|---|---|---|
| Gate projection | 8,192 × 2,048 | 16,777,216 |
| Up projection | 8,192 × 2,048 | 16,777,216 |
| Down projection | 2,048 × 8,192 | 16,777,216 |
| Per-instance total | 50,331,648 | |
One in each of the 23 attention layers, each with its own weights.
Explanation
Its output is scaled by a sigmoid gate computed from the MoE’s input before it joins the routed output (modeling_qwen3_5_moe.py 905).
Sources
In codeself.shared_expert · Qwen3_5MoeMLP
Direct evidence
The shared expert is a SwiGLU MLP: down(SiLU(gate x) · up x).
modeling_qwen3_5_moe.py 814–827 ↗ · huggingface/transformers @ 18329f106538
Excerpt
class Qwen3_5MoeMLP(nn.Module):
def __init__(self, config: Qwen3_5MoeConfig, intermediate_size: int):
super().__init__()
self.config = config
self.hidden_size = config.hidden_size
self.intermediate_size = intermediate_size
self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=False)
self.act_fn = ACT2FN[config.hidden_act]
def forward(self, x):
down_proj = self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
return down_projThe MoE block holds the router, the experts, a shared expert of shared_expert_intermediate_size and a one-output shared-expert gate; the shared expert’s output is multiplied by the sigmoid of that gate and added to the experts’ output.
modeling_qwen3_5_moe.py 890–909 ↗ · huggingface/transformers @ 18329f106538
Excerpt
class Qwen3_5MoeSparseMoeBlock(nn.Module):
def __init__(self, config):
super().__init__()
self.gate = Qwen3_5MoeTopKRouter(config)
self.experts = Qwen3_5MoeExperts(config)
self.shared_expert = Qwen3_5MoeMLP(config, intermediate_size=config.shared_expert_intermediate_size)
self.shared_expert_gate = torch.nn.Linear(config.hidden_size, 1, bias=False)
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
batch_size, sequence_length, hidden_dim = hidden_states.shape
hidden_states_reshaped = hidden_states.view(-1, hidden_dim)
shared_expert_output = self.shared_expert(hidden_states_reshaped)
_, routing_weights, selected_experts = self.gate(hidden_states_reshaped)
expert_output = self.experts(hidden_states_reshaped, selected_experts, routing_weights)
shared_expert_output = F.sigmoid(self.shared_expert_gate(hidden_states_reshaped)) * shared_expert_output
expert_output = expert_output + shared_expert_output
expert_output = expert_output.reshape(batch_size, sequence_length, hidden_dim)
return expert_output