Input features
×The normalized features entering this SwiGLU shared expert.
- shape
- [B batch, T positions, 8192 d]
Sources
Contextual evidence from MoE RMSNorm
The model’s RMSNorm is zero-centred: its weight is initialised to zeros and the normalised input is multiplied by (1 + weight), in float32.
modeling_qwen3_5_moe.py 912–927 ↗ · huggingface/transformers @ 18329f106538
Excerpt
@use_kernel_forward_from_hub("RMSNormZeroCentered")
class Qwen3_5MoeRMSNorm(nn.Module):
def __init__(self, dim: int, eps: float = 1e-6):
super().__init__()
self.eps = eps
self.weight = nn.Parameter(torch.zeros(dim))
def _norm(self, x):
return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
def forward(self, x):
output = self._norm(x.float())
# Llama does x.to(float16) * w whilst Qwen3_5Moe is (x * w).to(float16)
# See https://github.com/huggingface/transformers/pull/29402
output = output * (1.0 + self.weight.float())
return output.type_as(x)A layer is pre-norm: the token mixer reads the normalised input and its output is added to the residual; the MoE reads the normalised sum and its output is added again.
modeling_qwen3_5_moe.py 946–989 ↗ · huggingface/transformers @ 18329f106538
Excerpt
def forward(
self,
hidden_states: torch.Tensor,
position_embeddings: tuple[torch.Tensor, torch.Tensor],
attention_mask: torch.Tensor | None = None,
position_ids: torch.LongTensor | None = None,
past_key_values: Cache | None = None,
**kwargs: Unpack[FlashAttentionKwargs],
) -> torch.FloatTensor:
residual = hidden_states
hidden_states = self.input_layernorm(hidden_states)
# Token Mixer
if self.block_type == "linear_attention":
hidden_states = self.linear_attn(
hidden_states=hidden_states,
cache_params=past_key_values,
attention_mask=attention_mask,
**kwargs,
)
elif self.block_type == "full_attention":
# Self Attention
hidden_states, _ = self.self_attn(
hidden_states=hidden_states,
attention_mask=attention_mask,
position_ids=position_ids,
past_key_values=past_key_values,
position_embeddings=position_embeddings,
**kwargs,
)
hidden_states = residual + hidden_states
# Fully Connected
residual = hidden_states
hidden_states = self.post_attention_layernorm(hidden_states)
hidden_states = self.mlp(hidden_states)
# For the MoE layers, we need to unpack
if isinstance(hidden_states, tuple):
hidden_states, _ = hidden_states
hidden_states = residual + hidden_states
return hidden_statesContextual evidence from Gate projection
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_proj