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

Nca pair loss #65

Merged
merged 4 commits into from
Dec 13, 2024
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
6 changes: 6 additions & 0 deletions turbo_alignment/settings/pipelines/train/dpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class DPOLossesType(str, Enum):
APO_DOWN = 'apo_down'
ASFT = 'asft'
DPOP = 'dpop'
NCA_PAIR = 'nca_pair'


class DPOLossSettings(ExtraFieldsNotAllowedBaseModel):
Expand Down Expand Up @@ -93,6 +94,10 @@ class DPOPLossSettings(DPOLossSettings):
lam: float = 0.1


class NCAPairLossSettings(DPOLossSettings):
loss_type: Literal[DPOLossesType.NCA_PAIR]


class SyncRefModelSettings(ExtraFieldsNotAllowedBaseModel):
sync_ref_model: bool = False
alpha: float = 1.0
Expand All @@ -114,6 +119,7 @@ class DPOTrainerSettings(TrainerSettings):
| APOZeroLossSettings
| APODownLossSettings
| DPOPLossSettings
| NCAPairLossSettings
)
sync_ref_settings: SyncRefModelSettings
use_ref_model: bool = True
Expand Down
32 changes: 32 additions & 0 deletions turbo_alignment/trainers/dpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
HingeLossSettings,
IPOLossSettings,
KTOLossSettings,
NCAPairLossSettings,
ORPOLossSettings,
SigmoidLossSettings,
SigmoidLossWithMarginSettings,
Expand Down Expand Up @@ -456,6 +457,35 @@ def compute_loss(
return loss, chosen_rewards, rejected_rewards


@DPOLossRegistry.register(DPOLossesType.NCA_PAIR)
class NCAPairLoss(DPOLossRegistry):
def __init__(self, *args, beta: float = 0.1, **kwargs) -> None:
self.beta = beta
super().__init__(*args, **kwargs)

def compute_loss(
self,
policy_chosen_logps: torch.FloatTensor,
policy_rejected_logps: torch.FloatTensor,
reference_chosen_logps: torch.FloatTensor,
reference_rejected_logps: torch.FloatTensor,
precomputed_margins: torch.FloatTensor | None,
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
chosen_logratios = policy_chosen_logps - reference_chosen_logps
rejected_logratios = policy_rejected_logps - reference_rejected_logps

chosen_rewards = self.beta * (policy_chosen_logps - reference_chosen_logps).detach()
rejected_rewards = self.beta * (policy_rejected_logps - reference_rejected_logps).detach()

loss = (
-F.logsigmoid(chosen_logratios * self.beta)
- 0.5 * F.logsigmoid(-chosen_logratios * self.beta)
- 0.5 * F.logsigmoid(-rejected_logratios * self.beta)
)

return loss, chosen_rewards, rejected_rewards


@dataclass
class DPOTrainingArguments(TrainingArguments):
loss_settings: (
Expand All @@ -473,6 +503,7 @@ class DPOTrainingArguments(TrainingArguments):
| APOZeroLossSettings
| APODownLossSettings
| DPOPLossSettings
| NCAPairLossSettings
) = field(
default_factory=SigmoidLossSettings(loss_type=DPOLossesType.SIGMOID)
) # type: ignore[call-overload]
Expand Down Expand Up @@ -822,6 +853,7 @@ def compute_loss(
model: PreTrainedModel | nn.Module,
inputs: dict[str, torch.Tensor | Any],
return_outputs=False,
num_items_in_batch=None,
) -> torch.Tensor | tuple[torch.Tensor, dict[str, float]]:
loss, metrics = self.get_batch_metrics(model, inputs, train_eval='train')

Expand Down
Loading