| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | import pytest |
| 5 | import torch |
| 6 | |
| 7 | from vllm.model_executor.models.clip import ( |
| 8 | dual_encoder_has_text_tokens, |
| 9 | merge_dual_encoder_text_and_vision, |
| 10 | ) |
| 11 | |
| 12 | pytestmark = [pytest.mark.cpu_test, pytest.mark.skip_global_cleanup] |
| 13 | |
| 14 | |
| 15 | def test_text_only_batch_runs_text_encoder(): |
| 16 | assert dual_encoder_has_text_tokens(False, None) |
| 17 | assert dual_encoder_has_text_tokens(False, torch.zeros(4, dtype=torch.bool)) |
| 18 | |
| 19 | |
| 20 | def test_vision_only_batch_skips_text_encoder(): |
| 21 | is_multimodal = torch.ones(8, dtype=torch.bool) |
| 22 | assert not dual_encoder_has_text_tokens(True, is_multimodal) |
| 23 | |
| 24 | |
| 25 | def test_mixed_batch_runs_text_encoder(): |
| 26 | # Image tokens followed by a text sequence: the old batch-wide flag |
| 27 | # treated this as vision-only and skipped the text encoder (#53091). |
| 28 | is_multimodal = torch.tensor([True, True, True, False, False, False]) |
| 29 | assert dual_encoder_has_text_tokens(True, is_multimodal) |
| 30 | |
| 31 | |
| 32 | def test_missing_token_mask_with_mm_embeddings_is_vision_only(): |
| 33 | assert not dual_encoder_has_text_tokens(True, None) |
| 34 | |
| 35 | |
| 36 | def test_merge_keeps_vision_on_mm_tokens(): |
| 37 | text = torch.zeros(4, 2) |
| 38 | vision = torch.ones(4, 2) |
| 39 | is_multimodal = torch.tensor([True, True, False, False]) |
| 40 | out = merge_dual_encoder_text_and_vision(text, vision, is_multimodal) |
| 41 | assert torch.equal(out[:2], vision[:2]) |
| 42 | assert torch.equal(out[2:], text[2:]) |