Skip to content

Cpu memory accumulation bug #20730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

ved1beta
Copy link

@ved1beta ved1beta commented Apr 20, 2025

What does this PR do?

This PR addresses the memory leak issue during prediction in PyTorch Lightning. It adds proper memory management when return_predictions=False and includes comprehensive tests to verify the fix.

Fixes #19398

Key Changes:

  • Added garbage collection in prediction loop when return_predictions=False
  • Implemented memory leak tests with large dataset simulation
  • Added environment variable cleanup in tests
  • Fixed pre-commit formatting issues

Before submitting

  • Was this discussed/agreed via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
    • No documentation changes needed
  • Did you write any new necessary tests? (not for typos and docs)
    • Yes, added memory leak tests
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request?
    • No breaking changes
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or minor internal changes/refactors)
    • Yes, will add entry for memory leak fix

PR review

Anyone in the community is welcome to review the PR.
Before you start reviewing, make sure you have read the review guidelines. In short, see the following bullet-list:

Reviewer checklist
  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

📚 Documentation preview 📚: https://pytorch-lightning--20730.org.readthedocs.build/en/20730/

@github-actions github-actions bot added the pl Generic label for PyTorch Lightning package label Apr 20, 2025
@Borda
Copy link
Member

Borda commented Apr 22, 2025

  • Implemented memory leak tests with large dataset simulation
  • Added environment variable cleanup in tests

seems this is not included in this PR yet

Copy link
Contributor

@deependujha deependujha left a comment

Choose a reason for hiding this comment

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

Hi @ved1beta, thanks for the contribution.

Added some reviews. Please try fixing the failing tests and also mypy issue.

# Clear memory if not returning predictions
import gc

gc.collect()
Copy link
Contributor

Choose a reason for hiding this comment

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

do you think it would be a good idea to have an argument collect_gc or something that users can toggle.

As Adrian said: it might be expensive in certain situations.

if predictions is None:
self._warning_cache.warn("predict returned None if it was on purpose, ignore this warning...")
step_args = self._build_step_args_from_hook_kwargs(hook_kwargs, "predict_step")
step_output = call._call_lightning_module_hook(trainer, "predict_step", *step_args)
Copy link
Contributor

Choose a reason for hiding this comment

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

why are you directly calling lightning module hook without calling strategy hook?

After couple of checks and precision_plugin context, it does call lightning_module's predict_step.

@Borda Borda added the waiting on author Waiting on user action, correction, or update label May 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pl Generic label for PyTorch Lightning package waiting on author Waiting on user action, correction, or update
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CPU-Memory keeps accumulating during trainer.predict
3 participants