Fix GRPO tool loop handling for image-returning tools#6290
Open
vieveks wants to merge 1 commit into
Open
Conversation
Author
|
Additional smoke test on a local RTX 5070 Ti: env -u PYTHONPATH \
PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 \
PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True \
MODEL_ID=Qwen/Qwen3.5-2B \
/home/vieveks/miniconda3/envs/trl-dev/bin/python \
/home/vieveks/Vivek/opensource_contri/trl_real_vlm_tool_image_smoke.pyResult: The smoke uses the local checkout by prepending |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
This PR fixes multimodal tool responses in
GRPOTrainerwhen a tool returns images for a VLM.Previously, image blocks returned by tools were kept inside
role="tool"messages. Some VLM chat templates render image placeholders only forrole="user"messages, so the image tensors could be collected while the tokenized prompt contained no matching image tokens. That can lead to image feature/token mismatches.This PR splits multimodal tool results so text remains in the tool message, while returned image blocks are surfaced as synthetic user messages for prompt rendering. The synthetic user image messages are kept out of
completionsso reward tokenizers do not receive structured image blocks. It also makes tool suffix token extraction robust to suffixes that include non-tool messages and templates with assistant-final scaffolding.Fixes #5663.
Tests
python3 -m py_compile trl/trainer/grpo_trainer.py tests/test_grpo_trainer.pyenv -u PYTHONPATH PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 /home/vieveks/miniconda3/envs/trl-dev/bin/pytest tests/test_grpo_trainer.py::TestGRPOTrainerVLM::test_tool_suffix_ids_include_user_image_from_tool_response tests/test_grpo_trainer.py::TestGRPOTrainerVLM::test_train_with_tools_multimodal_response -qenv -u PYTHONPATH PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 /home/vieveks/miniconda3/envs/trl-dev/bin/pytest tests/test_grpo_trainer.py::TestGRPOTrainer::test_train_with_tools -qenv -u PYTHONPATH PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 /home/vieveks/miniconda3/envs/trl-dev/bin/pytest tests/test_grpo_trainer.py::TestGRPOTrainer::test_train_with_environment_factory -qenv -u PYTHONPATH PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 /home/vieveks/miniconda3/envs/trl-dev/bin/pytest tests/test_grpo_trainer.py::TestGRPOTrainer::test_train_with_malformed_tool_calls -qAlso ran
tests/test_grpo_trainer.py -q -k "tool or vlm"; interrupted during a slow HF Hub download after 18 passed, 3 skipped, no failures observed.Before submitting
AI writing disclosure
Note
Medium Risk
Changes GRPO tool-loop tokenization and message shaping on a critical VLM path; misalignment could still break training or image feature/token matching, though scoped to tool follow-ups with new tests.
Overview
Fixes GRPOTrainer multimodal tool handling when tools return images: many VLM chat templates only emit image placeholder tokens on
usermessages, so keeping image blocks insidetoolmessages could collect image tensors without matching tokens in the prompt.The tool loop now splits list-shaped tool results—text stays in the tool message; image blocks are appended as a synthetic user message on the prompt transcript only (not in
completions, so reward tokenizers avoid structured image blocks)._get_tool_suffix_idspicks the first tool role’s name (nottool_messages[0]), falls back to suffix alignment when prefix slicing fails (templates that drop assistant scaffolding after tool/user follow-ups), and suffix extraction walks the prompt transcript for trailingtoolandusermessages instead of only tool rows fromcompletions.Adds
test_tool_suffix_ids_include_user_image_from_tool_responseto assert image pad tokens appear in suffix IDs when a synthetic user image follows a tool message.Reviewed by Cursor Bugbot for commit 985de76. Bugbot is set up for automated code reviews on this repo. Configure here.