Skip to content

ORPO / CPO / DPO / KTO chunked head: fall back under torch's global module hooks - #15259

Merged
danielhanchen merged 6 commits into
unslothai:mainfrom
lonexreb:fix/preference-chunked-head-global-module-hooks
Nov 8, 2026
Merged

danielhanchen merged 6 commits into
unslothai:mainfrom
lonexreb:fix/preference-chunked-head-global-module-hooks

Conversation

@lonexreb

@lonexreb lonexreb commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The chunked-head guards from #14185 (_unsloth_pref_chunked_head for ORPO / CPO, _unsloth_dpo_chunked_head for 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 patched forward. The fused kernel reads head.weight / head.bias directly 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_hook and register_module_full_backward_pre_hook live 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.py already 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_contained now 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 returns None; after handle.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 the output head has hooks or a patched forward fallback and matches TRL's loss.
  • Locally (CPU, unsloth_zoo without 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

  • Merged current main.
  • GKD had the same gap. Its chunked JSD reads both heads' weights directly, so neither the heads' own forward / backward hooks nor torch's global module hooks fired. It now falls back to TRL's dense logits in both cases. Accelerate's _hf_hook still uses the fused path, because the loss moves to the head's device.
  • Tests cover all four global hook kinds (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.
  • The generated GKD trainer source carries the helper.
main this branch
ORPO / CPO / DPO / GKD under a global module hook fused path runs, hook never sees the head falls back to TRL logits
hook tests (latest venv) 19 failed 20 passed
hook tests (floor, transformers 4.57.6) 23 failed 24 passed
preference, DPO and GKD test files, with TRL tags 131 passed / 17 skipped (latest), 143 passed / 5 skipped (floor)

GRPO also reads lm_head.weight directly, inside unsloth_zoo's fused loss. It has no plain path to fall back to, so it is left for a separate change.

lonexreb and others added 2 commits October 31, 2026 16:24
…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]>
@lonexreb
lonexreb force-pushed the fix/preference-chunked-head-global-module-hooks branch from f0ac4c4 to 711e12d Compare October 31, 2026 23:30
@danielhanchen danielhanchen self-assigned this Nov 7, 2026
…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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-07T15:55:39.566993Z 7b8670b 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. 🎉

Reviewed commit: f42104ee0e

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

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.
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 7b8670b3f9

ℹ️ 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 22a024f into unslothai:main Nov 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants