Skip to content

Fix gradient corruption with layerwise param all-gather overlap - #4609

Merged
ericharper merged 2 commits into
NVIDIA:mainfrom
deepakn94:dnarayanan/fix_layerwise_overlap_grad_corruption
May 6, 2026
Merged

Fix gradient corruption with layerwise param all-gather overlap#4609
ericharper merged 2 commits into
NVIDIA:mainfrom
deepakn94:dnarayanan/fix_layerwise_overlap_grad_corruption

Conversation

@deepakn94

@deepakn94 deepakn94 commented May 4, 2026

Copy link
Copy Markdown
Contributor

Summary

  • reuse grad buffer for layer-wise param allgather #3751 introduced a bug affecting --overlap-param-gather with the layerwise distributed optimizer (e.g., Muon).
  • The layerwise param sync path (start_param_sync) reuses bucket.grad_data as the all-gather receive buffer. After finish_param_sync copies gathered params to model params, grad_data was not zeroed — leaving stale all-gather data in the buffer.
  • Since param.main_grad is a view into grad_data, the backward pass accumulates gradients (main_grad.add_(param.grad.data)) on top of this stale data, corrupting gradients.
  • Fix: zero bucket.grad_data after copying gathered params in both the synchronous (start_param_sync) and async (finish_param_sync) paths, matching the existing pattern used for the mxfp8 path.

Test plan

  • Run Muon training with --overlap-param-gather --overlap-grad-reduce and verify loss curves match non-overlapped baseline
image

🤖 Generated with Claude Code

@copy-pr-bot

copy-pr-bot Bot commented May 4, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

Comment thread megatron/core/distributed/param_and_grad_buffer.py
@deepakn94
deepakn94 requested review from FDecaYed and skyw May 4, 2026 21:02
@deepakn94

Copy link
Copy Markdown
Contributor Author

/claude review

Comment thread megatron/core/distributed/param_and_grad_buffer.py

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix looks correct. grad_data is reused as the all-gather receive buffer (line 355) and must be zeroed after params are copied out, otherwise main_grad (a view into grad_data) accumulates on stale data. Both sync and async paths are covered, and the pattern matches the existing mxfp8 zeroing at line 490.

Left one suggestion about adding a multi-step regression test — the existing unit tests only do a single step and wouldn't have caught this.

@deepakn94

Copy link
Copy Markdown
Contributor Author

/claude review

Comment thread tests/unit_tests/test_layer_wise_optimizer.py Outdated

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean, well-scoped bug fix. The root cause analysis is solid — grad_data reused as the all-gather receive buffer leaves stale data that corrupts gradients via the main_grad view. Zeroing in both sync and async paths is the right fix.

One minor suggestion: the async path through finish_param_sync also got the fix but test_overlap_param_gather_async_dispatch_and_finish doesn't assert that grad_data is zeroed afterward. Adding the same assertion there would guard both code paths against regressions.

Comment thread megatron/core/distributed/param_and_grad_buffer.py Outdated
Comment thread tests/unit_tests/test_layer_wise_optimizer.py Outdated
@deepakn94
deepakn94 force-pushed the dnarayanan/fix_layerwise_overlap_grad_corruption branch from 38f9830 to b9c58ed Compare May 4, 2026 21:25
@deepakn94

Copy link
Copy Markdown
Contributor Author

/claude review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@deepakn94
deepakn94 marked this pull request as ready for review May 4, 2026 21:27
@deepakn94
deepakn94 requested review from a team as code owners May 4, 2026 21:27
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the Final Review PR is in the "final review" stage label May 4, 2026
@svcnvidia-nemo-ci
svcnvidia-nemo-ci requested a review from a team May 4, 2026 21:27
@deepakn94
deepakn94 force-pushed the dnarayanan/fix_layerwise_overlap_grad_corruption branch from b9c58ed to f14e20a Compare May 4, 2026 21:34
@deepakn94

Copy link
Copy Markdown
Contributor Author

/claude review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

…orruption

The layerwise param sync path reuses grad_data as the all-gather receive
buffer. After copying gathered params to model params, grad_data was not
zeroed, so it still contained the all-gather results. Since main_grad is
a view into grad_data, accumulation into main_grad would start from the
result of the latest parameter all-gather instead of zero, corrupting
gradients when using --overlap-param-gather with the layerwise
distributed optimizer (e.g., Muon).

Fix: zero bucket.grad_data after copying gathered params in both the
synchronous (start_param_sync) and async (finish_param_sync) paths,
matching the existing pattern used for the mxfp8 path.

Add grad_data zeroing assertions to test_overlap_param_gather_multi_iteration
and test_overlap_param_gather_async_dispatch_and_finish.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@deepakn94
deepakn94 force-pushed the dnarayanan/fix_layerwise_overlap_grad_corruption branch from f14e20a to c486e79 Compare May 4, 2026 21:38

@FDecaYed FDecaYed left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@deepakn94
deepakn94 enabled auto-merge May 5, 2026 13:30
@svcnvidia-nemo-ci svcnvidia-nemo-ci added Approved All necessary approvals have been made and removed Final Review PR is in the "final review" stage labels May 5, 2026
@deepakn94
deepakn94 added this pull request to the merge queue May 5, 2026
@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/25403344061

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks May 5, 2026
@deepakn94
deepakn94 added this pull request to the merge queue May 5, 2026
@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/25408640123

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks May 6, 2026
@ericharper
ericharper added this pull request to the merge queue May 6, 2026
@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/25415024224

Merged via the queue into NVIDIA:main with commit b25a76e May 6, 2026
67 checks passed
@deepakn94
deepakn94 deleted the dnarayanan/fix_layerwise_overlap_grad_corruption branch May 6, 2026 04:39
Andron00e pushed a commit to Andron00e/Megatron-LM that referenced this pull request May 28, 2026
…ync-2026-05

Upstream tip: 932d9ee (Add periodic GPU sniff tests, 2026-05-07).

Conflict resolution:
- 13 modify/delete conflicts in .github/* and skills/*: kept deletion
  (matches policy from c037427 removing upstream-NVIDIA CI machinery
  not relevant to this fork).
- AGENTS.md content conflict: kept fork version (skills/contributing
  sections from upstream don't apply since the directories were removed).
- New upstream additions matching the same removal policy were dropped:
  .claude/settings.json, .github/workflows/nightly-sync-main-to-dev.yml,
  skills/{cicd,linting-and-formatting,nightly-sync,run-on-slurm,testing,
  update-golden-values}/SKILL.md.
- SECURITY.md (new from upstream): kept (generic security policy).

Verified post-merge:
- pretrain_gpt.py logging-patch hook intact (lines 405-408).
- megatron/core/optimizer/__init__.py emerging_optimizers + muon logic intact.
- megatron/training/arguments.py custom muon flags intact (lines 2297-2309).

Notable upstream changes pulled in:
- Removed legacy transformer + legacy GPT modules (NVIDIA#4207, NVIDIA#4322).
- Docker base image bump to 26.04-py3 (NVIDIA#4611).
- Inference fixes (vLLM grouped GEMM NVIDIA#4566, FlashInfer sampling NVIDIA#2456,
  EP sync NVIDIA#4607, MoE dispatcher fixes NVIDIA#4576).
- Gradient corruption fix with layerwise param all-gather overlap (NVIDIA#4609).
- Hybrid model + Flextron + GPU sniff tests + named validation sets +
  fault injection + InJob restart.
yhgalaxy pushed a commit to yhgalaxy/Megatron-LM that referenced this pull request Jun 17, 2026
…IA#4609)

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: yhgalaxy <yhgalaxy@outlook.com>
jon-barker pushed a commit to jon-barker/Megatron-LM that referenced this pull request Jul 10, 2026
…IA#4609)

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: Jon Barker <jbarker@aws-cmh-slurm-1-vscode-02.cm.cluster>
terminator123 pushed a commit to 021ai/Megatron-LM that referenced this pull request Aug 3, 2026
…IA#4609)

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
svcnvidia-nemo-ci pushed a commit to dimapihtar/Megatron-LM that referenced this pull request Aug 4, 2026
…IA#4609)

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: Dmytro Pykhtar <dpykhtar@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Approved All necessary approvals have been made complexity: low

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants