preprocess.py 24.2 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

4
from collections.abc import Mapping
5
from typing import Any, cast
6
7
8

from typing_extensions import assert_never

9
from vllm.config import ModelConfig, ObservabilityConfig
10
from vllm.logger import init_logger
11
from vllm.multimodal import MULTIMODAL_REGISTRY, MultiModalRegistry
12
from vllm.multimodal.cache import BaseMultiModalProcessorCache
13
14
15
16
17
18
from vllm.multimodal.inputs import (
    MultiModalDataDict,
    MultiModalEncDecInputs,
    MultiModalInputs,
    MultiModalUUIDDict,
)
19
from vllm.multimodal.processing import BaseMultiModalProcessor
20
from vllm.renderers import renderer_from_config
21
from vllm.tokenizers import TokenizerLike
22
from vllm.utils.jsontree import json_iter_leaves
23
from vllm.v1.metrics.stats import MultiModalCacheStats
24

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from .data import (
    DecoderOnlyInputs,
    EmbedsInputs,
    EmbedsPrompt,
    EncoderDecoderInputs,
    ExplicitEncoderDecoderPrompt,
    ProcessorInputs,
    PromptType,
    SingletonInputs,
    SingletonPrompt,
    TextPrompt,
    TokenInputs,
    TokensPrompt,
    embeds_inputs,
    token_inputs,
)
41
from .parse import is_explicit_encoder_decoder_prompt, parse_singleton_prompt
42
43
44
45
46
47
48

logger = init_logger(__name__)


class InputPreprocessor:
    def __init__(
        self,
49
        model_config: ModelConfig,
50
        observability_config: ObservabilityConfig | None = None,
51
        mm_registry: MultiModalRegistry = MULTIMODAL_REGISTRY,
52
        mm_processor_cache: BaseMultiModalProcessorCache | None = None,
53
54
55
    ) -> None:
        super().__init__()

56
        self.model_config = model_config
57
        self.observability_config = observability_config
58
        self.renderer = renderer_from_config(model_config)
59
        self.mm_registry = mm_registry
60
        self.mm_processor_cache = mm_processor_cache
61

62
63
        self.mm_cache_stats = MultiModalCacheStats() if mm_processor_cache else None

64
65
66
    @property
    def tokenizer(self) -> TokenizerLike | None:
        return self.renderer.tokenizer
67

68
69
    def get_tokenizer(self) -> TokenizerLike:
        return self.renderer.get_tokenizer()
70

71
    def get_bos_token_id(self) -> int | None:
72
        if self.tokenizer is None:
73
            logger.warning_once(
74
75
                "Using None for BOS token id because tokenizer is not initialized"
            )
76
77
            return None

78
        return self.tokenizer.bos_token_id
79

80
    def get_eos_token_id(self) -> int | None:
81
        if self.tokenizer is None:
82
            logger.warning_once(
83
84
                "Using None for EOS token id because tokenizer is not initialized"
            )
85
86
            return None

87
        return self.tokenizer.eos_token_id
88

89
    def get_decoder_start_token_id(self) -> int | None:
90
        """
91
92
93
        Obtain the decoder start token id employed by an encoder/decoder
        model. Returns None for non-encoder/decoder models or if the
        model config is unavailable.
94
        """
95

96
        if not self.model_config.is_encoder_decoder:
97
98
            logger.warning_once(
                "Using None for decoder start token id because "
99
100
                "this is not an encoder/decoder model."
            )
101
102
            return None

103
        if self.model_config is None or self.model_config.hf_config is None:
104
105
            logger.warning_once(
                "Using None for decoder start token id because "
106
107
                "model config is not available."
            )
108
109
            return None

110
111
112
        dec_start_token_id = getattr(
            self.model_config.hf_config, "decoder_start_token_id", None
        )
113
        if dec_start_token_id is None:
114
115
116
            logger.warning_once(
                "Falling back on <BOS> for decoder start token "
                "id because decoder start token id is not "
117
118
                "available."
            )
119
120
121
122
            dec_start_token_id = self.get_bos_token_id()

        return dec_start_token_id

123
    def _get_default_enc_dec_decoder_prompt(self) -> list[int]:
