Add opt-in periodic device cache clearing during training - #402
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
Adds an opt-in training setting to periodically clear PyTorch device caches (with additional MPS graph-cache clearing when supported) to mitigate allocator/cache growth during long-running variable-shape workloads, especially on MPS.
Changes:
- Introduces
training.empty_cache_steps(default0) and wires it intoTrainerConfig. - Calls a new
Trainer._maybe_clear_device_cache()hook during the training loop to invokeclear_gpu_cache()at the configured interval. - Extends
clear_gpu_cache()to also calltorch.mps.clear_graph_cache()when available, and adds unit tests for the new behavior.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| eole/utils/misc.py | Extends cache clearing to include optional MPS graph-cache clearing. |
| eole/trainer.py | Adds empty_cache_steps plumbing and periodic cache clearing during training. |
| eole/tests/test_trainer_cache.py | Adds tests for cache-clearing behavior across disabled/mismatch/CUDA/MPS scenarios. |
| eole/config/training.py | Adds the empty_cache_steps config field to the training schema with documentation. |
Comments suppressed due to low confidence (4)
eole/tests/test_trainer_cache.py:40
- This test patches
torch.mps.empty_cachejust to assert it’s not called, which can fail on non-MPS builds. Sincetorch.backends.mps.is_available()is forced False, it’s sufficient to only patch/assert the CUDA path.
with patch("torch.cuda.is_available", return_value=True), patch(
"torch.backends.mps.is_available", return_value=False
), patch("torch.cuda.empty_cache") as cuda_empty_cache, patch("torch.mps.empty_cache") as mps_empty_cache:
trainer._maybe_clear_device_cache(step=10)
cuda_empty_cache.assert_called_once_with()
mps_empty_cache.assert_not_called()
eole/tests/test_trainer_cache.py:54
- Patching
torch.mps.empty_cacheby dotted string requirestorch.mpsto exist, which may not be true on non-Apple builds. Patchtorch.mpsitself (withcreate=True) to a small namespace containing the methods you need to assert.
with patch("torch.cuda.is_available", return_value=False), patch(
"torch.backends.mps.is_available", return_value=True
), patch("torch.mps.empty_cache") as mps_empty_cache, patch(
"torch.mps.clear_graph_cache", create=True
) as clear_graph_cache:
trainer._maybe_clear_device_cache(step=10)
mps_empty_cache.assert_called_once_with()
clear_graph_cache.assert_called_once_with()
eole/tests/test_trainer_cache.py:63
patch("torch.mps", mps)will fail iftorchhas nompsattribute. Usepatch.object(torch, "mps", ..., create=True)to make this test portable across non-MPS PyTorch builds.
with patch("torch.cuda.is_available", return_value=False), patch(
"torch.backends.mps.is_available", return_value=True
), patch("torch.mps", mps):
trainer._maybe_clear_device_cache(step=10)
eole/tests/test_trainer_cache.py:28
- Same issue as the disabled test: patching
torch.mps.empty_cacheis unnecessary here and can fail iftorch.mpsdoesn’t exist. Patch/asserteole.trainer.clear_gpu_cacheinstead to test the interval-mismatch behavior without depending on optional PyTorch backends.
def test_empty_cache_steps_interval_mismatch_does_not_clear_cache(self):
trainer = object.__new__(Trainer)
trainer.config = TrainerConfig(empty_cache_steps=10)
with patch("torch.cuda.empty_cache") as cuda_empty_cache, patch("torch.mps.empty_cache") as mps_empty_cache:
trainer._maybe_clear_device_cache(step=9)
cuda_empty_cache.assert_not_called()
mps_empty_cache.assert_not_called()
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Summary
Adds an opt-in
training.empty_cache_stepssetting to periodically clear PyTorch device caches during training.This is primarily useful for MPS training runs where cached allocator memory can grow over time with variable-shape workloads, with open issues in PyTorch for this.
On supported PyTorch versions, MPS graph cache clearing is also invoked via a guarded
torch.mps.clear_graph_cache()call.Default behavior is unchanged:
empty_cache_steps: 0disables periodic clearing.Changes
training.empty_cache_steps, defaulting to0.clear_gpu_cache()from the trainer loop when the configured interval matches.clear_gpu_cache()to clear the MPS graph cache when available.Notes
This should not affect training quality. It only clears cached device allocations and, when available, cached MPS graph objects. Model weights, gradients, optimizer state, batches, and loss computation are unchanged.
The main tradeoff is performance: lower intervals may reduce MPS memory pressure but can add cache-clearing/recompilation overhead.