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

Fix LigerCrossEntropyLoss Reduction Behavior for "None" Mode #435

Merged
merged 4 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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/liger_kernel/ops/cross_entropy.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,11 @@ def cross_entropy_forward(
num_warps=32 if not is_hip() else 16,
)

loss = torch.sum(loss_1d)
if reduction == "none":
loss = loss_1d
else:
loss = torch.sum(loss_1d)

if return_z_loss == _TRUE.value:
z_loss = torch.sum(z_loss_1d)
hebiao064 marked this conversation as resolved.
Show resolved Hide resolved
else:
Expand Down
24 changes: 12 additions & 12 deletions test/transformers/test_cross_entropy.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ def _test_correctness_once(target_ce, B, T, V, reduction, scalar, dtype, atol, r
output2 = target_ce(_input2, target)
assert torch.allclose(output, output2, atol=atol, rtol=rtol)

output.backward()
output2.backward()
output.backward(gradient=torch.ones_like(output))
output2.backward(gradient=torch.ones_like(output))
assert torch.allclose(_input.grad, _input2.grad, atol=atol, rtol=rtol)


Expand Down Expand Up @@ -118,8 +118,8 @@ def _test_correctness_with_ignore_index_once(

assert torch.allclose(output, output2, atol=atol, rtol=rtol)

output.backward()
output2.backward()
output.backward(gradient=torch.ones_like(output))
output2.backward(gradient=torch.ones_like(output))
assert torch.allclose(_input.grad, _input2.grad, atol=atol, rtol=rtol)


Expand Down Expand Up @@ -199,8 +199,8 @@ def _test_correctness_with_softcap_once(

assert torch.allclose(output, output2, atol=atol, rtol=rtol)

output.backward()
output2.backward()
output.backward(gradient=torch.ones_like(output))
output2.backward(gradient=torch.ones_like(output))

assert torch.allclose(_input.grad, _input2.grad, atol=atol, rtol=rtol)

Expand Down Expand Up @@ -325,8 +325,8 @@ def _test_correctness_not_last_layer_once(
loss1 = output * 3
loss2 = output2 * 3

loss1.backward()
loss2.backward()
loss1.backward(gradient=torch.ones_like(output))
loss2.backward(gradient=torch.ones_like(output))
assert torch.allclose(_input.grad, _input2.grad, atol=atol, rtol=rtol)


Expand Down Expand Up @@ -384,7 +384,7 @@ def _test_correctness_functional(
(3, 423, 32000), # weird shapes
],
)
@pytest.mark.parametrize("reduction", ["sum", "mean"])
@pytest.mark.parametrize("reduction", ["sum", "mean", "none"])
@pytest.mark.parametrize(
"scalar, dtype, atol, rtol",
[
Expand Down Expand Up @@ -432,7 +432,7 @@ def test_correctness_functional(B, T, V, scalar, dtype, atol, rtol):
(3, 423, 32000, -123),
],
)
@pytest.mark.parametrize("reduction", ["sum", "mean"])
@pytest.mark.parametrize("reduction", ["sum", "mean", "none"])
@pytest.mark.parametrize(
"scalar, dtype, atol, rtol",
[
Expand Down Expand Up @@ -532,7 +532,7 @@ def test_correctness_with_label_smoothing_with_ignore_index_once(
(3, 423, 32000, 30.0),
],
)
@pytest.mark.parametrize("reduction", ["sum", "mean"])
@pytest.mark.parametrize("reduction", ["sum", "mean", "none"])
@pytest.mark.parametrize(
"scalar, dtype, atol, rtol",
[
Expand Down Expand Up @@ -700,7 +700,7 @@ def test_correctness_with_z_loss_with_other_params_once(
(3, 423, 32000),
],
)
@pytest.mark.parametrize("reduction", ["sum", "mean"])
@pytest.mark.parametrize("reduction", ["sum", "mean", "none"])
@pytest.mark.parametrize(
"scalar, dtype, atol, rtol",
[
Expand Down
Loading