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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2026 The Qwen team, Alibaba Group 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 |
+
from transformers.configuration_utils import PretrainedConfig, layer_type_validation
|
| 16 |
+
from transformers.modeling_rope_utils import rope_config_validation
|
| 17 |
+
from transformers.utils import logging
|
| 18 |
+
|
| 19 |
+
logger = logging.get_logger(__name__)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class Qwen3TTSSpeakerEncoderConfig(PretrainedConfig):
|
| 23 |
+
r"""
|
| 24 |
+
This is the configuration class to store the configuration of a [`Qwen3TTSSpeakerEncoder`].
|
| 25 |
+
It is used to instantiate a Qwen3TTS speaker encoder model according to the specified arguments, defining the model
|
| 26 |
+
architecture. The architecture is based on the ECAPA-TDNN model.
|
| 27 |
+
|
| 28 |
+
Args:
|
| 29 |
+
mel_dim (`int`, *optional*, defaults to 128):
|
| 30 |
+
The dimension of the input mel-spectrogram.
|
| 31 |
+
enc_dim (`int`, *optional*, defaults to 192):
|
| 32 |
+
The dimension of the final speaker embedding.
|
| 33 |
+
enc_channels (`list[int]`, *optional*, defaults to `[512, 512, 512, 512, 1536]`):
|
| 34 |
+
A list of output channels for each TDNN/SERes2Net layer in the encoder. The first channel size is for the initial TDNN layer,
|
| 35 |
+
the intermediate ones for the `SqueezeExcitationRes2NetBlock` layers, and the last one for the multi-layer feature aggregation.
|
| 36 |
+
enc_kernel_sizes (`list[int]`, *optional*, defaults to `[5, 3, 3, 3, 1]`):
|
| 37 |
+
A list of kernel sizes for each layer in the encoder, corresponding to `enc_channels`.
|
| 38 |
+
enc_dilations (`list[int]`, *optional*, defaults to `[1, 2, 3, 4, 1]`):
|
| 39 |
+
A list of dilations for each layer in the encoder, corresponding to `enc_channels`.
|
| 40 |
+
enc_attention_channels (`int`, *optional*, defaults to 128):
|
| 41 |
+
The number of attention channels in the `AttentiveStatisticsPooling` layer.
|
| 42 |
+
enc_res2net_scale (`int`, *optional*,defaults to 8):
|
| 43 |
+
The scale of the `Res2NetBlock` in the encoder.
|
| 44 |
+
enc_se_channels (`int`, *optional*, defaults to 128):
|
| 45 |
+
The number of channels in the squeeze part of the `SqueezeExcitationBlock`.
|
| 46 |
+
"""
|
| 47 |
+
def __init__(
|
| 48 |
+
self,
|
| 49 |
+
mel_dim=128,
|
| 50 |
+
enc_dim=1024,
|
| 51 |
+
enc_channels=[512, 512, 512, 512, 1536],
|
| 52 |
+
enc_kernel_sizes=[5, 3, 3, 3, 1],
|
| 53 |
+
enc_dilations=[1, 2, 3, 4, 1],
|
| 54 |
+
enc_attention_channels=128,
|
| 55 |
+
enc_res2net_scale=8,
|
| 56 |
+
enc_se_channels=128,
|
| 57 |
+
sample_rate=24000,
|
| 58 |
+
):
|
| 59 |
+
self.mel_dim = mel_dim
|
| 60 |
+
self.enc_dim = enc_dim
|
| 61 |
+
self.enc_channels = enc_channels
|
| 62 |
+
self.enc_kernel_sizes = enc_kernel_sizes
|
| 63 |
+
self.enc_dilations = enc_dilations
|
| 64 |
+
self.enc_attention_channels = enc_attention_channels
|
| 65 |
+
self.enc_res2net_scale = enc_res2net_scale
|
| 66 |
+
self.enc_se_channels = enc_se_channels
|
| 67 |
+
self.sample_rate = sample_rate
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
class Qwen3TTSTalkerCodePredictorConfig(PretrainedConfig):
|
| 71 |
+
r"""
|
| 72 |
+
This is the configuration class to store the configuration of a [`Qwen3TTSTalkerCodePredictorModel`]. It is used to instantiate a
|
| 73 |
+
Qwen3TTSTalkerCodePredictor model according to the specified arguments, defining the model architecture.
|
| 74 |
+
|
| 75 |
+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
|
| 76 |
+
documentation from [`PretrainedConfig`] for more information.
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
Args:
|
| 80 |
+
vocab_size (`int`, *optional*, defaults to 151936):
|
| 81 |
+
Vocabulary size of the Qwen3TTSTalkerCodePredictor model. Defines the number of different tokens that can be represented by the
|
| 82 |
+
`inputs_ids` passed when calling [`Qwen3TTSTalkerCodePredictorModel`]
|
| 83 |
+
hidden_size (`int`, *optional*, defaults to 4096):
|
| 84 |
+
Dimension of the hidden representations.
|
| 85 |
+
intermediate_size (`int`, *optional*, defaults to 22016):
|
| 86 |
+
Dimension of the MLP representations.
|
| 87 |
+
num_hidden_layers (`int`, *optional*, defaults to 32):
|
| 88 |
+
Number of hidden layers in the Transformer encoder.
|
| 89 |
+
num_attention_heads (`int`, *optional*, defaults to 32):
|
| 90 |
+
Number of attention heads for each attention layer in the Transformer encoder.
|
| 91 |
+
num_key_value_heads (`int`, *optional*, defaults to 32):
|
| 92 |
+
This is the number of key_value heads that should be used to implement Grouped Query Attention. If
|
| 93 |
+
`num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
|
| 94 |
+
`num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When
|
| 95 |
+
converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
|
| 96 |
+
by meanpooling all the original heads within that group. For more details, check out [this
|
| 97 |
+
paper](https://huggingface.co/papers/2305.13245). If it is not specified, will default to `32`.
|
| 98 |
+
head_dim (`int`, *optional*, defaults to 128):
|
| 99 |
+
The attention head dimension.
|
| 100 |
+
hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
|
| 101 |
+
The non-linear activation function (function or string) in the decoder.
|
| 102 |
+
max_position_embeddings (`int`, *optional*, defaults to 32768):
|
| 103 |
+
The maximum sequence length that this model might ever be used with.
|
| 104 |
+
initializer_range (`float`, *optional*, defaults to 0.02):
|
| 105 |
+
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
|
| 106 |
+
rms_norm_eps (`float`, *optional*, defaults to 1e-06):
|
| 107 |
+
The epsilon used by the rms normalization layers.
|
| 108 |
+
use_cache (`bool`, *optional*, defaults to `True`):
|
| 109 |
+
Whether or not the model should return the last key/values attentions (not used by all models). Only
|
| 110 |
+
relevant if `config.is_decoder=True`.
|
| 111 |
+
tie_word_embeddings (`bool`, *optional*, defaults to `False`):
|
| 112 |
+
Whether the model's input and output word embeddings should be tied.
|
| 113 |
+
rope_theta (`float`, *optional*, defaults to 10000.0):
|
| 114 |
+
The base period of the RoPE embeddings.
|
| 115 |
+
rope_scaling (`Dict`, *optional*):
|
| 116 |
+
Dictionary containing the scaling configuration for the RoPE embeddings. NOTE: if you apply new rope type
|
| 117 |
+
and you expect the model to work on longer `max_position_embeddings`, we recommend you to update this value
|
| 118 |
+
accordingly.
|
| 119 |
+
Expected contents:
|
| 120 |
+
`rope_type` (`str`):
|
| 121 |
+
The sub-variant of RoPE to use. Can be one of ['default', 'linear', 'dynamic', 'yarn', 'longrope',
|
| 122 |
+
'llama3'], with 'default' being the original RoPE implementation.
|
| 123 |
+
`factor` (`float`, *optional*):
|
| 124 |
+
Used with all rope types except 'default'. The scaling factor to apply to the RoPE embeddings. In
|
| 125 |
+
most scaling types, a `factor` of x will enable the model to handle sequences of length x *
|
| 126 |
+
original maximum pre-trained length.
|
| 127 |
+
`original_max_position_embeddings` (`int`, *optional*):
|
| 128 |
+
Used with 'dynamic', 'longrope' and 'llama3'. The original max position embeddings used during
|
| 129 |
+
pretraining.
|
| 130 |
+
`attention_factor` (`float`, *optional*):
|
| 131 |
+
Used with 'yarn' and 'longrope'. The scaling factor to be applied on the attention
|
| 132 |
+
computation. If unspecified, it defaults to value recommended by the implementation, using the
|
| 133 |
+
`factor` field to infer the suggested value.
|
| 134 |
+
`beta_fast` (`float`, *optional*):
|
| 135 |
+
Only used with 'yarn'. Parameter to set the boundary for extrapolation (only) in the linear
|
| 136 |
+
ramp function. If unspecified, it defaults to 32.
|
| 137 |
+
`beta_slow` (`float`, *optional*):
|
| 138 |
+
Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear
|
| 139 |
+
ramp function. If unspecified, it defaults to 1.
|
| 140 |
+
`short_factor` (`list[float]`, *optional*):
|
| 141 |
+
Only used with 'longrope'. The scaling factor to be applied to short contexts (<
|
| 142 |
+
`original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden
|
| 143 |
+
size divided by the number of attention heads divided by 2
|
| 144 |
+
`long_factor` (`list[float]`, *optional*):
|
| 145 |
+
Only used with 'longrope'. The scaling factor to be applied to long contexts (<
|
| 146 |
+
`original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden
|
| 147 |
+
size divided by the number of attention heads divided by 2
|
| 148 |
+
`low_freq_factor` (`float`, *optional*):
|
| 149 |
+
Only used with 'llama3'. Scaling factor applied to low frequency components of the RoPE
|
| 150 |
+
`high_freq_factor` (`float`, *optional*):
|
| 151 |
+
Only used with 'llama3'. Scaling factor applied to high frequency components of the RoPE
|
| 152 |
+
attention_bias (`bool`, defaults to `False`, *optional*, defaults to `False`):
|
| 153 |
+
Whether to use a bias in the query, key, value and output projection layers during self-attention.
|
| 154 |
+
use_sliding_window (`bool`, *optional*, defaults to `False`):
|
| 155 |
+
Whether to use sliding window attention.
|
| 156 |
+
sliding_window (`int`, *optional*, defaults to 4096):
|
| 157 |
+
Sliding window attention (SWA) window size. If not specified, will default to `4096`.
|
| 158 |
+
max_window_layers (`int`, *optional*, defaults to 28):
|
| 159 |
+
The number of layers using full attention. The first `max_window_layers` layers will use full attention, while any
|
| 160 |
+
additional layer afterwards will use SWA (Sliding Window Attention).
|
| 161 |
+
layer_types (`list`, *optional*):
|
| 162 |
+
Attention pattern for each layer.
|
| 163 |
+
attention_dropout (`float`, *optional*, defaults to 0.0):
|
| 164 |
+
The dropout ratio for the attention probabilities.
|
| 165 |
+
|
| 166 |
+
"""
|
| 167 |
+
|
| 168 |
+
model_type = "qwen3_tts_talker_code_predictor"
|
| 169 |
+
keys_to_ignore_at_inference = ["past_key_values"]
|
| 170 |
+
|
| 171 |
+
# Default tensor parallel plan for base model `Qwen3TTSTalkerCodePredictor`
|
| 172 |
+
base_model_tp_plan = {
|
| 173 |
+
"layers.*.self_attn.q_proj": "colwise",
|
| 174 |
+
"layers.*.self_attn.k_proj": "colwise",
|
| 175 |
+
"layers.*.self_attn.v_proj": "colwise",
|
| 176 |
+
"layers.*.self_attn.o_proj": "rowwise",
|
| 177 |
+
"layers.*.mlp.gate_proj": "colwise",
|
| 178 |
+
"layers.*.mlp.up_proj": "colwise",
|
| 179 |
+
"layers.*.mlp.down_proj": "rowwise",
|
| 180 |
+
}
|
| 181 |
+
base_model_pp_plan = {
|
| 182 |
+
"embed_tokens": (["input_ids"], ["inputs_embeds"]),
|
| 183 |
+
"layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
|
| 184 |
+
"norm": (["hidden_states"], ["hidden_states"]),
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
def __init__(
|
| 188 |
+
self,
|
| 189 |
+
vocab_size=2048,
|
| 190 |
+
hidden_size=1024,
|
| 191 |
+
intermediate_size=3072,
|
| 192 |
+
num_hidden_layers=5,
|
| 193 |
+
num_attention_heads=16,
|
| 194 |
+
num_key_value_heads=8,
|
| 195 |
+
head_dim=128,
|
| 196 |
+
hidden_act="silu",
|
| 197 |
+
max_position_embeddings=32768,
|
| 198 |
+
initializer_range=0.02,
|
| 199 |
+
rms_norm_eps=0.000001,
|
| 200 |
+
use_cache=True,
|
| 201 |
+
tie_word_embeddings=False,
|
| 202 |
+
rope_theta=10000,
|
| 203 |
+
rope_scaling=None,
|
| 204 |
+
attention_bias=False,
|
| 205 |
+
use_sliding_window=False,
|
| 206 |
+
sliding_window=4096,
|
| 207 |
+
max_window_layers=28,
|
| 208 |
+
layer_types=None,
|
| 209 |
+
attention_dropout=0,
|
| 210 |
+
num_code_groups=32,
|
| 211 |
+
**kwargs,
|
| 212 |
+
):
|
| 213 |
+
super().__init__(
|
| 214 |
+
tie_word_embeddings=tie_word_embeddings,
|
| 215 |
+
**kwargs,
|
| 216 |
+
)
|
| 217 |
+
self.vocab_size = vocab_size
|
| 218 |
+
self.max_position_embeddings = max_position_embeddings
|
| 219 |
+
self.hidden_size = hidden_size
|
| 220 |
+
self.intermediate_size = intermediate_size
|
| 221 |
+
self.num_hidden_layers = num_hidden_layers
|
| 222 |
+
self.num_attention_heads = num_attention_heads
|
| 223 |
+
self.use_sliding_window = use_sliding_window
|
| 224 |
+
self.sliding_window = sliding_window if self.use_sliding_window else None
|
| 225 |
+
self.max_window_layers = max_window_layers
|
| 226 |
+
|
| 227 |
+
# for backward compatibility
|
| 228 |
+
if num_key_value_heads is None:
|
| 229 |
+
num_key_value_heads = num_attention_heads
|
| 230 |
+
|
| 231 |
+
self.num_key_value_heads = num_key_value_heads
|
| 232 |
+
self.head_dim = head_dim
|
| 233 |
+
self.hidden_act = hidden_act
|
| 234 |
+
self.initializer_range = initializer_range
|
| 235 |
+
self.rms_norm_eps = rms_norm_eps
|
| 236 |
+
self.use_cache = use_cache
|
| 237 |
+
self.rope_theta = rope_theta
|
| 238 |
+
self.rope_scaling = rope_scaling
|
| 239 |
+
self.attention_bias = attention_bias
|
| 240 |
+
self.attention_dropout = attention_dropout
|
| 241 |
+
# Validate the correctness of rotary position embeddings parameters
|
| 242 |
+
# BC: if there is a 'type' field, move it to 'rope_type'.
|
| 243 |
+
if self.rope_scaling is not None and "type" in self.rope_scaling:
|
| 244 |
+
self.rope_scaling["rope_type"] = self.rope_scaling["type"]
|
| 245 |
+
rope_config_validation(self)
|
| 246 |
+
|
| 247 |
+
self.layer_types = layer_types
|
| 248 |
+
if self.layer_types is None:
|
| 249 |
+
self.layer_types = [
|
| 250 |
+
"sliding_attention"
|
| 251 |
+
if self.sliding_window is not None and i >= self.max_window_layers
|
| 252 |
+
else "full_attention"
|
| 253 |
+
for i in range(self.num_hidden_layers)
|
| 254 |
+
]
|
| 255 |
+
layer_type_validation(self.layer_types)
|
| 256 |
+
self.num_code_groups = num_code_groups
|
| 257 |
+
|
| 258 |
+
|
| 259 |
+
class Qwen3TTSTalkerConfig(PretrainedConfig):
|
| 260 |
+
r"""
|
| 261 |
+
This is the configuration class to store the configuration of a [`Qwen3TTSTalkerModel`]. It is used to instantiate a
|
| 262 |
+
Qwen3TTSTalker model according to the specified arguments, defining the model architecture.
|
| 263 |
+
|
| 264 |
+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
|
| 265 |
+
documentation from [`PretrainedConfig`] for more information.
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
Args:
|
| 269 |
+
vocab_size (`int`, *optional*, defaults to 151936):
|
| 270 |
+
Vocabulary size of the Qwen3TTSTalker model. Defines the number of different tokens that can be represented by the
|
| 271 |
+
`inputs_ids` passed when calling [`Qwen3TTSTalkerModel`]
|
| 272 |
+
hidden_size (`int`, *optional*, defaults to 2048):
|
| 273 |
+
Dimension of the hidden representations.
|
| 274 |
+
intermediate_size (`int`, *optional*, defaults to 6144):
|
| 275 |
+
Dimension of the MLP representations.
|
| 276 |
+
num_hidden_layers (`int`, *optional*, defaults to 24):
|
| 277 |
+
Number of hidden layers in the Transformer encoder.
|
| 278 |
+
num_attention_heads (`int`, *optional*, defaults to 32):
|
| 279 |
+
Number of attention heads for each attention layer in the Transformer encoder.
|
| 280 |
+
num_key_value_heads (`int`, *optional*, defaults to 4):
|
| 281 |
+
This is the number of key_value heads that should be used to implement Grouped Query Attention. If
|
| 282 |
+
`num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
|
| 283 |
+
`num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When
|
| 284 |
+
converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
|
| 285 |
+
by meanpooling all the original heads within that group. For more details, check out [this
|
| 286 |
+
paper](https://huggingface.co/papers/2305.13245). If it is not specified, will default to `32`.
|
| 287 |
+
|
| 288 |
+
hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
|
| 289 |
+
The non-linear activation function (function or string) in the decoder.
|
| 290 |
+
max_position_embeddings (`int`, *optional*, defaults to 32768):
|
| 291 |
+
The maximum sequence length that this model might ever be used with.
|
| 292 |
+
initializer_range (`float`, *optional*, defaults to 0.02):
|
| 293 |
+
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
|
| 294 |
+
rms_norm_eps (`float`, *optional*, defaults to 1e-06):
|
| 295 |
+
The epsilon used by the rms normalization layers.
|
| 296 |
+
use_cache (`bool`, *optional*, defaults to `True`):
|
| 297 |
+
Whether or not the model should return the last key/values attentions (not used by all models). Only
|
| 298 |
+
relevant if `config.is_decoder=True`.
|
| 299 |
+
tie_word_embeddings (`bool`, *optional*, defaults to `False`):
|
| 300 |
+
Whether the model's input and output word embeddings should be tied.
|
| 301 |
+
rope_theta (`float`, *optional*, defaults to 10000.0):
|
| 302 |
+
The base period of the RoPE embeddings.
|
| 303 |
+
rope_scaling (`Dict`, *optional*):
|
| 304 |
+
Dictionary containing the scaling configuration for the RoPE embeddings. NOTE: if you apply new rope type
|
| 305 |
+
and you expect the model to work on longer `max_position_embeddings`, we recommend you to update this value
|
| 306 |
+
accordingly.
|
| 307 |
+
Expected contents:
|
| 308 |
+
`rope_type` (`str`):
|
| 309 |
+
The sub-variant of RoPE to use. Can be one of ['default', 'linear', 'dynamic', 'yarn', 'longrope',
|
| 310 |
+
'llama3'], with 'default' being the original RoPE implementation.
|
| 311 |
+
`factor` (`float`, *optional*):
|
| 312 |
+
Used with all rope types except 'default'. The scaling factor to apply to the RoPE embeddings. In
|
| 313 |
+
most scaling types, a `factor` of x will enable the model to handle sequences of length x *
|
| 314 |
+
original maximum pre-trained length.
|
| 315 |
+
`original_max_position_embeddings` (`int`, *optional*):
|
| 316 |
+
Used with 'dynamic', 'longrope' and 'llama3'. The original max position embeddings used during
|
| 317 |
+
pretraining.
|
| 318 |
+
`attention_factor` (`float`, *optional*):
|
| 319 |
+
Used with 'yarn' and 'longrope'. The scaling factor to be applied on the attention
|
| 320 |
+
computation. If unspecified, it defaults to value recommended by the implementation, using the
|
| 321 |
+
`factor` field to infer the suggested value.
|
| 322 |
+
`beta_fast` (`float`, *optional*):
|
| 323 |
+
Only used with 'yarn'. Parameter to set the boundary for extrapolation (only) in the linear
|
| 324 |
+
ramp function. If unspecified, it defaults to 32.
|
| 325 |
+
`beta_slow` (`float`, *optional*):
|
| 326 |
+
Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear
|
| 327 |
+
ramp function. If unspecified, it defaults to 1.
|
| 328 |
+
`short_factor` (`list[float]`, *optional*):
|
| 329 |
+
Only used with 'longrope'. The scaling factor to be applied to short contexts (<
|
| 330 |
+
`original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden
|
| 331 |
+
size divided by the number of attention heads divided by 2
|
| 332 |
+
`long_factor` (`list[float]`, *optional*):
|
| 333 |
+
Only used with 'longrope'. The scaling factor to be applied to long contexts (<
|
| 334 |
+
`original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden
|
| 335 |
+
size divided by the number of attention heads divided by 2
|
| 336 |
+
`low_freq_factor` (`float`, *optional*):
|
| 337 |
+
Only used with 'llama3'. Scaling factor applied to low frequency components of the RoPE
|
| 338 |
+
`high_freq_factor` (`float`, *optional*):
|
| 339 |
+
Only used with 'llama3'. Scaling factor applied to high frequency components of the RoPE
|
| 340 |
+
attention_bias (`bool`, defaults to `False`, *optional*, defaults to `False`):
|
| 341 |
+
Whether to use a bias in the query, key, value and output projection layers during self-attention.
|
| 342 |
+
use_sliding_window (`bool`, *optional*, defaults to `False`):
|
| 343 |
+
Whether to use sliding window attention.
|
| 344 |
+
sliding_window (`int`, *optional*, defaults to 4096):
|
| 345 |
+
Sliding window attention (SWA) window size. If not specified, will default to `4096`.
|
| 346 |
+
attention_dropout (`float`, *optional*, defaults to 0.0):
|
| 347 |
+
The dropout ratio for the attention probabilities.
|
| 348 |
+
"""
|
| 349 |
+
|
| 350 |
+
model_type = "qwen3_tts_talker"
|
| 351 |
+
keys_to_ignore_at_inference = ["past_key_values"]
|
| 352 |
+
|
| 353 |
+
# Default tensor parallel plan for base model `Qwen3TTSTalker`
|
| 354 |
+
base_model_tp_plan = {
|
| 355 |
+
"layers.*.self_attn.q_proj": "colwise",
|
| 356 |
+
"layers.*.self_attn.k_proj": "colwise",
|
| 357 |
+
"layers.*.self_attn.v_proj": "colwise",
|
| 358 |
+
"layers.*.self_attn.o_proj": "rowwise",
|
| 359 |
+
"layers.*.mlp.gate_proj": "colwise",
|
| 360 |
+
"layers.*.mlp.up_proj": "colwise",
|
| 361 |
+
"layers.*.mlp.down_proj": "rowwise",
|
| 362 |
+
}
|
| 363 |
+
base_model_pp_plan = {
|
| 364 |
+
"embed_tokens": (["input_ids"], ["inputs_embeds"]),
|
| 365 |
+
"layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
|
| 366 |
+
"norm": (["hidden_states"], ["hidden_states"]),
|
| 367 |
+
}
|
| 368 |
+
sub_configs = {"code_predictor_config": Qwen3TTSTalkerCodePredictorConfig}
|
| 369 |
+
|
| 370 |
+
def __init__(
|
| 371 |
+
self,
|
| 372 |
+
code_predictor_config=None,
|
| 373 |
+
vocab_size=3072,
|
| 374 |
+
hidden_size=1024,
|
| 375 |
+
intermediate_size=2048,
|
| 376 |
+
num_hidden_layers=20,
|
| 377 |
+
num_attention_heads=16,
|
| 378 |
+
num_key_value_heads=2,
|
| 379 |
+
hidden_act="silu",
|
| 380 |
+
max_position_embeddings=32768,
|
| 381 |
+
initializer_range=0.02,
|
| 382 |
+
rms_norm_eps=0.000001,
|
| 383 |
+
use_cache=True,
|
| 384 |
+
tie_word_embeddings=False,
|
| 385 |
+
rope_theta=10000,
|
| 386 |
+
rope_scaling=None,
|
| 387 |
+
attention_bias=False,
|
| 388 |
+
use_sliding_window=False,
|
| 389 |
+
sliding_window=4096,
|
| 390 |
+
attention_dropout=0,
|
| 391 |
+
num_code_groups=32,
|
| 392 |
+
text_hidden_size=2048,
|
| 393 |
+
codec_eos_token_id=4198,
|
| 394 |
+
codec_think_id=4202,
|
| 395 |
+
codec_nothink_id=4203,
|
| 396 |
+
codec_think_bos_id=4204,
|
| 397 |
+
codec_think_eos_id=4205,
|
| 398 |
+
codec_pad_id=4196,
|
| 399 |
+
codec_bos_id=4197,
|
| 400 |
+
spk_id=None,
|
| 401 |
+
spk_is_dialect=None,
|
| 402 |
+
codec_language_id=None,
|
| 403 |
+
**kwargs,
|
| 404 |
+
):
|
| 405 |
+
super().__init__(
|
| 406 |
+
tie_word_embeddings=tie_word_embeddings,
|
| 407 |
+
**kwargs,
|
| 408 |
+
)
|
| 409 |
+
self.vocab_size = vocab_size
|
| 410 |
+
self.max_position_embeddings = max_position_embeddings
|
| 411 |
+
self.hidden_size = hidden_size
|
| 412 |
+
self.intermediate_size = intermediate_size
|
| 413 |
+
self.num_hidden_layers = num_hidden_layers
|
| 414 |
+
self.num_attention_heads = num_attention_heads
|
| 415 |
+
self.use_sliding_window = use_sliding_window
|
| 416 |
+
self.sliding_window = sliding_window if use_sliding_window else None
|
| 417 |
+
|
| 418 |
+
self.num_key_value_heads = num_key_value_heads
|
| 419 |
+
self.hidden_act = hidden_act
|
| 420 |
+
self.initializer_range = initializer_range
|
| 421 |
+
self.rms_norm_eps = rms_norm_eps
|
| 422 |
+
self.use_cache = use_cache
|
| 423 |
+
self.rope_theta = rope_theta
|
| 424 |
+
self.rope_scaling = rope_scaling
|
| 425 |
+
self.attention_bias = attention_bias
|
| 426 |
+
self.attention_dropout = attention_dropout
|
| 427 |
+
# Validate the correctness of rotary position embeddings parameters
|
| 428 |
+
# BC: if there is a 'type' field, move it to 'rope_type'.
|
| 429 |
+
if self.rope_scaling is not None and "type" in self.rope_scaling:
|
| 430 |
+
self.rope_scaling["rope_type"] = self.rope_scaling["type"]
|
| 431 |
+
|
| 432 |
+
if code_predictor_config is None:
|
| 433 |
+
code_predictor_config = {}
|
| 434 |
+
self.code_predictor_config = Qwen3TTSTalkerCodePredictorConfig()
|
| 435 |
+
logger.info("code_predictor_config is None. Initializing code_predictor model with default values")
|
| 436 |
+
elif isinstance(code_predictor_config, Qwen3TTSTalkerCodePredictorConfig):
|
| 437 |
+
self.code_predictor_config = code_predictor_config
|
| 438 |
+
else:
|
| 439 |
+
self.code_predictor_config = Qwen3TTSTalkerCodePredictorConfig(**code_predictor_config)
|
| 440 |
+
self.num_code_groups = num_code_groups
|
| 441 |
+
self.text_hidden_size = text_hidden_size
|
| 442 |
+
self.codec_eos_token_id = codec_eos_token_id
|
| 443 |
+
self.codec_think_id = codec_think_id
|
| 444 |
+
self.codec_language_id = codec_language_id
|
| 445 |
+
self.codec_nothink_id = codec_nothink_id
|
| 446 |
+
self.codec_think_bos_id = codec_think_bos_id
|
| 447 |
+
self.codec_think_eos_id = codec_think_eos_id
|
| 448 |
+
self.codec_pad_id = codec_pad_id
|
| 449 |
+
self.codec_bos_id = codec_bos_id
|
| 450 |
+
self.spk_id = spk_id
|
| 451 |
+
self.spk_is_dialect = spk_is_dialect
|
| 452 |
+
|
| 453 |
+
|
| 454 |
+
class Qwen3TTSConfig(PretrainedConfig):
|
| 455 |
+
"""
|
| 456 |
+
This is the configuration class to store the configuration of a [`Qwen3TTSForConditionalGeneration`].
|
| 457 |
+
"""
|
| 458 |
+
|
| 459 |
+
model_type = "qwen3_tts"
|
| 460 |
+
sub_configs = {
|
| 461 |
+
"talker_config": Qwen3TTSTalkerConfig,
|
| 462 |
+
"speaker_encoder_config": Qwen3TTSSpeakerEncoderConfig,
|
| 463 |
+
}
|
| 464 |
+
|
| 465 |
+
def __init__(
|
| 466 |
+
self,
|
| 467 |
+
talker_config=None,
|
| 468 |
+
speaker_encoder_config=None,
|
| 469 |
+
tokenizer_type=None,
|
| 470 |
+
tts_model_size=None,
|
| 471 |
+
tts_model_type=None,
|
| 472 |
+
im_start_token_id=151644,
|
| 473 |
+
im_end_token_id=151645,
|
| 474 |
+
tts_pad_token_id=151671,
|
| 475 |
+
tts_bos_token_id=151672,
|
| 476 |
+
tts_eos_token_id=151673,
|
| 477 |
+
**kwargs,
|
| 478 |
+
):
|
| 479 |
+
super().__init__(**kwargs)
|
| 480 |
+
|
| 481 |
+
if talker_config is None:
|
| 482 |
+
talker_config = {}
|
| 483 |
+
logger.info("talker_config is None. Initializing talker model with default values")
|
| 484 |
+
if speaker_encoder_config is None:
|
| 485 |
+
speaker_encoder_config = {}
|
| 486 |
+
logger.info("speaker_encoder_config is None. Initializing talker model with default values")
|
| 487 |
+
|
| 488 |
+
self.talker_config = Qwen3TTSTalkerConfig(**talker_config)
|
| 489 |
+
self.speaker_encoder_config = Qwen3TTSSpeakerEncoderConfig(**speaker_encoder_config)
|
| 490 |
+
|
| 491 |
+
self.tokenizer_type = tokenizer_type
|
| 492 |
+
self.tts_model_size = tts_model_size
|
| 493 |
+
self.tts_model_type = tts_model_type
|
| 494 |
+
|
| 495 |
+
self.im_start_token_id = im_start_token_id
|
| 496 |
+
self.im_end_token_id = im_end_token_id
|
| 497 |
+
self.tts_pad_token_id = tts_pad_token_id
|
| 498 |
+
self.tts_bos_token_id = tts_bos_token_id
|
| 499 |
+
self.tts_eos_token_id = tts_eos_token_id
|
| 500 |
+
|
| 501 |
+
|
| 502 |
+
__all__ = ["Qwen3TTSConfig", "Qwen3TTSTalkerConfig", "Qwen3TTSSpeakerEncoderConfig"]
|