Repository navigation
Hand the LoRA to vLLM on TRL 1.x GRPO rollouts - #8701
Conversation
TRL 1.10.0 dropped the collective_rpc("reload_weights") call from
VLLMGeneration.generate, so the regex anchored on it matched nothing and
raised. The lora_request injection ran after that anchor in the same
function, so it never happened, and _init_vllm/sync_weights had already
been installed by then. fast_inference=True GRPO therefore sampled every
rollout from the base model with the adapter ignored, and reported finite
losses while doing it.
Wrap VLLMGeneration.generate instead of rewriting its source, and
intercept on the vLLM engine (generate/chat/collective_rpc), which is the
part Unsloth owns. Restore all three methods if any patch fails, so the
half-patched state is unreachable, and wake a sleeping engine before
sync_weights returns early.
Two metadata caps, and both have to move together or neither does anything. trl<=0.24.0 is metadata only. Nothing in unsloth or unsloth_zoo reads the TRL version and refuses 1.x at runtime; the only comparisons are floors at trainer.py:725 and :993. Measured against real TRL 1.10.0 with the trees on PYTHONPATH so no resolver ever sees the cap: import ok, tests/ -k grpo 991 passed, tests/version_compat 1702 passed, and four real 3-step GRPO runs whose grad-norms and reward stats match TRL 0.22.2 to every digit. The 5-test gap against 0.22.2 is this repo's own TRL >= 1.7.0 gated assertions, which 0.22.2 skips and 1.10.0 runs and passes. datasets<4.4.0 is the second cap, and raising only the TRL one ships a pin no user exercises: with the datasets cap in place, a resolver asked for trl<=1.10.0 settles on trl==0.29.1, because TRL 1.10.0 requires datasets>=4.7.0. So the datasets cap has to open at the same time. It opens as a hole rather than a floor bump. A datasets>=4.7.0 floor would drop Python 3.9 and force 4.x on TRL 0.22.2 users who do not need it. The hole !=4.4.*,!=4.5.0 is exactly what patch_datasets in import_fixes.py already refuses at runtime over the RLock recursion bug, so metadata and the runtime guard now agree instead of the metadata being the stricter of the two. Verified on datasets 4.8.5 with TRL 1.10.0: 991 / 1702 passed, exit 0 both, and the GRPO smoke numbers are identical to the datasets 3.6.0 run to every digit.
… shape
The failure this locks down is silent: with TRL 1.10.0's generate, the old
source patch anchored on collective_rpc("reload_weights") matched nothing,
raised, and took the lora_request injection down with it, so fast_inference
GRPO sampled every rollout from the base model at exit 0.
The tests are shape-driven rather than version-driven. A synthetic
trl.generation.vllm_generation goes into sys.modules with a VLLMGeneration
whose _init_vllm and sync_weights carry the anchors the source patches need,
and whose generate reaches a fake engine the way a given TRL era reaches it:
sync_weights on 1.10.0, collective_rpc on 0.22.2, chat for conversational
rollouts, and no engine at all in server mode. Then the real
vllm_generation_init_patch runs over it and the fake engine records what
arrived. No vLLM, no GPU, no network, and importlib_version is monkeypatched
so the installed TRL is irrelevant - these pass against the 0.25.1 in this
env, which the patch would otherwise skip outright.
Also covered: the engine methods are restored after the call so a later
model.fast_generate is clean, an engine that already carried its own generate
gets it back rather than deleted, a TRL-created engine is left alone, a
sleeping engine is woken before sync_weights returns early, a failed sibling
patch rolls all three methods back, and patching twice does not double wrap.
Seven of the ten fail on c160215.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b87e071ee7
ℹ️ 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".
| "protobuf", | ||
| "sentencepiece>=0.2.0", | ||
| "datasets>=3.4.1,!=4.0.*,!=4.1.0,<4.4.0", | ||
| "datasets>=3.4.1,!=4.0.*,!=4.1.0,!=4.4.*,!=4.5.0,<5.0.0", |
There was a problem hiding this comment.
Bump the unsloth-zoo floor with these dependency caps
When upgrading an environment that already has the permitted unsloth_zoo==2026.8.10, these widened constraints remain intersected with that release's metadata (trl<=0.24.0 and datasets<4.4.0). Since the dependency at line 133 still considers 2026.8.10 satisfactory, ordinary upgrades can silently retain the old TRL/datasets versions, while explicitly selecting TRL 1.x makes the environment inconsistent. Raise the zoo floor to the first release carrying the matching caps.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
The analysis is right, and it is measured rather than argued. With the widened caps from this
PR plus the newest zoo release, the resolver still lands on the old versions:
$ cat req.txt
unsloth_zoo==2026.8.11
trl>=0.18.2,!=0.19.0,<=1.10.0
datasets>=3.4.1,!=4.0.*,!=4.1.0,!=4.4.*,!=4.5.0,<5.0.0
$ uv pip compile req.txt
datasets==4.3.0
trl==0.24.0
unsloth-zoo==2026.8.11
and forcing TRL 1.x is not merely inconsistent, it is unsatisfiable:
$ uv pip compile # unsloth_zoo>=2026.8.10, trl==1.10.0
Because unsloth-zoo>=2026.8.10 depends on one of:
datasets>=3.4.1,<4.0.dev0
datasets>=4.1.dev0,<4.1.0
datasets>4.1.0,<4.4.0
and trl>=1.10.0 depends on datasets>=4.7.0, we can conclude that
trl>=1.10.0 and unsloth-zoo>=2026.8.10 are incompatible.
The remedy cannot be applied here yet. There is no zoo release carrying the matching caps to
raise the floor to: 2026.8.11, published today, is the newest, and its metadata is still
trl!=0.19.0,<=0.24.0,>=0.18.2 and datasets!=4.0.*,!=4.1.0,<4.4.0,>=3.4.1. A floor naming a
version that does not exist would make pip install unsloth unresolvable the moment this
merged.
That coupling is why this PR is gated on unsloth-zoo #1060, which opens that datasets cap
and, since a moment ago, the trl<=0.24.0 one beside it: opening datasets alone left zoo
still capping TRL at 0.24.0, and zoo's cap binds whatever this file asks for. The floor bump lands with the zoo release that carries it, and it is needed:
an environment already holding 2026.8.11 satisfies unsloth_zoo>=2026.8.10, so without the
bump pip has no reason to upgrade and TRL stays at 0.24.0.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b87e071ee7
ℹ️ 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".
| "protobuf", | ||
| "sentencepiece>=0.2.0", | ||
| "datasets>=3.4.1,!=4.0.*,!=4.1.0,<4.4.0", | ||
| "datasets>=3.4.1,!=4.0.*,!=4.1.0,!=4.4.*,!=4.5.0,<5.0.0", |
There was a problem hiding this comment.
Bump the unsloth-zoo floor with these dependency caps
When upgrading an environment that already has the permitted unsloth_zoo==2026.8.10, these widened constraints remain intersected with that release's metadata (trl<=0.24.0 and datasets<4.4.0). Since the dependency at line 133 still considers 2026.8.10 satisfactory, ordinary upgrades can silently retain the old TRL/datasets versions, while explicitly selecting TRL 1.x makes the environment inconsistent. Raise the zoo floor to the first release carrying the matching caps.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
The analysis is right, and it is measured rather than argued. With the widened caps from this
PR plus the newest zoo release, the resolver still lands on the old versions:
$ cat req.txt
unsloth_zoo==2026.8.11
trl>=0.18.2,!=0.19.0,<=1.10.0
datasets>=3.4.1,!=4.0.*,!=4.1.0,!=4.4.*,!=4.5.0,<5.0.0
$ uv pip compile req.txt
datasets==4.3.0
trl==0.24.0
unsloth-zoo==2026.8.11
and forcing TRL 1.x is not merely inconsistent, it is unsatisfiable:
$ uv pip compile # unsloth_zoo>=2026.8.10, trl==1.10.0
Because unsloth-zoo>=2026.8.10 depends on one of:
datasets>=3.4.1,<4.0.dev0
datasets>=4.1.dev0,<4.1.0
datasets>4.1.0,<4.4.0
and trl>=1.10.0 depends on datasets>=4.7.0, we can conclude that
trl>=1.10.0 and unsloth-zoo>=2026.8.10 are incompatible.
The remedy cannot be applied here yet. There is no zoo release carrying the matching caps to
raise the floor to: 2026.8.11, published today, is the newest, and its metadata is still
trl!=0.19.0,<=0.24.0,>=0.18.2 and datasets!=4.0.*,!=4.1.0,<4.4.0,>=3.4.1. A floor naming a
version that does not exist would make pip install unsloth unresolvable the moment this
merged.
That coupling is why this PR is gated on unsloth-zoo #1060, which opens that datasets cap
and, since a moment ago, the trl<=0.24.0 one beside it: opening datasets alone left zoo
still capping TRL at 0.24.0, and zoo's cap binds whatever this file asks for. The floor bump lands with the zoo release that carries it, and it is needed:
an environment already holding 2026.8.11 satisfies unsloth_zoo>=2026.8.10, so without the
bump pip has no reason to upgrade and TRL stays at 0.24.0.
|
The six failing checks here are not from this branch. zoo #1057 landed earlier today and added One module failing to import ends the job at exit 2, so the Core and Python-version legs go red on every open pull request here regardless of what it changed. main looks green only because its last run predates that merge. Fix is unslothai/unsloth-zoo#1061. Once it lands these will need a re-run. |
|
@codex review |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. 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". |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 66521061b1
ℹ️ 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".
| return | ||
| had_own = name in getattr(llm, "__dict__", {}) | ||
| try: | ||
| setattr(llm, name, make_replacement(bound)) |
There was a problem hiding this comment.
Avoid mutating shared engine methods across calls
When another thread calls model.fast_generate or a second VLLMGeneration.generate while a rollout is active, this instance-level setattr replaces methods on the shared engine for both callers, so the unrelated generation can receive the training LoRA. Overlapping wrappers also save one another as the prior value, allowing out-of-order finally blocks to delete an active wrapper or leave one installed afterward; use call-local argument injection or serialize the override rather than mutating the shared llm object.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not a reachable path, so leaving this as is.
VLLMGeneration.generate is only ever called synchronously from the training loop: grpo_trainer.py:1823 inside _generate_single_turn, and the same shape in rloo_trainer.py:1223, distillation_trainer.py:995 and the experimental gold/sdft/sdpo/ssd/iw_opd trainers. There is no Thread(, ThreadPoolExecutor or run_in_executor in trl/trainer/grpo_trainer.py or trl/generation/vllm_generation.py. The one background thread in TRL (trainer/utils.py) runs async reward and tool functions, which execute after generation has returned with the main thread blocked on .result(), and the async GRPO trainer is server mode only and never touches this llm object. The wrapper is additionally gated on shared_weights, i.e. only the colocate engine Unsloth handed to TRL.
model.fast_generate cannot see the override at all: vision.py:1499 and llama.py:2811 do model.fast_generate = model.vllm_engine.generate, which binds the class method once at load time, before any instance attribute exists. A later setattr(llm, "generate", wrapper) is therefore invisible to it, so even a hypothetical concurrent fast_generate call cannot pick up the training adapter.
That leaves user hand rolled threads around the trainer as the only way in, which is out of scope here. Restoration for the single threaded case is covered by test_engine_is_restored_after_the_call.
| "if not (getattr(getattr(self, 'llm', None), 'shared_weights', False) or getattr(self, 'unsloth_fast_inference_lora', False)):", | ||
| "generate", | ||
| ) | ||
| generate_patched = install_generate_wrapper() |
There was a problem hiding this comment.
Roll back when a patch helper returns false
When a method is unavailable to inspect.getsource—for example because another integration has already replaced sync_weights—patch_vllm_generation_method explicitly returns False rather than raising, but this result is ignored and the remaining methods are still patched. In the sync_weights case, _init_vllm has already been changed to install the shared engine and generate is then wrapped, while upstream weight synchronization remains active, defeating the stated all-or-nothing guarantee and potentially reloading or pushing weights through a path meant for a TRL-owned engine; treat any false result as a failure and restore the snapshot.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
The half patched state is real as a code property, but it is unreachable on any TRL this package accepts, so I am leaving the guard off.
patch_vllm_generation_method only returns False in two cases: the method is missing from VLLMGeneration, or inspect.getsource raises. Every version in the supported window (trl>=0.18.2,!=0.19.0,<=1.10.0, with the shim further gated on >=0.28.0) defines _init_vllm(self), sync_weights(self) and generate(...) as ordinary source backed methods, and nothing in Unsloth replaces them: the only other reference to VLLMGeneration is the dispatcher in rl.py:4275, and re-entry into this patch is safe because the exec'd replacements are cached with mtime=None, which linecache.checkcache skips, so getsource keeps working (test_patching_twice_does_not_double_wrap).
The drift that actually happens, TRL changing a method body or signature, is the regex path, and that already raises RuntimeError and restores all three from the snapshot, which is what test_failed_patch_rolls_all_three_methods_back exercises. A future TRL that renames sync_weights outright is what the <=1.10.0 cap is there to catch.
The cap raise is what moves anyone onto TRL 1.x, and that is a separate decision from fixing the silent rollout bug. Split out so this PR changes no resolved version for anyone: the pins go back to trl<=0.24.0 and datasets<4.4.0 exactly as main has them. What is left is inert on the TRL this repo installs. vllm_generation_init_patch returns at rl_replacements.py before touching anything unless the installed TRL is 0.28.0 or newer, and TRL 0.22.2 has no trl.generation package at all. It only does something for someone who has installed a newer TRL themselves, which is exactly who hits the bug. The tests build a synthetic trl module rather than importing the real one, so they run on any installed version: 10 passed here against TRL 0.25.1. The cap raise, with the datasets hole it needs to be reachable at all, moves to its own PR so it can be judged on its own evidence.
|
@codex review |
|
Codex Review: Didn't find any major issues. 👍 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". |
…ng over a positional adapter Checked LLM.generate, LLM.chat, LLM.collective_rpc, LLM.wake_up and LLM.sleep on every vLLM release from 0.11.0 to 0.27.1. All are present at each one, LLM has no __slots__ and no __setattr__/__getattr__ hook, and none of the methods are decorated, so the instance-level override always takes. One real gap turned up. lora_request is keyword-only on generate at every version, but on chat it is an ordinary positional parameter whose index has already moved once (tokenization_kwargs landed in 0.18.0). A TRL that fills it positionally plus an injector that adds it as a keyword is "got multiple values for argument", not a missing adapter. Bind the callable's real signature rather than counting arguments, so a future reshuffle cannot reintroduce it. A keyword lora_request = None is still overridden: on a shared-weights engine that means base-model rollouts, which is the bug this patch exists to fix. Three tests: a signature-accurate engine carrying vLLM 0.27.1's parameter lists on both entry points, the positional-chat case, and the explicit-None case. The positional one fails with TypeError without this change. 13 passed.
|
@codex review |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! 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". |
The bug
fast_inference = TrueGRPO on TRL 1.10.0 samples every rollout from the base model, with the LoRA adapter ignored and no error raised. Losses are finite, reward std is plausible, exit code is 0. The only signal is one line:vllm_generation_init_patchsource-patchesVLLMGeneration.generatewith two regexes: one anchored onself.llm.collective_rpc("reload_weights"), one injectinglora_request=intoself.llm.generate(...). TRL removed thatcollective_rpccall fromgeneratein huggingface/trl#5313 and callsself.sync_weights()instead, citing vllm-project/vllm#29341.grep -n collective_rpcon TRL 1.10.0'strl/generation/vllm_generation.pyreturns nothing.The failure is worse than one missed transform:
lora_requestinjection after it in the same function never runs.try/exceptthat only logs, and_init_vllm/sync_weightswere already installed by then.So weights are never synced into vLLM and the adapter is never attached.
That change missed the 1.9.2 cut and first shipped in 1.10.0, released 2026-08-13. Checked against all 19 TRL releases that ship the file: the old anchor matches everywhere except 1.10.0.
Measured
Qwen2.5-0.5B + LoRA, with
lora_Bre-initialised to N(0, 0.02) so base and LoRA outputs actually differ, countinglora_requeston real vLLM calls:lora_requestpassedThe fix
Stop source-patching
generate. Wrap it, and intercept on the vLLM engine (llm.generate,llm.chat,llm.collective_rpc) for the dynamic extent of one call, restoring in afinally.self.llmis the vLLMLLMobject in colocate mode in every TRL release that hasVLLMGeneration, and those three are public vLLM APIs, checked against every vLLM release from 0.11.0 to 0.27.1. TRL's method body is free to keep moving; whatever shape it takes, it has to reach the engine through them. The oldlora_requestregex(self\.llm\.generate\([^\)]+)\)was not paren-balanced either, so it mis-edited any call with nested parentheses or spanning lines.Also in this commit:
sync_weightsreturns early, since TRL >= 1.x only wakes a sleeping engine inside that method andgeneratedelegates to it.Scoped to one
VLLMGeneration.generatecall, somodel.fast_generateand any other user of the same engine are unaffected. TRL 0.22.2 has notrl.generationpackage at all, so it cannot regress.After the fix, TRL 1.10.0 tracks 0.22.2 instead of diverging: step 1 identical to all 16 digits.
The pins are no longer here
The TRL and datasets cap raise moved to #8810, so this PR changes no resolved version for
anyone.
trl<=0.24.0anddatasets<4.4.0stay exactly as main has them.That leaves this fix inert on the TRL this repo installs:
vllm_generation_init_patchreturns at
rl_replacements.py:3248-3250unless the installed TRL is 0.28.0 or newer, andTRL 0.22.2 has no
trl.generationpackage at all. It only does something for someone whohas installed a newer TRL themselves, which is exactly who hits the bug.
The tests build a synthetic
trlmodule rather than importing the real one, so they run onany installed version: 13 passed against TRL 0.25.1.
vLLM coverage
Checked
vllm/entrypoints/llm.pyat 0.11.2, 0.15.1, 0.18.1, 0.20.2, 0.23.0, 0.25.1 and0.27.1 (current latest). At every one of them:
generate,chat,collective_rpc,wake_upandsleepall exist onLLM;generateandchatboth takelora_request;LLMhas no__slots__, no__setattr__/__getattr__hook and no decorated methods, sothe instance-level override this fix installs always takes. That last point matters, because
a failed override would fall through to exactly the silent base-model sampling being fixed.
One gap that check found and this PR closes:
lora_requestis keyword-only onLLM.generateat every version, but onLLM.chatit is an ordinary positional parameterwhose index has already moved once (
tokenization_kwargswas inserted at 0.18.0). A callerthat fills it positionally plus an injector that adds it as a keyword is
TypeError: got multiple values for argument, not a missing adapter. The wrapper now bindsthe callable's real signature rather than counting arguments, so a future reshuffle cannot
reintroduce it. A keyword
lora_request = Noneis still overridden on purpose: on ashared-weights engine that means base-model rollouts, which is the bug itself.
Not covered
Multi-rank / TP and TRL server mode; sleep mode is the least-evidenced part of the fix; TRL versions between 0.22.2 and 1.10.0 at runtime; Mac, Windows and WSL; transformers 5.x. The vLLM coverage above is a source-level API check across releases, not a GPU run on each: only 0.15.1 was executed.