You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/source/customization.md
+64Lines changed: 64 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -111,3 +111,67 @@ training_args = DPOConfig(
111
111
gradient_accumulation_steps=8,
112
112
)
113
113
```
114
+
115
+
## Change the training objective
116
+
117
+
Subclassing a trainer is the simplest way to customize training. The loss is defined in the `compute_loss` method, so to train with a different objective, subclass the trainer and override it. Data preparation, generation, logging, and checkpointing are inherited, so the subclass holds only what actually changes.
118
+
119
+
```python
120
+
import torch
121
+
122
+
from trl import DPOTrainer
123
+
from trl.trainer.utils import selective_log_softmax
Subclass the config to declare the parameters your loss needs:
148
+
149
+
```python
150
+
from dataclasses import dataclass, field
151
+
152
+
from trl import DPOConfig
153
+
154
+
155
+
@dataclass
156
+
classMyDPOConfig(DPOConfig):
157
+
my_coef: float= field(default=0.1, metadata={"help": "Coefficient of the custom term."})
158
+
```
159
+
160
+
### Change the batch format
161
+
162
+
When the loss needs inputs that the default collator doesn't produce, pass your own collator, and override `_prepare_dataset` to leave the dataset untouched when it is already in the expected format:
The block-diffusion SFT example [`examples/sft_diffusion_gemma/sft_diffusion_gemma.py`](https://github.com/huggingface/trl/blob/main/examples/sft_diffusion_gemma/sft_diffusion_gemma.py) combines the three: `DiffusionGemmaSFTConfig` extends [`SFTConfig`] with the canvas and corruption parameters, and `DiffusionGemmaSFTTrainer` extends [`SFTTrainer`] and replaces the autoregressive cross-entropy with a block-diffusion denoising objective. Neither requires a change to the library.
176
+
177
+
[Antidoom](https://github.com/Liquid4All/antidoom), an open-source tool from [Liquid AI](https://huggingface.co/LiquidAI), extends [`DPOTrainer`] with Final Token Preference Optimization ([Antislop](https://huggingface.co/papers/2510.15061)), a preference loss over a single token position that reduces repetition loops in reasoning models.
0 commit comments