Skip to content

Fix perplexity overflow to inf for long float16 sequences - #3576

Open
mangeshraut712 wants to merge 7 commits into
Lightning-AI:masterfrom
mangeshraut712:cursor/perplexity-float16-sum-c9f6
Open

mangeshraut712 wants to merge 7 commits into
Lightning-AI:masterfrom
mangeshraut712:cursor/perplexity-float16-sum-c9f6

Conversation

@mangeshraut712

@mangeshraut712 mangeshraut712 commented Oct 12, 2026 •

Copy link
Copy Markdown

What does this PR do?

Fixes #3575

_perplexity_update summed the selected token log-probabilities in the input dtype. In float16 that sum overflows past 65504, so a long sequence came back as inf even though the average negative log-likelihood was finite. The sum now uses torch.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 return 2.0004 in 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.py passed (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
  • Was this discussed/agreed via a Github issue? (no need for typos and docs improvements)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure to update the docs?
  • Did you write any new necessary tests?
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 🙃

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>
@mangeshraut712

Copy link
Copy Markdown
Author

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.

@mangeshraut712

mangeshraut712 commented Oct 25, 2026 •

Copy link
Copy Markdown
Author

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 9b6027c .float().sum() variant.

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 d75d4db is now waiting for fork-workflow approval. I cannot approve it with this token (GitHub requires repository admin rights), so a maintainer needs to choose “Approve and run” before the new CI can proceed.

@anishpentyala

Copy link
Copy Markdown

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
@mangeshraut712

Copy link
Copy Markdown
Author

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.

@mangeshraut712
mangeshraut712 force-pushed the cursor/perplexity-float16-sum-c9f6 branch from 352ee3b to 757ac8c Compare November 29, 2026 17:53

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Perplexity returns inf in float16 when one update sums many tokens (follow-up to #3537)

2 participants