bagel.py 2.68 KB
Newer Older
1
2
3
4
5
6
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
# Copyright 2025 Bytedance Ltd. and/or its affiliates.
"""BAGEL processor for image and text inputs."""

from transformers import AutoProcessor
7
from transformers.feature_extraction_utils import BatchFeature
8
from transformers.image_utils import ImageInput
9
from transformers.processing_utils import ProcessingKwargs, ProcessorMixin, Unpack
10
11
12
from transformers.tokenization_utils_base import PreTokenizedInput, TextInput


13
14
15
16
17
18
19
20
class BagelProcessorKwargs(ProcessingKwargs, total=False):  # type: ignore[call-arg]
    _defaults = {
        "images_kwargs": {
            "return_tensors": "pt",
        },
    }


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class BagelProcessor(ProcessorMixin):
    """
    Constructs a BAGEL processor which wraps a
    SigLIP image processor and a Qwen2 tokenizer.
    """

    attributes = ["image_processor", "tokenizer"]
    image_processor_class = "SiglipImageProcessor"
    tokenizer_class = "AutoTokenizer"

    def __call__(
        self,
        text: TextInput
        | PreTokenizedInput
        | list[TextInput]
        | list[PreTokenizedInput] = None,
        images: ImageInput = None,
38
        **kwargs: Unpack[BagelProcessorKwargs],
39
40
41
42
    ):
        """
        Main method to prepare for the model one or several sequences(s) and image(s).
        """
43
44
45
46
47
48
        output_kwargs = self._merge_kwargs(
            BagelProcessorKwargs,
            tokenizer_init_kwargs=self.tokenizer.init_kwargs,
            **kwargs,
        )

49
50
        if images is not None:
            # Process images with the image processor
51
52
53
            pixel_values = self.image_processor(
                images, **output_kwargs["images_kwargs"]
            )
54
        else:
55
            pixel_values = {}
56

57
58
59
60
61
        text_inputs = (
            self.tokenizer(text, **output_kwargs["text_kwargs"])
            if text is not None
            else {}
        )
62

63
        return BatchFeature(data={**pixel_values, **text_inputs})
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

    def batch_decode(self, *args, **kwargs):
        """
        This method forwards all its arguments to Qwen2TokenizerFast's batch_decode.
        """
        return self.tokenizer.batch_decode(*args, **kwargs)

    def decode(self, *args, **kwargs):
        """
        This method forwards all its arguments to Qwen2TokenizerFast's decode.
        """
        return self.tokenizer.decode(*args, **kwargs)

    @property
    def model_input_names(self):
        tokenizer_input_names = self.tokenizer.model_input_names
        image_processor_input_names = self.image_processor.model_input_names
        return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names))


AutoProcessor.register("BagelProcessor", BagelProcessor)