image.py 2.68 KB
Newer Older
1
from functools import lru_cache
2
3
4

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

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

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

logger = init_logger(__name__)

17
cached_get_image_processor = lru_cache(get_image_processor)
18
19


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

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

26
    def _get_hf_image_processor(self, model_config: ModelConfig):
27
28
29
30
        mm_processor_kwargs = ({} if model_config.mm_processor_kwargs is None
                               else model_config.mm_processor_kwargs)
        # We don't explicitly check kwarg overrides to the HF class
        # since the automodel just takes kwargs, so we can't inspect it
31
        return cached_get_image_processor(
32
            model_config.model,
33
34
            trust_remote_code=model_config.trust_remote_code,
            **mm_processor_kwargs)
35

36
37
38
39
40
    def _default_input_mapper(
        self,
        ctx: InputContext,
        data: MultiModalData[object],
    ) -> MultiModalInputs:
41
        model_config = ctx.model_config
42

43
44
45
46
        # Processed by input processor
        if isinstance(data, BatchFeature):
            return MultiModalInputs(data.data)

47
        # PIL image
48
        if isinstance(data, Image.Image) or is_list_of(data, Image.Image):
49
            image_processor = self._get_hf_image_processor(model_config)
50

51
            if image_processor is None:
52
                raise RuntimeError("No HuggingFace processor is available "
53
54
                                   "to process the image object")
            try:
55
56
57
                batch_data = image_processor \
                    .preprocess(data, return_tensors="pt") \
                    .data
58
            except Exception:
59
60
61
62
63
64
                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__)
65
66
                raise

67
            return MultiModalInputs(batch_data)
68
69

        # Image embedding
70
        elif isinstance(data, torch.Tensor) or is_list_of(data, torch.Tensor):
71
            return MultiModalInputs({"image_embeds": data})
72
73

        raise TypeError(f"Invalid image type: {type(data)}")
74
75
76

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