minicpmv.py 37.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Adapted from
# https://github.com/huggingface/transformers/blob/v4.28.0/src/transformers/models/llama/modeling_llama.py
# Copyright 2023 The vLLM team.
# Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved.
#
# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
# and OPT implementations in this library. It has been modified from its
# original forms to accommodate minor architectural differences compared
# to GPT-NeoX and OPT used by the Meta AI team that trained the model.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Alphi's avatar
Alphi committed
22
"""Inference-only MiniCPM-V model compatible with HuggingFace weights."""
23
24
import math
import re
25
from functools import cached_property, partial
26
from typing import (Any, Callable, Iterable, List, Literal, Mapping, Optional,
27
                    Set, Tuple, TypedDict, Union)
28
29

import torch
Alphi's avatar
Alphi committed
30
import torch.types
31
32
from PIL import Image
from torch import nn
33
from transformers import PretrainedConfig
34
from typing_extensions import NotRequired
35
36

from vllm.attention import AttentionMetadata
37
from vllm.config import VllmConfig
38
39
from vllm.inputs import (INPUT_REGISTRY, DecoderOnlyInputs, DummyData,
                         InputContext, token_inputs)
40
from vllm.model_executor.layers.quantization import QuantizationConfig
41
from vllm.model_executor.layers.resampler import (BaseResampler, Resampler2,
42
                                                  get_2d_sincos_pos_embed)
Joe Runde's avatar
Joe Runde committed
43
from vllm.model_executor.layers.sampler import SamplerOutput, get_sampler
Jee Jee Li's avatar
Jee Jee Li committed
44
from vllm.model_executor.model_loader.utils import set_default_torch_dtype
45
46
from vllm.model_executor.models.llama import LlamaForCausalLM
from vllm.model_executor.models.minicpm import MiniCPMForCausalLM
47
from vllm.model_executor.models.module_mapping import MultiModelKeys
48
from vllm.model_executor.models.qwen2 import Qwen2ForCausalLM
49
from vllm.model_executor.sampling_metadata import SamplingMetadata
50
from vllm.multimodal import MULTIMODAL_REGISTRY, MultiModalKwargs
51
52
from vllm.multimodal.image import cached_get_image_processor
from vllm.multimodal.utils import cached_get_tokenizer
53
from vllm.sequence import IntermediateTensors, SequenceData
54

Jee Jee Li's avatar
Jee Jee Li committed
55
from .idefics2_vision_model import Idefics2VisionTransformer
56
from .interfaces import SupportsLoRA, SupportsMultiModal, SupportsPP
57
from .utils import AutoWeightsLoader, maybe_prefix
58

59
RawImageType = Union[Image.Image, torch.Tensor]
60

61
62

class MiniCPMVRawImageInput(TypedDict):
63
    """Input mapper input with auxiliary data for computing image bounds."""
64
    image: RawImageType
65
66
67
68
69
70
71
72

    # Image bounds token ids in 0-dim scaler tensor.
    im_start_id: torch.Tensor
    im_end_id: torch.Tensor
    slice_start_id: NotRequired[torch.Tensor]
    slice_end_id: NotRequired[torch.Tensor]


Jee Jee Li's avatar
Jee Jee Li committed
73
class MiniCPMVImagePixelInputs(TypedDict):
74
75
    type: Literal["pixel_values"]
    data: List[torch.Tensor]
Jee Jee Li's avatar
Jee Jee Li committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    """
    Shape: `(batch_size * num_images, num_channels, height, width)`

    Note that the image size may vary, so we pass it as a list
    instead of a batched tensor.
    """

    image_bounds: torch.Tensor
    """
    Shape: `(batch_size * num_images, 2)`

    This should be in `(start, stop)` format.
    """

    tgt_sizes: torch.Tensor
    """
    Shape: `(batch_size * num_images, 2)`

    This should be in `(height, width)` format.
    """


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
class MiniCPMVImageEmbeddingInputs(TypedDict):
    type: Literal["image_embeds"]
    data: torch.Tensor
    """
    Shape: `(batch_size * num_images, image_feature_size, hidden_size)`

    `hidden_size` must match the hidden size of language model backbone.
    instead of a batched tensor.
    """

    image_bounds: torch.Tensor
    """
    Shape: `(batch_size * num_images, 2)`

    This should be in `(start, stop)` format.
    """


MiniCPMVImageInputs = Union[MiniCPMVImagePixelInputs,
                            MiniCPMVImageEmbeddingInputs]

Jee Jee Li's avatar
Jee Jee Li committed
119
120
121
122
123
DEFAULT_LN = partial(nn.LayerNorm, eps=1e-6)


class Resampler2_5(BaseResampler):

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    def __init__(self,
                 num_queries: int,
                 embed_dim: int,
                 num_heads: int,
                 kv_dim: Optional[int] = None,
                 norm_layer: Callable[[int], nn.LayerNorm] = DEFAULT_LN,
                 max_size: Tuple[int, int] = (70, 70),
                 quant_config: Optional[QuantizationConfig] = None,
                 prefix: str = "") -> None:
        super().__init__(num_queries,
                         embed_dim,
                         num_heads,
                         kv_dim,
                         norm_layer,
                         quant_config=quant_config,
                         prefix=prefix)
Jee Jee Li's avatar
Jee Jee Li committed
140
141
142

        self.max_size = max_size
        self._set_2d_pos_cache(self.max_size)
143
144
145

        self.apply(self._init_weights)

