jordan0811 commited on
Commit
4e13c07
·
verified ·
1 Parent(s): 2685b0c

Upload modeling_fgclip2.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. modeling_fgclip2.py +274 -0
modeling_fgclip2.py ADDED
@@ -0,0 +1,274 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
2
+ # This file was automatically generated from src/transformers/models/fgclip2/modular_fgclip2.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_fgclip2.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 transformers.configuration_utils import PretrainedConfig
22
+ from transformers.utils import logging
23
+
24
+
25
+ logger = logging.get_logger(__name__)
26
+
27
+
28
+ class Fgclip2TextConfig(PretrainedConfig):
29
+ r"""
30
+ This is the configuration class to store the configuration of a [`Fgclip2TextModel`]. It is used to instantiate a
31
+ Fgclip2 text encoder according to the specified arguments, defining the model architecture. Instantiating a
32
+ configuration with the defaults will yield a similar configuration to that of the text encoder of the Fgclip2
33
+ [qihoo360/fg-clip2-base](https://huggingface.co/qihoo360/fg-clip2-base) architecture.
34
+
35
+ Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
36
+ documentation from [`PretrainedConfig`] for more information.
37
+
38
+ Args:
39
+ vocab_size (`int`, *optional*, defaults to 32000):
40
+ Vocabulary size of the Fgclip2 text model. Defines the number of different tokens that can be represented by
41
+ the `inputs_ids` passed when calling [`Fgclip2Model`].
42
+ hidden_size (`int`, *optional*, defaults to 768):
43
+ Dimensionality of the encoder layers and the pooler layer.
44
+ intermediate_size (`int`, *optional*, defaults to 3072):
45
+ Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
46
+ num_hidden_layers (`int`, *optional*, defaults to 12):
47
+ Number of hidden layers in the Transformer encoder.
48
+ num_attention_heads (`int`, *optional*, defaults to 12):
49
+ Number of attention heads for each attention layer in the Transformer encoder.
50
+ max_position_embeddings (`int`, *optional*, defaults to 64):
51
+ The maximum sequence length that this model might ever be used with. Typically set this to something large
52
+ just in case (e.g., 512 or 1024 or 2048).
53
+ hidden_act (`str` or `function`, *optional*, defaults to `"gelu_pytorch_tanh"`):
54
+ The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
55
+ `"relu"`, `"selu"` and `"gelu_new"` `"quick_gelu"` are supported.
56
+ layer_norm_eps (`float`, *optional*, defaults to 1e-06):
57
+ The epsilon used by the layer normalization layers.
58
+ attention_dropout (`float`, *optional*, defaults to 0.0):
59
+ The dropout ratio for the attention probabilities.
60
+ pad_token_id (`int`, *optional*, defaults to 1):
61
+ The id of the padding token in the vocabulary.
62
+ bos_token_id (`int`, *optional*, defaults to 49406):
63
+ The id of the beginning-of-sequence token in the vocabulary.
64
+ eos_token_id (`int`, *optional*, defaults to 49407):
65
+ The id of the end-of-sequence token in the vocabulary.
66
+ projection_size (`int`, *optional*, defaults to `hidden_size`):
67
+ The size of the projection head.
68
+ keep_len (`int`, *optional*, defaults to 20):
69
+ When processing long texts, the retained tokens are used for handling short text lengths.
70
+ For details, please refer to the FG-CLIP 'https://arxiv.org/abs/2505.05071' paper.
71
+ longtext_len (`int`, *optional*, defaults to 196):
72
+ The maximum number of tokens in long texts that can be processed
73
+
74
+
75
+ Example:
76
+
77
+ ```python
78
+ >>> from transformers import Fgclip2TextConfig, Fgclip2TextModel
79
+
80
+ >>> # Initializing a Fgclip2TextConfig with qihoo/fgclip2-base-patch16 style configuration
81
+ >>> configuration = Fgclip2TextConfig()
82
+
83
+ >>> # Initializing a Fgclip2TextModel (with random weights) from the qihoo/fgclip2-base-patch16 style configuration
84
+ >>> model = Fgclip2TextModel(configuration)
85
+
86
+ >>> # Accessing the model configuration
87
+ >>> configuration = model.config
88
+ ```"""
89
+
90
+ model_type = "fgclip2_text_model"
91
+ base_config_key = "text_config"
92
+
93
+ def __init__(
94
+ self,
95
+ vocab_size=32000,
96
+ hidden_size=768,
97
+ intermediate_size=3072,
98
+ num_hidden_layers=12,
99
+ num_attention_heads=12,
100
+ max_position_embeddings=64,
101
+ hidden_act="gelu_pytorch_tanh",
102
+ layer_norm_eps=1e-6,
103
+ attention_dropout=0.0,
104
+ # This differs from `CLIPTokenizer`'s default and from openai/fgclip2
105
+ # See https://github.com/huggingface/transformers/pull/24773#issuecomment-1632287538
106
+ pad_token_id=1,
107
+ bos_token_id=49406,
108
+ eos_token_id=49407,
109
+ projection_size=None,
110
+ keep_len=20,
111
+ longtext_len=196,
112
+ **kwargs,
113
+ ):
114
+ super().__init__(pad_token_id=pad_token_id, bos_token_id=bos_token_id, eos_token_id=eos_token_id, **kwargs)
115
+
116
+ self.vocab_size = vocab_size
117
+ self.hidden_size = hidden_size
118
+ self.intermediate_size = intermediate_size
119
+ self.num_hidden_layers = num_hidden_layers
120
+ self.num_attention_heads = num_attention_heads
121
+ self.max_position_embeddings = max_position_embeddings
122
+ self.layer_norm_eps = layer_norm_eps
123
+ self.hidden_act = hidden_act
124
+ self.attention_dropout = attention_dropout
125
+ self.projection_size = projection_size if projection_size is not None else hidden_size
126
+ self.keep_len = keep_len
127
+ self.longtext_len = longtext_len
128
+
129
+
130
+ class Fgclip2VisionConfig(PretrainedConfig):
131
+ r"""
132
+ This is the configuration class to store the configuration of a [`Fgclip2VisionModel`]. It is used to instantiate a
133
+ Fgclip2 vision encoder according to the specified arguments, defining the model architecture. Instantiating a
134
+ configuration with the defaults will yield a similar configuration to that of the vision encoder of the Fgclip2
135
+ [qihoo/fgclip2-base-patch16](https://huggingface.co/qihoo/fgclip2-base-patch16) architecture.
136
+
137
+ Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
138
+ documentation from [`PretrainedConfig`] for more information.
139
+
140
+ Args:
141
+ hidden_size (`int`, *optional*, defaults to 768):
142
+ Dimensionality of the encoder layers and the pooler layer.
143
+ intermediate_size (`int`, *optional*, defaults to 3072):
144
+ Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
145
+ num_hidden_layers (`int`, *optional*, defaults to 12):
146
+ Number of hidden layers in the Transformer encoder.
147
+ num_attention_heads (`int`, *optional*, defaults to 12):
148
+ Number of attention heads for each attention layer in the Transformer encoder.
149
+ num_channels (`int`, *optional*, defaults to 3):
150
+ Number of channels in the input images.
151
+ num_patches (`int`, *optional*, defaults to 256):
152
+ The number of patches in the image with the size of (`patch_size`, `patch_size`).
153
+ The image is resized to fill maximum of this number of patches, and to preserve
154
+ the aspect ratio. In case the resulted number of patches is lower, the image is
155
+ padded in "patch" dimension.
156
+ patch_size (`int`, *optional*, defaults to 16):
157
+ The size (resolution) of each patch.
158
+ hidden_act (`str` or `function`, *optional*, defaults to `"gelu_pytorch_tanh"`):
159
+ The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
160
+ `"relu"`, `"selu"` and `"gelu_new"` `"quick_gelu"` are supported.
161
+ layer_norm_eps (`float`, *optional*, defaults to 1e-06):
162
+ The epsilon used by the layer normalization layers.
163
+ attention_dropout (`float`, *optional*, defaults to 0.0):
164
+ The dropout ratio for the attention probabilities.
165
+
166
+ Example:
167
+
168
+ ```python
169
+ >>> from transformers import Fgclip2VisionConfig, Fgclip2VisionModel
170
+
171
+ >>> # Initializing a Fgclip2VisionConfig with qihoo/fgclip2-base-patch16 style configuration
172
+ >>> configuration = Fgclip2VisionConfig()
173
+
174
+ >>> # Initializing a Fgclip2VisionModel (with random weights) from the qihoo/fgclip2-base-patch16 style configuration
175
+ >>> model = Fgclip2VisionModel(configuration)
176
+
177
+ >>> # Accessing the model configuration
178
+ >>> configuration = model.config
179
+ ```"""
180
+
181
+ model_type = "fgclip2_vision_model"
182
+ base_config_key = "vision_config"
183
+
184
+ def __init__(
185
+ self,
186
+ hidden_size=768,
187
+ intermediate_size=3072,
188
+ num_hidden_layers=12,
189
+ num_attention_heads=12,
190
+ num_channels=3,
191
+ num_patches=256,
192
+ patch_size=16,
193
+ hidden_act="gelu_pytorch_tanh",
194
+ layer_norm_eps=1e-6,
195
+ attention_dropout=0.0,
196
+ **kwargs,
197
+ ):
198
+ super().__init__(**kwargs)
199
+
200
+ self.hidden_size = hidden_size
201
+ self.intermediate_size = intermediate_size
202
+ self.num_hidden_layers = num_hidden_layers
203
+ self.num_attention_heads = num_attention_heads
204
+ self.num_channels = num_channels
205
+ self.patch_size = patch_size
206
+ self.attention_dropout = attention_dropout
207
+ self.layer_norm_eps = layer_norm_eps
208
+ self.hidden_act = hidden_act
209
+ self.num_patches = num_patches
210
+
211
+
212
+ class Fgclip2Config(PretrainedConfig):
213
+ r"""
214
+ [`Fgclip2Config`] is the configuration class to store the configuration of a [`Fgclip2Model`]. It is used to
215
+ instantiate a Fgclip2 model according to the specified arguments, defining the text model and vision model configs.
216
+ Instantiating a configuration with the defaults will yield a similar configuration to that of the Fgclip2
217
+ [qihoo/fgclip2-base-patch16](https://huggingface.co/qihoo/fgclip2-base-patch16) architecture.
218
+
219
+ Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
220
+ documentation from [`PretrainedConfig`] for more information.
221
+
222
+ Args:
223
+ text_config (`dict`, *optional*):
224
+ Dictionary of configuration options used to initialize [`Fgclip2TextConfig`].
225
+ vision_config (`dict`, *optional*):
226
+ Dictionary of configuration options used to initialize [`Fgclip2VisionConfig`].
227
+ kwargs (*optional*):
228
+ Dictionary of keyword arguments.
229
+
230
+ Example:
231
+
232
+ ```python
233
+ >>> from transformers import Fgclip2Config, Fgclip2Model
234
+
235
+ >>> # Initializing a Fgclip2Config with qihoo/fgclip2-base-patch16 style configuration
236
+ >>> configuration = Fgclip2Config()
237
+
238
+ >>> # Initializing a Fgclip2Model (with random weights) from the qihoo/fgclip2-base-patch16 style configuration
239
+ >>> model = Fgclip2Model(configuration)
240
+
241
+ >>> # Accessing the model configuration
242
+ >>> configuration = model.config
243
+
244
+ >>> # We can also initialize a Fgclip2Config from a Fgclip2TextConfig and a Fgclip2VisionConfig
245
+ >>> from transformers import Fgclip2TextConfig, Fgclip2VisionConfig
246
+
247
+ >>> # Initializing a Fgclip2Text and Fgclip2Vision configuration
248
+ >>> config_text = Fgclip2TextConfig()
249
+ >>> config_vision = Fgclip2VisionConfig()
250
+
251
+ >>> config = Fgclip2Config.from_text_vision_configs(config_text, config_vision)
252
+ ```"""
253
+
254
+ model_type = "fgclip2"
255
+ sub_configs = {"text_config": Fgclip2TextConfig, "vision_config": Fgclip2VisionConfig}
256
+
257
+ def __init__(self, text_config=None, vision_config=None, **kwargs):
258
+ super().__init__(**kwargs)
259
+
260
+ if text_config is None:
261
+ text_config = {}
262
+ logger.info("`text_config` is `None`. Initializing the `Fgclip2TextConfig` with default values.")
263
+
264
+ if vision_config is None:
265
+ vision_config = {}
266
+ logger.info("`vision_config` is `None`. initializing the `Fgclip2VisionConfig` with default values.")
267
+
268
+ self.text_config = Fgclip2TextConfig(**text_config)
269
+ self.vision_config = Fgclip2VisionConfig(**vision_config)
270
+
271
+ self.initializer_factor = 1.0
272
+
273
+
274
+ __all__ = ["Fgclip2Config", "Fgclip2TextConfig", "Fgclip2VisionConfig"]