configuration_minimax.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  2. # This file was automatically generated from src/transformers/models/minimax/modular_minimax.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_minimax.py file directly. One of our CI enforces this.
  6. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  7. # coding=utf-8
  8. # Copyright 2025 MiniMaxAI and HuggingFace Inc. teams. All rights reserved.
  9. #
  10. #
  11. # Licensed under the Apache License, Version 2.0 (the "License");
  12. # you may not use this file except in compliance with the License.
  13. # You may obtain a copy of the License at
  14. #
  15. # http://www.apache.org/licenses/LICENSE-2.0
  16. #
  17. # Unless required by applicable law or agreed to in writing, software
  18. # distributed under the License is distributed on an "AS IS" BASIS,
  19. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. # See the License for the specific language governing permissions and
  21. # limitations under the License.
  22. from ...configuration_utils import PretrainedConfig, layer_type_validation
  23. class MiniMaxConfig(PretrainedConfig):
  24. r"""
  25. This is the configuration class to store the configuration of a [`MiniMaxModel`]. It is used to instantiate an
  26. MiniMax model according to the specified arguments, defining the model architecture. Instantiating a configuration
  27. with the defaults will yield a similar configuration to that of the MiniMax.
  28. [MiniMaxAI/MiniMax-Text-01-hf](https://huggingface.co/MiniMaxAI/MiniMax-Text-01-hf)
  29. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  30. documentation from [`PretrainedConfig`] for more information.
  31. Args:
  32. vocab_size (`int`, *optional*, defaults to 32000):
  33. Vocabulary size of the MiniMax model. Defines the number of different tokens that can be represented by the
  34. `inputs_ids` passed when calling [`MiniMaxModel`]
  35. hidden_size (`int`, *optional*, defaults to 4096):
  36. Dimension of the hidden representations.
  37. intermediate_size (`int`, *optional*, defaults to 14336):
  38. Dimension of the MLP representations.
  39. num_hidden_layers (`int`, *optional*, defaults to 32):
  40. Number of hidden layers in the Transformer encoder.
  41. num_attention_heads (`int`, *optional*, defaults to 32):
  42. Number of attention heads for each attention layer in the Transformer encoder.
  43. num_key_value_heads (`int`, *optional*, defaults to 8):
  44. This is the number of key_value heads that should be used to implement Grouped Query Attention. If
  45. `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
  46. `num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When
  47. converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
  48. by meanpooling all the original heads within that group. For more details, check out [this
  49. paper](https://huggingface.co/papers/2305.13245). If it is not specified, will default to `8`.
  50. head_dim (`int`, *optional*, defaults to `hidden_size // num_attention_heads`):
  51. The attention head dimension.
  52. hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
  53. The non-linear activation function (function or string) in the decoder.
  54. max_position_embeddings (`int`, *optional*, defaults to `4096*32`):
  55. The maximum sequence length that this model might ever be used with. MiniMax's sliding window attention
  56. allows sequence of up to 4096*32 tokens.
  57. initializer_range (`float`, *optional*, defaults to 0.02):
  58. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  59. rms_norm_eps (`float`, *optional*, defaults to 1e-05):
  60. The epsilon used by the rms normalization layers.
  61. use_cache (`bool`, *optional*, defaults to `True`):
  62. Whether or not the model should return the last key/values attentions (not used by all models). Only
  63. relevant if `config.is_decoder=True`.
  64. pad_token_id (`int`, *optional*):
  65. The id of the padding token.
  66. bos_token_id (`int`, *optional*, defaults to 1):
  67. The id of the "beginning-of-sequence" token.
  68. eos_token_id (`int`, *optional*, defaults to 2):
  69. The id of the "end-of-sequence" token.
  70. tie_word_embeddings (`bool`, *optional*, defaults to `False`):
  71. Whether the model's input and output word embeddings should be tied.
  72. rope_theta (`float`, *optional*, defaults to 1000000.0):
  73. The base period of the RoPE embeddings.
  74. sliding_window (`int`, *optional*):
  75. Sliding window attention window size. If not specified, will default to `4096`.
  76. attention_dropout (`float`, *optional*, defaults to 0.0):
  77. The dropout ratio for the attention probabilities.
  78. num_experts_per_tok (`int`, *optional*, defaults to 2):
  79. The number of experts to route per-token, can be also interpreted as the `top-k` routing
  80. parameter
  81. num_local_experts (`int`, *optional*, defaults to 8):
  82. Number of experts per Sparse MLP layer.
  83. output_router_logits (`bool`, *optional*, defaults to `False`):
  84. Whether or not the router logits should be returned by the model. Enabling this will also
  85. allow the model to output the auxiliary loss. See [here]() for more details
  86. router_aux_loss_coef (`float`, *optional*, defaults to 0.001):
  87. The aux loss factor for the total loss.
  88. router_jitter_noise (`float`, *optional*, defaults to 0.0):
  89. Amount of noise to add to the router.
  90. layer_types (`list`, *optional*):
  91. Attention pattern for each layer.
  92. block_size (`int`, *optional*, defaults to 256):
  93. The length of each attention block, determining how queries, keys, and values
  94. are grouped and processed for intra- and inter-block attention.
  95. full_attn_alpha_factor (`float`, *optional*, defaults to 1):
  96. Weight for residual value in residual connection after normal attention.
  97. full_attn_beta_factor (`float`, *optional*, defaults to 1):
  98. Weight for hidden state value in residual connection after normal attention.
  99. linear_attn_alpha_factor (`float`, *optional*, defaults to 1):
  100. Weight for residual value in residual connection after lightning attention.
  101. linear_attn_beta_factor (`float`, *optional*, defaults to 1):
  102. Weight for hidden state value in residual connection after lightning attention.
  103. mlp_alpha_factor (`float`, *optional*, defaults to 1):
  104. Weight for residual value in residual connection after MLP.
  105. mlp_beta_factor (`float`, *optional*, defaults to 1):
  106. Weight for hidden state value in residual connection after MLP.
  107. ```python
  108. >>> from transformers import MiniMaxModel, MiniMaxConfig
  109. >>> # Initializing a MiniMax style configuration
  110. >>> configuration = MiniMaxConfig()
  111. >>> # Initializing a model from the MiniMax style configuration
  112. >>> model = MiniMaxModel(configuration)
  113. >>> # Accessing the model configuration
  114. >>> configuration = model.config
  115. ```"""
  116. model_type = "minimax"
  117. keys_to_ignore_at_inference = ["past_key_values"]
  118. base_model_tp_plan = {
  119. "layers.*.self_attn.q_proj": "colwise",
  120. "layers.*.self_attn.k_proj": "colwise",
  121. "layers.*.self_attn.v_proj": "colwise",
  122. "layers.*.self_attn.o_proj": "rowwise",
  123. "layers.*.block_sparse_moe.gate": "colwise_rep", # we need to replicate here to correctly route experts
  124. "layers.*.block_sparse_moe.experts.*.w1": "colwise",
  125. "layers.*.block_sparse_moe.experts.*.w2": "rowwise",
  126. "layers.*.block_sparse_moe.experts.*.w3": "colwise",
  127. }
  128. base_model_pp_plan = {
  129. "embed_tokens": (["input_ids"], ["inputs_embeds"]),
  130. "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
  131. "norm": (["hidden_states"], ["hidden_states"]),
  132. }
  133. def __init__(
  134. self,
  135. vocab_size=32000,
  136. hidden_size=4096,
  137. intermediate_size=14336,
  138. num_hidden_layers=32,
  139. num_attention_heads=32,
  140. num_key_value_heads=8,
  141. head_dim=None,
  142. hidden_act="silu",
  143. max_position_embeddings=4096 * 32,
  144. initializer_range=0.02,
  145. rms_norm_eps=1e-5,
  146. use_cache=True,
  147. pad_token_id=None,
  148. bos_token_id=1,
  149. eos_token_id=2,
  150. tie_word_embeddings=False,
  151. rope_theta=1e6,
  152. sliding_window=None,
  153. attention_dropout=0.0,
  154. num_experts_per_tok=2,
  155. num_local_experts=8,
  156. output_router_logits=False,
  157. router_aux_loss_coef=0.001,
  158. router_jitter_noise=0.0,
  159. layer_types=None,
  160. block_size=256,
  161. full_attn_alpha_factor=1,
  162. full_attn_beta_factor=1,
  163. linear_attn_alpha_factor=1,
  164. linear_attn_beta_factor=1,
  165. mlp_alpha_factor=1,
  166. mlp_beta_factor=1,
  167. **kwargs,
  168. ):
  169. super().__init__(
  170. pad_token_id=pad_token_id,
  171. bos_token_id=bos_token_id,
  172. eos_token_id=eos_token_id,
  173. tie_word_embeddings=tie_word_embeddings,
  174. **kwargs,
  175. )
  176. self.vocab_size = vocab_size
  177. self.max_position_embeddings = max_position_embeddings
  178. self.hidden_size = hidden_size
  179. self.intermediate_size = intermediate_size
  180. self.num_hidden_layers = num_hidden_layers
  181. self.num_attention_heads = num_attention_heads
  182. self.sliding_window = sliding_window
  183. # for backward compatibility
  184. if num_key_value_heads is None:
  185. num_key_value_heads = num_attention_heads
  186. self.num_key_value_heads = num_key_value_heads
  187. self.hidden_act = hidden_act
  188. self.initializer_range = initializer_range
  189. self.rms_norm_eps = rms_norm_eps
  190. self.use_cache = use_cache
  191. self.rope_theta = rope_theta
  192. self.attention_dropout = attention_dropout
  193. self.head_dim = head_dim
  194. self.num_experts_per_tok = num_experts_per_tok
  195. self.num_local_experts = num_local_experts
  196. self.output_router_logits = output_router_logits
  197. self.router_aux_loss_coef = router_aux_loss_coef
  198. self.router_jitter_noise = router_jitter_noise
  199. self.layer_types = layer_types
  200. self.block_size = block_size
  201. self.full_attn_alpha_factor = full_attn_alpha_factor
  202. self.full_attn_beta_factor = full_attn_beta_factor
  203. self.linear_attn_alpha_factor = linear_attn_alpha_factor
  204. self.linear_attn_beta_factor = linear_attn_beta_factor
  205. self.mlp_alpha_factor = mlp_alpha_factor
  206. self.mlp_beta_factor = mlp_beta_factor
  207. if self.layer_types is None:
  208. self.layer_types = [
  209. "full_attention" if bool((i + 1) % 2) else "linear_attention" for i in range(self.num_hidden_layers)
  210. ]
  211. layer_type_validation(self.layer_types, self.num_hidden_layers)
  212. __all__ = ["MiniMaxConfig"]