124
        """
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
        Specifically for encoder/decoder models:
        generate a default decoder prompt for when
        the user specifies only the encoder prompt.

        Encoder/decoder models utilize the decoder
        prompt in different ways; as new models are
        added, it is intended that this function
        will be extended to produce differing
        default decoder prompts, depending on the
        model variety.

        Absent a special case, the default behavior
        of this method is to mirror the behavior of
        the HuggingFace (HF) GenerationMixin for a None
        decoder prompt, which is to employ a logit processor
        setting to force the first decoded token to be <BOS>.
        Here, this behavior is approximated by having the
        "default" decoder prompt be <BOS>.

        However, it is possible that in the future
145
        other models may have different or more
146
147
148
149
150
151
152
        complex logic for the default decoder prompt.
        This motivates having a special helper method
        for default decoder prompts.

        Returns:

        * prompt_token_ids
153
        """
154
155
156
157
158
159
160

        bos_token_id = self.get_bos_token_id()
        assert bos_token_id is not None
        return [bos_token_id]

    def _prepare_decoder_input_ids_for_generation(
        self,
161
        decoder_input_ids: list[int] | None,
162
    ) -> list[int]:
163
164
165
        """
        Prepares `decoder_input_ids` for generation with encoder-decoder models.

166
167
168
169
        Based on:
        https://github.com/huggingface/transformers/blob/4037a2b5b1278736e566aec12e169100275545ea/src/transformers/generation/utils.py
        specifically,
        `GenerationMixin._prepare_decoder_input_ids_for_generation()`.
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187

        Arguments:

        * decoder_input_ids: input token ids to preprocess

        Returns:

        * Processed token list
        """

        decoder_start_token_id = self.get_decoder_start_token_id()
        assert decoder_start_token_id is not None

        if decoder_input_ids is None:
            # no decoder prompt input ->
            # use decoder_start_token_id as decoder_input_ids
            decoder_input_ids = self._get_default_enc_dec_decoder_prompt()

188
189
190
191
        if (
            len(decoder_input_ids) == 0
            or decoder_input_ids[0] != decoder_start_token_id
        ):
192
193
194
195
            decoder_input_ids = [decoder_start_token_id] + decoder_input_ids

        return decoder_input_ids

196
197
    def _get_tokenization_kw(
        self,
198
        overrides: dict[str, Any] | None = None,
199
200
201
    ) -> dict[str, Any]:
        kwargs = dict[str, Any]()

202
        if self.model_config.is_encoder_decoder:
203
204
205
206
207
208
209
210
211
212
            # For Whisper, special tokens should be provided by the user based
            # on the task and language of their request. Also needed to avoid
            # appending an EOS token to the prompt which disrupts generation.
            kwargs["add_special_tokens"] = False

        if overrides:
            kwargs.update(overrides)

        return kwargs

213
214
215
    def _tokenize_prompt(
        self,
        prompt: str,
216
        tokenization_kwargs: dict[str, Any] | None = None,
217
    ) -> list[int]:
218
219
220
221
        """
        Apply the model's tokenizer to a text prompt, returning the
        corresponding token IDs.
        """
222
        tokenizer = self.get_tokenizer()
223
        tokenization_kwargs = self._get_tokenization_kw(tokenization_kwargs)
224

225
        encoder_config = self.model_config.encoder_config
226

227
        if encoder_config and encoder_config.get("do_lower_case", False):
228
229
            prompt = prompt.lower()

230
        return tokenizer.encode(prompt, **tokenization_kwargs)
231

232
233
234
    def _get_mm_processor(self) -> BaseMultiModalProcessor:
        if not hasattr(self, "_mm_processor"):
            self._mm_processor = self.mm_registry.create_processor(
235
                self.model_config,
236
                self.observability_config,
237
                tokenizer=self.tokenizer,
238
239
240
241
                cache=self.mm_processor_cache,
            )

        return self._mm_processor
242

243
244
    def _process_multimodal(
        self,
245
        prompt: str | list[int],
246
        mm_data: MultiModalDataDict,
247
248
        mm_processor_kwargs: Mapping[str, object] | None,
        tokenization_kwargs: dict[str, Any] | None = None,
249
        *,
250
        mm_uuids: MultiModalUUIDDict | None = None,
251
    ) -> MultiModalInputs:
252
253
254
255
        """
        Apply the model's multi-modal processor to a multi-modal prompt,
        returning the corresponding token IDs and metadata.
        """
256
        mm_processor = self._get_mm_processor()
257

258
259
260
        if mm_processor_kwargs is None:
            mm_processor_kwargs = {}

