cross_entropy=True rebinds torch.nn.functional.cross_entropy process-wide, so unrelated 3-D or soft-target calls fail
Maintainers usually reply within 3 days
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 65/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Domain
- backend, machine-learning
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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from linkedin/Liger-Kernel
-
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
linkedin/Liger-Kernel#1658 ·
Maintainers usually reply within 3 days
-
LigerPolyNorm defaults to in_place=True (docstring says False) and overwrites shared gradientsPossibly taken @nazek52 claimed this 1 day ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
linkedin/Liger-Kernel#1647 ·
Maintainers usually reply within 3 days
-
[Bug] Experimental embedding kernels overflow int32 row offsets (illegal memory access past 2^31 output elements)Possibly taken @0xDexe claimed this 8 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
linkedin/Liger-Kernel#1570 · 3 comments ·
Maintainers usually reply within 3 days
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
linkedin/Liger-Kernel#1661 ·
Maintainers usually reply within 3 days
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
linkedin/Liger-Kernel#1660 ·
Maintainers usually reply within 3 days
All issues in linkedin/Liger-Kernel
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
GoogleCloudPlatform/professional-services-data-validator#1899 ·
Maintainers usually reply within 4 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
mohanraj00/entelos#200 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 68/100
-
sweep
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
bketelsen/truenas-mcp#77 ·
Maintainers usually reply within 1 day
-
Console writes URL credentials to the local log, though AGENTS.md says secrets never reach diskOpen
Difficulty 1/5 Under an hour Newbie friendliness 86/100
sandbox-quantum/switch#2163 ·
Maintainers usually reply within 1 day