Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⛔ Add EOS token to processed input in SFT #3091

Merged
merged 3 commits into from
Mar 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions tests/test_sft_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,7 @@ def test_sft_trainer_only_train_packing(self):
eval_dataset=self.conversational_lm_dataset["test"],
)

self.assertEqual(len(trainer.train_dataset["input_ids"]), 46) # w/ this dataset, we end up with 46 seqs
self.assertEqual(len(trainer.train_dataset["input_ids"]), 47) # w/ this dataset, we end up with 46 seqs
self.assertEqual(len(trainer.eval_dataset["input_ids"]), len(self.conversational_lm_dataset["test"]))

def test_sft_trainer_eval_packing(self):
Expand All @@ -1125,8 +1125,8 @@ def test_sft_trainer_eval_packing(self):
eval_dataset=self.conversational_lm_dataset["test"],
)

self.assertEqual(len(trainer.train_dataset["input_ids"]), 46) # w/ this dataset, we end up with 46 seqs
self.assertEqual(len(trainer.eval_dataset["input_ids"]), 6) # w/ this dataset, we end up with 6 seqs
self.assertEqual(len(trainer.train_dataset["input_ids"]), 47) # w/ this dataset, we end up with 47 seqs
self.assertEqual(len(trainer.eval_dataset["input_ids"]), 7) # w/ this dataset, we end up with 7 seqs

def test_sft_trainer_no_packing(self):
with tempfile.TemporaryDirectory() as tmp_dir:
Expand Down
9 changes: 8 additions & 1 deletion trl/trainer/sft_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,14 @@ def concat_prompt_completion(example):
map_kwargs["desc"] = f"Tokenizing {dataset_name} dataset"

def tokenize(example, processing_class, dataset_text_field):
return processing_class(text=example[dataset_text_field])
processed = processing_class(text=example[dataset_text_field])
if (
processing_class.eos_token_id is not None
and processed["input_ids"][-1] != processing_class.eos_token_id
):
processed["input_ids"] = processed["input_ids"] + [processing_class.eos_token_id]
processed["attention_mask"] = processed["attention_mask"] + [1]
return processed

dataset = dataset.map(
tokenize,
Expand Down