feat: add DFT loss and refactor SFT losses into submodule#1153
feat: add DFT loss and refactor SFT losses into submodule#1153kryvokhyzha wants to merge 1 commit intogoogle:mainfrom
Conversation
Summary of ChangesHello @kryvokhyzha, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a significant and valuable refactoring by moving the SFT loss functions into a dedicated tunix/sft/losses submodule. This greatly improves modularity and prepares the ground for adding new loss functions like the newly introduced DFT loss. The implementation of DFT loss is clear and well-tested. I have a couple of suggestions: one to ensure the new losses module is correctly exposed, and another to optimize the cross-entropy calculation for better performance and numerical stability.
73c0031 to
9e464cc
Compare
PR: Add Dynamic Fine-Tuning (DFT) loss and refactor SFT losses into submodule
Description
This PR introduces Dynamic Fine-Tuning (DFT) loss support.
Refactor:
tunix/sft/losses/submoduleThe existing
_default_loss_fnis moved into a properlosses/package with shared building blocks:per_token_cross_entropy(model, ...)- runs the forward pass and returns raw per-token cross-entropy values with the target mask.aggregate_loss(per_token_loss, mask)- masks and averages per-token losses over valid tokens.cross_entropy_loss_fn(model, ...)- standard next-token prediction NLL (drop-in replacement for the old_default_loss_fn).New: DFT loss
Based on the paper On the Generalization of Supervised Fine-Tuning: A Reinforcement Learning Perspective with Reward Rectification, DFT rescales the per-token cross-entropy loss by the model's own predicted probability (with stop-gradient):
The implementation composes on top of the standard cross-entropy building blocks:
dft_rescale(per_token_loss)- applies the per-token rescaling, usable with any per-token loss computation.dft_loss_fn(model, ...)- drop-in replacement forcross_entropy_loss_fn:Reference
Checklist