Align KTO with DPO: Add disable_gradient_checkpointing to ref model forward passes#5900
Merged
Merged
Conversation
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
qgallouedec
approved these changes
Jun 1, 2026
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.
Align KTO with DPO: Add disable_gradient_checkpointing to ref model forward passes.
Part of:
This PR improves the handling of gradient checkpointing during reference log probability computations in the
KTOTrainerby ensuring gradient checkpointing is disabled when running inference on the reference model. This prevents unnecessary memory usage and potential side effects during evaluation.Solution
This PR adds the
disable_gradient_checkpointingcontext manager to all reference model forward passes inKTOTrainer, matching the pattern already used inDPOTrainer.Changes
Gradient checkpointing management:
disable_gradient_checkpointingfrommodels.utilsand included it alongside other utility imports inkto_trainer.py.compute_ref_log_probs,_compute_loss_liger, and_compute_loss) with thedisable_gradient_checkpointingcontext manager to ensure checkpointing is turned off during these computations._compute_loss_liger, thetorch.no_gradwas missing entirely: the ref hidden states were computed with gradients enabled, which is a bug.Note
Low Risk
Training-path correctness and memory behavior for ref inference only; no auth, data, or API surface changes.
Overview
KTOTrainer now matches DPOTrainer for reference-policy inference: all reference forward paths run inside
disable_gradient_checkpointing(using traininggradient_checkpointing_kwargs) so checkpointing stays off duringno_gradref log-prob work—less memory, fewer PyTorch warnings, and behavior consistent with other preference trainers.That wrapper is applied in
compute_ref_log_probs, the on-the-fly ref branch in_compute_loss, and the Liger path in_compute_loss_liger. On the Liger path, ref decoder forward is also wrapped intorch.no_grad()(it was missing before), so ref hidden states are no longer computed with gradients enabled.Reviewed by Cursor Bugbot for commit d082d3c. Bugbot is set up for automated code reviews on this repo. Configure here.