Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
176 commits
Select commit Hold shift + click to select a range
6983b6f
Pin the distillation objective before refactoring
qgallouedec Jul 19, 2026
d443d3b
Pin on-policy and off-policy training end to end
qgallouedec Jul 19, 2026
a8bb14c
Document the num_items_in_batch denominator bug
qgallouedec Jul 19, 2026
98eef79
Move IW-OPD to a dedicated experimental trainer
qgallouedec Jul 19, 2026
b65f8a7
Extract the teacher-server path into ServerDistillationTrainer
qgallouedec Jul 19, 2026
f75cd7e
Remove the local sparse top-1 loss path from the base trainer
qgallouedec Jul 19, 2026
6458636
Remove top-k support from the base generalized JSD loss
qgallouedec Jul 19, 2026
166808b
Validate the teacher by vocab size instead of tokenizer identity
qgallouedec Jul 19, 2026
952b0c3
Deprecate `lmbda` on/off-policy mixing
qgallouedec Jul 19, 2026
55e5973
Point the GKD paper reproduction at GKDTrainer
qgallouedec Jul 19, 2026
0b72a4e
Remove `lmbda` and the off-policy training branch
qgallouedec Jul 19, 2026
2527d3d
style
qgallouedec Jul 20, 2026
1c5bdea
Merge branch '04-move-iw-opd' into 05-extract-server-distillation
qgallouedec Jul 20, 2026
f232c18
Merge branch '05-extract-server-distillation' into 06-remove-local-sp…
qgallouedec Jul 20, 2026
7aab597
Merge branch '06-remove-local-sparse-top1' into 07-remove-topk-from-jsd
qgallouedec Jul 20, 2026
6efeb9f
Merge branch '07-remove-topk-from-jsd' into 08-teacher-vocab-validation
qgallouedec Jul 20, 2026
0a4a3d6
Merge branch '08-teacher-vocab-validation' into 09-deprecate-lmbda
qgallouedec Jul 20, 2026
5eabac4
Merge branch '09-deprecate-lmbda' into 10-move-gkd-paper-repro
qgallouedec Jul 20, 2026
aa0b5fb
Merge branch '10-move-gkd-paper-repro' into 11-remove-lmbda-offpolicy
qgallouedec Jul 20, 2026
e058764
style
qgallouedec Jul 20, 2026
4c65faf
Merge branch '05-extract-server-distillation' into 06-remove-local-sp…
qgallouedec Jul 20, 2026
3500348
Merge branch '06-remove-local-sparse-top1' into 07-remove-topk-from-jsd
qgallouedec Jul 20, 2026
de167a9
Merge branch '07-remove-topk-from-jsd' into 08-teacher-vocab-validation
qgallouedec Jul 20, 2026
1908e5f
Merge branch '08-teacher-vocab-validation' into 09-deprecate-lmbda
qgallouedec Jul 20, 2026
5a4f715
Merge branch '09-deprecate-lmbda' into 10-move-gkd-paper-repro
qgallouedec Jul 20, 2026
f270c1f
Merge branch '10-move-gkd-paper-repro' into 11-remove-lmbda-offpolicy
qgallouedec Jul 20, 2026
456df91
Fix IW-OPD paper metadata used in the model card
qgallouedec Jul 20, 2026
9ecf348
Merge branch '04-move-iw-opd' into 05-extract-server-distillation
qgallouedec Jul 20, 2026
28382eb
Merge branch '05-extract-server-distillation' into 06-remove-local-sp…
qgallouedec Jul 20, 2026
c4acfb4
Merge branch '06-remove-local-sparse-top1' into 07-remove-topk-from-jsd
qgallouedec Jul 20, 2026
d95c977
Merge branch '07-remove-topk-from-jsd' into 08-teacher-vocab-validation
qgallouedec Jul 20, 2026
f64f96c
Merge branch '08-teacher-vocab-validation' into 09-deprecate-lmbda
qgallouedec Jul 20, 2026
614bb6b
Merge branch '09-deprecate-lmbda' into 10-move-gkd-paper-repro
qgallouedec Jul 20, 2026
5cd0e91
Merge branch '10-move-gkd-paper-repro' into 11-remove-lmbda-offpolicy
qgallouedec Jul 20, 2026
26038dd
Remove stale on/off-policy comments after the off-policy removal
qgallouedec Jul 20, 2026
3515b22
Accept a `prompt` column alongside `messages`
qgallouedec Jul 20, 2026
4e5d06f
Accept a `prompt` column alongside `messages`
qgallouedec Jul 20, 2026
dfbf0e5
style
qgallouedec Jul 20, 2026
89494f2
Relax self-distillation train-loss assertion to tolerate fp noise
qgallouedec Jul 20, 2026
9b5ad81
Fix server-config construction to only copy dataclass init fields
qgallouedec Jul 20, 2026
d616372
Merge branch '06-remove-local-sparse-top1' into 07-remove-topk-from-jsd
qgallouedec Jul 20, 2026
1341b51
Merge branch '07-remove-topk-from-jsd' into 08-teacher-vocab-validation
qgallouedec Jul 20, 2026
33667d3
Merge branch 'main' into 01-pin-distillation-objective
qgallouedec Jul 20, 2026
041e337
Merge branch '01-pin-distillation-objective' into 02-pin-on-and-off-p…
qgallouedec Jul 20, 2026
9e989d1
Merge branch '02-pin-on-and-off-policy-training' into 03-document-num…
qgallouedec Jul 20, 2026
b2c2b73
Merge branch '03-document-num-items-in-batch-bug' into 04-move-iw-opd
qgallouedec Jul 20, 2026
97cb164
Merge branch '04-move-iw-opd' into 05-extract-server-distillation
qgallouedec Jul 20, 2026
7db5120
Merge branch '05-extract-server-distillation' into 06-remove-local-sp…
qgallouedec Jul 20, 2026
2d2d32f
Merge branch '06-remove-local-sparse-top1' into 07-remove-topk-from-jsd
qgallouedec Jul 20, 2026
e63069d
Merge branch '07-remove-topk-from-jsd' into 08-teacher-vocab-validation
qgallouedec Jul 20, 2026
e4764cc
Merge branch '08-teacher-vocab-validation' into 09-deprecate-lmbda
qgallouedec Jul 20, 2026
800b68b
Merge branch '09-deprecate-lmbda' into 10-move-gkd-paper-repro
qgallouedec Jul 20, 2026
b290c1a
Merge branch '10-move-gkd-paper-repro' into 11-remove-lmbda-offpolicy
qgallouedec Jul 20, 2026
cad0c5e
Merge branch '11-remove-lmbda-offpolicy' into 12-accept-prompt-column
qgallouedec Jul 20, 2026
c978997
0.5 -> 0.25
qgallouedec Jul 20, 2026
acc70cc
Merge branch 'main' into 01-pin-distillation-objective
qgallouedec Jul 20, 2026
854d89d
Merge branch '01-pin-distillation-objective' into 02-pin-on-and-off-p…
qgallouedec Jul 20, 2026
b9f99f1
Diverge the teacher in the on/off-policy param-update pin
qgallouedec Jul 20, 2026
911b0e5
Merge branch 'main' into 01-pin-distillation-objective
qgallouedec Jul 20, 2026
6e8a528
Merge branch '01-pin-distillation-objective' into 02-pin-on-and-off-p…
qgallouedec Jul 20, 2026
fd67ed4
Merge branch '02-pin-on-and-off-policy-training' into 03-document-num…
qgallouedec Jul 20, 2026
a4831c6
Merge branch '03-document-num-items-in-batch-bug' into 04-move-iw-opd
qgallouedec Jul 20, 2026
222615d
Merge branch '04-move-iw-opd' into 05-extract-server-distillation
qgallouedec Jul 20, 2026
15a7f6f
Merge branch '05-extract-server-distillation' into 06-remove-local-sp…
qgallouedec Jul 20, 2026
34e801a
Merge branch '06-remove-local-sparse-top1' into 07-remove-topk-from-jsd
qgallouedec Jul 20, 2026
0a79977
Merge branch '07-remove-topk-from-jsd' into 08-teacher-vocab-validation
qgallouedec Jul 20, 2026
cff3b68
Merge branch '08-teacher-vocab-validation' into 09-deprecate-lmbda
qgallouedec Jul 20, 2026
1f8690a
Merge branch '09-deprecate-lmbda' into 10-move-gkd-paper-repro
qgallouedec Jul 20, 2026
4871948
Merge branch '10-move-gkd-paper-repro' into 11-remove-lmbda-offpolicy
qgallouedec Jul 20, 2026
8057ca4
Merge branch '11-remove-lmbda-offpolicy' into 12-accept-prompt-column
qgallouedec Jul 20, 2026
a0d5328
Merge branch 'main' into 02-pin-on-and-off-policy-training
qgallouedec Jul 20, 2026
f750ddd
Merge branch '02-pin-on-and-off-policy-training' into 03-document-num…
qgallouedec Jul 20, 2026
d0d7de3
Merge branch '03-document-num-items-in-batch-bug' into 04-move-iw-opd
qgallouedec Jul 20, 2026
e8ac1df
Merge branch '04-move-iw-opd' into 05-extract-server-distillation
qgallouedec Jul 20, 2026
0b4fa84
Merge branch '05-extract-server-distillation' into 06-remove-local-sp…
qgallouedec Jul 20, 2026
b4f7d75
Merge branch '06-remove-local-sparse-top1' into 07-remove-topk-from-jsd
qgallouedec Jul 20, 2026
f7d9dc8
Merge branch '07-remove-topk-from-jsd' into 08-teacher-vocab-validation
qgallouedec Jul 20, 2026
5914158
Merge branch '08-teacher-vocab-validation' into 09-deprecate-lmbda
qgallouedec Jul 20, 2026
b8f85a4
Merge branch '09-deprecate-lmbda' into 10-move-gkd-paper-repro
qgallouedec Jul 20, 2026
1492b20
Merge branch '10-move-gkd-paper-repro' into 11-remove-lmbda-offpolicy
qgallouedec Jul 20, 2026
d8892e4
Merge branch '11-remove-lmbda-offpolicy' into 12-accept-prompt-column
qgallouedec Jul 20, 2026
26eba3c
remove unused import
qgallouedec Jul 20, 2026
f51acd6
Merge branch '06-remove-local-sparse-top1' into 07-remove-topk-from-jsd
qgallouedec Jul 20, 2026
0462f03
Merge branch '07-remove-topk-from-jsd' into 08-teacher-vocab-validation
qgallouedec Jul 20, 2026
ce31b5d
Merge branch '08-teacher-vocab-validation' into 09-deprecate-lmbda
qgallouedec Jul 20, 2026
989ae4d
Merge branch '09-deprecate-lmbda' into 10-move-gkd-paper-repro
qgallouedec Jul 20, 2026
15e24cc
Merge branch '10-move-gkd-paper-repro' into 11-remove-lmbda-offpolicy
qgallouedec Jul 20, 2026
3cbd70f
Merge branch '11-remove-lmbda-offpolicy' into 12-accept-prompt-column
qgallouedec Jul 20, 2026
24b235c
Merge remote-tracking branch 'origin/main' into pr/qgallouedec/6452
qgallouedec Jul 20, 2026
5e05b9d
Rework the num_items_in_batch xfail: applied denominator, on-policy
qgallouedec Jul 20, 2026
23784c3
Use a non-strict xfail for the num_items_in_batch pin
qgallouedec Jul 20, 2026
7615bfe
Merge branch '03-document-num-items-in-batch-bug' into 04-move-iw-opd
qgallouedec Jul 20, 2026
82a2017
Merge branch '04-move-iw-opd' into 05-extract-server-distillation
qgallouedec Jul 20, 2026
3eaabc7
Merge branch '05-extract-server-distillation' into 06-remove-local-sp…
qgallouedec Jul 20, 2026
4d9ac2b
Merge branch '06-remove-local-sparse-top1' into 07-remove-topk-from-jsd
qgallouedec Jul 20, 2026
e881f07
Merge branch '07-remove-topk-from-jsd' into 08-teacher-vocab-validation
qgallouedec Jul 20, 2026
7632074
Merge branch '08-teacher-vocab-validation' into 09-deprecate-lmbda
qgallouedec Jul 20, 2026
4b3c62a
Merge branch 'main' into 04-move-iw-opd
qgallouedec Jul 20, 2026
685d3f5
Merge branch '04-move-iw-opd' into 05-extract-server-distillation
qgallouedec Jul 20, 2026
7bf9d07
Merge branch '05-extract-server-distillation' into 06-remove-local-sp…
qgallouedec Jul 20, 2026
40f96ed
Merge branch '06-remove-local-sparse-top1' into 07-remove-topk-from-jsd
qgallouedec Jul 20, 2026
40ee117
Merge branch '07-remove-topk-from-jsd' into 08-teacher-vocab-validation
qgallouedec Jul 20, 2026
821d875
Merge branch '08-teacher-vocab-validation' into 09-deprecate-lmbda
qgallouedec Jul 20, 2026
9ad8c2a
Merge remote-tracking branch 'origin/main' into pr/qgallouedec/6454
qgallouedec Jul 20, 2026
fc5b481
Merge branch '05-extract-server-distillation' into 06-remove-local-sp…
qgallouedec Jul 20, 2026
6f8c4a7
Merge branch '06-remove-local-sparse-top1' into 07-remove-topk-from-jsd
qgallouedec Jul 20, 2026
59a804d
Merge branch '07-remove-topk-from-jsd' into 08-teacher-vocab-validation
qgallouedec Jul 20, 2026
f23fc4f
Merge branch '08-teacher-vocab-validation' into 09-deprecate-lmbda
qgallouedec Jul 20, 2026
d3be590
Merge branch '09-deprecate-lmbda' into 10-move-gkd-paper-repro
qgallouedec Jul 20, 2026
ba3aa35
Merge branch '10-move-gkd-paper-repro' into 11-remove-lmbda-offpolicy
qgallouedec Jul 20, 2026
6a2be12
Deprecate `messages`-format datasets
qgallouedec Jul 20, 2026
a41d5df
Merge remote-tracking branch 'origin/main' into pr/qgallouedec/6455
qgallouedec Jul 20, 2026
f069c1e
Merge branch '06-remove-local-sparse-top1' into 07-remove-topk-from-jsd
qgallouedec Jul 20, 2026
58c13be
Merge branch '07-remove-topk-from-jsd' into 08-teacher-vocab-validation
qgallouedec Jul 20, 2026
01b6f67
Merge branch '08-teacher-vocab-validation' into 09-deprecate-lmbda
qgallouedec Jul 20, 2026
3faa04b
Merge branch '09-deprecate-lmbda' into 10-move-gkd-paper-repro
qgallouedec Jul 20, 2026
1375800
Merge branch '10-move-gkd-paper-repro' into 11-remove-lmbda-offpolicy
qgallouedec Jul 20, 2026
43c42f4
Merge remote-tracking branch 'origin/11-remove-lmbda-offpolicy' into …
qgallouedec Jul 20, 2026
1a58484
Merge remote-tracking branch 'origin/main' into 07-remove-topk-from-jsd
qgallouedec Jul 20, 2026
45fdb23
Merge branch '07-remove-topk-from-jsd' into 08-teacher-vocab-validation
qgallouedec Jul 20, 2026
0bca659
Merge branch '08-teacher-vocab-validation' into 09-deprecate-lmbda
qgallouedec Jul 20, 2026
939143d
Merge branch '09-deprecate-lmbda' into 10-move-gkd-paper-repro
qgallouedec Jul 20, 2026
0a08547
Merge branch '10-move-gkd-paper-repro' into 11-remove-lmbda-offpolicy
qgallouedec Jul 20, 2026
901b155
Merge branch '11-remove-lmbda-offpolicy' into 12-accept-prompt-column
qgallouedec Jul 20, 2026
a056303
Merge branch '12-accept-prompt-column' into 13-deprecate-messages-dat…
qgallouedec Jul 20, 2026
93f6f55
Drop the loss_top_k usage-tips bullet from the docs
qgallouedec Jul 20, 2026
dc4c01b
Merge branch '07-remove-topk-from-jsd' into 08-teacher-vocab-validation
qgallouedec Jul 20, 2026
356eaab
Merge branch '08-teacher-vocab-validation' into 09-deprecate-lmbda
qgallouedec Jul 20, 2026
bca4b35
Merge branch '09-deprecate-lmbda' into 10-move-gkd-paper-repro
qgallouedec Jul 20, 2026
825de01
Merge branch 'main' into 08-teacher-vocab-validation
qgallouedec Jul 20, 2026
6d09aa1
Merge branch '08-teacher-vocab-validation' into 09-deprecate-lmbda
qgallouedec Jul 20, 2026
5b99699
Merge branch '09-deprecate-lmbda' into 10-move-gkd-paper-repro
qgallouedec Jul 20, 2026
e185842
Merge branch '10-move-gkd-paper-repro' into 11-remove-lmbda-offpolicy
qgallouedec Jul 20, 2026
4365c60
Merge branch '11-remove-lmbda-offpolicy' into 12-accept-prompt-column
qgallouedec Jul 20, 2026
a5fa96c
Merge branch '12-accept-prompt-column' into 13-deprecate-messages-dat…
qgallouedec Jul 20, 2026
94a185d
Merge branch 'main' into 09-deprecate-lmbda
qgallouedec Jul 20, 2026
e9365a6
Merge branch '09-deprecate-lmbda' into 10-move-gkd-paper-repro
qgallouedec Jul 20, 2026
a320f99
Merge branch '10-move-gkd-paper-repro' into 11-remove-lmbda-offpolicy
qgallouedec Jul 20, 2026
16ec8c3
Merge branch '11-remove-lmbda-offpolicy' into 12-accept-prompt-column
qgallouedec Jul 20, 2026
e6b1d37
Merge branch '12-accept-prompt-column' into 13-deprecate-messages-dat…
qgallouedec Jul 20, 2026
8567f94
Merge branch 'main' into 10-move-gkd-paper-repro
qgallouedec Jul 20, 2026
9ef74d9
Merge branch '10-move-gkd-paper-repro' into 11-remove-lmbda-offpolicy
qgallouedec Jul 20, 2026
109333e
Merge branch '11-remove-lmbda-offpolicy' into 12-accept-prompt-column
qgallouedec Jul 20, 2026
0d76047
Merge branch '12-accept-prompt-column' into 13-deprecate-messages-dat…
qgallouedec Jul 20, 2026
782f088
Merge branch 'main' into 11-remove-lmbda-offpolicy
qgallouedec Jul 20, 2026
88795b0
Merge branch '11-remove-lmbda-offpolicy' into 12-accept-prompt-column
qgallouedec Jul 20, 2026
749efd6
Merge branch '12-accept-prompt-column' into 13-deprecate-messages-dat…
qgallouedec Jul 20, 2026
ddcb836
Drop leftover lmbda=1.0 from the num_items xfail test
qgallouedec Jul 20, 2026
d36d174
Merge branch '11-remove-lmbda-offpolicy' into 12-accept-prompt-column
qgallouedec Jul 20, 2026
c077758
Merge branch '12-accept-prompt-column' into 13-deprecate-messages-dat…
qgallouedec Jul 20, 2026
71883b9
Merge remote-tracking branch 'origin/main' into 12-accept-prompt-column
qgallouedec Jul 21, 2026
ff9a39a
Merge branch '12-accept-prompt-column' into 13-deprecate-messages-dat…
qgallouedec Jul 21, 2026
46d1c26
Fix num_items_in_batch to count generated completion tokens
qgallouedec Jul 21, 2026
010f250
Revert "Fix num_items_in_batch to count generated completion tokens"
qgallouedec Jul 21, 2026
6c60211
Fix num_items_in_batch to count generated completion tokens
qgallouedec Jul 21, 2026
fbf4c3b
style
qgallouedec Jul 21, 2026
fa5e277
Normalize the num_items loss correctly for DDP and grad-accumulation
qgallouedec Jul 21, 2026
8351cb0
Normalize the num_items loss correctly for DDP and grad-accumulation
qgallouedec Jul 21, 2026
289c40c
Switch tests, docs, and example to prompt-only datasets
qgallouedec Jul 21, 2026
b02741c
remove pr desc
qgallouedec Jul 21, 2026
4a9273f
Qualify expected dataset as conversational prompt-only
qgallouedec Jul 21, 2026
2b5b737
Remove messages-format support and prompt-length config
qgallouedec Jul 21, 2026
790e9eb
Merge remote-tracking branch 'origin/main' into 13-deprecate-messages…
Jul 21, 2026
db068b1
Merge branch '13-deprecate-messages-datasets' into 14-fix-num-items
qgallouedec Jul 21, 2026
5f60751
Merge branch '14-fix-num-items' into 15-switch-tests-docs-prompt-only
qgallouedec Jul 21, 2026
14bb155
Merge branch '15-switch-tests-docs-prompt-only' into 16-remove-messag…
qgallouedec Jul 21, 2026
5d9178a
Merge branch 'main' into 14-fix-num-items
qgallouedec Jul 21, 2026
194481e
Merge branch '14-fix-num-items' into 15-switch-tests-docs-prompt-only
qgallouedec Jul 21, 2026
5aac5aa
Merge branch '15-switch-tests-docs-prompt-only' into 16-remove-messag…
qgallouedec Jul 21, 2026
9378f79
Merge branch 'main' into 15-switch-tests-docs-prompt-only
qgallouedec Jul 21, 2026
76115bf
Merge branch '15-switch-tests-docs-prompt-only' into 16-remove-messag…
qgallouedec Jul 21, 2026
f1d4896
Remove stale teacher-server section from the distillation doc
qgallouedec Jul 21, 2026
7cd12e0
Merge branch '15-switch-tests-docs-prompt-only' into 16-remove-messag…
qgallouedec Jul 21, 2026
fe497cd
Merge branch 'main' into 15-switch-tests-docs-prompt-only
qgallouedec Jul 21, 2026
9f68612
Merge branch '15-switch-tests-docs-prompt-only' into 16-remove-messag…
qgallouedec Jul 21, 2026
f07dd4e
Merge remote-tracking branch 'origin/main' into 16-remove-messages-su…
qgallouedec Jul 22, 2026
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
48 changes: 18 additions & 30 deletions tests/experimental/test_distillation_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.