261
        mm_input = mm_processor.apply(
262
263
264
265
            prompt,
            mm_data,
            hf_processor_mm_kwargs=mm_processor_kwargs,
            tokenization_kwargs=tokenization_kwargs,
266
            mm_uuids=mm_uuids,
267
        )
268
269
270
        mm_hashes = mm_input["mm_hashes"]

        # Validate that all mm items have a string as their hash
271
272
273
274
        contains_only_strings = all(
            isinstance(leaf, str) for leaf in json_iter_leaves(mm_hashes)
        )
        if not contains_only_strings:
275
276
277
            raise ValueError(
                f"mm_hashes must contain only strings, got: {mm_hashes}. "
                "This is likely due to an incorrect custom implementation of "
278
279
                "MultiModalProcessor.apply method."
            )
280
281

        return mm_input
282

283
284
285
286
    def _process_embeds(
        self,
        parsed_content: EmbedsPrompt,
    ) -> EmbedsInputs:
287
        if not self.model_config.enable_prompt_embeds:
288
289
290
            raise ValueError(
                "You must set `--enable-prompt-embeds` to input `prompt_embeds`."
            )
291
292

        prompt_embeds = parsed_content["prompt_embeds"]
293

294
295
296
297
298
299
300
301
        # prompt_embeds must be (seq_len, hidden_size), but if the user
        # passes in a batch of size 1, i.e. (1, seq_len, hidden_size),
        # we can unambiguously process the intent by squeezing the batch
        # dimension.
        if prompt_embeds.ndim == 3:
            prompt_embeds = prompt_embeds.squeeze(dim=0)

        if prompt_embeds.ndim != 2:
302
            raise ValueError("prompt_embeds must be of shape (seq_len, hidden_size).")
303

304
305
306
307
308
        # Tensors must be on CPU for serialization between processes
        # in the MsgpackEncoder. Casting to CPU here ensures that there is no
        # hidden device transfer in the critical path of generation.
        prompt_embeds = prompt_embeds.cpu()

309
310
311
        return embeds_inputs(
            prompt_embeds=prompt_embeds, cache_salt=parsed_content.get("cache_salt")
        )
312

313
    def _truncate_inputs(
314
        self, inputs: list[int], tokenization_kwargs: dict[str, Any] | None = None
315
316
317
318
319
320
    ) -> list[int]:
        if (
            not tokenization_kwargs
            or "truncation" not in tokenization_kwargs
            or self.tokenizer is None
        ):
321
322
323
324
325
326
327
328
329
            return inputs

        max_length = tokenization_kwargs["max_length"]

        if self.tokenizer.truncation_side == "left":
            return inputs[-max_length:]
        else:
            return inputs[:max_length]

330
331
332
    def _process_tokens(
        self,
        parsed_content: TokensPrompt,
333
        tokenization_kwargs: dict[str, Any] | None = None,
334
        *,
335
336
        mm_uuids: MultiModalUUIDDict | None = None,
    ) -> TokenInputs | MultiModalInputs:
337
        prompt_token_ids = self._truncate_inputs(
338
339
            parsed_content["prompt_token_ids"], tokenization_kwargs
        )
340

341
        inputs: TokenInputs | MultiModalInputs
342
        if multi_modal_data := parsed_content.get("multi_modal_data"):
343
344
            inputs = self._process_multimodal(
                prompt_token_ids,
345
                multi_modal_data,
346
                parsed_content.get("mm_processor_kwargs") or {},
347
                tokenization_kwargs=tokenization_kwargs,
348
                mm_uuids=mm_uuids,
349
            )
350
        else:
351
            inputs = token_inputs(prompt_token_ids)
352
353
354
355
356
357
358
359
360

        if cache_salt := parsed_content.get("cache_salt"):
            inputs["cache_salt"] = cache_salt

        return inputs

    def _process_text(
        self,
        parsed_content: TextPrompt,
361
        tokenization_kwargs: dict[str, Any] | None = None,
362
        *,
363
364
        mm_uuids: MultiModalUUIDDict | None = None,
    ) -> TokenInputs | MultiModalInputs:
365
366
        prompt_text = parsed_content["prompt"]

367
        inputs: TokenInputs | MultiModalInputs
368
        if multi_modal_data := parsed_content.get("multi_modal_data"):
369
370
            inputs = self._process_multimodal(
                prompt_text,
371
                multi_modal_data,
372
                parsed_content.get("mm_processor_kwargs") or {},
373
                tokenization_kwargs=tokenization_kwargs,
374
                mm_uuids=mm_uuids,
375
376
377
378
379
380
            )
        else:
            prompt_token_ids = self._tokenize_prompt(
                prompt_text,
                tokenization_kwargs=tokenization_kwargs,
            )
