Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions tests/multimodal/media/test_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,24 @@ def test_load_base64_jpeg_raises_on_zero_num_frames():
videoio.load_base64("video/jpeg", data)


def test_molmo2_backend_honors_num_frames(tmp_path):
"""Request-selected molmo2 loader must not ignore a positive frame cap."""
image_path = get_vllm_public_assets(
filename="stop_sign.jpg", s3_prefix="vision_model_images"
)
video_path = tmp_path / "molmo2_cap.mp4"
create_video_from_image(str(image_path), str(video_path), num_frames=8, fps=8)

videoio = VideoMediaIO(
ImageMediaIO(),
num_frames=1,
video_backend="molmo2",
)
frames, _metadata = videoio.load_file(video_path)

assert frames.shape[0] == 1


def test_pynvvideocodec_unrelated_error_propagates(
monkeypatch: pytest.MonkeyPatch,
):
Expand Down
42 changes: 42 additions & 0 deletions tests/multimodal/test_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,42 @@ def test_torchcodec_backend_returns_target_frames_not_keyframes():
)


def _molmo2_source(total_frames: int, fps: float = 30.0) -> VideoSourceMetadata:
return VideoSourceMetadata(
total_frames_num=total_frames,
original_fps=fps,
duration=total_frames / fps if fps > 0 else 0.0,
)


def test_molmo2_default_mode_honors_positive_num_frames():
"""Unset frame_sample_mode must still respect target.num_frames."""
source = _molmo2_source(1800)
target = VideoTargetMetadata(num_frames=1, fps=-1, max_duration=-1)
idx = Molmo2VideoBackend.compute_frames_index_to_sample(source, target)

assert len(idx) == 1
assert 0 <= idx[0] < 1800


def test_molmo2_default_mode_uniformly_caps_to_num_frames():
source = _molmo2_source(1800)
target = VideoTargetMetadata(num_frames=32, fps=-1, max_duration=-1)
idx = Molmo2VideoBackend.compute_frames_index_to_sample(source, target)

assert len(idx) == 32
assert idx[0] == 0
assert idx[-1] == 1799


def test_molmo2_default_mode_keeps_all_frames_when_unlimited():
source = _molmo2_source(12)
target = VideoTargetMetadata(num_frames=-1, fps=-1, max_duration=-1)
idx = Molmo2VideoBackend.compute_frames_index_to_sample(source, target)

assert idx == list(range(12))


@pytest.mark.parametrize(
"loader_key, kwargs, expected_num_frames",
[
Expand Down Expand Up @@ -1282,6 +1318,12 @@ def test_torchcodec_backend_returns_target_frames_not_keyframes():
pytest.param(
"openpangu", {"num_frames": 32, "fps": -1}, 32, id="openpangu-num_frames"
),
pytest.param(
"molmo2",
{"num_frames": 1},
1,
id="molmo2-default-honors-num_frames",
),
pytest.param(
"molmo2",
{"num_frames": 32, "frame_sample_mode": "uniform_last_frame"},
Expand Down
9 changes: 8 additions & 1 deletion vllm/multimodal/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,14 @@ def compute_frames_index_to_sample(
max_fps = kwargs.get("max_fps")
frame_sample_mode = kwargs.get("frame_sample_mode")
if frame_sample_mode is None:
return list(range(0, source.total_frames_num))
total_num_frames = source.total_frames_num
num_frames = target.num_frames
if num_frames > 0:
n = min(num_frames, total_num_frames)
if n == total_num_frames:
return list(range(total_num_frames))
return np.linspace(0, total_num_frames - 1, n, dtype=int).tolist()
return list(range(0, total_num_frames))

if frame_sample_mode not in {"uniform_last_frame", "fps"}:
raise NotImplementedError(
Expand Down
Loading