Repository navigation
Fix perplexity overflow to inf for long float16 sequences - #3576
mangeshraut712 wants to merge 7 commits into
Conversation
Sum the selected log-probabilities in at least float32 so a long float16 update cannot overflow the accumulator. Co-authored-by: Mangesh Raut <mangeshraut712@users.noreply.github.com>
|
The CUDA py3.12/torch2.10 Lit job failure looks unrelated to this change. The same job is also failing on #3578 (pearson), #3571 (concordance) and #3570 (tracker), none of which touch perplexity. The perplexity tests pass locally on CPU (25 passed, CUDA cases skipped). I can't open the Lit job logs, so a rerun would confirm it. |
|
Rechecked the failure: this exact Lit matrix is red on #3578, #3571, and #3570 too, while their 22.04/CUDA12.4/Python 3.11/Torch 2.6 sibling passes. It also failed on every commit here, including the The new regression test is the #3575 100k-token float16 reproduction and passes locally with Python 3.12 / Torch 2.10 on CPU (25 passed; 8 CUDA-only skips). GitHub exposes zero annotations and the Lightning job URL now returns 404, so there is no failure trace to act on. I tried re-requesting the external check through GitHub, but the provider returns 404. The fresh test-only push |
|
AI-assisted independent acceptance check on d75d4db, including actual two-rank CPU/Gloo accumulation with unequal long shards. Ranks process 120,000 and 17,000 float16 tokens with nonuniform three-class logits and every fifth target ignored. Head returns 3.2565212 on both ranks, matching a reference that sums the already-rounded input-dtype token logs in float64, weighted by globally reduced token counts. With only the changed implementation file replaced by master 6c551c6, both ranks return inf. A separate 16-case functional/module matrix crosses float16/bfloat16/float32/float64, ignore masking and whole/split updates. Head passes all 16; baseline passes nine and fails seven inspected value assertions. The baseline failures include float16 overflow and smaller finite low-precision accumulation errors, not seven overflow cases. Float32/64 controls pass both. The reference intentionally keeps the existing per-token softmax/log rounding, so this verifies the sum widening, not a fix for softmax underflow or full float64-logit accuracy. Identical final non-DDP perplexity tests give head 21 passed/8 skipped versus baseline 20 passed/8 skipped/1 new regression failed; four distributed cases are deselected on both. The Gloo check is a separate harness. Python 3.10.12, PyTorch 2.14.1+cpu. No CUDA or CI-failure diagnosis claim. |
# Conflicts: # src/torchmetrics/functional/text/perplexity.py # tests/unittests/text/test_perplexity.py
|
Exact-head validation on 352ee3b: I ran the complete tests/unittests/text/test_perplexity.py file on macOS arm64 with Python 3.11 and PyTorch 2.15.0. Result: 28 passed, including the long float16 regression and the four DDP cases, and 8 skipped because they require CUDA. This verifies the current head locally. The hosted CUDA matrix was outside this run. |
352ee3b to
757ac8c
Compare
What does this PR do?
Fixes #3575
_perplexity_updatesummed the selected token log-probabilities in the input dtype. In float16 that sum overflows past 65504, so a long sequence came back asinfeven though the average negative log-likelihood was finite. The sum now usestorch.promote_types(log_probs.dtype, torch.float32). Float32 and float64 stay on their own dtype. Only the selected log-probabilities are widened, not the full vocabulary table.On the 100,000-token float16 example from the issue (uniform logits, perplexity 2), the old sum is
inf. After this change both the functional and the module return2.0004in float32. The leftover error is float16 rounding of the per-token log. Float16 input now returns float32 from the functional as well, because the accumulator is float32. The module already did that.This does not address the softmax underflow in #3537.
Locally,
pytest tests/unittests/text/test_perplexity.pypassed (25 passed, 8 skipped; no GPU here), and pre-commit passed on the changed files.The public docs do not describe the accumulator dtype, so I left them unchanged.
Before submitting
PR review
Anyone in the community is free to review the PR once the tests have passed.
If we didn't discuss your PR in Github issues there's a high chance it will not be merged.
Did you have fun?
Make sure you had fun coding 🙃