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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import re
from os import PathLike
from typing import cast

import numpy as np
import transformers.image_transforms as image_transforms
import transformers.image_utils as image_utils
import transformers.video_utils as video_utils
from PIL.Image import Image
from transformers.feature_extraction_utils import BatchFeature
from transformers.image_utils import ImageInput
from transformers.models.qwen2 import Qwen2Tokenizer, Qwen2TokenizerFast
from transformers.models.siglip import SiglipImageProcessor, SiglipImageProcessorFast
from transformers.processing_utils import ProcessingKwargs, ProcessorMixin, Unpack, VideosKwargs
from transformers.tokenization_utils_base import BatchEncoding, TextInput
from transformers.video_utils import VideoInput, VideoMetadata


class NVILAProcessorKwargs(ProcessingKwargs, total=False):
    _defaults = {}  # type: ignore


class NVILAProcessor(ProcessorMixin):
    attributes = [
        "image_processor",
        "tokenizer",
    ]
    image_processor_class = "AutoImageProcessor"
    tokenizer_class = "AutoTokenizer"
    _auto_class = "AutoProcessor"

    def __init__(
        self,
        image_processor: SiglipImageProcessor | SiglipImageProcessorFast,
        tokenizer: Qwen2Tokenizer | Qwen2TokenizerFast,
        chat_template: str | None = None,
        **kwargs,
    ):
        super().__init__(
            image_processor,
            tokenizer,
            chat_template=chat_template,
            **kwargs,
        )

        self.image_processor: SiglipImageProcessor | SiglipImageProcessorFast
        self.tokenizer: Qwen2Tokenizer | Qwen2TokenizerFast

    def __call__(
        self,
        *,
        text: TextInput | list[TextInput],
        images: ImageInput | None = None,
        videos: VideoInput | None = None,
        **kwargs: Unpack[NVILAProcessorKwargs],
    ) -> BatchFeature:
        normalized_text, normalized_images, normalized_videos = self._normalize_inputs(
            text=text,
            images=images,
            videos=videos,
        )

        images_inputs, image_token_padding_strategy = (
            self._preprocess_images(
                normalized_images,
                **kwargs,
            )
            if len(normalized_images) > 0
            else (BatchFeature(), [])
        )

        videos_inputs, video_token_padding_strategy = (
            self._preprocess_videos(
                normalized_videos,
                **kwargs,
            )
            if len(normalized_videos) > 0
            else (BatchFeature(), [])
        )

        text_inputs = self._preprocess_text(
            normalized_text,
            image_token_padding_strategy=image_token_padding_strategy,
            video_token_padding_strategy=video_token_padding_strategy,
            **kwargs,
        )

        return BatchFeature(
            {
                **text_inputs,
                **images_inputs,
                **videos_inputs,
            }
        )

    def batch_decode(self, *args, **kwargs) -> list[str]:
        return self.tokenizer.batch_decode(*args, **kwargs)

    def _normalize_inputs(
        self,
        *,
        text: TextInput | list[TextInput],
        images: ImageInput | None,
        videos: VideoInput | None,
    ) -> tuple[list[str], list[Image], list[list[Image]]]:
        if isinstance(text, list):
            normalized_text = text
        else:
            normalized_text = [text]

        if images is not None and images != []:
            image_flat_list = cast(list, image_utils.make_flat_list_of_images(images))
            normalized_images = [cast(Image, image_transforms.to_pil_image(image)) for image in image_flat_list]
        else:
            normalized_images = []

        if videos is not None and videos != []:
            video_list = cast(list[list], video_utils.make_batched_videos(videos))
            normalized_videos = [
                [cast(Image, image_transforms.to_pil_image(image)) for image in video] for video in video_list
            ]
        else:
            normalized_videos = []

        return normalized_text, normalized_images, normalized_videos

    def _preprocess_images(
        self,
        images: list[Image],
        **kwargs: Unpack[NVILAProcessorKwargs],
    ) -> tuple[BatchFeature, list[list[int]]]:
        merged_kwargs = self._merge_kwargs(
            NVILAProcessorKwargs,  # type: ignore
            tokenizer_init_kwargs=self.tokenizer.init_kwargs,
            **kwargs,
        )

        images = [image.convert("RGB") for image in images]

        if len(images) == 1:
            assert self.image_processor.size["height"] == self.image_processor.size["width"]

            images, block_size = dynamic_s2_preprocess(
                images[0],
                s2_scales=[448, 896, 1344],
                max_num=12,
                image_size=self.image_processor.size["height"],
            )

            pixel_values = self.image_processor(
                images,
                **merged_kwargs["images_kwargs"],
            )["pixel_values"]

            images_inputs = BatchFeature(
                {
                    "block_sizes": [block_size],
                    "pixel_values": pixel_values,
                }
            )

            padding_strategy = [[block_size[0] * block_size[1] * 256]]

        else:
            pixel_values = self.image_processor(
                images,
                **merged_kwargs["images_kwargs"],
            )["pixel_values"]

            images_inputs = BatchFeature(
                {
                    "pixel_values": pixel_values,
                }
            )

            padding_strategy = [[256]] * len(images)

        return images_inputs, padding_strategy

    def _preprocess_text(
        self,
        text: list[str],
        *,
        image_token_padding_strategy: list[list[int]],
        video_token_padding_strategy: list[list[int]],
        **kwargs: Unpack[NVILAProcessorKwargs],
    ) -> BatchEncoding:
        # Pad media tokens.
        assert isinstance(self.tokenizer.image_token, str)
        assert isinstance(self.tokenizer.video_token, str)

        for media_token, padding_strategy in (
            (self.tokenizer.image_token, image_token_padding_strategy),
            (self.tokenizer.video_token, video_token_padding_strategy),
        ):
            assert sum([s.count(media_token) for s in text]) == len(padding_strategy)

            # Pad to number of tiles.
            pad_lens = [len(x) for x in padding_strategy]
            text = [re.sub(rf"({re.escape(media_token)})", lambda _: media_token * pad_lens.pop(0), s) for s in text]

            # Pad to number of features.
            pad_lens = [y for x in padding_strategy for y in x]
            pad_lens = [x + 1 for x in pad_lens]  # Reserve for lf ending.
            text = [re.sub(rf"({re.escape(media_token)})", lambda _: media_token * pad_lens.pop(0), s) for s in text]

        merged_kwargs = self._merge_kwargs(
            NVILAProcessorKwargs,  # type: ignore
            tokenizer_init_kwargs=self.tokenizer.init_kwargs,
            **kwargs,
        )

        text_inputs = self.tokenizer(
            text=text,
            **merged_kwargs["text_kwargs"],
        )

        # Replace last token id of every image tile with lf token id.
        lf_token_id = self.tokenizer.encode("\n")[0]
        assert isinstance(self.tokenizer.image_token_id, int)
        assert isinstance(self.tokenizer.video_token_id, int)

        input_ids = text_inputs.input_ids

        for media_token_id, padding_strategy in (
            (self.tokenizer.image_token_id, image_token_padding_strategy),
            (self.tokenizer.video_token_id, video_token_padding_strategy),
        ):
            pad_lens = [y for x in padding_strategy for y in x]

            for i in range(len(input_ids)):
                j = 0
                while j < len(input_ids[i]):
                    if input_ids[i][j] != media_token_id:
                        j += 1
                        continue

                    j += pad_lens.pop(0)
                    input_ids[i][j] = lf_token_id

                    j += 1

        return text_inputs

    def _preprocess_videos(
        self,
        videos: list[list[Image]],
        **kwargs: Unpack[NVILAProcessorKwargs],
    ) -> tuple[BatchFeature, list[list[int]]]:
        merged_kwargs = self._merge_kwargs(
            NVILAProcessorKwargs,  # type: ignore
            tokenizer_init_kwargs=self.tokenizer.init_kwargs,
            **kwargs,
        )

        # Support sampling frames.
        if merged_kwargs["videos_kwargs"].get("do_sample_frames"):
            videos = [
                self._sample_frames(
                    video,
                    **merged_kwargs["videos_kwargs"],
                )
                for video in videos
            ]

        videos = [[image.convert("RGB") for image in video] for video in videos]

        frames = [image for video in videos for image in video]
        pixel_values_videos = self.image_processor(
            frames,
            **merged_kwargs["images_kwargs"],
        )["pixel_values"]

        videos_inputs = BatchFeature(
            {
                "pixel_values_videos": pixel_values_videos,
            }
        )

        padding_strategy = [[256] * len(video) for video in videos]

        return videos_inputs, padding_strategy

    def _sample_frames(
        self,
        video: list[Image],
        **kwargs: Unpack[VideosKwargs],
    ) -> list[Image]:
        fps = kwargs.get("fps")
        num_frames = kwargs.get("num_frames")

        if num_frames is not None and fps is None:
            indices = np.round(np.linspace(0, len(video) - 1, num_frames)).astype(int)

            return [video[i] for i in indices]

        elif num_frames is None and fps is not None:
            video_metadata = kwargs.get("video_metadata")

            if isinstance(video_metadata, VideoMetadata):
                total_num_frames = video_metadata.total_num_frames
                duration = video_metadata.duration

            elif isinstance(video_metadata, dict):
                total_num_frames = video_metadata.get("total_num_frames")
                duration = video_metadata.get("duration")

                assert total_num_frames is not None
                assert duration is not None

            else:
                raise NotImplementedError

            indices = np.round(np.linspace(0, total_num_frames - 1, int(fps * duration))).astype(int)

            return [video[i] for i in indices]

        else:
            raise NotImplementedError


