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

CrossEntropyLoss return single value when reduction is "none" #421

Closed
leng-yue opened this issue Dec 3, 2024 · 5 comments · Fixed by #435
Closed

CrossEntropyLoss return single value when reduction is "none" #421

leng-yue opened this issue Dec 3, 2024 · 5 comments · Fixed by #435

Comments

@leng-yue
Copy link

leng-yue commented Dec 3, 2024

🐛 Describe the bug

When using LigerCrossEntropyLoss with reduction="none", it returns a single value, instead of an array.

Reproduce

import torch
from liger_kernel.transformers.cross_entropy import LigerCrossEntropyLoss


def test_cross_entropy_matches_torch():
    # Test cases with various shapes and values
    batch_size = 3
    num_classes = 5
    seq_length = 4

    # Generate random logits and labels using torch
    torch.manual_seed(42)
    logits = torch.randn(batch_size, seq_length, num_classes, device='cuda')
    labels = torch.randint(0, num_classes, (batch_size, seq_length), device='cuda')

    # Set some labels to -100 to ignore them
    labels[0, -1] = -100  # Ignore last token of first sequence
    labels[1, -2:] = -100  # Ignore last two tokens of second sequence

    # Liger cross entropy
    liger_ce = LigerCrossEntropyLoss(reduction="none")
    liger_loss = liger_ce(logits.view(-1, num_classes), labels.view(-1))
    print(liger_loss)

    # PyTorch cross entropy
    torch_ce = torch.nn.CrossEntropyLoss(reduction="none")
    torch_loss = torch_ce(logits.view(-1, num_classes), labels.view(-1))

    # Check if losses match
    torch.testing.assert_close(
        liger_loss,
        torch_loss,
        rtol=1e-5,
        atol=1e-5,
        msg="Liger and PyTorch losses don't match",
    )

    print("All tests passed! LigerCrossEntropyLoss matches PyTorch's implementation")


if __name__ == "__main__":
    test_cross_entropy_matches_torch()

Versions

Environment Report:

Operating System: Linux-5.15.0-88-generic-x86_64-with-glibc2.35
Python version: 3.10.15
PyTorch version: 2.5.1+cu124
CUDA version: 12.4
Triton version: 3.1.0
Transformers version: 4.46.2

@ByronHsu
Copy link
Collaborator

ByronHsu commented Dec 3, 2024

Sorry. We only support "mean" and "sum" for now. Are you willing to contribute by adding an assertion or implementing "none" reduction? Thanks!

@leng-yue
Copy link
Author

leng-yue commented Dec 4, 2024

I think I can make a PR to add an assert to block "none" first, then let's see how to implement the "none" reduction one.

@Tcc0403
Copy link
Collaborator

Tcc0403 commented Dec 5, 2024

The triton kernel of liger ce is actually giving none reduction loss, just need to make a reduction=="none" condition to output loss1d directly without torch.sum()

loss = torch.sum(loss_1d)

@hebiao064
Copy link
Collaborator

#take I made a PR about this issue, please take a look, thanks @ByronHsu @leng-yue @Tcc0403

@leng-yue
Copy link
Author

leng-yue commented Dec 9, 2024

Looks good to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants