cross_entropy=True rebinds torch.nn.functional.cross_entropy process-wide, so unrelated 3-D or soft-target calls fail

Open
#1,639 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 3 days

@ArchanaChetan07 is already working on this.

Since Nov 19, 2026.

  • #1657 by @ArchanaChetan07 — open

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
65/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python, pytorch

Research direction

Start by tracing the 20 apply_liger_kernel_to_* functions that assign nn.functional.cross_entropy, then inspect liger_cross_entropy and its tests. Compare scoping the patch to the model's loss module with falling back to the original function for unsupported inputs, such as 3-D inputs or floating-point targets. Done means unrelated valid cross-entropy calls continue to work after a model is patched, with regression coverage for the reported cases.

Written by the indexing model from the issue text.

Description

apply_liger_kernel_to_<model>(cross_entropy=True) replaces torch.nn.functional.cross_entropy for the whole process, not only inside the patched model. Any later F.cross_entropy call in the same process goes to Liger, including calls Liger's kernel doesn't support, and those raise:

import torch, torch.nn.functional as F
from liger_kernel.transformers import apply_liger_kernel_to_granite

x3, y3 = torch.randn(2, 7, 5), torch.randint(0, 7, (2, 5))   # (N, C, d) input: valid for F.cross_entropy
x2, soft = torch.randn(4, 7), torch.softmax(torch.randn(4, 7), -1)  # probability targets: also valid
print(F.cross_entropy(x3, y3), F.cross_entropy(x2, soft))   # both fine
apply_liger_kernel_to_granite(rope=False, rms_norm=False, swiglu=False, cross_entropy=True)
print(F.cross_entropy.__module__)                            # liger_kernel.transformers.functional
F.cross_entropy(x3, y3)    # ValueError: too many values to unpack (expected 2)
F.cross_entropy(x2, soft)  # raises here on CPU; we did not check what it does on GPU

(CPU, Liger main.) 36 of the 45 apply_liger_kernel_to_* functions set nn.functional.cross_entropy = liger_cross_entropy, so an unrelated loss in the same training script, say a distillation loss with soft targets, breaks once a model is patched.

Patching only the patched model's module reference, or having liger_cross_entropy fall back to the original function for inputs it doesn't handle (more than 2 dims, floating-point targets), would avoid it. Found in Halo, which scopes its Liger cross entropy to transformers' loss module for this reason.

Dominant language
Python
Stars
6.7k
Forks
656
Avg merge
4d 12h
Merged PRs (30d)
27

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from linkedin/Liger-Kernel

All issues in linkedin/Liger-Kernel

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.