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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 17 |
+
import os
|
| 18 |
+
from typing import Union
|
| 19 |
+
|
| 20 |
+
from transformers.configuration_utils import PretrainedConfig
|
| 21 |
+
from transformers.utils import logging
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
logger = logging.get_logger(__name__)
|
| 25 |
+
|
| 26 |
+
SIGLIP_PRETRAINED_CONFIG_ARCHIVE_MAP = {
|
| 27 |
+
"google/siglip-base-patch16-224": "https://huggingface.co/google/siglip-base-patch16-224/resolve/main/config.json",
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class SiglipTextConfig(PretrainedConfig):
|
| 32 |
+
r"""
|
| 33 |
+
This is the configuration class to store the configuration of a [`SiglipTextModel`]. It is used to instantiate a
|
| 34 |
+
Siglip text encoder according to the specified arguments, defining the model architecture. Instantiating a
|
| 35 |
+
configuration with the defaults will yield a similar configuration to that of the text encoder of the Siglip
|
| 36 |
+
[google/siglip-base-patch16-224](https://huggingface.co/google/siglip-base-patch16-224) architecture.
|
| 37 |
+
|
| 38 |
+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
|
| 39 |
+
documentation from [`PretrainedConfig`] for more information.
|
| 40 |
+
|
| 41 |
+
Args:
|
| 42 |
+
vocab_size (`int`, *optional*, defaults to 32000):
|
| 43 |
+
Vocabulary size of the Siglip text model. Defines the number of different tokens that can be represented by
|
| 44 |
+
the `inputs_ids` passed when calling [`SiglipModel`].
|
| 45 |
+
hidden_size (`int`, *optional*, defaults to 768):
|
| 46 |
+
Dimensionality of the encoder layers and the pooler layer.
|
| 47 |
+
intermediate_size (`int`, *optional*, defaults to 3072):
|
| 48 |
+
Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
|
| 49 |
+
num_hidden_layers (`int`, *optional*, defaults to 12):
|
| 50 |
+
Number of hidden layers in the Transformer encoder.
|
| 51 |
+
num_attention_heads (`int`, *optional*, defaults to 12):
|
| 52 |
+
Number of attention heads for each attention layer in the Transformer encoder.
|
| 53 |
+
max_position_embeddings (`int`, *optional*, defaults to 64):
|
| 54 |
+
The maximum sequence length that this model might ever be used with. Typically set this to something large
|
| 55 |
+
just in case (e.g., 512 or 1024 or 2048).
|
| 56 |
+
hidden_act (`str` or `function`, *optional*, defaults to `"gelu_pytorch_tanh"`):
|
| 57 |
+
The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
|
| 58 |
+
`"relu"`, `"selu"` and `"gelu_new"` `"quick_gelu"` are supported.
|
| 59 |
+
layer_norm_eps (`float`, *optional*, defaults to 1e-06):
|
| 60 |
+
The epsilon used by the layer normalization layers.
|
| 61 |
+
attention_dropout (`float`, *optional*, defaults to 0.0):
|
| 62 |
+
The dropout ratio for the attention probabilities.
|
| 63 |
+
pad_token_id (`int`, *optional*, defaults to 1):
|
| 64 |
+
The id of the padding token in the vocabulary.
|
| 65 |
+
bos_token_id (`int`, *optional*, defaults to 49406):
|
| 66 |
+
The id of the beginning-of-sequence token in the vocabulary.
|
| 67 |
+
eos_token_id (`int`, *optional*, defaults to 49407):
|
| 68 |
+
The id of the end-of-sequence token in the vocabulary.
|
| 69 |
+
|
| 70 |
+
Example:
|
| 71 |
+
|
| 72 |
+
```python
|
| 73 |
+
>>> from transformers import SiglipTextConfig, SiglipTextModel
|
| 74 |
+
|
| 75 |
+
>>> # Initializing a SiglipTextConfig with google/siglip-base-patch16-224 style configuration
|
| 76 |
+
>>> configuration = SiglipTextConfig()
|
| 77 |
+
|
| 78 |
+
>>> # Initializing a SiglipTextModel (with random weights) from the google/siglip-base-patch16-224 style configuration
|
| 79 |
+
>>> model = SiglipTextModel(configuration)
|
| 80 |
+
|
| 81 |
+
>>> # Accessing the model configuration
|
| 82 |
+
>>> configuration = model.config
|
| 83 |
+
```"""
|
| 84 |
+
|
| 85 |
+
model_type = "siglip_text_model"
|
| 86 |
+
|
| 87 |
+
def __init__(
|
| 88 |
+
self,
|
| 89 |
+
vocab_size=32000,
|
| 90 |
+
hidden_size=768,
|
| 91 |
+
intermediate_size=3072,
|
| 92 |
+
num_hidden_layers=12,
|
| 93 |
+
num_attention_heads=12,
|
| 94 |
+
max_position_embeddings=64,
|
| 95 |
+
hidden_act="gelu_pytorch_tanh",
|
| 96 |
+
layer_norm_eps=1e-6,
|
| 97 |
+
attention_dropout=0.0,
|
| 98 |
+
# This differs from `CLIPTokenizer`'s default and from openai/siglip
|
| 99 |
+
# See https://github.com/huggingface/transformers/pull/24773#issuecomment-1632287538
|
| 100 |
+
pad_token_id=1,
|
| 101 |
+
bos_token_id=49406,
|
| 102 |
+
eos_token_id=49407,
|
| 103 |
+
_flash_attn_2_enabled=True,
|
| 104 |
+
**kwargs,
|
| 105 |
+
):
|
| 106 |
+
super().__init__(pad_token_id=pad_token_id, bos_token_id=bos_token_id, eos_token_id=eos_token_id, **kwargs)
|
| 107 |
+
|
| 108 |
+
self.vocab_size = vocab_size
|
| 109 |
+
self.hidden_size = hidden_size
|
| 110 |
+
self.intermediate_size = intermediate_size
|
| 111 |
+
self.num_hidden_layers = num_hidden_layers
|
| 112 |
+
self.num_attention_heads = num_attention_heads
|
| 113 |
+
self.max_position_embeddings = max_position_embeddings
|
| 114 |
+
self.layer_norm_eps = layer_norm_eps
|
| 115 |
+
self.hidden_act = hidden_act
|
| 116 |
+
self.attention_dropout = attention_dropout
|
| 117 |
+
self._flash_attn_2_enabled = _flash_attn_2_enabled
|
| 118 |
+
|
| 119 |
+
@classmethod
|
| 120 |
+
def from_pretrained(cls, pretrained_model_name_or_path: Union[str, os.PathLike], **kwargs) -> "PretrainedConfig":
|
| 121 |
+
cls._set_token_in_kwargs(kwargs)
|
| 122 |
+
|
| 123 |
+
config_dict, kwargs = cls.get_config_dict(pretrained_model_name_or_path, **kwargs)
|
| 124 |
+
|
| 125 |
+
# get the text config dict if we are loading from SiglipConfig
|
| 126 |
+
if config_dict.get("model_type") == "siglip":
|
| 127 |
+
config_dict = config_dict["text_config"]
|
| 128 |
+
|
| 129 |
+
if "model_type" in config_dict and hasattr(cls, "model_type") and config_dict["model_type"] != cls.model_type:
|
| 130 |
+
logger.warning(
|
| 131 |
+
f"You are using a model of type {config_dict['model_type']} to instantiate a model of type "
|
| 132 |
+
f"{cls.model_type}. This is not supported for all configurations of models and can yield errors."
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
return cls.from_dict(config_dict, **kwargs)
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
class SiglipVisionConfig(PretrainedConfig):
|
| 139 |
+
r"""
|
| 140 |
+
This is the configuration class to store the configuration of a [`SiglipVisionModel`]. It is used to instantiate a
|
| 141 |
+
Siglip vision encoder according to the specified arguments, defining the model architecture. Instantiating a
|
| 142 |
+
configuration with the defaults will yield a similar configuration to that of the vision encoder of the Siglip
|
| 143 |
+
[google/siglip-base-patch16-224](https://huggingface.co/google/siglip-base-patch16-224) architecture.
|
| 144 |
+
|
| 145 |
+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
|
| 146 |
+
documentation from [`PretrainedConfig`] for more information.
|
| 147 |
+
|
| 148 |
+
Args:
|
| 149 |
+
hidden_size (`int`, *optional*, defaults to 768):
|
| 150 |
+
Dimensionality of the encoder layers and the pooler layer.
|
| 151 |
+
intermediate_size (`int`, *optional*, defaults to 3072):
|
| 152 |
+
Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
|
| 153 |
+
num_hidden_layers (`int`, *optional*, defaults to 12):
|
| 154 |
+
Number of hidden layers in the Transformer encoder.
|
| 155 |
+
num_attention_heads (`int`, *optional*, defaults to 12):
|
| 156 |
+
Number of attention heads for each attention layer in the Transformer encoder.
|
| 157 |
+
num_channels (`int`, *optional*, defaults to 3):
|
| 158 |
+
Number of channels in the input images.
|
| 159 |
+
image_size (`int`, *optional*, defaults to 224):
|
| 160 |
+
The size (resolution) of each image.
|
| 161 |
+
patch_size (`int`, *optional*, defaults to 16):
|
| 162 |
+
The size (resolution) of each patch.
|
| 163 |
+
hidden_act (`str` or `function`, *optional*, defaults to `"gelu_pytorch_tanh"`):
|
| 164 |
+
The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
|
| 165 |
+
`"relu"`, `"selu"` and `"gelu_new"` ``"quick_gelu"` are supported.
|
| 166 |
+
layer_norm_eps (`float`, *optional*, defaults to 1e-06):
|
| 167 |
+
The epsilon used by the layer normalization layers.
|
| 168 |
+
attention_dropout (`float`, *optional*, defaults to 0.0):
|
| 169 |
+
The dropout ratio for the attention probabilities.
|
| 170 |
+
|
| 171 |
+
Example:
|
| 172 |
+
|
| 173 |
+
```python
|
| 174 |
+
>>> from transformers import SiglipVisionConfig, SiglipVisionModel
|
| 175 |
+
|
| 176 |
+
>>> # Initializing a SiglipVisionConfig with google/siglip-base-patch16-224 style configuration
|
| 177 |
+
>>> configuration = SiglipVisionConfig()
|
| 178 |
+
|
| 179 |
+
>>> # Initializing a SiglipVisionModel (with random weights) from the google/siglip-base-patch16-224 style configuration
|
| 180 |
+
>>> model = SiglipVisionModel(configuration)
|
| 181 |
+
|
| 182 |
+
>>> # Accessing the model configuration
|
| 183 |
+
>>> configuration = model.config
|
| 184 |
+
```"""
|
| 185 |
+
|
| 186 |
+
model_type = "siglip_vision_model"
|
| 187 |
+
|
| 188 |
+
def __init__(
|
| 189 |
+
self,
|
| 190 |
+
hidden_size=768,
|
| 191 |
+
intermediate_size=3072,
|
| 192 |
+
num_hidden_layers=12,
|
| 193 |
+
num_attention_heads=12,
|
| 194 |
+
num_channels=3,
|
| 195 |
+
image_size=224,
|
| 196 |
+
patch_size=16,
|
| 197 |
+
hidden_act="gelu_pytorch_tanh",
|
| 198 |
+
layer_norm_eps=1e-6,
|
| 199 |
+
attention_dropout=0.0,
|
| 200 |
+
_flash_attn_2_enabled=True,
|
| 201 |
+
**kwargs,
|
| 202 |
+
):
|
| 203 |
+
super().__init__(**kwargs)
|
| 204 |
+
|
| 205 |
+
self.hidden_size = hidden_size
|
| 206 |
+
self.intermediate_size = intermediate_size
|
| 207 |
+
self.num_hidden_layers = num_hidden_layers
|
| 208 |
+
self.num_attention_heads = num_attention_heads
|
| 209 |
+
self.num_channels = num_channels
|
| 210 |
+
self.patch_size = patch_size
|
| 211 |
+
self.image_size = image_size
|
| 212 |
+
self.attention_dropout = attention_dropout
|
| 213 |
+
self.layer_norm_eps = layer_norm_eps
|
| 214 |
+
self.hidden_act = hidden_act
|
| 215 |
+
self._flash_attn_2_enabled = _flash_attn_2_enabled
|
| 216 |
+
|
| 217 |
+
@classmethod
|
| 218 |
+
def from_pretrained(cls, pretrained_model_name_or_path: Union[str, os.PathLike], **kwargs) -> "PretrainedConfig":
|
| 219 |
+
cls._set_token_in_kwargs(kwargs)
|
| 220 |
+
|
| 221 |
+
config_dict, kwargs = cls.get_config_dict(pretrained_model_name_or_path, **kwargs)
|
| 222 |
+
|
| 223 |
+
# get the vision config dict if we are loading from SiglipConfig
|
| 224 |
+
if config_dict.get("model_type") == "siglip":
|
| 225 |
+
config_dict = config_dict["vision_config"]
|
| 226 |
+
|
| 227 |
+
if "model_type" in config_dict and hasattr(cls, "model_type") and config_dict["model_type"] != cls.model_type:
|
| 228 |
+
logger.warning(
|
| 229 |
+
f"You are using a model of type {config_dict['model_type']} to instantiate a model of type "
|
| 230 |
+
f"{cls.model_type}. This is not supported for all configurations of models and can yield errors."
|
| 231 |
+
)
|
| 232 |
+
|
| 233 |
+
return cls.from_dict(config_dict, **kwargs)
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
class SiglipConfig(PretrainedConfig):
|
| 237 |
+
r"""
|
| 238 |
+
[`SiglipConfig`] is the configuration class to store the configuration of a [`SiglipModel`]. It is used to
|
| 239 |
+
instantiate a Siglip model according to the specified arguments, defining the text model and vision model configs.
|
| 240 |
+
Instantiating a configuration with the defaults will yield a similar configuration to that of the Siglip
|
| 241 |
+
[google/siglip-base-patch16-224](https://huggingface.co/google/siglip-base-patch16-224) architecture.
|
| 242 |
+
|
| 243 |
+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
|
| 244 |
+
documentation from [`PretrainedConfig`] for more information.
|
| 245 |
+
|
| 246 |
+
Args:
|
| 247 |
+
text_config (`dict`, *optional*):
|
| 248 |
+
Dictionary of configuration options used to initialize [`SiglipTextConfig`].
|
| 249 |
+
vision_config (`dict`, *optional*):
|
| 250 |
+
Dictionary of configuration options used to initialize [`SiglipVisionConfig`].
|
| 251 |
+
kwargs (*optional*):
|
| 252 |
+
Dictionary of keyword arguments.
|
| 253 |
+
|
| 254 |
+
Example:
|
| 255 |
+
|
| 256 |
+
```python
|
| 257 |
+
>>> from transformers import SiglipConfig, SiglipModel
|
| 258 |
+
|
| 259 |
+
>>> # Initializing a SiglipConfig with google/siglip-base-patch16-224 style configuration
|
| 260 |
+
>>> configuration = SiglipConfig()
|
| 261 |
+
|
| 262 |
+
>>> # Initializing a SiglipModel (with random weights) from the google/siglip-base-patch16-224 style configuration
|
| 263 |
+
>>> model = SiglipModel(configuration)
|
| 264 |
+
|
| 265 |
+
>>> # Accessing the model configuration
|
| 266 |
+
>>> configuration = model.config
|
| 267 |
+
|
| 268 |
+
>>> # We can also initialize a SiglipConfig from a SiglipTextConfig and a SiglipVisionConfig
|
| 269 |
+
>>> from transformers import SiglipTextConfig, SiglipVisionConfig
|
| 270 |
+
|
| 271 |
+
>>> # Initializing a SiglipText and SiglipVision configuration
|
| 272 |
+
>>> config_text = SiglipTextConfig()
|
| 273 |
+
>>> config_vision = SiglipVisionConfig()
|
| 274 |
+
|
| 275 |
+
>>> config = SiglipConfig.from_text_vision_configs(config_text, config_vision)
|
| 276 |
+
```"""
|
| 277 |
+
|
| 278 |
+
model_type = "siglip"
|
| 279 |
+
|
| 280 |
+
def __init__(self, text_config=None, vision_config=None, **kwargs):
|
| 281 |
+
super().__init__(**kwargs)
|
| 282 |
+
|
| 283 |
+
if text_config is None:
|
| 284 |
+
text_config = {}
|
| 285 |
+
logger.info("`text_config` is `None`. Initializing the `SiglipTextConfig` with default values.")
|
| 286 |
+
|
| 287 |
+
if vision_config is None:
|
| 288 |
+
vision_config = {}
|
| 289 |
+
logger.info("`vision_config` is `None`. initializing the `SiglipVisionConfig` with default values.")
|
| 290 |
+
|
| 291 |
+
self.text_config = SiglipTextConfig(**text_config)
|
| 292 |
+
self.vision_config = SiglipVisionConfig(**vision_config)
|
| 293 |
+
|
| 294 |
+
self.initializer_factor = 1.0
|
| 295 |
+
|
| 296 |
+
@classmethod
|
| 297 |
+
def from_text_vision_configs(cls, text_config: SiglipTextConfig, vision_config: SiglipVisionConfig, **kwargs):
|
| 298 |
+
r"""
|
| 299 |
+
Instantiate a [`SiglipConfig`] (or a derived class) from siglip text model configuration and siglip vision
|
| 300 |
+
model configuration.
|
| 301 |
+
|
| 302 |
+
Returns:
|
| 303 |
+
[`SiglipConfig`]: An instance of a configuration object
|
| 304 |
+
"""
|
| 305 |
+
|
| 306 |
+
return cls(text_config=text_config.to_dict(), vision_config=vision_config.to_dict(), **kwargs)
|
| 307 |
+
|