configuration_ovis2.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # coding=utf-8
  2. # Copyright 2025 The 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. from ...configuration_utils import PretrainedConfig
  16. from ..qwen2.configuration_qwen2 import Qwen2Config
  17. class Ovis2VisionConfig(PretrainedConfig):
  18. r"""
  19. This is the configuration class to store the configuration of a [`Ovis2VisionModel`]. It is used to instantiate a
  20. Ovis2VisionModel model according to the specified arguments, defining the model architecture. Instantiating a configuration
  21. with the defaults will yield a similar configuration to that of Ovis2.
  22. Args:
  23. hidden_size (`int`, *optional*, defaults to 1024):
  24. Dimensionality of the encoder layers and the pooler layer.
  25. intermediate_size (`int`, *optional*, defaults to 2816):
  26. Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
  27. num_hidden_layers (`int`, *optional*, defaults to 24):
  28. Number of hidden layers in the Transformer encoder.
  29. num_attention_heads (`int`, *optional*, defaults to 8):
  30. Number of attention heads for each attention layer in the Transformer encoder.
  31. num_channels (`int`, *optional*, defaults to 3):
  32. Number of channels in the input images.
  33. image_size (`int`, *optional*, defaults to 224):
  34. The size (resolution) of each image.
  35. patch_size (`int`, *optional*, defaults to 14):
  36. The size (resolution) of each patch.
  37. rms_norm_eps (`float`, *optional*, defaults to 1e-05):
  38. The epsilon used by the RMSNorm layers.
  39. attention_dropout (`float`, *optional*, defaults to 0.0):
  40. The dropout ratio for the attention probabilities.
  41. qkv_bias (`bool`, *optional*, defaults to `False`):
  42. Whether to add a learnable bias to the query, key, and value sequences at each attention head.
  43. mlp_bias (`bool`, *optional*, defaults to `False`):
  44. Whether to add a learnable bias to the MLP layers.
  45. hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
  46. The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
  47. `"relu"`, `"selu"` and `"gelu_new"` `"quick_gelu"` are supported.
  48. vocab_size (`int`, *optional*, defaults to 16384):
  49. Vocabulary size of the Vision Transformer.
  50. hidden_stride (`int`, *optional*, defaults to 1):
  51. The stride of the hidden layer in the Vision Transformer.
  52. num_visual_indicator_tokens (`int`, *optional*, defaults to 5):
  53. Number of visual indicator tokens.
  54. initializer_range (`float`, *optional*, defaults to 0.02):
  55. The standard deviation of the truncated normal initializer for initializing all weight matrices.
  56. tokenize_function (`str`, *optional*, defaults to `"softmax"`):
  57. The function used to tokenize the visual indicator tokens.
  58. """
  59. base_config_key = "vision_config"
  60. def __init__(
  61. self,
  62. hidden_size: int = 1024,
  63. intermediate_size: int = 2816,
  64. num_hidden_layers: int = 24,
  65. num_attention_heads: int = 8,
  66. num_channels: int = 3,
  67. image_size: int = 224,
  68. patch_size: int = 14,
  69. rms_norm_eps: float = 1e-5,
  70. attention_dropout: float = 0.0,
  71. qkv_bias: bool = False,
  72. mlp_bias: bool = False,
  73. hidden_act="silu",
  74. vocab_size=16384,
  75. hidden_stride=1,
  76. num_visual_indicator_tokens=5,
  77. initializer_range=0.02,
  78. tokenize_function="softmax",
  79. **kwargs,
  80. ):
  81. super().__init__(**kwargs)
  82. self.hidden_size = hidden_size
  83. self.intermediate_size = intermediate_size
  84. self.num_hidden_layers = num_hidden_layers
  85. self.num_attention_heads = num_attention_heads
  86. self.num_channels = num_channels
  87. self.patch_size = patch_size
  88. self.image_size = image_size
  89. self.attention_dropout = attention_dropout
  90. self.hidden_act = hidden_act
  91. self.qkv_bias = qkv_bias
  92. self.mlp_bias = mlp_bias
  93. self.rms_norm_eps = rms_norm_eps
  94. self.vocab_size = vocab_size
  95. self.hidden_stride = hidden_stride
  96. self.num_visual_indicator_tokens = num_visual_indicator_tokens
  97. self.tokenize_function = tokenize_function
  98. self.initializer_range = initializer_range
  99. class Ovis2Config(PretrainedConfig):
  100. r"""
  101. This is the configuration class to store the configuration of a [`Ovis2ForConditionalGeneration`]. It is used to instantiate a
  102. Ovis2 model according to the specified arguments, defining the model architecture. Instantiating a configuration
  103. with the defaults will yield a similar configuration to that of Ovis2.
  104. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  105. documentation from [`PretrainedConfig`] for more information.
  106. e.g. [thisisiron/Ovis2-1B-hf](https://huggingface.co/thisisiron/Ovis2-1B-hf)
  107. Args:
  108. vision_config (`Union[AutoConfig, dict]`, *optional*, defaults to `Ovis2VisionConfig`):
  109. The config object or dictionary of the vision backbone.
  110. text_config (`Union[AutoConfig, dict]`, *optional*, defaults to `Qwen2Config`):
  111. The config object or dictionary of the text backbone.
  112. image_token_id (`int`, *optional*, defaults to 151665):
  113. The image token id to encode the image prompt.
  114. visual_indicator_token_ids (`List[int]`, *optional*, defaults to `[151666, 151667, 151668, 151669, 151670]`):
  115. The visual indicator token ids to encode the image prompt.
  116. vocab_size (`int`, *optional*, defaults to 151643):
  117. Vocabulary size of the text model.
  118. hidden_size (`int`, *optional*, defaults to 1536):
  119. Dimensionality of the encoder layers and the pooler layer.
  120. ```python
  121. >>> from transformers import Ovis2ForConditionalGeneration, Ovis2Config
  122. >>> # Initializing a Ovis2 style configuration
  123. >>> configuration = Ovis2Config()
  124. >>> # Initializing a model from the Ovis2-2B style configuration
  125. >>> model = Ovis2ForConditionalGeneration(configuration)
  126. >>> # Accessing the model configuration
  127. >>> configuration = model.config
  128. ```
  129. """
  130. model_type = "ovis2"
  131. sub_configs = {"text_config": Qwen2Config, "vision_config": Ovis2VisionConfig}
  132. def __init__(
  133. self,
  134. vision_config=None,
  135. text_config=None,
  136. image_token_id=151665,
  137. visual_indicator_token_ids=[151666, 151667, 151668, 151669, 151670],
  138. vocab_size=151643,
  139. hidden_size=1536,
  140. **kwargs,
  141. ):
  142. if isinstance(vision_config, dict):
  143. self.vision_config = Ovis2VisionConfig(**vision_config)
  144. elif isinstance(vision_config, Ovis2VisionConfig):
  145. self.vision_config = vision_config
  146. if vision_config is None:
  147. self.vision_config = Ovis2VisionConfig(num_visual_indicator_tokens=len(visual_indicator_token_ids))
  148. if isinstance(text_config, dict):
  149. self.text_config = Qwen2Config(**text_config)
  150. elif isinstance(text_config, Qwen2Config):
  151. self.text_config = text_config
  152. elif text_config is None:
  153. self.text_config = Qwen2Config()
  154. self.vocab_size = vocab_size
  155. self.hidden_size = hidden_size
  156. self.image_token_id = image_token_id
  157. self.visual_indicator_token_ids = visual_indicator_token_ids
  158. super().__init__(**kwargs)
  159. __all__ = ["Ovis2VisionConfig", "Ovis2Config"]