Skip to content

Fused preference loss: fall back when lm_head has module backward hooks - #14788

Merged
danielhanchen merged 1 commit into
mainfrom
dh/s3-fused-scoring-hooks
Oct 22, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
dh/s3-fused-scoring-hooks

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

With UNSLOTH_FUSED_PREFERENCE_LOSS=1, DPO / ORPO / CPO score completions from
head.weight through the chunked kernel and never call the output head module.
_output_head already declined heads with forward hooks, but it accepted heads
with module backward hooks. Those hooks then never ran, so a hook that rewrites
grad_input (gradient scaling, masking, some PEFT/analysis tooling) silently gave
different gradients than TRL's own forward.

Repro (tiny float64 Llama, ORPO, CPU):

hook on lm_head hook calls stock / fused max grad diff
none 0 / 0 5.6e-17
full backward hook zeroing grad_input 1 / 0 2.38
full backward pre hook, grad_output x3 1 / 0 4.77

Change: _output_head now also falls back to TRL when the head has
_backward_hooks (full or legacy), _backward_pre_hooks, or a global module
backward (pre) hook is registered. Weight tensor hooks and AccumulateGrad hooks
are left alone: the kernel returns dW through autograd, so they already fire
and match the stock gradients (covered by a new test).

The no-hook path is unchanged; the existing TRL parity cases pass as before.

Tests (CPU, torch 2.13, zoo main):

  • TRL 0.24.0: base 4 failed / 74 passed, head 78 passed
  • TRL 1.15: base 4 failed / 21 passed, head 25 passed (DPO cases skip by design)
    The 4 base failures are the new test_backward_hooked_head_falls_back cases.

… hooks

The fused DPO / ORPO / CPO path scores from head.weight without calling the
head module, so module backward hooks on lm_head (full, pre, legacy and
global module hooks) never fired and could silently change gradients versus
TRL's forward. Treat them like forward hooks and let TRL's forward run.
Weight tensor and AccumulateGrad hooks still see the kernel's dW and keep
the fused path.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-22T19:34:13.661507Z 4f823d9 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 4f823d9c42

ℹ️ 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".

@danielhanchen
danielhanchen merged commit 2350ba8 into main Oct 22, 2026
3 of 30 checks passed
@danielhanchen
danielhanchen deleted the dh/s3-fused-scoring-hooks branch October 22, 2026 21:13
danielhanchen added a commit that referenced this pull request Oct 22, 2026
pre-commit.ci fails `ruff-format-with-kwargs` on every open PR (#14806,
#14805, #14803, #14790, #14785, #14781, #14780, #14777, #14776, #14775,
#14774, #14773, #14772, #14770, #14769, #14768, and others): "files were
modified by this hook". pre-commit.ci runs the hooks on all files, and
20 files on main (26 by the time this lands) are not in the formatter's
output, so every PR is reformatted outside its own diff. Where
pre-commit.ci can push, its autofix commit drags those unrelated files
into the PR (for example #14790's touches `tests/test_block_compile.py`,
`tests/test_bnb_integration_compile.py` and `unsloth/models/_utils.py`).

The drift came in with PRs merged today, among them #14702, #14751,
#14759, #14784, #14786, #14787, #14788, #14791 and the FP8 block decode,
GRPO routing replay, 4-bit decode, compiled blocks and MoE LoRA target
changes.

### Change

`python scripts/run_ruff_format.py` (the hook's own entry point,
`ruff==0.6.9` as pinned in `.pre-commit-config.yaml`) applied to those
20 files, plus 6 more from #14739, #14779 and #14806, which merged while
this was open. Formatting only.

### Verification

- Every changed file parses to an identical AST before and after.
- A second run of the hook over the same files changes nothing (fixed
point), and over all tracked Python files on this branch it changes
nothing.
- `ruff check` passes on the changed files;
`tests/test_formatter_fixed_point.py` passes (16).

Open PRs pick this up on their next pre-commit.ci run after merging
main.
lonexreb added a commit to lonexreb/unsloth that referenced this pull request Oct 31, 2026
…ered

unslothai#14788 made _output_head refuse the fused kernel when the head has module backward hooks or a global full-backward (pre-)hook is registered, since the kernel never calls the head module. The global forward tables were left out: register_module_forward_hook / register_module_forward_pre_hook fire on every nn.Linear in TRL's stock forward, so with one registered the fused path silently skipped a hook that rewrites the head's input or output. Check those two tables in the same gate.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant