Skip to content

Fused preference loss test: take TRL's own trainer as the reference - #15269

Merged
danielhanchen merged 1 commit into
mainfrom
ci-preference-test-trl-reference
Oct 25, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
ci-preference-test-trl-reference

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

16 cases in tests/test_fused_preference_loss.py fail in Repo tests (CPU, rest) on PRs based on current main, for example #15224 and #15245: every test_orpo_cpo_match_trl case plus the DDP / FSDP1 / hook fallback cases. The reference run trips the hidden-states counter (assert (1 >= 1) == False) or the dtype check.

Cause

The test, not the product. The test builds its reference from from trl.experimental.orpo import ORPOTrainer (and the CPO / DPO equivalents). Unsloth's RL patch rebinds those names, and the defining module's attribute, to the generated UnslothORPOTrainer (unsloth/models/rl.py, the trl.{name} = created_module.Unsloth{name} lines). Since #14185, that generated class's concatenated_forward routes through unsloth_fused_orpo_concatenated_forward. So once TRL has been patched in the process, the "TRL" side of the comparison is the code under test. The stack of the reference forward shows it: test_fused_preference_loss.py:437 -> unsloth_compiled_cache/UnslothORPOTrainer.py:1988.

It reproduces locally with unsloth-zoo main on the path (16 failed, 9 passed, run alone). With the older installed zoo 2026.10.7, TRL is not rebound here, which is why it looked like an ordering leak at first.

Fix

  • _trl_trainer(name) loads TRL's own trainer module from its file under a private module name in the same package, so TRL's relative imports still resolve and nothing Unsloth rebinds is reused.
  • It asserts that the class it returns is not an Unsloth* class.
  • ORPO, CPO and DPO all take their reference from it.

Verification

  • tests/test_fused_preference_loss.py against unsloth-zoo main: 16 failed, 9 passed on main; 25 passed here.
  • Against the installed zoo 2026.10.7: 25 passed on both main and this PR.
  • Sabotage on the product: scaling the fused ORPO path's chosen_logps by 1.001 in unsloth/models/dpo.py fails all 10 test_orpo_cpo_match_trl cases.

Unsloth's RL patch rebinds trl's ORPOTrainer / CPOTrainer / DPOTrainer, and
the defining module's attribute, to the generated Unsloth<name>. Since #14185
that class's concatenated_forward takes the fused path, so once TRL had been
patched in the process the test's TRL reference ran the code under test,
and 16 cases failed on the hidden-states counter and dtype checks against
unsloth-zoo main. The reference is now loaded from TRL's own module file
under a private name, and the test asserts it is not an Unsloth class.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 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-25T14:10:27.455848Z f606e23 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: f606e2366a

ℹ️ 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 24015fc into main Oct 25, 2026
0 of 14 checks passed
@danielhanchen
danielhanchen deleted the ci-preference-test-trl-reference branch October 25, 2026 14:15
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