fix(gkd): promote low-precision logits before loss computation - #10175
Open
taking-lying-flat wants to merge 3 commits into
Open
taking-lying-flat wants to merge 3 commits into
taking-lying-flat wants to merge 3 commits 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.
PR type
PR information
GKD can produce negative JSD and inaccurate student-logit gradients when student logits are FP32 and teacher logits are BF16. Temperature scaling and probability calculations round the teacher values before they are combined with the student values.
Promote FP16/BF16 logits to FP32 in the shared loss pipeline before vocabulary alignment and temperature scaling, preserving existing FP32/FP64 inputs. The top-k path gathers the selected student scores before promotion to avoid an additional full-vocabulary FP32 buffer.
The loss change adds 7 net lines. Update one existing empty-mask test assertion to expect FP32 for FP16/BF16 inputs while preserving FP64. The test continues to check zero loss, zero gradients, and vocabulary alignment.
Fixes #10156.
Experiment results
Validated on CPU and one NVIDIA RTX A1000 Laptop GPU with PyTorch 2.13.0+cu130:
python -m pytest tests/train/test_gkd_loss.py -q: 5 tests and 24 subtests passed.pre-commit run --files swift/rlhf_trainers/gkd_loss.py tests/train/test_gkd_loss.py: all applicable hooks passed.git diff --check: passed.Multi-rank TP/CP and end-to-end training were not run.