fix(loss): keep autograd and honor reduction in NemotronParseLoss - #3760
Draft
HuiyingLi wants to merge 1 commit into
Draft
fix(loss): keep autograd and honor reduction in NemotronParseLoss#3760HuiyingLi wants to merge 1 commit into
HuiyingLi wants to merge 1 commit into
Conversation
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>
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.
What does this PR do ?
Fixes two defects in
NemotronParseLoss, split out of #3614 where they were found:ignore_index, the loss returned a freshtorch.tensor(0.0)with no autograd graph, soloss.backward()raises (or, inside a larger sum, silently contributes no gradient path). Returningloss_sum * 0keeps the graph while still contributing zero.reduction='sum'silently behaved as mean. The final return always divided byvalid_tokens, so a config declaringreduction='sum'(required for global-token-normalized finetuning) actually got a mean. Reduction is now dispatched explicitly, with a clear error for unsupported values. Thenum_label_tokenspath is unchanged.Changelog
nemo_automodel/components/models/nemotron_parse/nemotron_parse_loss.py: keep the autograd path for empty batches; honorreduction='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 checkclean.Before your PR is "Ready for review"
Pre checks:
Additional Information
馃 Generated with Claude Code