| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | from types import SimpleNamespace |
| 5 | |
| 6 | import pytest |
| 7 | import torch |
| 8 | |
| 9 | from vllm.config.multimodal import MultiModalDummyOptions, VideoDummyOptions |
| 10 | from vllm.model_executor.models.molmo2 import ( |
| 11 | Molmo2DummyInputsBuilder, |
| 12 | build_flat_image_bool_length, |
| 13 | ) |
| 14 | |
| 15 | |
| 16 | def test_build_flat_image_bool_length_matches_molmoweb_processor_tokens(): |
| 17 | hf_config = SimpleNamespace( |
| 18 | image_patch_id=151938, |
| 19 | low_res_image_start_token_id=151940, |
| 20 | image_start_token_id=151936, |
| 21 | image_col_id=151939, |
| 22 | image_end_token_id=151937, |
| 23 | ) |
| 24 | image_grids = torch.tensor([[14, 14, 14, 23]], dtype=torch.long) |
| 25 | |
| 26 | image_tokens, num_image_tokens = build_flat_image_bool_length( |
| 27 | image_grids, |
| 28 | hf_config, |
| 29 | image_use_col_tokens=True, |
| 30 | use_single_crop_col_tokens=None, |
| 31 | use_single_crop_start_token=False, |
| 32 | ) |
| 33 | |
| 34 | assert num_image_tokens.tolist() == [550] |
| 35 | assert len(image_tokens) == 550 |
| 36 | assert image_tokens[0].item() == hf_config.image_start_token_id |
| 37 | assert (image_tokens == hf_config.image_col_id).sum().item() == 28 |
| 38 | |
| 39 | |
| 40 | def test_build_flat_image_bool_length_respects_disabled_col_tokens(): |
| 41 | hf_config = SimpleNamespace( |
| 42 | image_patch_id=151938, |
| 43 | low_res_image_start_token_id=151940, |
| 44 | image_start_token_id=151936, |
| 45 | image_col_id=151939, |
| 46 | image_end_token_id=151937, |
| 47 | ) |
| 48 | image_grids = torch.tensor([[2, 3, 5, 7]], dtype=torch.long) |
| 49 | |
| 50 | image_tokens, num_image_tokens = build_flat_image_bool_length( |
| 51 | image_grids, |
| 52 | hf_config, |
| 53 | image_use_col_tokens=False, |
| 54 | use_single_crop_col_tokens=False, |
| 55 | use_single_crop_start_token=True, |
| 56 | ) |
| 57 | |
| 58 | assert num_image_tokens.tolist() == [45] |
| 59 | assert len(image_tokens) == 45 |
| 60 | assert image_tokens[0].item() == hf_config.low_res_image_start_token_id |
| 61 | assert (image_tokens == hf_config.image_col_id).sum().item() == 0 |
| 62 | |
| 63 | |
| 64 | @pytest.mark.parametrize( |
| 65 | ("num_frames_override", "expected_frames"), |
| 66 | [(1, 2), (2, 2), (3, 3)], |
| 67 | ) |
| 68 | def test_dummy_video_num_frames_override_honors_min_of_two( |
| 69 | num_frames_override, expected_frames |
| 70 | ): |
| 71 | """A ``num_frames`` override below 2 must be ignored (the model needs at |
| 72 | least 2 frames), matching the "cannot be less than 2, will be ignored" |
| 73 | warning.""" |
| 74 | builder = object.__new__(Molmo2DummyInputsBuilder) |
| 75 | builder.info = SimpleNamespace( |
| 76 | get_hf_processor=lambda: SimpleNamespace( |
| 77 | video_processor=SimpleNamespace(size={"width": 64, "height": 64}) |
| 78 | ), |
| 79 | get_num_frames_with_most_features=lambda seq_len, mm_counts: 16, |
| 80 | get_image_size_with_most_features=lambda: (64, 64), |
| 81 | ) |
| 82 | |
| 83 | data = builder.get_dummy_mm_data( |
| 84 | seq_len=128, |
| 85 | mm_counts={"image": 0, "video": 1}, |
| 86 | mm_options=MultiModalDummyOptions( |
| 87 | video=VideoDummyOptions(num_frames=num_frames_override) |
| 88 | ), |
| 89 | ) |
| 90 | |
| 91 | video, _metadata = data["video"][0] |
| 92 | assert video.shape[0] == expected_frames |