Skip to content

Conversation

@sbhavani
Copy link
Contributor

@sbhavani sbhavani commented Dec 1, 2025

Summary

Fixes #2362 - UnslothTrainer/SFTTrainer now respects the gradient_checkpointing setting

Changes

  • Updated model.for_training() calls in unsloth/models/rl.py (lines 308, 561) to pass use_gradient_checkpointing parameter from args
  • This ensures gradient checkpointing is only enabled when explicitly set in training arguments

Test plan

  • Verify gradient checkpointing can be disabled via training args
  • Test with UnslothTrainer/SFTTrainer
  • Confirm no regression in default behavior (gradient checkpointing enabled by default)

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @sbhavani, 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 issue where the UnslothTrainer and SFTTrainer were not properly honoring the gradient_checkpointing configuration. By modifying how the model.for_training() method is invoked, the change ensures that the model's training behavior, particularly regarding memory optimization through gradient checkpointing, aligns with the user's specified training arguments. This leads to more predictable and controllable training processes.

Highlights

  • Bug Fix: Resolves issue [Bug] Gradient checkpointing is always enabled when using UnslothTrainer #2362 by ensuring that UnslothTrainer and SFTTrainer correctly respect the gradient_checkpointing setting provided in training arguments.
  • Gradient Checkpointing Control: The model.for_training() calls in unsloth/models/rl.py are updated to explicitly pass the use_gradient_checkpointing parameter, making its activation conditional on the training arguments.
  • Configuration Adherence: This change guarantees that gradient checkpointing is only enabled when it is explicitly set in the training arguments, providing more precise control over memory usage during training.
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 correctly addresses the issue of passing the gradient_checkpointing parameter to model.for_training(). The changes made in the __init__ method and the patching logic are well-implemented.

I've added one comment pointing out another location in unsloth/models/rl.py where model.for_training() is called without the gradient_checkpointing parameter. Applying this suggestion will ensure the fix is comprehensive across the file. Overall, this is a good fix that improves the configurability of the trainer.

args._n_gpu = 1
if "model" in locals() and hasattr(model, "for_training"):
model.for_training()
model.for_training(use_gradient_checkpointing=getattr(args, 'gradient_checkpointing', True))
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This correctly passes the gradient_checkpointing argument. To make this fix complete, another instance of model.for_training() in this file should also be updated.

In the prepare_for_training_mode wrapper (around line 239), the call is still self.model.for_training(). It should be updated to self.model.for_training(use_gradient_checkpointing=getattr(self.args, 'gradient_checkpointing', True)) to consistently respect the training arguments throughout the trainer's lifecycle.

@danielhanchen danielhanchen merged commit e64c53a into unslothai:main Dec 1, 2025
1 check passed
@danielhanchen
Copy link
Contributor

Wonderful thank you!

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.

[Bug] Gradient checkpointing is always enabled when using UnslothTrainer

2 participants