Add MoE update
×Add the mechanism’s update to the residual stream element by element.
- in
- [B batch, T positions, 2880 d] [B batch, T positions, 2880 d]
- out
- [B batch, T positions, 2880 d]
Sources
In codex + t · 336
Direct evidence
The MLP block’s forward: normalise; score the experts; take the 4 highest scores and their experts; softmax over those 4; run each chosen expert’s first matrix and bias, the activation, its second matrix and bias; sum the 4 outputs by their weights; add to the block’s input.
model.py 312–336 ↗ · openai/gpt-oss @ 5681a6b65526
Excerpt
def forward(self, x: torch.Tensor) -> torch.Tensor:
t = self.norm(x)
g = self.gate(t)
experts = torch.topk(g, k=self.experts_per_token, dim=-1, sorted=True)
expert_weights = torch.nn.functional.softmax(experts.values, dim=1)
expert_indices = experts.indices
# MLP #1
mlp1_weight = self.mlp1_weight[expert_indices, ...]
mlp1_bias = self.mlp1_bias[expert_indices, ...]
t = torch.einsum("beck,bk->bec", mlp1_weight, t) + mlp1_bias
t = swiglu(t, limit=self.swiglu_limit)
# MLP #2
mlp2_weight = self.mlp2_weight[expert_indices, ...]
mlp2_bias = self.mlp2_bias[expert_indices, ...]
t = torch.einsum("beck,bek->bec", mlp2_weight, t)
if self.world_size > 1:
dist.all_reduce(t, op=dist.ReduceOp.SUM)
t += mlp2_bias
# Weighted sum of experts
t = torch.einsum("bec,be->bc", t, expert_weights)
return x + t