configuration_siglip.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. # coding=utf-8
  2. # Copyright 2024 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. """Siglip model configuration"""
  16. from ...configuration_utils import PretrainedConfig
  17. from ...utils import logging
  18. logger = logging.get_logger(__name__)
  19. class SiglipTextConfig(PretrainedConfig):
  20. r"""
  21. This is the configuration class to store the configuration of a [`SiglipTextModel`]. It is used to instantiate a
  22. Siglip text encoder according to the specified arguments, defining the model architecture. Instantiating a
  23. configuration with the defaults will yield a similar configuration to that of the text encoder of the Siglip
  24. [google/siglip-base-patch16-224](https://huggingface.co/google/siglip-base-patch16-224) architecture.
  25. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  26. documentation from [`PretrainedConfig`] for more information.
  27. Args:
  28. vocab_size (`int`, *optional*, defaults to 32000):
  29. Vocabulary size of the Siglip text model. Defines the number of different tokens that can be represented by
  30. the `inputs_ids` passed when calling [`SiglipModel`].
  31. hidden_size (`int`, *optional*, defaults to 768):
  32. Dimensionality of the encoder layers and the pooler layer.
  33. intermediate_size (`int`, *optional*, defaults to 3072):
  34. Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
  35. num_hidden_layers (`int`, *optional*, defaults to 12):
  36. Number of hidden layers in the Transformer encoder.
  37. num_attention_heads (`int`, *optional*, defaults to 12):
  38. Number of attention heads for each attention layer in the Transformer encoder.
  39. max_position_embeddings (`int`, *optional*, defaults to 64):
  40. The maximum sequence length that this model might ever be used with. Typically set this to something large
  41. just in case (e.g., 512 or 1024 or 2048).
  42. hidden_act (`str` or `function`, *optional*, defaults to `"gelu_pytorch_tanh"`):
  43. The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
  44. `"relu"`, `"selu"` and `"gelu_new"` `"quick_gelu"` are supported.
  45. layer_norm_eps (`float`, *optional*, defaults to 1e-06):
  46. The epsilon used by the layer normalization layers.
  47. attention_dropout (`float`, *optional*, defaults to 0.0):
  48. The dropout ratio for the attention probabilities.
  49. pad_token_id (`int`, *optional*, defaults to 1):
  50. The id of the padding token in the vocabulary.
  51. bos_token_id (`int`, *optional*, defaults to 49406):
  52. The id of the beginning-of-sequence token in the vocabulary.
  53. eos_token_id (`int`, *optional*, defaults to 49407):
  54. The id of the end-of-sequence token in the vocabulary.
  55. projection_size (`int`, *optional*, defaults to `hidden_size`):
  56. The size of the projection head.
  57. Example:
  58. ```python
  59. >>> from transformers import SiglipTextConfig, SiglipTextModel
  60. >>> # Initializing a SiglipTextConfig with google/siglip-base-patch16-224 style configuration
  61. >>> configuration = SiglipTextConfig()
  62. >>> # Initializing a SiglipTextModel (with random weights) from the google/siglip-base-patch16-224 style configuration
  63. >>> model = SiglipTextModel(configuration)
  64. >>> # Accessing the model configuration
  65. >>> configuration = model.config
  66. ```"""
  67. model_type = "siglip_text_model"
  68. base_config_key = "text_config"
  69. def __init__(
  70. self,
  71. vocab_size=32000,
  72. hidden_size=768,
  73. intermediate_size=3072,
  74. num_hidden_layers=12,
  75. num_attention_heads=12,
  76. max_position_embeddings=64,
  77. hidden_act="gelu_pytorch_tanh",
  78. layer_norm_eps=1e-6,
  79. attention_dropout=0.0,
  80. # This differs from `CLIPTokenizer`'s default and from openai/siglip
  81. # See https://github.com/huggingface/transformers/pull/24773#issuecomment-1632287538
  82. pad_token_id=1,
  83. bos_token_id=49406,
  84. eos_token_id=49407,
  85. projection_size=None,
  86. **kwargs,
  87. ):
  88. super().__init__(pad_token_id=pad_token_id, bos_token_id=bos_token_id, eos_token_id=eos_token_id, **kwargs)
  89. self.vocab_size = vocab_size
  90. self.hidden_size = hidden_size
  91. self.intermediate_size = intermediate_size
  92. self.num_hidden_layers = num_hidden_layers
  93. self.num_attention_heads = num_attention_heads
  94. self.max_position_embeddings = max_position_embeddings
  95. self.layer_norm_eps = layer_norm_eps
  96. self.hidden_act = hidden_act
  97. self.attention_dropout = attention_dropout
  98. self.projection_size = projection_size if projection_size is not None else hidden_size
  99. class SiglipVisionConfig(PretrainedConfig):
  100. r"""
  101. This is the configuration class to store the configuration of a [`SiglipVisionModel`]. It is used to instantiate a
  102. Siglip vision encoder according to the specified arguments, defining the model architecture. Instantiating a
  103. configuration with the defaults will yield a similar configuration to that of the vision encoder of the Siglip
  104. [google/siglip-base-patch16-224](https://huggingface.co/google/siglip-base-patch16-224) architecture.
  105. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  106. documentation from [`PretrainedConfig`] for more information.
  107. Args:
  108. hidden_size (`int`, *optional*, defaults to 768):
  109. Dimensionality of the encoder layers and the pooler layer.
  110. intermediate_size (`int`, *optional*, defaults to 3072):
  111. Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
  112. num_hidden_layers (`int`, *optional*, defaults to 12):
  113. Number of hidden layers in the Transformer encoder.
  114. num_attention_heads (`int`, *optional*, defaults to 12):
  115. Number of attention heads for each attention layer in the Transformer encoder.
  116. num_channels (`int`, *optional*, defaults to 3):
  117. Number of channels in the input images.
  118. image_size (`int`, *optional*, defaults to 224):
  119. The size (resolution) of each image.
  120. patch_size (`int`, *optional*, defaults to 16):
  121. The size (resolution) of each patch.
  122. hidden_act (`str` or `function`, *optional*, defaults to `"gelu_pytorch_tanh"`):
  123. The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
  124. `"relu"`, `"selu"` and `"gelu_new"` `"quick_gelu"` are supported.
  125. layer_norm_eps (`float`, *optional*, defaults to 1e-06):
  126. The epsilon used by the layer normalization layers.
  127. attention_dropout (`float`, *optional*, defaults to 0.0):
  128. The dropout ratio for the attention probabilities.
  129. Example:
  130. ```python
  131. >>> from transformers import SiglipVisionConfig, SiglipVisionModel
  132. >>> # Initializing a SiglipVisionConfig with google/siglip-base-patch16-224 style configuration
  133. >>> configuration = SiglipVisionConfig()
  134. >>> # Initializing a SiglipVisionModel (with random weights) from the google/siglip-base-patch16-224 style configuration
  135. >>> model = SiglipVisionModel(configuration)
  136. >>> # Accessing the model configuration
  137. >>> configuration = model.config
  138. ```"""
  139. model_type = "siglip_vision_model"
  140. base_config_key = "vision_config"
  141. def __init__(
  142. self,
  143. hidden_size=768,
  144. intermediate_size=3072,
  145. num_hidden_layers=12,
  146. num_attention_heads=12,
  147. num_channels=3,
  148. image_size=224,
  149. patch_size=16,
  150. hidden_act="gelu_pytorch_tanh",
  151. layer_norm_eps=1e-6,
  152. attention_dropout=0.0,
  153. **kwargs,
  154. ):
  155. super().__init__(**kwargs)
  156. self.hidden_size = hidden_size
  157. self.intermediate_size = intermediate_size
  158. self.num_hidden_layers = num_hidden_layers
  159. self.num_attention_heads = num_attention_heads
  160. self.num_channels = num_channels
  161. self.patch_size = patch_size
  162. self.image_size = image_size
  163. self.attention_dropout = attention_dropout
  164. self.layer_norm_eps = layer_norm_eps
  165. self.hidden_act = hidden_act
  166. class SiglipConfig(PretrainedConfig):
  167. r"""
  168. [`SiglipConfig`] is the configuration class to store the configuration of a [`SiglipModel`]. It is used to
  169. instantiate a Siglip model according to the specified arguments, defining the text model and vision model configs.
  170. Instantiating a configuration with the defaults will yield a similar configuration to that of the Siglip
  171. [google/siglip-base-patch16-224](https://huggingface.co/google/siglip-base-patch16-224) architecture.
  172. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  173. documentation from [`PretrainedConfig`] for more information.
  174. Args:
  175. text_config (`dict`, *optional*):
  176. Dictionary of configuration options used to initialize [`SiglipTextConfig`].
  177. vision_config (`dict`, *optional*):
  178. Dictionary of configuration options used to initialize [`SiglipVisionConfig`].
  179. kwargs (*optional*):
  180. Dictionary of keyword arguments.
  181. Example:
  182. ```python
  183. >>> from transformers import SiglipConfig, SiglipModel
  184. >>> # Initializing a SiglipConfig with google/siglip-base-patch16-224 style configuration
  185. >>> configuration = SiglipConfig()
  186. >>> # Initializing a SiglipModel (with random weights) from the google/siglip-base-patch16-224 style configuration
  187. >>> model = SiglipModel(configuration)
  188. >>> # Accessing the model configuration
  189. >>> configuration = model.config
  190. >>> # We can also initialize a SiglipConfig from a SiglipTextConfig and a SiglipVisionConfig
  191. >>> from transformers import SiglipTextConfig, SiglipVisionConfig
  192. >>> # Initializing a SiglipText and SiglipVision configuration
  193. >>> config_text = SiglipTextConfig()
  194. >>> config_vision = SiglipVisionConfig()
  195. >>> config = SiglipConfig.from_text_vision_configs(config_text, config_vision)
  196. ```"""
  197. model_type = "siglip"
  198. sub_configs = {"text_config": SiglipTextConfig, "vision_config": SiglipVisionConfig}
  199. def __init__(self, text_config=None, vision_config=None, **kwargs):
  200. super().__init__(**kwargs)
  201. if text_config is None:
  202. text_config = {}
  203. logger.info("`text_config` is `None`. Initializing the `SiglipTextConfig` with default values.")
  204. if vision_config is None:
  205. vision_config = {}
  206. logger.info("`vision_config` is `None`. initializing the `SiglipVisionConfig` with default values.")
  207. self.text_config = SiglipTextConfig(**text_config)
  208. self.vision_config = SiglipVisionConfig(**vision_config)
  209. self.initializer_factor = 1.0
  210. __all__ = ["SiglipConfig", "SiglipTextConfig", "SiglipVisionConfig"]