Mixture of experts

xffnx_{\mathrm{ffn}} [B, T, 6144]to Δffn\Delta_{\mathrm{ffn}} [B, T, 6144]

Parameters9,702,998,272 × 75 layers

9,702,998,272 parameters per instance, across its learned components

ComponentCalculationParameters
Router1,573,120
Routed expert37,748,736 × 2569,663,676,416
Shared expert37,748,736
Per-instance total9,702,998,272

One in each of the 75 MoE layers, each with its own weights.

Weights · one MoE layer

Held · bfloat1618.1 GiB
Used per token · bfloat16651 MiB

A token uses 8 of the 256 routed experts, the shared expert and the router: 341,311,744 parameters (3.5%).

bfloat16 experts; the router’s matrix in bfloat16 and its bias in float32.

config.json 7modeling_glm_moe_dsa.py 487–492, 647, 535–559, 576–583
Explanation

The implementation flattens batch and positions into one token axis for the experts and restores the shape (modeling_glm_moe_dsa.py 580–581).

Sources

In codeself.mlp

Open pinned code ↗

Direct evidence

The MoE layer holds the routed experts, the router and one shared expert MLP of n_shared_experts × moe_intermediate_size features.

modeling_glm_moe_dsa.py 562–574 ↗ · huggingface/transformers @ 5e4d6304de55

Excerpt
class GlmMoeDsaMoE(nn.Module):
    """
    A mixed expert module containing shared experts.
    """

    def __init__(self, config: GlmMoeDsaConfig):
        super().__init__()
        self.config = config
        self.experts = GlmMoeDsaExperts(config)
        self.gate = GlmMoeDsaTopkRouter(config)
        self.shared_experts = GlmMoeDsaMLP(
            config=config, intermediate_size=config.moe_intermediate_size * config.n_shared_experts
        )

Each token’s output is the router-weighted sum of its selected experts plus the shared expert applied to every token.

modeling_glm_moe_dsa.py 535–583 ↗ · huggingface/transformers @ 5e4d6304de55

Excerpt
    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

… lines 560–575 omitted …

    def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
        residuals = hidden_states
        orig_shape = hidden_states.shape
        _, topk_weights, topk_indices = self.gate(hidden_states)
        hidden_states = hidden_states.view(-1, hidden_states.shape[-1])
        hidden_states = self.experts(hidden_states, topk_indices, topk_weights).view(*orig_shape)
        hidden_states = hidden_states + self.shared_experts(residuals)
        return hidden_states