import os
import warnings

import pytest
import torch
Expand All @@ -22,7 +21,7 @@
from transformers import AutoModelForCausalLM, AutoTokenizer

from trl.experimental.distillation import DistillationConfig, DistillationTrainer
from trl.experimental.distillation.distillation_trainer import _DistillationCollator, _RepeatBatchDataLoader
from trl.experimental.distillation.distillation_trainer import _RepeatBatchDataLoader
from trl.experimental.gkd.gkd_trainer import GKDTrainer

from ..testing_utils import TrlTestCase, require_liger_kernel, require_torch_accelerator
Expand Down Expand Up @@ -188,19 +187,6 @@ def setup_method(self):
self.tokenizer = AutoTokenizer.from_pretrained(self.model_id)
self.tokenizer.pad_token = self.tokenizer.eos_token

def test_messages_format_dataset_is_deprecated(self):
"""The `messages` (language-modeling) format is deprecated in favour of a prompt-only `prompt` column."""
messages_example = {"messages": [{"role": "user", "content": "Hi"}, {"role": "assistant", "content": "Hi!"}]}
with pytest.warns(FutureWarning, match="`messages`-format"):
_DistillationCollator(self.tokenizer, max_length=128, max_prompt_length=64)([messages_example])

# The forward-looking `prompt` column must not trigger the deprecation.
with warnings.catch_warnings():
warnings.simplefilter("error", FutureWarning)
_DistillationCollator(self.tokenizer, max_length=128, max_prompt_length=64)(
[{"prompt": [{"role": "user", "content": "Hi"}]}]
)

