|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2025 HuggingFace Inc. team. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | + |
| 17 | +from ...configuration_utils import PretrainedConfig |
| 18 | +from ..auto import CONFIG_MAPPING, AutoConfig |
| 19 | + |
| 20 | + |
| 21 | +class InternVLVisionConfig(PretrainedConfig): |
| 22 | + r""" |
| 23 | + This is the configuration class to store the configuration of a [`InternVLVisionModel`]. It is used to instantiate an InternVLVisionModel |
| 24 | + model according to the specified arguments, defining the model architecture. Instantiating a configuration with the defaults will yield |
| 25 | + a similar configuration to that of the InternVL3-1B. |
| 26 | + e.g. [OpenGVLab/InternVL3-1B-hf](https://huggingface.co/OpenGVLab/InternVL3-1B-hf) |
| 27 | +
|
| 28 | + Args: |
| 29 | + hidden_size (`int`, *optional*, defaults to 1024): |
| 30 | + Dimensionality of the encoder layers and the pooler layer. |
| 31 | + num_hidden_layers (`int`, *optional*, defaults to 24): |
| 32 | + Number of hidden layers in the Transformer encoder. |
| 33 | + num_attention_heads (`int`, *optional*, defaults to 16): |
| 34 | + Number of attention heads for each attention layer in the Transformer encoder. |
| 35 | + attention_bias (`bool`, *optional*, defaults to `False`): |
| 36 | + Whether to add a bias to the queries, keys and values. |
| 37 | + use_qk_norm (`bool`, *optional*, defaults to `False`): |
| 38 | + Whether to apply normalization to the queries and keys before the attention operation. |
| 39 | + intermediate_size (`int`, *optional*, defaults to 4096): |
| 40 | + Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder. |
| 41 | + hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`): |
| 42 | + The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`, |
| 43 | + `"relu"`, `"selu"` and `"gelu_new"` are supported. |
| 44 | + hidden_dropout_prob (`float`, *optional*, defaults to 0.0): |
| 45 | + The dropout probability for all fully connected layers in the embeddings, encoder, and pooler. |
| 46 | + attention_dropout (`float`, *optional*, defaults to 0.0): |
| 47 | + Dropout probability for attention weights. |
| 48 | + projection_dropout (`float`, *optional*, defaults to 0.0): |
| 49 | + Dropout probability for the projection layer. |
| 50 | + initializer_range (`float`, *optional*, defaults to 0.02): |
| 51 | + The standard deviation of the truncated_normal_initializer for initializing all weight matrices. |
| 52 | + norm_type (`str`, *optional*, defaults to `"layer_norm"`): |
| 53 | + The type of normalization to use in the encoder. Can be `"layer_norm"` or `"rms_norm"`. |
| 54 | + layer_norm_eps (`float`, *optional*, defaults to 1e-06): |
| 55 | + The epsilon used by the layer normalization layers. |
| 56 | + image_size (`int` or `list[int]`, *optional*, defaults to `[448, 448]`): |
| 57 | + The size (resolution) of each image. |
| 58 | + patch_size (`int` or `list[int]`, *optional*, defaults to `[14, 14]`): |
| 59 | + The size (resolution) of each patch. |
| 60 | + num_channels (`int`, *optional*, defaults to 3): |
| 61 | + The number of input channels. |
| 62 | + use_mask_token (`bool`, *optional*, defaults to `False`): |
| 63 | + Whether to use a mask token for masked image modeling. |
| 64 | + use_absolute_position_embeddings (`bool`, *optional*, defaults to `True`): |
| 65 | + Whether to use BERT-style absolute position embeddings. |
| 66 | + layer_scale_init_value (`float`, *optional*, defaults to 0.1): |
| 67 | + Scale to use in the self-attention layers. 0.1 for base, 1e-5 for large. Set 0 to disable layer scale. |
| 68 | + use_mean_pooling (`bool`, *optional*, defaults to `True`): |
| 69 | + Whether to mean pool the final hidden states of the patches instead of using the final hidden state of the |
| 70 | + CLS token, before applying the classification head. |
| 71 | +
|
| 72 | + Example: |
| 73 | +
|
| 74 | + ```python |
| 75 | + >>> from transformers import InternVLVisionConfig, InternVLVisionModel |
| 76 | +
|
| 77 | + >>> # Initializing a InternVLVisionModel OpenGVLab/InternVL3-1B-hf style configuration |
| 78 | + >>> configuration = InternVLVisionConfig() |
| 79 | +
|
| 80 | + >>> # Initializing a model (with random weights) from the OpenGVLab/InternVL3-1B-hf configuration |
| 81 | + >>> model = InternVLVisionModel(configuration) |
| 82 | +
|
| 83 | + >>> # Accessing the model configuration |
| 84 | + >>> configuration = model.config |
| 85 | + ```""" |
| 86 | + |
| 87 | + model_type = "internvl_vision" |
| 88 | + base_config_key = "vision_config" |
| 89 | + |
| 90 | + def __init__( |
| 91 | + self, |
| 92 | + hidden_size=1024, |
| 93 | + num_hidden_layers=24, |
| 94 | + num_attention_heads=16, |
| 95 | + attention_bias=False, |
| 96 | + use_qk_norm=False, |
| 97 | + intermediate_size=4096, |
| 98 | + hidden_act="gelu", |
| 99 | + hidden_dropout_prob=0.0, |
| 100 | + attention_dropout=0.0, |
| 101 | + projection_dropout=0.0, |
| 102 | + initializer_range=0.02, |
| 103 | + norm_type="layer_norm", |
| 104 | + layer_norm_eps=1e-06, |
| 105 | + image_size=[448, 448], |
| 106 | + patch_size=[14, 14], |
| 107 | + num_channels=3, |
| 108 | + use_mask_token=False, |
| 109 | + use_absolute_position_embeddings=True, |
| 110 | + layer_scale_init_value=0.1, |
| 111 | + use_mean_pooling=True, |
| 112 | + **kwargs, |
| 113 | + ): |
| 114 | + super().__init__(**kwargs) |
| 115 | + |
| 116 | + self.hidden_size = hidden_size |
| 117 | + self.num_hidden_layers = num_hidden_layers |
| 118 | + self.num_attention_heads = num_attention_heads |
| 119 | + self.attention_bias = attention_bias |
| 120 | + self.use_qk_norm = use_qk_norm |
| 121 | + self.intermediate_size = intermediate_size |
| 122 | + self.hidden_act = hidden_act |
| 123 | + self.hidden_dropout_prob = hidden_dropout_prob |
| 124 | + self.attention_dropout = attention_dropout |
| 125 | + self.projection_dropout = projection_dropout |
| 126 | + self.initializer_range = initializer_range |
| 127 | + self.norm_type = norm_type |
| 128 | + self.layer_norm_eps = layer_norm_eps |
| 129 | + |
| 130 | + image_size = image_size if isinstance(image_size, (list, tuple)) else (image_size, image_size) |
| 131 | + patch_size = patch_size if isinstance(patch_size, (list, tuple)) else (patch_size, patch_size) |
| 132 | + self.image_size = image_size |
| 133 | + self.patch_size = patch_size |
| 134 | + |
| 135 | + self.num_channels = num_channels |
| 136 | + self.use_mask_token = use_mask_token |
| 137 | + self.use_absolute_position_embeddings = use_absolute_position_embeddings |
| 138 | + self.layer_scale_init_value = layer_scale_init_value |
| 139 | + self.use_mean_pooling = use_mean_pooling |
| 140 | + |
| 141 | + |
| 142 | +class InternVLConfig(PretrainedConfig): |
| 143 | + r""" |
| 144 | + This is the configuration class to store the configuration of a [`InternVLForConditionalGeneration`]. It is used to instantiate a |
| 145 | + InternVL model according to the specified arguments, defining the model architecture. Instantiating a configuration |
| 146 | + with the defaults will yield a similar configuration to that of InternVL3-1B. |
| 147 | + e.g. [OpenGVLab/InternVL3-1B-hf](https://huggingface.co/OpenGVLab/InternVL3-1B-hf) |
| 148 | +
|
| 149 | + Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the |
| 150 | + documentation from [`PretrainedConfig`] for more information. |
| 151 | +
|
| 152 | +
|
| 153 | + Args: |
| 154 | + vision_config (`Union[AutoConfig, dict]`, *optional*, defaults to `InternVisonConfig`): |
| 155 | + The config object or dictionary of the vision backbone. |
| 156 | + text_config (`Union[AutoConfig, dict]`, *optional*, defaults to `Qwen2Config`): |
| 157 | + The config object or dictionary of the text backbone. |
| 158 | + image_token_id (`int`, *optional*, defaults to 151667): |
| 159 | + The image token index to encode the image prompt. |
| 160 | + image_seq_length (`int`, *optional*, defaults to 256): |
| 161 | + Number of image tokens to use per image patch. |
| 162 | + downsample_ratio (`float`, *optional*, defaults to 0.5): |
| 163 | + Factor by which to downsample the image. |
| 164 | + projector_hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`): |
| 165 | + The non-linear activation function (function or string) in the projector. |
| 166 | + vision_feature_layer (`int`, *optional*, defaults to -1): |
| 167 | + The index of the layer to use as the image features. |
| 168 | + vision_feature_select_strategy (`str`, *optional*, defaults to `"default"`): |
| 169 | + The feature selection strategy used to select the vision feature from the vision backbone. |
| 170 | + Can be one of `"default"` or `"full"`. |
| 171 | +
|
| 172 | + ```python |
| 173 | + >>> from transformers import InternVLForConditionalGeneration, InternVLConfig |
| 174 | +
|
| 175 | + >>> # Initializing a InternVL style configuration |
| 176 | + >>> configuration = InternVLConfig() |
| 177 | +
|
| 178 | + >>> # Initializing a model (with random weights) from the OpenGVLab/InternVL3-1B-hf configuration |
| 179 | + >>> model = InternVLForConditionalGeneration(configuration) |
| 180 | +
|
| 181 | + >>> # Accessing the model configuration |
| 182 | + >>> configuration = model.config |
| 183 | + ```""" |
| 184 | + |
| 185 | + model_type = "internvl" |
| 186 | + sub_configs = {"text_config": AutoConfig, "vision_config": InternVLVisionConfig} |
| 187 | + |
| 188 | + def __init__( |
| 189 | + self, |
| 190 | + vision_config=None, |
| 191 | + text_config=None, |
| 192 | + image_token_id=151667, |
| 193 | + image_seq_length=256, |
| 194 | + downsample_ratio=0.5, |
| 195 | + projector_hidden_act="gelu", |
| 196 | + vision_feature_layer=-1, |
| 197 | + vision_feature_select_strategy="default", |
| 198 | + **kwargs, |
| 199 | + ): |
| 200 | + self.image_token_id = image_token_id |
| 201 | + self.image_seq_length = image_seq_length |
| 202 | + self.downsample_ratio = downsample_ratio |
| 203 | + self.projector_hidden_act = projector_hidden_act |
| 204 | + self.vision_feature_layer = vision_feature_layer |
| 205 | + self.vision_feature_select_strategy = vision_feature_select_strategy |
| 206 | + |
| 207 | + if isinstance(vision_config, dict): |
| 208 | + self.vision_config = InternVLVisionConfig(**vision_config) |
| 209 | + elif isinstance(vision_config, InternVLVisionConfig): |
| 210 | + self.vision_config = vision_config |
| 211 | + elif vision_config is None: |
| 212 | + self.vision_config = InternVLVisionConfig() |
| 213 | + |
| 214 | + if isinstance(text_config, dict): |
| 215 | + text_config["model_type"] = text_config["model_type"] if "model_type" in text_config else "qwen2" |
| 216 | + text_config = CONFIG_MAPPING[text_config["model_type"]](**text_config) |
| 217 | + elif text_config is None: |
| 218 | + text_config = CONFIG_MAPPING["qwen2"]() |
| 219 | + |
| 220 | + self.text_config = text_config |
| 221 | + |
| 222 | + super().__init__(**kwargs) |
| 223 | + |
| 224 | + |
| 225 | +__all__ = ["InternVLVisionConfig", "InternVLConfig"] |
0 commit comments