|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | +from collections.abc import Iterable, Mapping, Sequence |
| 4 | +from typing import TypeVar |
| 5 | + |
| 6 | +import torch |
| 7 | +import torch.nn as nn |
| 8 | +from transformers import ( |
| 9 | + BatchFeature, |
| 10 | + PixtralVisionConfig, |
| 11 | +) |
| 12 | + |
| 13 | +from vllm.config import VllmConfig |
| 14 | +from vllm.model_executor.models.mistral3 import ( |
| 15 | + Mistral3DummyInputsBuilder, |
| 16 | + Mistral3ForConditionalGeneration, |
| 17 | + Mistral3MultiModalProjector, |
| 18 | + Mistral3ProcessingInfo, |
| 19 | + _build_mistral3_info, |
| 20 | + init_vision_tower_for_llava, |
| 21 | +) |
| 22 | +from vllm.model_executor.models.pixtral import PixtralHFEncoderInfo |
| 23 | +from vllm.model_executor.models.utils import ( |
| 24 | + AutoWeightsLoader, |
| 25 | + WeightsMapper, |
| 26 | + init_vllm_registered_model, |
| 27 | + maybe_prefix, |
| 28 | +) |
| 29 | +from vllm.multimodal import MULTIMODAL_REGISTRY |
| 30 | +from vllm.multimodal.cache import BaseMultiModalProcessorCache |
| 31 | +from vllm.multimodal.inputs import MultiModalFieldConfig, MultiModalKwargs |
| 32 | +from vllm.multimodal.parse import ImageProcessorItems, MultiModalDataItems |
| 33 | +from vllm.multimodal.processing import ( |
| 34 | + BaseMultiModalProcessor, |
| 35 | + PromptReplacement, |
| 36 | + PromptUpdate, |
| 37 | + PromptUpdateDetails, |
| 38 | +) |
| 39 | +from vllm.multimodal.profiling import BaseDummyInputsBuilder |
| 40 | + |
| 41 | +_I = TypeVar("_I", bound=Mistral3ProcessingInfo) |
| 42 | + |
| 43 | + |
| 44 | +class LightOnOCRMultiModalProcessor(BaseMultiModalProcessor[Mistral3ProcessingInfo]): |
| 45 | + def _call_hf_processor( |
| 46 | + self, |
| 47 | + prompt: str, |
| 48 | + mm_data: Mapping[str, object], |
| 49 | + mm_kwargs: Mapping[str, object], |
| 50 | + tok_kwargs: Mapping[str, object], |
| 51 | + ) -> BatchFeature: |
| 52 | + processed_outputs = super()._call_hf_processor( |
| 53 | + prompt=prompt, |
| 54 | + mm_data=mm_data, |
| 55 | + mm_kwargs=mm_kwargs, |
| 56 | + tok_kwargs=tok_kwargs, |
| 57 | + ) |
| 58 | + |
| 59 | + # NOTE: LightOnOCR does not use break/end tokens, so we remove them here. |
| 60 | + input_ids = processed_outputs.get("input_ids") |
| 61 | + if input_ids is not None: |
| 62 | + processor = self.info.get_hf_processor() |
| 63 | + tokenizer = self.info.get_tokenizer() |
| 64 | + vocab = tokenizer.get_vocab() |
| 65 | + |
| 66 | + break_id = vocab.get(processor.image_break_token) |
| 67 | + end_id = vocab.get(processor.image_end_token) |
| 68 | + |
| 69 | + # create mask to remove break/end tokens |
| 70 | + keep_mask = ~torch.isin( |
| 71 | + input_ids, |
| 72 | + torch.tensor([break_id, end_id]), |
| 73 | + ) |
| 74 | + |
| 75 | + processed_outputs["input_ids"] = input_ids[keep_mask].unsqueeze(0) |
| 76 | + if "attention_mask" in processed_outputs: |
| 77 | + processed_outputs["attention_mask"] = processed_outputs[ |
| 78 | + "attention_mask" |
| 79 | + ][keep_mask].unsqueeze(0) |
| 80 | + |
| 81 | + # un-pad pixel_values per-image so caches remain independent. |
| 82 | + pixel_values = processed_outputs.get("pixel_values") |
| 83 | + if pixel_values is not None: |
| 84 | + image_sizes = processed_outputs["image_sizes"] |
| 85 | + assert len(pixel_values) == len(image_sizes) |
| 86 | + processed_outputs["pixel_values"] = [ |
| 87 | + p[:, :h, :w] for p, (h, w) in zip(pixel_values, image_sizes) |
| 88 | + ] |
| 89 | + |
| 90 | + return processed_outputs |
| 91 | + |
| 92 | + def _get_mm_fields_config( |
| 93 | + self, |
| 94 | + hf_inputs: BatchFeature, |
| 95 | + hf_processor_mm_kwargs: Mapping[str, object], |
| 96 | + ) -> Mapping[str, MultiModalFieldConfig]: |
| 97 | + return dict( |
| 98 | + pixel_values=MultiModalFieldConfig.batched("image"), |
| 99 | + image_embeds=MultiModalFieldConfig.batched("image"), |
| 100 | + ) |
| 101 | + |
| 102 | + def _get_prompt_updates( |
| 103 | + self, |
| 104 | + mm_items: MultiModalDataItems, |
| 105 | + hf_processor_mm_kwargs: Mapping[str, object], |
| 106 | + out_mm_kwargs: MultiModalKwargs, |
| 107 | + ) -> Sequence[PromptUpdate]: |
| 108 | + hf_config = self.info.get_hf_config() |
| 109 | + image_token_id = hf_config.image_token_index |
| 110 | + |
| 111 | + assert isinstance(hf_config.vision_config, PixtralVisionConfig) |
| 112 | + encoder_info = PixtralHFEncoderInfo(hf_config) |
| 113 | + |
| 114 | + def replace(item_idx: int): |
| 115 | + images = mm_items.get_items("image", ImageProcessorItems) |
| 116 | + size = images.get_image_size(item_idx) |
| 117 | + ncols, nrows = encoder_info.get_patch_grid_size( |
| 118 | + image_width=size.width, image_height=size.height |
| 119 | + ) |
| 120 | + # break/end tokens are not used in LightOnOCR |
| 121 | + tokens = [image_token_id] * (ncols * nrows) |
| 122 | + return PromptUpdateDetails.select_token_id(tokens, image_token_id) |
| 123 | + |
| 124 | + return [ |
| 125 | + PromptReplacement( |
| 126 | + modality="image", target=[image_token_id], replacement=replace |
| 127 | + ) |
| 128 | + ] |
| 129 | + |
| 130 | + |
| 131 | +def _build_LightOnOCR_processor( |
| 132 | + info: _I, |
| 133 | + dummy_inputs: BaseDummyInputsBuilder[_I], |
| 134 | + *, |
| 135 | + cache: BaseMultiModalProcessorCache | None = None, |
| 136 | +): |
| 137 | + assert isinstance(info, Mistral3ProcessingInfo) |
| 138 | + return LightOnOCRMultiModalProcessor(info, dummy_inputs, cache=cache) |
| 139 | + |
| 140 | + |
| 141 | +@MULTIMODAL_REGISTRY.register_processor( |
| 142 | + _build_LightOnOCR_processor, |
| 143 | + info=_build_mistral3_info, |
| 144 | + dummy_inputs=Mistral3DummyInputsBuilder, |
| 145 | +) |
| 146 | +class LightOnOCRForConditionalGeneration(Mistral3ForConditionalGeneration): |
| 147 | + hf_to_vllm_mapper = WeightsMapper( |
| 148 | + orig_to_new_prefix={ |
| 149 | + "model.vision_encoder.": "vision_tower.", |
| 150 | + "model.vision_projection.": "multi_modal_projector.", |
| 151 | + "lm_head.": "language_model.lm_head.", |
| 152 | + "model.language_model.": "language_model.model.", |
| 153 | + } |
| 154 | + ) |
| 155 | + |
| 156 | + def __init__(self, *, vllm_config: VllmConfig, prefix: str = "") -> None: |
| 157 | + nn.Module.__init__(self) |
| 158 | + config = vllm_config.model_config.hf_config |
| 159 | + quant_config = vllm_config.quant_config |
| 160 | + multimodal_config = vllm_config.model_config.multimodal_config |
| 161 | + |
| 162 | + self.config = config |
| 163 | + self.multimodal_config = multimodal_config |
| 164 | + |
| 165 | + self.vision_tower = init_vision_tower_for_llava( |
| 166 | + config, |
| 167 | + quant_config, |
| 168 | + require_post_norm=False, |
| 169 | + prefix=maybe_prefix(prefix, "vision_tower"), |
| 170 | + ) |
| 171 | + |
| 172 | + self.multi_modal_projector = Mistral3MultiModalProjector( |
| 173 | + vision_hidden_size=config.vision_config.hidden_size, |
| 174 | + text_hidden_size=config.text_config.hidden_size, |
| 175 | + projector_hidden_act=config.projector_hidden_act, |
| 176 | + spatial_merge_size=config.spatial_merge_size, |
| 177 | + patch_size=config.vision_config.patch_size, |
| 178 | + multimodal_projector_bias=config.multimodal_projector_bias, |
| 179 | + quant_config=quant_config, |
| 180 | + prefix=maybe_prefix(prefix, "multi_modal_projector"), |
| 181 | + ) |
| 182 | + |
| 183 | + self.language_model = init_vllm_registered_model( |
| 184 | + vllm_config=vllm_config, |
| 185 | + hf_config=config.text_config, |
| 186 | + prefix=maybe_prefix(prefix, "language_model"), |
| 187 | + ) |
| 188 | + |
| 189 | + self.make_empty_intermediate_tensors = ( |
| 190 | + self.language_model.make_empty_intermediate_tensors |
| 191 | + ) |
| 192 | + |
| 193 | + def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]: |
| 194 | + loader = AutoWeightsLoader(self) |
| 195 | + return loader.load_weights(weights, mapper=self.hf_to_vllm_mapper) |
0 commit comments