Skip to content

Patch the TRL trainers that moved to trl.experimental (ORPO, CPO, Online DPO, GKD, ...) - #12097

Merged
danielhanchen merged 3 commits into
mainfrom
rl-patch-experimental-trainers
Sep 27, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
rl-patch-experimental-trainers

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

TRL 0.29 moved ORPO, CPO, Online DPO, GKD, PPO, Nash-MD, XPO, BCO and PRM out of trl.trainer into trl.experimental.<name>. Unsloth only walks trl.trainer, so on TRL 0.29+ none of these trainers get Unsloth's patched trainer / config (padding-free, fused loss, the #12069 sequence length clamp, the config default contract, and so on), while they were all patched on TRL 0.28 and older.

TRL Only in trl.experimental
<= 0.28 none (all under trl.trainer)
0.29 / 1.0 orpo, cpo, gkd, ppo, online_dpo, nash_md, xpo, bco, prm
1.14 orpo, cpo, gkd, online_dpo (ppo, nash_md, xpo, bco, prm removed upstream)

Changes

  • _import_trl_experimental_trainers() imports trl.experimental.<name>.<name>_trainer for each of those names that is not under trl.trainer (TRL's experimental warning suppressed, missing modules skipped).
  • _patch_trl_experimental_trainer() runs the existing trl.trainer pipeline through temporary trl.trainer aliases, rebinds the patched classes on trl.experimental.<name> and its config module, then removes the aliases, so trl.ORPOTrainer and friends stay absent as in TRL.
  • The experimental modules are imported before the trl.trainer walk. On main, GKDTrainer(SFTTrainer) picked up UnslothSFTTrainer as its base or TRL's SFTTrainer depending on whether the user imported it before or after Unsloth; now it is always UnslothGKDTrainer -> _UnslothGKDTrainer -> SFTTrainer.
  • Two small edits inside the existing patcher: the generated file imports from trainer.__name__ instead of the hardcoded trl.trainer.<file> (same value for existing trainers), and the thin wrapper resolution skips classes defined in trl.experimental itself, so NashMDTrainer(OnlineDPOTrainer) is not patched as its parent.

Every trainer Unsloth already generated (SFT, DPO, GRPO, KTO, RLOO, Reward) is byte-identical before and after on TRL 0.22.2, 0.25.1, 0.27.0, 0.29.0 and 1.14.0; the only new files are the experimental trainers. The extra imports cost about 0.02 s on TRL 1.14 (4 modules) and 0.11 s on TRL 1.0 (9 modules).

Evidence

Two step LoRA smokes on tiny Qwen3, TRL 1.14 unless noted:

Trainer Class after this PR Losses Plain TRL + PEFT
ORPO UnslothORPOTrainer 12.0027, 11.9996 12.0027, 11.9963
CPO UnslothCPOTrainer 13.3885, 13.3906 13.3886, 13.3866
Online DPO UnslothOnlineDPOTrainer 0.6931, 0.6931, all LoRA weights updated
GKD, lmbda=0 UnslothGKDTrainer 0.0025, 0.0025 0.0025, 0.0024
XPO (TRL 1.0) UnslothXPOTrainer 0.6922, 0.6922, all LoRA weights updated

Nash-MD fails the same way in plain TRL on transformers 5.17 ('GeometricMixtureWrapper' object has no attribute 'get_experts_implementation'), and PPO with an Unsloth model already fails on main, where it is not patched; neither changes here.

Tests

New tests/version_compat/test_trl_experimental_trainers_patched.py (registered in version-compat CI): the experimental trainers and configs are patched, trl.<Name> stays absent, and GKD keeps TRL's SFTTrainer as its base.

TRL main this PR
0.29.0 2 failed, 1 passed 3 passed
1.14.0 2 failed, 1 passed 3 passed
0.22.2 / 0.25.1 skipped skipped

test_trl_config_defaults_contract.py now also runs its ORPO, CPO and GKD cases on TRL 0.29+ (they skipped with "Unsloth does not patch ORPOConfig on this TRL") and they pass. The rest of tests/version_compat is unchanged on TRL 1.14 and 0.25.1.

TRL 0.29 moved ORPO, CPO, GKD, PPO, Online DPO, Nash-MD, XPO, BCO and
PRM out of trl.trainer into trl.experimental.<name>, so the trl.trainer
walk in patch_trl_rl_trainers never generated Unsloth<Name>Trainer or
Config for them. Run the same pipeline on those modules and rebind the
experimental namespaces. The modules are imported before the walk
rebinds SFTTrainer, so GKDTrainer keeps TRL's own base class regardless
of import order.
@danielhanchen danielhanchen self-assigned this Sep 27, 2026
@chatgpt-codex-connector

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 🔄 Running since 2026-09-27T07:37:42.889498Z 30f1ec7 PR opened
ℹ️ 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.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 27, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 27, 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-09-27T07:40:50.354275Z 30f1ec7 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for security reviews. Please try again later.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 30f1ec7c61

ℹ️ 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 69623ef into main Sep 27, 2026
36 of 39 checks passed
@danielhanchen
danielhanchen deleted the rl-patch-experimental-trainers branch September 27, 2026 11:55
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
…ine DPO, GKD, ...) (unslothai#12097)

* Patch TRL trainers that moved to trl.experimental

TRL 0.29 moved ORPO, CPO, GKD, PPO, Online DPO, Nash-MD, XPO, BCO and
PRM out of trl.trainer into trl.experimental.<name>, so the trl.trainer
walk in patch_trl_rl_trainers never generated Unsloth<Name>Trainer or
Config for them. Run the same pipeline on those modules and rebind the
experimental namespaces. The modules are imported before the walk
rebinds SFTTrainer, so GKDTrainer keeps TRL's own base class regardless
of import order.

* Test that trl.experimental trainers are patched and GKD keeps its base

* Run the trl.experimental patch test in version-compat CI
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.

1 participant