Repository navigation
Fix NaN loss when all tokens are masked (num_items_in_batch=0) in distillation trainers - #8773
Open
newcloud123 wants to merge 1 commit into
Open
newcloud123 wants to merge 1 commit into
newcloud123 wants to merge 1 commit into
Conversation
This branch has not been deployed
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.
Summary
In
server_distillation_trainer.py,gkd_trainer.py, andiw_opd_trainer.py, thenum_items_in_batchreduction branch returnedjsd.sum() / num_items_in_batchwith no lower bound on the denominator. HF Trainer's_get_num_items_in_batchreturns 0 when every label in a batch is-100(all completions fully masked / truncated), andjsd[mask]is then empty sojsd.sum() == 0— producing0/0 = NaN. The NaN gradient then poisons the whole optimizer step (logging_nan_inf_filteronly hides it from logs).Notably, the adjacent
reduction="batchmean"branch in each of these files already clamps its denominator (clamp_min(1)) and even has a comment describing exactly this 0/0 scenario — thenum_items_in_batchbranch (the one taken whengradient_accumulation_steps > 1) simply missed it. The same pattern exists insft_trainer.pyandgrpo_trainer.py, which already clamp.Fix
Clamp the denominator to at least 1 for both Tensor and int paths (4 occurrences across the 3 files, including
iw_opd's mainiw_opd_loss):Verification (Tier A, GPU torch 2.7)
Extracted the real
_reduce_divergence_lossbefore/after and ran withnum_items_in_batch=0, all-masked:Normal path (
num_items_in_batch=6, unmasked) is unchanged (1.0 == 1.0). Addedtests/test_num_items_zero_clamp.pywith 3 unit tests.🤖 Assisted by AI; verified before/after before submission.
Note
Low Risk
Small, localized loss-reduction guard aligned with existing batchmean clamps; normal batches unchanged per tests.
Overview
Fixes NaN training loss when gradient accumulation uses
num_items_in_batchand every label in a batch is masked (-100), so the global valid-token count is 0.In GKD, IW-OPD (JSD reduction and IW-OPD loss), and server distillation, the
num_items_in_batchbranch now clamps the denominator to at least 1 (tensor:clamp_min(1); scalar:max(..., 1)), matching the existingbatchmeanpath. Loss becomes 0 with finite gradients instead of0/0.Adds
tests/test_num_items_zero_clamp.pyto lock this behavior forServerDistillationTrainer._reduce_divergence_loss.Reviewed by Cursor Bugbot for commit b2bbc8a. Bugbot is set up for automated code reviews on this repo. Configure here.