def _make_args(self, **kwargs):
args = {
"output_dir": self.tmp_dir,
Expand All @@ -212,7 +198,6 @@ def _make_args(self, **kwargs):
"disable_tqdm": True,
"use_cpu": True,
"bf16": False,
"max_length": 128,
"max_completion_length": 32,
"model_init_kwargs": {"dtype": "float32", "device_map": None},
"teacher_model_init_kwargs": {"dtype": "float32", "device_map": None},
Expand All @@ -221,9 +206,7 @@ def _make_args(self, **kwargs):
return DistillationConfig(**args)

def _make_local_trainer(self, **kwargs):
# Messages-format: `_make_batch` reads completion tokens straight from the collator (no generation), which the
# `prompt`-only format cannot provide until generation replaces the collator. Switched to prompt-only then.
dataset = load_dataset("trl-internal-testing/zen", "conversational_language_modeling", split="train")
dataset = load_dataset("trl-internal-testing/zen", "conversational_prompt_only", split="train")
return DistillationTrainer(
model=self.model_id,
teacher_model=self.model_id,
Expand All @@ -232,14 +215,6 @@ def _make_local_trainer(self, **kwargs):
processing_class=self.tokenizer,
)

def _make_batch(self, trainer):
examples = [trainer.train_dataset[i] for i in range(2)]
return trainer.data_collator(examples)

@staticmethod
def _move_batch_to_device(batch, device):
return {key: value.to(device) for key, value in batch.items()}

def test_distillation_trainer_train_runs_with_local_teacher(self):
training_args = self._make_args(
dataloader_drop_last=True,
Expand Down Expand Up @@ -398,11 +373,24 @@ def test_loss_normalizes_by_num_items_in_batch(self):
for p in trainer.teacher_model.parameters():
p.add_(0.5 * torch.randn_like(p))

batch = self._move_batch_to_device(self._make_batch(trainer), trainer.accelerator.device)
# The collator is prompt-only (completions come from on-policy generation); build a batch with completion
# tokens directly to exercise the loss reduction.
device = trainer.accelerator.device
prompt_length, completion_length = 4, 3
seq_length = prompt_length + completion_length
input_ids = torch.randint(0, trainer.model.config.vocab_size, (2, seq_length), device=device)
labels = input_ids.clone()
labels[:, :prompt_length] = -100
batch = {
"input_ids": input_ids,
"attention_mask": torch.ones_like(input_ids),
"labels": labels,
"prompts": input_ids[:, :prompt_length],
"prompt_attention_mask": torch.ones(2, prompt_length, dtype=torch.long, device=device),
}

# Number of valid (non-ignored) tokens in the local batch, sliced the same way `compute_loss` does.
prompt_length = trainer._compute_prompt_length(batch)
num_valid = (batch["labels"][:, prompt_length:] != -100).sum()
num_valid = (labels[:, prompt_length:] != -100).sum()

trainer.model.eval()
with torch.no_grad():
Expand Down
14 changes: 2 additions & 12 deletions tests/experimental/test_server_distillation_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,8 @@ def _canned_teacher_logprobs(**kwargs):
def _variable_length_dataset():
return Dataset.from_list(
[
{"messages": [{"role": "user", "content": "What's 2+2?"}, {"role": "assistant", "content": "4."}]},
{
"messages": [
{"role": "user", "content": "Name three primary colors."},
{
"role": "assistant",
"content": "Red, green, and blue are the three primary colors commonly used in additive color mixing.",
},
]
},
{"prompt": [{"role": "user", "content": "What's 2+2?"}]},
{"prompt": [{"role": "user", "content": "Name three primary colors."}]},
]
)

Expand Down Expand Up @@ -272,8 +264,6 @@ def _run_one_step(self, bs, ga, monkeypatch):
per_device_train_batch_size=bs,
gradient_accumulation_steps=ga,
learning_rate=1e-4,
max_length=64,
max_prompt_length=32,
max_completion_length=32,
teacher_model_server_url="http://fake-teacher.invalid:8000",
loss_top_k=1,
Expand Down
27 changes: 0 additions & 27 deletions trl/experimental/distillation/distillation_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ class DistillationConfig(_BaseConfig):
Whether to allow loading models and tokenizers that ship custom Python code from the Hub. Forwarded to
[`~transformers.AutoModelForCausalLM.from_pretrained`] and
[`~transformers.AutoTokenizer.from_pretrained`], for both the student and teacher.
max_length (`int` or `None`, *optional*, defaults to `1024`):
Maximum total sequence length (prompt + completion) for tokenization and truncation.

> Parameters that control the distillation

Expand All @@ -55,9 +53,6 @@ class DistillationConfig(_BaseConfig):
JSD.
max_completion_length (`int`, *optional*, defaults to `512`):
Maximum number of tokens to generate per completion during on-policy generation.
max_prompt_length (`int` or `None`, *optional*):
Maximum number of tokens for the prompt. If `None`, auto-computed as `max_length - max_completion_length`.
Prompts are truncated according to the tokenizer's `truncation_side` setting.
disable_dropout (`bool`, *optional*, defaults to `True`):
Whether to disable dropout in the student model during training.

Expand Down Expand Up @@ -143,11 +138,6 @@ class DistillationConfig(_BaseConfig):
"student and teacher."
},
)
max_length: int | None = field(
default=1024,
metadata={"help": "Maximum total sequence length (prompt + completion) for tokenization and truncation."},
)

