image.py 3.01 KB
Newer Older
1
from functools import lru_cache
2
from typing import Any, Dict, Optional
3
4
5

import torch
from PIL import Image
6
from transformers.image_processing_base import BatchFeature
7

8
9
from vllm.config import ModelConfig
from vllm.inputs.registry import InputContext
10
from vllm.logger import init_logger
11
from vllm.transformers_utils.processor import get_image_processor
12
from vllm.utils import is_list_of
13

14
from .base import MultiModalData, MultiModalInputs, MultiModalPlugin
15
16
17

logger = init_logger(__name__)

18
cached_get_image_processor = lru_cache(get_image_processor)
19
20


21
class ImagePlugin(MultiModalPlugin):
22
    """Plugin for image data."""
23

24
25
    def get_data_key(self) -> str:
        return "image"
26

27
28
29
30
31
32
33
    def _get_hf_image_processor(
        self,
        model_config: ModelConfig,
        mm_processor_kwargs: Optional[Dict[str, Any]] = None,
    ):
        if mm_processor_kwargs is None:
            mm_processor_kwargs = {}
34
        return cached_get_image_processor(
35
            model_config.model,
36
37
            trust_remote_code=model_config.trust_remote_code,
            **mm_processor_kwargs)
38

39
40
41
42
    def _default_input_mapper(
        self,
        ctx: InputContext,
        data: MultiModalData[object],
43
        **mm_processor_kwargs,
44
    ) -> MultiModalInputs:
45
        model_config = ctx.model_config
46

47
48
49
50
        # Processed by input processor
        if isinstance(data, BatchFeature):
            return MultiModalInputs(data.data)

51
        # PIL image
52
        if isinstance(data, Image.Image) or is_list_of(data, Image.Image):
53
54
55
56
            image_processor = self._get_hf_image_processor(
                model_config,
                mm_processor_kwargs,
            )
57

58
            if image_processor is None:
59
                raise RuntimeError("No HuggingFace processor is available "
60
61
                                   "to process the image object")
            try:
62
63
64
65
66
                # NOTE: It may make sense to forward the mm_processor_kwargs
                # here too. For now, to keep it simple, we only allow it be
                # used for the initialization call though, just in case the
                # signatures of the preprocessor initializer don't match
                # preprocess()
67
68
69
                batch_data = image_processor \
                    .preprocess(data, return_tensors="pt") \
                    .data
70
            except Exception:
71
72
73
74
75
76
                logger.error(
                    "Failed to process image (%s) with the default mapper. "
                    "This is most likely an edge-case with this model's image "
                    "processor in transformers (type: %s), and not vLLM.",
                    data,
                    type(image_processor).__name__)
77
78
                raise

79
            return MultiModalInputs(batch_data)
80
81

        # Image embedding
82
        elif isinstance(data, torch.Tensor) or is_list_of(data, torch.Tensor):
83
            return MultiModalInputs({"image_embeds": data})
84
85

        raise TypeError(f"Invalid image type: {type(data)}")
86
87
88

    def _default_max_multimodal_tokens(self, ctx: InputContext) -> int:
        return 3000