configuration_moonshine.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  2. # This file was automatically generated from src/transformers/models/moonshine/modular_moonshine.py.
  3. # Do NOT edit this file manually as any edits will be overwritten by the generation of
  4. # the file from the modular. If any change should be done, please apply the change to the
  5. # modular_moonshine.py file directly. One of our CI enforces this.
  6. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  7. # Copyright 2025 The HuggingFace Inc. team. All rights reserved.
  8. #
  9. # Licensed under the Apache License, Version 2.0 (the "License");
  10. # you may not use this file except in compliance with the License.
  11. # You may obtain a copy of the License at
  12. #
  13. # http://www.apache.org/licenses/LICENSE-2.0
  14. #
  15. # Unless required by applicable law or agreed to in writing, software
  16. # distributed under the License is distributed on an "AS IS" BASIS,
  17. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. # See the License for the specific language governing permissions and
  19. # limitations under the License.
  20. from ...configuration_utils import PretrainedConfig
  21. from ...modeling_rope_utils import rope_config_validation
  22. class MoonshineConfig(PretrainedConfig):
  23. r"""
  24. This is the configuration class to store the configuration of a [`MoonshineModel`]. It is used to instantiate a Moonshine
  25. model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
  26. defaults will yield a similar configuration to that of the Moonshine
  27. [UsefulSensors/moonshine-tiny](https://huggingface.co/UsefulSensors/moonshine-tiny).
  28. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  29. documentation from [`PretrainedConfig`] for more information.
  30. Args:
  31. vocab_size (`int`, *optional*, defaults to 32768):
  32. Vocabulary size of the Moonshine model. Defines the number of different tokens that can be represented by the
  33. `inputs_ids` passed when calling [`MoonshineModel`].
  34. hidden_size (`int`, *optional*, defaults to 288):
  35. Dimension of the hidden representations.
  36. intermediate_size (`int`, *optional*, defaults to 1152):
  37. Dimension of the MLP representations.
  38. encoder_num_hidden_layers (`int`, *optional*, defaults to 6):
  39. Number of hidden layers in the Transformer encoder.
  40. decoder_num_hidden_layers (`int`, *optional*, defaults to 6):
  41. Number of hidden layers in the Transformer decoder.
  42. encoder_num_attention_heads (`int`, *optional*, defaults to 8):
  43. Number of attention heads for each attention layer in the Transformer encoder.
  44. decoder_num_attention_heads (`int`, *optional*, defaults to 8):
  45. Number of attention heads for each attention layer in the Transformer decoder.
  46. encoder_num_key_value_heads (`int`, *optional*):
  47. This is the number of key_value heads that should be used to implement Grouped Query Attention. If
  48. `encoder_num_key_value_heads=encoder_num_attention_heads`, the model will use Multi Head Attention (MHA), if
  49. `encoder_num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When
  50. converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
  51. by meanpooling all the original heads within that group. For more details, check out [this
  52. paper](https://huggingface.co/papers/2305.13245). If it is not specified, will default to
  53. `num_attention_heads`.
  54. decoder_num_key_value_heads (`int`, *optional*):
  55. This is the number of key_value heads that should be used to implement Grouped Query Attention. If
  56. `decoder_num_key_value_heads=decoder_num_attention_heads`, the model will use Multi Head Attention (MHA), if
  57. `decoder_num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When
  58. converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
  59. by meanpooling all the original heads within that group. For more details, check out [this
  60. paper](https://huggingface.co/papers/2305.13245). If it is not specified, will default to
  61. `decoder_num_attention_heads`.
  62. pad_head_dim_to_multiple_of (`int`, *optional*):
  63. Pad head dimension in encoder and decoder to the next multiple of this value. Necessary for using certain
  64. optimized attention implementations.
  65. encoder_hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`):
  66. The non-linear activation function (function or string) in the encoder.
  67. decoder_hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
  68. The non-linear activation function (function or string) in the decoder.
  69. max_position_embeddings (`int`, *optional*, defaults to 512):
  70. The maximum sequence length that this model might ever be used with.
  71. initializer_range (`float`, *optional*, defaults to 0.02):
  72. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  73. decoder_start_token_id (`int`, *optional*, defaults to 1):
  74. Corresponds to the "<|startoftranscript|>" token, which is automatically used when no `decoder_input_ids`
  75. are provided to the `generate` function. It is used to guide the model`s generation process depending on
  76. the task.
  77. use_cache (`bool`, *optional*, defaults to `True`):
  78. Whether or not the model should return the last key/values attentions (not used by all models).
  79. rope_theta (`float`, *optional*, defaults to 10000.0):
  80. The base period of the RoPE embeddings.
  81. rope_scaling (`Dict`, *optional*):
  82. Dictionary containing the scaling configuration for the RoPE embeddings. NOTE: if you apply new rope type
  83. and you expect the model to work on longer `max_position_embeddings`, we recommend you to update this value
  84. accordingly.
  85. Expected contents:
  86. `rope_type` (`str`):
  87. The sub-variant of RoPE to use. Can be one of ['default', 'linear', 'dynamic', 'yarn', 'longrope',
  88. 'llama3'], with 'default' being the original RoPE implementation.
  89. `factor` (`float`, *optional*):
  90. Used with all rope types except 'default'. The scaling factor to apply to the RoPE embeddings. In
  91. most scaling types, a `factor` of x will enable the model to handle sequences of length x *
  92. original maximum pre-trained length.
  93. `original_max_position_embeddings` (`int`, *optional*):
  94. Used with 'dynamic', 'longrope' and 'llama3'. The original max position embeddings used during
  95. pretraining.
  96. `attention_factor` (`float`, *optional*):
  97. Used with 'yarn' and 'longrope'. The scaling factor to be applied on the attention
  98. computation. If unspecified, it defaults to value recommended by the implementation, using the
  99. `factor` field to infer the suggested value.
  100. `beta_fast` (`float`, *optional*):
  101. Only used with 'yarn'. Parameter to set the boundary for extrapolation (only) in the linear
  102. ramp function. If unspecified, it defaults to 32.
  103. `beta_slow` (`float`, *optional*):
  104. Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear
  105. ramp function. If unspecified, it defaults to 1.
  106. `short_factor` (`list[float]`, *optional*):
  107. Only used with 'longrope'. The scaling factor to be applied to short contexts (<
  108. `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden
  109. size divided by the number of attention heads divided by 2
  110. `long_factor` (`list[float]`, *optional*):
  111. Only used with 'longrope'. The scaling factor to be applied to long contexts (<
  112. `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden
  113. size divided by the number of attention heads divided by 2
  114. `low_freq_factor` (`float`, *optional*):
  115. Only used with 'llama3'. Scaling factor applied to low frequency components of the RoPE
  116. `high_freq_factor` (`float`, *optional*):
  117. Only used with 'llama3'. Scaling factor applied to high frequency components of the RoPE
  118. partial_rotary_factor (`float`, *optional*, defaults to 0.9):
  119. Percentage of the query and keys which will have rotary embedding.
  120. is_encoder_decoder (`bool`, *optional*, defaults to `True`):
  121. Whether the model is used as an encoder/decoder or not.
  122. attention_bias (`bool`, *optional*, defaults to `False`):
  123. Whether to use a bias in the query, key, value and output projection layers during self-attention.
  124. attention_dropout (`float`, *optional*, defaults to 0.0):
  125. The dropout ratio for the attention probabilities.
  126. bos_token_id (`int`, *optional*, defaults to 1):
  127. Denotes beginning of sequences token id.
  128. eos_token_id (`int`, *optional*, defaults to 2):
  129. Denotes end of sequences token id.
  130. Example:
  131. ```python
  132. >>> from transformers import MoonshineModel, MoonshineConfig
  133. >>> # Initializing a Moonshine style configuration
  134. >>> configuration = MoonshineConfig().from_pretrained("UsefulSensors/moonshine-tiny")
  135. >>> # Initializing a model from the configuration
  136. >>> model = MoonshineModel(configuration)
  137. >>> # Accessing the model configuration
  138. >>> configuration = model.config
  139. ```"""
  140. model_type = "moonshine"
  141. keys_to_ignore_at_inference = ["past_key_values"]
  142. attribute_map = {
  143. "num_key_value_heads": "encoder_num_key_value_heads",
  144. "num_attention_heads": "encoder_num_attention_heads",
  145. "num_hidden_layers": "encoder_num_hidden_layers",
  146. }
  147. def __init__(
  148. self,
  149. vocab_size=32768,
  150. hidden_size=288,
  151. intermediate_size=1152,
  152. encoder_num_hidden_layers=6,
  153. decoder_num_hidden_layers=6,
  154. encoder_num_attention_heads=8,
  155. decoder_num_attention_heads=8,
  156. encoder_num_key_value_heads=None,
  157. decoder_num_key_value_heads=None,
  158. pad_head_dim_to_multiple_of=None,
  159. encoder_hidden_act="gelu",
  160. decoder_hidden_act="silu",
  161. max_position_embeddings=512,
  162. initializer_range=0.02,
  163. decoder_start_token_id=1,
  164. use_cache=True,
  165. rope_theta=10000.0,
  166. rope_scaling=None,
  167. partial_rotary_factor=0.9,
  168. is_encoder_decoder=True,
  169. attention_bias=False,
  170. attention_dropout=0.0,
  171. bos_token_id=1,
  172. eos_token_id=2,
  173. **kwargs,
  174. ):
  175. self.vocab_size = vocab_size
  176. self.hidden_size = hidden_size
  177. self.intermediate_size = intermediate_size
  178. self.encoder_num_hidden_layers = encoder_num_hidden_layers
  179. self.decoder_num_hidden_layers = decoder_num_hidden_layers
  180. self.encoder_num_attention_heads = encoder_num_attention_heads
  181. self.decoder_num_attention_heads = decoder_num_attention_heads
  182. if encoder_num_key_value_heads is None:
  183. encoder_num_key_value_heads = encoder_num_attention_heads
  184. self.encoder_num_key_value_heads = encoder_num_key_value_heads
  185. if decoder_num_key_value_heads is None:
  186. decoder_num_key_value_heads = decoder_num_attention_heads
  187. self.decoder_num_key_value_heads = decoder_num_key_value_heads
  188. self.pad_head_dim_to_multiple_of = pad_head_dim_to_multiple_of
  189. self.encoder_hidden_act = encoder_hidden_act
  190. self.decoder_hidden_act = decoder_hidden_act
  191. self.max_position_embeddings = max_position_embeddings
  192. self.initializer_range = initializer_range
  193. self.decoder_start_token_id = decoder_start_token_id
  194. self.use_cache = use_cache
  195. self.rope_theta = rope_theta
  196. self.rope_scaling = rope_scaling
  197. self.partial_rotary_factor = partial_rotary_factor
  198. self.is_encoder_decoder = is_encoder_decoder
  199. self.attention_bias = attention_bias
  200. self.attention_dropout = attention_dropout
  201. # Validate the correctness of rotary position embeddings parameters
  202. rope_config_validation(self)
  203. super().__init__(
  204. bos_token_id=bos_token_id,
  205. eos_token_id=eos_token_id,
  206. is_encoder_decoder=is_encoder_decoder,
  207. decoder_start_token_id=decoder_start_token_id,
  208. **kwargs,
  209. )
  210. __all__ = ["MoonshineConfig"]