Add Weighted Instruction Tuning loss to SFTTrainer - #6747
Conversation
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Want higher recall? High effort reviews run extra passes and find more bugs. A team admin can switch effort levels in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit ac7cce8. Configure here.
|
Hi @qgallouedec, when you have a chance, could you please take a look at this PR or route it to the appropriate reviewer? I'm the co-first author of the WIT paper, which has been peer-reviewed and accepted to the Transactions of the Association for Computational Linguistics (TACL) -- the flagship journal of the ACL community with impact factor of 11.7 in 2025. I am available to explain the objective, validate the mathematical details, and address any requested changes. The PR adds WIT as an opt-in SFTTrainer loss, including exact nonzero-token normalization under gradient accumulation, compatibility validation, documentation, and tests. I have reviewed the code thoroughly from my side. |

What does this PR do?
Adds Weighted Instruction Tuning (WIT) as an opt-in
SFTTrainerloss for text prompt-completion datasets:WIT assigns independent weights to prompt and completion token losses. The weighted loss is normalized by the number of tokens whose weight is non-zero, rather than by the sum of weights. Consequently,
(0.0, 1.0)recovers completion-only SFT and(1.0, 1.0)recovers full-sequence language modeling.The implementation:
labels, allowing current Transformers to compute the exact nonzero-token denominator across gradient-accumulation microbatches;num_items_in_batchin the custom loss so Trainer does not apply an additional gradient-accumulation division;Validation performed locally on the current
mainbranch:make precommitpython -m pytest -p no:rerunfailures tests/test_sft_trainer.py— 116 passed, 166 skippedFixes #6746
Before submitting
AI writing disclosure
We welcome the use of AI tools to help with contributions. For transparency and to help us improve our review process, please indicate the level of AI involvement in this PR.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag members/contributors who may be interested in your PR.