| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | import weakref |
| 5 | |
| 6 | import pytest |
| 7 | |
| 8 | from vllm import LLM, SamplingParams |
| 9 | from vllm.assets.image import ImageAsset |
| 10 | from vllm.exceptions import VLLMValidationError |
| 11 | |
| 12 | MODEL = "llava-hf/llava-1.5-7b-hf" |
| 13 | PROMPT = "USER: <image>\nDescribe this image briefly.\nASSISTANT:" |
| 14 | TEXT_ONLY_PROMPT = "USER: What is 2 + 2?\nASSISTANT:" |
| 15 | |
| 16 | |
| 17 | @pytest.fixture(scope="module") |
| 18 | def llm(vllm_runner): |
| 19 | """LLM with enable_mm_embeds=True and all modality limits zeroed out.""" |
| 20 | with vllm_runner( |
| 21 | MODEL, |
| 22 | max_model_len=2048, |
| 23 | enforce_eager=True, |
| 24 | gpu_memory_utilization=0.8, |
| 25 | enable_mm_embeds=True, |
| 26 | limit_mm_per_prompt={"image": 0}, |
| 27 | ) as runner: |
| 28 | # pytest caches yielded fixtures until after teardown, so use a proxy to |
| 29 | # avoid retaining the LLM while VllmRunner.__exit__ releases ROCm memory. |
| 30 | yield weakref.proxy(runner.llm) |
| 31 | |
| 32 | |
| 33 | @pytest.mark.skip_global_cleanup |
| 34 | def test_generate_with_embedding(llm: LLM): |
| 35 | """Pre-computed embedding produces tokens without hanging.""" |
| 36 | embedding = ImageAsset("stop_sign").image_embeds |
| 37 | outputs = llm.generate( |
| 38 | {"prompt": PROMPT, "multi_modal_data": {"image": embedding}}, |
| 39 | sampling_params=SamplingParams(max_tokens=32, temperature=0.0), |
| 40 | ) |
| 41 | assert len(outputs) == 1 |
| 42 | assert len(outputs[0].outputs[0].text) > 0 |
| 43 | |
| 44 | |
| 45 | @pytest.mark.skip_global_cleanup |
| 46 | def test_raw_image_rejected(llm: LLM): |
| 47 | """Raw image input is still rejected when limit=0.""" |
| 48 | raw_image = ImageAsset("stop_sign").pil_image |
| 49 | with pytest.raises(VLLMValidationError, match=r"At most 0 image\(s\)"): |
| 50 | llm.generate( |
| 51 | {"prompt": PROMPT, "multi_modal_data": {"image": raw_image}}, |
| 52 | sampling_params=SamplingParams(max_tokens=16), |
| 53 | ) |
| 54 | |
| 55 | |
| 56 | @pytest.mark.skip_global_cleanup |
| 57 | def test_text_only_prompt(llm: LLM): |
| 58 | """Text-only prompts still work under this config.""" |
| 59 | outputs = llm.generate( |
| 60 | TEXT_ONLY_PROMPT, |
| 61 | sampling_params=SamplingParams(max_tokens=16, temperature=0.0), |
| 62 | ) |
| 63 | assert len(outputs) == 1 |
| 64 | assert len(outputs[0].outputs[0].text) > 0 |