Raise a clear error when FSDP is enabled on a mesh with no shard dimension - #4180
Open
qgallouedec wants to merge 1 commit into
Open
Raise a clear error when FSDP is enabled on a mesh with no shard dimension#4180qgallouedec wants to merge 1 commit into
qgallouedec wants to merge 1 commit into
Conversation
…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.
|
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. |
SunMarc
reviewed
Aug 31, 2026
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`." | ||
| ) |
Member
There was a problem hiding this comment.
Can't we put that in _validate_accelerator ? i feel like should detect this way earlier
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When FSDP2 is enabled together with a
ParallelismConfigwhose ranks are fully consumed by other dimensions (for exampletp_size == world_size, which is also what transformers' expert parallelism produces),fsdp2_prepare_modelslices the mesh withfsdp_dim_namesand fails with a confusing error:This PR raises an explicit error instead:
Reproduction (2 GPUs):