add confidence weighted CE (DFT) as fused loss function#903
add confidence weighted CE (DFT) as fused loss function#9030xEljh wants to merge 6 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a new fused DFT loss function (unsloth_fused_dft_loss and compute_fused_dft_loss) to the Unsloth Zoo, along with comprehensive unit tests to verify its correctness, gradient behavior, and compilation compatibility. The feedback suggests optimizing the DFT loss implementation by removing redundant .contiguous() calls, using reshape for safer layout handling, and replacing a 0-D tensor allocation in torch.where with a scalar 0.0 to reduce GPU overhead.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ff710d2463
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
@codex review |
|
Codex Review: Didn't find any major issues. 🎉 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
…le order, and label-smoothing guard
|
@codex review |
|
Codex Review: Didn't find any major issues. Bravo. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Summary
Dynamic Fine-Tuning (DFT) has already been added to TRL and is accessible by setting
loss_type="dft"in theSFTTrainer. But doing so would currently deviate from the unsloth memory optimized path.This PR adds DFT fused loss support for using the existing chunked fused-loss infrastructure. It is the first in a planned series of PRs to add support for RL x SFT hybrid losses as initially attempted in this PR in the main unsloth repo.
DFT weights each token’s negative log-likelihood by its detached predicted probability:
$$\text{sg}(\exp(-NLL_t)) * NLL_t$$
This reduces the contribution of low-confidence (high surprise) tokens while preserving the intended detached-weight gradient behavior.
Notably, this isn't just a matter of passing a scaling to fused CE, as access to the per-token$$NLL$$ is required.
Creating this PR in
unsloth-zoofirst before patchingunsloth.Changes
compute_fused_dft_lossfor direct DFT loss computation.unsloth_fused_dft_lossusing the existing UnslothFusedLoss autograd and chunking path.fused_ce_lossbehavior as possible:unsloth_fused_dft_lossthroughloss_utils.For the purpose of this PR, we intentionally avoid doing anything to
compute_fused_ce_lossand instead duplicate most of its behavior intocompute_fused_dft_loss.Testing
Correctness
Correctness tests are in
tests/test_fused_dft_loss.py.Most notable are
tests/test_fused_dft_loss.py::test_compute_dft_minus_p_log_p_shapetests/test_fused_dft_loss.py::test_compute_dft_single_token_closed_form_loss_and_gradienttests/test_fused_dft_loss.py::test_unsloth_fused_dft_compiled_parity_and_probe_successCompilation
DFT compiled successfully on:
Full compilation test
Future Work
loss_type=dftonSFTTrainerroutes through this change.loss_type=asft