Skip to content
Closed
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
15 changes: 13 additions & 2 deletions src/transformers/loss/loss_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,21 @@ def fixed_cross_entropy(
target: torch.Tensor,
num_items_in_batch: torch.Tensor | None = None,
ignore_index: int = -100,
**kwargs,
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,
reduction=reduction,
label_smoothing=label_smoothing,
weight=weight,
)

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