Skip to content

Raise a clear error when FSDP is enabled on a mesh with no shard dimension - #4180

Open
qgallouedec wants to merge 1 commit into
mainfrom
fsdp2-ep-mesh-error
Open

Raise a clear error when FSDP is enabled on a mesh with no shard dimension#4180
qgallouedec wants to merge 1 commit into
mainfrom
fsdp2-ep-mesh-error

Conversation

@qgallouedec

Copy link
Copy Markdown
Member

When FSDP2 is enabled together with a ParallelismConfig whose ranks are fully consumed by other dimensions (for example tp_size == world_size, which is also what transformers' expert parallelism produces), fsdp2_prepare_model slices the mesh with fsdp_dim_names and fails with a confusing error:

KeyError: "Invalid mesh_dim_names ('dp_shard_cp',) specified. Valid mesh_dim_names are ['tp']."

This PR raises an explicit error instead:

ValueError: FSDP is enabled but the device mesh is ('tp',), which has no dimension for FSDP to shard across (both `dp_shard_size` and `cp_size` are 1). This usually means a model that is already parallelized another way -- e.g. loaded with `DistributedConfig(tp_size=N)` or `enable_expert_parallel=True`, which makes the whole world size tensor/expert parallel -- was launched under an FSDP config. Either launch it without the FSDP config, or leave ranks for FSDP to use by lowering `tp_size`.

Reproduction (2 GPUs):

# torchrun --nproc_per_node 2 repro.py
import torch
import torch.nn as nn
from accelerate import Accelerator
from accelerate.parallelism_config import ParallelismConfig
from accelerate.utils import FullyShardedDataParallelPlugin

accelerator = Accelerator(
    parallelism_config=ParallelismConfig(tp_size=2),
    fsdp_plugin=FullyShardedDataParallelPlugin(fsdp_version=2),
)
model = nn.Linear(8, 8)
optimizer = torch.optim.AdamW(model.parameters())
model, optimizer = accelerator.prepare(model, optimizer)

…nsion

`ParallelismConfig.fsdp_dim_names` always asks for `dp_shard_cp`, but that joint
dimension is only flattened into the device mesh when `dp_shard` or `cp` is enabled.
Launching an already-parallelized model -- e.g. one loaded with
`DistributedConfig(tp_size=world_size)` or `enable_expert_parallel=True` -- under an
FSDP config therefore slices a tp-only mesh by a name it does not contain, and the user
sees a bare `KeyError` from `torch.distributed.device_mesh`, several frames below any
code they wrote. Say what is actually wrong instead.
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@qgallouedec
qgallouedec requested a review from SunMarc August 27, 2026 22:45

@SunMarc SunMarc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks ! Left a comment

Comment on lines +756 to +768
pc = accelerator.parallelism_config

# `fsdp_dim_names` always asks for `dp_shard_cp`, but that joint dimension is only flattened into the mesh when
# `dp_shard` or `cp` is enabled. Without either, slicing the mesh below raises a `KeyError` from `device_mesh`.
if mesh is not None and not pc.dp_shard_enabled and not pc.cp_enabled:
raise ValueError(
f"FSDP is enabled but the device mesh is {tuple(mesh.mesh_dim_names)}, which has no dimension for FSDP "
"to shard across (both `dp_shard_size` and `cp_size` are 1). This usually means a model that is already "
"parallelized another way -- e.g. loaded with `DistributedConfig(tp_size=N)` or "
"`enable_expert_parallel=True`, which makes the whole world size tensor/expert parallel -- was launched "
"under an FSDP config. Either launch it without the FSDP config, or leave ranks for FSDP to use by "
"lowering `tp_size`."
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can't we put that in _validate_accelerator ? i feel like should detect this way earlier

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants