Repository navigation
ORPO / CPO / DPO / KTO chunked head: fall back under torch's global module hooks - #15259
Conversation
…odule hooks The chunked-head guards (unslothai#14185) skip the fused path when the output head carries a hook of its own (_hf_hook, forward / forward-pre / backward tables, a patched forward): the kernel reads head.weight directly and never calls the module. A hook registered through torch.nn.modules.module.register_module_forward_hook / _forward_pre_hook / _full_backward_(pre_)hook applies to every module through the global tables instead, which neither guard reads, so under such a hook (profilers, FLOP counters, user tooling) every module fires it except the head. Add _unsloth_global_module_hooks and consult it from both guards, listed in the ORPO / CPO, DPO and KTO source-copy lists so the generated trainers stay self-contained. unsloth/models/dpo.py already guards the global backward tables for the SFT-style fused head (unslothai#15226 extends it to the forward ones). Tests: a global forward hook makes _unsloth_pref_chunked_head return None and the DPO run record the "output head has hooks" fallback with TRL's loss. Co-Authored-By: Claude Fable 5.1 <[email protected]>
…trainers too 8fec282 (unslothai#15244) routed BCO, Online DPO, XPO and Nash-MD through _unsloth_dpo_chunked_head after this branch was cut. Their generated source copies the guard but not _unsloth_global_module_hooks, which the guard now calls, so every Online DPO fused pass raised NameError (tests/python/test_fused_linear_online_dpo.py, 6 failures on this PR's run). Add the helper to that copy list, and a test that every generated trainer carrying a chunked-head guard also defines the helper. Co-Authored-By: Claude Opus 5.5 <[email protected]>
f0ac4c4 to
711e12d
Compare
…oks or torch global module hooks are set The JSD reads both heads' weights directly, so a hook on the head never fired. Tests cover all four global hook kinds for ORPO / CPO, DPO and GKD, with a positive control and a check that removing the hook restores the fused path.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
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". |
TRL activation_offloading registers a forward pre / post pair on lm_head that only pauses offloading around the logits. The new head-hook guard treated it like any other hook and sent GKD back to dense logits, which the fused path did not do before. Ignore only that pair; any other forward or backward hook on either head still falls back.
|
@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". |
Problem
The chunked-head guards from #14185 (
_unsloth_pref_chunked_headfor ORPO / CPO,_unsloth_dpo_chunked_headfor DPO and, through_unsloth_fused_pass_head, KTO) keep TRL's logits path when the output head has a hook of its own:_hf_hook, the head's forward / forward-pre / backward tables, or a patchedforward. The fused kernel readshead.weight/head.biasdirectly and never calls the module, so a hook on it would be skipped.Hooks registered through
torch.nn.modules.module.register_module_forward_hook,register_module_forward_pre_hook,register_module_full_backward_hookandregister_module_full_backward_pre_hooklive in torch's global tables, not on the head, and neither guard reads them. Under such a hook (memory profilers, FLOP counters, user tooling) every module in the model fires it except the head, silently.unsloth/models/dpo.pyalready guards the global backward tables for the SFT-style fused head (_global_module_backward_hooks); #15226 extends that to the forward tables. These two guards are the sibling entry points.Fix
One helper,
_unsloth_global_module_hooks, consulted by both guards alongside the head's own tables, and listed in the ORPO / CPO, DPO and KTO source-copy lists so the generated trainer sources stay self-contained (test_generated_items_are_self_containednow checks for it).Verification
tests/python/test_preference_chunked_logprobs.py::test_declines_without_kernel_or_on_unsupported_setups: with a global forward hook registered the ORPO guard returnsNone; afterhandle.remove()it returns the head again.tests/test_dpo_chunked_logprobs.py::test_global_module_hook_falls_back: a DPO run under a global forward hook makes zero chunked calls, records theoutput head has hooks or a patched forwardfallback and matches TRL's loss.chunked_logprob) the fused route cannot fire, so those tests skip; verified instead that the helper flips on each of the four global tables and is clear after removal, that both guards call it, and ruff / duplicate-definition lint are clean.Follow-up
_hf_hookstill uses the fused path, because the loss moves to the head's device.forward,forward_pre,full_backward,full_backward_pre) for ORPO / CPO, DPO and GKD. Each test first checks that the fused path runs with no hook (call counter above zero), then that removing the hook restores it. Under a hook, ORPO's loss and grads equal TRL's exactly.GRPO also reads
lm_head.weightdirectly, inside unsloth_zoo's fused loss. It has no plain path to fall back to, so it is left for a separate change.