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

Correctly pass gen_kwarg to eval during model runs #5451

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions src/llamafactory/train/sft/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ class CustomSeq2SeqTrainer(Seq2SeqTrainer):
"""

def __init__(
self, finetuning_args: "FinetuningArguments", processor: Optional["ProcessorMixin"], **kwargs
self, finetuning_args: "FinetuningArguments", processor: Optional["ProcessorMixin"], gen_kwargs: Optional[Dict[str, Any]], **kwargs
) -> None:
super().__init__(**kwargs)
self.finetuning_args = finetuning_args
self.gen_kwargs = gen_kwargs

if processor is not None:
self.add_callback(SaveProcessorCallback(processor))
Expand Down Expand Up @@ -102,7 +103,7 @@ def prediction_step(
inputs["labels"] = inputs["labels"][:, :prompt_len]

loss, generated_tokens, _ = super().prediction_step( # ignore the returned labels (may be truncated)
model, inputs, prediction_loss_only=prediction_loss_only, ignore_keys=ignore_keys
model, inputs, prediction_loss_only=prediction_loss_only, ignore_keys=ignore_keys, **self.gen_kwargs
)
if generated_tokens is not None and self.args.predict_with_generate:
generated_tokens[:, :prompt_len] = self.tokenizer.pad_token_id
Expand Down
13 changes: 7 additions & 6 deletions src/llamafactory/train/sft/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,17 @@ def run_sft(
metric_module["compute_metrics"] = ComputeAccuracy()
metric_module["preprocess_logits_for_metrics"] = eval_logit_processor

# Keyword arguments for `model.generate`
gen_kwargs = generating_args.to_dict()
gen_kwargs["eos_token_id"] = [tokenizer.eos_token_id] + tokenizer.additional_special_tokens_ids
gen_kwargs["pad_token_id"] = tokenizer.pad_token_id
gen_kwargs["logits_processor"] = get_logits_processor()

# Initialize our Trainer
trainer = CustomSeq2SeqTrainer(
model=model,
args=training_args,
gen_kwargs=gen_kwargs,
finetuning_args=finetuning_args,
data_collator=data_collator,
callbacks=callbacks,
Expand All @@ -85,12 +92,6 @@ def run_sft(
**metric_module,
)

# Keyword arguments for `model.generate`
gen_kwargs = generating_args.to_dict()
gen_kwargs["eos_token_id"] = [tokenizer.eos_token_id] + tokenizer.additional_special_tokens_ids
gen_kwargs["pad_token_id"] = tokenizer.pad_token_id
gen_kwargs["logits_processor"] = get_logits_processor()

# Training
if training_args.do_train:
train_result = trainer.train(resume_from_checkpoint=training_args.resume_from_checkpoint)
Expand Down