# Overridden defaults
learning_rate: float = field(
default=1e-6,
Expand All @@ -172,14 +162,6 @@ class DistillationConfig(_BaseConfig):
default=512,
metadata={"help": "Maximum number of tokens to generate per completion."},
)
max_prompt_length: int | None = field(
default=None,
metadata={
"help": "Maximum number of tokens for the prompt. If None, auto-computed as "
"max_length - max_completion_length. Prompts are truncated according to the "
"tokenizer's truncation_side setting."
},
)
disable_dropout: bool = field(
default=True,
metadata={"help": "Whether to disable dropout in the student model during training."},
Expand Down Expand Up @@ -306,15 +288,6 @@ def __post_init__(self):
if self.beta < 0.0 or self.beta > 1.0:
raise ValueError(f"beta must be in [0.0, 1.0], got {self.beta}.")

if self.max_length is not None and self.max_completion_length >= self.max_length:
raise ValueError(
f"max_completion_length ({self.max_completion_length}) must be smaller than "
f"max_length ({self.max_length}) to leave room for the prompt."
)

if self.max_prompt_length is None and self.max_length is not None:
self.max_prompt_length = self.max_length - self.max_completion_length

if self.num_generations < 1:
raise ValueError(f"num_generations must be at least 1, got {self.num_generations}.")

Expand Down
92 changes: 9 additions & 83 deletions trl/experimental/distillation/distillation_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import random
import textwrap
import warnings
from collections import defaultdict
from collections.abc import Callable
from contextlib import nullcontext
Expand Down Expand Up @@ -147,33 +146,19 @@ def _jsd_divergence(student_log_probs, teacher_log_probs, beta, support_mask=Non


class _DistillationCollator:
"""Data collator for the distillation trainer with independent prompt/completion budgets.
"""Data collator for the distillation trainer.

Accepts either of two dataset formats:

- a ``prompt`` column (prompt-only, the forward-looking format shared with GRPO): the student generates the
completion on-policy, so there is nothing to train on in the dataset;
- a ``messages`` column (language-modeling format): the prompt is everything before the last assistant turn and the
completion is that turn.

Unlike ``DataCollatorForChatML``, this collator tokenizes prompts and completions separately so that long
completions can never truncate the prompt to empty.
Accepts a prompt-only dataset (a ``prompt`` column, the format shared with GRPO): the student generates the
completion on-policy, so there is nothing to train on in the dataset.
"""

def __init__(
self,
tokenizer: "PreTrainedTokenizerBase",
max_length: int,
max_prompt_length: int,
messages_key: str = "messages",
ignore_index: int = -100,
):
self.tokenizer = tokenizer
self.max_length = max_length
self.max_prompt_length = max_prompt_length
self.messages_key = messages_key
self.ignore_index = ignore_index
self._warned_messages_deprecated = False

if tokenizer.pad_token_id is None:
raise ValueError("The tokenizer does not have a pad token. Please set `pad_token_id` in the tokenizer.")
Expand All @@ -184,69 +169,14 @@ def __call__(self, examples: list[dict[str, Any]]) -> dict[str, torch.Tensor]:
all_prompt_ids: list[list[int]] = []

for example in examples:
if "prompt" in example:
# Prompt-only dataset (the forward-looking format, shared with GRPO): no completion to train on, the
# student generates one on-policy.
prompt_messages = example["prompt"]
has_completion = False
else:
if not self._warned_messages_deprecated:
warnings.warn(
"Passing a `messages`-format (conversational language-modeling) dataset to "
"`DistillationTrainer` is deprecated and will be removed. Use a prompt-only dataset with a "
"`prompt` column instead.",
FutureWarning,
)
self._warned_messages_deprecated = True
messages = example[self.messages_key]
# Split: prompt = everything before the last assistant turn, completion = last assistant turn
has_completion = len(messages) > 1 and messages[-1].get("role") == "assistant"
prompt_messages = messages[:-1] if has_completion else messages

# Tokenize prompt with its own budget using the tokenizer's truncation side
formatted_prompt = self.tokenizer.apply_chat_template(
prompt_messages, tokenize=False, add_generation_prompt=True
example["prompt"], tokenize=False, add_generation_prompt=True
)
prompt_ids = self.tokenizer(
formatted_prompt,
truncation=True,
max_length=self.max_prompt_length,
padding=False,
add_special_tokens=False,
)["input_ids"]

if has_completion:
# Tokenize the full message (prompt + completion) without truncation first
formatted_full = self.tokenizer.apply_chat_template(
messages, tokenize=False, add_generation_prompt=False
)
full_ids = self.tokenizer(formatted_full, truncation=False, padding=False, add_special_tokens=False)[
"input_ids"
]

# Identify completion tokens: everything after the prompt in the full sequence.
# Use the un-truncated prompt length as the split point.
formatted_prompt_ids = self.tokenizer(
formatted_prompt, truncation=False, padding=False, add_special_tokens=False
)["input_ids"]
completion_ids = full_ids[len(formatted_prompt_ids) :]

# Trim completion so prompt + completion <= max_length
max_comp = self.max_length - len(prompt_ids)
if max_comp > 0 and len(completion_ids) > max_comp:
completion_ids = completion_ids[:max_comp]
elif max_comp <= 0:
completion_ids = []

input_ids = prompt_ids + completion_ids
labels = [self.ignore_index] * len(prompt_ids) + list(completion_ids)
else:
# Prompt-only: no completion to train on (on-policy will generate one)
input_ids = list(prompt_ids)
labels = [self.ignore_index] * len(prompt_ids)
prompt_ids = self.tokenizer(formatted_prompt, padding=False, add_special_tokens=False)["input_ids"]

all_input_ids.append(input_ids)
all_labels.append(labels)
# Prompt-only: no completion to train on (on-policy will generate one)
all_input_ids.append(list(prompt_ids))
all_labels.append([self.ignore_index] * len(prompt_ids))
Comment thread
qgallouedec marked this conversation as resolved.
all_prompt_ids.append(list(prompt_ids))

# Convert to tensors and left-pad
Expand Down Expand Up @@ -433,11 +363,7 @@ def __init__(

# ── Data collator ──
if data_collator is None:
data_collator = _DistillationCollator(
tokenizer=processing_class,
max_length=args.max_length,
max_prompt_length=args.max_prompt_length,
)
data_collator = _DistillationCollator(tokenizer=processing_class)

# ── Liger fused JSD loss ──
self.use_liger_loss = False
Expand Down
Loading