|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from vllm.multimodal.video import sample_frames_from_video |
| 7 | + |
| 8 | +from ....conftest import VIDEO_ASSETS |
| 9 | + |
| 10 | +models = ["Qwen/Qwen2.5-VL-3B-Instruct"] |
| 11 | +target_dtype = "bfloat16" |
| 12 | + |
| 13 | +VIDEO_PLACEHOLDER = "<|vision_start|><|video_pad|><|vision_end|>" |
| 14 | + |
| 15 | + |
| 16 | +def qwen2_5_vl_chat_template(*query): |
| 17 | + return f"<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n{''.join(query)}<|im_end|><|im_start|>assistant\n" # noqa: E501 |
| 18 | + |
| 19 | + |
| 20 | +VIDEO_PROMPTS = VIDEO_ASSETS.prompts({ |
| 21 | + "baby_reading": |
| 22 | + qwen2_5_vl_chat_template( |
| 23 | + VIDEO_PLACEHOLDER, |
| 24 | + "Describe this video with a short sentence ", |
| 25 | + "(no more than 20 words)", |
| 26 | + ), |
| 27 | +}) |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.core_model |
| 31 | +@pytest.mark.parametrize("model", models) |
| 32 | +@pytest.mark.parametrize("video_pruning_rate", [0.0, 0.75]) |
| 33 | +@pytest.mark.parametrize("num_frames", [16]) |
| 34 | +@pytest.mark.parametrize("dtype", [target_dtype]) |
| 35 | +@pytest.mark.parametrize("max_tokens", [128]) |
| 36 | +def test_qwen2_5_vl_evs_functionality(vllm_runner, video_assets, model, |
| 37 | + video_pruning_rate: float, |
| 38 | + num_frames: int, dtype: str, |
| 39 | + max_tokens: int) -> None: |
| 40 | + """Test EVS (Efficient Video Sampling) functionality with different |
| 41 | + pruning rates. |
| 42 | + """ |
| 43 | + |
| 44 | + # Sample frames from video assets |
| 45 | + sampled_vids = [ |
| 46 | + sample_frames_from_video(asset.np_ndarrays, num_frames) |
| 47 | + for asset in video_assets |
| 48 | + ] |
| 49 | + |
| 50 | + prompts = [VIDEO_PROMPTS[0]] |
| 51 | + videos = [sampled_vids[0]] |
| 52 | + |
| 53 | + # Initialize model with EVS configuration |
| 54 | + with vllm_runner(model, |
| 55 | + runner="generate", |
| 56 | + max_model_len=4000, |
| 57 | + max_num_seqs=1, |
| 58 | + dtype=dtype, |
| 59 | + limit_mm_per_prompt={"video": 1}, |
| 60 | + tensor_parallel_size=1, |
| 61 | + video_pruning_rate=video_pruning_rate) as vllm_model: |
| 62 | + |
| 63 | + # Generate output - this should not crash |
| 64 | + outputs = vllm_model.generate_greedy(prompts, |
| 65 | + max_tokens, |
| 66 | + videos=videos) |
| 67 | + |
| 68 | + # Basic validation that we got a response |
| 69 | + assert len(outputs) == 1 |
| 70 | + output_ids, output_text = outputs[0] |
| 71 | + |
| 72 | + # Ensure we got some output |
| 73 | + assert len(output_ids) > 0 |
| 74 | + assert len(output_text) > 0 |
| 75 | + |
| 76 | + # Ensure the output is a string |
| 77 | + assert isinstance(output_text, str) |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.core_model |
| 81 | +@pytest.mark.parametrize("model", models) |
| 82 | +@pytest.mark.parametrize("video_pruning_rate", [0.0, 0.75]) |
| 83 | +@pytest.mark.parametrize("num_frames", [16]) |
| 84 | +@pytest.mark.parametrize("dtype", [target_dtype]) |
| 85 | +@pytest.mark.parametrize("max_tokens", [128]) |
| 86 | +def test_qwen2_5_vl_evs_batched_videos(vllm_runner, video_assets, model, |
| 87 | + video_pruning_rate: float, |
| 88 | + num_frames: int, dtype: str, |
| 89 | + max_tokens: int) -> None: |
| 90 | + """Test EVS functionality with batched videos. |
| 91 | +
|
| 92 | + This test validates that: |
| 93 | + 1. The model handles batched video inputs correctly with EVS |
| 94 | + 2. Both pruning configurations work with multiple videos |
| 95 | + 3. The model doesn't crash when processing multiple videos simultaneously |
| 96 | + """ |
| 97 | + # Sample frames from video assets |
| 98 | + sampled_vids = [ |
| 99 | + sample_frames_from_video(asset.np_ndarrays, num_frames) |
| 100 | + for asset in video_assets |
| 101 | + ] |
| 102 | + |
| 103 | + # Test batched videos |
| 104 | + prompts = [VIDEO_PROMPTS[0], VIDEO_PROMPTS[0]] |
| 105 | + videos = [sampled_vids[0], |
| 106 | + sampled_vids[0]] # Use same video twice for testing |
| 107 | + |
| 108 | + # Initialize model with EVS configuration |
| 109 | + with vllm_runner(model, |
| 110 | + runner="generate", |
| 111 | + max_model_len=4000, |
| 112 | + max_num_seqs=2, |
| 113 | + dtype=dtype, |
| 114 | + limit_mm_per_prompt={"video": 2}, |
| 115 | + tensor_parallel_size=1, |
| 116 | + video_pruning_rate=video_pruning_rate) as vllm_model: |
| 117 | + |
| 118 | + # Generate output - this should not crash |
| 119 | + outputs = vllm_model.generate_greedy(prompts, |
| 120 | + max_tokens, |
| 121 | + videos=videos) |
| 122 | + |
| 123 | + # Basic validation that we got responses for both videos |
| 124 | + assert len(outputs) == 2 |
| 125 | + |
| 126 | + for output_ids, output_text in outputs: |
| 127 | + # Ensure we got some output for each video |
| 128 | + assert len(output_ids) > 0 |
| 129 | + assert len(output_text) > 0 |
| 130 | + |
| 131 | + # Ensure the output is a string |
| 132 | + assert isinstance(output_text, str) |
0 commit comments