configuration_dac.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # coding=utf-8
  2. # Copyright 2024 Descript 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. """Dac model configuration"""
  16. import math
  17. import numpy as np
  18. from ...configuration_utils import PretrainedConfig
  19. from ...utils import logging
  20. logger = logging.get_logger(__name__)
  21. class DacConfig(PretrainedConfig):
  22. r"""
  23. This is the configuration class to store the configuration of an [`DacModel`]. It is used to instantiate a
  24. Dac model according to the specified arguments, defining the model architecture. Instantiating a configuration
  25. with the defaults will yield a similar configuration to that of the
  26. [descript/dac_16khz](https://huggingface.co/descript/dac_16khz) architecture.
  27. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  28. documentation from [`PretrainedConfig`] for more information.
  29. Args:
  30. encoder_hidden_size (`int`, *optional*, defaults to 64):
  31. Intermediate representation dimension for the encoder.
  32. downsampling_ratios (`List[int]`, *optional*, defaults to `[2, 4, 8, 8]`):
  33. Ratios for downsampling in the encoder. These are used in reverse order for upsampling in the decoder.
  34. decoder_hidden_size (`int`, *optional*, defaults to 1536):
  35. Intermediate representation dimension for the decoder.
  36. n_codebooks (`int`, *optional*, defaults to 9):
  37. Number of codebooks in the VQVAE.
  38. codebook_size (`int`, *optional*, defaults to 1024):
  39. Number of discrete codes in each codebook.
  40. codebook_dim (`int`, *optional*, defaults to 8):
  41. Dimension of the codebook vectors. If not defined, uses `encoder_hidden_size`.
  42. quantizer_dropout (`bool`, *optional*, defaults to 0):
  43. Whether to apply dropout to the quantizer.
  44. commitment_loss_weight (float, *optional*, defaults to 0.25):
  45. Weight of the commitment loss term in the VQVAE loss function.
  46. codebook_loss_weight (float, *optional*, defaults to 1.0):
  47. Weight of the codebook loss term in the VQVAE loss function.
  48. sampling_rate (`int`, *optional*, defaults to 16000):
  49. The sampling rate at which the audio waveform should be digitalized expressed in hertz (Hz).
  50. Example:
  51. ```python
  52. >>> from transformers import DacModel, DacConfig
  53. >>> # Initializing a "descript/dac_16khz" style configuration
  54. >>> configuration = DacConfig()
  55. >>> # Initializing a model (with random weights) from the "descript/dac_16khz" style configuration
  56. >>> model = DacModel(configuration)
  57. >>> # Accessing the model configuration
  58. >>> configuration = model.config
  59. ```"""
  60. model_type = "dac"
  61. def __init__(
  62. self,
  63. encoder_hidden_size=64,
  64. downsampling_ratios=[2, 4, 8, 8],
  65. decoder_hidden_size=1536,
  66. n_codebooks=9,
  67. codebook_size=1024,
  68. codebook_dim=8,
  69. quantizer_dropout=0,
  70. commitment_loss_weight=0.25,
  71. codebook_loss_weight=1.0,
  72. sampling_rate=16000,
  73. **kwargs,
  74. ):
  75. self.encoder_hidden_size = encoder_hidden_size
  76. self.downsampling_ratios = downsampling_ratios
  77. self.decoder_hidden_size = decoder_hidden_size
  78. self.upsampling_ratios = downsampling_ratios[::-1]
  79. self.n_codebooks = n_codebooks
  80. self.codebook_size = codebook_size
  81. self.codebook_dim = codebook_dim
  82. self.quantizer_dropout = quantizer_dropout
  83. self.sampling_rate = sampling_rate
  84. self.hidden_size = encoder_hidden_size * (2 ** len(downsampling_ratios))
  85. self.hop_length = int(np.prod(downsampling_ratios))
  86. self.commitment_loss_weight = commitment_loss_weight
  87. self.codebook_loss_weight = codebook_loss_weight
  88. super().__init__(**kwargs)
  89. @property
  90. def frame_rate(self) -> int:
  91. hop_length = np.prod(self.upsampling_ratios)
  92. return math.ceil(self.sampling_rate / hop_length)