Skip to content

Restore warnings_issued for trl trainers on transformers 5.1+ - #7854

Merged
danielhanchen merged 4 commits into
unslothai:mainfrom
danielhanchen:pr/warnings-issued-transformers5
Aug 5, 2026
Merged

danielhanchen merged 4 commits into
unslothai:mainfrom
danielhanchen:pr/warnings-issued-transformers5

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

warnings_issued

transformers set self.warnings_issued = {} in PreTrainedModel.__init__ up to and including 5.0.0, and dropped it in 5.1.0. trl did not follow. Eight trainers (grpo, dpo, online_dpo, kto, orpo, cpo, rloo and experimental bco) still open __init__ with

model.warnings_issued["estimate_tokens"] = True

so on transformers 5.1+ the trainer cannot be constructed at all:

AttributeError: 'Qwen2ForCausalLM' object has no attribute 'warnings_issued'

models/rl.py already guards this, but only inside the source it generates, so the guard exists exactly when compilation does. UNSLOTH_COMPILE_DISABLE=1 takes the guard away while trl's write remains, which is the opposite of what that flag is for. The guard now lives in the backwards-compatibility wrapper, which runs in both modes.

It is deliberately no stricter than the generated one: a non-module (trl also accepts a repo id string) is left alone, an existing dict is left alone, and a model that refuses the assignment is trl's problem to report rather than ours to turn into a different traceback.

Verified live: NeMo-Gym-Sudoku on an L4 with UNSLOTH_COMPILE_DISABLE=1 now trains, loss 0.617 to 0.002.

A silent fallback made visible

_patch_trl_rl_trainers swallowed generation failures at logger.info level. The benign case that swallow exists for (a trainer this TRL version does not ship) already returns from the impl, so anything reaching the handler means the module imported and generation itself failed. The run then falls back to trl's own trainer, losing Unsloth's compute_loss, its bf16/fp16 fixup and its dataset handling at once, and says nothing about it at default log level.

That is not hypothetical. A single missing name in a generated module took out several notebooks at once, each with a different and entirely misleading error message. The handler now uses logger.warning_once and names both the trainer file and the exception. It still swallows, because TRL 1.x renames classes and hard-failing there would be worse.

Tests

28 new tests, 28 passing:

  • tests/test_warnings_issued_guard.py (23) covers the helper against dicts, non-dicts, non-modules and objects that reject the assignment, checks the call site sits outside the version branch, and enumerates the trl trainers that write the attribute.
  • tests/test_generation_failure_is_visible.py (5) asserts the handler warns rather than informs, and that it still returns instead of raising.

@danielhanchen
danielhanchen requested a review from Datta0 as a code owner August 4, 2026 15:32
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b5eaf33cd6

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

Comment thread unsloth/trainer.py
# Reconstruct kwargs for Trainer
kwargs = trainer_kwargs
kwargs["args"] = config
_ensure_warnings_issued(args[0] if args else kwargs.get("model"))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Initialize models loaded from repository IDs

When a supported TRL trainer is constructed with model="org/model" on transformers 5.1+, this call passes the string to _ensure_warnings_issued, which deliberately returns for non-modules. TRL then loads a PreTrainedModel internally and reaches its unconditional model.warnings_issued["estimate_tokens"] = True with the attribute still absent, so trainer construction continues to fail for the repository-ID input path this wrapper claims to support. The guard needs to run on the model after TRL materializes it, or that loading path must otherwise initialize the attribute.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The repo-id path is deliberately out of scope here, not a claim: the docstring at trainer.py:618-619 says a non-module is left alone, and tests/test_warnings_issued_guard.py:132-139 parametrizes "Qwen/Qwen2.5-1.5B" and asserts exactly that. It is also not a regression, since before this commit every input crashed on transformers 5.1+ with UNSLOTH_COMPILE_DISABLE=1, and the string case is byte-for-byte unchanged. Separately, trl removed the unconditional write in trl#4960 (v0.27.2), so the underlying issue is fixed upstream.

transformers set `self.warnings_issued = {}` in `PreTrainedModel.__init__` up to
and including 5.0.0, and dropped it in 5.1.0. trl did not follow. Eight of its
trainers (grpo, dpo, online_dpo, kto, orpo, cpo, rloo and experimental bco) open
`__init__` with

    model.warnings_issued["estimate_tokens"] = True

so on transformers 5.1+ the trainer cannot be constructed at all:

    AttributeError: 'Qwen2ForCausalLM' object has no attribute 'warnings_issued'

