Skip to content

GRPO: one vLLM rollout scope per tool-calling generate - #18452

Merged
danielhanchen merged 1 commit into
mainfrom
grpo-tool-rollout-scope
Nov 19, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
grpo-tool-rollout-scope

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Runs TRL's GRPO _generate inside unsloth-zoo's new vllm_rollout_scope, so a multi-turn tool rollout folds the adapter and resets the prefix cache once per rollout instead of once per vLLM call (companion PR: unslothai/unsloth-zoo#2610). Engaged only when tools are set, vLLM shares the training weights, and there is no custom rollout_func (one could run the training model while the weights are folded). UNSLOTH_VLLM_ROLLOUT_SCOPE=0 turns it off; with an older unsloth-zoo it does nothing.

Tests

8 tests (2 fail on main, 8 pass here), including the wrapper applied to real TRL 0.24.0 and 1.18.0 _generate source; the generated UnslothGRPOTrainer.py compiles. Correctness and Colab T4 numbers (-7.0% merged rollout, -6.9% vLLM LoRA on a 4-turn tool loop) are in the zoo PR.

Not covered

No full GRPOTrainer run with tools on a GPU.

TRL's tool loop calls the engine once per turn with unchanged weights,
and Unsloth hands every call a fresh load_lora, so each turn folded and
restored the adapter and dropped vLLM's prefix cache. Run _generate
inside unsloth_zoo's vllm_rollout_scope when tools are set, vLLM shares
the training weights and no custom rollout_func runs. Off with
UNSLOTH_VLLM_ROLLOUT_SCOPE=0 and a no-op on an older unsloth_zoo.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-19T10:06:47.111091Z 1aaba26 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 review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 1aaba26874

ℹ️ 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 8dac30f into main Nov 19, 2026
@danielhanchen
danielhanchen deleted the grpo-tool-rollout-scope branch November 19, 2026 12:26
oobabooga pushed a commit to oobabooga/unsloth that referenced this pull request Nov 20, 2026
…ild mode (unslothai#18634)

Follow-up fixes for unslothai#18452, unslothai#18438, found while re-checking the PRs
merged on 2026-11-19. One commit per fix; every fix has a test that
fails on main and passes here.

| Fix | Follows up | Severity |
|---|---|---|
| GRPO rollout scope: off when a tool is async (deadlock) | unslothai#18452 | P1
|
| GRPO packing leak probe: a child shared by two parents | unslothai#18438 | P2 |

## Tests (this branch vs main `54af93780b`, CPU)

- New and changed tests: main 3 failed / 20 passed, this branch 23
passed. Each test loads the code of its own tree.
- Neighbouring suites on this branch: rollout scope, packing probe and
boundary leak suites: 44 passed, 2 skipped.
- `prepush_gate.py` passes.

## GRPO rollout scope: off when a tool is async (deadlock) (follows up
unslothai#18452)

#### What broke

unslothai#18452 wraps TRL's tool-calling `_generate` in `vllm_rollout_scope` by
default (zoo unslothai#2610). The scope keeps the merged-rollout fold entered,
and with it the process-wide `_MERGED_ROLLOUT_LOCK`, until `_generate`
returns. TRL 1.18 runs async tools on its event-loop thread through
`asyncio.run_coroutine_threadsafe(...).result()`
(`trl/trainer/grpo_trainer.py:2128`) while the main thread waits. An
async tool that calls a vLLM engine wrapped for merged rollouts needs
that lock, and the main thread holding it is waiting on the tool. So the
rollout hangs with no error. This applies both to the policy's own
engine (an agent that asks the policy a sub-question) and to another
engine.

#### Repro (CPU, zoo merged-rollout test doubles, TRL-style async tool
on a separate event-loop thread, 2 s timeout)

- main: `DEADLOCK` (the async tool blocks on the lock held by the scope)
- this branch: completes, `['out']`

#### Fix

`_unsloth_rollout_scope` returns a null context when any tool is async,
checking both TRL's per-batch `_async_tool_dicts` and
`inspect.iscoroutinefunction` over `trainer.tools`. Each engine call
then folds and restores on its own, as before unslothai#18452. Rollouts with sync
tools only keep the scope.

#### Flip test

`tests/test_grpo_rollout_scope.py`:

- `test_scope_stays_off_with_an_async_tool[tools|per_batch_dicts]`: main
2 failed, branch passed
- `test_scope_still_on_with_sync_tools_only` passes on both
- File total on the branch: 11 passed

## GRPO packing leak probe: a child shared by two parents (follows up
unslothai#18438)

#### What broke

The GRPO packing leak probe (unslothai#18438) saves each module's mode and
restores it through `module.train(mode)` in `modules()` order.
`modules()` yields a child shared by two parents only once. So when the
later parent was in training mode, its recursive `train(True)` switched
the shared child back on after the child's own restore had run. Example:
one `Dropout` shared by two decoder blocks and kept in eval mode came
back from the probe with `training == True`, so its dropout stayed
active for the rest of training.

#### Fix

After each module's own `train(mode)` runs (this keeps side effects such
as `Int4PackedLinear` syncing its quant state), every saved flag is set
directly once more.

#### Flip test


`tests/test_grpo_packing_leak_probe.py::test_probe_restores_a_child_shared_by_two_parents`
(tiny Llama on CPU, one Dropout shared by layers 0 and 1):

- main: failed
- this branch: passed
- The full leak-probe and boundary-blind files pass: 19 passed.

## Not proven here

A real GRPO run with an async tool calling fast_generate (vLLM on GPU);
the deadlock is shown with the real scope and lock on CPU.
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