Skip to content

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

Merged
SunMarc merged 3 commits into
mainfrom
fsdp2-ep-mesh-error
Sep 3, 2026
Merged

Raise a clear error when FSDP is enabled on a mesh with no shard dimension#4180
SunMarc merged 3 commits 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 thread src/accelerate/utils/fsdp_utils.py Outdated
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

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.

Good call, moved to _validate_accelerator.
The condition only needs the config (is_fsdp2 + neither dp_shard nor cp enabled), so it now raises at Accelerator() init instead of inside prepare_model.

@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

@SunMarc
SunMarc merged commit fd13282 into main Sep 3, 2026
27 of 29 checks passed
@SunMarc
SunMarc deleted the fsdp2-ep-mesh-error branch September 3, 2026 13:55
qgallouedec added a commit that referenced this pull request Sep 5, 2026
Both sides add a guard at the same point of `_validate_accelerator`: this branch refuses
`sp_backend="torch"` under DeepSpeed, and main (#4180) refuses FSDP2 with no dimension to shard across.
Both are kept as written.
qgallouedec added a commit that referenced this pull request Sep 5, 2026
#4180 refuses FSDP2 when neither `dp_shard` nor `cp` is enabled. `sp` is folded into `dp_shard_cp` the
same way `cp` is, so `sp_size=2` alone gives FSDP a dimension to shard across, and the guard rejected
it. Testing `dp_shard_cp_dim_names` directly is the condition the comment describes and the idiom the
mesh builder already uses.
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