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 option to check dataset labels in SFTTrainer #1414

Closed
wants to merge 8 commits into from
Closed
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
14 changes: 14 additions & 0 deletions trl/trainer/sft_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from transformers.modeling_utils import unwrap_model
from transformers.trainer_callback import TrainerCallback
from transformers.trainer_utils import EvalPrediction
from transformers.utils import logging

from ..extras.dataset_formatting import get_formatting_func_from_dataset
from ..import_utils import is_peft_available
Expand All @@ -47,6 +48,7 @@
trl_sanitze_kwargs_for_tagging,
)

logger = logging.get_logger(__name__)

if is_peft_available():
from peft import PeftConfig, PeftModel, get_peft_model, prepare_model_for_kbit_training
Expand Down Expand Up @@ -118,6 +120,8 @@ class SFTTrainer(Trainer):
Dict of Optional kwargs to pass when creating packed or non-packed datasets
eval_packing: (`Optional[bool]`, *optional*):
Whether to pack the eval dataset as well. Defaults to `packing` if `None` is passed.
check_dataset_labels (`Optional[bool]`):
Flag to enable debugging of dataset labels and tokenization. If set to True, the trainer will print the tokens, decoded tokens, and their corresponding labels for the first item in the training dataset during initialization. Defaults to False.
"""

_tag_names = ["trl", "sft"]
Expand Down Expand Up @@ -149,6 +153,7 @@ def __init__(
model_init_kwargs: Optional[Dict] = None,
dataset_kwargs: Optional[Dict] = None,
eval_packing: Optional[bool] = None,
check_dataset_labels: Optional[bool] = False,
):
if model_init_kwargs is None:
model_init_kwargs = {}
Expand Down Expand Up @@ -302,6 +307,15 @@ def make_inputs_require_grad(module, input, output):
"overflow issues when training a model in half-precision. You might consider adding `tokenizer.padding_side = 'right'` to your code."
)


if check_dataset_labels:
if train_dataset is not None and len(train_dataset) > 0:
input_ids, attention_mask, labels = data_collator([train_dataset[0]]).values()
logger.info("check_dataset_labels:") # noqa
logger.info(tokenizer.decode(input_ids[0])) # noqa
for token, label in zip(input_ids[0], labels[0]):
logger.info(f"{token.item()}, '{tokenizer.decode(token)}', {label.item()}") # noqa

super().__init__(
model=model,
args=args,
Expand Down
Loading