8000 Empty the MPS allocator cache after each training step by JHenzi · Pull Request #38 · volotat/mini-AGI · GitHub
[go: up one dir, main page]

Skip to content

Empty the MPS allocator cache after each training step - #38

Open
JHenzi wants to merge 1 commit into
volotat:mainfrom
JHenzi:mps-empty-cache
Open

JHenzi wants to merge 1 commit into
volotat:mainfrom
JHenzi:mps-empty-cache

Conversation

@JHenzi
@JHenzi JHenzi commented Nov 29, 2026
Copy link
Copy Markdown

A small fix for training on Apple Silicon. It adds 7 lines to train.py read and does nothing on any device other than MPS.

The problem

On a 16 GB M1, train.py read --device mps slowed 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. vmmap showed 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_exact in minagi/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):

row count per call process memory
fixed 0.17 GB, flat
random, 1-2,048 0.2 GB → 7 GB, still rising
random, rounded up to a multiple of 64 2.4 GB, still rising
random, torch.mps.empty_cache() every 50 calls 0.2-0.5 GB

The 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

  • The cost of emptying the cache every step. I didn't time it on its own. Against the swapping it removes, the trade is clearly worth it, but on a Mac with more memory, emptying every N steps might be enough.
  • Longer runs. The evidence is one machine over 12 minutes. What it shows is that memory stopped only climbing.
  • serve.py and 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

  • As is: one guarded call in the step loop.
  • If you'd rather not add MPS-specific code to the loop, the same call could go in a small helper next to minagi/precision.py's device handling, or behind a config setting.
  • If Macs aren't a target, it's fine to close this. The numbers above may still help anyone who hits the same slowdown.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0