forked from huggingface/trl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_gold_trainer.py
More file actions
725 lines (578 loc) · 28.3 KB
/
test_gold_trainer.py
File metadata and controls
725 lines (578 loc) · 28.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
# Copyright 2020-2026 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from types import SimpleNamespace
import pytest
import torch
from datasets import load_dataset
from transformers import AutoTokenizer
from trl.experimental.gold.gold_trainer import GOLDTrainer, ULDLoss, build_teacher_inputs_from_texts
from trl.experimental.utils import DataCollatorForChatML
@pytest.fixture(scope="module")
def openr1_examples():
try:
dataset = load_dataset(
"HuggingFaceTB/OpenR1-Math-220k-default-verified",
"all",
split="train[:3]",
)
except Exception as exc: # pragma: no cover - network/environment dependent
pytest.skip(f"OpenR1 dataset unavailable: {exc}")
return [{"messages": row["messages"]} for row in dataset]
@pytest.fixture(scope="module")
def countdown_examples():
try:
dataset = load_dataset(
"HuggingFaceTB/Countdown-Tasks-3to4",
"gkd_verified_Qwen2.5-7B-Instruct",
split="train[:3]",
)
except Exception as exc: # pragma: no cover - network/environment dependent
pytest.skip(f"Countdown dataset unavailable: {exc}")
return [{"messages": row["messages"]} for row in dataset]
def _teacher_inputs_from_collator(student_tok, teacher_tok, batch):
prompt_texts = []
completion_texts = []
pad_token_id = student_tok.pad_token_id
for prompt_ids_tensor, input_ids_tensor, labels_tensor in zip(
batch["prompts"], batch["input_ids"], batch["labels"], strict=True
):
prompt_ids = prompt_ids_tensor.tolist()
if pad_token_id is not None:
prompt_ids = [tok for tok in prompt_ids if tok != pad_token_id]
prompt_texts.append(student_tok.decode(prompt_ids, skip_special_tokens=False))
input_ids = input_ids_tensor.tolist()
labels = labels_tensor.tolist()
completion_token_ids = [tok for tok, label in zip(input_ids, labels, strict=True) if label != -100]
completion_texts.append(student_tok.decode(completion_token_ids, skip_special_tokens=False))
teacher_input_ids, teacher_labels, _, _ = build_teacher_inputs_from_texts(
teacher_tok, prompt_texts, completion_texts
)
return teacher_input_ids, teacher_labels, completion_texts
def _assert_alignment_covers_completion(loss_fn, batch, teacher_input_ids, teacher_labels):
for idx in range(batch["input_ids"].shape[0]):
student_mask = batch["attention_mask"][idx].bool()
student_ids = batch["input_ids"][idx][student_mask]
student_labels = batch["labels"][idx][student_mask]
student_answer_ids = student_ids[student_labels != -100].tolist()
teacher_answer_mask = teacher_labels[idx] != -100
teacher_answer_ids = teacher_input_ids[idx][teacher_answer_mask].tolist()
student_groups, teacher_groups = loss_fn._build_alignment_groups_from_ids(
student_answer_ids, teacher_answer_ids
)
assert student_groups, "Student alignment groups must not be empty"
assert teacher_groups, "Teacher alignment groups must not be empty"
assert sorted(idx for group in student_groups for idx in group) == list(range(len(student_answer_ids)))
assert sorted(idx for group in teacher_groups for idx in group) == list(range(len(teacher_answer_ids)))
@pytest.mark.slow
def test_chatml_collator_preserves_completion_llama(llama_tokenizer, qwen_tokenizer, openr1_examples):
collator = DataCollatorForChatML(tokenizer=llama_tokenizer, max_length=512)
batch = collator(openr1_examples)
assistant_texts = [example["messages"][-1]["content"] for example in openr1_examples]
decoded_batch = llama_tokenizer.batch_decode(batch["input_ids"], skip_special_tokens=False)
for decoded, assistant in zip(decoded_batch, assistant_texts, strict=True):
assert assistant.strip() in decoded
teacher_input_ids, teacher_labels, completion_texts = _teacher_inputs_from_collator(
llama_tokenizer, qwen_tokenizer, batch
)
for completion, assistant in zip(completion_texts, assistant_texts, strict=True):
assert assistant.strip() in completion
assert completion.strip()
config = build_config(
uld_use_hybrid_loss=True,
uld_hybrid_matched_weight=0.6,
uld_hybrid_unmatched_weight=0.4,
)
loss_fn = ULDLoss(config, student_tokenizer=llama_tokenizer, teacher_tokenizer=qwen_tokenizer)
_assert_alignment_covers_completion(loss_fn, batch, teacher_input_ids, teacher_labels)
torch.manual_seed(0)
student_vocab = len(llama_tokenizer)
teacher_vocab = len(qwen_tokenizer)
batch_size, seq_len = batch["input_ids"].shape
student_logits = torch.randn(batch_size, seq_len, student_vocab)
teacher_logits = torch.randn(batch_size, teacher_input_ids.shape[1], teacher_vocab)
loss = loss_fn(
student_logits=student_logits,
teacher_logits=teacher_logits,
student_labels=batch["labels"],
teacher_labels=teacher_labels,
student_input_ids=batch["input_ids"],
teacher_input_ids=teacher_input_ids,
)
assert torch.isfinite(loss)
@pytest.mark.slow
def test_chatml_collator_preserves_completion_llama_countdown(llama_tokenizer, qwen_tokenizer, countdown_examples):
collator = DataCollatorForChatML(tokenizer=llama_tokenizer, max_length=512)
batch = collator(countdown_examples)
assistant_texts = [example["messages"][-1]["content"] for example in countdown_examples]
decoded_batch = llama_tokenizer.batch_decode(batch["input_ids"], skip_special_tokens=False)
for decoded, assistant in zip(decoded_batch, assistant_texts, strict=True):
assert assistant.strip() in decoded
teacher_input_ids, teacher_labels, completion_texts = _teacher_inputs_from_collator(
llama_tokenizer, qwen_tokenizer, batch
)
for completion, assistant in zip(completion_texts, assistant_texts, strict=True):
assert assistant.strip() in completion
assert completion.strip()
config = build_config(
uld_use_hybrid_loss=True,
uld_hybrid_matched_weight=0.6,
uld_hybrid_unmatched_weight=0.4,
)
loss_fn = ULDLoss(config, student_tokenizer=llama_tokenizer, teacher_tokenizer=qwen_tokenizer)
_assert_alignment_covers_completion(loss_fn, batch, teacher_input_ids, teacher_labels)
torch.manual_seed(2)
student_vocab = len(llama_tokenizer)
teacher_vocab = len(qwen_tokenizer)
batch_size, seq_len = batch["input_ids"].shape
student_logits = torch.randn(batch_size, seq_len, student_vocab)
teacher_logits = torch.randn(batch_size, teacher_input_ids.shape[1], teacher_vocab)
loss = loss_fn(
student_logits=student_logits,
teacher_logits=teacher_logits,
student_labels=batch["labels"],
teacher_labels=teacher_labels,
student_input_ids=batch["input_ids"],
teacher_input_ids=teacher_input_ids,
)
assert torch.isfinite(loss)
@pytest.mark.slow
def test_chatml_collator_preserves_completion_smollm(smollm_tokenizer, qwen_tokenizer, openr1_examples):
collator = DataCollatorForChatML(tokenizer=smollm_tokenizer, max_length=512)
batch = collator(openr1_examples)
assistant_texts = [example["messages"][-1]["content"] for example in openr1_examples]
decoded_batch = smollm_tokenizer.batch_decode(batch["input_ids"], skip_special_tokens=False)
for decoded, assistant in zip(decoded_batch, assistant_texts, strict=True):
assert assistant.strip() in decoded
teacher_input_ids, teacher_labels, completion_texts = _teacher_inputs_from_collator(
smollm_tokenizer, qwen_tokenizer, batch
)
for completion, assistant in zip(completion_texts, assistant_texts, strict=True):
assert assistant.strip() in completion
assert completion.strip()
config = build_config(
uld_use_hybrid_loss=True,
uld_hybrid_matched_weight=0.5,
uld_hybrid_unmatched_weight=0.5,
)
loss_fn = ULDLoss(config, student_tokenizer=smollm_tokenizer, teacher_tokenizer=qwen_tokenizer)
_assert_alignment_covers_completion(loss_fn, batch, teacher_input_ids, teacher_labels)
torch.manual_seed(1)
student_vocab = len(smollm_tokenizer)
teacher_vocab = len(qwen_tokenizer)
batch_size, seq_len = batch["input_ids"].shape
student_logits = torch.randn(batch_size, seq_len, student_vocab)
teacher_logits = torch.randn(batch_size, teacher_input_ids.shape[1], teacher_vocab)
loss = loss_fn(
student_logits=student_logits,
teacher_logits=teacher_logits,
student_labels=batch["labels"],
teacher_labels=teacher_labels,
student_input_ids=batch["input_ids"],
teacher_input_ids=teacher_input_ids,
)
assert torch.isfinite(loss)
def build_config(**overrides):
base = dict(
uld_crossentropy_weight=0.0,
uld_distillation_weight=1.0,
uld_student_temperature=1.0,
uld_teacher_temperature=1.0,
uld_skip_student_eos=False,
uld_skip_teacher_eos=False,
use_extended_uld=True,
uld_use_hybrid_loss=False,
uld_hybrid_matched_weight=None,
uld_hybrid_unmatched_weight=None,
beta=0.5,
)
base.update(overrides)
return SimpleNamespace(**base)
@pytest.fixture(scope="session")
def llama_tokenizer():
tokenizer = AutoTokenizer.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
return tokenizer
@pytest.fixture(scope="session")
def qwen_tokenizer():
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct")
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
return tokenizer
@pytest.fixture(scope="session")
def smollm_tokenizer():
tokenizer = AutoTokenizer.from_pretrained("HuggingFaceTB/SmolLM3-3B")
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
return tokenizer
def encode_prompt_completion(tokenizer, prompt, completion):
prompt_ids = tokenizer(prompt, add_special_tokens=False)["input_ids"]
completion_ids = tokenizer(completion, add_special_tokens=False)["input_ids"]
eos_id = tokenizer.eos_token_id
if eos_id is not None:
completion_ids = completion_ids + [eos_id]
input_ids = prompt_ids + completion_ids
labels = [-100] * len(prompt_ids) + completion_ids
return input_ids, labels
def pad_tokens(ids, pad_id, target_length):
return ids + [pad_id] * (target_length - len(ids))
def pad_labels(labels, target_length):
return labels + [-100] * (target_length - len(labels))
def test_process_completions_to_buffer_left_pads_prompt_retokenization():
class DummyBatch:
def __init__(self, input_ids):
self.input_ids = input_ids
def to(self, device):
self.input_ids = self.input_ids.to(device)
return self
class RecordingTokenizer:
pad_token_id = 0
pad_token = "<pad>"
def __init__(self):
self.padding_side = "right"
self.calls = []
self._prompt_ids = {
"short": [11],
"longer": [21, 22],
}
def __call__(
self,
texts,
return_tensors,
padding,
truncation,
max_length,
add_special_tokens,
padding_side=None,
):
assert return_tensors == "pt"
assert padding == "longest"
assert not truncation
assert max_length is None
assert not add_special_tokens
self.calls.append(padding_side)
side = padding_side or self.padding_side
encoded = [torch.tensor(self._prompt_ids[text], dtype=torch.long) for text in texts]
max_len = max(len(ids) for ids in encoded)
padded = []
for ids in encoded:
pad_width = max_len - len(ids)
if pad_width:
pad = torch.full((pad_width,), self.pad_token_id, dtype=torch.long)
ids = torch.cat([pad, ids]) if side == "left" else torch.cat([ids, pad])
padded.append(ids)
return DummyBatch(torch.stack(padded))
def batch_decode(self, sequences, skip_special_tokens=False, clean_up_tokenization_spaces=False):
del skip_special_tokens, clean_up_tokenization_spaces
return [" ".join(str(token) for token in sequence) for sequence in sequences]
trainer = GOLDTrainer.__new__(GOLDTrainer)
trainer.accelerator = SimpleNamespace(device=torch.device("cpu"))
trainer.processing_class = RecordingTokenizer()
trainer.args = SimpleNamespace(max_length=None)
trainer._buffered_inputs = [None]
trainer._buffered_text_logs = [None]
GOLDTrainer._process_completions_to_buffer(
trainer,
slices=[{"slice": "original"}],
on_policy_indices=[0],
local_slice_indices=[0, 0],
completion_ids=[[31], [41]],
prompts_text=["short", "longer"],
prompts_text_with_special=["short", "longer"],
max_completion_length=1,
)
buffered_inputs = trainer._buffered_inputs[0]
assert trainer.processing_class.calls == ["left"]
assert trainer.processing_class.padding_side == "right"
assert torch.equal(buffered_inputs["input_ids"], torch.tensor([[0, 11, 31], [21, 22, 41]], dtype=torch.long))
assert torch.equal(buffered_inputs["attention_mask"], torch.tensor([[0, 1, 1], [1, 1, 1]], dtype=torch.long))
assert torch.equal(buffered_inputs["labels"], torch.tensor([[-100, -100, 31], [-100, -100, 41]]))
def test_alignment_groups_cover_all_tokens(llama_tokenizer, qwen_tokenizer):
config = build_config()
loss = ULDLoss(config, student_tokenizer=llama_tokenizer, teacher_tokenizer=qwen_tokenizer)
text = "SmolLM3-3B is smaller than Llama 3.2 but still capable."
student_ids = llama_tokenizer(text, add_special_tokens=False)["input_ids"]
teacher_ids = qwen_tokenizer(text, add_special_tokens=False)["input_ids"]
student_groups, teacher_groups = loss._build_alignment_groups_from_ids(student_ids, teacher_ids)
assert len(student_groups) == len(teacher_groups)
assert sorted(idx for group in student_groups for idx in group) == list(range(len(student_ids)))
assert sorted(idx for group in teacher_groups for idx in group) == list(range(len(teacher_ids)))
def test_merge_probabilities_multiplies_split_tokens():
config = build_config()
# Use simple 3-token vocabulary to validate merging behaviour
# probs[0] = P(token | context) at position 0 for all vocab tokens
# probs[1] = P(token | context) at position 1 for all vocab tokens
probs = torch.tensor([[0.6, 0.3, 0.1], [0.2, 0.5, 0.3]])
loss = ULDLoss(config, student_tokenizer=None, teacher_tokenizer=None)
# token_ids[1] = 1 means the actual token at position 1 is token ID 1
# So we should extract P(token_id=1 | ...) = probs[1, 1] = 0.5
token_ids = [0, 1] # Actual generated tokens
merged = loss._merge_probabilities_with_alignment_groups(probs, [[0, 1]], token_ids=token_ids)
# Expected: P_merged(y) = P(y | context_0) × P(token_1=1 | context_1)
# For each vocab token y, multiply marginal prob at pos 0 by scalar conditional prob of actual token at pos 1
expected = probs[0] * probs[1, 1] # probs[1, 1] = 0.5
# Expected unnormalized: [0.6 * 0.5, 0.3 * 0.5, 0.1 * 0.5] = [0.3, 0.15, 0.05]
torch.testing.assert_close(merged[0], expected)
def test_initialize_vocabulary_mapping_contains_common_tokens(llama_tokenizer, qwen_tokenizer):
config = build_config(
uld_use_hybrid_loss=True,
uld_hybrid_matched_weight=1.0,
uld_hybrid_unmatched_weight=0.0,
)
loss = ULDLoss(config, student_tokenizer=llama_tokenizer, teacher_tokenizer=qwen_tokenizer)
common_tokens = ["Hello", "world", "-", "ol", "LM", "3", "B"]
for token in common_tokens:
student_id = llama_tokenizer.convert_tokens_to_ids(token)
teacher_id = qwen_tokenizer.convert_tokens_to_ids(token)
assert student_id is not None
assert teacher_id is not None
assert teacher_id in loss._vocab_mapping
assert loss._vocab_mapping[teacher_id] == student_id
assert teacher_id in loss._teacher_matched_ids
assert student_id in loss._student_matched_ids
def test_get_start_and_size_answers_skips_prompt_tokens():
trainer = ULDLoss.__new__(ULDLoss)
trainer.ignore_index = -100
answers = torch.tensor(
[
[-100, -100, -100, 10, 20, 30, -100, -100],
[-100, 5, 6, 7, -100, -100, -100, -100],
[-100, -100, -100, -100, -100, -100, -100, -100],
]
)
starts, sizes = trainer._get_start_and_size_answers(answers)
assert starts == [3, 1, 0]
assert sizes == [3, 3, 0]
@pytest.mark.slow
def test_generate_on_policy_outputs_masks_prompt(llama_tokenizer):
trainer = GOLDTrainer.__new__(GOLDTrainer)
trainer.use_transformers_paged = False
trainer.processing_class = llama_tokenizer
prompt_text = "<|begin_of_text|><|start_header_id|>user<|end_header_id|>\nHello?<|eot_id|>"
completion_text = "<|start_header_id|>assistant<|end_header_id|>\nHi there!"
prompt_ids = llama_tokenizer(prompt_text, add_special_tokens=False)["input_ids"]
completion_ids = llama_tokenizer(completion_text, add_special_tokens=False)["input_ids"]
pad_id = llama_tokenizer.pad_token_id
pad_width = 3
prompt_tensor = torch.full((1, len(prompt_ids) + pad_width), pad_id, dtype=torch.long)
prompt_tensor[0, pad_width:] = torch.tensor(prompt_ids, dtype=torch.long)
prompt_mask = (prompt_tensor != pad_id).long()
generated_sequence = torch.tensor(prompt_ids + completion_ids, dtype=torch.long).unsqueeze(0)
class DummyModel:
def generate(self, input_ids, attention_mask, generation_config, return_dict_in_generate):
assert torch.equal(input_ids, prompt_tensor)
assert torch.equal(attention_mask, prompt_mask)
return SimpleNamespace(sequences=generated_sequence)
generation_config = SimpleNamespace(max_completion_length=None, temperature=None, top_k=None, top_p=None)
new_ids, new_mask, new_labels, prompt_texts, completion_texts = GOLDTrainer.generate_on_policy_outputs(
trainer,
DummyModel(),
{"prompts": prompt_tensor, "prompt_attention_mask": prompt_mask},
generation_config,
pad_id,
)
assert torch.equal(new_ids, generated_sequence)
if pad_id is not None:
expected_mask = (generated_sequence != pad_id).long()
assert torch.equal(new_mask, expected_mask)
else:
assert torch.all(new_mask == 1)
prompt_len = len(prompt_ids)
assert torch.all(new_labels[0, :prompt_len] == -100)
assert torch.equal(new_labels[0, prompt_len:], torch.tensor(completion_ids, dtype=torch.long))
assert prompt_texts[0] == llama_tokenizer.decode(prompt_ids, skip_special_tokens=False)
assert completion_texts[0] == llama_tokenizer.decode(completion_ids, skip_special_tokens=False)
@pytest.mark.slow
def test_generate_on_policy_outputs_masks_prompt_smollm(smollm_tokenizer, openr1_examples):
trainer = GOLDTrainer.__new__(GOLDTrainer)
trainer.use_transformers_paged = False
trainer.processing_class = smollm_tokenizer
collator = DataCollatorForChatML(tokenizer=smollm_tokenizer)
batch = collator([openr1_examples[0]])
batch = {k: v.cpu() for k, v in batch.items()}
class DummyModel:
def generate(self, input_ids, attention_mask, generation_config, return_dict_in_generate):
assert torch.equal(input_ids, batch["prompts"])
assert torch.equal(attention_mask, batch["prompt_attention_mask"])
return SimpleNamespace(sequences=batch["input_ids"])
generation_config = SimpleNamespace(max_completion_length=None, temperature=None, top_k=None, top_p=None)
pad_id = smollm_tokenizer.pad_token_id
new_ids, new_mask, new_labels, prompt_texts, completion_texts = GOLDTrainer.generate_on_policy_outputs(
trainer,
DummyModel(),
{"prompts": batch["prompts"], "prompt_attention_mask": batch["prompt_attention_mask"]},
generation_config,
pad_id,
)
assert torch.equal(new_ids, batch["input_ids"])
if pad_id is not None:
expected_mask = (batch["input_ids"] != pad_id).long()
assert torch.equal(new_mask, expected_mask)
else:
assert torch.all(new_mask == 1)
prompt_len = int(batch["prompt_attention_mask"].sum().item())
tail_labels = new_labels[0, prompt_len:]
expected_tail = batch["input_ids"][0, prompt_len:]
active_mask = tail_labels != -100
assert torch.all(new_labels[0, :prompt_len] == -100)
assert torch.equal(tail_labels[active_mask], expected_tail[active_mask])
assert torch.all(tail_labels[~active_mask] == -100)
prompt_tokens = batch["prompts"][0, batch["prompt_attention_mask"][0].bool()]
decoded_prompt = smollm_tokenizer.decode(prompt_tokens.tolist(), skip_special_tokens=False)
assert prompt_texts[0] == decoded_prompt
assistant_completion = openr1_examples[0]["messages"][-1]["content"].strip()
assert assistant_completion in completion_texts[0]
def test_generalized_jsd_loss_accepts_probability_inputs():
student_probs = torch.tensor([[[0.6, 0.3, 0.1]]])
teacher_probs = torch.tensor([[[0.5, 0.4, 0.1]]])
mixture = 0.5 * (student_probs + teacher_probs)
expected = 0.5 * (
torch.sum(student_probs * (torch.log(student_probs) - torch.log(mixture)))
+ torch.sum(teacher_probs * (torch.log(teacher_probs) - torch.log(mixture)))
)
loss = GOLDTrainer.generalized_jsd_loss(
student_probs,
teacher_probs,
beta=0.5,
reduction="batchmean",
logits_are_probs=True,
)
torch.testing.assert_close(loss, expected)
def test_uldloss_handles_llama_student_qwen_teacher_sequence(llama_tokenizer, qwen_tokenizer):
config = build_config(
uld_use_hybrid_loss=True,
uld_hybrid_matched_weight=0.6,
uld_hybrid_unmatched_weight=0.4,
)
loss_fn = ULDLoss(config, student_tokenizer=llama_tokenizer, teacher_tokenizer=qwen_tokenizer)
prompt = "User: Summarize the difference between llamas and alpacas."
completion = "Assistant: Llamas are taller while alpacas have softer wool."
student_ids, student_labels = encode_prompt_completion(llama_tokenizer, prompt, completion)
teacher_ids, teacher_labels = encode_prompt_completion(qwen_tokenizer, prompt, completion)
pad_id_student = llama_tokenizer.pad_token_id
pad_id_teacher = qwen_tokenizer.pad_token_id
max_length = max(len(student_ids), len(teacher_ids))
student_ids = pad_tokens(student_ids, pad_id_student, max_length)
teacher_ids = pad_tokens(teacher_ids, pad_id_teacher, max_length)
student_labels = pad_labels(student_labels, max_length)
teacher_labels = pad_labels(teacher_labels, max_length)
student_input_ids = torch.tensor([student_ids])
teacher_input_ids = torch.tensor([teacher_ids])
student_labels = torch.tensor([student_labels])
teacher_labels = torch.tensor([teacher_labels])
student_vocab = len(llama_tokenizer)
teacher_vocab = len(qwen_tokenizer)
student_logits = torch.randn(1, max_length, student_vocab)
teacher_logits = torch.randn(1, max_length, teacher_vocab)
loss = loss_fn(
student_logits=student_logits,
teacher_logits=teacher_logits,
student_labels=student_labels,
teacher_labels=teacher_labels,
student_input_ids=student_input_ids,
teacher_input_ids=teacher_input_ids,
)
assert torch.isfinite(loss)
assert loss.dim() == 0
assert loss_fn.last_matched_loss is not None
assert loss_fn.last_unmatched_loss is not None
def test_uldloss_handles_smollm_student_qwen_teacher_sequence(smollm_tokenizer, qwen_tokenizer):
config = build_config(
uld_use_hybrid_loss=True,
uld_hybrid_matched_weight=0.5,
uld_hybrid_unmatched_weight=0.5,
)
loss_fn = ULDLoss(config, student_tokenizer=smollm_tokenizer, teacher_tokenizer=qwen_tokenizer)
prompt = "User: Describe SmolLM3 in a sentence."
completion = "Assistant: SmolLM3 is a compact yet capable language model."
student_ids, student_labels = encode_prompt_completion(smollm_tokenizer, prompt, completion)
teacher_ids, teacher_labels = encode_prompt_completion(qwen_tokenizer, prompt, completion)
pad_id_student = smollm_tokenizer.pad_token_id
pad_id_teacher = qwen_tokenizer.pad_token_id
max_length = max(len(student_ids), len(teacher_ids))
student_ids = pad_tokens(student_ids, pad_id_student, max_length)
teacher_ids = pad_tokens(teacher_ids, pad_id_teacher, max_length)
student_labels = pad_labels(student_labels, max_length)
teacher_labels = pad_labels(teacher_labels, max_length)
student_input_ids = torch.tensor([student_ids])
teacher_input_ids = torch.tensor([teacher_ids])
student_labels = torch.tensor([student_labels])
teacher_labels = torch.tensor([teacher_labels])
student_vocab = len(smollm_tokenizer)
teacher_vocab = len(qwen_tokenizer)
student_logits = torch.randn(1, max_length, student_vocab)
teacher_logits = torch.randn(1, max_length, teacher_vocab)
loss = loss_fn(
student_logits=student_logits,
teacher_logits=teacher_logits,
student_labels=student_labels,
teacher_labels=teacher_labels,
student_input_ids=student_input_ids,
teacher_input_ids=teacher_input_ids,
)
assert torch.isfinite(loss)
assert loss.dim() == 0
assert loss_fn.last_matched_loss is not None
assert loss_fn.last_unmatched_loss is not None
def test_uldloss_hybrid_config_beta_zero(llama_tokenizer, qwen_tokenizer):
config = build_config(
uld_use_hybrid_loss=True,
uld_hybrid_matched_weight=0.0,
uld_hybrid_unmatched_weight=1.0,
use_extended_uld=True,
uld_crossentropy_weight=0.0,
uld_distillation_weight=1.0,
uld_student_temperature=1.0,
uld_teacher_temperature=1.0,
temperature=1.0,
top_p=0.95,
top_k=0,
lmbda=1.0,
beta=0.0,
)
loss_fn = ULDLoss(config, student_tokenizer=llama_tokenizer, teacher_tokenizer=qwen_tokenizer)
prompt = "User: Explain how GOLD handles tokenizer mismatches."
completion = "Assistant: GOLD merges aligned subwords and applies hybrid ULD loss."
student_ids, student_labels = encode_prompt_completion(llama_tokenizer, prompt, completion)
teacher_ids, teacher_labels = encode_prompt_completion(qwen_tokenizer, prompt, completion)
pad_id_student = llama_tokenizer.pad_token_id
pad_id_teacher = qwen_tokenizer.pad_token_id
max_length = max(len(student_ids), len(teacher_ids))
student_ids = pad_tokens(student_ids, pad_id_student, max_length)
teacher_ids = pad_tokens(teacher_ids, pad_id_teacher, max_length)
student_labels = pad_labels(student_labels, max_length)
teacher_labels = pad_labels(teacher_labels, max_length)
student_input_ids = torch.tensor([student_ids])
teacher_input_ids = torch.tensor([teacher_ids])
student_labels = torch.tensor([student_labels])
teacher_labels = torch.tensor([teacher_labels])
student_vocab = len(llama_tokenizer)
teacher_vocab = len(qwen_tokenizer)
torch.manual_seed(0)
student_logits = torch.randn(1, max_length, student_vocab)
teacher_logits = torch.randn(1, max_length, teacher_vocab)
loss = loss_fn(
student_logits=student_logits,
teacher_logits=teacher_logits,
student_labels=student_labels,
teacher_labels=teacher_labels,
student_input_ids=student_input_ids,
teacher_input_ids=teacher_input_ids,
)
assert torch.isfinite(loss)
assert loss.dim() == 0
assert loss_fn.last_matched_loss is not None
assert loss_fn.last_unmatched_loss is not None
expected = config.uld_hybrid_unmatched_weight * loss_fn.last_unmatched_loss
torch.testing.assert_close(loss, expected, atol=1e-6, rtol=1e-5)