Add inplace=False to liger_cross_entropy to fix upstream gradient corruption (#272)#1251
Open
lollinng wants to merge 2 commits into
Open
Add inplace=False to liger_cross_entropy to fix upstream gradient corruption (#272)#1251lollinng wants to merge 2 commits into
lollinng wants to merge 2 commits into
Conversation
… grads (linkedin#272) liger_cross_entropy stores its backward gradient in-place into the forward input via a Triton tl.store. Triton writes do not bump PyTorch's tensor version counter, so autograd's in-place-correctness check never fires. If an upstream op (e.g. softmax) saved that same tensor for its own backward, it is silently overwritten and computes wrong gradients with no error (linkedin#272). Thread an inplace flag (default True, preserving the current memory-saving behavior) through liger_cross_entropy -> LigerCrossEntropyFunction -> cross_entropy_forward. When inplace=False, operate on a clone of the input so the caller's tensor is preserved and upstream gradients stay correct. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The kernel only writes the gradient when HAS_GRADIENTS is true, which was read from _input.requires_grad at kernel-launch time. With inplace=False the clone happens inside the autograd Function (grad disabled), so the clone reported requires_grad=False and the kernel skipped the gradient write, leaving logit values in the returned buffer. Capture requires_grad before the clone. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem (#272)
liger_cross_entropystores its backward gradient in-place into its input tensor via a Tritontl.store. Triton writes don't bump PyTorch's tensor version counter, so autograd's in-place-correctness check never fires. When the input is the output of an upstream op (e.g. asoftmax) that saved it for its own backward, that tensor is silently overwritten and the upstream op computes wrong gradients with no error — the exact scenario in #272.Fix
Thread an
inplaceflag (defaultTrue, so existing behavior and the memory savings are unchanged) throughliger_cross_entropy→LigerCrossEntropyFunction→cross_entropy_forward. Wheninplace=False, the gradient is computed into a clone of the input, leaving the caller's tensor intact.One subtlety worth calling out: the clone happens inside
autograd.Function.forward(grad disabled), so the clone reportsrequires_grad=False. Since the kernel only writes the gradient whenHAS_GRADIENTSis true, I capturerequires_gradfrom the original input before cloning — otherwise the kernel would skip the gradient write entirely.Verification (NVIDIA T4)
Ran the issue's reproducer (
softmax(_p) → cross_entropy → backward) on a GPU, comparing the gradient w.r.t. the pre-softmax input againstF.cross_entropy:Added
test/transformers/test_cross_entropy.py::test_cross_entropy_inplace_does_not_corrupt_upstream_grad, which asserts the default path corrupts the upstream gradient (reproducing the bug) and thatinplace=Falsematches theF.cross_entropyreference.Fixes #272