Skip to content
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
9 changes: 8 additions & 1 deletion src/liger_kernel/chunked_loss/dpo_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def forward(
compute_nll_loss=False,
compiled=True,
use_ref_model=True,
average_log_prob=False,
chunk_size=1,
):
"""
Expand All @@ -85,6 +86,7 @@ def forward(
compute_nll_loss (bool): Whether to compute the NLL loss
compiled (bool): Whether to use torch compile
use_ref_model (bool): Whether to use a reference model
average_log_prob (bool): Whether to average the log probability per non-masked token
chunk_size (int): Size of chunks for processing.
Returns:
torch.Tensor: Computed loss
Expand All @@ -104,13 +106,14 @@ def forward(
ref_input=ref_input,
ref_weight=ref_weight,
ref_bias=ref_bias,
average_log_prob=average_log_prob,
chunk_size=chunk_size,
)

@staticmethod
def backward(ctx, *grad_output):
grads = LigerFusedLinearPreferenceBase.backward(ctx, grad_output)[:4]
return *grads, None, None, None, None, None, None, None, None, None
return *grads, None, None, None, None, None, None, None, None, None, None


class LigerFusedLinearDPOLoss(torch.nn.Module):
Expand All @@ -125,6 +128,7 @@ def __init__(
compute_nll_loss: bool = False,
compiled: bool = True,
use_ref_model: bool = True,
average_log_prob: bool = True,
chunk_size: int = 1,
):
"""
Expand All @@ -134,6 +138,7 @@ def __init__(
compute_nll_loss (bool): Whether to compute the NLL loss.
compiled (bool): Whether to use the torch compiled kernel.
use_ref_model (bool): Whether to use a reference model for the DPO loss.
average_log_prob (bool): Whether to average the log probability per non-masked token.
chunk_size (int): Size of chunks for processing.
"""
super().__init__()
Expand All @@ -142,6 +147,7 @@ def __init__(
self.compute_nll_loss = compute_nll_loss
self.compiled = compiled
self.use_ref_model = use_ref_model
self.average_log_prob = average_log_prob
self.chunk_size = chunk_size

def forward(
Expand All @@ -167,5 +173,6 @@ def forward(
self.compute_nll_loss,
self.compiled,
self.use_ref_model,
self.average_log_prob,
self.chunk_size,
)