Alphi's avatar
Alphi committed
146
147
    def _set_2d_pos_cache(self,
                          max_size: Tuple[int, int],
Jee Jee Li's avatar
Jee Jee Li committed
148
149
150
151
152
                          device: torch.types.Device = "cpu") -> None:
        pos_embed_arr = get_2d_sincos_pos_embed(self.embed_dim,
                                                max_size,
                                                version=(2, 5))
        pos_embed = torch.from_numpy(pos_embed_arr).float().to(device)
153
154
        self.register_buffer("pos_embed", pos_embed, persistent=False)

Alphi's avatar
Alphi committed
155
    def _adjust_pos_cache(self, tgt_sizes: torch.Tensor,
Jee Jee Li's avatar
Jee Jee Li committed
156
157
158
159
160
                          device: torch.types.Device) -> None:
        max_h = tgt_sizes[:, 0].max().item()
        max_w = tgt_sizes[:, 1].max().item()
        assert isinstance(max_h, int) and isinstance(max_w, int)

161
        if max_h > self.max_size[0] or max_w > self.max_size[1]:
Jee Jee Li's avatar
Jee Jee Li committed
162
            self.max_size = (
163
                max(max_h, self.max_size[0]),
Jee Jee Li's avatar
Jee Jee Li committed
164
165
                max(max_w, self.max_size[1]),
            )
166
167
            self._set_2d_pos_cache(self.max_size, device)

Jee Jee Li's avatar
Jee Jee Li committed
168
169
    def forward(self, x: torch.Tensor,
                tgt_sizes: torch.Tensor) -> torch.Tensor:
170
171
172
173
174
175
176
177
178
179
        assert x.shape[0] == tgt_sizes.shape[0]
        bs = x.shape[0]

        device = x.device
        dtype = x.dtype

        patch_len = tgt_sizes[:, 0] * tgt_sizes[:, 1]

        self._adjust_pos_cache(tgt_sizes, device=device)

Jee Jee Li's avatar
Jee Jee Li committed
180
181
182
        max_patch_len = patch_len.max().item()
        assert isinstance(max_patch_len, int)

183
184
185
186
187
188
        key_padding_mask = torch.zeros((bs, max_patch_len),
                                       dtype=torch.bool,
                                       device=device)

        pos_embed = []
        for i in range(bs):
Jee Jee Li's avatar
Jee Jee Li committed
189
            tgt_h, tgt_w = tgt_sizes[i].tolist()
190
191
192
193
194
195
196
197
            pos_embed.append(self.pos_embed[:tgt_h, :tgt_w, :].reshape(
                (tgt_h * tgt_w, -1)).to(dtype))  # patches * D
            key_padding_mask[i, patch_len[i]:] = True
        pos_embed = torch.nn.utils.rnn.pad_sequence(pos_embed,
                                                    batch_first=True,
                                                    padding_value=0.0).permute(
                                                        1, 0,
                                                        2)  # BLD => L * B * D
Jee Jee Li's avatar
Jee Jee Li committed
198
        x, _ = self.kv_proj(x)  # B * L * D
199
200
201
202
203
204
205
206
        x = self.ln_kv(x).permute(1, 0, 2)  # L * B * D

        q = self.ln_q(self.query)  # Q * D

        out = self.attn(
            self._repeat(q, bs),  # Q * B * D
            x + pos_embed,  # L * B * D +  L * B * D
            x,
Jee Jee Li's avatar
Jee Jee Li committed
207
208
            key_padding_mask=key_padding_mask,
        )[0]
209
210
211
212
213
214
215
216
        #  out: Q * B * D
        x = out.permute(1, 0, 2)  # B * Q * D

        x = self.ln_post(x)
        x = x @ self.proj
        return x


217
def _build_image_input(ctx: InputContext,
218
                       image: RawImageType) -> MiniCPMVRawImageInput:
219
220
221
222
    tokenizer = cached_get_tokenizer(
        ctx.model_config.tokenizer,
        trust_remote_code=ctx.model_config.trust_remote_code)
    if hasattr(tokenizer, "slice_start_id"):
223
        return MiniCPMVRawImageInput(
224
225
226
227
228
229
            image=image,
            im_start_id=torch.tensor(tokenizer.im_start_id),
            im_end_id=torch.tensor(tokenizer.im_end_id),
            slice_start_id=torch.tensor(tokenizer.slice_start_id),
            slice_end_id=torch.tensor(tokenizer.slice_end_id))
    else:
230
231
232
233
        return MiniCPMVRawImageInput(
            image=image,
            im_start_id=torch.tensor(tokenizer.im_start_id),
            im_end_id=torch.tensor(tokenizer.im_end_id))
234
235


236
237
238
239
240
241
242
243
244
245
246
247
248
249
def get_version_by_config(config: PretrainedConfig) -> Tuple[int, ...]:
    version_float = getattr(config, "version", None)

    # The old configs do not include version number
    # TODO: Remove this after the HF repos are updated
    if version_float is None:
        if config.hidden_size == 2304 and config.query_num == 64:
            return (2, 0)
        return (2, 5)

    version_str = str(version_float)
    return tuple(int(x) for x in version_str.split("."))


250
def get_max_minicpmv_image_tokens(ctx: InputContext):
251
    hf_config = ctx.get_hf_config()
252
253
254
    return getattr(hf_config, "query_num", 64)


255
def dummy_seq_data_for_minicpmv(seq_len: int, num_images: int):
256
    return SequenceData.from_prompt_token_counts((0, seq_len))
257
258


259
260
def dummy_image_for_minicpmv(ctx: InputContext, hf_config: PretrainedConfig,
                             num_images: int):
261
    width = height = hf_config.image_size
262
263
264
265
    image = _build_image_input(ctx,
                               image=Image.new("RGB", (width, height),
                                               color=0))
    return {"image": [image] if num_images == 1 else [image] * num_images}
