configuration_clap.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. # Copyright 2023 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. """CLAP model configuration"""
  15. from huggingface_hub.dataclasses import strict
  16. from ...configuration_utils import PreTrainedConfig
  17. from ...utils import auto_docstring, logging
  18. logger = logging.get_logger(__name__)
  19. @auto_docstring(checkpoint="laion/clap-htsat-fused")
  20. @strict
  21. class ClapTextConfig(PreTrainedConfig):
  22. r"""
  23. Examples:
  24. ```python
  25. >>> from transformers import ClapTextConfig, ClapTextModel
  26. >>> # Initializing a CLAP text configuration
  27. >>> configuration = ClapTextConfig()
  28. >>> # Initializing a model (with random weights) from the configuration
  29. >>> model = ClapTextModel(configuration)
  30. >>> # Accessing the model configuration
  31. >>> configuration = model.config
  32. ```"""
  33. model_type = "clap_text_model"
  34. base_config_key = "text_config"
  35. vocab_size: int = 50265
  36. hidden_size: int = 768
  37. num_hidden_layers: int = 12
  38. num_attention_heads: int = 12
  39. intermediate_size: int = 3072
  40. hidden_act: str = "gelu"
  41. hidden_dropout_prob: float | int = 0.1
  42. attention_probs_dropout_prob: float | int = 0.1
  43. max_position_embeddings: int = 514
  44. type_vocab_size: int = 1
  45. initializer_factor: float = 1.0
  46. layer_norm_eps: float = 1e-12
  47. projection_dim: int = 512
  48. pad_token_id: int | None = 1
  49. bos_token_id: int | None = 0
  50. eos_token_id: int | list[int] | None = 2
  51. projection_hidden_act: str = "relu"
  52. @auto_docstring(checkpoint="laion/clap-htsat-fused")
  53. @strict
  54. class ClapAudioConfig(PreTrainedConfig):
  55. r"""
  56. window_size (`int`, *optional*, defaults to 8):
  57. Image size of the spectrogram
  58. spec_size (`int`, *optional*, defaults to 256):
  59. Desired input size of the spectrogram that the model supports. It can be different from the output of the
  60. `ClapFeatureExtractor`, in which case the input features will be resized. Corresponds to the `image_size`
  61. of the audio models.
  62. patch_stride (`list`, *optional*, defaults to `[4, 4]`):
  63. Patch stride for the audio spectrogram
  64. num_classes (`int`, *optional*, defaults to 527):
  65. Number of classes used for the head training
  66. enable_fusion (`bool`, *optional*, defaults to `False`):
  67. Whether or not to enable patch fusion. This is the main contribution of the authors, and should give the
  68. best results.
  69. fusion_type (`[type]`, *optional*):
  70. Fusion type used for the patch fusion.
  71. patch_embed_input_channels (`int`, *optional*, defaults to 1):
  72. Number of channels used for the input spectrogram
  73. flatten_patch_embeds (`bool`, *optional*, defaults to `True`):
  74. Whether or not to flatten the patch embeddings
  75. patch_embeds_hidden_size (`int`, *optional*, defaults to 96):
  76. Hidden size of the patch embeddings. It is used as the number of output channels.
  77. enable_patch_layer_norm (`bool`, *optional*, defaults to `True`):
  78. Whether or not to enable layer normalization for the patch embeddings
  79. aff_block_r (`int`, *optional*, defaults to 4):
  80. downsize_ratio used in the AudioFF block
  81. Example:
  82. ```python
  83. >>> from transformers import ClapAudioConfig, ClapAudioModel
  84. >>> # Initializing a ClapAudioConfig with laion/clap-htsat-fused style configuration
  85. >>> configuration = ClapAudioConfig()
  86. >>> # Initializing a ClapAudioModel (with random weights) from the laion/clap-htsat-fused style configuration
  87. >>> model = ClapAudioModel(configuration)
  88. >>> # Accessing the model configuration
  89. >>> configuration = model.config
  90. ```"""
  91. model_type = "clap_audio_model"
  92. base_config_key = "audio_config"
  93. window_size: int = 8
  94. num_mel_bins: int = 64
  95. spec_size: int = 256
  96. hidden_act: str = "gelu"
  97. patch_size: int | list[int] | tuple[int, int] = 4
  98. patch_stride: int | list[int] | tuple[int, ...] = (4, 4)
  99. num_classes: int = 527
  100. hidden_size: int = 768
  101. projection_dim: int = 512
  102. depths: list[int] | tuple[int, ...] = (2, 2, 6, 2)
  103. num_attention_heads: list[int] | tuple[int, ...] = (4, 8, 16, 32)
  104. enable_fusion: bool = False
  105. hidden_dropout_prob: float | int = 0.1
  106. fusion_type: str | None = None
  107. patch_embed_input_channels: int = 1
  108. flatten_patch_embeds: bool = True
  109. patch_embeds_hidden_size: int = 96
  110. enable_patch_layer_norm: bool = True
  111. drop_path_rate: float | int = 0.0
  112. attention_probs_dropout_prob: float | int = 0.0
  113. qkv_bias: bool = True
  114. mlp_ratio: float = 4.0
  115. aff_block_r: int = 4
  116. num_hidden_layers: int = 4
  117. projection_hidden_act: str = "relu"
  118. layer_norm_eps: float = 1e-5
  119. initializer_factor: float = 1.0
  120. @auto_docstring(checkpoint="laion/clap-htsat-fused")
  121. @strict
  122. class ClapConfig(PreTrainedConfig):
  123. r"""
  124. Example:
  125. ```python
  126. >>> from transformers import ClapConfig, ClapModel
  127. >>> # Initializing a ClapConfig with laion-ai/base style configuration
  128. >>> configuration = ClapConfig()
  129. >>> # Initializing a ClapModel (with random weights) from the laion-ai/base style configuration
  130. >>> model = ClapModel(configuration)
  131. >>> # Accessing the model configuration
  132. >>> configuration = model.config
  133. >>> # We can also initialize a ClapConfig from a ClapTextConfig and a ClapAudioConfig
  134. >>> from transformers import ClapTextConfig, ClapAudioConfig
  135. >>> # Initializing a ClapText and ClapAudioConfig configuration
  136. >>> config_text = ClapTextConfig()
  137. >>> config_audio = ClapAudioConfig()
  138. >>> config = ClapConfig(text_config=config_text, audio_config=config_audio)
  139. ```"""
  140. model_type = "clap"
  141. sub_configs = {"text_config": ClapTextConfig, "audio_config": ClapAudioConfig}
  142. text_config: dict | PreTrainedConfig | None = None
  143. audio_config: dict | PreTrainedConfig | None = None
  144. logit_scale_init_value: float = 1 / 0.07
  145. projection_dim: int = 512
  146. projection_hidden_act: str = "relu"
  147. initializer_factor: float = 1.0
  148. def __post_init__(self, **kwargs):
  149. if self.text_config is None:
  150. self.text_config = ClapTextConfig()
  151. logger.info("`text_config` is `None`. initializing the `ClapTextConfig` with default values.")
  152. elif isinstance(self.text_config, dict):
  153. self.text_config = ClapTextConfig(**self.text_config)
  154. if self.audio_config is None:
  155. self.audio_config = ClapAudioConfig()
  156. logger.info("`audio_config` is `None`. initializing the `ClapAudioConfig` with default values.")
  157. elif isinstance(self.audio_config, dict):
  158. self.audio_config = ClapAudioConfig(**self.audio_config)
  159. self.text_config.projection_dim = self.projection_dim
  160. self.audio_config.projection_dim = self.projection_dim
  161. self.text_config.projection_hidden_act = self.projection_hidden_act
  162. self.audio_config.projection_hidden_act = self.projection_hidden_act
  163. self.hidden_size = self.text_config.hidden_size
  164. self.num_hidden_layers = self.text_config.num_hidden_layers + len(self.audio_config.depths)
  165. super().__post_init__(**kwargs)
  166. __all__ = ["ClapAudioConfig", "ClapConfig", "ClapTextConfig"]