verl icon indicating copy to clipboard operation
verl copied to clipboard

[Question] Video input causes IndexError in MRoPE position calculation with Qwen3-VL + vLLM

Open Solus-sano opened this issue 10 months ago • 8 comments

[Question] Video input causes IndexError in MRoPE position calculation with Qwen3-VL + vLLM

Environment

  • vLLM version: 0.11.0
  • Model: Qwen3-VL-8B-Instruct
  • Framework: verl (GRPO trainer with agent loop)
  • Video input: 30 frames per clip

Problem Description

I'm encountering an IndexError: list index out of range when processing video inputs with Qwen3-VL model using vLLM. Image inputs work perfectly fine with the same setup.

Error Traceback

File "vllm/model_executor/layers/rotary_embedding/mrope.py", line 652, in _qwen3vl_get_input_positions_tensor
    video_grid_thw[video_index][0],
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^
IndexError: list index out of range

Root Cause Analysis

1. The Error Location

In vllm/model_executor/layers/rotary_embedding/mrope.py, the MRoPE position calculation:

  • Line 613-614: Extracts video_grid_thw from multimodal kwargs → length is 15 (one entry per video frame, each being [1, 24, 40])
  • Line 625-626: Calculates video_nums by counting <|video_pad|> tokens in prompt_ids → result is 29

When video_index >= 15, accessing video_grid_thw[video_index] causes the IndexError.

2. Why the Mismatch? (The Core Issue)

The problem is a mismatch between how processor.apply_chat_template() generates video placeholders and how vLLM's _apply_matches() replaces them.

Step 1: processor.apply_chat_template() Output

When processing a video with 30 frames (sampled to 15 frames), Qwen3-VL's processor.apply_chat_template() generates per-frame timestamp placeholders:

<0.5 seconds><|vision_start|><|video_pad|><|vision_end|>
<2.5 seconds><|vision_start|><|video_pad|><|vision_end|>
<4.5 seconds><|vision_start|><|video_pad|><|vision_end|>
... (15 such structures, one per frame)

This means there are 15 <|vision_start|><|video_pad|><|vision_end|> patterns in the prompt.

Step 2: vLLM's _get_prompt_updates() Expectation

In vllm/model_executor/models/qwen3_vl.py:913-985, the _get_prompt_updates() function defines how to replace video placeholders:

PromptReplacement(
    modality="video",
    target="<|vision_start|><|video_pad|><|vision_end|>",  # Expects ONE pattern
    replacement=get_video_replacement_qwen3vl,  # Generates ALL frame tokens
)

The get_video_replacement_qwen3vl() function generates the complete video token sequence for all 15 frames:

placeholder = []
for frame_idx in frames_idx_token:
    placeholder.extend(frame_idx)  # <X.X seconds> tokens
    placeholder.extend([vision_start_token_id] +
                       [video_token_id] * num_tokens_per_frame +
                       [vision_end_token_id])
return PromptUpdateDetails.select_token_id(placeholder, video_token_id)

Key insight: vLLM expects one placeholder <|vision_start|><|video_pad|><|vision_end|> to be replaced with all frame tokens.

Step 3: _apply_matches() Only Replaces the First Match

In vllm/multimodal/processing.py, the _apply_matches() function finds and replaces placeholders. However, since there's only one video item (with one replacement function), it only replaces the first <|vision_start|><|video_pad|><|vision_end|> pattern.

Result:

  • out_seqs[1]: Correctly expanded video tokens (15 frames worth of <|video_pad|> tokens)
  • out_seqs[2]: Remaining prompt still containing 14 unexpanded <|video_pad|> placeholders

Step 4: The Final Mismatch

Final prompt_ids contains 15 + 14 = 29 <|video_pad|> tokens, but video_grid_thw only has 15 entries.

When MRoPE tries to calculate positions for video_index = 15, 16, ..., 28, it causes IndexError: list index out of range.

My Current Workaround

I modified the apply_chat_template method to return two different prompt_ids:

