Skip to content

GRPO packing leak probe: restore each submodule's own training flag - #18438

Merged
danielhanchen merged 2 commits into
mainfrom
kp-leak-probe-eval
Nov 19, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
kp-leak-probe-eval

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

The GRPO sequence packing leak probe from #18335 now restores each submodule's own train / eval mode. Before, it could leave a deliberately eval-mode submodule in training mode for the rest of the run, turning its dropout back on.

Root cause

_unsloth_grpo_packing_leaks (unsloth/models/rl_replacements.py) recorded only unwrapped.training, called the recursive unwrapped.eval() for its two no-grad forwards, and afterwards called the recursive unwrapped.train(). With a training model whose block was kept in eval mode on purpose (for example a frozen block whose dropout must stay off), that block came back in training mode: before (parent=True, block=False), after (True, True). The probe runs once per model when GRPO packing is considered, so the change persists through training.

Fix

Record (module, module.training) for every submodule before eval() and restore exactly those flags in the finally block. There is no change to the probe's forwards, threshold or result.

Tests

tests/test_grpo_packing_leak_probe.py::test_probe_restores_every_submodule_mode[opt, llama] keeps the first decoder block of a training tiny OPT / Llama in eval mode and checks that every submodule's mode is the same after the probe.

Arm Result (test_grpo_packing_leak_probe.py + test_grpo_packing_boundary_blind.py)
main (8ef13a7a83) with the new test 2 failed (both new cases), 15 passed
this branch 17 passed

CPU, torch 2.14.1, transformers 4.57.6; the test loads each arm's own rl_replacements.py.

Not proven here

An end-to-end GRPO run with a deliberately eval-mode submodule.

The probe from #18335 saved only the root module's training flag, called
eval() and then a recursive train(), which also switched submodules kept in
eval mode on purpose (a frozen block whose dropout must stay off) back to
training for the rest of the run. Save and restore each submodule's own flag.
@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-19T09:19:56.912873Z 40ac70c 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 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: 51eb6aa421

ℹ️ 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/models/rl_replacements.py Resolved
Setting module.training directly skipped Int4PackedLinear.train(), which
also syncs quant_state.training, so after the probe's eval() a packed INT4
layer kept its quantization state in inference mode while training.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 40ac70c043

ℹ️ 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 bca4371 into main Nov 19, 2026
@danielhanchen
danielhanchen deleted the kp-leak-probe-eval 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