Skip to content

fix: avoid NaN in JSD metric when logprobs underflow to zero - #4025

Open
kashif wants to merge 2 commits into
NVIDIA-NeMo:mainfrom
kashif:fix/jsd-metric-logaddexp-underflow
Open

kashif wants to merge 2 commits into
NVIDIA-NeMo:mainfrom
kashif:fix/jsd-metric-logaddexp-underflow

Conversation

@kashif

@kashif kashif commented Sep 6, 2026

Copy link
Copy Markdown

What does this PR do ?

Fixes a NaN in the js_divergence_error training metric when logprobs underflow to zero.

Issues

Our JSD metric builds the mixture distribution as log(0.5*exp(a) + 0.5*exp(b)). Once a logprob drops low enough that exp() underflows to 0 in fp32, this becomes log(0) and the KL terms downstream turn into inf - inf, i.e. NaN. Switched to torch.logaddexp, which stays stable in that regime.

Only affects the logged metric, not gradients/training - but a NaN there makes debugging confusing.

Usage

No usage change.

Before your PR is "Ready for review"

Pre checks:

  • Make sure you read and followed Contributor guidelines
  • Did you write any new necessary tests?
  • Did you run the unit tests and functional tests locally? Visit our Testing Guide for how to run tests
  • Did you add or update any necessary documentation? Visit our Document Development Guide for how to write, build and test the docs.

Additional Information

  • ...

@kashif
kashif requested a review from a team as a code owner September 6, 2026 11:20
@copy-pr-bot

copy-pr-bot Bot commented Sep 6, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

Noticed our js_divergence_error metric builds the mixture distribution the
same way as log(0.5*exp(a) + 0.5*exp(b)), which produces log(0) -> NaN once
both logprobs underflow in fp32. Switching to logaddexp keeps it stable
(same thing Automodel's parity_metrics.py already does).

Signed-off-by: Kashif Rasul <kashif.rasul@gmail.com>
Adds a small CPU test that feeds ClippedPGLossFn logprobs low enough to
underflow to zero in fp32, so it catches the NaN this same commit fixed.

Signed-off-by: Kashif Rasul <kashif.rasul@gmail.com>
@kashif
kashif force-pushed the fix/jsd-metric-logaddexp-underflow branch from ef7040a to 3cf6b2c Compare September 6, 2026 11:25
@kashif
kashif requested a review from a team as a code owner September 6, 2026 11:25
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the waiting-on-maintainers Waiting on maintainers to respond label Sep 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-request waiting-on-maintainers Waiting on maintainers to respond

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants