"vllm/vscode:/vscode.git/clone" did not exist on "e85829450d8016309d71de9f347e2147ee03400a"
processing.py 35 KB
Newer Older
1
import pickle
2
3
import re
from abc import ABC, abstractmethod
4
from collections import defaultdict
5
from collections.abc import Callable, ItemsView, Iterable, Mapping, Sequence
6
from dataclasses import dataclass, field
7
from functools import lru_cache
8
from typing import Any, NamedTuple, Optional, Protocol, TypeVar, Union
9

10
import numpy as np
11
import numpy.typing as npt
12
import torch
13
from blake3 import blake3
14
from PIL import Image
15
from transformers import BatchFeature, ProcessorMixin
16

17
from vllm.inputs import DummyData, InputProcessingContext
18
from vllm.logger import init_logger
19
from vllm.transformers_utils.tokenizer import AnyTokenizer, MistralTokenizer
20
from vllm.utils import LRUCache, flatten_2d_lists, full_groupby
21

22
23
24
25
from .inputs import (MultiModalDataDict, MultiModalFieldConfig,
                     MultiModalFieldItem, MultiModalInputsV2, MultiModalKwargs,
                     PlaceholderRange)
from .parse import MultiModalDataItems, MultiModalDataParser
26

27
logger = init_logger(__name__)
28
29

_S = TypeVar("_S", str, list[int])
30
_PromptSeq = Union[str, list[int]]
31

32
33

@dataclass
34
35
class PromptReplacement:
    modality: str
36
    """The modality for which the replacement is made."""
37

38
39
    target: _PromptSeq
    """The text or token sequence to find and replace."""
40

41
42
    replacement: Union[Callable[[int], _PromptSeq],
                       _PromptSeq] = field(repr=False)
43
    """
44
45
    Given the index of the processed item within :attr:`modality`, output the
    replacement text or token sequence.
46

47
48
    For convenience, you can pass in the replacement instead of a function
    if it does not depend on the input.
49
50
    """

51
    def bind(self, tokenizer: AnyTokenizer) -> "_BoundPromptReplacement":
52
        return _BoundPromptReplacement(
53
54
55
56
            tokenizer=tokenizer,
            modality=self.modality,
            _target=self.target,
            _replacement=self.replacement,
57
        )
58
59


60
61
62
63
64
65
66
67
68
69
70
71
72
73
def _encode(
    tokenizer: AnyTokenizer,
    text: str,
    *,
    add_special_tokens: bool = False,
) -> list[int]:
    """
    Backend-agnostic equivalent of HF's
    :code:`tokenizer.encode(text, add_special_tokens=...)`.
    """
    if isinstance(tokenizer, MistralTokenizer):
        return tokenizer.tokenizer.encode(text,
                                          bos=add_special_tokens,
                                          eos=add_special_tokens)
74

75
    return tokenizer.encode(text, add_special_tokens=add_special_tokens)
76
77


78
79
80
81
82
83
84
85
@lru_cache(maxsize=2048)
def _cached_encode(
    tokenizer: AnyTokenizer,
    text: str,
    *,
    add_special_tokens: bool = False,
) -> list[int]:
    return _encode(tokenizer, text, add_special_tokens=add_special_tokens)
86
87


88
89
90
91
92
93
94
95
96
97
98
def _decode(
    tokenizer: AnyTokenizer,
    token_ids: list[int],
    *,
    skip_special_tokens: bool = False,
) -> str:
    """
    Backend-agnostic equivalent of HF's
    :code:`tokenizer.decode(token_ids, skip_special_tokens=...)`.
    """
    return tokenizer.decode(token_ids, skip_special_tokens=skip_special_tokens)
99
100


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
@lru_cache(maxsize=2048)
def _cached_decode(
    tokenizer: AnyTokenizer,
    token_ids: tuple[int, ...],
    *,
    skip_special_tokens: bool = False,
) -> str:
    return _decode(tokenizer,
                   list(token_ids),
                   skip_special_tokens=skip_special_tokens)


class _HasModalityAttr(Protocol):
    modality: str

116

117
class _HasModalityProp(Protocol):
118

119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
    @property
    def modality(self) -> str:
        ...


_M = TypeVar("_M", bound=Union[_HasModalityAttr, _HasModalityProp])


def full_groupby_modality(values: Iterable[_M]) -> ItemsView[str, list[_M]]:
    """Convenience function to apply :func:`full_groupby` based on modality."""
    return full_groupby(values, key=lambda x: x.modality)


@dataclass
class _BoundPromptSequence:
134
135
    tokenizer: AnyTokenizer = field(repr=False)

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
    _text: Optional[str]
    _token_ids: Optional[list[int]]

    def __post_init__(self) -> None:
        if self._text is None and self._token_ids is None:
            raise ValueError("At least one of 'text' and 'token_ids' must be "
                             "specified")

    @property
    def text(self) -> str:
        if self._text is None:
            assert self._token_ids is not None
            self._text = _cached_decode(self.tokenizer, tuple(self._token_ids))

        return self._text

    @property
    def token_ids(self) -> list[int]:
        if self._token_ids is None:
            assert self._text is not None
            self._token_ids = _cached_encode(self.tokenizer, self._text)

        return self._token_ids


@dataclass
162
163
class _BoundPromptReplacement:
    tokenizer: AnyTokenizer = field(repr=False)
164
165
    modality: str

166
167
168
    _target: _PromptSeq
    _replacement: Union[Callable[[int], _PromptSeq],
                        _PromptSeq] = field(repr=False)
169

170
171
172
173
174
175
    def __post_init__(self) -> None:
        self._replacement_cache = dict[int, _BoundPromptSequence]()

    @property
    def target(self) -> _BoundPromptSequence:
        target = self._target
176

177
178
179
180
181
        return _BoundPromptSequence(
            tokenizer=self.tokenizer,
            _text=target if isinstance(target, str) else None,
            _token_ids=target if isinstance(target, list) else None,
        )
182

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
    def get_replacement(self, item_idx: int) -> _BoundPromptSequence:
        replacement = self._replacement
        if callable(replacement):
            cache_key = item_idx
            if cache_key in self._replacement_cache:
                return self._replacement_cache[cache_key]

            replacement = replacement(item_idx)
        else:
            cache_key = None

        bound_replacement = _BoundPromptSequence(
            tokenizer=self.tokenizer,
            _text=replacement if isinstance(replacement, str) else None,
            _token_ids=replacement if isinstance(replacement, list) else None,
        )

        if cache_key is not None:
            self._replacement_cache[cache_key] = bound_replacement

        return bound_replacement


206
207
208
class _TokenMatch(NamedTuple):
    start_idx: int
    end_idx: int
209
210


211
212
213
214
def iter_token_matches(
    token_ids: list[int],
    match_ids: list[int],
) -> Iterable[_TokenMatch]:
215
216
217
218
219
220
    """
    Yield each occurrence of :code:`match_ids` in :code:`token_ids`.

    Note that empty matches are ignored.
    """
    prompt_len = len(token_ids)
221
    match_len = len(match_ids)
222

223
224
    if match_len == 0:
        return
225

226
227
    start_idx = 0
    while start_idx < prompt_len - match_len + 1:
228
        end_idx = start_idx + match_len
229

230
231
        if token_ids[start_idx:end_idx] == match_ids:
            yield _TokenMatch(start_idx=start_idx, end_idx=end_idx)
232
233
234
235
236

            # Exclude overlapping matches
            start_idx = end_idx
        else:
            start_idx += 1
237
238


239
240
241
@dataclass(repr=False)
class _PromptReplacementMatch(ABC):
    prompt_repl: _BoundPromptReplacement
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262

    @property
    def modality(self) -> str:
        return self.prompt_repl.modality

    @property
    @abstractmethod
    def start_idx(self) -> int:
        raise NotImplementedError

    @property
    @abstractmethod
    def end_idx(self) -> int:
        raise NotImplementedError

    def __repr__(self) -> str:
        return (f"{type(self).__name__}(modality={self.modality!r}, "
                f"start_idx={self.start_idx!r}, end_idx={self.end_idx!r})")


@dataclass(repr=False)
263
class _PromptReplacementTokenMatch(_PromptReplacementMatch):
264
265
266
267
268
269
270
271
272
273
274
275
    match: _TokenMatch

    @property
    def start_idx(self) -> int:
        return self.match.start_idx

    @property
    def end_idx(self) -> int:
        return self.match.end_idx


@dataclass(repr=False)
276
class _PromptReplacementTextMatch(_PromptReplacementMatch):
277
278
279
280
281
282
283
284
285
286
    match: re.Match[str]

    @property
    def start_idx(self) -> int:
        return self.match.start()

    @property
    def end_idx(self) -> int:
        return self.match.end()

287
288
289
290

class _PlaceholderInfo(NamedTuple):
    modality: str
    start_idx: int
291
    replacement: list[int]
292
293
294

    @property
    def length(self) -> int:
295
        return len(self.replacement)
296
297
298
299
300
301

    def to_range(self) -> PlaceholderRange:
        return PlaceholderRange(
            offset=self.start_idx,
            length=self.length,
        )
302
303
304
305


