Skip to content

Fix memory leak on MPS by explicitly clearing cache in trainer step#10190

Open
asebaq wants to merge 2 commits intohiyouga:mainfrom
asebaq:dev_fix_mps_memory_leak
Open

Fix memory leak on MPS by explicitly clearing cache in trainer step#10190
asebaq wants to merge 2 commits intohiyouga:mainfrom
asebaq:dev_fix_mps_memory_leak

Conversation

@asebaq
Copy link

@asebaq asebaq commented Feb 14, 2026

What does this PR do?

This PR addresses a memory leak issue encountered when fine-tuning models on macOS using the MPS backend.

  • Added torch.mps.empty_cache() calls within training_step and prediction_step in src/llamafactory/train/sft/trainer.py.
  • This ensures that GPU memory is properly released after each step, preventing Out of Memory errors during long training runs on Mac Studio/Pro.
  • Also corrected the training_step signature to include *args and **kwargs (or specifically num_items_in_batch) to be compatible with recent transformers updates.

Fixes # (issue)

Before submitting

Note: Verified locally on a Mac Studio (M3 Ultra), where training previously crashed due to memory accumulation.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @asebaq, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical memory leak issue affecting model fine-tuning on macOS systems utilizing the MPS backend. By introducing explicit cache clearing mechanisms after each training and prediction step, it prevents out-of-memory errors and ensures stable long-duration training. Additionally, it updates a method signature to maintain compatibility with the latest transformers library.

Highlights

  • MPS Memory Management: Implemented torch.mps.empty_cache() calls within training_step and prediction_step to explicitly clear GPU memory on the macOS MPS backend, resolving memory leak issues during fine-tuning.
  • Trainer Signature Update: Modified the training_step method signature to include num_items_in_batch (or *args, **kwargs) for compatibility with recent transformers library updates.
Changelog
  • src/llamafactory/train/sft/trainer.py
    • Added torch.mps.empty_cache() call within the training_step method, conditional on MPS availability.
    • Injected torch.mps.empty_cache() call into the prediction_step method, conditional on MPS availability.
    • Updated the training_step method signature to accept num_items_in_batch for broader compatibility.
Activity
  • The author, asebaq, created this pull request to fix a memory leak on the MPS backend.
  • The author verified the fix locally on a Mac Studio (M3 Ultra), confirming that training no longer crashes due to memory accumulation.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a workaround for a memory leak on the MPS backend by explicitly calling torch.mps.empty_cache() in both the training_step and prediction_step. This is a reasonable fix to prevent crashes due to memory accumulation. My main feedback is to make this behavior optional by adding a new training argument. This will prevent potential performance degradation for users who do not experience this issue and provides an easy way to disable the workaround in the future.

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