configuration_vits.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # Copyright 2023 The Kakao Enterprise Authors and the HuggingFace Inc. team. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """VITS model configuration"""
  15. from huggingface_hub.dataclasses import strict
  16. from ...configuration_utils import PreTrainedConfig
  17. from ...utils import auto_docstring
  18. @auto_docstring(checkpoint="facebook/mms-tts-eng")
  19. @strict
  20. class VitsConfig(PreTrainedConfig):
  21. r"""
  22. window_size (`int`, *optional*, defaults to 4):
  23. Window size for the relative positional embeddings in the attention layers of the Transformer encoder.
  24. use_bias (`bool`, *optional*, defaults to `True`):
  25. Whether to use bias in the key, query, value projection layers in the Transformer encoder.
  26. ffn_kernel_size (`int`, *optional*, defaults to 3):
  27. Kernel size of the 1D convolution layers used by the feed-forward network in the Transformer encoder.
  28. flow_size (`int`, *optional*, defaults to 192):
  29. Dimensionality of the flow layers.
  30. spectrogram_bins (`int`, *optional*, defaults to 513):
  31. Number of frequency bins in the target spectrogram.
  32. use_stochastic_duration_prediction (`bool`, *optional*, defaults to `True`):
  33. Whether to use the stochastic duration prediction module or the regular duration predictor.
  34. num_speakers (`int`, *optional*, defaults to 1):
  35. Number of speakers if this is a multi-speaker model.
  36. speaker_embedding_size (`int`, *optional*, defaults to 0):
  37. Number of channels used by the speaker embeddings. Is zero for single-speaker models.
  38. upsample_initial_channel (`int`, *optional*, defaults to 512):
  39. The number of input channels into the HiFi-GAN upsampling network.
  40. upsample_rates (`tuple[int]` or `list[int]`, *optional*, defaults to `[8, 8, 2, 2]`):
  41. A tuple of integers defining the stride of each 1D convolutional layer in the HiFi-GAN upsampling network.
  42. The length of `upsample_rates` defines the number of convolutional layers and has to match the length of
  43. `upsample_kernel_sizes`.
  44. upsample_kernel_sizes (`tuple[int]` or `list[int]`, *optional*, defaults to `[16, 16, 4, 4]`):
  45. A tuple of integers defining the kernel size of each 1D convolutional layer in the HiFi-GAN upsampling
  46. network. The length of `upsample_kernel_sizes` defines the number of convolutional layers and has to match
  47. the length of `upsample_rates`.
  48. resblock_kernel_sizes (`tuple[int]` or `list[int]`, *optional*, defaults to `[3, 7, 11]`):
  49. A tuple of integers defining the kernel sizes of the 1D convolutional layers in the HiFi-GAN
  50. multi-receptive field fusion (MRF) module.
  51. resblock_dilation_sizes (`tuple[tuple[int]]` or `list[list[int]]`, *optional*, defaults to `[[1, 3, 5], [1, 3, 5], [1, 3, 5]]`):
  52. A nested tuple of integers defining the dilation rates of the dilated 1D convolutional layers in the
  53. HiFi-GAN multi-receptive field fusion (MRF) module.
  54. leaky_relu_slope (`float`, *optional*, defaults to 0.1):
  55. The angle of the negative slope used by the leaky ReLU activation.
  56. depth_separable_channels (`int`, *optional*, defaults to 2):
  57. Number of channels to use in each depth-separable block.
  58. depth_separable_num_layers (`int`, *optional*, defaults to 3):
  59. Number of convolutional layers to use in each depth-separable block.
  60. duration_predictor_flow_bins (`int`, *optional*, defaults to 10):
  61. Number of channels to map using the unonstrained rational spline in the duration predictor model.
  62. duration_predictor_tail_bound (`float`, *optional*, defaults to 5.0):
  63. Value of the tail bin boundary when computing the unconstrained rational spline in the duration predictor
  64. model.
  65. duration_predictor_kernel_size (`int`, *optional*, defaults to 3):
  66. Kernel size of the 1D convolution layers used in the duration predictor model.
  67. duration_predictor_dropout (`float`, *optional*, defaults to 0.5):
  68. The dropout ratio for the duration predictor model.
  69. duration_predictor_num_flows (`int`, *optional*, defaults to 4):
  70. Number of flow stages used by the duration predictor model.
  71. duration_predictor_filter_channels (`int`, *optional*, defaults to 256):
  72. Number of channels for the convolution layers used in the duration predictor model.
  73. prior_encoder_num_flows (`int`, *optional*, defaults to 4):
  74. Number of flow stages used by the prior encoder flow model.
  75. prior_encoder_num_wavenet_layers (`int`, *optional*, defaults to 4):
  76. Number of WaveNet layers used by the prior encoder flow model.
  77. posterior_encoder_num_wavenet_layers (`int`, *optional*, defaults to 16):
  78. Number of WaveNet layers used by the posterior encoder model.
  79. wavenet_kernel_size (`int`, *optional*, defaults to 5):
  80. Kernel size of the 1D convolution layers used in the WaveNet model.
  81. wavenet_dilation_rate (`int`, *optional*, defaults to 1):
  82. Dilation rates of the dilated 1D convolutional layers used in the WaveNet model.
  83. wavenet_dropout (`float`, *optional*, defaults to 0.0):
  84. The dropout ratio for the WaveNet layers.
  85. speaking_rate (`float`, *optional*, defaults to 1.0):
  86. Speaking rate. Larger values give faster synthesised speech.
  87. noise_scale (`float`, *optional*, defaults to 0.667):
  88. How random the speech prediction is. Larger values create more variation in the predicted speech.
  89. noise_scale_duration (`float`, *optional*, defaults to 0.8):
  90. How random the duration prediction is. Larger values create more variation in the predicted durations.
  91. Example:
  92. ```python
  93. >>> from transformers import VitsModel, VitsConfig
  94. >>> # Initializing a "facebook/mms-tts-eng" style configuration
  95. >>> configuration = VitsConfig()
  96. >>> # Initializing a model (with random weights) from the "facebook/mms-tts-eng" style configuration
  97. >>> model = VitsModel(configuration)
  98. >>> # Accessing the model configuration
  99. >>> configuration = model.config
  100. ```"""
  101. model_type = "vits"
  102. vocab_size: int = 38
  103. hidden_size: int = 192
  104. num_hidden_layers: int = 6
  105. num_attention_heads: int = 2
  106. window_size: int = 4
  107. use_bias: bool = True
  108. ffn_dim: int = 768
  109. layerdrop: float | int = 0.1
  110. ffn_kernel_size: int = 3
  111. flow_size: int = 192
  112. spectrogram_bins: int = 513
  113. hidden_act: str = "relu"
  114. hidden_dropout: float | int = 0.1
  115. attention_dropout: float | int = 0.1
  116. activation_dropout: float | int = 0.1
  117. initializer_range: float = 0.02
  118. layer_norm_eps: float = 1e-5
  119. use_stochastic_duration_prediction: bool = True
  120. num_speakers: int = 1
  121. speaker_embedding_size: int = 0
  122. upsample_initial_channel: int = 512
  123. upsample_rates: list[int] | tuple[int, ...] = (8, 8, 2, 2)
  124. upsample_kernel_sizes: list[int] | tuple[int, ...] = (16, 16, 4, 4)
  125. resblock_kernel_sizes: list[int] | tuple[int, ...] = (3, 7, 11)
  126. resblock_dilation_sizes: list | tuple = ((1, 3, 5), (1, 3, 5), (1, 3, 5))
  127. leaky_relu_slope: float = 0.1
  128. depth_separable_channels: int = 2
  129. depth_separable_num_layers: int = 3
  130. duration_predictor_flow_bins: int = 10
  131. duration_predictor_tail_bound: float = 5.0
  132. duration_predictor_kernel_size: int = 3
  133. duration_predictor_dropout: float | int = 0.5
  134. duration_predictor_num_flows: int = 4
  135. duration_predictor_filter_channels: int = 256
  136. prior_encoder_num_flows: int = 4
  137. prior_encoder_num_wavenet_layers: int = 4
  138. posterior_encoder_num_wavenet_layers: int = 16
  139. wavenet_kernel_size: int = 5
  140. wavenet_dilation_rate: int = 1
  141. wavenet_dropout: float | int = 0.0
  142. speaking_rate: float | int = 1.0
  143. noise_scale: float = 0.667
  144. noise_scale_duration: float = 0.8
  145. sampling_rate: int = 16_000
  146. pad_token_id: int | None = None
  147. def validate_architecture(self):
  148. """Part of `@strict`-powered validation. Validates the architecture of the config."""
  149. if len(self.upsample_kernel_sizes) != len(self.upsample_rates):
  150. raise ValueError(
  151. f"The length of `upsample_kernel_sizes` ({len(self.upsample_kernel_sizes)}) must match the length of "
  152. f"`upsample_rates` ({len(self.upsample_rates)})"
  153. )
  154. __all__ = ["VitsConfig"]