Repository navigation
Conversation
On Apple Silicon the MPS caching allocator keeps a freed buffer of every size it has used. Exact-size expert dispatch and halting give most matmuls a row count of their own, so on unified memory the cache grows until the machine swaps. On a 16 GB M1 the trainer reached 19 GB and reading fell from 64 to 2 char/s; with the cache emptied each step it held at 4-7 GB. No effect on CUDA, ROCm or CPU. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
A small fix for training on Apple Silicon. It adds 7 lines to
train.py readand does nothing on any device other than MPS.The problem
On a 16 GB M1,
train.py read --device mpsslowed from 64 to 2 characters a second over about three hours, with the same settings throughout. Held-out loss kept improving the whole time. The trainer's memory reached 19 GB, and the machine spent its time swapping.vmmapshowed roughly 8 GB of GPU buffers plus 8 GB of heap.PyTorch's MPS caching allocator keeps a freed buffer of every size it has used, and never returns them. Two parts of the model give most matmuls a row count of their own: exact-size expert dispatch (
run_exactinminagi/pool.py) and halting subsets (halt_freeze). As a result, the cache only grows. CUDA's allocator splits and reuses blocks, so it doesn't run into this. On unified memory it runs the machine out.A standalone check, with one small MLP forward and backward on MPS for 2,000 calls (torch 2.14.1):
torch.mps.empty_cache()every 50 callsThe change
The change calls
torch.mps.empty_cache()after each training step, when the device is MPS.In the real run on the same Mac, the trainer then stayed between 4 and 7 GB for the 12 minutes I measured, going down as well as up. It read about 75 characters a second; before the fix it was at 17 a second and still slowing at the same point.
What I didn't measure
serve.pyand the tools. I left them alone. Writing also gives the GPU changing sizes, so a long serving session on a Mac might want the same treatment.How to fold it, if at all
minagi/precision.py's device handling, or behind a config setting.