|
| 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 | + sub_configs = { |
| 151 | + "text_config": AutoConfig, |
| 152 | + "encoder_config": GraniteSpeechEncoderConfig, |
| 153 | + "projector_config": AutoConfig, |
| 154 | + } |
| 155 | + |
| 156 | + def __init__( |
| 157 | + self, |
| 158 | + text_config=None, |
| 159 | + encoder_config=None, |
| 160 | + projector_config=None, |
| 161 | + audio_token_index=49155, |
| 162 | + initializer_range=0.02, |
| 163 | + has_lora_adapter=True, |
| 164 | + downsample_rate=5, |
| 165 | + window_size=15, |
| 166 | + **kwargs, |
| 167 | + ): |
| 168 | + if isinstance(text_config, dict): |
| 169 | + text_config["model_type"] = text_config["model_type"] if "model_type" in text_config else "granite" |
| 170 | + text_config = CONFIG_MAPPING[text_config["model_type"]](**text_config) |
| 171 | + elif text_config is None: |
| 172 | + text_config = CONFIG_MAPPING["granite"]() |
| 173 | + |
| 174 | + if isinstance(projector_config, dict): |
| 175 | + projector_config["model_type"] = ( |
| 176 | + projector_config["model_type"] if "model_type" in projector_config else "blip_2_qformer" |
| 177 | + ) |
| 178 | + projector_config = CONFIG_MAPPING[projector_config["model_type"]](**projector_config) |
| 179 | + elif projector_config is None: |
| 180 | + projector_config = CONFIG_MAPPING["blip_2_qformer"]() |
| 181 | + |
| 182 | + if not isinstance(encoder_config, GraniteSpeechEncoderConfig): |
| 183 | + encoder_config = {} if encoder_config is None else encoder_config |
| 184 | + encoder_config = GraniteSpeechEncoderConfig(**encoder_config) |
| 185 | + |
| 186 | + self.text_config = text_config |
| 187 | + self.encoder_config = encoder_config |
| 188 | + self.projector_config = projector_config |
| 189 | + self.audio_token_index = audio_token_index |
| 190 | + self.initializer_range = initializer_range |
| 191 | + self.has_lora_adapter = has_lora_adapter |
| 192 | + self.downsample_rate = downsample_rate |
| 193 | + self.window_size = window_size |
| 194 | + super().__init__(**kwargs) |
| 195 | + |
| 196 | + |
| 197 | +__all__ = ["GraniteSpeechEncoderConfig", "GraniteSpeechConfig"] |
0 commit comments