Skip to content

Optimize memory usage of partial CUDA graphs - #5451

Merged
jiemingz merged 9 commits into
NVIDIA:mainfrom
jiemingz:jiemingz/cg_mem
Jul 2, 2026
Merged

Optimize memory usage of partial CUDA graphs#5451
jiemingz merged 9 commits into
NVIDIA:mainfrom
jiemingz:jiemingz/cg_mem

Conversation

@jiemingz

@jiemingz jiemingz commented Jun 23, 2026

Copy link
Copy Markdown
Contributor
  • I, the PR author, have personally reviewed every line of this PR.

What does this PR do ?

⚠️ For major changes (either in lines of code or in its impact), please make sure to first share a design doc with the team. If you're unsure what's the best way to do so, contact @NVIDIA/mcore-oncall.

Issue tracking

For PRs from open-source community contributors:

  • New features: a linked issue is required. Please open a feature request and reference it here before submitting the PR.
  • Small updates (bug fixes, minor improvements): a linked issue is recommended and will accelerate the PR review process.

Linked issue:

Contribution process

Pre-checks

  • I have added relevant unit tests
  • I have added relevant functional tests
  • I have added proper typing to my code Typing guidelines
  • I have added relevant documentation
  • I have run the autoformatter.sh on my PR

Code review

Feel free to message or comment @NVIDIA/mcore-oncall to help accelerate your merge into main. The less complex your PR is, the faster it will be approved and merged!

All PRs start as draft. If you open a non-draft PR, it will be automatically converted to draft.

Step 1: Mark PR as "Ready for Review"

  1. When your PR is ready, click Ready for Review.
  2. An oncall reviewer is auto-assigned and expert reviewers are notified based on your changes.
    • Some PRs may jump straight to step 2. This is determined by .github/CODEOWNERS.

⚠️ Only mark as ready once merge-conflicts are resolved and the CI is passing.
Final Review might get declined if these requirements are not fulfilled.

Step 2: Final Review

For PRs that change megatron/core, once all expert reviewers have approved, the Final Review label is applied automatically and final reviewers are assigned.

For PRs outside megatron/core, this step is skipped.

Step 3: Approved

Once all required reviewers have approved, the Approved label is applied automatically.

Merge

Any member of mcore-engineers will be able to merge your PR.

jiemingz added 2 commits June 11, 2026 13:41
Signed-off-by: Jieming Zhang <jiemingz@nvidia.com>
Signed-off-by: root <jiemingz@nvidia.com>
@jiemingz
jiemingz requested review from a team as code owners June 23, 2026 16:36
@copy-pr-bot

copy-pr-bot Bot commented Jun 23, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@jiemingz
jiemingz marked this pull request as draft June 23, 2026 16:36
@svcnvidia-nemo-ci
svcnvidia-nemo-ci marked this pull request as draft June 23, 2026 16:36
@github-actions

Copy link
Copy Markdown
Contributor

This PR has been automatically converted to draft because all PRs must start as drafts.

When you are ready for review, click Ready for Review to begin the review process. This will:

  1. Add the oncall reviewer (optional reviewer)
  2. Add required review teams based on your changes

See the contribution guide for more details.

@deepakn94 deepakn94 changed the title optimize partial cudagraph memory usage Optimize memory usage of partial CUDA graphs Jun 23, 2026
@jiemingz
jiemingz marked this pull request as ready for review June 23, 2026 17:15
Comment thread megatron/core/transformer/cuda_graphs.py
Comment thread megatron/core/transformer/cuda_graphs.py Outdated
Comment thread megatron/core/transformer/transformer_layer.py
Comment thread megatron/core/transformer/moe/token_dispatcher.py
Comment thread megatron/core/transformer/cuda_graphs.py
Signed-off-by: root <jiemingz@nvidia.com>
@jiemingz

Copy link
Copy Markdown
Contributor Author

/claude review

Comment thread megatron/core/transformer/cuda_graphs.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.

Two bugs in the new make_weakref function (line 340 and the except block) — see inline comment. Otherwise the changes look correct.

Comment thread megatron/core/models/hybrid/hybrid_block.py Outdated
Comment thread megatron/core/transformer/transformer_block.py Outdated
Signed-off-by: Jieming Zhang <jiemingz@nvidia.com>

@Phlip79 Phlip79 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we have a unit or functional test to prevent memory regression? If not, should we?

@jiemingz

jiemingz commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

Do we have a unit or functional test to prevent memory regression? If not, should we?
We do not beyond the usual checks in functional tests, however they are not accurate.

Indeed I am working on a test to track memory usage (as well as tracking the numerical correctness of graphs) but unfortunately measuring memory usage under partial CG isn't straighforward. The usual methods are not currently cleanly supported in pytorch- polling memory usage with cudagraphs pools withtorch.cuda.get_memory_allocated doesnt account for graph mem pools, and directly querying total device memory with get_mem_info is too unstable.
See this comment:
#5451 (comment)
I would suggest merging these optimizations first and revisiting how we catch regressions in a separate PR.

@jiemingz

jiemingz commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

Do we have a unit or functional test to prevent memory regression? If not, should we?

I created an issue to create these tests:
#5594

@jiemingz
jiemingz enabled auto-merge July 1, 2026 19:14
@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 Jul 1, 2026
@jiemingz

jiemingz commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 0589635

@jiemingz
jiemingz disabled auto-merge July 1, 2026 19:23
@yaox12
yaox12 added this pull request to the merge queue Jul 2, 2026
@yaox12
yaox12 removed this pull request from the merge queue due to a manual request Jul 2, 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/28562317268

@jiemingz
jiemingz added this pull request to the merge queue Jul 2, 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/28599091205

Merged via the queue into NVIDIA:main with commit 5e4fe9b Jul 2, 2026
181 checks passed
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: medium Run tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants