Skip to content

Scale ZeRO-3 __avg_scatter_grads by sequence_parallel_size - #8854

Open
deep1blue wants to merge 1 commit into
deepspeedai:masterfrom
deep1blue:fix-zero3-sp-avg-scatter-grads
Open

deep1blue wants to merge 1 commit into
deepspeedai:masterfrom
deep1blue:fix-zero3-sp-avg-scatter-grads

Conversation

@deep1blue

@deep1blue deep1blue commented Oct 18, 2026 •

Copy link
Copy Markdown

Summary

Fixes #8692.

In DeepSpeedZeroOptimizer_Stage3, __avg_scatter_contiguous_grads (stage3.py:1877) and allreduce_bucket (stage3.py:2278) both divide the gradient averaging world size by float(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 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:

partition_world_size = dist.get_world_size(group=process_group)
averaging_world_size = self._gradient_averaging_world_size(
    params_to_reduce, partition_world_size) / float(self.sequence_parallel_size)
if averaging_world_size != partition_world_size:
    scale = partition_world_size / float(averaging_world_size)
    grad_partitions_for_rank = [g.mul(scale) for g in grad_partitions_for_rank]

Without this division by float(self.sequence_parallel_size):

  1. Under sequence_parallel_size > 1, ZeRO-3 with reduce_scatter=True or contiguous_gradients=False produces gradients smaller by a factor of sequence_parallel_size than reduce_scatter=False, contiguous_gradients=True or ZeRO-1/2.
  2. Even within a single ZeRO-3 model using reduce_scatter=False, contiguous_gradients=True, parameters whose size exceeds reduce_bucket_size fall 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 by 1 / sp_size vs 1.0 in the same backward step.

Non-duplication Check

Validation & Test Results

Added TestZero3SequenceParallelGradientScale in tests/unit/v1/zero/test_zero.py covering all four ZeRO-3 reduction configurations under sequence_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) where small takes __avg_scatter_contiguous_grads and large takes __avg_scatter_grads).

Commands Run

  1. New regression test (TestZero3SequenceParallelGradientScale):

    PYTHONPATH=.:"$PWD/tests" DS_ACCELERATOR=cpu LOCAL_SIZE=2 pytest tests/unit/v1/zero/test_zero.py -k "TestZero3SequenceParallelGradientScale" -v
    • Before fix: 3 failed (test[False-False-500], test[True-True-500], test[False-True-16]), 1 passed (test[False-True-500]).
    • After fix: 4 passed in 23.18s.
  2. Existing ZeRO gradient reduction tests (TestGradientAllreduceOp):

    PYTHONPATH=.:"$PWD/tests" DS_ACCELERATOR=cpu LOCAL_SIZE=2 pytest tests/unit/v1/zero/test_zero.py -k "TestGradientAllreduceOp and not Training" -v
    • Result: 18 passed in 90.64s.

Compatibility Matrix

Feature / Path Validated Locally Notes
ZeRO-3 (reduce_scatter=False, contiguous_gradients=True) Yes (2-rank CPU/Gloo) Matches 1.0 scale
ZeRO-3 (reduce_scatter=False, contiguous_gradients=False) Yes (2-rank CPU/Gloo) Fixed from 1/sp to 1.0
ZeRO-3 (reduce_scatter=True, contiguous_gradients=True) Yes (2-rank CPU/Gloo) Fixed from 1/sp to 1.0
ZeRO-3 mixed bucket (param.ds_numel > reduce_bucket_size) Yes (2-rank CPU/Gloo) Both small and large params get 1.0 scale
ZeRO-0 / ZeRO-1 / ZeRO-2 Yes (2-rank CPU/Gloo) Unchanged (18 passed)
Multi-node all_to_all_quant_reduce Not run locally (single-node host) Uses same post-reduction averaging_world_size rescale

Environment (ds_report)

  • OS: Linux aarch64 (Debian 13)
  • Python: 3.13.5, PyTorch: 2.14.1+cpu (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

…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>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-18T03:39:27.833856Z 086cf17 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

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

No deployments
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.

Gradient scale under sequence parallelism differs between ZeRO reduction paths

1 participant