def find_token_matches(
    prompt: list[int],
306
307
    prompt_repls: Sequence[_BoundPromptReplacement],
) -> list[_PromptReplacementTokenMatch]:
308
309
310
311
312
313
314
315
316
317
    """Return each target of :code:`prompt_repls` found in :code:`prompt`."""
    return [
        _PromptReplacementTokenMatch(prompt_repl, match)
        for prompt_repl in prompt_repls
        for match in iter_token_matches(prompt, prompt_repl.target.token_ids)
    ]


def find_text_matches(
    prompt: str,
318
319
    prompt_repls: Sequence[_BoundPromptReplacement],
) -> list[_PromptReplacementTextMatch]:
320
321
322
323
324
325
326
327
328
    """Return each target of :code:`prompt_repls` found in :code:`prompt`."""
    return [
        _PromptReplacementTextMatch(prompt_repl, match)
        for prompt_repl in prompt_repls
        for match in re.finditer(re.escape(prompt_repl.target.text), prompt)
    ]


def _resolve_matches(
329
330
331
    prompt: _PromptSeq,
    matches: Sequence[_PromptReplacementMatch],
) -> list[_PromptReplacementMatch]:
332
333
334
    """
    Resolve :code:`matches` to ensure that there are no overlapping matches,
    and sort them such that earlier matches take priority over later ones.
335
    """
336
337
    seen_matches: list[Optional[_PromptReplacementMatch]] = [None
                                                             ] * len(prompt)
338

339
    for match in matches:
340
341
342
343
344
        for idx in range(match.start_idx, match.end_idx):
            if seen_matches[idx] is not None:
                raise ValueError("Found overlapping matches "
                                 f"({seen_matches[idx]} and {match}) "
                                 f"at index={idx} of prompt={prompt}")
345

346
            seen_matches[idx] = match
347
348
349
350
351
352

    return sorted(matches, key=lambda x: x.start_idx)


def _replace_matches(
    prompt: _S,
353
    matches: Sequence[_PromptReplacementMatch],
354
    mm_item_counts: Mapping[str, int],
355
356
357
) -> list[_S]:
    out_seqs = list[_S]()
    prev_end_idx = 0
358
    next_idx_by_modality = defaultdict[str, int](lambda: 0)
359
360
361
362
363

    for match in _resolve_matches(prompt, matches):
        modality = match.modality

        item_idx = next_idx_by_modality[modality]
364
        if item_idx >= mm_item_counts.get(modality, 0):
365
366
367
368
            continue

        start_idx = match.start_idx
        end_idx = match.end_idx
369

370
        repl_info = match.prompt_repl
371
372
373
374
375
376
377
378
        replacement = repl_info.get_replacement(item_idx)

        if isinstance(prompt, str):
            repl_seq = replacement.text
            out_seqs.append(prompt[prev_end_idx:start_idx] + repl_seq)
        else:
            repl_seq = replacement.token_ids
            out_seqs.append(prompt[prev_end_idx:start_idx] + repl_seq)
379
380
381
382
383
384
385
386
387
388
389

        prev_end_idx = end_idx
        next_idx_by_modality[modality] += 1

    out_seqs.append(prompt[prev_end_idx:])

    return out_seqs


def replace_token_matches(
    prompt: list[int],
390
    matches: Sequence[_PromptReplacementTokenMatch],
391
    mm_item_counts: Mapping[str, int],
392
393
394
395
396
) -> list[int]:
    """Apply :code:`prompt_repls` to :code:`prompt`."""
    if not matches:
        return prompt

397
    token_id_seqs = _replace_matches(prompt, matches, mm_item_counts)
398
399

    return flatten_2d_lists(token_id_seqs)
400
401


402
403
def replace_text_matches(
    prompt: str,
404
    matches: Sequence[_PromptReplacementTextMatch],
405
    mm_item_counts: Mapping[str, int],
406
407
408
409
) -> str:
    """Apply :code:`prompt_repls` to :code:`prompt`."""
    if not matches:
        return prompt
410

411
    texts = _replace_matches(prompt, matches, mm_item_counts)
412
413

    return "".join(texts)
414
415


416
417
418
419
def _iter_modality_placeholders(
    prompt: list[int],
    modality: str,
    modality_repls: Sequence[_BoundPromptReplacement],
420
    modal_item_count: int,
421
) -> Iterable[_PlaceholderInfo]:
422
    if modal_item_count == 0:
423
        return
424

425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
    prompt_len = len(prompt)
    item_index = 0

    start_idx = 0
    while start_idx < prompt_len:
        found = False

        for repl_info in modality_repls:
            replacement = repl_info.get_replacement(item_index)
            repl_tokens = replacement.token_ids
            repl_len = len(repl_tokens)
            end_idx = start_idx + repl_len

            if repl_len == 0 or end_idx > prompt_len:
                continue
440

