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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 |
+
"""Qwen3TTSTokenizerV2 model configuration"""
|
| 16 |
+
|
| 17 |
+
from transformers.configuration_utils import PretrainedConfig
|
| 18 |
+
from transformers.utils import logging
|
| 19 |
+
|
| 20 |
+
from transformers import MimiConfig
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
logger = logging.get_logger(__name__)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class Qwen3TTSTokenizerV2DecoderConfig(PretrainedConfig):
|
| 27 |
+
r"""
|
| 28 |
+
This is the configuration class to store the configuration of a [`Qwen3TTSTokenizerV2DecoderConfig`].
|
| 29 |
+
|
| 30 |
+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
|
| 31 |
+
documentation from [`PretrainedConfig`] for more information.
|
| 32 |
+
|
| 33 |
+
Args:
|
| 34 |
+
codebook_size (`int`, *optional*, defaults to 2048):
|
| 35 |
+
Number of entries in each residual codebook used for acoustic token quantization.
|
| 36 |
+
hidden_size (`int`, *optional*, defaults to 1024):
|
| 37 |
+
Dimensionality of the hidden states and embeddings in the autoregressive transformer decoder.
|
| 38 |
+
max_position_embeddings (`int`, *optional*, defaults to 8000):
|
| 39 |
+
Maximum sequence length that the autoregressive decoder can handle. Determines positional embedding size.
|
| 40 |
+
rope_theta (`float`, *optional*, defaults to 10000.0):
|
| 41 |
+
The base period for rotary position embeddings (RoPE) applied to attention layers.
|
| 42 |
+
num_attention_heads (`int`, *optional*, defaults to 16):
|
| 43 |
+
Number of attention heads for each attention layer in the decoder.
|
| 44 |
+
num_key_value_heads (`int`, *optional*, defaults to 16):
|
| 45 |
+
Number of key and value attention heads used in grouped-query attention (if applicable).
|
| 46 |
+
attention_bias (`bool`, *optional*, defaults to `False`):
|
| 47 |
+
Whether to use bias in the attention projection layers.
|
| 48 |
+
sliding_window (`int`, *optional*, defaults to 72):
|
| 49 |
+
Window size for local attention mechanism, limiting attention context to improve efficiency.
|
| 50 |
+
intermediate_size (`int`, *optional*, defaults to 3072):
|
| 51 |
+
Dimensionality of the feed-forward (intermediate) layer in each transformer block.
|
| 52 |
+
hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
|
| 53 |
+
The non-linear activation function used in the feed-forward layers. Supports `"silu"`, `"relu"`, `"gelu"`, etc.
|
| 54 |
+
layer_scale_initial_scale (`float`, *optional*, defaults to 0.01):
|
| 55 |
+
Initial value for LayerScale applied in transformer blocks, helping stabilize training.
|
| 56 |
+
rms_norm_eps (`float`, *optional*, defaults to 1e-5):
|
| 57 |
+
Epsilon value for RMS normalization layers to prevent division by zero.
|
| 58 |
+
num_hidden_layers (`int`, *optional*, defaults to 8):
|
| 59 |
+
Number of transformer blocks in the autoregressive decoder.
|
| 60 |
+
num_quantizers (`int`, *optional*, defaults to 16):
|
| 61 |
+
Number of residual vector quantizers used in the vocoder for fine-grained audio reconstruction.
|
| 62 |
+
upsample_rates (`Tuple[int]`, *optional*, defaults to `(8, 5, 4, 3)`):
|
| 63 |
+
Rate at which features are upsampled in the final waveform synthesis stage.
|
| 64 |
+
upsampling_ratios (`Tuple[int]`, *optional*, defaults to `(2, 2)`):
|
| 65 |
+
Ratios used in transposed convolutional layers to progressively upsample feature maps to waveform.
|
| 66 |
+
decoder_dim (`int`, *optional*, defaults to 1536):
|
| 67 |
+
Final dimensionality of the decoder's output before waveform generation.
|
| 68 |
+
attention_dropout (`float`, *optional*, defaults to 0.0):
|
| 69 |
+
Dropout probability applied to attention weights in the decoder.
|
| 70 |
+
"""
|
| 71 |
+
|
| 72 |
+
def __init__(
|
| 73 |
+
self,
|
| 74 |
+
codebook_size=2048,
|
| 75 |
+
hidden_size=1024,
|
| 76 |
+
latent_dim=1024,
|
| 77 |
+
max_position_embeddings=8000,
|
| 78 |
+
rope_theta=10000,
|
| 79 |
+
num_attention_heads=16,
|
| 80 |
+
num_key_value_heads=16,
|
| 81 |
+
attention_bias=False,
|
| 82 |
+
sliding_window=72,
|
| 83 |
+
intermediate_size=3072,
|
| 84 |
+
hidden_act="silu",
|
| 85 |
+
layer_scale_initial_scale=0.01,
|
| 86 |
+
rms_norm_eps=1e-5,
|
| 87 |
+
num_hidden_layers=8,
|
| 88 |
+
num_quantizers=16,
|
| 89 |
+
upsample_rates=(8, 5, 4, 3),
|
| 90 |
+
upsampling_ratios=(2, 2),
|
| 91 |
+
decoder_dim=1536,
|
| 92 |
+
attention_dropout=0.0,
|
| 93 |
+
**kwargs,
|
| 94 |
+
):
|
| 95 |
+
super().__init__(**kwargs)
|
| 96 |
+
self.codebook_size = codebook_size
|
| 97 |
+
self.hidden_size = hidden_size
|
| 98 |
+
self.latent_dim = latent_dim
|
| 99 |
+
self.max_position_embeddings = max_position_embeddings
|
| 100 |
+
self.rope_theta = rope_theta
|
| 101 |
+
self.num_attention_heads = num_attention_heads
|
| 102 |
+
self.num_key_value_heads = num_key_value_heads
|
| 103 |
+
self.attention_bias = attention_bias
|
| 104 |
+
self.sliding_window = sliding_window
|
| 105 |
+
self.intermediate_size = intermediate_size
|
| 106 |
+
self.hidden_act = hidden_act
|
| 107 |
+
self.layer_scale_initial_scale = layer_scale_initial_scale
|
| 108 |
+
self.rms_norm_eps = rms_norm_eps
|
| 109 |
+
self.num_hidden_layers = num_hidden_layers
|
| 110 |
+
self.num_quantizers = num_quantizers
|
| 111 |
+
self.upsample_rates = upsample_rates
|
| 112 |
+
self.upsampling_ratios = upsampling_ratios
|
| 113 |
+
self.decoder_dim = decoder_dim
|
| 114 |
+
self.attention_dropout = attention_dropout
|
| 115 |
+
|
| 116 |
+
@property
|
| 117 |
+
def layer_types(self):
|
| 118 |
+
"""
|
| 119 |
+
All layer in code2wav should be sliding attention
|
| 120 |
+
"""
|
| 121 |
+
return ["sliding_attention"] * self.num_hidden_layers
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
class Qwen3TTSTokenizerV2Config(PretrainedConfig):
|
| 125 |
+
"""
|
| 126 |
+
This is the configuration class to store the configuration of a [`Qwen3TTSTokenizerV2Config`]. It is used to instantiate a Qwen3TTSTokenizerV2Model
|
| 127 |
+
model according to the specified sub-models configurations, defining the model architecture.
|
| 128 |
+
|
| 129 |
+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
|
| 130 |
+
documentation from [`PretrainedConfig`] for more information.
|
| 131 |
+
|
| 132 |
+
Args:
|
| 133 |
+
encoder_config (`dict`, *optional*): Configuration of the underlying encoder sub-model.
|
| 134 |
+
decoder_config (`dict`, *optional*): Configuration of the underlying decoder sub-model.
|
| 135 |
+
"""
|
| 136 |
+
|
| 137 |
+
model_type = "qwen3_tts_tokenizer_12hz"
|
| 138 |
+
sub_configs = {
|
| 139 |
+
"encoder_config": MimiConfig,
|
| 140 |
+
"decoder_config": Qwen3TTSTokenizerV2DecoderConfig,
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
def __init__(
|
| 144 |
+
self,
|
| 145 |
+
encoder_config=None,
|
| 146 |
+
decoder_config=None,
|
| 147 |
+
encoder_valid_num_quantizers=16,
|
| 148 |
+
input_sample_rate=24000,
|
| 149 |
+
output_sample_rate=24000,
|
| 150 |
+
decode_upsample_rate=1920,
|
| 151 |
+
encode_downsample_rate=1920,
|
| 152 |
+
**kwargs,
|
| 153 |
+
):
|
| 154 |
+
super().__init__(**kwargs)
|
| 155 |
+
if encoder_config is None:
|
| 156 |
+
encoder_config = {}
|
| 157 |
+
logger.info("encoder_config is None. Initializing encoder with default values")
|
| 158 |
+
if decoder_config is None:
|
| 159 |
+
decoder_config = {}
|
| 160 |
+
logger.info("decoder_config is None. Initializing decoder with default values")
|
| 161 |
+
|
| 162 |
+
self.encoder_config = MimiConfig(**encoder_config)
|
| 163 |
+
self.decoder_config = Qwen3TTSTokenizerV2DecoderConfig(**decoder_config)
|
| 164 |
+
|
| 165 |
+
self.encoder_valid_num_quantizers = encoder_valid_num_quantizers
|
| 166 |
+
self.input_sample_rate = input_sample_rate
|
| 167 |
+
self.output_sample_rate = output_sample_rate
|
| 168 |
+
self.decode_upsample_rate = decode_upsample_rate
|
| 169 |
+
self.encode_downsample_rate = encode_downsample_rate
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
__all__ = ["Qwen3TTSTokenizerV2Config", "Qwen3TTSTokenizerV2DecoderConfig"]
|