Weighted expert sum
×Sum the selected experts’ outputs, each scaled by its routing weight.
- in
- [B batch, T positions, 128 experts] [B batch, T positions, 7168 d]
- out
- [B batch, T positions, 7168 d]
Each token’s outputs of its experts are multiplied by their routing weights and summed (topk_weight_and_reduce.py 104–117); fused-MoE kernels can fold this step into the expert kernel.
Sources
In codefused_expert_output.mul_(topk_weights.view(m, -1, 1)); ops.moe_sum(fused_expert_output, output)
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)