Skip to content

Add type hints to dpo_trainer.py #3631

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

Merged
merged 5 commits into from
Jul 6, 2025
Merged
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
32 changes: 23 additions & 9 deletions trl/trainer/dpo_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,13 @@ def _prepare_dataset(
return dataset

@staticmethod
def tokenize_row(features, processing_class, max_prompt_length, max_completion_length, add_special_tokens):
def tokenize_row(
features: dict[str, str],
processing_class: PreTrainedTokenizerBase,
max_prompt_length: Optional[int] = None,
max_completion_length: Optional[int] = None,
add_special_tokens: bool = True,
) -> dict[str, list[int]]:
"""
Tokenize a row of the dataset.

Expand Down Expand Up @@ -719,7 +725,13 @@ def tokenize_row(features, processing_class, max_prompt_length, max_completion_l
}

@staticmethod
def process_row(features, processing_class, max_prompt_length, max_completion_length, add_special_tokens):
def process_row(
features: dict[str, str],
processing_class: PreTrainedTokenizerBase,
max_prompt_length: Optional[int] = None,
max_completion_length: Optional[int] = None,
add_special_tokens: bool = True,
) -> dict[str, list[int]]:
"""
Same as `tokenize_row` but for vision models. Please refer to `tokenize_row` for more information.
"""
Expand Down Expand Up @@ -887,7 +899,7 @@ def null_ref_context(self):
if self.ref_adapter_name:
self.model.set_adapter(self.model_adapter_name or "default")

def compute_ref_log_probs(self, batch: dict[str, torch.LongTensor]) -> dict:
def compute_ref_log_probs(self, batch: dict[str, torch.LongTensor]) -> tuple[torch.Tensor, torch.Tensor]:
"""Computes log probabilities of the reference model for a single padded batch of a DPO specific dataset."""
compte_ref_context_manager = (
autocast(self.accelerator.device.type) if self._peft_has_been_casted_to_bf16 else nullcontext()
Expand Down Expand Up @@ -1163,7 +1175,9 @@ def dpo_loss(

return losses, chosen_rewards, rejected_rewards

def _compute_loss_liger(self, model: nn.Module, batch: dict[str, Union[list, torch.LongTensor]]):
def _compute_loss_liger(
self, model: nn.Module, batch: dict[str, Union[list, torch.LongTensor]]
) -> dict[str, torch.Tensor]:
unwrapped_model = self.accelerator.unwrap_model(model)
concatenated_batch = self.concatenated_inputs(batch, padding_value=self.padding_value)

Expand Down Expand Up @@ -1394,7 +1408,7 @@ def _compute_loss_liger(self, model: nn.Module, batch: dict[str, Union[list, tor

def concatenated_forward(
self, model: nn.Module, batch: dict[str, Union[list, torch.LongTensor]], is_ref_model: bool = False
):
) -> dict[str, torch.Tensor]:
"""
Runs the given model on the given batch of inputs, concatenating the chosen and rejected inputs together.

Expand Down Expand Up @@ -1615,10 +1629,10 @@ def concatenated_forward(

def get_batch_loss_metrics(
self,
model,
model: Union[PreTrainedModel, nn.Module],
batch: dict[str, Union[list, torch.LongTensor]],
train_eval: Literal["train", "eval"] = "train",
):
) -> tuple[torch.Tensor, dict[str, float]]:
"""Compute the DPO loss and other metrics for the given batch of inputs for train or test."""
metrics = {}

Expand Down Expand Up @@ -1687,7 +1701,7 @@ def compute_loss(
inputs: dict[str, Union[torch.Tensor, Any]],
return_outputs=False,
num_items_in_batch=None,
) -> Union[torch.Tensor, tuple[torch.Tensor, dict[str, torch.Tensor]]]:
) -> Union[torch.Tensor, tuple[torch.Tensor, dict[str, float]]]:
compute_loss_context_manager = (
autocast(self.accelerator.device.type) if self._peft_has_been_casted_to_bf16 else nullcontext()
)
Expand Down Expand Up @@ -1758,7 +1772,7 @@ def prediction_step(
inputs: dict[str, Union[torch.Tensor, Any]],
prediction_loss_only: bool,
ignore_keys: Optional[list[str]] = None,
):
) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[torch.Tensor]]:
if ignore_keys is None:
if hasattr(model, "config"):
ignore_keys = getattr(model.config, "keys_to_ignore_at_inference", [])
Expand Down
Loading