Expert scores
×One learned score vector per expert: 512 scores per position.
- in
- [B batch, T positions, 8192 d]
- out
- [B batch, T positions, 512 experts]
Parameters4,194,304 × 69 layers
4,194,304 parameters per instance.
| Component | Calculation | Parameters |
|---|---|---|
| Weight matrix | 8,192 × 512 | 4,194,304 |
This projection has no bias parameter.
The reference module holds the weight output by input, [out, in].
One in each of the 69 GDN layers, each with its own weights.
Sources
In codeF.linear(hidden_states, self.weight) · 881
Direct evidence
The router scores all experts with one weight matrix, takes the softmax in float32, keeps the top 10 and divides them by their sum.
modeling_qwen3_5_moe.py 870–887 ↗ · huggingface/transformers @ 18329f106538
Excerpt
@use_kernel_forward_from_hub("SoftmaxTopKRouter")
class Qwen3_5MoeTopKRouter(nn.Module):
def __init__(self, config):
super().__init__()
self.top_k = config.num_experts_per_tok
self.num_experts = config.num_experts
self.hidden_dim = config.hidden_size
self.weight = nn.Parameter(torch.zeros(self.num_experts, self.hidden_dim))
def forward(self, hidden_states):
hidden_states = hidden_states.reshape(-1, self.hidden_dim)
router_logits = F.linear(hidden_states, self.weight) # (seq_len, num_experts)
router_probs = torch.nn.functional.softmax(router_logits, dtype=torch.float, dim=-1)
router_top_value, router_indices = torch.topk(router_probs, self.top_k, dim=-1) # (seq_len, top_k)
router_top_value /= router_top_value.sum(dim=-1, keepdim=True)
router_top_value = router_top_value.to(router_logits.dtype)
router_scores = router_top_value
return router_logits, router_scores, router_indicesThe 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