async def apply_chat_template(self, messages, videos=None, images=None, ...):
    # 1. Get raw_prompt from processor (contains per-frame timestamps)
    raw_prompt = self.processor.apply_chat_template(messages, tokenize=False, ...)

    # 2. prompt_ids from processor - for TRAINING (fully expanded video tokens)
    model_inputs = self.processor(text=[raw_prompt], videos=videos, ...)
    prompt_ids_from_processor = model_inputs["input_ids"].squeeze(0).tolist()

    # 3. prompt_ids from tokenizer - for vLLM INFERENCE (simple placeholder)
    #    This uses tokenizer instead of processor, generating only ONE placeholder
    prompt_ids_from_tokenizer = self.tokenizer.apply_chat_template(messages, tokenize=True, ...)

    return prompt_ids_from_processor, prompt_ids_from_tokenizer

Usage:

prompt_ids_from_processor, prompt_ids_from_tokenizer = await self.apply_chat_template(
    messages, videos=videos, images=images,
)

# Use tokenizer version for vLLM generation (simple placeholder for vLLM to expand)
gen_output = await self.server_manager.generate(
    prompt_ids=prompt_ids_from_tokenizer,
    video_data=videos,
    ...
)

# Use processor version for training output (already expanded)
clip_output = AgentLoopOutput(prompt_ids=prompt_ids_from_processor, ...)

Questions

  1. Is this the expected behavior for Qwen3-VL video processing in vLLM? It seems like processor.apply_chat_template() and vLLM's _apply_matches() have different expectations about video placeholder format.

  2. Is my workaround (using tokenizer.apply_chat_template() for vLLM inference and processor.apply_chat_template() + processor() for training) a valid approach? Are there any potential issues?

  3. Is there a recommended way to handle video inputs with Qwen3-VL in vLLM that avoids this per-frame timestamp placeholder conflict?

Thank you for any guidance!

Solus-sano avatar Jan 22 '26 15:01 Solus-sano

Please! I really need some solutions.

Zeth-Lee avatar Jan 24 '26 09:01 Zeth-Lee

@Solus-sano We may need vllm support pixel_values_videos input to bypass video processing in server side. As a workaround, you can change video input to multiple images.

wuxibin89 avatar Jan 29 '26 06:01 wuxibin89

@Solus-sano We may need vllm support pixel_values_videos input to bypass video processing in server side. As a workaround, you can change video input to multiple images.

if we change video input to multiple images, how to input the video timestamp?

jiinhui avatar Jan 30 '26 03:01 jiinhui

@Solus-sano We may need vllm support pixel_values_videos input to bypass video processing in server side. As a workaround, you can change video input to multiple images.

if we change video input to multiple images, how to input the video timestamp?

My current workaround is to use a tokenizer instead of a processor to get the incoming prompt_ids (the tokenizer will bypass perform video processing on the prompt; that processing is done by VLLM), which works for me.

Solus-sano avatar Jan 30 '26 09:01 Solus-sano

@Solus-sano We may need vllm support pixel_values_videos input to bypass video processing in server side. As a workaround, you can change video input to multiple images.

if we change video input to multiple images, how to input the video timestamp?

My current workaround is to use a tokenizer instead of a processor to get the incoming prompt_ids (the tokenizer will bypass perform video processing on the prompt; that processing is done by VLLM), which works for me.

ok, As what you do in "My Current Workaround" ? And take a look the fuction self.processor() in verl official code , the param 'video_metadatas' need to be replaced with 'video_metadata'

jiinhui avatar Jan 31 '26 01:01 jiinhui

@Solus-sano We may need vllm support pixel_values_videos input to bypass video processing in server side. As a workaround, you can change video input to multiple images.

if we change video input to multiple images, how to input the video timestamp?

My current workaround is to use a tokenizer instead of a processor to get the incoming prompt_ids (the tokenizer will bypass perform video processing on the prompt; that processing is done by VLLM), which works for me.

ok, As what you do in "My Current Workaround" ? And take a look the fuction self.processor() in verl official code , the param 'video_metadatas' need to be replaced with 'video_metadata'

Yes, you can refer to this link for the video_metadata issue: https://github.com/verl-project/verl/issues/5021

Solus-sano avatar Feb 03 '26 10:02 Solus-sano