381
            inputs = token_inputs(prompt_token_ids)
382
383
384
385
386

        if cache_salt := parsed_content.get("cache_salt"):
            inputs["cache_salt"] = cache_salt

        return inputs
387

388
    def _prompt_to_llm_inputs(
389
        self,
390
        prompt: SingletonPrompt,
391
        tokenization_kwargs: dict[str, Any] | None = None,
392
        *,
393
        mm_uuids: MultiModalUUIDDict | None = None,
394
    ) -> SingletonInputs:
395
396
        """
        Extract the singleton inputs from a prompt.
397
398
399

        Arguments:

400
        * prompt: single encoder or decoder input prompt
401
402
403

        Returns:

404
        * [`SingletonInputs`][vllm.inputs.data.SingletonInputs] instance
405
        """
406
        parsed = parse_singleton_prompt(prompt)
407
408

        if parsed["type"] == "embeds":
409
410
411
412
            return self._process_embeds(parsed["content"])
        if parsed["type"] == "tokens":
            return self._process_tokens(
                parsed["content"],
413
                mm_uuids=mm_uuids,
414
            )
415
416
417
418
        if parsed["type"] == "text":
            return self._process_text(
                parsed["content"],
                tokenization_kwargs=tokenization_kwargs,
419
                mm_uuids=mm_uuids,
420
421
422
423
            )
        if parsed["type"] == "str":
            return self._process_text(
                TextPrompt(prompt=parsed["content"]),
424
                tokenization_kwargs=tokenization_kwargs,
425
                mm_uuids=mm_uuids,
426
            )
427

428
429
        assert_never(parsed)

430
431
    def _build_enc_dec_llm_inputs(
        self,
432
        encoder_inputs: SingletonInputs,
433
        decoder_inputs: SingletonInputs | None,
434
    ) -> EncoderDecoderInputs:
435
436
437
438
439
440
441
442
        if (
            encoder_inputs["type"] == "embeds"
            or decoder_inputs
            and decoder_inputs["type"] == "embeds"
        ):
            raise ValueError(
                "Embedding inputs are not supported for encoder-decoder models"
            )
443

444
        # Needed for mypy
445
446
        encoder_inputs = cast(TokenInputs | MultiModalInputs, encoder_inputs)
        decoder_inputs = cast(TokenInputs | MultiModalInputs | None, decoder_inputs)
447

448
        if decoder_inputs is None:
449
450
451
452
453
454
455
            if self.model_config.hf_config.model_type == "whisper":
                # For Whisper models, the text prompt should go to the decoder.
                # If no explicit encoder/decoder inputs, then copy the prompt
                # from the encoder to the decoder. The encoder tokens are later
                # overridden by the audio features.
                dec_token_ids = encoder_inputs["prompt_token_ids"].copy()
            else:
456
                dec_token_ids = self._prepare_decoder_input_ids_for_generation(None)
457
            decoder_inputs = token_inputs(dec_token_ids)
458
        else:
459
            if "multi_modal_data" in decoder_inputs:
460
461
462
463
                raise ValueError(
                    "Multi-modal decoder inputs of encoder-"
                    "decoder models are not supported yet"
                )
464
465

            dec_token_ids = self._prepare_decoder_input_ids_for_generation(
466
467
                decoder_inputs["prompt_token_ids"]
            )
468
            decoder_inputs["prompt_token_ids"] = dec_token_ids
469

470
        return EncoderDecoderInputs(
471
472
            encoder=encoder_inputs,
            decoder=decoder_inputs,
473
474
        )

475
    def _split_enc_dec_mm_inputs(
476
        self,
477
478
        inputs: SingletonInputs | MultiModalEncDecInputs,
        decoder_inputs_to_override: SingletonInputs | None = None,
479
    ) -> tuple[SingletonInputs, SingletonInputs]:
480
481
482
483
        """
        For encoder/decoder models only:
        Separate Encoder/Decoder inputs from a MultiModalEncDecInputs
        """
484
485
486
487
488
489
490
491
        if (
            inputs["type"] == "embeds"
            or decoder_inputs_to_override
            and decoder_inputs_to_override["type"] == "embeds"
        ):
            raise ValueError(
                "Embedding inputs are not supported for encoder-decoder models"
            )
492
493
494

        # Needed for mypy
        inputs = cast(
495
            TokenInputs | MultiModalInputs | MultiModalEncDecInputs,
496
497
498
            inputs,
        )
        decoder_inputs_to_override = cast(
499
            TokenInputs | MultiModalInputs | None,
500
501
502
            decoder_inputs_to_override,
        )

503
504
        encoder_inputs: SingletonInputs
        decoder_inputs: SingletonInputs
505
506

        if inputs["type"] == "multimodal":  # Multimodal data inputs
507
            if "encoder_prompt_token_ids" not in inputs:
508
509
510
511
512
                raise RuntimeError(
                    "You should register an encoder-decoder "
                    "multi-modal processor for encoder-decoder "
                    "models."
                )
513
            inputs = cast(MultiModalEncDecInputs, inputs)
514

515
            encoder_inputs = token_inputs(inputs["encoder_prompt_token_ids"])
516

517
518
519
520
521
522
523
524
525
            decoder_prompt_inputs = decoder_inputs_to_override or inputs
            decoder_inputs = MultiModalInputs(
                type="multimodal",
                prompt_token_ids=decoder_prompt_inputs["prompt_token_ids"],
                mm_kwargs=inputs["mm_kwargs"],
                mm_hashes=inputs["mm_hashes"],
                mm_placeholders=inputs["mm_placeholders"],
            )
            if cache_salt := inputs.get("cache_salt"):
526
527
                decoder_inputs["cache_salt"] = cache_salt

528
        elif inputs["type"] == "token":  # Text-only inputs
529
            encoder_inputs = token_inputs(prompt_token_ids=[])
530
531
532
            decoder_inputs = decoder_inputs_to_override or inputs
        else:
            assert_never(inputs)  # type: ignore[arg-type]
533

534
535
        return encoder_inputs, decoder_inputs

536
537
    def _process_encoder_decoder_prompt(
        self,
538
        prompt: PromptType,
539
        tokenization_kwargs: dict[str, Any] | None = None,
540
        *,
541
        mm_uuids: MultiModalUUIDDict | None = None,
542
    ) -> EncoderDecoderInputs:
543
        """
544
        For encoder/decoder models only:
545
546
547
        Process an input prompt into an
        [`EncoderDecoderInputs`][vllm.inputs.data.EncoderDecoderInputs]
        instance.
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565

        There are two types of input prompts:
        singleton prompts which carry only the
        encoder prompt, and explicit encoder/decoder
        prompts which carry both the encoder and the
        decoder prompts as member variables.

        This function handles the following scenarios:
        * Singleton encoder prompt: extract encoder prompt
          token ids & infer default decoder prompt token ids
        * Explicit encoder/decoder prompt: extract encoder
          and decoder prompt token ids

        Note that for Explicit encoder/decoder prompts,
        each sub-prompt (encoder or decoder prompt) can
        have any possible singleton type; thus this
        method relies on helper functions to obtain
        token ids for the sub-prompts.
566

567
568
        Arguments:

569
        * prompt: an input prompt
570
571
572

        Returns:

573
574
        * [`EncoderDecoderInputs`][vllm.inputs.data.EncoderDecoderInputs]
          instance
575
        """
576
        encoder_inputs: SingletonInputs
577
        decoder_inputs: SingletonInputs | None
578
        if is_explicit_encoder_decoder_prompt(prompt):
579
580
            # `cast` is needed for mypy, but not pyright
            prompt_ = cast(ExplicitEncoderDecoderPrompt, prompt)
581
            encoder_inputs = self._prompt_to_llm_inputs(
582
                prompt_["encoder_prompt"],
583
                tokenization_kwargs=tokenization_kwargs,
584
                mm_uuids=mm_uuids,
585
            )
586
            if (decoder_input := prompt_["decoder_prompt"]) is None:
587
                decoder_inputs = None
588
            else:
589
590
591
                decoder_inputs = self._prompt_to_llm_inputs(
                    decoder_input, tokenization_kwargs=tokenization_kwargs
                )
592
593
            # For multimodal model, override decoder prompt from processor
            # with explicit decoder prompt.
594
            if self.model_config.is_multimodal_model:
595
596
597
                encoder_inputs, decoder_inputs = self._split_enc_dec_mm_inputs(
                    encoder_inputs, decoder_inputs
                )
598
        else:
599
            # `cast` is needed for mypy, but not pyright
