[Multimodal] Add Qwen2-VL/Qwen2.5-VL processor-mapped video loader (#45555)
Signed-off-by: Ranran <hzz5361@psu.edu> Signed-off-by: Ranran Haoran Zhang <ranzhang@redhat.com> Signed-off-by: Isotr0py <mozf@mail2.sysu.edu.cn> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Isotr0py <mozf@mail2.sysu.edu.cn>
This commit is contained in:
co-authored by
mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Isotr0py
parent
c441ad1c07
commit
635c38338a
@@ -7,6 +7,7 @@ from typing import Any, ClassVar, Literal, NamedTuple, cast
|
||||
|
||||
import numpy as np
|
||||
import numpy.typing as npt
|
||||
import torch
|
||||
|
||||
from vllm.logger import init_logger
|
||||
from vllm.utils.import_utils import PlaceholderModule
|
||||
@@ -653,6 +654,85 @@ class Qwen3VLVideoBackend(VideoBackend):
|
||||
)
|
||||
|
||||
|
||||
@VIDEO_LOADER_REGISTRY.register(
|
||||
"qwen2_vl",
|
||||
video_processor="Qwen2VLVideoProcessor",
|
||||
)
|
||||
class Qwen2VLVideoBackend(VideoBackend):
|
||||
"""Qwen2-VL / Qwen2.5-VL fps-based video backend.
|
||||
|
||||
Ports transformers' ``Qwen2VLVideoProcessor.sample_frames`` (fps mode),
|
||||
shared by Qwen2-VL and Qwen2.5-VL (the latter has no video processor of its
|
||||
own): sample ``total / original_fps * fps`` frames, clamp to
|
||||
``[min_frames, max_frames]`` (4 and 768), floor to a multiple of
|
||||
``temporal_patch_size`` (2), and take indices with the exact
|
||||
``torch.arange(0, total, total / n)`` call so they match HF byte-for-byte.
|
||||
|
||||
``num_frames`` is ignored (fps-driven, like the Qwen3-VL loader). The
|
||||
float32 step can emit an out-of-range tail index (e.g. 451 for a 451-frame
|
||||
clip); it is clamped to the last valid frame.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def compute_frames_index_to_sample(
|
||||
cls,
|
||||
source: VideoSourceMetadata,
|
||||
target: VideoTargetMetadata,
|
||||
**kwargs,
|
||||
) -> list[int]:
|
||||
# Refer to:
|
||||
# https://github.com/huggingface/transformers/blob/v5.7.0/src/transformers/models/qwen2_vl/video_processing_qwen2_vl.py#L122-L190
|
||||
total_frames_num = source.total_frames_num
|
||||
original_fps = source.original_fps
|
||||
temporal_patch_size = kwargs.get("temporal_patch_size", 2)
|
||||
min_frames = kwargs.get("min_frames", 4)
|
||||
max_frames = kwargs.get("max_frames", 768)
|
||||
|
||||
# vLLM reports original_fps == 0 for clips with unknown/variable fps
|
||||
# (VFR, malformed, streaming); fail loudly instead of dividing by zero.
|
||||
if original_fps <= 0:
|
||||
raise ValueError(
|
||||
"Qwen2-VL video sampling needs a known source fps, but the "
|
||||
"container reported 0 (variable or unknown frame rate)."
|
||||
)
|
||||
|
||||
max_frames = (
|
||||
math.floor(min(max_frames, total_frames_num) / temporal_patch_size)
|
||||
* temporal_patch_size
|
||||
)
|
||||
n = total_frames_num / original_fps * target.fps
|
||||
n = min(max(n, min_frames), max_frames, total_frames_num)
|
||||
n = math.floor(n / temporal_patch_size) * temporal_patch_size
|
||||
|
||||
# ``torch.arange`` matches transformers' float32 index math exactly
|
||||
# (numpy's float64 diverges by a frame on some inputs); clamp the tail
|
||||
# because that step can emit an index == total_frames_num.
|
||||
indices = torch.arange(0, total_frames_num, total_frames_num / n).int()
|
||||
return torch.clamp(indices, max=total_frames_num - 1).tolist()
|
||||
|
||||
@classmethod
|
||||
def load_bytes(
|
||||
cls,
|
||||
data: bytes,
|
||||
num_frames: int = -1,
|
||||
fps: int = 2,
|
||||
max_duration: int = 300,
|
||||
frame_recovery: bool = False,
|
||||
*,
|
||||
backend: Literal["opencv", "pyav"] = "opencv",
|
||||
**kwargs,
|
||||
) -> tuple[npt.NDArray, dict[str, Any]]:
|
||||
return super().load_bytes(
|
||||
data,
|
||||
num_frames=num_frames,
|
||||
fps=fps,
|
||||
max_duration=max_duration,
|
||||
frame_recovery=frame_recovery,
|
||||
backend=backend,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
|
||||
@VIDEO_LOADER_REGISTRY.register(
|
||||
"opencv_dynamic",
|
||||
video_processor="Glm4vVideoProcessor",
|
||||
|
||||
Reference in New Issue
Block a user