中文:我重新核对了这个 issue 的当前状态。这里的问题仍然更像是 Qwen3-VL video prompt 在 verl client 侧 processor 处理与 vLLM server 侧 placeholder replacement 之间的契约不一致,而不是 #7038 修复的“解码阶段采样出 <|video_pad|>”问题。

证据上,verl 当前 agent loop 在有 processor 时会先用 processor/build_multimodal_processor_inputs 生成 prompt ids(verl/experimental/agent_loop/agent_loop.py),随后 vLLM async server 仍把 prompt_token_ids 和 raw video_data 作为 multi_modal_data["video"] 交给 vLLM(verl/workers/rollout/vllm_rollout/vllm_async_server.py)。vLLM 的 Qwen3-VL _get_prompt_updates 会针对 video placeholder 做 PromptReplacement 并为一个 video item 生成完整 video replacement(vllm/model_executor/models/qwen3_vl.py),这与本 issue 里描述的 placeholder 数量和 video_grid_thw 行数不一致的根因相符。

所以,你现在的 workaround(vLLM inference 使用 tokenizer 生成的简单 placeholder prompt ids,让 vLLM 负责展开;训练侧继续使用 processor 处理后的 ids)从当前证据看是合理的短期方案。需要注意的是它必须保证 rollout 和训练侧看到的语义 prompt 一致,否则仍可能引入 token-in/token-out 对齐风险。另一个 workaround 是维护者提到的把 video 拆成 multiple images,但如果任务依赖 timestamp,这不是完全等价的替代。

长期看,比较干净的方向仍是上游 vLLM 支持 pixel_values_videos/预处理后视频 tensor 输入,从而避免 server 端重复视频 processor 处理;对应请求是 vLLM #33311,目前仍 open。另请注意,verl PR #7038 已合入但其说明明确写明不覆盖 #5024,因为 #5024 的 crash 发生在 vLLM mrope.py 对 prompt 的处理阶段,是 processor 与 vLLM placeholder-expansion contract 的独立问题。

English: I rechecked the current status of this issue. The problem still looks like a contract mismatch between Qwen3-VL video prompt processing on the verl client side and placeholder replacement on the vLLM server side, rather than the decode-time “model sampled <|video_pad|>” issue fixed by #7038.

Evidence: when a processor exists, verl currently builds prompt ids through the processor/build_multimodal_processor_inputs path (verl/experimental/agent_loop/agent_loop.py), and the vLLM async server then sends prompt_token_ids together with raw video_data as multi_modal_data["video"] to vLLM (verl/workers/rollout/vllm_rollout/vllm_async_server.py). vLLM's Qwen3-VL _get_prompt_updates uses PromptReplacement for the video placeholder and generates the full video replacement for one video item (vllm/model_executor/models/qwen3_vl.py), which matches the mismatch described in this issue: the number of video placeholders in the prompt can disagree with the rows in video_grid_thw.

Given that, your workaround is a reasonable short-term approach: use tokenizer-generated simple placeholder prompt ids for vLLM inference, while keeping processor-generated ids for the training side. The main caveat is that the rollout and training paths must still represent the same semantic prompt, otherwise token-in/token-out alignment can drift. The other workaround mentioned by the maintainer is to convert video input into multiple images, but that is not fully equivalent when timestamp semantics matter.

The cleaner long-term fix is still on the vLLM side: support pixel_values_videos or otherwise accept preprocessed video tensor inputs so the server-side video processor can be bypassed. That is tracked in vLLM #33311 and is still open. Also note that verl PR #7038 is merged, but its own scope explicitly says it does not cover #5024; #5024 happens in vLLM mrope.py on the prompt path and is a separate processor ↔ vLLM placeholder-expansion contract issue.

This response was generated by a scheduled Codex GPT5.5 issue processing task.

Kirrito-k423 avatar Jul 31 '26 01:07 Kirrito-k423

@wuxibin89 @Solus-sano

I hit the "timestamp+video_pad(s)" issue too when I run GRPO with multiple videos input. I submitted fix PR https://github.com/verl-project/verl/pull/8169, Would you please check if it can solve your issue.

ZongqiangZhang avatar Oct 16 '26 03:10 ZongqiangZhang