Skip to content
Closed
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
12 changes: 8 additions & 4 deletions src/transformers/distributed/tensor_parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,9 +969,10 @@ def resolve_parallel_plans(
) -> tuple[dict[str, str], dict[str, str]]:
"""Merge the `DistributedConfig` overrides into the model's plans and split them between TP and EP.

Returns the TP plan to apply to the dense modules and the EP plan to apply to the experts. Each plan is empty
when its parallel size is 1. EP owns every module it names, so TP rules for those modules and their children
are dropped: expert weights are sharded once, by the EP plan.
Selecting `ep_dispatch_experts` in the EP overrides removes inherited `ep_router` rules. Returns the TP plan to
apply to the dense modules and the EP plan to apply to the experts. Each plan is empty when its parallel size is
1. EP owns every module it names, so TP rules for those modules and their children are dropped: expert weights
are sharded once, by the EP plan.
"""
# Reject invalid paths before merging, e.g. "layers.*" when the model uses "model.layers.*".
names = {replace_layer_number_by_wildcard(n) for n, _ in chain(model.named_modules(), model.named_parameters())}
Expand All @@ -981,7 +982,10 @@ def resolve_parallel_plans(
plan = getattr(model, plan_name)
if unknown := override.keys() - names - plan.keys():
raise ValueError(f"`{plan_name}` keys {sorted(unknown)} match nothing in {type(model).__name__}.")
setattr(model, f"_{plan_name}", plan | override)
merged = plan | override
if plan_name == "ep_plan" and "ep_dispatch_experts" in override.values():
merged = {name: style for name, style in merged.items() if style != "ep_router" or name in override}

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.

not a fan of filtering a single key.
Can't we filter what appears in both?
Or module based only?

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.

lets clean this up

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not sure what you mean by what appears in both. We are trying to remove keys from the same plan not different plans (ep is already taking precedence over tp keys)

Here it's more if ep_plan has ep_router + moe_tp_experts and we want to add expert_dispatch. We are forced to remove the key manually (as the PR does). Maybe we can specify None to a key through the ep_plan which will delete the key ? wdyt ?

setattr(model, f"_{plan_name}", merged)

tp_plan = dict(model.tp_plan) if distributed_config.tp_size > 1 else {}
ep_plan = dict(model.ep_plan) if distributed_config.ep_size > 1 else {}
Expand Down
Loading