Repository navigation
fix(inference): free MPS memory between requests - #547
Merged
Merged
Conversation
_check_memory() returned False on MPS, and both generate paths only checked memory on CUDA, so _clear_memory() never ran on Apple Silicon. PyTorch's MPS allocator keeps the buffers it caches, and each new input length adds more, so the server grew with every request until the machine ran out. Check MPS too, against the same threshold, using torch.mps.driver_allocated_memory(), which includes that cache. Co-Authored-By: Claude Opus 5.5 (1M context) <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.
On Apple Silicon, the server's memory grows with every request and never comes back down.
_check_memory()returnsFalsefor MPS, and bothgeneratepaths only check memory when the device iscuda, so_clear_memory()(which already callstorch.mps.empty_cache()) never runs on MPS. PyTorch's MPS allocator keeps the buffers it caches, and each new input length adds more.This checks MPS too, against the existing
memory_threshold, usingtorch.mps.driver_allocated_memory(), which includes the allocator's cache. The CUDA path is unchanged.Measured
M3 Pro,
start-gpu_mac.shsettings, 60/v1/audio/speechrequests of 5 to 120 words withaf_heart+af_nicole(2)+af_jadzia, process footprint fromfootprint. Measured on v0.9.0, whosekokoro_v1.pyis identical to master's:The default
memory_threshold(0.8 GB) is below the ~1.2 GB of GPU memory the process holds after loading, so the cache is likely emptied at the start of nearly every request. That made no measurable difference to speed in the run above.Testing
generatepaths clearing memory over the threshold. They fail on master and pass here.uv run --extra test --extra cpu pytest: 568 passed, 9 skipped.ruff formatandruff checkclean.uvon Apple Silicon. Not tested in Docker, since containers on macOS can't use MPS, and not on CUDA hardware, though that path is unchanged.🤖 Generated with Claude Code