-
Notifications
You must be signed in to change notification settings - Fork 26.3k
[MPS] Fix critical memory leaks in allocator #167940
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
jmpnop
wants to merge
5
commits into
pytorch:main
Choose a base branch
from
jmpnop:fix-mps-memory-leaks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/167940
Note: Links to docs will display an error until the docs builds have been completed. ❗ 2 Active SEVsThere are 2 currently active SEVs. If your PR is affected, please view them below:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
e3e63eb to
9b63e10
Compare
Two critical bugs fixed: 1. emptyCache() doesn't free pending buffers after synchronization - After GPU sync, buffers in buffers_pending_free were never freed - Added freeInactiveBuffers() call to complete cleanup 2. buffers_pending_free mechanism designed but never implemented - Data structure documented but producer code missing - Implemented retainCount check in free_buffer() - Complete lifecycle: pending → available when GPU done Impact: Eliminates ~150MB/step memory leak in long training runs Fixes pytorch#105839 Fixes pytorch#145374
9b63e10 to
af79a17
Compare
The original fix only called freeInactiveBuffers() from emptyCache(). This left buffers_pending_free accumulating indefinitely during training, causing 150MB/step memory leaks that eventually crash the system. Now freeInactiveBuffers() is called in the allocation path when a free buffer isn't found, ensuring pending buffers are regularly processed without requiring explicit empty_cache() calls from user code.
Add CMAKE_POLICY_VERSION_MINIMUM=3.5 to suppress deprecation warnings when building with CMake 4.x on macOS. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
2a9bcff to
7319321
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
open source
release notes: mps
Release notes category
triaged
This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes two critical memory leaks in MPS (Metal Performance Shaders) allocator that cause out-of-memory crashes during long training runs on Apple Silicon.
Issues Fixed
Closes #105839
Closes #145374
Problem
Bug #1:
emptyCache()incomplete implementationtorch.mps.empty_cache()only freed buffers inavailable_buffersbuffers_pending_freewere never freed, accumulating indefinitelyBug #2:
buffers_pending_freedesigned but never implementedfreeInactiveBuffers()) writtenavailable_buffersregardless ofretainCountSolution
Fix #1: Complete emptyCache() Implementation (1 line)
File:
aten/src/ATen/mps/MPSAllocator.mm:450After GPU synchronization in
release_cached_buffers(), callfreeInactiveBuffers()to free buffers whoseretainCountdropped to 1.Fix #2: Implement buffers_pending_free Mechanism (17 lines)
File:
aten/src/ATen/mps/MPSAllocator.mm:305-333, 686-708Part A: Route buffers based on retainCount
In
free_buffer(), checkretainCountand route accordingly:retainCount > 1→buffers_pending_free(GPU still using)retainCount == 1→available_buffers(ready for reuse)Part B: Complete the lifecycle
In
freeInactiveBuffers(), move freed buffers toavailable_buffersinstead of callingfree_buffer()recursively.Testing
Manual Testing
Tested with long-running transformer training (FLAN-T5-XL, 2.8B parameters):
Before fixes:
After fixes:
Reproduction
Long training runs on Apple Silicon with MPS backend:
Impact
Checklist
Related Issues/PRs
Notes
These bugs affect all long-running training on Apple Silicon MPS, not just LSTMs. The fixes implement the originally intended design documented in the header file but never completed.