We already guard this, but only in the source generated for the compiled trainer
(models/rl.py), so the guard exists exactly when compilation does. Running with
UNSLOTH_COMPILE_DISABLE=1, the documented way out of a compile problem, removes
the generated module and takes the guard with it while trl's write stays.
`_backwards_compatible_trainer` wraps every trl trainer whose Config exists,
which on trl 0.25.1 is all eight writers, and it runs in both modes, so the
guard belongs there as well. The generated guard is left in place; this adds a
second one rather than moving it. On transformers 4.x and 5.0 the attribute is
already a dict and the guard returns immediately.

Verified on an L4 with UNSLOTH_COMPILE_DISABLE=1 running NeMo-Gym-Sudoku: the
run now trains, loss 0.617 -> 0.002.

Also raise the level of the failure path in `_patch_trl_rl_trainers`. It
swallowed every exception into `logger.info`. The swallow is deliberate, since
TRL 1.x moves and renames trainer classes, but that benign case never reaches
the handler: `_patch_trl_rl_trainers_impl` returns early when
`trl.trainer.<name>` cannot be imported. Anything arriving at the handler means
the module imported and generation itself failed, and the run then continues on
trl's own trainer, losing Unsloth's compute_loss, its bf16/fp16 fixup and its
dataset handling at once. That happened, and six notebooks failed with six
unrelated-looking errors before the info-level log was noticed. Now warned once,
with the exception and with what the user loses. Still swallowed rather than
raised.

Tests: tests/test_warnings_issued_guard.py (23, including a control that the
unguarded trainer still reproduces the reported AttributeError, and a check that
every trl trainer writing the attribute is one the patch loop wraps) and
tests/test_generation_failure_is_visible.py (5).
@danielhanchen
danielhanchen force-pushed the pr/warnings-issued-transformers5 branch from b5eaf33 to 258b794 Compare August 4, 2026 18:05
@danielhanchen

Copy link
Copy Markdown
Member Author

CI note. The two test_hf_cache_dangling_refs.py failures on the earlier run were stale-base artifacts: the branch was cut from df5f139ba, where they fail, and main has since fixed them. Rebased onto f11dc5ac4 and they pass (241 passed locally).

The remaining Repo tests (CPU) failure is pre-existing on main and unrelated to this change:

FAILED tests/studio/test_chat_response_details_ui_contract.py::test_response_model_badge_is_user_configurable_and_rendered_once_per_message
AssertionError: assert 'Show response model' in '...SettingsSection...'

Verified by running that file on a pristine main worktree: 1 failed, 5 passed, same assertion. The setting the test requires is not in the settings component on main. Nothing in this PR touches Studio, the frontend, or the HF cache scan; it changes unsloth/trainer.py and unsloth/models/rl.py only.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 8c583141f4

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

Two tests asserted upstream facts that TRL 1.x has already changed, so they
failed hard on a version inside this PR's stated envelope:

- test_trl_still_writes_the_attribute_unconditionally asserted the literal is
  in trl/trainer/grpo_trainer.py. On 1.9.2 that file exists, so the
  "trl layout changed" escape does not fire, but the write is gone. It is a
  signal that this guard has become a no-op, not a requirement, so it skips.
- test_every_trl_trainer_that_writes_it_goes_through_the_wrapper rglobbed the
  whole package and reached trl/experimental/{sdft,ssd,sdpo}. Those do still
  write it, but each is preceded by `if hasattr(model, "warnings_issued")`,
  and none is exported from trl.trainer, so none needs wrapping. Only
  unguarded, non-experimental writers count now.

trl 1.9.2: 2 failed, 21 passed -> 21 passed, 2 skipped. trl 0.25.1: 28 passed,
unchanged.

Also corrected a factual claim in the docstring and the test header. Both said
UNSLOTH_COMPILE_DISABLE=1 removes the generated module and takes the guard
with it. It does not: measured with a fresh cache, that mode still writes
unsloth_compiled_cache/UnslothGRPOTrainer.py with the guard in it (10
occurrences) and trl.GRPOTrainer is still the generated class. The real gap
this wrapper closes is trainer generation FAILING, after which unsloth falls
back to trl's own class and the write is unguarded again, which is the other
half of this PR.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 5, 2026
@danielhanchen
danielhanchen merged commit f6f3b03 into unslothai:main Aug 5, 2026
4 of 47 checks passed
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