configuration_internvl.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # Copyright 2025 HuggingFace Inc. team. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from huggingface_hub.dataclasses import strict
  15. from ...configuration_utils import PreTrainedConfig
  16. from ...utils import auto_docstring
  17. from ..auto import CONFIG_MAPPING, AutoConfig
  18. @auto_docstring(checkpoint="OpenGVLab/InternVL3-1B-hf")
  19. @strict
  20. class InternVLVisionConfig(PreTrainedConfig):
  21. r"""
  22. projection_dropout (`float`, *optional*, defaults to 0.0):
  23. Dropout probability for the projection layer.
  24. norm_type (`str`, *optional*, defaults to `"layer_norm"`):
  25. The type of normalization to use in the encoder. Can be `"layer_norm"` or `"rms_norm"`.
  26. use_mask_token (`bool`, *optional*, defaults to `False`):
  27. Whether to use a mask token for masked image modeling
  28. use_mean_pooling (`bool`, *optional*, defaults to `True`):
  29. Whether to mean pool the final hidden states of the patches instead of using the final hidden state of the
  30. CLS token, before applying the classification head.
  31. Example:
  32. ```python
  33. >>> from transformers import InternVLVisionConfig, InternVLVisionModel
  34. >>> # Initializing a InternVLVisionModel OpenGVLab/InternVL3-1B-hf style configuration
  35. >>> configuration = InternVLVisionConfig()
  36. >>> # Initializing a model (with random weights) from the OpenGVLab/InternVL3-1B-hf configuration
  37. >>> model = InternVLVisionModel(configuration)
  38. >>> # Accessing the model configuration
  39. >>> configuration = model.config
  40. ```"""
  41. model_type = "internvl_vision"
  42. base_config_key = "vision_config"
  43. hidden_size: int = 1024
  44. num_hidden_layers: int = 24
  45. num_attention_heads: int = 16
  46. attention_bias: bool = False
  47. use_qk_norm: bool = False
  48. intermediate_size: int = 4096
  49. hidden_act: str = "gelu"
  50. hidden_dropout_prob: float | int = 0.0
  51. attention_dropout: float | int = 0.0
  52. projection_dropout: float | int = 0.0
  53. initializer_range: float = 0.02
  54. norm_type: str = "layer_norm"
  55. layer_norm_eps: float = 1e-06
  56. image_size: int | list[int] | tuple[int, ...] = (448, 448)
  57. patch_size: int | list[int] | tuple[int, ...] = (14, 14)
  58. num_channels: int = 3
  59. use_mask_token: bool = False
  60. use_absolute_position_embeddings: bool = True
  61. layer_scale_init_value: float = 0.1
  62. use_mean_pooling: bool = True
  63. def __post_init__(self, **kwargs):
  64. self.image_size = (
  65. self.image_size if isinstance(self.image_size, (list, tuple)) else (self.image_size, self.image_size)
  66. )
  67. self.patch_size = (
  68. self.patch_size if isinstance(self.patch_size, (list, tuple)) else (self.patch_size, self.patch_size)
  69. )
  70. super().__post_init__(**kwargs)
  71. @auto_docstring(checkpoint="OpenGVLab/InternVL3-1B-hf")
  72. @strict
  73. class InternVLConfig(PreTrainedConfig):
  74. r"""
  75. downsample_ratio (`float`, *optional*, defaults to 0.5):
  76. Factor by which to downsample the image.
  77. Example:
  78. ```python
  79. >>> from transformers import InternVLForConditionalGeneration, InternVLConfig
  80. >>> # Initializing a InternVL style configuration
  81. >>> configuration = InternVLConfig()
  82. >>> # Initializing a model (with random weights) from the OpenGVLab/InternVL3-1B-hf configuration
  83. >>> model = InternVLForConditionalGeneration(configuration)
  84. >>> # Accessing the model configuration
  85. >>> configuration = model.config
  86. ```"""
  87. model_type = "internvl"
  88. sub_configs = {"text_config": AutoConfig, "vision_config": InternVLVisionConfig}
  89. vision_config: dict | PreTrainedConfig | None = None
  90. text_config: dict | PreTrainedConfig | None = None
  91. image_token_id: int = 151667
  92. image_seq_length: int = 256
  93. downsample_ratio: float = 0.5
  94. projector_hidden_act: str = "gelu"
  95. vision_feature_layer: int | list[int] = -1
  96. vision_feature_select_strategy: str = "default"
  97. tie_word_embeddings: bool = True
  98. def __post_init__(self, **kwargs):
  99. if isinstance(self.vision_config, dict):
  100. self.vision_config = InternVLVisionConfig(**self.vision_config)
  101. elif self.vision_config is None:
  102. self.vision_config = InternVLVisionConfig()
  103. if isinstance(self.text_config, dict):
  104. self.text_config["model_type"] = self.text_config.get("model_type", "qwen2")
  105. self.text_config = CONFIG_MAPPING[self.text_config["model_type"]](**self.text_config)
  106. elif self.text_config is None:
  107. self.text_config = CONFIG_MAPPING["qwen2"]()
  108. super().__post_init__(**kwargs)
  109. __all__ = ["InternVLVisionConfig", "InternVLConfig"]