Skip to content

Fused preference loss: fall back when a global forward hook is registered - #733

Open
Imagineer99 wants to merge 3 commits into
fix/mirror-base-16090-20261030from
fix/mirror-pr-15226-20261029
Open

Imagineer99 wants to merge 3 commits into
fix/mirror-base-16090-20261030from
fix/mirror-pr-15226-20261029

Conversation

@Imagineer99

@Imagineer99 Imagineer99 commented Oct 30, 2026 •

Copy link
Copy Markdown
Owner

What

_output_head in unsloth/models/dpo.py decides whether DPO / ORPO / CPO under UNSLOTH_FUSED_PREFERENCE_LOSS=1 may read the head's weight directly instead of calling the head module. unslothai#14788 added the module backward hooks and the global full-backward (pre-)hook tables to that gate, because the kernel never calls the head and so none of its hooks fire. The global forward tables were left out: torch.nn.modules.module.register_module_forward_hook / register_module_forward_pre_hook fire on every nn.Linear in TRL's stock forward, the head included, so with one registered (activation capture, profiling, a hook that rewrites the head's input or output) the fused path was still taken and the hook silently skipped. Same defect class unslothai#14788 closed, one table over.

How

_global_module_backward_hooks becomes _global_module_hooks and also checks _global_forward_hooks and _global_forward_pre_hooks; the gate's call site follows. The _with_kwargs / _always_called tables are keyed by the same hook ids, so the two main tables cover them. Nothing else changes: per-module hooks, _hf_hook, overridden forward and subclassed heads were already refused.

Verification

tests/test_fused_preference_loss.py::test_hooked_or_custom_head_falls_back gains global_forward_hook and global_pre_hook cases (registered through the torch module-level API, removed by a finalizer). Each asserts _output_head returns (None, None), the hidden-states path is not taken, and the loss equals TRL's stock path with the hook honoured.

global forward hook global forward pre-hook
main (2faad9f) fused path taken, hook skipped (test fails at the (None, None) assert) same
this branch falls back, loss matches stock same

Whole file on CPU with the zoo kernels: 27 passed, 53 skipped (the skips are the GPU rows). ruff check, enforce_kwargs_spacing.py and the ruff-format-with-kwargs hook pass; dpo.py parses under Python 3.9.

🤖 Generated with Claude Code


Mirror of unslothai#15226 by @lonexreb, opened for review only.
Do not merge here. Fixes agreed on this mirror must be applied to the source PR.
Source description digest at mirror time: 0eefe0c250d4

Prepared against upstream main 316e5757fe7b541d6172498a7bee2076be9d941b.

Original PR replay only; no new product edits.

Validation: 80 fused preference-loss CPU tests passed; no skips.

Latest CI base: upstream main 4e799e0fdc09fa3757674fa8c513b9773f7a2b08 at mirror head 09abfec28c3203b1f7510f90122d9f3e24d9b021. The PR diff is byte-for-byte unchanged after integration. 80 targeted tests pass with no skips. This includes main's existing correction for the media-turn source-contract assertion; no unrelated test patch was added to this PR.

Current CI base: upstream 754bd217dcb1bff4c7f358075615ba743a2a3d8a (the test-only unslothai#16090 correction). The translated-label and runtime-list expectations now come from upstream; 248 affected tests pass. The net PR diff remains byte-for-byte unchanged by this base integration. Current mirror head: 11c391d19cc40ba5d3e3c02193f5a4bb77637003.

…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]>
@Imagineer99

Copy link
Copy Markdown
Owner Author

@codex review

1 similar comment
@Imagineer99

Copy link
Copy Markdown
Owner Author

@codex review

@Imagineer99
Imagineer99 changed the base branch from fix/mirror-base-20261029 to fix/mirror-base-20261030 October 30, 2026 11:18
@Imagineer99

Copy link
Copy Markdown
Owner Author

@codex review

1 similar comment
@Imagineer99

Copy link
Copy Markdown
Owner Author

@codex review

@Imagineer99
Imagineer99 changed the base branch from fix/mirror-base-20261030 to fix/mirror-base-16090-20261030 October 30, 2026 11:57
@Imagineer99

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 2, 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-11-02T08:17:29.165364Z 11c391d 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. Keep it up!

Reviewed commit: 11c391d19c

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

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.

2 participants