Skip to content

[fix] Fixes Qwen3-VL prompt expansion when a message contains multiple videos/multiple round chat#10518

Merged
Kuangdd01 merged 4 commits into
hiyouga:mainfrom
luca-888:fix/qwen3vl-multi-video-messages
Jul 2, 2026
Merged

[fix] Fixes Qwen3-VL prompt expansion when a message contains multiple videos/multiple round chat#10518
Kuangdd01 merged 4 commits into
hiyouga:mainfrom
luca-888:fix/qwen3vl-multi-video-messages

Conversation

@luca-888

@luca-888 luca-888 commented May 28, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #9704

This PR fixes Qwen3-VL prompt expansion when a single message contains multiple <video> placeholders.

Previously, Qwen3-VL video metadata was selected by message index during prompt expansion. This breaks when one message contains more than one video, because later <video> placeholders can reuse the wrong metadata and frame count.

The fix changes video expansion to consume video_grid_thw and video_metadata by video placeholder order instead of message index. Each <video> placeholder now uses its corresponding video grid, frame count, timestamps, and per-frame token length.

This PR also updates Qwen3-VL timestamp calculation to pass temporal_patch_size to processor._calculate_timestamps, matching the upstream Qwen3-VL processor behavior. merge_size is still used for spatial token merging, while temporal_patch_size controls timestamp grouping across frames.

A regression test is added for a user message with two videos of different lengths.

Before submitting

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the multi-modal plugin to properly support processing multiple videos. It overrides the get_mm_inputs method, updates process_messages to dynamically index video grids and metadata using the current video token count rather than the message index, and uses temporal_patch_size as a fallback for merge_size. Additionally, the test suite has been updated to include multi-video test cases and verify the new get_mm_inputs functionality. There are no review comments, so I have no further feedback to provide.

@luca-888 luca-888 force-pushed the fix/qwen3vl-multi-video-messages branch from 08abd61 to 2dde55e Compare May 28, 2026 07:33
@luca-888 luca-888 changed the title Fix/qwen3vl multi video messages [fix] Fixes Qwen3-VL prompt expansion when a message contains multiple videos May 28, 2026
@luca-888 luca-888 changed the title [fix] Fixes Qwen3-VL prompt expansion when a message contains multiple videos [fix] Fixes Qwen3-VL prompt expansion when a message contains multiple videos/multiple round chat May 28, 2026
@luca-888 luca-888 force-pushed the fix/qwen3vl-multi-video-messages branch from 2dde55e to 3282346 Compare May 28, 2026 08:00
@luca-888 luca-888 force-pushed the fix/qwen3vl-multi-video-messages branch from 3282346 to 6e8e7e5 Compare May 28, 2026 08:03
@luca-888

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the video processing logic in mm_plugin.py to dynamically retrieve num_frames and metadata based on num_video_tokens when expanding video tokens, rather than using hardcoded values or message indices. It also updates the test suite to verify multi-video processing. The review feedback suggests using getattr with a default value when accessing temporal_patch_size from video_processor to improve robustness and maintain consistency with other parts of the codebase.

Comment on lines 2406 to 2410
timestamps = processor._calculate_timestamps(
metadata.frames_indices,
metadata.fps,
video_processor.merge_size,
video_processor.temporal_patch_size,
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better robustness and consistency with other parts of the codebase (such as lines 2074 and 2351), it is recommended to use getattr with a default value when accessing temporal_patch_size from video_processor.

Suggested change
timestamps = processor._calculate_timestamps(
metadata.frames_indices,
metadata.fps,
video_processor.merge_size,
video_processor.temporal_patch_size,
)
timestamps = processor._calculate_timestamps(
metadata.frames_indices,
metadata.fps,
getattr(video_processor, "temporal_patch_size", 2),
)

Co-authored-by: gemini-code-assist <200291788+gemini-code-assist@users.noreply.github.com>
@luca-888

Copy link
Copy Markdown
Contributor Author

For Qwen3-VL, should we treat multiple videos across both of these forms as supported?

  1. Multiple videos in one message:
    <video> ... <video>

  2. Multiple videos across turns:
    user turn 1: <video>
    user turn 2: <video>

I also noticed a similar first-video assumption in GLM4VPlugin, where num_frames and timestamps are derived from the first video before replacing each <video> placeholder. I did not include that change here to keep this PR clean. @Kuangdd01

@Kuangdd01

Copy link
Copy Markdown
Collaborator

thanks, we will look through this.

@Kuangdd01 Kuangdd01 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing, LGTM!

@Kuangdd01 Kuangdd01 reopened this Jun 29, 2026
@Kuangdd01 Kuangdd01 merged commit c8a082e into hiyouga:main Jul 2, 2026
15 of 16 checks passed
@luca-888 luca-888 deleted the fix/qwen3vl-multi-video-messages branch July 2, 2026 03:08
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.

qwen3vl 多模态video sft: index out of range

3 participants