Skip to content
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
f50e74d
initial liger support
kashif Jan 14, 2025
e3eebd3
fix outputs
kashif Jan 15, 2025
2d82b39
fix config merge conflict
kashif Jan 15, 2025
50d341e
Merge branch 'main' into liger-dpo
kashif Jan 15, 2025
8ae06b1
fix comment
kashif Jan 15, 2025
cc2b7b9
fix peft training
kashif Jan 15, 2025
03fd005
use parametrized
qgallouedec Jan 17, 2025
5f4110f
raise error as soon as dep is not met
qgallouedec Jan 17, 2025
b22eb24
move param to the right section
qgallouedec Jan 17, 2025
b8e6f8c
reducing memory doc
qgallouedec Jan 17, 2025
6310dbd
use liger specifc method
kashif Jan 21, 2025
bdca4f1
Merge branch 'main' into liger-dpo
kashif Jan 21, 2025
5efe4d0
Merge branch 'main' into liger-dpo
kashif Jan 22, 2025
dbece54
update return signature
kashif Jan 22, 2025
d21bd81
Merge branch 'main' into liger-dpo
kashif Mar 14, 2025
c441925
fix typo
kashif Jan 22, 2025
f1af5d6
fix tests
kashif Mar 14, 2025
2814228
truncation and logits to keep
kashif Mar 15, 2025
7c821b5
Fix liger loss mask and input for liger (#3346)
vaibhavjindal Apr 23, 2025
baba3bb
fix formatting
kashif Apr 23, 2025
f41c023
Merge branch 'main' into liger-dpo
kashif Apr 23, 2025
94422db
Merge branch 'main' into liger-dpo
kashif May 5, 2025
614e5d9
update liger to fix dpo bug
kashif May 5, 2025
8939796
skip test for python 3.9
kashif May 5, 2025
50a4adc
fix asserts
kashif May 5, 2025
11fa70c
Merge branch 'main' into liger-dpo
kashif Jun 10, 2025
d47ab9f
formatting
kashif Jun 10, 2025
30ee209
fix issue due to wraparound from roll
kashif Jun 10, 2025
4a46a0d
revert back tol
kashif Jun 10, 2025
9fbdf80
fix skip test
kashif Jun 10, 2025
9178c67
use unwrapped model
kashif Jun 11, 2025
68aba88
fix and expand doc
qgallouedec Jun 11, 2025
6de6070
nits in test
qgallouedec Jun 11, 2025
e067a15
style
qgallouedec Jun 11, 2025
28963ce
fix doc
qgallouedec Jun 11, 2025
588ebf1
fix for review
kashif Jun 11, 2025
f04100c
Merge branch 'main' into liger-dpo
kashif Jun 12, 2025
3e5802a
add Flush and truncate to liger
kashif Jun 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion docs/source/reducing_memory_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Sequence lengths in the dataset can vary widely. When data is batched, sequences

To reduce memory usage, it's important to truncate sequences to a reasonable length. While TRL trainers truncate sequences by default, you may want to adjust the default truncation length to better align with your specific use case.

<hfoptions id="dpo">
<hfoptions id="truncation">
<hfoption id="DPO">

DPO truncation is applied first to the prompt and to the completion via the `max_prompt_length` and `max_completion_length` parameters. The `max_length` parameter is then used to truncate the resulting sequence.
Expand Down Expand Up @@ -100,6 +100,48 @@ Packing may cause batch contamination, where adjacent sequences influence one an

</Tip>

## Liger for reducing peak memory usage

> [Liger Kernel](https://github.com/linkedin/Liger-Kernel) is a collection of Triton kernels designed specifically for LLM training. It can effectively increase multi-GPU training throughput by 20% and reduces memory usage by 60%.

For more information, see [Liger Kernel Integration](liger_kernel_integration)

<hfoptions id="liger">
<hfoption id="DPO">

To use Liger for reducing peak memory usage, use the following code snippet:

```python
from trl import DPOConfig

training_args = DPOConfig(..., use_liger_loss=True)
```

</hfoption>
<hfoption id="GRPO">

To use Liger for reducing peak memory usage, use the following code snippet:

```python
from trl import GRPOConfig

training_args = GRPOConfig(..., use_liger_loss=True)
```

</hfoption>
<hfoption id="KTO">

To use Liger for reducing peak memory usage, use the following code snippet:

```python
from trl import KTOConfig

training_args = KTOConfig(..., use_liger_loss=True)
```

</hfoption>
</hfoptions>

## Padding-free

Padding-free batching is an alternative approach for reducing memory usage. In this method, a batch is first sampled and then flattened into a single sequence, avoiding padding. Unlike packing, which can result in incomplete sequences by combining parts of different samples, padding-free batching ensures that all sequences remain complete and intact.
Expand Down
75 changes: 75 additions & 0 deletions tests/test_dpo_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import tempfile
import unittest
from unittest.mock import MagicMock
Expand All @@ -31,6 +32,7 @@
)
from transformers.testing_utils import (
get_device_properties,
require_liger_kernel,
require_peft,
require_torch_gpu_if_bnb_not_multi_backend_enabled,
require_vision,
Expand Down Expand Up @@ -1296,6 +1298,79 @@ def test_train_with_length_desensitization(self):
if param.sum() != 0: # ignore 0 biases
self.assertFalse(torch.allclose(param, new_param, rtol=1e-12, atol=1e-12))

@unittest.skipUnless(sys.version_info >= (3, 10), "Liger kernel is not supported on Python 3.9")
@parameterized.expand([(0.1,), (0.5,)])
@require_liger_kernel
def test_dpo_trainer_with_liger(self, beta):
"""Test DPO trainer with Liger loss enabled.

This test verifies that:
1. Training runs successfully with Liger loss
2. Model parameters update as expected
3. Loss values are reasonable and finite
4. Training works with both default and custom beta values
"""

with tempfile.TemporaryDirectory() as tmp_dir:
training_args = DPOConfig(
output_dir=tmp_dir,
per_device_train_batch_size=2,
do_eval=True,
eval_steps=1,
max_steps=3,
remove_unused_columns=False,
gradient_accumulation_steps=1,
learning_rate=9e-1,
eval_strategy="steps",
beta=beta,
use_liger_loss=True, # Enable Liger loss
report_to="none",
)

dummy_dataset = load_dataset("trl-internal-testing/zen", "standard_preference")

trainer = DPOTrainer(
model=self.model,
ref_model=self.ref_model, # Add reference model
args=training_args,
processing_class=self.tokenizer,
train_dataset=dummy_dataset["train"],
eval_dataset=dummy_dataset["test"],
)

# Store initial parameters
previous_trainable_params = {n: param.clone() for n, param in trainer.model.named_parameters()}

# Train the model
train_output = trainer.train()

# Verify training completed successfully
self.assertIsNotNone(train_output)
self.assertIsNotNone(trainer.state.log_history[-1]["train_loss"])

# Verify loss is finite
self.assertTrue(np.isfinite(trainer.state.log_history[-1]["train_loss"]))

# Check parameters have been updated
for n, param in previous_trainable_params.items():
new_param = trainer.model.get_parameter(n)
# Only check non-zero parameters
if param.sum() != 0:
self.assertFalse(torch.equal(param, new_param))
# Verify new parameters are finite
self.assertTrue(torch.isfinite(new_param).all())

# Verify model can still do forward pass after training
dummy_batch = next(iter(trainer.get_train_dataloader()))
model_inputs = {
"input_ids": dummy_batch["prompt_input_ids"],
"attention_mask": dummy_batch["prompt_attention_mask"],
}
with torch.no_grad():
output = trainer.model(**model_inputs)
self.assertIsNotNone(output)
self.assertFalse("loss" in output.keys())


@require_vision
class DPOVisionTrainerTester(unittest.TestCase):
Expand Down
17 changes: 17 additions & 0 deletions trl/trainer/dpo_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ class DPOConfig(TrainingArguments):
- `"apo_zero"`: APO-zero loss from the [APO](https://huggingface.co/papers/2408.06266) paper.
- `"apo_down"`: APO-down loss from the [APO](https://huggingface.co/papers/2408.06266) paper.

use_liger_loss (`bool`, *optional*, defaults to `False`):
Whether to use Liger loss.
base_model_attribute_name (`str`, *optional*, defaults to `"model"`):
Name of the attribute in the model that contains the base model. This is used to get the base model from
the model when the model does not have a `get_decoder` method in the case when `use_liger_loss` is `True`.
beta (`float`, *optional*, defaults to `0.1`):
Parameter controlling the deviation from the reference model. Higher β means less deviation from the
reference model. For the IPO loss (`loss_type="ipo"`), β is the regularization parameter denoted by τ in
Expand Down Expand Up @@ -318,6 +323,18 @@ class DPOConfig(TrainingArguments):
],
},
)
use_liger_loss: bool = field(
default=False,
metadata={"help": "Whether to use Liger loss."},
)
base_model_attribute_name: str = field(
default="model",
metadata={
"help": "Name of the attribute in the model that contains the base model. This is used to get the base "
"model from the model when the model does not have a `get_decoder` method in the case when "
"`use_liger_loss` is `True`."
},
)
beta: float = field(
default=0.1,
metadata={
Expand Down
Loading
Loading