Skip to content
Closed
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: 5 additions & 1 deletion src/transformers/loss/loss_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ def fixed_cross_entropy(
target: torch.Tensor,
num_items_in_batch: torch.Tensor | None = None,
ignore_index: int = -100,
weight: torch.Tensor | None = None,
label_smoothing: float = 0.0,
**kwargs,
) -> torch.Tensor:
reduction = "sum" if num_items_in_batch is not None else "mean"
loss = nn.functional.cross_entropy(source, target, ignore_index=ignore_index, reduction=reduction)
loss = nn.functional.cross_entropy(
source, target, ignore_index=ignore_index, weight=weight, reduction=reduction, label_smoothing=label_smoothing
)
if reduction == "sum":
# just in case users pass an int for num_items_in_batch, which could be the case for custom trainer
if torch.is_tensor(num_items_in_batch):
Expand Down