Upload edit\Qwen3-TTS-test\.venv\Lib\site-packages\transformers\models\groupvit\configuration_groupvit.py with huggingface_hub
Browse files
edit//Qwen3-TTS-test//.venv//Lib//site-packages//transformers//models//groupvit//configuration_groupvit.py
ADDED
|
@@ -0,0 +1,407 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2022 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 |
+
"""GroupViT model configuration"""
|
| 16 |
+
|
| 17 |
+
from collections import OrderedDict
|
| 18 |
+
from collections.abc import Mapping
|
| 19 |
+
from typing import TYPE_CHECKING, Any, Optional
|
| 20 |
+
|
| 21 |
+
from ...configuration_utils import PretrainedConfig
|
| 22 |
+
from ...onnx import OnnxConfig
|
| 23 |
+
from ...utils import logging
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
if TYPE_CHECKING:
|
| 27 |
+
from ...processing_utils import ProcessorMixin
|
| 28 |
+
from ...utils import TensorType
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
logger = logging.get_logger(__name__)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class GroupViTTextConfig(PretrainedConfig):
|
| 35 |
+
r"""
|
| 36 |
+
This is the configuration class to store the configuration of a [`GroupViTTextModel`]. It is used to instantiate an
|
| 37 |
+
GroupViT model according to the specified arguments, defining the model architecture. Instantiating a configuration
|
| 38 |
+
with the defaults will yield a similar configuration to that of the GroupViT
|
| 39 |
+
[nvidia/groupvit-gcc-yfcc](https://huggingface.co/nvidia/groupvit-gcc-yfcc) architecture.
|
| 40 |
+
|
| 41 |
+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
|
| 42 |
+
documentation from [`PretrainedConfig`] for more information.
|
| 43 |
+
|
| 44 |
+
Args:
|
| 45 |
+
vocab_size (`int`, *optional*, defaults to 49408):
|
| 46 |
+
Vocabulary size of the GroupViT text model. Defines the number of different tokens that can be represented
|
| 47 |
+
by the `inputs_ids` passed when calling [`GroupViTModel`].
|
| 48 |
+
hidden_size (`int`, *optional*, defaults to 256):
|
| 49 |
+
Dimensionality of the encoder layers and the pooler layer.
|
| 50 |
+
intermediate_size (`int`, *optional*, defaults to 1024):
|
| 51 |
+
Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
|
| 52 |
+
num_hidden_layers (`int`, *optional*, defaults to 12):
|
| 53 |
+
Number of hidden layers in the Transformer encoder.
|
| 54 |
+
num_attention_heads (`int`, *optional*, defaults to 4):
|
| 55 |
+
Number of attention heads for each attention layer in the Transformer encoder.
|
| 56 |
+
max_position_embeddings (`int`, *optional*, defaults to 77):
|
| 57 |
+
The maximum sequence length that this model might ever be used with. Typically set this to something large
|
| 58 |
+
just in case (e.g., 512 or 1024 or 2048).
|
| 59 |
+
hidden_act (`str` or `function`, *optional*, defaults to `"quick_gelu"`):
|
| 60 |
+
The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
|
| 61 |
+
`"relu"`, `"selu"` and `"gelu_new"` `"quick_gelu"` are supported.
|
| 62 |
+
layer_norm_eps (`float`, *optional*, defaults to 1e-5):
|
| 63 |
+
The epsilon used by the layer normalization layers.
|
| 64 |
+
attention_dropout (`float`, *optional*, defaults to 0.0):
|
| 65 |
+
The dropout ratio for the attention probabilities.
|
| 66 |
+
dropout (`float`, *optional*, defaults to 0.0):
|
| 67 |
+
The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
|
| 68 |
+
initializer_range (`float`, *optional*, defaults to 0.02):
|
| 69 |
+
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
|
| 70 |
+
initializer_factor (`float`, *optional*, defaults to 1.0):
|
| 71 |
+
A factor for initializing all weight matrices (should be kept to 1, used internally for initialization
|
| 72 |
+
testing).
|
| 73 |
+
|
| 74 |
+
Example:
|
| 75 |
+
|
| 76 |
+
```python
|
| 77 |
+
>>> from transformers import GroupViTTextConfig, GroupViTTextModel
|
| 78 |
+
|
| 79 |
+
>>> # Initializing a GroupViTTextModel with nvidia/groupvit-gcc-yfcc style configuration
|
| 80 |
+
>>> configuration = GroupViTTextConfig()
|
| 81 |
+
|
| 82 |
+
>>> model = GroupViTTextModel(configuration)
|
| 83 |
+
|
| 84 |
+
>>> # Accessing the model configuration
|
| 85 |
+
>>> configuration = model.config
|
| 86 |
+
```"""
|
| 87 |
+
|
| 88 |
+
model_type = "groupvit_text_model"
|
| 89 |
+
base_config_key = "text_config"
|
| 90 |
+
|
| 91 |
+
def __init__(
|
| 92 |
+
self,
|
| 93 |
+
vocab_size=49408,
|
| 94 |
+
hidden_size=256,
|
| 95 |
+
intermediate_size=1024,
|
| 96 |
+
num_hidden_layers=12,
|
| 97 |
+
num_attention_heads=4,
|
| 98 |
+
max_position_embeddings=77,
|
| 99 |
+
hidden_act="quick_gelu",
|
| 100 |
+
layer_norm_eps=1e-5,
|
| 101 |
+
dropout=0.0,
|
| 102 |
+
attention_dropout=0.0,
|
| 103 |
+
initializer_range=0.02,
|
| 104 |
+
initializer_factor=1.0,
|
| 105 |
+
pad_token_id=1,
|
| 106 |
+
bos_token_id=49406,
|
| 107 |
+
eos_token_id=49407,
|
| 108 |
+
**kwargs,
|
| 109 |
+
):
|
| 110 |
+
super().__init__(pad_token_id=pad_token_id, bos_token_id=bos_token_id, eos_token_id=eos_token_id, **kwargs)
|
| 111 |
+
|
| 112 |
+
self.vocab_size = vocab_size
|
| 113 |
+
self.hidden_size = hidden_size
|
| 114 |
+
self.intermediate_size = intermediate_size
|
| 115 |
+
self.dropout = dropout
|
| 116 |
+
self.num_hidden_layers = num_hidden_layers
|
| 117 |
+
self.num_attention_heads = num_attention_heads
|
| 118 |
+
self.max_position_embeddings = max_position_embeddings
|
| 119 |
+
self.layer_norm_eps = layer_norm_eps
|
| 120 |
+
self.hidden_act = hidden_act
|
| 121 |
+
self.initializer_range = initializer_range
|
| 122 |
+
self.initializer_factor = initializer_factor
|
| 123 |
+
self.attention_dropout = attention_dropout
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
class GroupViTVisionConfig(PretrainedConfig):
|
| 127 |
+
r"""
|
| 128 |
+
This is the configuration class to store the configuration of a [`GroupViTVisionModel`]. It is used to instantiate
|
| 129 |
+
an GroupViT model according to the specified arguments, defining the model architecture. Instantiating a
|
| 130 |
+
configuration with the defaults will yield a similar configuration to that of the GroupViT
|
| 131 |
+
[nvidia/groupvit-gcc-yfcc](https://huggingface.co/nvidia/groupvit-gcc-yfcc) architecture.
|
| 132 |
+
|
| 133 |
+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
|
| 134 |
+
documentation from [`PretrainedConfig`] for more information.
|
| 135 |
+
|
| 136 |
+
Args:
|
| 137 |
+
hidden_size (`int`, *optional*, defaults to 384):
|
| 138 |
+
Dimensionality of the encoder layers and the pooler layer.
|
| 139 |
+
intermediate_size (`int`, *optional*, defaults to 1536):
|
| 140 |
+
Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
|
| 141 |
+
depths (`list[int]`, *optional*, defaults to [6, 3, 3]):
|
| 142 |
+
The number of layers in each encoder block.
|
| 143 |
+
num_group_tokens (`list[int]`, *optional*, defaults to [64, 8, 0]):
|
| 144 |
+
The number of group tokens for each stage.
|
| 145 |
+
num_output_groups (`list[int]`, *optional*, defaults to [64, 8, 8]):
|
| 146 |
+
The number of output groups for each stage, 0 means no group.
|
| 147 |
+
num_attention_heads (`int`, *optional*, defaults to 6):
|
| 148 |
+
Number of attention heads for each attention layer in the Transformer encoder.
|
| 149 |
+
image_size (`int`, *optional*, defaults to 224):
|
| 150 |
+
The size (resolution) of each image.
|
| 151 |
+
patch_size (`int`, *optional*, defaults to 16):
|
| 152 |
+
The size (resolution) of each patch.
|
| 153 |
+
hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`):
|
| 154 |
+
The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
|
| 155 |
+
`"relu"`, `"selu"` and `"gelu_new"` `"quick_gelu"` are supported.
|
| 156 |
+
layer_norm_eps (`float`, *optional*, defaults to 1e-5):
|
| 157 |
+
The epsilon used by the layer normalization layers.
|
| 158 |
+
dropout (`float`, *optional*, defaults to 0.0):
|
| 159 |
+
The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
|
| 160 |
+
attention_dropout (`float`, *optional*, defaults to 0.0):
|
| 161 |
+
The dropout ratio for the attention probabilities.
|
| 162 |
+
initializer_range (`float`, *optional*, defaults to 0.02):
|
| 163 |
+
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
|
| 164 |
+
initializer_factor (`float`, *optional*, defaults to 1.0):
|
| 165 |
+
A factor for initializing all weight matrices (should be kept to 1, used internally for initialization
|
| 166 |
+
testing).
|
| 167 |
+
|
| 168 |
+
Example:
|
| 169 |
+
|
| 170 |
+
```python
|
| 171 |
+
>>> from transformers import GroupViTVisionConfig, GroupViTVisionModel
|
| 172 |
+
|
| 173 |
+
>>> # Initializing a GroupViTVisionModel with nvidia/groupvit-gcc-yfcc style configuration
|
| 174 |
+
>>> configuration = GroupViTVisionConfig()
|
| 175 |
+
|
| 176 |
+
>>> model = GroupViTVisionModel(configuration)
|
| 177 |
+
|
| 178 |
+
>>> # Accessing the model configuration
|
| 179 |
+
>>> configuration = model.config
|
| 180 |
+
```"""
|
| 181 |
+
|
| 182 |
+
model_type = "groupvit_vision_model"
|
| 183 |
+
base_config_key = "vision_config"
|
| 184 |
+
|
| 185 |
+
def __init__(
|
| 186 |
+
self,
|
| 187 |
+
hidden_size=384,
|
| 188 |
+
intermediate_size=1536,
|
| 189 |
+
depths=[6, 3, 3],
|
| 190 |
+
num_hidden_layers=12,
|
| 191 |
+
num_group_tokens=[64, 8, 0],
|
| 192 |
+
num_output_groups=[64, 8, 8],
|
| 193 |
+
num_attention_heads=6,
|
| 194 |
+
image_size=224,
|
| 195 |
+
patch_size=16,
|
| 196 |
+
num_channels=3,
|
| 197 |
+
hidden_act="gelu",
|
| 198 |
+
layer_norm_eps=1e-5,
|
| 199 |
+
dropout=0.0,
|
| 200 |
+
attention_dropout=0.0,
|
| 201 |
+
initializer_range=0.02,
|
| 202 |
+
initializer_factor=1.0,
|
| 203 |
+
assign_eps=1.0,
|
| 204 |
+
assign_mlp_ratio=[0.5, 4],
|
| 205 |
+
**kwargs,
|
| 206 |
+
):
|
| 207 |
+
super().__init__(**kwargs)
|
| 208 |
+
|
| 209 |
+
self.hidden_size = hidden_size
|
| 210 |
+
self.intermediate_size = intermediate_size
|
| 211 |
+
self.depths = depths
|
| 212 |
+
if num_hidden_layers != sum(depths):
|
| 213 |
+
logger.warning(
|
| 214 |
+
f"Manually setting num_hidden_layers to {num_hidden_layers}, but we expect num_hidden_layers ="
|
| 215 |
+
f" sum(depth) = {sum(depths)}"
|
| 216 |
+
)
|
| 217 |
+
self.num_hidden_layers = num_hidden_layers
|
| 218 |
+
self.num_group_tokens = num_group_tokens
|
| 219 |
+
self.num_output_groups = num_output_groups
|
| 220 |
+
self.num_attention_heads = num_attention_heads
|
| 221 |
+
self.image_size = image_size
|
| 222 |
+
self.patch_size = patch_size
|
| 223 |
+
self.num_channels = num_channels
|
| 224 |
+
self.hidden_act = hidden_act
|
| 225 |
+
self.layer_norm_eps = layer_norm_eps
|
| 226 |
+
self.dropout = dropout
|
| 227 |
+
self.attention_dropout = attention_dropout
|
| 228 |
+
self.initializer_range = initializer_range
|
| 229 |
+
self.initializer_factor = initializer_factor
|
| 230 |
+
self.assign_eps = assign_eps
|
| 231 |
+
self.assign_mlp_ratio = assign_mlp_ratio
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
class GroupViTConfig(PretrainedConfig):
|
| 235 |
+
r"""
|
| 236 |
+
[`GroupViTConfig`] is the configuration class to store the configuration of a [`GroupViTModel`]. It is used to
|
| 237 |
+
instantiate a GroupViT model according to the specified arguments, defining the text model and vision model
|
| 238 |
+
configs. Instantiating a configuration with the defaults will yield a similar configuration to that of the GroupViT
|
| 239 |
+
[nvidia/groupvit-gcc-yfcc](https://huggingface.co/nvidia/groupvit-gcc-yfcc) architecture.
|
| 240 |
+
|
| 241 |
+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
|
| 242 |
+
documentation from [`PretrainedConfig`] for more information.
|
| 243 |
+
|
| 244 |
+
Args:
|
| 245 |
+
text_config (`dict`, *optional*):
|
| 246 |
+
Dictionary of configuration options used to initialize [`GroupViTTextConfig`].
|
| 247 |
+
vision_config (`dict`, *optional*):
|
| 248 |
+
Dictionary of configuration options used to initialize [`GroupViTVisionConfig`].
|
| 249 |
+
projection_dim (`int`, *optional*, defaults to 256):
|
| 250 |
+
Dimensionality of text and vision projection layers.
|
| 251 |
+
projection_intermediate_dim (`int`, *optional*, defaults to 4096):
|
| 252 |
+
Dimensionality of intermediate layer of text and vision projection layers.
|
| 253 |
+
logit_scale_init_value (`float`, *optional*, defaults to 2.6592):
|
| 254 |
+
The initial value of the *logit_scale* parameter. Default is used as per the original GroupViT
|
| 255 |
+
implementation.
|
| 256 |
+
kwargs (*optional*):
|
| 257 |
+
Dictionary of keyword arguments.
|
| 258 |
+
"""
|
| 259 |
+
|
| 260 |
+
model_type = "groupvit"
|
| 261 |
+
sub_configs = {"text_config": GroupViTTextConfig, "vision_config": GroupViTVisionConfig}
|
| 262 |
+
|
| 263 |
+
def __init__(
|
| 264 |
+
self,
|
| 265 |
+
text_config=None,
|
| 266 |
+
vision_config=None,
|
| 267 |
+
projection_dim=256,
|
| 268 |
+
projection_intermediate_dim=4096,
|
| 269 |
+
logit_scale_init_value=2.6592,
|
| 270 |
+
**kwargs,
|
| 271 |
+
):
|
| 272 |
+
# If `_config_dict` exist, we use them for the backward compatibility.
|
| 273 |
+
# We pop out these 2 attributes before calling `super().__init__` to avoid them being saved (which causes a lot
|
| 274 |
+
# of confusion!).
|
| 275 |
+
text_config_dict = kwargs.pop("text_config_dict", None)
|
| 276 |
+
vision_config_dict = kwargs.pop("vision_config_dict", None)
|
| 277 |
+
|
| 278 |
+
super().__init__(**kwargs)
|
| 279 |
+
|
| 280 |
+
# Instead of simply assigning `[text|vision]_config_dict` to `[text|vision]_config`, we use the values in
|
| 281 |
+
# `[text|vision]_config_dict` to update the values in `[text|vision]_config`. The values should be same in most
|
| 282 |
+
# cases, but we don't want to break anything regarding `_config_dict` that existed before commit `8827e1b2`.
|
| 283 |
+
if text_config_dict is not None:
|
| 284 |
+
if text_config is None:
|
| 285 |
+
text_config = {}
|
| 286 |
+
|
| 287 |
+
# This is the complete result when using `text_config_dict`.
|
| 288 |
+
_text_config_dict = GroupViTTextConfig(**text_config_dict).to_dict()
|
| 289 |
+
|
| 290 |
+
# Give a warning if the values exist in both `_text_config_dict` and `text_config` but being different.
|
| 291 |
+
for key, value in _text_config_dict.items():
|
| 292 |
+
if key in text_config and value != text_config[key] and key != "transformers_version":
|
| 293 |
+
# If specified in `text_config_dict`
|
| 294 |
+
if key in text_config_dict:
|
| 295 |
+
message = (
|
| 296 |
+
f"`{key}` is found in both `text_config_dict` and `text_config` but with different values. "
|
| 297 |
+
f'The value `text_config_dict["{key}"]` will be used instead.'
|
| 298 |
+
)
|
| 299 |
+
# If inferred from default argument values (just to be super careful)
|
| 300 |
+
else:
|
| 301 |
+
message = (
|
| 302 |
+
f"`text_config_dict` is provided which will be used to initialize `GroupViTTextConfig`. "
|
| 303 |
+
f'The value `text_config["{key}"]` will be overridden.'
|
| 304 |
+
)
|
| 305 |
+
logger.info(message)
|
| 306 |
+
|
| 307 |
+
# Update all values in `text_config` with the ones in `_text_config_dict`.
|
| 308 |
+
text_config.update(_text_config_dict)
|
| 309 |
+
|
| 310 |
+
if vision_config_dict is not None:
|
| 311 |
+
if vision_config is None:
|
| 312 |
+
vision_config = {}
|
| 313 |
+
|
| 314 |
+
# This is the complete result when using `vision_config_dict`.
|
| 315 |
+
_vision_config_dict = GroupViTVisionConfig(**vision_config_dict).to_dict()
|
| 316 |
+
# convert keys to string instead of integer
|
| 317 |
+
if "id2label" in _vision_config_dict:
|
| 318 |
+
_vision_config_dict["id2label"] = {
|
| 319 |
+
str(key): value for key, value in _vision_config_dict["id2label"].items()
|
| 320 |
+
}
|
| 321 |
+
|
| 322 |
+
# Give a warning if the values exist in both `_vision_config_dict` and `vision_config` but being different.
|
| 323 |
+
for key, value in _vision_config_dict.items():
|
| 324 |
+
if key in vision_config and value != vision_config[key] and key != "transformers_version":
|
| 325 |
+
# If specified in `vision_config_dict`
|
| 326 |
+
if key in vision_config_dict:
|
| 327 |
+
message = (
|
| 328 |
+
f"`{key}` is found in both `vision_config_dict` and `vision_config` but with different "
|
| 329 |
+
f'values. The value `vision_config_dict["{key}"]` will be used instead.'
|
| 330 |
+
)
|
| 331 |
+
# If inferred from default argument values (just to be super careful)
|
| 332 |
+
else:
|
| 333 |
+
message = (
|
| 334 |
+
f"`vision_config_dict` is provided which will be used to initialize `GroupViTVisionConfig`."
|
| 335 |
+
f' The value `vision_config["{key}"]` will be overridden.'
|
| 336 |
+
)
|
| 337 |
+
logger.info(message)
|
| 338 |
+
|
| 339 |
+
# Update all values in `vision_config` with the ones in `_vision_config_dict`.
|
| 340 |
+
vision_config.update(_vision_config_dict)
|
| 341 |
+
|
| 342 |
+
if text_config is None:
|
| 343 |
+
text_config = {}
|
| 344 |
+
logger.info("`text_config` is `None`. Initializing the `GroupViTTextConfig` with default values.")
|
| 345 |
+
|
| 346 |
+
if vision_config is None:
|
| 347 |
+
vision_config = {}
|
| 348 |
+
logger.info("`vision_config` is `None`. initializing the `GroupViTVisionConfig` with default values.")
|
| 349 |
+
|
| 350 |
+
self.text_config = GroupViTTextConfig(**text_config)
|
| 351 |
+
self.vision_config = GroupViTVisionConfig(**vision_config)
|
| 352 |
+
|
| 353 |
+
self.projection_dim = projection_dim
|
| 354 |
+
self.projection_intermediate_dim = projection_intermediate_dim
|
| 355 |
+
self.logit_scale_init_value = logit_scale_init_value
|
| 356 |
+
self.initializer_range = 0.02
|
| 357 |
+
self.initializer_factor = 1.0
|
| 358 |
+
self.output_segmentation = False
|
| 359 |
+
|
| 360 |
+
|
| 361 |
+
class GroupViTOnnxConfig(OnnxConfig):
|
| 362 |
+
@property
|
| 363 |
+
def inputs(self) -> Mapping[str, Mapping[int, str]]:
|
| 364 |
+
return OrderedDict(
|
| 365 |
+
[
|
| 366 |
+
("input_ids", {0: "batch", 1: "sequence"}),
|
| 367 |
+
("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
|
| 368 |
+
("attention_mask", {0: "batch", 1: "sequence"}),
|
| 369 |
+
]
|
| 370 |
+
)
|
| 371 |
+
|
| 372 |
+
@property
|
| 373 |
+
def outputs(self) -> Mapping[str, Mapping[int, str]]:
|
| 374 |
+
return OrderedDict(
|
| 375 |
+
[
|
| 376 |
+
("logits_per_image", {0: "batch"}),
|
| 377 |
+
("logits_per_text", {0: "batch"}),
|
| 378 |
+
("text_embeds", {0: "batch"}),
|
| 379 |
+
("image_embeds", {0: "batch"}),
|
| 380 |
+
]
|
| 381 |
+
)
|
| 382 |
+
|
| 383 |
+
@property
|
| 384 |
+
def atol_for_validation(self) -> float:
|
| 385 |
+
return 1e-4
|
| 386 |
+
|
| 387 |
+
def generate_dummy_inputs(
|
| 388 |
+
self,
|
| 389 |
+
processor: "ProcessorMixin",
|
| 390 |
+
batch_size: int = -1,
|
| 391 |
+
seq_length: int = -1,
|
| 392 |
+
framework: Optional["TensorType"] = None,
|
| 393 |
+
) -> Mapping[str, Any]:
|
| 394 |
+
text_input_dict = super().generate_dummy_inputs(
|
| 395 |
+
processor.tokenizer, batch_size=batch_size, seq_length=seq_length, framework=framework
|
| 396 |
+
)
|
| 397 |
+
image_input_dict = super().generate_dummy_inputs(
|
| 398 |
+
processor.image_processor, batch_size=batch_size, framework=framework
|
| 399 |
+
)
|
| 400 |
+
return {**text_input_dict, **image_input_dict}
|
| 401 |
+
|
| 402 |
+
@property
|
| 403 |
+
def default_onnx_opset(self) -> int:
|
| 404 |
+
return 14
|
| 405 |
+
|
| 406 |
+
|
| 407 |
+
__all__ = ["GroupViTConfig", "GroupViTOnnxConfig", "GroupViTTextConfig", "GroupViTVisionConfig"]
|