Mixture of experts

Add shared output

×

Add the shared expert’s output to the routed output.

Δffn=yrouted+yshared{\htmlData{light=output add}{\Delta_{\mathrm{ffn}}}}={\htmlData{light=routed combine}{y^{\mathrm{routed}}}}+{\htmlData{light=shared shared-expert}{y^{\mathrm{shared}}}}
in
yroutedy^{\mathrm{routed}} [B batch, T positions, 7168 d]ysharedy^{\mathrm{shared}} [B batch, T positions, 7168 d]
out
Δffn\Delta_{\mathrm{ffn}} [B batch, T positions, 7168 d]
Sources

In codeshared_output + fused_output

Open pinned code ↗

Direct evidence

The MoE layer flattens positions into one token axis, hands them to the fused experts, which also run the router, and restores the shape.

deepseek_v2.py 411–434 ↗ · vllm-project/vllm @ 5df63a7203cd

Excerpt
    def forward(
        self,
        hidden_states: torch.Tensor,
        already_sequence_parallel: bool = False,
    ) -> torch.Tensor:
        num_tokens, hidden_dim = hidden_states.shape
        hidden_states = hidden_states.view(-1, hidden_dim)

        # Chunk the hidden states so they aren't replicated across TP ranks.
        # This avoids duplicate computation in self.experts.
        if self.is_sequence_parallel and not already_sequence_parallel:
            hidden_states = sequence_parallel_chunk(hidden_states)

        final_hidden_states = self.experts(
            hidden_states=hidden_states, router_logits=hidden_states
        )

        if self.is_sequence_parallel and not already_sequence_parallel:
            final_hidden_states = tensor_model_parallel_all_gather(
                final_hidden_states, 0
            )
            final_hidden_states = final_hidden_states[:num_tokens]

        return final_hidden_states.view(num_tokens, hidden_dim)