266
267


268
269
def dummy_data_for_minicpmv(ctx: InputContext, seq_len: int,
                            mm_counts: Mapping[str, int]):
270
    hf_config = ctx.get_hf_config()
271
    num_images = mm_counts["image"]
272

273
    seq_data = dummy_seq_data_for_minicpmv(seq_len, num_images)
274
    mm_data = dummy_image_for_minicpmv(ctx, hf_config, num_images)
275

276
    return DummyData(seq_data, mm_data)
277
278


279
280
def input_processor_for_minicpmv(ctx: InputContext, inputs: DecoderOnlyInputs):
    multi_modal_data = inputs.get("multi_modal_data")
281
    if multi_modal_data is None or "image" not in multi_modal_data:
282
        return inputs
283
    model_config = ctx.model_config
284
    version = get_version_by_config(model_config.hf_config)
285
286
287
    tokenizer = cached_get_tokenizer(
        model_config.tokenizer,
        trust_remote_code=model_config.trust_remote_code)
288
289
290
291
    image_processor = cached_get_image_processor(model_config.tokenizer)

    def get_placeholder(image_size: Tuple[int, int], num_image: int):
        if version == (2, 0) or version == (2, 5):
292
293
294
            return image_processor.get_slice_image_placeholder(image_size)
        return image_processor.get_slice_image_placeholder(
            image_size, num_image)
295

296
297
    prompt = inputs.get("prompt")
    token_ids = inputs.get("prompt_token_ids")
298
299
300
301
    if prompt is None:
        prompt = tokenizer.decode(token_ids)

    pattern = "(<image>./</image>)"
302
    images = multi_modal_data["image"]
303
    image_tags = re.findall(pattern, prompt)
Jee Jee Li's avatar
Jee Jee Li committed
304
305
306
307
    if len(image_tags) == 0:
        new_token_ids = token_ids
        new_prompt = prompt
    else:
308
309
310
311
312
313
314
315
        if isinstance(images, dict):
            image_size_list = images.get("image_size_list")
            images = [images.get("image_embeds")]
        else:
            if isinstance(images, Image.Image):
                images = [images]
            image_size_list = [image.size for image in images]

Jee Jee Li's avatar
Jee Jee Li committed
316
        text_chunks = prompt.split(pattern)
317
        new_prompt_chunks: List[str] = []
318
        for i in range(len(image_size_list)):
319
320
            new_prompt_chunks += [
                text_chunks[i],
321
                get_placeholder(image_size_list[i], i)
322
323
324
            ]
        new_prompt_chunks.append(text_chunks[-1])
        new_prompt = "".join(new_prompt_chunks)
Jee Jee Li's avatar
Jee Jee Li committed
325
326
        new_token_ids = tokenizer.encode(new_prompt)

327
328
329
330
    multi_modal_data["image"] = [
        _build_image_input(ctx, image) for image in images
    ]

331
    return token_inputs(
Jee Jee Li's avatar
Jee Jee Li committed
332
333
334
335
        prompt_token_ids=new_token_ids,
        prompt=new_prompt,
        multi_modal_data=multi_modal_data,
    )
336
337


338
339
340
341
342
343
344
345
346
347
348
349
def input_mapper_for_minicpmv(ctx: InputContext, data: object):
    model_config = ctx.model_config

    image_processor = cached_get_image_processor(
        model_config.model, trust_remote_code=model_config.trust_remote_code)
    if image_processor is None:
        raise RuntimeError("No HuggingFace processor is available "
                           "to process the image object")

    if not isinstance(data, list):
        raise ValueError(
            "Image input must be list of MiniCPMVImageInput, got (%s)", data)
350
351
352
353
354
355
356
357
358

    if len(data) > 0 and isinstance(data[0]['image'], torch.Tensor):
        batch_data = {
            "image_embeds": data[0]['image'],
        }
    else:
        batch_data = image_processor \
            .preprocess([img["image"] for img in data], return_tensors="pt") \
            .data
359
360
361
362
363
364
365
366

    if len(data) > 0:
        batch_data["im_start_id"] = data[0]["im_start_id"]
        batch_data["im_end_id"] = data[0]["im_end_id"]
        if "slice_start_id" in data[0]:
            batch_data["slice_start_id"] = data[0]["slice_start_id"]
            batch_data["slice_end_id"] = data[0]["slice_end_id"]

367
    return MultiModalKwargs(batch_data)
368
369


370
class MiniCPMVBaseModel(nn.Module, SupportsMultiModal, SupportsPP):
Jee Jee Li's avatar
Jee Jee Li committed
371
372
373
374
    """
    The abstract class of MiniCPMV can only be inherited, but cannot be
    instantiated.
    """
375

376
    def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
377
378
379
        config = vllm_config.model_config.hf_config
        multimodal_config = vllm_config.model_config.multimodal_config
        quant_config = vllm_config.quant_config
380
        super().__init__()
381
382
383
384
        # All MiniCPM-V models disable `tie_word_embeddings` but
        # `PretrainedConfig.tie_word_embeddings` defaults to True; we cannot
        # check `tie_word_embeddings` until vLLM integrate MiniCPM-V model
        # and config class
385
386
387
        self.config = config
        self.multimodal_config = multimodal_config

388
        self.version = get_version_by_config(self.config)
389
390
391
392
393
        self.llm = self.init_llm(vllm_config=vllm_config,
                                 prefix=maybe_prefix(prefix, "llm"))
        self.vpm = self.init_vision_module(config,
                                           quant_config,
                                           prefix=maybe_prefix(prefix, "vpm"))
Jee Jee Li's avatar
Jee Jee Li committed
394
395
        self.vision_dim = (self.vpm.embed_dim if self.version == (2, 0) else
                           self.vpm.embeddings.embed_dim)
Alphi's avatar
Alphi committed
396
        self.embed_dim = self.config.hidden_size
397

398
399
400
        self.resampler = self.init_resampler(self.embed_dim,
                                             self.vision_dim,
                                             quant_config=quant_config,
401
402
                                             prefix=maybe_prefix(
                                                 prefix, "resampler"))
403

404
405
406
        self.make_empty_intermediate_tensors = (
            self.llm.make_empty_intermediate_tensors)

407
408
409
410
411
412
413
    @cached_property
    def sampler(self):
        if hasattr(self.llm, "sampler"):
            return self.llm.sampler

        return get_sampler()

Jee Jee Li's avatar
Jee Jee Li committed
414
415
416
    def get_embedding(
        self,
        input_ids: torch.Tensor,
417
        image_inputs: Optional[MiniCPMVImageInputs],
Jee Jee Li's avatar
Jee Jee Li committed
418
    ) -> Tuple[torch.Tensor, torch.Tensor]:
419
        vlm_embedding: torch.Tensor = self.llm.get_input_embeddings(input_ids)
Jee Jee Li's avatar
Jee Jee Li committed
420
421
422

        if image_inputs is None:  # No image
            vision_hidden_states = torch.tensor([], device=input_ids.device)
423
        else:
424
425
426
427
428
429
            if image_inputs["type"] == "image_embeds":
                vision_hidden_states = (image_inputs["data"].type(
                    vlm_embedding.dtype).to(vlm_embedding.device))
            else:
                vision_hidden_states = self.get_vision_hidden_states(
                    image_inputs)
Jee Jee Li's avatar
Jee Jee Li committed
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444

            # See NOTE in _parse_and_validate_inputs
            image_bounds = image_inputs["image_bounds"]
            if len(image_bounds) > 0:
                image_indices = torch.stack([
                    torch.arange(start, end, dtype=torch.long)
                    for start, end in image_bounds.tolist()
                ]).to(vlm_embedding.device)
                vlm_embedding.scatter_(
                    0,
                    image_indices.view(-1, 1).repeat(1,
                                                     vlm_embedding.shape[-1]),
                    vision_hidden_states.view(-1,
                                              vision_hidden_states.shape[-1]),
                )
445

Jee Jee Li's avatar
Jee Jee Li committed
446
        return vlm_embedding, vision_hidden_states
447

448
449
450
451
452
453
454
455
456
457
458
459
460
461
    def _get_image_bounds(
            self,
            input_ids: torch.Tensor,
            im_start_id: torch.Tensor,
            im_end_id: torch.Tensor,
            slice_start_id: Optional[torch.Tensor] = None,
            slice_end_id: Optional[torch.Tensor] = None) -> torch.Tensor:
        # All the images in the batch should share the same special image
        # bound token ids.
        start_cond = input_ids == im_start_id[0]
        end_cond = input_ids == im_end_id[0]
        if slice_start_id is not None:
            start_cond |= (input_ids == slice_start_id[0])
            end_cond |= (input_ids == slice_end_id[0])
Alphi's avatar
Alphi committed
462

Jee Jee Li's avatar
Jee Jee Li committed
463
        image_start_tokens, = torch.where(start_cond)
464
        image_start_tokens += 1
Jee Jee Li's avatar
Jee Jee Li committed
465
        image_end_tokens, = torch.where(end_cond)
Alphi's avatar
Alphi committed
466
        valid_image_nums = max(len(image_start_tokens), len(image_end_tokens))
Jee Jee Li's avatar
Jee Jee Li committed
467

468
        if valid_image_nums == 0:
Jee Jee Li's avatar
Jee Jee Li committed
469
470
471
            return torch.zeros((0, 2), device=input_ids.device)

        return torch.hstack([
472
473
474
475
            image_start_tokens[:valid_image_nums].unsqueeze(-1),
            image_end_tokens[:valid_image_nums].unsqueeze(-1),
        ])

Jee Jee Li's avatar
Jee Jee Li committed
476
477
478
479
    def _parse_and_validate_inputs(
        self,
        input_ids: torch.Tensor,
        **kwargs: object,
480
    ) -> Optional[MiniCPMVImageInputs]:
Jee Jee Li's avatar
Jee Jee Li committed
481
482
        pixel_values = kwargs.pop("pixel_values", [])
        tgt_sizes = kwargs.pop("tgt_sizes", [])
483
484
485
486
487
488
489
490
491
492
493
494
495
496
        im_start_id = kwargs.pop("im_start_id", None)
        im_end_id = kwargs.pop("im_end_id", None)
        slice_start_id = kwargs.pop("slice_start_id", None)
        slice_end_id = kwargs.pop("slice_end_id", None)
        image_embeds = kwargs.pop("image_embeds", None)

        if image_embeds is not None:
            return MiniCPMVImageEmbeddingInputs(
                image_bounds=self._get_image_bounds(input_ids, im_start_id,
                                                    im_end_id, slice_start_id,
                                                    slice_end_id),
                data=image_embeds,
                type="image_embeds",
            )
Jee Jee Li's avatar
Jee Jee Li committed
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511

        if not isinstance(pixel_values, (torch.Tensor, list)):
            raise ValueError("Incorrect type of pixel values. "
                             f"Got type: {type(pixel_values)}")

        if not isinstance(tgt_sizes, (torch.Tensor, list)):
            raise ValueError("Incorrect type of target sizes. "
                             f"Got type: {type(tgt_sizes)}")

        if len(pixel_values) != len(tgt_sizes):
            raise ValueError("Inconsistent batch lengths, found: "
                             f"{len(pixel_values)} vs. {len(tgt_sizes)}")

        pixel_values_flat: List[torch.Tensor] = []
        tgt_sizes_flat: List[torch.Tensor] = []
512
513
514
515
516
517
518
519
        for pixel_b, tgt_b in zip(pixel_values, tgt_sizes):
            if len(pixel_b) != len(tgt_b):
                raise ValueError("Inconsistent N lengths, found: "
                                 f"{len(pixel_b)} vs {len(tgt_b)}")

            for pixel_n, tgt_n in zip(pixel_b, tgt_b):
                pixel_values_flat += pixel_n
                tgt_sizes_flat += tgt_n
Jee Jee Li's avatar
Jee Jee Li committed
520
521
522
523
524
525
526
527
528
529
530

        # NOTE: Input IDs does not contain image tokens during memory profiling,
        # so we allow it to be empty
        if len(pixel_values_flat) != len(tgt_sizes_flat):
            raise ValueError("Inconsistent flattened lengths, found: "
                             f"{len(pixel_values_flat)} vs. "
                             f"{len(tgt_sizes_flat)}")

        if len(pixel_values_flat) == 0:
            return None

531
532
533
534
535
536
537
        if im_start_id is None:
            return None

        return MiniCPMVImagePixelInputs(
            image_bounds=self._get_image_bounds(input_ids, im_start_id,
                                                im_end_id, slice_start_id,
                                                slice_end_id),
538
            data=pixel_values_flat,
Jee Jee Li's avatar
Jee Jee Li committed
539
            tgt_sizes=torch.stack(tgt_sizes_flat),
540
            type="pixel_values",
Jee Jee Li's avatar
Jee Jee Li committed
541
        )
542
543
544
545
546
547
548
549

    def forward(
        self,
        input_ids: torch.Tensor,
        positions: torch.Tensor,
        kv_caches: List[torch.Tensor],
        attn_metadata: AttentionMetadata,
        intermediate_tensors: Optional[IntermediateTensors] = None,
Jee Jee Li's avatar
Jee Jee Li committed
550
551
        **kwargs: Any,
    ) -> torch.Tensor:
552
553
554
555
        if intermediate_tensors is not None:
            vlm_embeddings = None
        else:
            image_inputs = self._parse_and_validate_inputs(input_ids, **kwargs)
Jee Jee Li's avatar
Jee Jee Li committed
556

557
            vlm_embeddings, _ = self.get_embedding(input_ids, image_inputs)
Jee Jee Li's avatar
Jee Jee Li committed
558

559
560
561
562
563
        # always pass the input via `inputs_embeds`
        # to make sure the computation graph is consistent
        # for `torch.compile` integration
        input_ids = None

564
        output = self.llm.model(
565
            input_ids=input_ids,
Jee Jee Li's avatar
Jee Jee Li committed
566
567
568
569
570
571
            positions=positions,
            kv_caches=kv_caches,
            attn_metadata=attn_metadata,
            intermediate_tensors=intermediate_tensors,
            inputs_embeds=vlm_embeddings,
        )
572
573
        return output

574
575
576
577
578
    def compute_logits(
        self,
        hidden_states: torch.Tensor,
        sampling_metadata: SamplingMetadata,
    ) -> Optional[torch.Tensor]:
579
        return self.llm.compute_logits(hidden_states, sampling_metadata)
580
581
582
583
584
585

    def sample(
        self,
        logits: torch.Tensor,
        sampling_metadata: SamplingMetadata,
    ) -> Optional[SamplerOutput]:
Alphi's avatar
Alphi committed
586
        next_tokens = self.sampler(logits, sampling_metadata)
587
588
        return next_tokens

589
590
    def load_weights(self, weights: Iterable[Tuple[str,
                                                   torch.Tensor]]) -> Set[str]:
591
592
        loader = AutoWeightsLoader(self)
        return loader.load_weights(weights)
Jee Jee Li's avatar
Jee Jee Li committed
593

594
595
596
597
598
599
600
601
    def get_mm_mapping(self) -> MultiModelKeys:
        """
        Get the module prefix in multimodal models
        """
        return MultiModelKeys.from_string_field(language_model="llm",
                                                connector="resampler",
                                                tower_model="vpm")

Jee Jee Li's avatar
Jee Jee Li committed
602
603
    def init_llm(
        self,
604
        vllm_config: VllmConfig,
605
        prefix: str = "",
Jee Jee Li's avatar
Jee Jee Li committed
606
607
608
    ) -> nn.Module:
        raise NotImplementedError

609
610
611
612
    def init_vision_module(
        self,
        config: PretrainedConfig,
        quant_config: Optional[QuantizationConfig],
613
        prefix: str = "",
614
    ) -> nn.Module:
Jee Jee Li's avatar
Jee Jee Li committed
615
616
        raise NotImplementedError

617
618
619
620
621
    def init_resampler(self,
                       embed_dim: int,
                       vision_dim: int,
                       quant_config: Optional[QuantizationConfig] = None,
                       prefix: str = "") -> nn.Module:
Jee Jee Li's avatar
Jee Jee Li committed
622
623
624
625
626
627
628
629
630
631
        raise NotImplementedError

    def get_vision_embedding(
        self,
        pixel_values: List[torch.Tensor],
        patch_attn_mask: Optional[torch.Tensor] = None,
        tgt_sizes: Optional[torch.Tensor] = None,
    ) -> torch.Tensor:
        raise NotImplementedError

632
633
    def get_vision_hidden_states(self,
                                 data: MiniCPMVImageInputs) -> torch.Tensor:
Jee Jee Li's avatar
Jee Jee Li committed
634
635
636
        raise NotImplementedError


637
class MiniCPMV2_0(MiniCPMVBaseModel):
Jee Jee Li's avatar
Jee Jee Li committed
638

639
640
    def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
        super().__init__(vllm_config=vllm_config, prefix=prefix)
Jee Jee Li's avatar
Jee Jee Li committed
641
642
643
644
        assert self.version == (2, 0)

    def init_llm(
        self,
645
        vllm_config: VllmConfig,
646
        prefix: str = "",
Jee Jee Li's avatar
Jee Jee Li committed
647
    ) -> nn.Module:
648
        return MiniCPMForCausalLM(vllm_config=vllm_config, prefix=prefix)
Jee Jee Li's avatar
Jee Jee Li committed
649

650
651
652
653
    def init_vision_module(
        self,
        config: PretrainedConfig,
        quant_config: Optional[QuantizationConfig],
654
        prefix: str = "",
655
    ) -> nn.Module:
656
        # TODO: refactor this vision model
Jee Jee Li's avatar
Jee Jee Li committed
657
658
659
660
        try:
            import timm
        except ImportError:
            raise ImportError("Please install timm==0.9.10") from ImportError
661

Jee Jee Li's avatar
Jee Jee Li committed
662
663
664
665
666
667
668
669
670
        with set_default_torch_dtype(torch.float16):
            model = timm.create_model(
                "vit_so400m_patch14_siglip_384.webli",
                pretrained=False,
                num_classes=0,
                dynamic_img_size=True,
                dynamic_img_pad=True,
            )

671
672
        model = model.to(dtype=torch.get_default_dtype())

Jee Jee Li's avatar
Jee Jee Li committed
673
674
675
676
677
678
679
680
681
        if (isinstance(model, timm.models.VisionTransformer)
                and model.attn_pool is not None):
            model.attn_pool = torch.nn.Identity()

        if self.config.drop_vision_last_layer:
            model.blocks = model.blocks[:-1]

        return model

682
683
684
    def get_input_embeddings(self, input_ids: torch.Tensor) -> torch.Tensor:
        return self.model.embed_tokens(input_ids)

685
686
687
688
689
    def init_resampler(self,
                       embed_dim: int,
                       vision_dim: int,
                       quant_config: Optional[QuantizationConfig] = None,
                       prefix: str = "") -> nn.Module:
Jee Jee Li's avatar
Jee Jee Li committed
690
        with set_default_torch_dtype(torch.float16):
691
692
693
694
695
696
697
698
699
            resampler = Resampler2(embed_dim=embed_dim,
                                   num_heads=embed_dim // 128,
                                   grid_size=int(
                                       math.sqrt(self.config.query_num)),
                                   kv_dim=vision_dim,
                                   adaptive=False,
                                   do_post_projection=True,
                                   quant_config=quant_config,
                                   prefix=prefix)
Jee Jee Li's avatar
Jee Jee Li committed
700

701
        return resampler.to(device="cuda", dtype=torch.get_default_dtype())
Jee Jee Li's avatar
Jee Jee Li committed
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725

    def get_vision_embedding(
        self,
        pixel_values: List[torch.Tensor],
        patch_attn_mask: Optional[torch.Tensor] = None,
        tgt_sizes: Optional[torch.Tensor] = None,
    ) -> torch.Tensor:
        res = []
        dtype = self.vpm.pos_embed.data.dtype
        for pixel_value in pixel_values:
            H, W = pixel_value[0].shape[-2:]
            tgt_size = (
                math.ceil(H / self.vpm.patch_embed.patch_size[0]),
                math.ceil(W / self.vpm.patch_embed.patch_size[0]),
            )
            vision_embedding = self.vpm.forward_features(
                pixel_value.unsqueeze(0).type(dtype))
            if (hasattr(self.vpm, "num_prefix_tokens")
                    and self.vpm.num_prefix_tokens > 0):
                vision_embedding = vision_embedding[:, self.vpm.
                                                    num_prefix_tokens:]
            res.append(self.resampler(vision_embedding, tgt_size))
        return torch.vstack(res)

726
727
728
    def get_vision_hidden_states(self,
                                 data: MiniCPMVImageInputs) -> torch.Tensor:
        pixel_values = data["data"]
Jee Jee Li's avatar
Jee Jee Li committed
729
730
731
732

        return self.get_vision_embedding(pixel_values)


733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
class MiniCPMV2_5(MiniCPMVBaseModel, SupportsLoRA):
    packed_modules_mapping = {
        "qkv_proj": [
            "q_proj",
            "k_proj",
            "v_proj",
        ],
        "gate_up_proj": [
            "gate_proj",
            "up_proj",
        ],
    }
    # LoRA specific attributes
    supported_lora_modules = [
        # vision encoder
        "fc1",
        "fc2",
        "out_proj",
        # language model
        "qkv_proj",  # same name with vision encoder
        "o_proj",
        "gate_up_proj",
        "down_proj",
        # resampler
        "kv_proj",
    ]
759
760
761
762
763
764
765
766
767
768
769

    # BitandBytes specific attributes
    bitsandbytes_stacked_params_mapping = {
        # shard_name, weight_name, index
        "q_proj": ("qkv_proj", 0),
        "k_proj": ("qkv_proj", 1),
        "v_proj": ("qkv_proj", 2),
        "gate_proj": ("gate_up_proj", 0),
        "up_proj": ("gate_up_proj", 1),
    }

770
771
    embedding_modules = {}
    embedding_padding_modules = []
Jee Jee Li's avatar
Jee Jee Li committed
772

773
774
    def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
        super().__init__(vllm_config=vllm_config, prefix=prefix)
Jee Jee Li's avatar
Jee Jee Li committed
775
776
777
778
        assert self.version == (2, 5)

    def init_llm(
        self,
779
        vllm_config: VllmConfig,
780
        prefix: str = "",
Jee Jee Li's avatar
Jee Jee Li committed
781
    ) -> nn.Module:
782
        return LlamaForCausalLM(vllm_config=vllm_config, prefix=prefix)
Jee Jee Li's avatar
Jee Jee Li committed
783

784
785
786
787
    def init_vision_module(
        self,
        config: PretrainedConfig,
        quant_config: Optional[QuantizationConfig],
788
        prefix: str = "",
789
790
    ) -> nn.Module:
        model = Idefics2VisionTransformer(config.vision_config,
791
792
                                          quant_config=quant_config,
                                          prefix=prefix)
Jee Jee Li's avatar
Jee Jee Li committed
793
794
795
796
        if self.config.drop_vision_last_layer:
            model.encoder.layers = model.encoder.layers[:-1]
        return model

797
798
799
800
801
    def init_resampler(self,
                       embed_dim: int,
                       vision_dim: int,
                       quant_config: Optional[QuantizationConfig] = None,
                       prefix: str = "") -> nn.Module:
Jee Jee Li's avatar
Jee Jee Li committed
802
        with set_default_torch_dtype(torch.float16):
803
804
805
806
807
808
            resampler = Resampler2_5(num_queries=self.config.query_num,
                                     embed_dim=embed_dim,
                                     num_heads=embed_dim // 128,
                                     kv_dim=vision_dim,
                                     quant_config=quant_config,
                                     prefix=prefix)
809
810

        return resampler.to(device="cuda", dtype=torch.get_default_dtype())
Jee Jee Li's avatar
Jee Jee Li committed
811
812
813
814
815
816
817
818
819
820
821
822

    def get_vision_embedding(
        self,
        pixel_values: List[torch.Tensor],
        patch_attn_mask: Optional[torch.Tensor] = None,
        tgt_sizes: Optional[torch.Tensor] = None,
    ) -> torch.Tensor:
        vision_embedding = self.vpm(pixel_values,
                                    patch_attention_mask=patch_attn_mask)
        vision_embedding = self.resampler(vision_embedding, tgt_sizes)
        return vision_embedding

823
824
825
    def get_vision_hidden_states(self,
                                 data: MiniCPMVImageInputs) -> torch.Tensor:
        pixel_values = data["data"]
Jee Jee Li's avatar
Jee Jee Li committed
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
        tgt_sizes = data["tgt_sizes"]

        device = self.vpm.embeddings.position_embedding.weight.device
        dtype = self.vpm.embeddings.position_embedding.weight.dtype
        all_pixel_values_lst = [
            i.flatten(end_dim=1).permute(1, 0) for i in pixel_values
        ]

        max_patches = (tgt_sizes[:, 0] * tgt_sizes[:, 1]).max().item()
        assert isinstance(max_patches, int)

        all_pixel_values = torch.nn.utils.rnn.pad_sequence(
            all_pixel_values_lst, batch_first=True, padding_value=0.0)
        B, L, _ = all_pixel_values.shape
        all_pixel_values = all_pixel_values.permute(0, 2,
                                                    1).reshape(B, 3, -1, L)

        patch_attn_mask = torch.zeros((B, 1, max_patches),
                                      dtype=torch.bool,
                                      device=device)
        for i in range(B):
            patch_attn_mask[i, :tgt_sizes[i][0] * tgt_sizes[i][1]] = True

        return self.get_vision_embedding(all_pixel_values.type(dtype),
                                         patch_attn_mask, tgt_sizes)


853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
class MiniCPMV2_6(MiniCPMVBaseModel, SupportsLoRA):
    packed_modules_mapping = {
        "qkv_proj": [
            "q_proj",
            "k_proj",
            "v_proj",
        ],
        "gate_up_proj": [
            "gate_proj",
            "up_proj",
        ],
    }
    # LoRA specific attributes
    supported_lora_modules = [
        # vision encoder
        "fc1",
        "fc2",
        "out_proj",
        # language model
        "qkv_proj",  # same name with vision encoder
        "o_proj",
        "gate_up_proj",
        "down_proj",
        # resampler
        "kv_proj",
    ]

880
881
882
883
884
885
886
887
888
889
    # BitandBytes specific attributes
    bitsandbytes_stacked_params_mapping = {
        # shard_name, weight_name, index
        "q_proj": ("qkv_proj", 0),
        "k_proj": ("qkv_proj", 1),
        "v_proj": ("qkv_proj", 2),
        "gate_proj": ("gate_up_proj", 0),
        "up_proj": ("gate_up_proj", 1),
    }

890
891
    embedding_modules = {}
    embedding_padding_modules = []
Jee Jee Li's avatar
Jee Jee Li committed
892

893
894
    def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
        super().__init__(vllm_config=vllm_config, prefix=prefix)
895
        assert self.version == (2, 6)
Jee Jee Li's avatar
Jee Jee Li committed
896
897
898

    def init_llm(
        self,
899
        vllm_config: VllmConfig,
900
        prefix: str = "",
Jee Jee Li's avatar
Jee Jee Li committed
901
    ) -> nn.Module:
902
        return Qwen2ForCausalLM(vllm_config=vllm_config, prefix=prefix)
Jee Jee Li's avatar
Jee Jee Li committed
903

904
905
906
907
    def init_vision_module(
        self,
        config: PretrainedConfig,
        quant_config: Optional[QuantizationConfig],
908
        prefix: str = "",
909
910
    ) -> nn.Module:
        model = Idefics2VisionTransformer(config.vision_config,
911
912
                                          quant_config=quant_config,
                                          prefix=prefix)
Jee Jee Li's avatar
Jee Jee Li committed
913
914
915
916
        if self.config.drop_vision_last_layer:
            model.encoder.layers = model.encoder.layers[:-1]
        return model

917
918
919
920
921
    def init_resampler(self,
                       embed_dim: int,
                       vision_dim: int,
                       quant_config: Optional[QuantizationConfig] = None,
                       prefix: str = "") -> nn.Module:
Jee Jee Li's avatar
Jee Jee Li committed
922
        with set_default_torch_dtype(torch.float16):
923
            # The resampler in 2.6 remains consistent with the one in 2.5.
924
925
926
927
928
929
            resampler = Resampler2_5(num_queries=self.config.query_num,
                                     embed_dim=embed_dim,
                                     num_heads=embed_dim // 128,
                                     kv_dim=vision_dim,
                                     quant_config=quant_config,
                                     prefix=prefix)
930
931

        return resampler.to(device="cuda", dtype=torch.get_default_dtype())
Jee Jee Li's avatar
Jee Jee Li committed
932
933
934
935
936
937
938
939
940
941
942

    def get_vision_embedding(
        self,
        pixel_values: List[torch.Tensor],
        patch_attn_mask: Optional[torch.Tensor] = None,
        tgt_sizes: Optional[torch.Tensor] = None,
    ) -> torch.Tensor:
        vision_embedding = self.vpm(
            pixel_values,
            patch_attention_mask=patch_attn_mask,
            tgt_sizes=tgt_sizes,
943
        )
Jee Jee Li's avatar
Jee Jee Li committed
944
945
        return vision_embedding

946
947
948
    def get_vision_hidden_states(self,
                                 data: MiniCPMVImageInputs) -> torch.Tensor:
        pixel_values = data["data"]
Jee Jee Li's avatar
Jee Jee Li committed
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
        tgt_sizes = data["tgt_sizes"]

        device = self.vpm.embeddings.position_embedding.weight.device
        dtype = self.vpm.embeddings.position_embedding.weight.dtype
        all_pixel_values_lst = [
            i.flatten(end_dim=1).permute(1, 0) for i in pixel_values
        ]

        max_patches = (tgt_sizes[:, 0] * tgt_sizes[:, 1]).max().item()
        assert isinstance(max_patches, int)

        all_pixel_values = torch.nn.utils.rnn.pad_sequence(
            all_pixel_values_lst, batch_first=True, padding_value=0.0)
        B, L, _ = all_pixel_values.shape
        all_pixel_values = all_pixel_values.permute(0, 2,
                                                    1).reshape(B, 3, -1, L)

        patch_attn_mask = torch.zeros((B, 1, max_patches),
                                      dtype=torch.bool,
                                      device=device)
        for i in range(B):
            patch_attn_mask[i, 0, :tgt_sizes[i][0] * tgt_sizes[i][1]] = True
        vision_embedding = self.vpm(
            all_pixel_values.type(dtype),
            patch_attention_mask=patch_attn_mask,
            tgt_sizes=tgt_sizes,
975
        )
Jee Jee Li's avatar
Jee Jee Li committed
976
977
978
979

        return self.resampler(vision_embedding, tgt_sizes)


980
981
982
983
984
985
986
_SUPPORT_VERSION = {
    (2, 0): MiniCPMV2_0,
    (2, 5): MiniCPMV2_5,
    (2, 6): MiniCPMV2_6
}


987
@MULTIMODAL_REGISTRY.register_image_input_mapper(input_mapper_for_minicpmv)
Jee Jee Li's avatar
Jee Jee Li committed
988
989
990
@MULTIMODAL_REGISTRY.register_max_image_tokens(get_max_minicpmv_image_tokens)
@INPUT_REGISTRY.register_dummy_data(dummy_data_for_minicpmv)
@INPUT_REGISTRY.register_input_processor(input_processor_for_minicpmv)
991
class MiniCPMV(MiniCPMVBaseModel, SupportsLoRA):
Jee Jee Li's avatar
Jee Jee Li committed
992
993
994
995
996
    """
    Different versions of MiniCPMV use different visual encoders and LLMs,
    which is not conducive to the current integration logic of LoRA and
    bitsandbytes in vLLM. Therefore, it is necessary to separate them.
    """
997
998
999
1000
1001
1002
1003
    # Ensure that the LoRA support check passes when the class is not
    # initialized, but set all these attributes to empty.
    packed_modules_mapping = {}
    supported_lora_modules = []
    embedding_modules = {}
    embedding_padding_modules = []

1004
    def __new__(cls, *, vllm_config: VllmConfig, prefix: str = ""):
1005
        config = vllm_config.model_config.hf_config
Jee Jee Li's avatar
Jee Jee Li committed
1006
1007
1008
1009
1010
1011
1012
1013
1014
        if not hasattr(config, "version"):
            if config.hidden_size == 2304 and config.query_num == 64:
                version = (2, 0)
            else:
                version = (2, 5)
        else:
            version = str(config.version).split(".")
            version = tuple([int(x) for x in version])
        # Dispatch class based on version
1015
        instance_class = _SUPPORT_VERSION.get(version)
1016
1017
1018
        if instance_class is None:
            raise ValueError(
                "Currently, MiniCPMV only supports versions 2.0, 2.5, and 2.6")
1019
        return instance_class(vllm_config=vllm_config, prefix=prefix)