Routed expert

Gate projection

×

Map 6,144 input features to 2,048 gate features.

g=xffnW1{\htmlData{light=g gate}{g}}={\htmlData{light=x}{x_{\mathrm{ffn}}}}W_{1}
in
xffnx_{\mathrm{ffn}} [B batch, T positions, 6144 d]
out
gg [B batch, T positions, 2048 expert d_ff]

Gate and up are the two halves of one stacked parameter per expert, gate_up_proj, held output by input (modeling_glm_moe_dsa.py 520, 542).

Parameters12,582,912 × 13,824 experts

12,582,912 parameters per instance.

ComponentCalculationParameters
Weight matrix6,144 × 2,04812,582,912

This projection has no bias parameter.

One in each of 13,824 experts: 256 in each of 3 sharing MoE layers in each of 18 index groups.

Sources

In codenn.functional.linear(current_state, self.gate_up_proj[expert_idx])

Open pinned code ↗

Direct evidence

The implementation holds the routed experts as stacked tensors, the gate and up projections fused into one; each expert is a SwiGLU of moe_intermediate_size hidden features applied to the tokens routed to it and weighted by its routing weight.

modeling_glm_moe_dsa.py 511–548 ↗ · huggingface/transformers @ c4c4605f9618

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

    def __init__(self, config):
        super().__init__()
        self.num_experts = config.num_local_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

Attention projections have no bias (attention_bias is false). The checkpoint stores its matrices in FP8 (e4m3) with one scale per 128 × 128 block, scaled dynamically per token at run time; the modules listed under modules_to_not_convert (the norms, the routers and their correction biases, the indexer’s key norm and head-weight projection, the embedding and the output head) are not converted.

config.json 5–233 ↗ · zai-org/GLM-5.3 @ ba32f8c19c87

Excerpt
  "attention_bias": false,
  "attention_dropout": 0.0,
  "dtype": "bfloat16",

… lines 8–223 omitted …

  "quantization_config": {
    "activation_scheme": "dynamic",
    "fmt": "e4m3",
    "quant_method": "fp8",
    "weight_block_size": [
      128,
      128
    ],
    "modules_to_not_convert": [
      "model.layers.59.post_attention_layernorm",