Skip to content

[model] fix: import AutoModelForCausalLMWithValueHead from trl.experimental.ppo in utils/model.py - #7625

Merged
wuxibin89 merged 1 commit into
verl-project:mainfrom
LiRunGuo:fix/trl-valuehead-import-in-model-py
Aug 31, 2026
Merged

wuxibin89 merged 1 commit into
verl-project:mainfrom
LiRunGuo:fix/trl-valuehead-import-in-model-py

Conversation

@LiRunGuo

@LiRunGuo LiRunGuo commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #5690

Motivation

Fixes the remaining gap of #5690
(verl cannot import name 'AutoModelForCausalLMWithValueHead' from 'trl').

PR #6539 fixed the import for
trl >= 0.29 only in verl/models/transformers/monkey_patch.py, but the same
top-level import still exists in two places in verl/utils/model.py, which
are hit when the FSDP engine loads a value-head critic
(verl/workers/engine/fsdp/transformer_impl.py:272 -> load_valuehead_model):

  • verl/utils/model.py:611 (patch_valuehead_model)
  • verl/utils/model.py:657 (load_valuehead_model)

With trl >= 0.29 (e.g. the latest trl==1.12.0, which removed the top-level
class in huggingface/trl#5044), PPO/RLHF training with a value-head critic on
the FSDP engine crashes with ImportError. This also blocks trl dependency
bumps (see closed dependabot PR #7478).

Verified on trl==1.12.0:

>>> from trl import AutoModelForCausalLMWithValueHead
ImportError: cannot import name 'AutoModelForCausalLMWithValueHead' from 'trl'
>>> from trl.experimental.ppo import AutoModelForCausalLMWithValueHead
OK  # trl.experimental.ppo.modeling_value_head.AutoModelForCausalLMWithValueHead

Change

Mirror the existing pattern in verl/models/transformers/monkey_patch.py:351-353
at both sites in verl/utils/model.py:

try:
    from trl.experimental.ppo import AutoModelForCausalLMWithValueHead  # type: ignore
except ImportError:
    from trl import AutoModelForCausalLMWithValueHead  # type: ignore
  • trl >= 0.29 (experimental path exists): uses trl.experimental.ppo.
  • trl < 0.29 (e.g. the fsdp/megatron extra pin trl==0.27.0): experimental
    import fails and falls back to the top-level import — unchanged behavior.

Why this is not duplicating an existing PR

Checked gh pr list --repo verl-project/verl --state open --search "trl" and
--search "valuehead OR value_head OR AutoModelForCausalLMWithValueHead":
no open PR touches verl/utils/model.py imports. #6539 (merged) covered only
monkey_patch.py; this PR completes the same fix for the FSDP valuehead path
that #6539 missed.

Tests

  • pip install trl==1.12.0 in a scratch env; both import paths verified as
    above (top-level fails, trl.experimental.ppo works).
  • Existing CPU unit tests unaffected:
    python -m pytest tests/workers/rollout/test_chunked_prefill_batched_tokens_on_cpu.py -v (6 passed)
    and the wider CPU suite (cpu_tests_tf510.log: 613 passed) were used as a
    regression baseline on this machine.
  • (Optional, pending reviewer preference) a unit test asserting the import
    resolves under both trl layouts via sys.modules stubbing.

AI assistance disclosure

Prepared with AI assistance (draft + verification). The submitting human has
reviewed the design, will review every changed line, and has run the
verification commands above before submitting.

…mental.ppo in utils/model.py

Completes the trl>=0.29 import fix for the value-head critic path: verl-project#6539 only
covered verl/models/transformers/monkey_patch.py, while the two remaining
top-level imports in verl/utils/model.py (patch_valuehead_model at :611 and
load_valuehead_model at :657, used by the FSDP engine when loading a
value-head critic) still crash with trl>=0.29/1.x, where the class was moved
to trl.experimental.ppo (huggingface/trl#5044). Verified on trl 1.12.0:
top-level import raises ImportError, trl.experimental.ppo import works.

Mirror the existing monkey_patch try/except pattern at both sites; with
trl<0.29 (e.g. the fsdp/megatron extra pin trl==0.27.0) the experimental
import fails and falls back to the top-level import, preserving current
behavior.

Co-authored-by: DeepSeek Harness Agent
@wuxibin89
wuxibin89 merged commit 9d4d2d0 into verl-project:main Aug 31, 2026
ETOgaosion added a commit to ETOgaosion/verl that referenced this pull request Aug 31, 2026
verl-project#7625 added an import block missing the blank line after the
`from transformers import PreTrainedModel` import, which fails
`ruff format` in the pre-commit `--all-files` CI run.
thecourageyu pushed a commit to thecourageyu/verl that referenced this pull request Nov 11, 2026
…mental.ppo in utils/model.py (verl-project#7625)

Fixes verl-project#5690

## Motivation

Fixes the remaining gap of
[verl-project#5690](verl-project#5690)
(`verl cannot import name 'AutoModelForCausalLMWithValueHead' from
'trl'`).

PR [verl-project#6539](verl-project#6539) fixed the
import for
`trl >= 0.29` only in `verl/models/transformers/monkey_patch.py`, but
the same
top-level import still exists in **two places** in
`verl/utils/model.py`, which
are hit when the FSDP engine loads a value-head critic
(`verl/workers/engine/fsdp/transformer_impl.py:272` ->
`load_valuehead_model`):

- `verl/utils/model.py:611` (`patch_valuehead_model`)
- `verl/utils/model.py:657` (`load_valuehead_model`)

With `trl >= 0.29` (e.g. the latest `trl==1.12.0`, which removed the
top-level
class in huggingface/trl#5044), PPO/RLHF training with a value-head
critic on
the FSDP engine crashes with `ImportError`. This also blocks trl
dependency
bumps (see closed dependabot PR verl-project#7478).

Verified on `trl==1.12.0`:

```
>>> from trl import AutoModelForCausalLMWithValueHead
ImportError: cannot import name 'AutoModelForCausalLMWithValueHead' from 'trl'
>>> from trl.experimental.ppo import AutoModelForCausalLMWithValueHead
OK  # trl.experimental.ppo.modeling_value_head.AutoModelForCausalLMWithValueHead
```

## Change

Mirror the existing pattern in
`verl/models/transformers/monkey_patch.py:351-353`
at both sites in `verl/utils/model.py`:

```python
try:
    from trl.experimental.ppo import AutoModelForCausalLMWithValueHead  # type: ignore
except ImportError:
    from trl import AutoModelForCausalLMWithValueHead  # type: ignore
```

- `trl >= 0.29` (experimental path exists): uses `trl.experimental.ppo`.
- `trl < 0.29` (e.g. the fsdp/megatron extra pin `trl==0.27.0`):
experimental
import fails and falls back to the top-level import — unchanged
behavior.

## Why this is not duplicating an existing PR

Checked `gh pr list --repo verl-project/verl --state open --search
"trl"` and
`--search "valuehead OR value_head OR
AutoModelForCausalLMWithValueHead"`:
no open PR touches `verl/utils/model.py` imports. verl-project#6539 (merged) covered
only
`monkey_patch.py`; this PR completes the same fix for the FSDP valuehead
path
that verl-project#6539 missed.

## Tests

- `pip install trl==1.12.0` in a scratch env; both import paths verified
as
  above (top-level fails, `trl.experimental.ppo` works).
- Existing CPU unit tests unaffected:
`python -m pytest
tests/workers/rollout/test_chunked_prefill_batched_tokens_on_cpu.py -v`
(6 passed)
and the wider CPU suite (`cpu_tests_tf510.log`: 613 passed) were used as
a
  regression baseline on this machine.
- (Optional, pending reviewer preference) a unit test asserting the
import
  resolves under both trl layouts via `sys.modules` stubbing.

## AI assistance disclosure

Prepared with AI assistance (draft + verification). The submitting human
has
reviewed the design, will review every changed line, and has run the
verification commands above before submitting.

Co-authored-by: LiRunGuo <[email protected]>
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.

verl cannot import name 'AutoModelForCausalLMWithValueHead' from 'trl'

3 participants