Prompt48 commited on
Commit
62d3a2e
·
verified ·
1 Parent(s): f96c81d

Upload edit\Qwen3-TTS-test\.venv\Lib\site-packages\transformers\models\hubert\configuration_hubert.py with huggingface_hub

Browse files
edit//Qwen3-TTS-test//.venv//Lib//site-packages//transformers//models//hubert//configuration_hubert.py ADDED
@@ -0,0 +1,265 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2021 The Fairseq Authors and 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
+ """Hubert model configuration"""
16
+
17
+ import functools
18
+ import operator
19
+
20
+ from ...configuration_utils import PretrainedConfig
21
+ from ...utils import logging
22
+
23
+
24
+ logger = logging.get_logger(__name__)
25
+
26
+
27
+ class HubertConfig(PretrainedConfig):
28
+ r"""
29
+ This is the configuration class to store the configuration of a [`HubertModel`]. It is used to instantiate an
30
+ Hubert model according to the specified arguments, defining the model architecture. Instantiating a configuration
31
+ with the defaults will yield a similar configuration to that of the Hubert
32
+ [facebook/hubert-base-ls960](https://huggingface.co/facebook/hubert-base-ls960) architecture.
33
+
34
+ Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
35
+ documentation from [`PretrainedConfig`] for more information.
36
+
37
+
38
+ Args:
39
+ vocab_size (`int`, *optional*, defaults to 32):
40
+ Vocabulary size of the Hubert model. Defines the number of different tokens that can be represented by the
41
+ `inputs_ids` passed when calling [`HubertModel`]. Vocabulary size of the model. Defines the different
42
+ tokens that can be represented by the *inputs_ids* passed to the forward method of [`HubertModel`].
43
+ hidden_size (`int`, *optional*, defaults to 768):
44
+ Dimensionality of the encoder layers and the pooler layer.
45
+ num_hidden_layers (`int`, *optional*, defaults to 12):
46
+ Number of hidden layers in the Transformer encoder.
47
+ num_attention_heads (`int`, *optional*, defaults to 12):
48
+ Number of attention heads for each attention layer in the Transformer encoder.
49
+ intermediate_size (`int`, *optional*, defaults to 3072):
50
+ Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
51
+ hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`):
52
+ The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
53
+ `"relu"`, `"selu"` and `"gelu_new"` are supported.
54
+ hidden_dropout(`float`, *optional*, defaults to 0.1):
55
+ The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
56
+ activation_dropout (`float`, *optional*, defaults to 0.1):
57
+ The dropout ratio for activations inside the fully connected layer.
58
+ attention_dropout(`float`, *optional*, defaults to 0.1):
59
+ The dropout ratio for the attention probabilities.
60
+ final_dropout (`float`, *optional*, defaults to 0.1):
61
+ The dropout probability for the final projection layer of [`Wav2Vec2ForCTC`].
62
+ layerdrop (`float`, *optional*, defaults to 0.1):
63
+ The LayerDrop probability. See the [LayerDrop paper](see https://huggingface.co/papers/1909.11556) for more
64
+ details.
65
+ initializer_range (`float`, *optional*, defaults to 0.02):
66
+ The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
67
+ layer_norm_eps (`float`, *optional*, defaults to 1e-12):
68
+ The epsilon used by the layer normalization layers.
69
+ feat_extract_norm (`str`, *optional*, defaults to `"group"`):
70
+ The norm to be applied to 1D convolutional layers in feature encoder. One of `"group"` for group
71
+ normalization of only the first 1D convolutional layer or `"layer"` for layer normalization of all 1D
72
+ convolutional layers.
73
+ feat_proj_dropout (`float`, *optional*, defaults to 0.0):
74
+ The dropout probability for output of the feature encoder.
75
+ feat_proj_layer_norm (`bool`, *optional*, defaults to `True`):
76
+ Whether to apply LayerNorm to the output of the feature encoder.
77
+ feat_extract_activation (`str, `optional`, defaults to `"gelu"`):
78
+ The non-linear activation function (function or string) in the 1D convolutional layers of the feature
79
+ extractor. If string, `"gelu"`, `"relu"`, `"selu"` and `"gelu_new"` are supported.
80
+ conv_dim (`tuple[int]`, *optional*, defaults to `(512, 512, 512, 512, 512, 512, 512)`):
81
+ A tuple of integers defining the number of input and output channels of each 1D convolutional layer in the
82
+ feature encoder. The length of *conv_dim* defines the number of 1D convolutional layers.
83
+ conv_stride (`tuple[int]`, *optional*, defaults to `(5, 2, 2, 2, 2, 2, 2)`):
84
+ A tuple of integers defining the stride of each 1D convolutional layer in the feature encoder. The length
85
+ of *conv_stride* defines the number of convolutional layers and has to match the length of *conv_dim*.
86
+ conv_kernel (`tuple[int]`, *optional*, defaults to `(10, 3, 3, 3, 3, 3, 3)`):
87
+ A tuple of integers defining the kernel size of each 1D convolutional layer in the feature encoder. The
88
+ length of *conv_kernel* defines the number of convolutional layers and has to match the length of
89
+ *conv_dim*.
90
+ conv_bias (`bool`, *optional*, defaults to `False`):
91
+ Whether the 1D convolutional layers have a bias.
92
+ num_conv_pos_embeddings (`int`, *optional*, defaults to 128):
93
+ Number of convolutional positional embeddings. Defines the kernel size of 1D convolutional positional
94
+ embeddings layer.
95
+ num_conv_pos_embedding_groups (`int`, *optional*, defaults to 16):
96
+ Number of groups of 1D convolutional positional embeddings layer.
97
+ conv_pos_batch_norm (`bool`, *optional*, defaults to `False`):
98
+ Whether to use batch norm instead of weight norm in conv_pos
99
+ do_stable_layer_norm (`bool`, *optional*, defaults to `False`):
100
+ Whether do apply *stable* layer norm architecture of the Transformer encoder. `do_stable_layer_norm is
101
+ True` corresponds to applying layer norm before the attention layer, whereas `do_stable_layer_norm is
102
+ False` corresponds to applying layer norm after the attention layer.
103
+ apply_spec_augment (`bool`, *optional*, defaults to `True`):
104
+ Whether to apply *SpecAugment* data augmentation to the outputs of the feature encoder. For reference see
105
+ [SpecAugment: A Simple Data Augmentation Method for Automatic Speech
106
+ Recognition](https://huggingface.co/papers/1904.08779).
107
+ mask_time_prob (`float`, *optional*, defaults to 0.05):
108
+ Percentage (between 0 and 1) of all feature vectors along the time axis which will be masked. The masking
109
+ procedure generates ''mask_time_prob*len(time_axis)/mask_time_length'' independent masks over the axis. If
110
+ reasoning from the probability of each feature vector to be chosen as the start of the vector span to be
111
+ masked, *mask_time_prob* should be `prob_vector_start*mask_time_length`. Note that overlap may decrease the
112
+ actual percentage of masked vectors. This is only relevant if `apply_spec_augment is True`.
113
+ mask_time_length (`int`, *optional*, defaults to 10):
114
+ Length of vector span along the time axis.
115
+ mask_time_min_masks (`int`, *optional*, defaults to 2),:
116
+ The minimum number of masks of length `mask_feature_length` generated along the time axis, each time step,
117
+ irrespectively of `mask_feature_prob`. Only relevant if ''mask_time_prob*len(time_axis)/mask_time_length <
118
+ mask_time_min_masks''
119
+ mask_feature_prob (`float`, *optional*, defaults to 0.0):
120
+ Percentage (between 0 and 1) of all feature vectors along the feature axis which will be masked. The
121
+ masking procedure generates ''mask_feature_prob*len(feature_axis)/mask_time_length'' independent masks over
122
+ the axis. If reasoning from the probability of each feature vector to be chosen as the start of the vector
123
+ span to be masked, *mask_feature_prob* should be `prob_vector_start*mask_feature_length`. Note that overlap
124
+ may decrease the actual percentage of masked vectors. This is only relevant if `apply_spec_augment is
125
+ True`.
126
+ mask_feature_length (`int`, *optional*, defaults to 10):
127
+ Length of vector span along the feature axis.
128
+ mask_feature_min_masks (`int`, *optional*, defaults to 0),:
129
+ The minimum number of masks of length `mask_feature_length` generated along the feature axis, each time
130
+ step, irrespectively of `mask_feature_prob`. Only relevant if
131
+ ''mask_feature_prob*len(feature_axis)/mask_feature_length < mask_feature_min_masks''
132
+ ctc_loss_reduction (`str`, *optional*, defaults to `"sum"`):
133
+ Specifies the reduction to apply to the output of `torch.nn.CTCLoss`. Only relevant when training an
134
+ instance of [`HubertForCTC`].
135
+ ctc_zero_infinity (`bool`, *optional*, defaults to `False`):
136
+ Whether to zero infinite losses and the associated gradients of `torch.nn.CTCLoss`. Infinite losses mainly
137
+ occur when the inputs are too short to be aligned to the targets. Only relevant when training an instance
138
+ of [`HubertForCTC`].
139
+ use_weighted_layer_sum (`bool`, *optional*, defaults to `False`):
140
+ Whether to use a weighted average of layer outputs with learned weights. Only relevant when using an
141
+ instance of [`HubertForSequenceClassification`].
142
+ classifier_proj_size (`int`, *optional*, defaults to 256):
143
+ Dimensionality of the projection before token mean-pooling for classification.
144
+
145
+ Example:
146
+
147
+ ```python
148
+ >>> from transformers import HubertModel, HubertConfig
149
+
150
+ >>> # Initializing a Hubert facebook/hubert-base-ls960 style configuration
151
+ >>> configuration = HubertConfig()
152
+
153
+ >>> # Initializing a model from the facebook/hubert-base-ls960 style configuration
154
+ >>> model = HubertModel(configuration)
155
+
156
+ >>> # Accessing the model configuration
157
+ >>> configuration = model.config
158
+ ```"""
159
+
160
+ model_type = "hubert"
161
+
162
+ def __init__(
163
+ self,
164
+ vocab_size=32,
165
+ hidden_size=768,
166
+ num_hidden_layers=12,
167
+ num_attention_heads=12,
168
+ intermediate_size=3072,
169
+ hidden_act="gelu",
170
+ hidden_dropout=0.1,
171
+ activation_dropout=0.1,
172
+ attention_dropout=0.1,
173
+ feat_proj_layer_norm=True,
174
+ feat_proj_dropout=0.0,
175
+ final_dropout=0.1,
176
+ layerdrop=0.1,
177
+ initializer_range=0.02,
178
+ layer_norm_eps=1e-5,
179
+ feat_extract_norm="group",
180
+ feat_extract_activation="gelu",
181
+ conv_dim=(512, 512, 512, 512, 512, 512, 512),
182
+ conv_stride=(5, 2, 2, 2, 2, 2, 2),
183
+ conv_kernel=(10, 3, 3, 3, 3, 2, 2),
184
+ conv_bias=False,
185
+ num_conv_pos_embeddings=128,
186
+ num_conv_pos_embedding_groups=16,
187
+ conv_pos_batch_norm=False,
188
+ do_stable_layer_norm=False,
189
+ apply_spec_augment=True,
190
+ mask_time_prob=0.05,
191
+ mask_time_length=10,
192
+ mask_time_min_masks=2,
193
+ mask_feature_prob=0.0,
194
+ mask_feature_length=10,
195
+ mask_feature_min_masks=0,
196
+ ctc_loss_reduction="sum",
197
+ ctc_zero_infinity=False,
198
+ use_weighted_layer_sum=False,
199
+ classifier_proj_size=256,
200
+ pad_token_id=0,
201
+ bos_token_id=1,
202
+ eos_token_id=2,
203
+ **kwargs,
204
+ ):
205
+ super().__init__(**kwargs, pad_token_id=pad_token_id, bos_token_id=bos_token_id, eos_token_id=eos_token_id)
206
+ self.hidden_size = hidden_size
207
+ self.feat_extract_norm = feat_extract_norm
208
+ self.feat_extract_activation = feat_extract_activation
209
+ self.conv_dim = list(conv_dim)
210
+ self.conv_stride = list(conv_stride)
211
+ self.conv_kernel = list(conv_kernel)
212
+ self.conv_bias = conv_bias
213
+ self.num_conv_pos_embeddings = num_conv_pos_embeddings
214
+ self.num_conv_pos_embedding_groups = num_conv_pos_embedding_groups
215
+ self.conv_pos_batch_norm = conv_pos_batch_norm
216
+ self.num_feat_extract_layers = len(self.conv_dim)
217
+ self.num_hidden_layers = num_hidden_layers
218
+ self.intermediate_size = intermediate_size
219
+ self.hidden_act = hidden_act
220
+ self.num_attention_heads = num_attention_heads
221
+ self.hidden_dropout = hidden_dropout
222
+ self.attention_dropout = attention_dropout
223
+ self.activation_dropout = activation_dropout
224
+ self.feat_proj_layer_norm = feat_proj_layer_norm
225
+ self.feat_proj_dropout = feat_proj_dropout
226
+ self.final_dropout = final_dropout
227
+ self.layerdrop = layerdrop
228
+ self.layer_norm_eps = layer_norm_eps
229
+ self.initializer_range = initializer_range
230
+ self.vocab_size = vocab_size
231
+ self.do_stable_layer_norm = do_stable_layer_norm
232
+ self.use_weighted_layer_sum = use_weighted_layer_sum
233
+ self.classifier_proj_size = classifier_proj_size
234
+
235
+ if (
236
+ (len(self.conv_stride) != self.num_feat_extract_layers)
237
+ or (len(self.conv_kernel) != self.num_feat_extract_layers)
238
+ or (len(self.conv_dim) != self.num_feat_extract_layers)
239
+ ):
240
+ raise ValueError(
241
+ "Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="
242
+ " `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="
243
+ f" {len(self.conv_dim)}`, `len(config.conv_stride) = {len(self.conv_stride)}`,"
244
+ f" `len(config.conv_kernel) = {len(self.conv_kernel)}`."
245
+ )
246
+
247
+ # fine-tuning config parameters for SpecAugment: https://huggingface.co/papers/1904.08779
248
+ self.apply_spec_augment = apply_spec_augment
249
+ self.mask_time_prob = mask_time_prob
250
+ self.mask_time_length = mask_time_length
251
+ self.mask_time_min_masks = mask_time_min_masks
252
+ self.mask_feature_prob = mask_feature_prob
253
+ self.mask_feature_length = mask_feature_length
254
+ self.mask_feature_min_masks = mask_feature_min_masks
255
+
256
+ # ctc loss
257
+ self.ctc_loss_reduction = ctc_loss_reduction
258
+ self.ctc_zero_infinity = ctc_zero_infinity
259
+
260
+ @property
261
+ def inputs_to_logits_ratio(self):
262
+ return functools.reduce(operator.mul, self.conv_stride, 1)
263
+
264
+
265
+ __all__ = ["HubertConfig"]