Repository navigation
Restore warnings_issued for trl trainers on transformers 5.1+ - #7854
danielhanchen merged 4 commits into
Conversation
|
@codex review |
There was a problem hiding this comment.
💡 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".
| # Reconstruct kwargs for Trainer | ||
| kwargs = trainer_kwargs | ||
| kwargs["args"] = config | ||
| _ensure_warnings_issued(args[0] if args else kwargs.get("model")) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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).
b5eaf33 to
258b794
Compare
|
CI note. The two The remaining Verified by running that file on a pristine |
|
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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.
warnings_issuedtransformers set
self.warnings_issued = {}inPreTrainedModel.__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__withso on transformers 5.1+ the trainer cannot be constructed at all:
models/rl.pyalready guards this, but only inside the source it generates, so the guard exists exactly when compilation does.UNSLOTH_COMPILE_DISABLE=1takes 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-Sudokuon an L4 withUNSLOTH_COMPILE_DISABLE=1now trains, loss 0.617 to 0.002.A silent fallback made visible
_patch_trl_rl_trainersswallowed generation failures atlogger.infolevel. 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'scompute_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_onceand 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.