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

👀 ORPO NLL Loss fix #24

Merged
merged 4 commits into from
Sep 20, 2024
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
12 changes: 5 additions & 7 deletions turbo_alignment/trainers/dpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def compute_loss(
)

ratio = -F.logsigmoid(log_odds)
losses = self.beta * ratio
losses = -policy_chosen_logps + self.beta * ratio

chosen_rewards = self.beta * policy_chosen_logps.detach()
rejected_rewards = self.beta * policy_rejected_logps.detach()
Expand Down Expand Up @@ -648,18 +648,16 @@ def get_batch_metrics(
metrics[f'{prefix}rewards/kto_grad_term_rejected'] = kto_grad_term_rejected.item()

elif self.loss_type == DPOLossesType.ORPO:
labels_w = batch['inputs_w']['labels'][:, 1:].clone()
loss_mask_w = labels_w != DISABLE_LOSS_LABEL
length_norm_policy_chosen_logps = policy_chosen_logps / loss_mask_w.sum(-1)

log_odds = (policy_chosen_logps - policy_rejected_logps) - (
torch.log1p(-torch.clamp(torch.exp(policy_chosen_logps), max=1 - 1e-7))
- torch.log1p(-torch.clamp(torch.exp(policy_rejected_logps), max=1 - 1e-7))
)
ratio = -F.logsigmoid(log_odds)
nll_loss = -length_norm_policy_chosen_logps
or_loss = self.dpo_loss_registry.beta * ratio
alekseymalakhov11 marked this conversation as resolved.
Show resolved Hide resolved
nll_loss = -policy_chosen_logps

metrics[f'{prefix}orpo/nll_loss'] = nll_loss.clone().detach().cpu().mean().item()
metrics[f'{prefix}orpo/nll_loss'] = nll_loss.detach().cpu().mean().item()
metrics[f'{prefix}orpo/or_loss'] = or_loss.detach().cpu().mean().item()
metrics[f'{prefix}orpo/ratio'] = (ratio).detach().cpu().mean().item()
metrics[f'{prefix}orpo/log_odds'] = (log_odds).detach().cpu().mean().item()

Expand Down
Loading