Skip to content

Commit d7cd063

Browse files
committed
[distributed] Context parallelism with expert parallelism
Each rank of a cp group dispatches the tokens of its own slice only, so no token reaches an expert twice and `efsdp * ep` still covers `fsdp * cp * tp`. The expert gradient divide factor already counts the cp ranks through `dp_size`, so only the explanatory comment and the mesh docstring change; `cp_size` with `ep_size` is no longer refused.
1 parent 405f6f5 commit d7cd063

2 files changed

Lines changed: 6 additions & 6 deletions

File tree

‎src/transformers/distributed/configuration_utils.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,10 @@ def _validate_mesh_config(self):
196196
if self.cp_size > 1:
197197
if not is_torch_greater_or_equal("2.10"):
198198
raise OSError("Context parallelism requires `torch>=2.10` (`DeviceMesh._unflatten`).")
199-
if unsupported := [name for name in ("pp_size", "ep_size") if getattr(self, name) > 1]:
199+
if self.pp_size > 1:
200200
raise ValueError(
201-
f"Context parallelism (`cp_size={self.cp_size}`) is not supported yet with `{unsupported[0]}`. "
202-
"Use `DistributedConfig(fsdp_size=N, tp_size=T, cp_size=M)`."
201+
f"Context parallelism (`cp_size={self.cp_size}`) is not supported yet with pipeline parallelism "
202+
f"(`pp_size={self.pp_size}`)."
203203
)
204204

205205
if self.dp_size > 1 and self.pp_size > 1:

‎src/transformers/distributed/fsdp.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,10 @@ def apply_fully_sharded_data_parallelism(
202202
for module_name, module in model.named_modules():
203203
if ep_plan.get(module_name) == "ep_dispatch_experts":
204204
fully_shard(module, mesh=expert_mesh, reshard_after_forward=True, **fsdp_policy_kwargs)
205-
# an EP group holds ep_size / tp_size distinct batches
205+
# an EP group holds ep_size / tp_size distinct batches, or cp slices of them under context parallelism
206206
# the efsdp reduce the sums on efsdp_size copies of that expert.
207-
# The expert gradient therefore covers ep_size / tp_size * efsdp_size = fsdp_size batches
208-
# not efsdp_size batches (the relationship is efsdp_size = fsdp_size * tp_size / ep_size)
207+
# The expert gradient therefore covers ep_size / tp_size * efsdp_size = fsdp_size * cp_size batches
208+
# not efsdp_size batches (the relationship is efsdp_size = fsdp_size * cp_size * tp_size / ep_size)
209209
# With HSDP, the all-reduce across replicas adds dp_replicate_size times more: dp_size batches.
210210
module.set_gradient_divide_factor(float(distributed_config.dp_size))
211211
# FSDP2 applies a divide factor other than the group size through PREMUL_SUM, which non-NCCL backends

0 commit comments

Comments
 (0)