Fix TVD gradient at p == q - #1374
Open
truong-v wants to merge 1 commit into
Open
Conversation
TVD(p, q) = 0.5 * sum |p - q|, so its derivative with respect to p is 0.5 * sgn(p - q), which is 0 where the two distributions agree. The kernel's two-way tl.where had no zero branch and returned -0.5 * scale for every tied element, so self-distillation (p == q) and padded vocabulary slots (exactly 0.0 in both) got a non-zero gradient. Add the tie branch and cover it in test_tvd.py.
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.
Summary
_tv_distance_kernelcomputes the gradient astl.where(p > q, 0.5 * scale, -0.5 * scale), a two-way split of a three-way sign:p == qfalls into thep < qbranch and gets-0.5 * scalewhere the derivative of0.5 * |p - q|is0, so the loss is unaffected but the gradient is wrong on exact ties. Ties are reachable with default settings — self-distillation (student and teacher identical at step 0) makes every element a tie, and padded vocabulary slots are exactly0.0in both distributions — and the suite's own reference,TorchTVDLoss(torch.abs(p - q) / 2.0), already returns0there, but the tests never draw a tie because both distributions are random.Fixes #1373
Details
src/liger_kernel/ops/tvd.py: wrap the existingtl.wherein a zero branch forp == q. Nothing else changes — untied elements take the same path as before.test/transformers/test_tvd.py: newtest_correctness_with_ties, which ties the trailing slots of the target (fraction1.0= self-distillation,0.5= padded vocabulary) and compares againstTorchTVDLossacross all four reductions and both dtypes.Gradient at a tie,
p == qeverywhere,reduction="batchmean",(8, 512)fp32 on H100:-0.06250.0TorchTVDLossreference0.0The extra comparison is one predicate and one select per element in a bandwidth-bound kernel. Forward op,
(4096, 32000)bf16,do_bench, three interleaved runs on one H100 NVL: 0.2337 / 0.2338 / 0.2335 ms before, 0.2337 / 0.2339 / 0.2354 ms after — within run-to-run noise.Testing Done
test/transformers/test_tvd.py: 232 passed (200 pre-existing + 32 new).Reverting only the kernel change makes 24 of the 32 new cases fail; the 8 that still pass are
reduction="none", which returns before the backward pass since the loss itself is correct.test_tvd.pyis the only file in the suite that exercises this kernel, so nothing else inmake testis affected. I ranruff check .andruff format --check .over the repo, but not the full suite or the convergence suite locally — boxes below reflect that.Hardware Type: H100 NVL
run
make testto ensure correctnessrun
make checkstyleto ensure code stylerun
make test-convergenceto ensure convergenceEnvironment
a5d795efd2c1436549e70118ef519134e9c27833(main) + this branch, editable install