Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -780,13 +780,14 @@ def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
layer_type="moe",
)

# Single merge-point all_reduce for routed + shared partial sums.
# Both branches produce per-rank partial outputs under TP/EP sharding
# (routed: MoEShardableNode; shared: rowwise down_proj inside the MLP).
# One reduction on the sum lifts both to full; reducing before the add
# would mix a full routed contribution with an unreduced shared one.
expert_output = expert_output + shared_expert_output
# The shared expert is replicated (Qwen3_5MoeMLP intentionally omits

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The premise here is wrong for the default pipeline. Qwen3_5MoeMLP does not omit layer_type — lines 643-662 tag all three projections with layer_type="mlp" and tp_mode="colwise"/"colwise"/"rowwise", and its own docstring comment at line 638 still says the opposite of this one ("Tagged layer_type=\"mlp\" so the shared expert is TP-sharded ... lifted to full by the single merge-point all_reduce").

apply_sharding_hints is the default sharding transform (config/default.yaml:134) and applies no shard_layers filter unless a config sets one, so on that path the shared expert's rowwise down_proj emits a per-rank partial. With the new order that partial is added after the only all_reduce and is never reduced — silently 1/world_size of the shared contribution on TP>1, no error. The 8x symptom you fixed comes from qwen3.5_moe_35b.yaml, which disables the hint path (sharding_source: ['manual'], shared_expert entries commented out) and therefore replicates it.

Pick one and make the model self-consistent: either drop the tp_mode/layer_type hints from Qwen3_5MoeMLP so the shared expert is genuinely replicated under both pipelines (then this order is correct and line 638's comment must go), or keep all_reduce(routed + shared) and make the 35b config shard the shared expert. As written, correctness depends on which YAML is loaded.

# ``layer_type`` and the yaml ``shard_layers`` whitelist excludes it),
# so its output is already the full value on every rank. All-reduce
# the sharded routed-expert partial first, then add the replicated
# shared output; adding before would scale the shared output by the
# TP world size.
expert_output = torch.ops.auto_deploy.all_reduce(expert_output, layer_type="moe")
expert_output = expert_output + shared_expert_output

expert_output = expert_output.reshape(batch_size, sequence_length, hidden_dim)
return expert_output
Expand Down
Loading