configuration_dac.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Copyright 2024 Descript 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. """Dac model configuration"""
  15. import math
  16. import numpy as np
  17. from huggingface_hub.dataclasses import strict
  18. from ...configuration_utils import PreTrainedConfig
  19. from ...utils import auto_docstring
  20. @auto_docstring(checkpoint="descript/dac_16khz")
  21. @strict
  22. class DacConfig(PreTrainedConfig):
  23. r"""
  24. downsampling_ratios (`list[int]`, *optional*, defaults to `[2, 4, 8, 8]`):
  25. Ratios for downsampling in the encoder. These are used in reverse order for upsampling in the decoder.
  26. quantizer_dropout (`bool`, *optional*, defaults to 0):
  27. Whether to apply dropout to the quantizer.
  28. commitment_loss_weight (float, *optional*, defaults to 0.25):
  29. Weight of the commitment loss term in the VQVAE loss function.
  30. codebook_loss_weight (float, *optional*, defaults to 1.0):
  31. Weight of the codebook loss term in the VQVAE loss function.
  32. Example:
  33. ```python
  34. >>> from transformers import DacModel, DacConfig
  35. >>> # Initializing a "descript/dac_16khz" style configuration
  36. >>> configuration = DacConfig()
  37. >>> # Initializing a model (with random weights) from the "descript/dac_16khz" style configuration
  38. >>> model = DacModel(configuration)
  39. >>> # Accessing the model configuration
  40. >>> configuration = model.config
  41. ```"""
  42. model_type = "dac"
  43. encoder_hidden_size: int = 64
  44. downsampling_ratios: list[int] | tuple[int, ...] = (2, 4, 8, 8)
  45. decoder_hidden_size: int = 1536
  46. n_codebooks: int = 9
  47. codebook_size: int = 1024
  48. codebook_dim: int = 8
  49. quantizer_dropout: float | int = 0.0
  50. commitment_loss_weight: float = 0.25
  51. codebook_loss_weight: float = 1.0
  52. sampling_rate: int = 16000
  53. def __post_init__(self, **kwargs):
  54. self.upsampling_ratios = self.downsampling_ratios[::-1]
  55. self.hidden_size = self.encoder_hidden_size * (2 ** len(self.downsampling_ratios))
  56. self.hop_length = int(np.prod(self.downsampling_ratios))
  57. super().__post_init__(**kwargs)
  58. @property
  59. def frame_rate(self) -> int:
  60. hop_length = np.prod(self.upsampling_ratios)
  61. return math.ceil(self.sampling_rate / hop_length)
  62. __all__ = ["DacConfig"]