Repository navigation
Conversation
…ai#8692) In DeepSpeedZeroOptimizer_Stage3, __avg_scatter_contiguous_grads and allreduce_bucket both divide the gradient averaging world size by self.sequence_parallel_size (matching ZeRO-0/1/2), whereas __avg_scatter_grads delegates to reduce_scatter_coalesced (which divides by partition_world_size) and only rescales when averaging_world_size != partition_world_size without accounting for self.sequence_parallel_size. As a result, under sequence parallelism (sequence_parallel_size > 1): 1. ZeRO-3 with reduce_scatter=True or contiguous_gradients=False produces gradients smaller by a factor of sequence_parallel_size than contiguous_gradients=True, reduce_scatter=False or ZeRO-1/2. 2. Within a single ZeRO-3 model using reduce_scatter=False and contiguous_gradients=True, parameters larger than reduce_bucket_size take __avg_scatter_grads while smaller parameters take __avg_scatter_contiguous_grads, scaling different layers' gradients by different factors in the same backward step. Divide averaging_world_size by float(self.sequence_parallel_size) in __avg_scatter_grads so all ZeRO-3 gradient reduction paths apply the same sequence-parallel scaling factor. Fixes deepspeedai#8692 Signed-off-by: Parth Vadera <parthvadera@gmail.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
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.
Summary
Fixes #8692.
In
DeepSpeedZeroOptimizer_Stage3,__avg_scatter_contiguous_grads(stage3.py:1877) andallreduce_bucket(stage3.py:2278) both divide the gradient averaging world size byfloat(self.sequence_parallel_size), matching ZeRO-0 (engine.py:3884) and ZeRO-1/2 (stage_1_and_2.py:1505, 1687, 2379).However,
__avg_scatter_grads(stage3.py:1936-1940) delegates toreduce_scatter_coalesced(which divides bypartition_world_size) and only rescales whenaveraging_world_size != partition_world_sizewithout accounting forself.sequence_parallel_size:Without this division by
float(self.sequence_parallel_size):sequence_parallel_size > 1, ZeRO-3 withreduce_scatter=Trueorcontiguous_gradients=Falseproduces gradients smaller by a factor ofsequence_parallel_sizethanreduce_scatter=False, contiguous_gradients=Trueor ZeRO-1/2.reduce_scatter=False, contiguous_gradients=True, parameters whose size exceedsreduce_bucket_sizefall back to__avg_scatter_grads(stage3.py:1686), while smaller parameters use__avg_scatter_contiguous_grads(stage3.py:1683), causing different layers of the same model to receive gradients scaled by1 / sp_sizevs1.0in the same backward step.Non-duplication Check
closed_by_pull_requests: 0) and searched open/closed PRs touchingstage3.py,__avg_scatter_grads, andsequence_parallel_size.gradient_predivide_factor == partition_world_sizepost-scaling instage3.py.gradient_predivide_factorpost-scaling instage_1_and_2.py.compile/AutoSP passes andgroups.py.Validation & Test Results
Added
TestZero3SequenceParallelGradientScaleintests/unit/v1/zero/test_zero.pycovering all four ZeRO-3 reduction configurations undersequence_parallel_size=2((reduce_scatter=False, contiguous_gradients=True, reduce_bucket_size=500),(False, False, 500),(True, True, 500), and mixed-bucket(False, True, 16)wheresmalltakes__avg_scatter_contiguous_gradsandlargetakes__avg_scatter_grads).Commands Run
New regression test (
TestZero3SequenceParallelGradientScale):test[False-False-500],test[True-True-500],test[False-True-16]), 1 passed (test[False-True-500]).4 passed in 23.18s.Existing ZeRO gradient reduction tests (
TestGradientAllreduceOp):18 passed in 90.64s.Compatibility Matrix
reduce_scatter=False, contiguous_gradients=True)1.0scalereduce_scatter=False, contiguous_gradients=False)1/spto1.0reduce_scatter=True, contiguous_gradients=True)1/spto1.0param.ds_numel > reduce_bucket_size)1.0scale18 passed)all_to_all_quant_reduceaveraging_world_sizerescaleEnvironment (
ds_report)DS_ACCELERATOR=cpu, Gloo backend, 2 processes)AI Assistance Disclosure
AI assistance (GitHub Copilot) was used to help draft the regression test and PR description; all root-cause analysis and test executions were run and verified locally.
Credits & Acknowledgments
__avg_scatter_gradsand__avg_scatter_contiguous_grads/allreduce_bucketunder sequence parallelism (including the mixed-bucket single-model case), and providing the 2-rank reproduction scripts; and to @Eliaaazzz for citing Fix the sequence-parallelism for the dense model architecture #4530 and_reduce_non_expert_gradientsconfirming the DP-world averaging convention.Co-authored-by:.