# NOTE: The following functions are directly copied from VILA codebase.


def dynamic_s2_preprocess(image, s2_scales=[384, 768, 1152], max_num=12, image_size=384):
    orig_width, orig_height = image.size
    aspect_ratio = orig_width / orig_height
    min_num = (s2_scales[-1] // s2_scales[0]) ** 2  # at least use number of tiles as the largest scale

    processed_images = []

    ##########################################################################################
    ############# Add tiles for all but the last scale using fixed squre ratio ###############
    ##########################################################################################

    for scale in s2_scales[:-1]:
        target_width = image_size * (scale // s2_scales[0])
        target_height = image_size * (scale // s2_scales[0])
        blocks = (scale // s2_scales[0]) ** 2

        # resize the image
        resized_img = image.resize((target_width, target_height))
        for i in range(blocks):
            box = (
                (i % (target_width // image_size)) * image_size,
                (i // (target_width // image_size)) * image_size,
                ((i % (target_width // image_size)) + 1) * image_size,
                ((i // (target_width // image_size)) + 1) * image_size,
            )
            # split the image
            split_img = resized_img.crop(box)
            processed_images.append(split_img)

    ##########################################################################################
    ################ Add tiles for the last scale using dynamic aspect ratio #################
    ##########################################################################################

    # calculate the existing image aspect ratio
    target_ratios = {
        (i, j)
        for n in range(min_num, max_num + 1)
        for i in range(1, n + 1)
        for j in range(1, n + 1)
        if i * j <= max_num and i * j >= min_num
    }
    target_ratios = sorted(target_ratios, key=lambda x: x[0] * x[1])

    # find the closest aspect ratio to the target
    target_aspect_ratio = find_closest_aspect_ratio(aspect_ratio, target_ratios, orig_width, orig_height, image_size)

    # calculate the target width and height
    target_width = image_size * target_aspect_ratio[0]
    target_height = image_size * target_aspect_ratio[1]
    blocks = target_aspect_ratio[0] * target_aspect_ratio[1]

    # resize the image
    resized_img = image.resize((target_width, target_height))
    for i in range(blocks):
        box = (
            (i % (target_width // image_size)) * image_size,
            (i // (target_width // image_size)) * image_size,
            ((i % (target_width // image_size)) + 1) * image_size,
            ((i // (target_width // image_size)) + 1) * image_size,
        )
        # split the image
        split_img = resized_img.crop(box)
        processed_images.append(split_img)

    return processed_images, (target_aspect_ratio[1], target_aspect_ratio[0])


def find_closest_aspect_ratio(
    aspect_ratio: float, target_ratios: list[tuple[int, int]], width: int, height: int, image_size: int
) -> tuple[int, int]:
    best_ratio_diff = float("inf")
    best_ratio = (1, 1)
    area = width * height
    for ratio in target_ratios:
        target_aspect_ratio = ratio[0] / ratio[1]
        ratio_diff = abs(aspect_ratio - target_aspect_ratio)
        if ratio_diff < best_ratio_diff:
            best_ratio_diff = ratio_diff
            best_ratio = ratio
        elif ratio_diff == best_ratio_diff:
            if area > 0.5 * image_size * image_size * ratio[0] * ratio[1]:
                best_ratio = ratio
    return best_ratio