|
| 1 | +# |
| 2 | +# Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# This file is a part of the vllm-ascend project. |
| 16 | +# |
| 17 | + |
| 18 | +import os |
| 19 | + |
| 20 | +# Set spawn method before any torch/NPU imports to avoid fork issues |
| 21 | +os.environ.setdefault('VLLM_WORKER_MULTIPROC_METHOD', 'spawn') |
| 22 | + |
| 23 | +import pytest |
| 24 | +from vllm.assets.image import ImageAsset |
| 25 | + |
| 26 | +from tests.e2e.conftest import VllmRunner |
| 27 | +from tests.e2e.model_utils import check_outputs_equal |
| 28 | + |
| 29 | +MODELS = [ |
| 30 | + "OpenGVLab/InternVL2-8B", |
| 31 | + "OpenGVLab/InternVL2_5-8B", |
| 32 | + "OpenGVLab/InternVL3-8B", |
| 33 | + "OpenGVLab/InternVL3_5-8B", |
| 34 | +] |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.parametrize("model", MODELS) |
| 38 | +def test_internvl_basic(model: str): |
| 39 | + """Test basic InternVL2 inference with single image.""" |
| 40 | + # Load test image |
| 41 | + image = ImageAsset("cherry_blossom").pil_image.convert("RGB") |
| 42 | + |
| 43 | + # InternVL uses chat template format |
| 44 | + # Format: <|im_start|>user\n<image>\nQUESTION<|im_end|>\n<|im_start|>assistant\n |
| 45 | + questions = [ |
| 46 | + "What is the content of this image?", |
| 47 | + "Describe this image in detail.", |
| 48 | + ] |
| 49 | + |
| 50 | + # Build prompts with InternVL2 chat template |
| 51 | + prompts = [ |
| 52 | + f"<|im_start|>user\n<image>\n{q}<|im_end|>\n<|im_start|>assistant\n" |
| 53 | + for q in questions |
| 54 | + ] |
| 55 | + images = [image] * len(prompts) |
| 56 | + |
| 57 | + outputs = {} |
| 58 | + for enforce_eager, mode in [(False, "eager"), (True, "graph")]: |
| 59 | + with VllmRunner( |
| 60 | + model, |
| 61 | + max_model_len=8192, |
| 62 | + limit_mm_per_prompt={"image": 4}, |
| 63 | + enforce_eager=enforce_eager, |
| 64 | + dtype="bfloat16", |
| 65 | + ) as vllm_model: |
| 66 | + generated_outputs = vllm_model.generate_greedy( |
| 67 | + prompts=prompts, |
| 68 | + images=images, |
| 69 | + max_tokens=128, |
| 70 | + ) |
| 71 | + |
| 72 | + assert len(generated_outputs) == len(prompts), \ |
| 73 | + f"Expected {len(prompts)} outputs, got {len(generated_outputs)} in {mode} mode" |
| 74 | + |
| 75 | + for i, (_, output_str) in enumerate(generated_outputs): |
| 76 | + assert output_str, \ |
| 77 | + f"{mode.capitalize()} mode output {i} should not be empty. Prompt: {prompts[i]}" |
| 78 | + assert len(output_str.strip()) > 0, \ |
| 79 | + f"{mode.capitalize()} mode Output {i} should have meaningful content" |
| 80 | + |
| 81 | + outputs[mode] = generated_outputs |
| 82 | + |
| 83 | + eager_outputs = outputs["eager"] |
| 84 | + graph_outputs = outputs["graph"] |
| 85 | + |
| 86 | + check_outputs_equal( |
| 87 | + outputs_0_lst=eager_outputs, |
| 88 | + outputs_1_lst=graph_outputs, |
| 89 | + name_0="eager mode", |
| 90 | + name_1="graph mode", |
| 91 | + similarity_threshold=0.50, |
| 92 | + ) |
0 commit comments