报错维度问题prompt_ids = torch.cat([input.prompt_ids for input in inputs], dim=0)
背景:GRPO 脚本指定: response_len=4 max_prompt_length=$((1024 * 4)) max_response_length=$((1024 * response_len))
按理说overlong的prompt会被过滤,但是报错 output = self._postprocess(outputs, input_non_tensor_batch=batch.non_tensor_batch) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "verl/experimental/agent_loop/agent_loop.py", line 796, in _postprocess prompt_ids = torch.cat([input.prompt_ids for input in inputs], dim=0) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RuntimeError: Sizes of tensors must match except in dimension 0. Expected size 4096 but got size 4128 for tensor number 32 in the list.
This is a genuine bug in the overlong-prompt filtering logic for VLM (Vision-Language Model) batches.
Root cause analysis:
Your diagnosis is correct. The doc2len function in RLHFDataset.maybe_filter_out_long_prompts() tokenizes only the prompt text with the chat template applied, but it does not account for the tool schemas that are appended to the prompt during the agent loop rollout. When the actual rollout occurs, the agent loop appends additional tool definitions and special tokens (e.g. \u003ctools\u003e..., \u003cIMPORTANT\u003e...) to the prompt, which increases the actual token count beyond what doc2len measured.
This means overlong prompts can slip through the filter and reach the agent loop, where torch.cat fails because different samples have different amounts of tool-related token overhead, resulting in the dimension mismatch error (expected size 4096 but got 4128 for tensor #32).
Proposed fix:
Your code modification to doc2len to account for tool schemas is on the right track. However, since the agent loop in agent_loop.py uses apply_chat_template with tools=None and only later injects tool schemas at the rendering stage (line ~265), you should ensure that doc2len uses the same tool schema configuration as the agent loop.
More specifically, in verl/utils/dataset/rl_dataset.py, the doc2len function should:
- Pass the same
toolsparameter toapply_chat_templateas the agent loop does (or use the actualtool_schemasfrom the dataset config if tools are defined) - Include any additional system prompt content that the agent loop adds
Alternatively, a simpler fix would be to increase the max_prompt_length filter threshold by a fixed overhead (e.g. 512 tokens) to absorb the tool schema token cost, though this is less precise than your proposed fix.
Recommended implementation (pseudocode for doc2len):
def doc2len(doc) -> int:
messages = self._build_messages(doc)
# Apply same template logic as agent_loop.py
raw_prompt = apply_chat_template(
self.tokenizer,
messages,
tools=self.tool_schemas, # Must match what agent_loop uses
add_generation_prompt=True,
tokenize=True,
)
return len(normalize_token_ids(tokenized_prompt))
If tool_schemas is None in your dataset config, then the issue may also be that the agent loop dynamically adds tool schemas at runtime (from tool_config.yaml), which cannot be known at dataset filtering time. In that case, you may need to either:
- Pre-specify the tool schemas in the dataset config
- Use a conservative length budget (max_prompt_length + overhead)
This is a real bug that should be addressed in the codebase. Thank you for the detailed analysis.