Upload edit\Qwen3-TTS-test\.venv\Lib\site-packages\transformers\models\granite_speech\configuration_granite_speech.py with huggingface_hub
Browse files
edit//Qwen3-TTS-test//.venv//Lib//site-packages//transformers//models//granite_speech//configuration_granite_speech.py
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2025 The HuggingFace Inc. team.
|
| 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 |
+
"""Config class for Granite Speech."""
|
| 16 |
+
|
| 17 |
+
from ...configuration_utils import PretrainedConfig
|
| 18 |
+
from ..auto import CONFIG_MAPPING, AutoConfig
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class GraniteSpeechEncoderConfig(PretrainedConfig):
|
| 22 |
+
r"""
|
| 23 |
+
This is the configuration class to store the configuration of a [`GraniteSpeechCTCEncoder`]. It is used to instantiate
|
| 24 |
+
a Granite Speech audio encoder according to the specified arguments, defining the model architecture. Instantiating a
|
| 25 |
+
configuration with the dfefaults will yield a similar configuration to that of the audio encoder of the Granite Speech
|
| 26 |
+
architecture.
|
| 27 |
+
|
| 28 |
+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
|
| 29 |
+
documentation from [`PretrainedConfig`] for more information.
|
| 30 |
+
|
| 31 |
+
Args:
|
| 32 |
+
input_dim (`int`, *optional*, defaults to 160):
|
| 33 |
+
Dimension of the first hidden layer of the encoder.
|
| 34 |
+
num_layers (`int`, *optional*, defaults to 10):
|
| 35 |
+
Number of encoder blocks.
|
| 36 |
+
hidden_dim (`int`, *optional*, defaults to 1024):
|
| 37 |
+
The size of the intermediate layers in the conformer encoder.
|
| 38 |
+
feedforward_mult (`int`, *optional*, defaults to 4):
|
| 39 |
+
Multiplier for the up/down projections in the encoder's feedforward layers;
|
| 40 |
+
The projections will have intermediate dim of size `hidden_dim * feedforward_mult`.
|
| 41 |
+
num_heads (`int`, *optional*, defaults to 8):
|
| 42 |
+
Number of attention heads for each attention layer in the Transformer encoder.
|
| 43 |
+
dim_head (`int`, *optional*, defaults to 128):
|
| 44 |
+
Dimension of attention heads for each attention layer in the Transformer encoder.
|
| 45 |
+
output_dim (`int`, *optional*, defaults to 42):
|
| 46 |
+
Intermediate dimension of the feedforward projections in the conformer
|
| 47 |
+
to be added to every other encoder block's output.
|
| 48 |
+
context_size (`int`, *optional*, defaults to 200):
|
| 49 |
+
Context size to be used in conformer attention.
|
| 50 |
+
max_pos_emb (`int`, *optional*, defaults to 512):
|
| 51 |
+
Max pos embeds to be used in attention (shaw's relative positional encoding).
|
| 52 |
+
dropout (`float`, *optional*, defaults to 0.1):
|
| 53 |
+
The dropout probability for fully connected layers in the encoder.
|
| 54 |
+
conv_kernel_size (`int`, *optional*, defaults to 15):
|
| 55 |
+
Kernel size to be used for 1D convolution in each conformer block.
|
| 56 |
+
conv_expansion_factor (`int`, *optional*, defaults to 2):
|
| 57 |
+
Intermediate dimension to be used in conformer convolutions.
|
| 58 |
+
|
| 59 |
+
Example:
|
| 60 |
+
|
| 61 |
+
```python
|
| 62 |
+
>>> from transformers import GraniteSpeechEncoderConfig, GraniteSpeechCTCEncoder
|
| 63 |
+
|
| 64 |
+
>>> # Initializing a GraniteSpeechEncoderConfig
|
| 65 |
+
>>> configuration = GraniteSpeechEncoderConfig()
|
| 66 |
+
|
| 67 |
+
>>> # Initializing a GraniteSpeechCTCEncoder (with random weights)
|
| 68 |
+
>>> model = GraniteSpeechCTCEncoder(configuration)
|
| 69 |
+
|
| 70 |
+
>>> # Accessing the model configuration
|
| 71 |
+
>>> configuration = model.config
|
| 72 |
+
```"""
|
| 73 |
+
|
| 74 |
+
model_type = "granite_speech_encoder"
|
| 75 |
+
|
| 76 |
+
def __init__(
|
| 77 |
+
self,
|
| 78 |
+
input_dim=160,
|
| 79 |
+
num_layers=10,
|
| 80 |
+
hidden_dim=1024,
|
| 81 |
+
feedforward_mult=4,
|
| 82 |
+
num_heads=8,
|
| 83 |
+
dim_head=128,
|
| 84 |
+
output_dim=42,
|
| 85 |
+
context_size=200,
|
| 86 |
+
max_pos_emb=512,
|
| 87 |
+
dropout=0.1,
|
| 88 |
+
conv_kernel_size=15,
|
| 89 |
+
conv_expansion_factor=2,
|
| 90 |
+
**kwargs,
|
| 91 |
+
):
|
| 92 |
+
super().__init__(**kwargs)
|
| 93 |
+
self.input_dim = input_dim
|
| 94 |
+
self.num_layers = num_layers
|
| 95 |
+
self.hidden_dim = hidden_dim
|
| 96 |
+
self.feedforward_mult = feedforward_mult
|
| 97 |
+
self.num_heads = num_heads
|
| 98 |
+
self.dim_head = dim_head
|
| 99 |
+
self.output_dim = output_dim
|
| 100 |
+
self.context_size = context_size
|
| 101 |
+
self.dropout = dropout
|
| 102 |
+
self.conv_kernel_size = conv_kernel_size
|
| 103 |
+
self.conv_expansion_factor = conv_expansion_factor
|
| 104 |
+
self.max_pos_emb = max_pos_emb
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
class GraniteSpeechConfig(PretrainedConfig):
|
| 108 |
+
r"""
|
| 109 |
+
This is the configuration class to store the configuration of a [`GraniteSpeechForConditionalGeneration`]. It is used to instantiate an
|
| 110 |
+
Granite Speech model according to the specified arguments, defining the model architecture.
|
| 111 |
+
|
| 112 |
+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
|
| 113 |
+
documentation from [`PretrainedConfig`] for more information.
|
| 114 |
+
|
| 115 |
+
Args:
|
| 116 |
+
text_config (`Union[AutoConfig, dict]`, *optional*, defaults to `GraniteConfig`):
|
| 117 |
+
The config object or dictionary of the text backbone.
|
| 118 |
+
encoder_config (`GraniteSpeechEncoderConfig`, *optional*):
|
| 119 |
+
The config object or dictionary of the Granite Speech CTC Encoder.
|
| 120 |
+
projector_config (`Union[AutoConfig, dict]`, *optional*, defaults to `Blip2QFormerConfig`):
|
| 121 |
+
The config object or dictionary of the audio projector.
|
| 122 |
+
audio_token_index (`int`, *optional*, defaults to 49155):
|
| 123 |
+
The audio token index to encode the audio prompt.
|
| 124 |
+
initializer_range (`float`, *optional*, defaults to 0.02):
|
| 125 |
+
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
|
| 126 |
+
has_lora_adapter (`bool`, *optional*, defaults to `True`):
|
| 127 |
+
Indicates whether or not the model has a lora adapter that should only
|
| 128 |
+
be activate when processing audio inputs.
|
| 129 |
+
downsample_rate (`int`, *optional*, defaults to 5):
|
| 130 |
+
Downsample rate for the audio feature extractor.
|
| 131 |
+
window_size (`int`, *optional*, defaults to 15):
|
| 132 |
+
Window size for the audio feature projector.
|
| 133 |
+
|
| 134 |
+
Example:
|
| 135 |
+
|
| 136 |
+
```python
|
| 137 |
+
>>> from transformers import GraniteSpeechConfig, GraniteSpeechForConditionalGeneration
|
| 138 |
+
|
| 139 |
+
>>> # Initializing a GraniteSpeechConfig
|
| 140 |
+
>>> configuration = GraniteSpeechConfig()
|
| 141 |
+
|
| 142 |
+
>>> # Initializing a GraniteSpeechForConditionalGeneration (with random weights)
|
| 143 |
+
>>> model = GraniteSpeechForConditionalGeneration(configuration)
|
| 144 |
+
|
| 145 |
+
>>> # Accessing the model configuration
|
| 146 |
+
>>> configuration = model.config
|
| 147 |
+
```"""
|
| 148 |
+
|
| 149 |
+
model_type = "granite_speech"
|
| 150 |
+
attribute_map = {
|
| 151 |
+
"audio_token_id": "audio_token_index",
|
| 152 |
+
}
|
| 153 |
+
sub_configs = {
|
| 154 |
+
"text_config": AutoConfig,
|
| 155 |
+
"encoder_config": GraniteSpeechEncoderConfig,
|
| 156 |
+
"projector_config": AutoConfig,
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
def __init__(
|
| 160 |
+
self,
|
| 161 |
+
text_config=None,
|
| 162 |
+
encoder_config=None,
|
| 163 |
+
projector_config=None,
|
| 164 |
+
audio_token_index=49155,
|
| 165 |
+
initializer_range=0.02,
|
| 166 |
+
has_lora_adapter=True,
|
| 167 |
+
downsample_rate=5,
|
| 168 |
+
window_size=15,
|
| 169 |
+
**kwargs,
|
| 170 |
+
):
|
| 171 |
+
if isinstance(text_config, dict):
|
| 172 |
+
text_config["model_type"] = text_config.get("model_type", "granite")
|
| 173 |
+
text_config = CONFIG_MAPPING[text_config["model_type"]](**text_config)
|
| 174 |
+
elif text_config is None:
|
| 175 |
+
text_config = CONFIG_MAPPING["granite"]()
|
| 176 |
+
|
| 177 |
+
if isinstance(projector_config, dict):
|
| 178 |
+
projector_config["model_type"] = projector_config.get("model_type", "blip_2_qformer")
|
| 179 |
+
projector_config = CONFIG_MAPPING[projector_config["model_type"]](**projector_config)
|
| 180 |
+
elif projector_config is None:
|
| 181 |
+
projector_config = CONFIG_MAPPING["blip_2_qformer"]()
|
| 182 |
+
|
| 183 |
+
if not isinstance(encoder_config, GraniteSpeechEncoderConfig):
|
| 184 |
+
encoder_config = {} if encoder_config is None else encoder_config
|
| 185 |
+
encoder_config = GraniteSpeechEncoderConfig(**encoder_config)
|
| 186 |
+
|
| 187 |
+
self.text_config = text_config
|
| 188 |
+
self.encoder_config = encoder_config
|
| 189 |
+
self.projector_config = projector_config
|
| 190 |
+
self.audio_token_index = audio_token_index
|
| 191 |
+
self.initializer_range = initializer_range
|
| 192 |
+
self.has_lora_adapter = has_lora_adapter
|
| 193 |
+
self.downsample_rate = downsample_rate
|
| 194 |
+
self.window_size = window_size
|
| 195 |
+
super().__init__(**kwargs)
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
__all__ = ["GraniteSpeechEncoderConfig", "GraniteSpeechConfig"]
|