Document how to change the training objective by subclassing a trainer - #6918
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
reviewed
Aug 25, 2026
| ... | ||
| ``` | ||
|
|
||
| TRL relies on this pattern internally. [`experimental.gkd.GKDTrainer`] subclasses [`SFTTrainer`] and overrides `compute_loss` to replace the cross-entropy with a generalized Jensen-Shannon divergence against a teacher model. [`experimental.gold.GOLDTrainer`] extends [`SFTTrainer`] and [`experimental.gmpo.GMPOTrainer`] extends [`GRPOTrainer`] the same way. |
Member
There was a problem hiding this comment.
can you please remove this? internally, we're actually trying to do the opposite internally.
What you can say instead is that it's the simplest way to customize training, something like this.
qgallouedec
reviewed
Aug 25, 2026
qgallouedec
left a comment
Member
There was a problem hiding this comment.
super useful, thanks, a few modifications needed
| my_coef: float = field(default=0.1, metadata={"help": "Coefficient of the custom term."}) | ||
| ``` | ||
|
|
||
| [`experimental.gkd.GKDConfig`] extends [`SFTConfig`] and [`experimental.gmpo.GMPOConfig`] extends [`GRPOConfig`] in the same way. |
Member
There was a problem hiding this comment.
same here, please remove this line
| class MyDPOTrainer(DPOTrainer): | ||
| def __init__(self, *args, **kwargs): | ||
| super().__init__(*args, **kwargs) | ||
| self.data_collator = my_collator |
Member
There was a problem hiding this comment.
data_collator is an argument of DPOTrainer, we shouldn't need to override the init.
Co-authored-by: Quentin Gallouédec <45557362+qgallouedec@users.noreply.github.com>
Member
Author
|
updated, thanks for the review! @qgallouedec |
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.
What does this PR do?
Adds a
Change the training objectivesection to the training customization guide.Every section in
customization.mdtoday customizes training by passing an argument: optimizers and schedulers, an 8-bit reference model, callbacks, evaluation metrics, mixed precision, gradient accumulation. The guide says nothing about the case where the objective itself differs, even though subclassing a trainer and overridingcompute_lossis a pattern the library relies on throughout:GKDTrainerandGOLDTrainerextendSFTTrainerGMPOTrainer,MiniLLMTrainerandGRPOWithReplayBufferTrainerextendGRPOTrainerXPOTrainerandNashMDTrainerextendOnlineDPOTrainerexamples/sft_diffusion_gemma/sft_diffusion_gemma.pyextendsSFTTrainerwith a block-diffusion objective, entirely from an example scriptToday the only traces of this in the docs are a one-line pointer in
sft_trainer.mdand a row inexample_overview.md.The new section documents the three override points (
compute_loss, the config, the collator plus_prepare_dataset), then links to the block-diffusion example as a complete in-repo case and to one external project using the same pattern.Note on scope: this documents subclassing a public trainer for a small, local delta, which is what GMPO, GKD, GOLD, XPO and NashMD do. It does not touch the self-contained trainer route on
_BaseTrainerdescribed inAGENTS.md, which remains the way to add a full new method.Docs only, no code changes.
Before submitting
AI writing disclosure
Who can review?
Anyone in the community is free to review the PR once the tests have passed.
Note
Low Risk
Documentation-only change with no runtime or API impact.
Overview
Adds a Change the training objective section to the training customization guide (
customization.md), filling a gap where other sections only cover config/callback-style tweaks.The new content explains subclassing a trainer (e.g.
DPOTrainer) and overridingcompute_loss, with a concrete hinge-loss DPO example usingselective_log_softmax. It also covers extendingDPOConfigfor custom hyperparameters, supplying a custom collator and skipping default dataset prep via_prepare_dataset, and points readers to the in-repo block-diffusion SFT example and the external Antidoom/AntislopDPOTrainerextension as end-to-end references.Reviewed by Cursor Bugbot for commit 8710f4a. Bugbot is set up for automated code reviews on this repo. Configure here.