Delete processing_kimi_vl.py
Browse files- processing_kimi_vl.py +0 -170
processing_kimi_vl.py
DELETED
@@ -1,170 +0,0 @@
|
|
1 |
-
# coding=utf-8
|
2 |
-
# Copyright 2025 The Moonshot Team and HuggingFace Inc. team. All rights reserved.
|
3 |
-
#
|
4 |
-
# The code is based on the Qwen2VL processor (qwen2_vl/processing_qwen2_vl.py), but modified for KimiVL.
|
5 |
-
#
|
6 |
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
7 |
-
# you may not use this file except in compliance with the License.
|
8 |
-
# You may obtain a copy of the License at
|
9 |
-
#
|
10 |
-
# http://www.apache.org/licenses/LICENSE-2.0
|
11 |
-
#
|
12 |
-
# Unless required by applicable law or agreed to in writing, software
|
13 |
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
14 |
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15 |
-
# See the License for the specific language governing permissions and
|
16 |
-
# limitations under the License.
|
17 |
-
"""
|
18 |
-
Processor class for KimiVL.
|
19 |
-
"""
|
20 |
-
|
21 |
-
from typing import List, Union
|
22 |
-
|
23 |
-
from transformers.feature_extraction_utils import BatchFeature
|
24 |
-
from transformers.image_utils import ImageInput
|
25 |
-
from transformers.processing_utils import ProcessingKwargs, ProcessorMixin, Unpack, _validate_images_text_input_order
|
26 |
-
from transformers.tokenization_utils_base import PreTokenizedInput, TextInput
|
27 |
-
from transformers.utils import logging
|
28 |
-
|
29 |
-
|
30 |
-
logger = logging.get_logger(__name__)
|
31 |
-
|
32 |
-
|
33 |
-
class KimiVLProcessorKwargs(ProcessingKwargs, total=False):
|
34 |
-
_defaults = {
|
35 |
-
"text_kwargs": {
|
36 |
-
"padding": False,
|
37 |
-
},
|
38 |
-
"images_kwargs": {},
|
39 |
-
}
|
40 |
-
|
41 |
-
|
42 |
-
class KimiVLProcessor(ProcessorMixin):
|
43 |
-
r"""
|
44 |
-
Constructs a KimiVL processor which wraps a KimiVL image processor and a tokenizer into a single processor.
|
45 |
-
|
46 |
-
[`KimiVLProcessor`] offers all the functionalities of [`KimiVLImageProcessor`] and [`TikTokenTokenizer`]. See the
|
47 |
-
[`~KimiVLProcessor.__call__`] and [`~KimiVLProcessor.decode`] for more information.
|
48 |
-
|
49 |
-
Args:
|
50 |
-
image_processor ([`KimiVLImageProcessor`], *optional*):
|
51 |
-
The image processor is a required input.
|
52 |
-
tokenizer ([`TikTokenTokenizer`], *optional*):
|
53 |
-
The tokenizer is a required input.
|
54 |
-
chat_template (`str`, *optional*): A Jinja template which will be used to convert lists of messages
|
55 |
-
in a chat into a tokenizable string.
|
56 |
-
"""
|
57 |
-
|
58 |
-
attributes = ["image_processor", "tokenizer"]
|
59 |
-
valid_kwargs = [ "chat_template"]
|
60 |
-
image_processor_class = "AutoImageProcessor"
|
61 |
-
tokenizer_class = "AutoTokenizer"
|
62 |
-
|
63 |
-
def __init__(
|
64 |
-
self,
|
65 |
-
image_processor=None,
|
66 |
-
tokenizer=None,
|
67 |
-
chat_template=None,
|
68 |
-
**kwargs,
|
69 |
-
):
|
70 |
-
self.image_token = "<|media_pad|>"
|
71 |
-
super().__init__(image_processor, tokenizer, chat_template=chat_template)
|
72 |
-
|
73 |
-
def __call__(
|
74 |
-
self,
|
75 |
-
images: ImageInput = None,
|
76 |
-
text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None,
|
77 |
-
**kwargs: Unpack[KimiVLProcessorKwargs],
|
78 |
-
) -> BatchFeature:
|
79 |
-
"""
|
80 |
-
Main method to prepare for the model one or several sequences(s) and image(s). This method forwards the `text`
|
81 |
-
and `kwargs` arguments to TikTokenTokenizer's [`~TikTokenTokenizer.__call__`] if `text` is not `None` to encode
|
82 |
-
the text. To prepare the image(s), this method forwards the `images` and `kwrags` arguments to
|
83 |
-
CLIPImageProcessor's [`~CLIPImageProcessor.__call__`] if `images` is not `None`. Please refer to the docstring
|
84 |
-
of the above two methods for more information.
|
85 |
-
|
86 |
-
Args:
|
87 |
-
images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`):
|
88 |
-
The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch
|
89 |
-
tensor. Both channels-first and channels-last formats are supported.
|
90 |
-
text (`str`, `List[str]`, `List[List[str]]`):
|
91 |
-
The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings
|
92 |
-
(pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set
|
93 |
-
`is_split_into_words=True` (to lift the ambiguity with a batch of sequences).
|
94 |
-
return_tensors (`str` or [`~utils.TensorType`], *optional*):
|
95 |
-
If set, will return tensors of a particular framework. Acceptable values are:
|
96 |
-
- `'tf'`: Return TensorFlow `tf.constant` objects.
|
97 |
-
- `'pt'`: Return PyTorch `torch.Tensor` objects.
|
98 |
-
- `'np'`: Return NumPy `np.ndarray` objects.
|
99 |
-
- `'jax'`: Return JAX `jnp.ndarray` objects.
|
100 |
-
|
101 |
-
Returns:
|
102 |
-
[`BatchFeature`]: A [`BatchFeature`] with the following fields:
|
103 |
-
|
104 |
-
- **input_ids** -- List of token ids to be fed to a model. Returned when `text` is not `None`.
|
105 |
-
- **attention_mask** -- List of indices specifying which tokens should be attended to by the model (when
|
106 |
-
`return_attention_mask=True` or if *"attention_mask"* is in `self.model_input_names` and if `text` is not
|
107 |
-
`None`).
|
108 |
-
- **pixel_values** -- Pixel values to be fed to a model. Returned when `images` is not `None`.
|
109 |
-
"""
|
110 |
-
if images is None and text is None:
|
111 |
-
raise ValueError("You have to specify at least one of `images` or `text`.")
|
112 |
-
|
113 |
-
# check if images and text inputs are reversed for BC
|
114 |
-
images, text = _validate_images_text_input_order(images, text)
|
115 |
-
|
116 |
-
output_kwargs = self._merge_kwargs(
|
117 |
-
KimiVLProcessorKwargs,
|
118 |
-
tokenizer_init_kwargs=self.tokenizer.init_kwargs,
|
119 |
-
**kwargs,
|
120 |
-
)
|
121 |
-
if images is not None:
|
122 |
-
image_inputs = self.image_processor(images, **output_kwargs["images_kwargs"])
|
123 |
-
image_grid_hws = image_inputs["image_grid_hws"]
|
124 |
-
else:
|
125 |
-
image_inputs = {}
|
126 |
-
image_grid_hws = None
|
127 |
-
|
128 |
-
if isinstance(text, str):
|
129 |
-
text = [text]
|
130 |
-
elif not isinstance(text, list) and not isinstance(text[0], str):
|
131 |
-
raise ValueError("Invalid input text. Please provide a string, or a list of strings")
|
132 |
-
|
133 |
-
if image_grid_hws is not None:
|
134 |
-
merge_length = self.image_processor.merge_kernel_size[0] * self.image_processor.merge_kernel_size[1]
|
135 |
-
index = 0
|
136 |
-
for i in range(len(text)):
|
137 |
-
while self.image_token in text[i]:
|
138 |
-
text[i] = text[i].replace(
|
139 |
-
self.image_token,
|
140 |
-
"<|placeholder|>" * (image_grid_hws[index].prod() // merge_length),
|
141 |
-
1,
|
142 |
-
)
|
143 |
-
index += 1
|
144 |
-
text[i] = text[i].replace("<|placeholder|>", self.image_token)
|
145 |
-
|
146 |
-
text_inputs = self.tokenizer(text, **output_kwargs["text_kwargs"])
|
147 |
-
return BatchFeature(data={**text_inputs, **image_inputs})
|
148 |
-
|
149 |
-
def batch_decode(self, *args, **kwargs):
|
150 |
-
"""
|
151 |
-
This method forwards all its arguments to LlamaTokenizerFast's [`~PreTrainedTokenizer.batch_decode`]. Please
|
152 |
-
refer to the docstring of this method for more information.
|
153 |
-
"""
|
154 |
-
return self.tokenizer.batch_decode(*args, **kwargs)
|
155 |
-
|
156 |
-
def decode(self, *args, **kwargs):
|
157 |
-
"""
|
158 |
-
This method forwards all its arguments to LlamaTokenizerFast's [`~PreTrainedTokenizer.decode`]. Please refer to
|
159 |
-
the docstring of this method for more information.
|
160 |
-
"""
|
161 |
-
return self.tokenizer.decode(*args, **kwargs)
|
162 |
-
|
163 |
-
@property
|
164 |
-
def model_input_names(self):
|
165 |
-
tokenizer_input_names = self.tokenizer.model_input_names
|
166 |
-
image_processor_input_names = self.image_processor.model_input_names
|
167 |
-
return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names))
|
168 |
-
|
169 |
-
|
170 |
-
__all__ = ["KimiVLProcessorKwargs"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|