441
442
443
444
445
446
447
448
            if prompt[start_idx:end_idx] == repl_tokens:
                yield _PlaceholderInfo(
                    modality=modality,
                    start_idx=start_idx,
                    replacement=repl_tokens,
                )

                item_index += 1
449
                if item_index >= modal_item_count:
450
451
452
453
454
455
456
457
458
                    return

                # Exclude overlapping matches
                start_idx = end_idx
                found = True
                break

        if not found:
            start_idx += 1
459
460
461


def iter_placeholders(
462
    prompt_repls: Sequence[_BoundPromptReplacement],
463
    prompt: list[int],
464
    mm_item_counts: Mapping[str, int],
465
) -> Iterable[_PlaceholderInfo]:
466
467
468
469
470
471
472
    """
    Yield each set of placeholder tokens found in :code:`prompt`.

    Note that empty matches are ignored.
    """
    repls_by_modality = dict(full_groupby_modality(prompt_repls))

473
    for modality, modal_item_count in mm_item_counts.items():
474
475
476
477
478
        if modality in repls_by_modality:
            yield from _iter_modality_placeholders(
                prompt,
                modality,
                repls_by_modality[modality],
479
                modal_item_count,
480
481
            )

482

483
484
485
@dataclass
class ProcessorInputs:
    """Keyword arguments to :meth:`BaseMultiModalProcessor`."""
486
487
    prompt_text: str
    mm_data: MultiModalDataDict
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
    hf_processor_mm_kwargs: Mapping[str, object] = field(default_factory=dict)


class ProcessingCache:

    def __init__(self, capacity: int) -> None:
        super().__init__()

        # DEBUG: Set to None to disable
        self.debug_cache_hit_ratio_steps: Optional[int] = None

        self._cache = LRUCache[str, Mapping[str,
                                            MultiModalFieldItem]](capacity)

    def _maybe_log_cache_stats(self) -> None:
        steps = self.debug_cache_hit_ratio_steps
        if not steps:
            return

        cache_stats = self._cache.stat()
        if cache_stats.total % steps == 0:
            logger.debug("ProcessingCache: hit_ratio = %.2f",
                         cache_stats.hit_ratio)

    def _serialize_item(self, obj: object) -> bytes:
        # Simple cases
        if isinstance(obj, str):
            return obj.encode("utf-8")
        if isinstance(obj, bytes):
            return obj
518
        if isinstance(obj, Image.Image):
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
            return obj.tobytes()

        # Convertible to NumPy arrays
        if isinstance(obj, torch.Tensor):
            obj = obj.numpy()
        if isinstance(obj, (int, float)):
            obj = np.array(obj)
        if isinstance(obj, np.ndarray):
            return obj.tobytes()

        logger.warning(
            "No serialization method found for %s. "
            "Falling back to pickle.", type(obj))

        return pickle.dumps(obj)

    def _item_to_bytes(
        self,
        key: str,
        obj: object,
    ) -> Iterable[tuple[bytes, bytes]]:
        # Recursive cases
        if isinstance(obj, (list, tuple)):
            for i, elem in enumerate(obj):
                yield from self._item_to_bytes(f"{key}.{i}", elem)
        elif isinstance(obj, dict):
            for k, v in obj.items():
                yield from self._item_to_bytes(f"{key}.{k}", v)
        else:
            key_bytes = self._serialize_item(key)
            value_bytes = self._serialize_item(obj)
            yield key_bytes, value_bytes

    def _hash_kwargs(self, **kwargs: object) -> str:
        hasher = blake3()

        for k, v in kwargs.items():
            for k_bytes, v_bytes in self._item_to_bytes(k, v):
                hasher.update(k_bytes)
                hasher.update(v_bytes)

        return hasher.hexdigest()

    def get(
        self,
        model_id: str,
        modality: str,
        input_item: object,
        input_kwargs: Mapping[str, object],
    ) -> Optional[Mapping[str, MultiModalFieldItem]]:
        """
        Get a processed multi-modal item from the cache
        according to its dependencies, including:

        - The model ID
        - The modality of the item
        - The original data item passed to the HF processor
        - The configuration options of the HF processor
        """
        self._maybe_log_cache_stats()

        cache_key = self._hash_kwargs(model_id=model_id,
                                      **{modality: input_item},
                                      **input_kwargs)
        return self._cache.get(cache_key)

    def put(
        self,
        model_id: str,
        modality: str,
        input_item: object,
        input_kwargs: Mapping[str, object],
        output_kwargs: Mapping[str, MultiModalFieldItem],
    ) -> None:
        """
        Put a processed multi-modal item into the cache
        according to its dependencies (see :meth:`get`).
        """
        cache_key = self._hash_kwargs(model_id=model_id,
                                      **{modality: input_item},
                                      **input_kwargs)
        self._cache.put(cache_key, output_kwargs)
601
602


603
class BaseMultiModalProcessor(ABC):
604
    """
605
    Abstract base class to process multi-modal inputs to be used in vLLM.
606
607
    """

608
609
610
611
612
    def __init__(self,
                 ctx: InputProcessingContext,
                 *,
                 cache: Optional[ProcessingCache] = None,
                 enable_sanity_checks: bool = True) -> None:
613
614
615
        super().__init__()

        self.ctx = ctx
616
617
        self.cache = cache
        self.enable_sanity_checks = enable_sanity_checks
618

619
    def __call__(
620
        self,
621
622
        prompt: str,
        mm_data: MultiModalDataDict,
623
        hf_processor_mm_kwargs: Mapping[str, object],
624
    ) -> MultiModalInputsV2:
625
        return self.apply(prompt, mm_data, hf_processor_mm_kwargs)
626

627
628
629
630
631
632
633
634
635
636
    def _get_data_parser(self) -> MultiModalDataParser:
        """
        Construct a data parser to preprocess multi-modal data items
        before passing them to :meth:`_get_hf_mm_data`.

        You can support additional modalities by creating a subclass
        of :class:`MultiModalDataParser` that has additional subparsers.
        """
        return MultiModalDataParser()

637
638
639
640
641
    def _get_hf_processor(self) -> ProcessorMixin:
        """
        Subclasses can add keyword arguments to this method to accept
        additional kwargs from model config or user inputs.
        """
642
643
644
645
646
        return self.ctx.get_hf_processor()

    def _get_tokenizer(self) -> AnyTokenizer:
        return self.ctx.tokenizer

647
    def _to_mm_items(
648
649
650
        self,
        mm_data: MultiModalDataDict,
    ) -> MultiModalDataItems:
651
652
653
654
655
656
        """
        Normalize :class:`MultiModalDataDict` to :class:`MultiModalDataItems`
        before passing them to :meth:`_get_hf_mm_data`.
        """
        parser = self._get_data_parser()
        return parser.parse_mm_data(mm_data)
657

658
659
660
661
662
663
664
665
666
    @abstractmethod
    def _get_mm_fields_config(
        self,
        hf_inputs: BatchFeature,
        hf_processor_mm_kwargs: Mapping[str, object],
    ) -> Mapping[str, MultiModalFieldConfig]:
        """Given the HF-processed data, output the metadata of each field."""
        raise NotImplementedError

667
668
    @abstractmethod
    def _get_prompt_replacements(
669
        self,
670
        mm_items: MultiModalDataItems,
671
672
        hf_processor_mm_kwargs: Mapping[str, object],
        out_mm_kwargs: MultiModalKwargs,
673
674
675
676
677
    ) -> list[PromptReplacement]:
        """
        Given the original multi-modal items for this modality
        and HF-processed data, output the replacements to perform.

678
679
680
681
682
683
684
685
        Notes:
            - You should not assume that HF processor always performs prompt
              replacement: in :meth:`_apply_hf_processor_missing`, this method
              is called on text-only and multimodal-only inputs separately,
              instead of passing them in the same call.
            - The replacement information returned by this method is also used
              to determine the placeholder token positions for each multi-modal
              item.
686
687
        """
        raise NotImplementedError
688

689
690
    def _find_placeholders(
        self,
691
        all_prompt_repls: Sequence[_BoundPromptReplacement],
692
        new_token_ids: list[int],
693
        mm_item_counts: Mapping[str, int],
694
695
    ) -> list[_PlaceholderInfo]:
        return list(
696
            iter_placeholders(all_prompt_repls, new_token_ids, mm_item_counts))
697

698
    def _get_hf_mm_data(
699
        self,
700
701
        mm_items: MultiModalDataItems,
    ) -> tuple[dict[str, Any], dict[str, Any]]:
702
703
        processor_data = dict[str, Any]()
        passthrough_data = dict[str, Any]()
704

705
706
707
        for items in mm_items.values():
            processor_data.update(items.get_processor_data())
            passthrough_data.update(items.get_passthrough_data())
708

709
710
        return processor_data, passthrough_data

711
712
713
    def _call_hf_processor(
        self,
        prompt: str,
714
715
716
717
        # Not to be confused with `mm_data` in `self.apply`.
        # This refers to the data to be passed to HF processor.
        mm_data: Mapping[str, object],
        mm_kwargs: Mapping[str, object],
718
    ) -> BatchFeature:
719
720
721
722
        """
        Call the HF processor on the prompt text and
        associated multi-modal data.
        """
723
        return self.ctx.call_hf_processor(
724
725
726
            self._get_hf_processor(**mm_kwargs),
            dict(text=prompt, **mm_data),
            mm_kwargs,
727
728
        )

729
730
    def _apply_hf_processor(
        self,
731
        prompt_text: str,
732
        mm_items: MultiModalDataItems,
733
734
735
        hf_processor_mm_kwargs: Mapping[str, object],
    ) -> tuple[list[int], MultiModalKwargs]:
        """
736
737
        Wrapper of :meth:`_call_hf_processor` that applies
        additional pre-processing and post-processing.
738
739
740
741
742
743
744
745
746
        """
        processor_data, passthrough_data = self._get_hf_mm_data(mm_items)

        processed_data = self._call_hf_processor(
            prompt=prompt_text,
            mm_data=processor_data,
            mm_kwargs=hf_processor_mm_kwargs,
        )
        processed_data.update(passthrough_data)
747

748
        prompt_ids, = processed_data.pop("input_ids").tolist()
749

750
751
752
753
        mm_kwargs = MultiModalKwargs.from_hf_inputs(
            processed_data,
            self._get_mm_fields_config(processed_data, hf_processor_mm_kwargs),
            enable_sanity_checks=self.enable_sanity_checks,
754
        )
755

756
757
758
759
760
761
762
763
764
765
766
767
        return prompt_ids, mm_kwargs

    def _apply_hf_processor_missing(
        self,
        prompt_text: str,
        mm_missing_data_items: MultiModalDataItems,
        hf_processor_mm_kwargs: Mapping[str, object],
    ):
        """
        Apply the HF processor on the full prompt text, but only on the
        multi-modal data that are missing from the cache.

768
769
770
771
772
        Note:
            We pass prompt text and multi-modal data into the HF processor
            in separate calls to avoid HF prompt replacement being done for
            cached items; instead, we rely on our own prompt replacement logic
            (:meth:`_get_prompt_replacements`) for the full text.
773
        """
774
        mm_missing_counts = mm_missing_data_items.get_all_counts()
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806

        prompt_ids, _ = self._apply_hf_processor(
            prompt_text=prompt_text,
            mm_items=MultiModalDataItems({}),
            hf_processor_mm_kwargs={},
        )

        # Some HF processors (e.g. Qwen2-VL) expect corresponding
        # multi-modal tokens to be in the prompt text
        dummy_inputs = self._get_dummy_mm_inputs(mm_missing_counts)

        _, mm_missing_kwargs = self._apply_hf_processor(
            prompt_text=dummy_inputs.prompt_text,
            mm_items=mm_missing_data_items,
            hf_processor_mm_kwargs=hf_processor_mm_kwargs,
        )

        return prompt_ids, mm_missing_kwargs

    def _cached_apply_hf_processor(
        self,
        prompt_text: str,
        mm_data_items: MultiModalDataItems,
        hf_processor_mm_kwargs: Mapping[str, object],
    ) -> tuple[list[int], MultiModalKwargs]:
        """
        Apply the HF processor on the full prompt text,
        caching the results and reusing cached results.
        """
        cache = self.cache
        model_id = self.ctx.model_config.model

807
808
        _, passthrough_data = self._get_hf_mm_data(mm_data_items)
        if cache is None or passthrough_data:
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
            return self._apply_hf_processor(
                prompt_text=prompt_text,
                mm_items=mm_data_items,
                hf_processor_mm_kwargs=hf_processor_mm_kwargs,
            )

        mm_maybe_cached_field_items = {
            modality: [
                cache.get(model_id, modality, item, hf_processor_mm_kwargs)
                for item in items
            ]
            for modality, items in mm_data_items.items()
        }

        mm_missing_idxs = {
            modality: [idx for idx, out in enumerate(fields) if out is None]
            for modality, fields in mm_maybe_cached_field_items.items()
        }
        mm_missing_data = {
            modality: [mm_data_items[modality][idx] for idx in idxs]
            for modality, idxs in mm_missing_idxs.items()
        }
831
        mm_missing_data_items = self._to_mm_items(mm_missing_data)
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870

        prompt_ids, mm_missing_kwargs = self._apply_hf_processor_missing(
            prompt_text=prompt_text,
            mm_missing_data_items=mm_missing_data_items,
            hf_processor_mm_kwargs=hf_processor_mm_kwargs,
        )

        mm_missing_next_idx = {
            modality: 0
            for modality in mm_missing_data_items
        }

        mm_merged_field_items = dict[str, list[Mapping[str,
                                                       MultiModalFieldItem]]]()
        for modality, modal_items_lst in mm_maybe_cached_field_items.items():
            merged_modal_items_lst = list[Mapping[str, MultiModalFieldItem]]()

            for idx, modal_items in enumerate(modal_items_lst):
                if modal_items is None:
                    modal_items = mm_missing_kwargs.get_items_by_modality(
                        modality,
                        mm_missing_next_idx[modality],
                    )

                    cache.put(
                        model_id,
                        modality,
                        mm_data_items[modality][idx],
                        hf_processor_mm_kwargs,
                        modal_items,
                    )

                    mm_missing_next_idx[modality] += 1

                merged_modal_items_lst.append(modal_items)

            mm_merged_field_items[modality] = merged_modal_items_lst

        if self.enable_sanity_checks:
871
            mm_missing_counts = mm_missing_data_items.get_all_counts()
872
873
874
875
876
877
878
879
880
881
882
883
            assert all(
                item_count == mm_missing_counts[modality]
                for modality, item_count in mm_missing_next_idx.items()), dict(
                    mm_missing_next_idx=mm_missing_next_idx,
                    mm_missing_counts=mm_missing_counts)

        mm_kwargs = MultiModalKwargs.from_items_by_modality(
            mm_merged_field_items,
            enable_sanity_checks=self.enable_sanity_checks,
        )

        if self.enable_sanity_checks:
884
            mm_item_counts = mm_data_items.get_all_counts()
885
886
887
888
889
890
891
892
893
894

            for modality, item_count in mm_item_counts.items():
                for item_idx in range(item_count):
                    try:
                        mm_kwargs.get_items_by_modality(modality, item_idx)
                    except Exception as e:
                        # Make it easy to set a breakpoint in the debugger
                        raise e

        return prompt_ids, mm_kwargs
895

896
897
    def _bind_prompt_replacements(
        self,
898
899
        prompt_repls: list[PromptReplacement],
    ) -> list[_BoundPromptReplacement]:
900
        tokenizer = self._get_tokenizer()
901

902
        return [prompt_repl.bind(tokenizer) for prompt_repl in prompt_repls]
903

904
905
906
    def _apply_prompt_replacements(
        self,
        token_ids: list[int],
907
        prompt_repls: Sequence[_BoundPromptReplacement],
908
        mm_item_counts: Mapping[str, int],
909
    ) -> tuple[list[int], str, list[_PlaceholderInfo]]:
910
        tokenizer = self._get_tokenizer()
911

912
        token_matches = find_token_matches(token_ids, prompt_repls)
913
914
915
916
        mm_match_counts = {
            modality: len(matches)
            for modality, matches in full_groupby_modality(token_matches)
        }
917
918
919
920
921
922
923
924
925
926
927
928

        # If the search text does not represent a special token,
        # it may have different token IDs in the prompt, because
        # the tokens may go across the boundaries of the search text.
        # ----
        # e.g. when searching for "foo" in "food", if "food" itself makes
        # up a token, then the token ID of "foo" will not appear at all
        # ----
        # Since it is inefficient to search for all possible tokenizations
        # of the search text in the prompt, we instead perform string
        # replacement on the decoded token IDs, then encode them back.
        if all(
929
930
            mm_match_counts.get(modality, 0) >= item_count
            for modality, item_count in mm_item_counts.items()
931
932
933
934
        ):  # yapf: disable
            token_ids = replace_token_matches(
                token_ids,
                token_matches,
935
                mm_item_counts,
936
937
938
939
940
941
942
943
944
945
946
            )

            text = _decode(tokenizer, token_ids)
            matched_repls = [match.prompt_repl for match in token_matches]
        else:
            text = _decode(tokenizer, token_ids)

            text_matches = find_text_matches(text, prompt_repls)
            text = replace_text_matches(
                text,
                text_matches,
947
                mm_item_counts,
948
949
950
951
952
            )

            token_ids = _encode(tokenizer, text)
            matched_repls = [match.prompt_repl for match in text_matches]

953
        placeholders = self._find_placeholders(matched_repls, token_ids,
954
                                               mm_item_counts)
955
956

        return token_ids, text, placeholders
957

958
959
960
961
    def apply(
        self,
        prompt_text: str,
        mm_data: MultiModalDataDict,
962
        hf_processor_mm_kwargs: Mapping[str, object],
963
964
965
966
967
968
969
970
971
972
973
974
975
976
    ) -> MultiModalInputsV2:
        """
        Process multi-modal inputs to be used in vLLM.

        The main steps are:

        1. Apply HF Processor on prompt text and multi-modal data together,
           outputting token IDs and processed tensors.
        2. Find and replace sequences in the token IDs with placeholder tokens.
           The number of placeholder tokens equals the feature size of the
           multi-modal data outputted by the multi-modal encoder.
        3. Extract information about the placeholder tokens from the
           processed token IDs.
        """
977
        mm_items = self._to_mm_items(mm_data)
978

979
980
981
982
983
        prompt_ids, mm_kwargs = self._cached_apply_hf_processor(
            prompt_text,
            mm_items,
            hf_processor_mm_kwargs,
        )
984

985
986
987
988
989
990
        unbound_prompt_repls = self._get_prompt_replacements(
            mm_items,
            hf_processor_mm_kwargs,
            mm_kwargs,
        )
        prompt_repls = self._bind_prompt_replacements(unbound_prompt_repls)
991

992
993
        # If HF processor already inserts placeholder tokens,
        # there is no need for us to insert them
994
        mm_item_counts = mm_items.get_all_counts()
995
996
        all_placeholders = self._find_placeholders(prompt_repls, prompt_ids,
                                                   mm_item_counts)
997

998
        if all_placeholders:
999
            tokenizer = self._get_tokenizer()
1000
1001
1002
1003
1004
1005
1006
1007
            prompt_text = _decode(tokenizer, prompt_ids)
        else:
            (
                prompt_ids,
                prompt_text,
                all_placeholders,
            ) = self._apply_prompt_replacements(
                prompt_ids,
1008
                prompt_repls,
1009
                mm_item_counts,
1010
1011
1012
1013
1014
1015
            )

        mm_placeholders = {
            modality: [item.to_range() for item in items]
            for modality, items in full_groupby_modality(all_placeholders)
        }
1016
1017
1018

        return MultiModalInputsV2(
            type="multimodal",
1019
1020
            prompt=prompt_text,
            prompt_token_ids=prompt_ids,
1021
1022
1023
            mm_kwargs=mm_kwargs,
            mm_placeholders=mm_placeholders,
        )
1024

1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
    def _get_dummy_audios(
        self,
        *,
        length: int,
        num_audios: int,
    ) -> list[npt.NDArray]:
        audio = np.zeros((length, ))
        return [audio] * num_audios

    def _get_dummy_images(
        self,
        *,
        width: int,
        height: int,
        num_images: int,
    ) -> list[Image.Image]:
        image = Image.new("RGB", (width, height), color=0)
        return [image] * num_images

    def _get_dummy_videos(
        self,
        *,
        width: int,
        height: int,
        num_frames: int,
        num_videos: int,
    ) -> list[npt.NDArray]:
        video = np.zeros((num_frames, width, height, 3))
        return [video] * num_videos

1055
    @abstractmethod
1056
    def _get_dummy_mm_inputs(
1057
1058
        self,
        mm_counts: Mapping[str, int],
1059
    ) -> ProcessorInputs:
1060
        """
1061
1062
        Build the multi-modal portion of the input which, after processing,
        results in `mm_max_tokens` in :meth:`get_dummy_data`.
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
        """
        raise NotImplementedError

    def get_dummy_data(
        self,
        seq_len: int,
        mm_counts: Mapping[str, int],
        mm_max_tokens: Mapping[str, int],
    ) -> DummyData:
        # Avoid circular import
        from vllm.sequence import SequenceData

1075
        processor_inputs = self._get_dummy_mm_inputs(mm_counts)
1076
1077
1078
1079
1080
        mm_inputs = self.apply(
            prompt_text=processor_inputs.prompt_text,
            mm_data=processor_inputs.mm_data,
            hf_processor_mm_kwargs=processor_inputs.hf_processor_mm_kwargs,
        )
1081
1082
1083
1084

        prompt_token_ids = mm_inputs["prompt_token_ids"]
        placeholders_by_modality = mm_inputs["mm_placeholders"]

1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
        total_placeholders_by_modality = {
            modality: sum(item["length"] for item in placeholders)
            for modality, placeholders in placeholders_by_modality.items()
        }
        expected_placeholders_by_modality = {
            modality: mm_max_tokens[modality]
            for modality in placeholders_by_modality
        }
        if total_placeholders_by_modality != expected_placeholders_by_modality:
            raise AssertionError(
                f"The processed dummy data has a total of "
                f"{total_placeholders_by_modality} placeholder tokens, which "
                f"is not the expected {expected_placeholders_by_modality} "
                "tokens.")
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110

        total_len = len(prompt_token_ids)
        if total_len > seq_len:
            logger.warning(
                "The context length (%d) of the model is too short "
                "to hold the multi-modal embeddings in the worst case "
                "(%d tokens in total, out of which %s are reserved for "
                "multi-modal embeddings). This may cause certain multi-modal "
                "inputs to fail during inference, even when the input text is "
                "short. To avoid this, you should increase `max_model_len`, "
                "reduce `max_num_seqs`, and/or reduce `mm_counts`.", seq_len,
                total_len, total_placeholders_by_modality)
1111
1112
1113
1114
1115

        prompt_token_ids.extend([0] * (seq_len - len(prompt_token_ids)))

        return DummyData(
            seq_data=SequenceData.from_seqs(prompt_token_ids),
1116
1117
            multi_modal_data=mm_inputs["mm_kwargs"],
            multi_modal_placeholders=placeholders_by_modality,
1118
        )