Skip to content

Remove torch cuda empty cache - #5678

Merged
deepakn94 merged 4 commits into
NVIDIA:mainfrom
shurkat-nvidia:remove-torch-cuda-empty-cache
Aug 13, 2026
Merged

deepakn94 merged 4 commits into
NVIDIA:mainfrom
shurkat-nvidia:remove-torch-cuda-empty-cache

Conversation

@shurkat-nvidia

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

What does this PR do?

The PR removes an expensive torch.cuda.empty_cache() call within save_checkpoint_and_time(). This call took multiple seconds on every checkpoint and also increased the size of the generate_state_dict() function by multiple seconds. To avoid OOM errors, the code also modifies the Nemotron3 Super script to 1) use SHM to avoid expensive rebuild_cuda_tensor() calls during the IPC handoff and 2) garbage collect the torch allocator at 80% GPU allocation with a lower overhead than a raw empty_cache() call, and to use larger blocks to prevent fragmentation in the first place.

⚠️ 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

  • (N/A) I have added relevant unit tests
  • (N/A) I have added relevant functional tests
  • (N/A) I have added proper typing to my code Typing guidelines
  • (N/A) 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.

@copy-pr-bot

copy-pr-bot Bot commented Jul 6, 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.

@shurkat-nvidia
shurkat-nvidia force-pushed the remove-torch-cuda-empty-cache branch 2 times, most recently from ddbde1f to da16039 Compare July 6, 2026 18:54
@shurkat-nvidia
shurkat-nvidia marked this pull request as ready for review July 15, 2026 18:05
@shurkat-nvidia
shurkat-nvidia requested a review from a team as a code owner July 15, 2026 18:05
@shurkat-nvidia
shurkat-nvidia requested a review from a team July 15, 2026 18:05
@asolergi-nv

Copy link
Copy Markdown
Contributor

/ok to test debb98a

@cuichenx

Copy link
Copy Markdown
Contributor

This is a replay of the change reverted in #5366, is CI passing with this PR?

@Phlip79

Phlip79 commented Aug 10, 2026

Copy link
Copy Markdown
Member

This is a replay of the change reverted in #5366, is CI passing with this PR?

This test was recently updated in #6239. @shurkat-nvidia can you please check to see if the changes to the config are needed anymore?

torch.cuda.empty_cache() was added as a workaround for out-of-memory
errors on some models. With async-shm checkpointing, some of the memory
pressure is reduced. Remaining memory fragmentation and pressure can be
reduced by properly configuring the torch allocator.

Signed-off-by: Skand Hurkat <shurkat@nvidia.com>
 - Update checkpointing to use nvrx with shm.

 - Provide PYTORCH_ALLOC_CONF environment variable to initiate garbage
   collection. Use larger blocks to avoid fragmentation due to
   splitting.

Signed-off-by: Skand Hurkat <shurkat@nvidia.com>
@shurkat-nvidia
shurkat-nvidia force-pushed the remove-torch-cuda-empty-cache branch from debb98a to c7830f1 Compare August 10, 2026 21:01
@shurkat-nvidia

shurkat-nvidia commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor Author

This is a replay of the change reverted in #5366, is CI passing with this PR?

@cuichenx, @balasaajay ran the test manually a few weeks ago, it appeared to have addressed the OOM issue.

This test was recently updated in #6239. @shurkat-nvidia can you please check to see if the changes to the config are needed anymore?

@Phlip79, I rebased the branch to the latest main. Still need changes to checkpoint args and to the PYTORCH_ALLOC_CONF environment variable.

shurkat-nvidia and others added 2 commits August 10, 2026 14:25
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Signed-off-by: Skand Hurkat <shurkat@nvidia.com>
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Signed-off-by: Skand Hurkat <shurkat@nvidia.com>
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the Approved All necessary approvals have been made label Aug 10, 2026
@deepakn94

Copy link
Copy Markdown
Contributor

/ok to test 0f9153b

@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/31719722747

Merged via the queue into NVIDIA:main with commit 2119719 Aug 13, 2026
185 checks passed
@shurkat-nvidia
shurkat-nvidia deleted the remove-torch-cuda-empty-cache branch August 17, 2026 18:01
ksivaman pushed a commit to ksivaman/Megatron-LM that referenced this pull request Sep 1, 2026
Signed-off-by: Skand Hurkat <shurkat@nvidia.com>
Co-authored-by: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>

This branch was previously deployed

2 inactive deployments
public — 0f9153bf Deployed Aug 10, 2026 by copy-pr-bot[bot] via finalize / publish-docs #5699
test — 0f9153bf Deployed Aug 10, 2026 by copy-pr-bot[bot] via cicd-wait-in-queue #18639
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants