| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | # adapted from https://github.com/deepseek-ai/DeepSeek-VL2/blob/faf18023f24b962b32d9f0a2d89e402a8d383a78/deepseek_vl2/models/modeling_deepseek_vl_v2.py#L115-L268 |
| 5 | |
| 6 | from huggingface_hub.dataclasses import strict |
| 7 | from transformers import DeepseekV2Config, PreTrainedConfig |
| 8 | |
| 9 | |
| 10 | class VisionEncoderConfig(PreTrainedConfig): |
| 11 | model_type: str = "vision" |
| 12 | |
| 13 | model_name: str = "vit_so400m_patch14_siglip_384.webli" |
| 14 | image_size: int = 384 |
| 15 | patch_size: int = 16 |
| 16 | width: int = 1024 |
| 17 | layers: int = 24 |
| 18 | heads: int = 16 |
| 19 | mlp_ratio: int = 4 |
| 20 | global_pool: str = "map" |
| 21 | ignore_head: bool = True |
| 22 | class_token: bool = False |
| 23 | num_classes: int = 0 |
| 24 | use_checkpoint: bool = False |
| 25 | weight_init: str = "skip" |
| 26 | deterministic: bool = False |
| 27 | num_recomputing_layers: int = 0 |
| 28 | |
| 29 | def __init__( |
| 30 | self, |
| 31 | model_name: str = "vit_so400m_patch14_siglip_384.webli", |
| 32 | image_size: int = 384, |
| 33 | patch_size: int = 16, |
| 34 | width: int = 1024, |
| 35 | layers: int = 24, |
| 36 | heads: int = 16, |
| 37 | mlp_ratio: int = 4, |
| 38 | global_pool: str = "map", |
| 39 | ignore_head: bool = True, |
| 40 | class_token: bool = False, |
| 41 | num_classes: int = 0, |
| 42 | use_checkpoint: bool = False, |
| 43 | **kwargs, |
| 44 | ): |
| 45 | self.model_name = model_name |
| 46 | self.image_size = image_size |
| 47 | self.patch_size = patch_size |
| 48 | self.width = width |
| 49 | self.layers = layers |
| 50 | self.heads = heads |
| 51 | self.mlp_ratio = mlp_ratio |
| 52 | self.global_pool = global_pool |
| 53 | self.ignore_head = ignore_head |
| 54 | self.class_token = class_token |
| 55 | self.num_classes = num_classes |
| 56 | self.use_checkpoint = use_checkpoint |
| 57 | |
| 58 | super().__init__(**kwargs) |
| 59 | |
| 60 | |
| 61 | class MlpProjectorConfig(PreTrainedConfig): |
| 62 | model_type = "mlp_projector" |
| 63 | projector_type: str = "downsample_mlp_gelu" |
| 64 | input_dim: int = 1152 |
| 65 | n_embed: int = 2048 |
| 66 | depth: int = 2 |
| 67 | mlp_ratio: int = 1 |
| 68 | downsample_ratio: int = 2 |
| 69 | token_pooling: bool = False |
| 70 | |
| 71 | def __init__( |
| 72 | self, |
| 73 | projector_type: str = "downsample_mlp_gelu", |
| 74 | input_dim: int = 1152, |
| 75 | n_embed: int = 2048, |
| 76 | depth: int = 2, |
| 77 | mlp_ratio: int = 1, |
| 78 | downsample_ratio: int = 2, |
| 79 | **kwargs, |
| 80 | ): |
| 81 | self.projector_type = projector_type |
| 82 | self.input_dim = input_dim |
| 83 | self.n_embed = n_embed |
| 84 | self.depth = depth |
| 85 | self.mlp_ratio = mlp_ratio |
| 86 | self.downsample_ratio = downsample_ratio |
| 87 | |
| 88 | super().__init__(**kwargs) |
| 89 | |
| 90 | |
| 91 | @strict |
| 92 | class DeepseekVLV2TextConfig(DeepseekV2Config): |
| 93 | # DeepSeek-VL2 checkpoints (e.g. deepseek-vl2-small) omit ``vocab_size`` and |
| 94 | # rely on the original DeepSeek-VL2 default of 102400; the generic |
| 95 | # Transformers ``DeepseekV2Config`` incorrectly defaults it to 32000. |
| 96 | # Upstream fix: https://github.com/huggingface/transformers/pull/48159 |
| 97 | vocab_size: int = 102400 |
| 98 | kv_lora_rank: int | None = DeepseekV2Config.kv_lora_rank |
| 99 | |
| 100 | |
| 101 | class DeepseekVLV2Config(PreTrainedConfig): |
| 102 | model_type = "deepseek_vl_v2" |
| 103 | |
| 104 | tile_tag: str = "2D" |
| 105 | global_view_pos: str = "head" |
| 106 | candidate_resolutions: tuple[tuple[int, int]] = ((384, 384),) |
| 107 | |
| 108 | def __init__( |
| 109 | self, |
| 110 | tile_tag: str = "tile_tag", |
| 111 | global_view_pos: str = "head", |
| 112 | candidate_resolutions: tuple[tuple[int, int]] = ((384, 384),), |
| 113 | **kwargs, |
| 114 | ): |
| 115 | architectures = kwargs.setdefault("architectures", ["DeepseekVLV2ForCausalLM"]) |
| 116 | |
| 117 | self.vision_config = VisionEncoderConfig(**kwargs.pop("vision_config", {})) |
| 118 | self.projector_config = MlpProjectorConfig(**kwargs.pop("projector_config", {})) |
| 119 | self.text_config = DeepseekVLV2TextConfig(**kwargs.pop("language_config", {})) |
| 120 | |
| 121 | self.tile_tag = tile_tag |
| 122 | self.global_view_pos = global_view_pos |
| 123 | self.candidate_resolutions = candidate_resolutions |
| 124 | self.vocab_size = self.text_config.vocab_size |
| 125 | |
| 126 | # update model_type for OCR models |
| 127 | if "DeepseekOCRForCausalLM" in architectures: |
| 128 | kwargs["model_type"] = "deepseek_ocr" |
| 129 | elif "DeepseekOCR2ForCausalLM" in architectures: |
| 130 | kwargs["model_type"] = "deepseek_ocr2" |
| 131 | super().__init__(**kwargs) |