600
            inputs = self._prompt_to_llm_inputs(
601
                cast(SingletonPrompt, prompt),
602
                tokenization_kwargs=tokenization_kwargs,
603
                mm_uuids=mm_uuids,
604
            )
605
            if self.model_config.is_multimodal_model:
606
                # Encoder-Decoder Multimodal model
607
                encoder_inputs, decoder_inputs = self._split_enc_dec_mm_inputs(inputs)
608
609
610
            else:
                encoder_inputs = inputs
                decoder_inputs = None
611
612

        return self._build_enc_dec_llm_inputs(encoder_inputs, decoder_inputs)
613
614
615

    def _build_decoder_only_llm_inputs(
        self,
616
        prompt_inputs: DecoderOnlyInputs,
617
    ) -> DecoderOnlyInputs:
618
        if "prompt_token_ids" in prompt_inputs:
619
            prompt_inputs = cast(
620
                TokenInputs | MultiModalInputs, prompt_inputs
621
            )  # Needed for mypy
622

623
        return prompt_inputs
624
625
626

    def _process_decoder_only_prompt(
        self,
627
        prompt: SingletonPrompt,
628
        tokenization_kwargs: dict[str, Any] | None = None,
629
        *,
630
        mm_uuids: MultiModalUUIDDict | None = None,
631
    ) -> DecoderOnlyInputs:
632
        """
633
        For decoder-only models:
634
635
        Process an input prompt into a
        [`DecoderOnlyInputs`][vllm.inputs.data.DecoderOnlyInputs] instance.
636
637
638

        Arguments:

639
        * prompt: input prompt
640
641
642

        Returns:

643
        * [`DecoderOnlyInputs`][vllm.inputs.data.DecoderOnlyInputs] instance
644
        """
645

646
        prompt_comps = self._prompt_to_llm_inputs(
647
            prompt,
648
            tokenization_kwargs=tokenization_kwargs,
649
            mm_uuids=mm_uuids,
650
651
        )

652
        return self._build_decoder_only_llm_inputs(prompt_comps)
653

654
    def _preprocess(
655
        self,
656
        prompt: PromptType,
657
        tokenization_kwargs: dict[str, Any] | None = None,
658
        *,
659
        mm_uuids: MultiModalUUIDDict | None = None,
660
    ) -> ProcessorInputs:
661
        if self.model_config.is_encoder_decoder:
662
            # Encoder-decoder model requires special mapping of
663
            # input prompts to encoder & decoder.
664
            return self._process_encoder_decoder_prompt(
665
666
                prompt,
                tokenization_kwargs,
667
                mm_uuids=mm_uuids,
668
            )
669

670
        if is_explicit_encoder_decoder_prompt(prompt):
671
672
673
            raise ValueError(
                "Cannot pass encoder-decoder prompt to decoder-only models"
            )
674
675

        # Decoder-only operation
676
        # `cast` is needed for mypy, but not pyright
677
        return self._process_decoder_only_prompt(
678
            cast(SingletonPrompt, prompt),
679
            tokenization_kwargs=tokenization_kwargs,
680
            mm_uuids=mm_uuids,
681
682
        )

683
684
685
    def preprocess(
        self,
        prompt: PromptType,
686
        tokenization_kwargs: dict[str, Any] | None = None,
687
        *,
688
        mm_uuids: MultiModalUUIDDict | None = None,
689
690
    ) -> ProcessorInputs:
        """Preprocess the input prompt."""
691
        res = self._preprocess(prompt, tokenization_kwargs, mm_uuids=mm_uuids)
692
693
694
695
696
697
698
699
700

        if self.mm_processor_cache and self.mm_cache_stats is not None:
            delta = self.mm_processor_cache.make_stats(delta=True)
            self.mm_cache_stats.requests += 1
            self.mm_cache_stats.queries += delta.total
            self.mm_cache_stats.hits += delta.hits

        return res

701
    def stat_mm_cache(self) -> MultiModalCacheStats | None:
702
703
704
705
706
707
708
709
710
        mm_cache_stats = self.mm_cache_stats
        if mm_cache_stats is None:
            return None

        self.mm_cache_stats = MultiModalCacheStats()

        return mm_cache_stats

    def clear_mm_cache(self) -> None:
711
712
        if self.mm_processor_cache is not None:
            self.mm_processor_cache.clear_cache()
713
714
715

        if self.mm_cache_stats is not None:
            self.mm_cache_stats.reset = True