Skip to content

feat: add empty_mps_cache handler for MPS device memory clearing#3765

Draft
Aanushka001 wants to merge 1 commit into
pytorch:masterfrom
Aanushka001:mps-cache-clearing
Draft

feat: add empty_mps_cache handler for MPS device memory clearing#3765
Aanushka001 wants to merge 1 commit into
pytorch:masterfrom
Aanushka001:mps-cache-clearing

Conversation

@Aanushka001
Copy link
Copy Markdown

Description

This PR adds MPS (Apple Metal Performance Shaders) cache clearing support, similar to the existing empty_cuda_cache() for CUDA devices.

Changes

  • Added empty_mps_cache() function that clears MPS cache via torch.mps.empty_cache()
  • Registered the MPS cache clearing handler in _setup_common_training_handlers() when running on MPS devices

Related

Based on original PR #3763

@github-actions github-actions Bot added the module: contrib Contrib module label May 31, 2026
@vfdev-5 vfdev-5 marked this pull request as draft May 31, 2026 19:46
def empty_mps_cache(_: Engine) -> None:
if hasattr(torch, "mps") and hasattr(torch.mps, "empty_cache"):
torch.mps.empty_cache()
import gc
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imports should be on top



def empty_mps_cache(_: Engine) -> None:
if hasattr(torch, "mps") and hasattr(torch.mps, "empty_cache"):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

either we can check in the function or outside it, currently irt checks in both place which make it kinda redundant.

@aaishwarymishra
Copy link
Copy Markdown
Collaborator

Hi, I also think we should add a separate parameter for the mps cache clearing instead of relying on pre_built cuda @vfdev-5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module: contrib Contrib module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants