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

[DPO] add reference log-prob outputs in DPO #521

Merged
merged 6 commits into from
Jan 30, 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
7 changes: 5 additions & 2 deletions src/liger_kernel/chunked_loss/dpo_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ def preference_loss_fn(
chosen_logratios = chosen_logps - ref_chosen_logps
rejected_logratios = rejected_logps - ref_rejected_logps

chosen_rewards = beta * (chosen_logps - ref_chosen_logps)
rejected_rewards = beta * (rejected_logps - ref_rejected_logps)

logits_diff = beta * (chosen_logratios - rejected_logratios)
loss = -F.logsigmoid(logits_diff).sum() / (full_target.shape[0] // 2)
return loss
return loss, chosen_rewards, rejected_rewards

@staticmethod
def forward(
Expand Down Expand Up @@ -99,7 +102,7 @@ def __init__(
beta: float = 0.1,
compute_nll_loss: bool = False,
compiled: bool = True,
use_ref_model: bool = False,
use_ref_model: bool = True,
):
"""
Args:
Expand Down
5 changes: 4 additions & 1 deletion test/chunked_loss/test_dpo_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,12 @@ def alignment_loss(
chosen_logratios = policy_chosen_logps - ref_chosen_logps
rejected_logratios = policy_rejected_logps - ref_rejected_logps

chosen_rewards = self.beta * (policy_chosen_logps - ref_chosen_logps)
rejected_rewards = self.beta * (policy_rejected_logps - ref_rejected_logps)

logits_diff = self.beta * (chosen_logratios - rejected_logratios)
losses = -F.logsigmoid(logits_diff)
return losses
return losses, chosen_rewards, rejected_rewards


class TorchLMHeadDPO(torch.nn.Module):
Expand Down
Loading