configuration_voxtral.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # Copyright 2025 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. from huggingface_hub.dataclasses import strict
  15. from ...configuration_utils import PreTrainedConfig
  16. from ...utils import auto_docstring
  17. from ..auto import CONFIG_MAPPING, AutoConfig
  18. @auto_docstring(checkpoint="mistralai/Voxtral-Mini-3B-2507")
  19. @strict
  20. class VoxtralEncoderConfig(PreTrainedConfig):
  21. r"""
  22. max_source_positions (`int`, *optional*, defaults to 1500):
  23. The maximum sequence length of log-mel filter-bank features that this model might ever be used with.
  24. ```python
  25. >>> from transformers import VoxtralEncoderConfig, VoxtralEncoder
  26. >>> # Initializing a VoxtralEncoderConfig
  27. >>> configuration = VoxtralEncoderConfig()
  28. >>> # Initializing a VoxtralEncoder (with random weights)
  29. >>> model = VoxtralEncoder(configuration)
  30. >>> # Accessing the model configuration
  31. >>> configuration = model.config
  32. ```
  33. """
  34. model_type = "voxtral_encoder"
  35. attribute_map = {
  36. "d_model": "hidden_size",
  37. "encoder_layers": "num_hidden_layers",
  38. "encoder_attention_heads": "num_attention_heads",
  39. "encoder_ffn_dim": "intermediate_size",
  40. "encoder_layerdrop": "layerdrop",
  41. }
  42. vocab_size: int = 51866
  43. hidden_size: int = 1280
  44. intermediate_size: int = 5120
  45. num_hidden_layers: int = 32
  46. num_attention_heads: int = 20
  47. scale_embedding: bool = False
  48. activation_function: str = "gelu"
  49. num_mel_bins: int = 128
  50. max_source_positions: int = 1500
  51. initializer_range: float = 0.02
  52. attention_dropout: float | int = 0.0
  53. # TODO: @eustlb, we do not use dropout and layerdrop, yet we need to hardcode them
  54. # to be able to use Whisper with modular (here actually from Qwen2-Audio and copied from).
  55. # After a future Whisper refactor, we should remove this.
  56. dropout: float | int = 0.0
  57. layerdrop: float | int = 0.0
  58. activation_dropout: float | int = 0.0
  59. @auto_docstring(checkpoint="mistralai/Voxtral-Mini-3B-2507")
  60. @strict
  61. class VoxtralConfig(PreTrainedConfig):
  62. r"""
  63. Example:
  64. ```python
  65. >>> from transformers import VoxtralForConditionalGeneration, VoxtralConfig
  66. >>> # Initializing a Voxtral configuration
  67. >>> configuration = VoxtralConfig(audio_token_id=24, projector_hidden_act="gelu")
  68. >>> # Initializing a 3B model with random weights
  69. >>> model = VoxtralForConditionalGeneration(configuration)
  70. >>> # Accessing the model configuration
  71. >>> configuration = model.config
  72. ```"""
  73. model_type = "voxtral"
  74. sub_configs = {"text_config": AutoConfig, "audio_config": AutoConfig}
  75. _default_text_config_kwargs = {
  76. "vocab_size": 131072,
  77. "hidden_size": 3072,
  78. "intermediate_size": 8192,
  79. "num_hidden_layers": 30,
  80. "num_key_value_heads": 8,
  81. "max_position_embeddings": 131072,
  82. "rms_norm_eps": 1e-05,
  83. "use_cache": True,
  84. "rope_theta": 100000000.0,
  85. "head_dim": 128,
  86. }
  87. audio_config: dict | PreTrainedConfig | None = None
  88. text_config: dict | PreTrainedConfig | None = None
  89. audio_token_id: int | None = None
  90. projector_hidden_act: str = "gelu"
  91. def __post_init__(self, **kwargs):
  92. if isinstance(self.audio_config, dict):
  93. self.audio_config["model_type"] = self.audio_config.get("model_type", "voxtral_encoder")
  94. self.audio_config = CONFIG_MAPPING[self.audio_config["model_type"]](**self.audio_config)
  95. elif self.audio_config is None:
  96. self.audio_config = CONFIG_MAPPING["voxtral_encoder"]()
  97. if isinstance(self.text_config, dict):
  98. self.text_config["model_type"] = self.text_config.get("model_type", "llama")
  99. self.text_config = CONFIG_MAPPING[self.text_config["model_type"]](
  100. **{**self._default_text_config_kwargs, **self.text_config}
  101. )
  102. elif self.text_config is None:
  103. self.text_config = CONFIG_MAPPING["llama"](**self._default_text_config_kwargs)
  104. self.hidden_size = self.text_config.hidden_size
  105. super().__post_init__(**kwargs)
  106. __all__ = ["VoxtralEncoderConfig", "VoxtralConfig"]