Repository navigation
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/204899
Note: Links to docs will display an error until the docs builds have been completed. ❌ 4 New FailuresAs of commit 92aca25 with merge base bba8f39 ( NEW FAILURES - The following jobs have failed:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This was referenced Nov 14, 2026
[FSDP2] Run fully_shard on the FlexShard engine runtime; delete FSDPParamGroup (AI-assisted)
#203764
Draft
Draft
This branch has not been deployed
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.
Stack from ghstack (oldest at bottom):
spin fixlint)Linked issue or supporting maintainer
BC-breaking?
Summary (human written only)
Agent details (AI-generated)
Drafted with an AI assistant at the author's request. A draft for discussion, not ready for review or merge.
Three things
fully_shardusers have thatflex_shardlacked. The engine already runs the last two forfully_shard;flex_shardonly had no way to ask for them.1. Global gradient norm.
flex_shardparameters are plain local tensors, sotorch.nn.utils.clip_grad_norm_computes each rank's norm of its own shard and clips each rank by a different factor; withfully_shard, DTensor gradients make the stock function reduce over the mesh. (With two equal halves, each rank sees 1/sqrt(2) of the true norm.)clip_grad_norm_(parameters, max_norm, norm_type=2.0, error_if_nonfinite=False, foreach=None)hastorch.nn.utils.clip_grad_norm_'s signature and result. Aflex_shardparameter's local gradient is this rank's part of the full gradient, disjoint from the other ranks' parts on its bucket's mesh (asShard,BlockShard,BucketedOwnedand the other placements lay them out), so each mesh's norm is the local norm combined by one all-reduce over that mesh: sum of p-th powers, max for the infinity norm, sum of counts for p = 0. Parameters thatflex_sharddoes not shard count once. The gradients are then scaled withtorch.nn.utils.clip_grads_with_norm_. A rank that holds no part of a gradient (e.g. one it does not own) contributes zero but still joins the all-reduce. Replicas of an HSDP mesh hold the same parts, so the shard mesh's all-reduce is the whole norm there too. A parameter that is itself a part of a bigger one (tensor or expert parallelism, declared withset_global_layout) is combined over the groups that split it too:set_outer_shard_groups(tensor, groups)declares them beforeflex_shard(), one group per mesh dim that shards the parameter, anddtensor_to_global_layoutdeclares the groups of a DTensor'sSharddims.flex_shardkeeps them on the parameters it installs, and the norm reduces over the bucket's mesh and then each of them. A layout that covers the full parameter (a replicated one) needs none; a partial layout without them raisesValueError, namingset_outer_shard_groups. Parameters with the same groups share their all-reduces.2. HSDP. A 2D
BucketSpec.meshwithmesh_dim_namesshards over dim 1 and replicates over dim 0, asfully_sharddoes.placement_fnand the parameters' sharding metadata see the 1D shard mesh; the replicate group goes to the engine'sbuild_bucket, which all-reduces each reduce-scatter's output over the replicas and counts them in the default divide factor, as FSDP2 does.FlexShardModule.set_requires_all_reduceexposes the engine's switch for gradient accumulation without the all-reduce, like FSDP2's. An unnamed 2D mesh raises, as infully_shard.3. An int
reshard_after_forward. An int that divides the shard mesh size gives each rank a group of that many consecutive shard-mesh ranks, whichbuild_bucketuses asfully_shard's post-forward group: the rank keeps its group's part of the unsharded parameters after forward, and the group all-gathers them before backward. Only the members create a group (new_group(..., use_local_synchronization=True)), so buckets on meshes over some of the ranks work too, and buckets with the same ranks share one. The shard mesh size reshards as True and 1 as False, as in FSDP2.HSDP and the post-forward group need the reduce and post-forward paths that the engine's
Shardimplements, soflex_shardandrelayout_bucketraise for placements that do not declarematches_fsdp2_collectives, with the check thatfsdp2_compatiblealready used.relayout_bucketkeeps a bucket's replicate and post-forward groups.Review order:
clip_grad.pyandset_outer_shard_groups(placement_contract.py, kept throughParamInfoandFlexShardRepresentation, declared by the DTensor adapter);flex_shard.py(_resolve_bucket_meshes, the shared placement check,build_flex_bucketandrelayout_bucketpassing the groups) and theBucketSpecdocs; then the tests.Test Plan
test_flex_shard_clip_grad.pychecks the total norm and every rank's clipped gradients againsttorch.nn.utils.clip_grad_norm_on an unsharded copy, forShard,BucketedBlockShardand whole-parameterBucketedOwnedbuckets with uneven sizes and a parameter outsideflex_shard, for the 2-, 1- and infinity norms.test_matches_unsharded_reference_with_outer_layoutsdoes the same on 4 ranks with tensor and expert parallelism over one dim of a 2x2 mesh andflex_shardover the other (column- and row-wise weights, stacked experts and a replicated bias, as DTensors throughdtensor_to_global_layout), each rank's gradients set to its parts of known full gradients;test_outer_layout_needs_its_shard_groupschecks the error.TestFlexShardHSDPAndReshardToSmallerGroup:fsdp2_compatiblebuckets matchfully_shardbit for bit (losses, local grads and parameters over 3 AdamW steps, and every all-gather and reduce-scatter input) on a (2, 2) HSDP mesh, with and without accumulation that all-reduces only the last microbatch, and with the layers'reshard_after_forward=2on a 4-rank mesh, for fp32 and bf16/fp32 mixed precision; plus the errors for an owner placement on an HSDP mesh or with an int reshard, an unnamed 2D mesh and an int that does not divide the mesh size.Results on main bba8f39 (4x GB300):
test_flex_shard_clip_grad.py3 passed;TestFlexShardHSDPAndReshardToSmallerGroup3 passed; the wholeflex_shardsuite 157 passed and the FSDP2 suite 479 passed (1 failure,test_fully_shard_alloc_from_pg, from this host's NCCL) at the top of the stack. Lint is clean.This PR was authored with an AI assistant (Claude).