configuration_vits.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. # coding=utf-8
  2. # Copyright 2023 The Kakao Enterprise 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. """VITS model configuration"""
  16. from ...configuration_utils import PretrainedConfig
  17. from ...utils import logging
  18. logger = logging.get_logger(__name__)
  19. class VitsConfig(PretrainedConfig):
  20. r"""
  21. This is the configuration class to store the configuration of a [`VitsModel`]. It is used to instantiate a VITS
  22. model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
  23. defaults will yield a similar configuration to that of the VITS
  24. [facebook/mms-tts-eng](https://huggingface.co/facebook/mms-tts-eng) architecture.
  25. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  26. documentation from [`PretrainedConfig`] for more information.
  27. Args:
  28. vocab_size (`int`, *optional*, defaults to 38):
  29. Vocabulary size of the VITS model. Defines the number of different tokens that can be represented by the
  30. `inputs_ids` passed to the forward method of [`VitsModel`].
  31. hidden_size (`int`, *optional*, defaults to 192):
  32. Dimensionality of the text encoder layers.
  33. num_hidden_layers (`int`, *optional*, defaults to 6):
  34. Number of hidden layers in the Transformer encoder.
  35. num_attention_heads (`int`, *optional*, defaults to 2):
  36. Number of attention heads for each attention layer in the Transformer encoder.
  37. window_size (`int`, *optional*, defaults to 4):
  38. Window size for the relative positional embeddings in the attention layers of the Transformer encoder.
  39. use_bias (`bool`, *optional*, defaults to `True`):
  40. Whether to use bias in the key, query, value projection layers in the Transformer encoder.
  41. ffn_dim (`int`, *optional*, defaults to 768):
  42. Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
  43. layerdrop (`float`, *optional*, defaults to 0.1):
  44. The LayerDrop probability for the encoder. See the [LayerDrop paper](see https://huggingface.co/papers/1909.11556)
  45. for more details.
  46. ffn_kernel_size (`int`, *optional*, defaults to 3):
  47. Kernel size of the 1D convolution layers used by the feed-forward network in the Transformer encoder.
  48. flow_size (`int`, *optional*, defaults to 192):
  49. Dimensionality of the flow layers.
  50. spectrogram_bins (`int`, *optional*, defaults to 513):
  51. Number of frequency bins in the target spectrogram.
  52. hidden_act (`str` or `function`, *optional*, defaults to `"relu"`):
  53. The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
  54. `"relu"`, `"selu"` and `"gelu_new"` are supported.
  55. hidden_dropout (`float`, *optional*, defaults to 0.1):
  56. The dropout probability for all fully connected layers in the embeddings and encoder.
  57. attention_dropout (`float`, *optional*, defaults to 0.1):
  58. The dropout ratio for the attention probabilities.
  59. activation_dropout (`float`, *optional*, defaults to 0.1):
  60. The dropout ratio for activations inside the fully connected layer.
  61. initializer_range (`float`, *optional*, defaults to 0.02):
  62. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  63. layer_norm_eps (`float`, *optional*, defaults to 1e-05):
  64. The epsilon used by the layer normalization layers.
  65. use_stochastic_duration_prediction (`bool`, *optional*, defaults to `True`):
  66. Whether to use the stochastic duration prediction module or the regular duration predictor.
  67. num_speakers (`int`, *optional*, defaults to 1):
  68. Number of speakers if this is a multi-speaker model.
  69. speaker_embedding_size (`int`, *optional*, defaults to 0):
  70. Number of channels used by the speaker embeddings. Is zero for single-speaker models.
  71. upsample_initial_channel (`int`, *optional*, defaults to 512):
  72. The number of input channels into the HiFi-GAN upsampling network.
  73. upsample_rates (`tuple[int]` or `list[int]`, *optional*, defaults to `[8, 8, 2, 2]`):
  74. A tuple of integers defining the stride of each 1D convolutional layer in the HiFi-GAN upsampling network.
  75. The length of `upsample_rates` defines the number of convolutional layers and has to match the length of
  76. `upsample_kernel_sizes`.
  77. upsample_kernel_sizes (`tuple[int]` or `list[int]`, *optional*, defaults to `[16, 16, 4, 4]`):
  78. A tuple of integers defining the kernel size of each 1D convolutional layer in the HiFi-GAN upsampling
  79. network. The length of `upsample_kernel_sizes` defines the number of convolutional layers and has to match
  80. the length of `upsample_rates`.
  81. resblock_kernel_sizes (`tuple[int]` or `list[int]`, *optional*, defaults to `[3, 7, 11]`):
  82. A tuple of integers defining the kernel sizes of the 1D convolutional layers in the HiFi-GAN
  83. multi-receptive field fusion (MRF) module.
  84. resblock_dilation_sizes (`tuple[tuple[int]]` or `list[list[int]]`, *optional*, defaults to `[[1, 3, 5], [1, 3, 5], [1, 3, 5]]`):
  85. A nested tuple of integers defining the dilation rates of the dilated 1D convolutional layers in the
  86. HiFi-GAN multi-receptive field fusion (MRF) module.
  87. leaky_relu_slope (`float`, *optional*, defaults to 0.1):
  88. The angle of the negative slope used by the leaky ReLU activation.
  89. depth_separable_channels (`int`, *optional*, defaults to 2):
  90. Number of channels to use in each depth-separable block.
  91. depth_separable_num_layers (`int`, *optional*, defaults to 3):
  92. Number of convolutional layers to use in each depth-separable block.
  93. duration_predictor_flow_bins (`int`, *optional*, defaults to 10):
  94. Number of channels to map using the unonstrained rational spline in the duration predictor model.
  95. duration_predictor_tail_bound (`float`, *optional*, defaults to 5.0):
  96. Value of the tail bin boundary when computing the unconstrained rational spline in the duration predictor
  97. model.
  98. duration_predictor_kernel_size (`int`, *optional*, defaults to 3):
  99. Kernel size of the 1D convolution layers used in the duration predictor model.
  100. duration_predictor_dropout (`float`, *optional*, defaults to 0.5):
  101. The dropout ratio for the duration predictor model.
  102. duration_predictor_num_flows (`int`, *optional*, defaults to 4):
  103. Number of flow stages used by the duration predictor model.
  104. duration_predictor_filter_channels (`int`, *optional*, defaults to 256):
  105. Number of channels for the convolution layers used in the duration predictor model.
  106. prior_encoder_num_flows (`int`, *optional*, defaults to 4):
  107. Number of flow stages used by the prior encoder flow model.
  108. prior_encoder_num_wavenet_layers (`int`, *optional*, defaults to 4):
  109. Number of WaveNet layers used by the prior encoder flow model.
  110. posterior_encoder_num_wavenet_layers (`int`, *optional*, defaults to 16):
  111. Number of WaveNet layers used by the posterior encoder model.
  112. wavenet_kernel_size (`int`, *optional*, defaults to 5):
  113. Kernel size of the 1D convolution layers used in the WaveNet model.
  114. wavenet_dilation_rate (`int`, *optional*, defaults to 1):
  115. Dilation rates of the dilated 1D convolutional layers used in the WaveNet model.
  116. wavenet_dropout (`float`, *optional*, defaults to 0.0):
  117. The dropout ratio for the WaveNet layers.
  118. speaking_rate (`float`, *optional*, defaults to 1.0):
  119. Speaking rate. Larger values give faster synthesised speech.
  120. noise_scale (`float`, *optional*, defaults to 0.667):
  121. How random the speech prediction is. Larger values create more variation in the predicted speech.
  122. noise_scale_duration (`float`, *optional*, defaults to 0.8):
  123. How random the duration prediction is. Larger values create more variation in the predicted durations.
  124. sampling_rate (`int`, *optional*, defaults to 16000):
  125. The sampling rate at which the output audio waveform is digitalized expressed in hertz (Hz).
  126. Example:
  127. ```python
  128. >>> from transformers import VitsModel, VitsConfig
  129. >>> # Initializing a "facebook/mms-tts-eng" style configuration
  130. >>> configuration = VitsConfig()
  131. >>> # Initializing a model (with random weights) from the "facebook/mms-tts-eng" style configuration
  132. >>> model = VitsModel(configuration)
  133. >>> # Accessing the model configuration
  134. >>> configuration = model.config
  135. ```"""
  136. model_type = "vits"
  137. def __init__(
  138. self,
  139. vocab_size=38,
  140. hidden_size=192,
  141. num_hidden_layers=6,
  142. num_attention_heads=2,
  143. window_size=4,
  144. use_bias=True,
  145. ffn_dim=768,
  146. layerdrop=0.1,
  147. ffn_kernel_size=3,
  148. flow_size=192,
  149. spectrogram_bins=513,
  150. hidden_act="relu",
  151. hidden_dropout=0.1,
  152. attention_dropout=0.1,
  153. activation_dropout=0.1,
  154. initializer_range=0.02,
  155. layer_norm_eps=1e-5,
  156. use_stochastic_duration_prediction=True,
  157. num_speakers=1,
  158. speaker_embedding_size=0,
  159. upsample_initial_channel=512,
  160. upsample_rates=[8, 8, 2, 2],
  161. upsample_kernel_sizes=[16, 16, 4, 4],
  162. resblock_kernel_sizes=[3, 7, 11],
  163. resblock_dilation_sizes=[[1, 3, 5], [1, 3, 5], [1, 3, 5]],
  164. leaky_relu_slope=0.1,
  165. depth_separable_channels=2,
  166. depth_separable_num_layers=3,
  167. duration_predictor_flow_bins=10,
  168. duration_predictor_tail_bound=5.0,
  169. duration_predictor_kernel_size=3,
  170. duration_predictor_dropout=0.5,
  171. duration_predictor_num_flows=4,
  172. duration_predictor_filter_channels=256,
  173. prior_encoder_num_flows=4,
  174. prior_encoder_num_wavenet_layers=4,
  175. posterior_encoder_num_wavenet_layers=16,
  176. wavenet_kernel_size=5,
  177. wavenet_dilation_rate=1,
  178. wavenet_dropout=0.0,
  179. speaking_rate=1.0,
  180. noise_scale=0.667,
  181. noise_scale_duration=0.8,
  182. sampling_rate=16_000,
  183. **kwargs,
  184. ):
  185. self.vocab_size = vocab_size
  186. self.hidden_size = hidden_size
  187. self.num_hidden_layers = num_hidden_layers
  188. self.num_attention_heads = num_attention_heads
  189. self.window_size = window_size
  190. self.use_bias = use_bias
  191. self.ffn_dim = ffn_dim
  192. self.layerdrop = layerdrop
  193. self.ffn_kernel_size = ffn_kernel_size
  194. self.flow_size = flow_size
  195. self.spectrogram_bins = spectrogram_bins
  196. self.hidden_act = hidden_act
  197. self.hidden_dropout = hidden_dropout
  198. self.attention_dropout = attention_dropout
  199. self.activation_dropout = activation_dropout
  200. self.initializer_range = initializer_range
  201. self.layer_norm_eps = layer_norm_eps
  202. self.use_stochastic_duration_prediction = use_stochastic_duration_prediction
  203. self.num_speakers = num_speakers
  204. self.speaker_embedding_size = speaker_embedding_size
  205. self.upsample_initial_channel = upsample_initial_channel
  206. self.upsample_rates = upsample_rates
  207. self.upsample_kernel_sizes = upsample_kernel_sizes
  208. self.resblock_kernel_sizes = resblock_kernel_sizes
  209. self.resblock_dilation_sizes = resblock_dilation_sizes
  210. self.leaky_relu_slope = leaky_relu_slope
  211. self.depth_separable_channels = depth_separable_channels
  212. self.depth_separable_num_layers = depth_separable_num_layers
  213. self.duration_predictor_flow_bins = duration_predictor_flow_bins
  214. self.duration_predictor_tail_bound = duration_predictor_tail_bound
  215. self.duration_predictor_kernel_size = duration_predictor_kernel_size
  216. self.duration_predictor_dropout = duration_predictor_dropout
  217. self.duration_predictor_num_flows = duration_predictor_num_flows
  218. self.duration_predictor_filter_channels = duration_predictor_filter_channels
  219. self.prior_encoder_num_flows = prior_encoder_num_flows
  220. self.prior_encoder_num_wavenet_layers = prior_encoder_num_wavenet_layers
  221. self.posterior_encoder_num_wavenet_layers = posterior_encoder_num_wavenet_layers
  222. self.wavenet_kernel_size = wavenet_kernel_size
  223. self.wavenet_dilation_rate = wavenet_dilation_rate
  224. self.wavenet_dropout = wavenet_dropout
  225. self.speaking_rate = speaking_rate
  226. self.noise_scale = noise_scale
  227. self.noise_scale_duration = noise_scale_duration
  228. self.sampling_rate = sampling_rate
  229. if len(upsample_kernel_sizes) != len(upsample_rates):
  230. raise ValueError(
  231. f"The length of `upsample_kernel_sizes` ({len(upsample_kernel_sizes)}) must match the length of "
  232. f"`upsample_rates` ({len(upsample_rates)})"
  233. )
  234. super().__init__(**kwargs)
  235. __all__ = ["VitsConfig"]