Routed expert

Gate projection

×

Map 8,192 input features to 2,048 gate features.

g=xffnWgate,i{\htmlData{light=g gate}{g}}={\htmlData{light=x}{x_{\mathrm{ffn}}}}W_{\mathrm{gate},i}
in
xffnx_{\mathrm{ffn}} [B batch, 1 position, 8192 d]
out
gg [B batch, 1 position, 2048 expert d_ff]

Gate and up are the two halves of one stored matrix per expert, gate_up_proj, split by chunk (modeling_qwen3_5_moe.py 861).

Parameters16,777,216 × 35,328 experts

16,777,216 parameters per instance.

ComponentCalculationParameters
Weight matrix8,192 × 2,04816,777,216

This projection has no bias parameter.

The reference module holds the weight output by input, [out, in].

One in each of 35,328 experts: 512 in each of 69 GDN layers.

Sources

In codegate_up_proj[expert_idx] (first half)

Open pinned code ↗

Direct evidence

The routed experts are stored as two 3-D tensors, gate_up_proj (experts × 2·2,048 × width) and down_proj (experts × width × 2,048); each selected expert runs SwiGLU on its tokens, scaled by its routing weight, and the results are summed.

modeling_qwen3_5_moe.py 830–867 ↗ · huggingface/transformers @ 18329f106538

Excerpt
@use_experts_implementation
class Qwen3_5MoeExperts(nn.Module):
    """Collection of expert weights stored as 3D tensors."""

    def __init__(self, config):
        super().__init__()
        self.num_experts = config.num_experts
        self.hidden_dim = config.hidden_size
        self.intermediate_dim = config.moe_intermediate_size
        self.gate_up_proj = nn.Parameter(torch.empty(self.num_experts, 2 * self.intermediate_dim, self.hidden_dim))
        self.down_proj = nn.Parameter(torch.empty(self.num_experts, self.hidden_dim, self.intermediate_dim))
        self.act_fn = ACT2FN[config.hidden_act]

    def forward(
        self,
        hidden_states: torch.Tensor,
        top_k_index: torch.Tensor,
        top_k_weights: torch.Tensor,
    ) -> torch.Tensor:
        final_hidden_states = torch.zeros_like(hidden_states)
        with torch.no_grad():
            expert_mask = torch.nn.functional.one_hot(top_k_index, num_classes=self.num_experts + 1)
            expert_mask = expert_mask.permute(2, 1, 0)
            expert_hit = torch.greater(expert_mask.sum(dim=(-1, -2)), 0).nonzero()

        for expert_idx in expert_hit:
            expert_idx = expert_idx[0]
            if expert_idx == self.num_experts:
                continue
            top_k_pos, token_idx = torch.where(expert_mask[expert_idx])
            current_state = hidden_states[token_idx]
            gate, up = nn.functional.linear(current_state, self.gate_up_proj[expert_idx]).chunk(2, dim=-1)
            current_hidden_states = self.act_fn(gate) * up
            current_hidden_states = nn.functional.linear(current_hidden_states, self.down_proj[expert_idx])
            current_hidden_states = current_hidden_states * top_k_weights[token_idx, top_k_pos, None]
            final_hidden_states.index_add_(0, token_idx, current_hidden_states.to(final_hidden_states.dtype))

        return final_hidden_states