Skip to content

fix(loss): keep autograd and honor reduction in NemotronParseLoss - #3760

Draft
HuiyingLi wants to merge 1 commit into
mainfrom
huiyingl/fix/nemotron-parse-zero-loss
Draft

fix(loss): keep autograd and honor reduction in NemotronParseLoss#3760
HuiyingLi wants to merge 1 commit into
mainfrom
huiyingl/fix/nemotron-parse-zero-loss

Conversation

@HuiyingLi

Copy link
Copy Markdown
Contributor

What does this PR do ?

Fixes two defects in NemotronParseLoss, split out of #3614 where they were found:

  1. Broken autograd on all-ignored batches. When every label is ignore_index, the loss returned a fresh torch.tensor(0.0) with no autograd graph, so loss.backward() raises (or, inside a larger sum, silently contributes no gradient path). Returning loss_sum * 0 keeps the graph while still contributing zero.
  2. reduction='sum' silently behaved as mean. The final return always divided by valid_tokens, so a config declaring reduction='sum' (required for global-token-normalized finetuning) actually got a mean. Reduction is now dispatched explicitly, with a clear error for unsupported values. The num_label_tokens path is unchanged.

Changelog

  • nemo_automodel/components/models/nemotron_parse/nemotron_parse_loss.py: keep the autograd path for empty batches; honor reduction='sum' / 'mean' explicitly.
  • tests/unit_tests/loss/test_nemotron_parse_loss.py: cover gradient flow through the empty-batch return, sum-vs-mean reduction, and the unsupported-reduction error.

Validation

  • tests/unit_tests/loss/test_nemotron_parse_loss.py: 27 passed.
  • ruff format / ruff check clean.

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 add or update any necessary documentation?

Additional Information

馃 Generated with Claude Code

An all-ignored batch returned a graph-free torch.tensor(0.0), breaking
backward; reduction='sum' silently divided by valid_tokens anyway. Return
loss_sum * 0 for the empty case and dispatch reduction explicitly.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: HuiyingLi <willwin.lee@gmail.com>
@copy-pr-bot

copy-pr-bot Bot commented Aug 30, 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.

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.

1 participant