configuration_siglip2.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  2. # This file was automatically generated from src/transformers/models/siglip2/modular_siglip2.py.
  3. # Do NOT edit this file manually as any edits will be overwritten by the generation of
  4. # the file from the modular. If any change should be done, please apply the change to the
  5. # modular_siglip2.py file directly. One of our CI enforces this.
  6. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  7. # coding=utf-8
  8. # Copyright 2025 The HuggingFace Inc. team.
  9. #
  10. # Licensed under the Apache License, Version 2.0 (the "License");
  11. # you may not use this file except in compliance with the License.
  12. # You may obtain a copy of the License at
  13. #
  14. # http://www.apache.org/licenses/LICENSE-2.0
  15. #
  16. # Unless required by applicable law or agreed to in writing, software
  17. # distributed under the License is distributed on an "AS IS" BASIS,
  18. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. # See the License for the specific language governing permissions and
  20. # limitations under the License.
  21. from ...configuration_utils import PretrainedConfig
  22. from ...utils import logging
  23. logger = logging.get_logger(__name__)
  24. class Siglip2TextConfig(PretrainedConfig):
  25. r"""
  26. This is the configuration class to store the configuration of a [`Siglip2TextModel`]. It is used to instantiate a
  27. Siglip2 text encoder according to the specified arguments, defining the model architecture. Instantiating a
  28. configuration with the defaults will yield a similar configuration to that of the text encoder of the Siglip2
  29. [google/siglip2-base-patch16-224](https://huggingface.co/google/siglip2-base-patch16-224) architecture.
  30. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  31. documentation from [`PretrainedConfig`] for more information.
  32. Args:
  33. vocab_size (`int`, *optional*, defaults to 32000):
  34. Vocabulary size of the Siglip2 text model. Defines the number of different tokens that can be represented by
  35. the `inputs_ids` passed when calling [`Siglip2Model`].
  36. hidden_size (`int`, *optional*, defaults to 768):
  37. Dimensionality of the encoder layers and the pooler layer.
  38. intermediate_size (`int`, *optional*, defaults to 3072):
  39. Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
  40. num_hidden_layers (`int`, *optional*, defaults to 12):
  41. Number of hidden layers in the Transformer encoder.
  42. num_attention_heads (`int`, *optional*, defaults to 12):
  43. Number of attention heads for each attention layer in the Transformer encoder.
  44. max_position_embeddings (`int`, *optional*, defaults to 64):
  45. The maximum sequence length that this model might ever be used with. Typically set this to something large
  46. just in case (e.g., 512 or 1024 or 2048).
  47. hidden_act (`str` or `function`, *optional*, defaults to `"gelu_pytorch_tanh"`):
  48. The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
  49. `"relu"`, `"selu"` and `"gelu_new"` `"quick_gelu"` are supported.
  50. layer_norm_eps (`float`, *optional*, defaults to 1e-06):
  51. The epsilon used by the layer normalization layers.
  52. attention_dropout (`float`, *optional*, defaults to 0.0):
  53. The dropout ratio for the attention probabilities.
  54. pad_token_id (`int`, *optional*, defaults to 1):
  55. The id of the padding token in the vocabulary.
  56. bos_token_id (`int`, *optional*, defaults to 49406):
  57. The id of the beginning-of-sequence token in the vocabulary.
  58. eos_token_id (`int`, *optional*, defaults to 49407):
  59. The id of the end-of-sequence token in the vocabulary.
  60. projection_size (`int`, *optional*, defaults to `hidden_size`):
  61. The size of the projection head.
  62. Example:
  63. ```python
  64. >>> from transformers import Siglip2TextConfig, Siglip2TextModel
  65. >>> # Initializing a Siglip2TextConfig with google/siglip2-base-patch16-224 style configuration
  66. >>> configuration = Siglip2TextConfig()
  67. >>> # Initializing a Siglip2TextModel (with random weights) from the google/siglip2-base-patch16-224 style configuration
  68. >>> model = Siglip2TextModel(configuration)
  69. >>> # Accessing the model configuration
  70. >>> configuration = model.config
  71. ```"""
  72. model_type = "siglip2_text_model"
  73. base_config_key = "text_config"
  74. def __init__(
  75. self,
  76. vocab_size=32000,
  77. hidden_size=768,
  78. intermediate_size=3072,
  79. num_hidden_layers=12,
  80. num_attention_heads=12,
  81. max_position_embeddings=64,
  82. hidden_act="gelu_pytorch_tanh",
  83. layer_norm_eps=1e-6,
  84. attention_dropout=0.0,
  85. # This differs from `CLIPTokenizer`'s default and from openai/siglip2
  86. # See https://github.com/huggingface/transformers/pull/24773#issuecomment-1632287538
  87. pad_token_id=1,
  88. bos_token_id=49406,
  89. eos_token_id=49407,
  90. projection_size=None,
  91. **kwargs,
  92. ):
  93. super().__init__(pad_token_id=pad_token_id, bos_token_id=bos_token_id, eos_token_id=eos_token_id, **kwargs)
  94. self.vocab_size = vocab_size
  95. self.hidden_size = hidden_size
  96. self.intermediate_size = intermediate_size
  97. self.num_hidden_layers = num_hidden_layers
  98. self.num_attention_heads = num_attention_heads
  99. self.max_position_embeddings = max_position_embeddings
  100. self.layer_norm_eps = layer_norm_eps
  101. self.hidden_act = hidden_act
  102. self.attention_dropout = attention_dropout
  103. self.projection_size = projection_size if projection_size is not None else hidden_size
  104. class Siglip2VisionConfig(PretrainedConfig):
  105. r"""
  106. This is the configuration class to store the configuration of a [`Siglip2VisionModel`]. It is used to instantiate a
  107. Siglip2 vision encoder according to the specified arguments, defining the model architecture. Instantiating a
  108. configuration with the defaults will yield a similar configuration to that of the vision encoder of the Siglip2
  109. [google/siglip2-base-patch16-naflex](https://huggingface.co/google/siglip2-base-patch16-naflex) architecture.
  110. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  111. documentation from [`PretrainedConfig`] for more information.
  112. Args:
  113. hidden_size (`int`, *optional*, defaults to 768):
  114. Dimensionality of the encoder layers and the pooler layer.
  115. intermediate_size (`int`, *optional*, defaults to 3072):
  116. Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
  117. num_hidden_layers (`int`, *optional*, defaults to 12):
  118. Number of hidden layers in the Transformer encoder.
  119. num_attention_heads (`int`, *optional*, defaults to 12):
  120. Number of attention heads for each attention layer in the Transformer encoder.
  121. num_channels (`int`, *optional*, defaults to 3):
  122. Number of channels in the input images.
  123. num_patches (`int`, *optional*, defaults to 256):
  124. The number of patches in the image with the size of (`patch_size`, `patch_size`).
  125. The image is resized to fill maximum of this number of patches, and to preserve
  126. the aspect ratio. In case the resulted number of patches is lower, the image is
  127. padded in "patch" dimension.
  128. patch_size (`int`, *optional*, defaults to 16):
  129. The size (resolution) of each patch.
  130. hidden_act (`str` or `function`, *optional*, defaults to `"gelu_pytorch_tanh"`):
  131. The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
  132. `"relu"`, `"selu"` and `"gelu_new"` `"quick_gelu"` are supported.
  133. layer_norm_eps (`float`, *optional*, defaults to 1e-06):
  134. The epsilon used by the layer normalization layers.
  135. attention_dropout (`float`, *optional*, defaults to 0.0):
  136. The dropout ratio for the attention probabilities.
  137. Example:
  138. ```python
  139. >>> from transformers import Siglip2VisionConfig, Siglip2VisionModel
  140. >>> # Initializing a Siglip2VisionConfig with google/siglip2-base-patch16-naflex style configuration
  141. >>> configuration = Siglip2VisionConfig()
  142. >>> # Initializing a Siglip2VisionModel (with random weights) from the google/siglip2-base-patch16-naflex style configuration
  143. >>> model = Siglip2VisionModel(configuration)
  144. >>> # Accessing the model configuration
  145. >>> configuration = model.config
  146. ```"""
  147. model_type = "siglip2_vision_model"
  148. base_config_key = "vision_config"
  149. def __init__(
  150. self,
  151. hidden_size=768,
  152. intermediate_size=3072,
  153. num_hidden_layers=12,
  154. num_attention_heads=12,
  155. num_channels=3,
  156. num_patches=256,
  157. patch_size=16,
  158. hidden_act="gelu_pytorch_tanh",
  159. layer_norm_eps=1e-6,
  160. attention_dropout=0.0,
  161. **kwargs,
  162. ):
  163. super().__init__(**kwargs)
  164. self.hidden_size = hidden_size
  165. self.intermediate_size = intermediate_size
  166. self.num_hidden_layers = num_hidden_layers
  167. self.num_attention_heads = num_attention_heads
  168. self.num_channels = num_channels
  169. self.patch_size = patch_size
  170. self.attention_dropout = attention_dropout
  171. self.layer_norm_eps = layer_norm_eps
  172. self.hidden_act = hidden_act
  173. self.num_patches = num_patches
  174. class Siglip2Config(PretrainedConfig):
  175. r"""
  176. [`Siglip2Config`] is the configuration class to store the configuration of a [`Siglip2Model`]. It is used to
  177. instantiate a Siglip2 model according to the specified arguments, defining the text model and vision model configs.
  178. Instantiating a configuration with the defaults will yield a similar configuration to that of the Siglip2
  179. [google/siglip2-base-patch16-224](https://huggingface.co/google/siglip2-base-patch16-224) architecture.
  180. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  181. documentation from [`PretrainedConfig`] for more information.
  182. Args:
  183. text_config (`dict`, *optional*):
  184. Dictionary of configuration options used to initialize [`Siglip2TextConfig`].
  185. vision_config (`dict`, *optional*):
  186. Dictionary of configuration options used to initialize [`Siglip2VisionConfig`].
  187. kwargs (*optional*):
  188. Dictionary of keyword arguments.
  189. Example:
  190. ```python
  191. >>> from transformers import Siglip2Config, Siglip2Model
  192. >>> # Initializing a Siglip2Config with google/siglip2-base-patch16-224 style configuration
  193. >>> configuration = Siglip2Config()
  194. >>> # Initializing a Siglip2Model (with random weights) from the google/siglip2-base-patch16-224 style configuration
  195. >>> model = Siglip2Model(configuration)
  196. >>> # Accessing the model configuration
  197. >>> configuration = model.config
  198. >>> # We can also initialize a Siglip2Config from a Siglip2TextConfig and a Siglip2VisionConfig
  199. >>> from transformers import Siglip2TextConfig, Siglip2VisionConfig
  200. >>> # Initializing a Siglip2Text and Siglip2Vision configuration
  201. >>> config_text = Siglip2TextConfig()
  202. >>> config_vision = Siglip2VisionConfig()
  203. >>> config = Siglip2Config.from_text_vision_configs(config_text, config_vision)
  204. ```"""
  205. model_type = "siglip2"
  206. sub_configs = {"text_config": Siglip2TextConfig, "vision_config": Siglip2VisionConfig}
  207. def __init__(self, text_config=None, vision_config=None, **kwargs):
  208. super().__init__(**kwargs)
  209. if text_config is None:
  210. text_config = {}
  211. logger.info("`text_config` is `None`. Initializing the `Siglip2TextConfig` with default values.")
  212. if vision_config is None:
  213. vision_config = {}
  214. logger.info("`vision_config` is `None`. initializing the `Siglip2VisionConfig` with default values.")
  215. self.text_config = Siglip2TextConfig(**text_config)
  216. self.vision_config = Siglip2VisionConfig(**vision_config)
  217. self.initializer_factor = 1.0
  218. __all__ = ["Siglip2Config", "Siglip2TextConfig", "Siglip2VisionConfig"]