configuration_mamba2.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. # coding=utf-8
  2. # Copyright 2024 The HuggingFace Inc. team.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """MAMBA2 configuration"""
  16. import math
  17. from ...configuration_utils import PretrainedConfig
  18. from ...utils import logging
  19. logger = logging.get_logger(__name__)
  20. class Mamba2Config(PretrainedConfig):
  21. """
  22. This is the configuration class to store the configuration of a [`Mamba2Model`]. It is used to instantiate a MAMBA2
  23. model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
  24. defaults will yield a similar configuration to that of the MAMBA2
  25. [state-spaces/mamba2-2.8b](https://huggingface.co/state-spaces/mamba2-2.8b) architecture.
  26. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  27. documentation from [`PretrainedConfig`] for more information.
  28. Args:
  29. num_heads (`int`, *optional*, defaults to 128):
  30. Number of heads for the evolution matrices of mamba 2.
  31. head_dim (`int`, *optional*, defaults to 64):
  32. Dimension of each head.
  33. vocab_size (`int`, *optional*, defaults to 32768):
  34. Vocabulary size of the MAMBA2 model. Defines the number of different tokens that can be represented by the
  35. `inputs_ids` passed when calling [`Mamba2Model`].
  36. hidden_size (`int`, *optional*, defaults to 4096):
  37. Dimensionality of the embeddings and hidden states.
  38. state_size (`int`, *optional*, defaults to 128): shape of the state space latents.
  39. num_hidden_layers (`int`, *optional*, defaults to 64):
  40. Number of hidden layers in the model.
  41. layer_norm_epsilon (`float`, *optional*, defaults to 1e-05):
  42. The epsilon to use in the layer normalization layers.
  43. pad_token_id (`int`, *optional*, defaults to 1):
  44. Padding token id.
  45. bos_token_id (`int`, *optional*, defaults to 0):
  46. The id of the beginning of sentence token in the vocabulary.
  47. eos_token_id (`int`, *optional*, defaults to 2):
  48. The id of the end of sentence token in the vocabulary.
  49. expand (`int`, *optional*, defaults to 2): Expanding factor used to determine the intermediate size.
  50. conv_kernel (`int`, *optional*, defaults to 4): Size of the convolution kernel.
  51. n_groups (`int`, *optional*, defaults to 8):
  52. Number of groups for the evolution matrices of mamba 2.
  53. use_bias (`bool`, *optional*, defaults to `False`):
  54. Whether or not to use bias in ["in_proj", "out_proj"] of the mixer block
  55. use_conv_bias (`bool`, *optional*, defaults to `True`):
  56. Whether or not to use bias in the convolution layer of the mixer block.
  57. hidden_act (`str`, *optional*, defaults to `"silu"`):
  58. The non-linear activation function (function or string) in the decoder.
  59. initializer_range (`float`, *optional*, defaults to 0.1):
  60. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  61. residual_in_fp32 (`bool`, *optional*, defaults to `True`):
  62. Whether or not residuals should be in `float32`. If set to `False` residuals will keep the same `dtype` as the rest of the model
  63. time_step_rank (`Union[int,str]`, *optional*, defaults to `"auto"`):
  64. Rank of the discretization projection matrix. `"auto"` means that it will default to `math.ceil(self.hidden_size / 16)`
  65. time_step_min (`float`, *optional*, defaults to 0.001):
  66. Minimum `time_step` used to bound `dt_proj.bias`.
  67. time_step_max (`float`, *optional*, defaults to 0.1):
  68. Maximum `time_step` used to bound `dt_proj.bias`.
  69. time_step_floor (`float`, *optional*, defaults to 0.0001):
  70. Minimum clamping value of the `dt_proj.bias` layer initialization.
  71. time_step_limit (`tuple`, *optional*, defaults to `(0.0, inf)`):
  72. Accepted range of time step values.
  73. rescale_prenorm_residual (`bool`, *optional*, defaults to `False`):
  74. Whether or not to rescale `out_proj` weights when initializing.
  75. use_cache (`bool`, *optional*, defaults to `True`):
  76. Whether or not the cache should be used.
  77. rms_norm (`bool`, *optional*, defaults to `True`):
  78. Whether to use RMS norm or not.
  79. chunk_size (`int`, *optional*, defaults to 256):
  80. Size of the chunks that will comprise the sequence.
  81. tie_word_embeddings (`bool`, *optional*, defaults to `False`):
  82. Whether to tie word embeddings or not.
  83. Example:
  84. ```python
  85. >>> from transformers import Mamba2Config, Mamba2Model
  86. >>> # Initializing a Mamba2 configuration
  87. >>> configuration = Mamba2Config()
  88. >>> # Initializing a model (with random weights) from the configuration
  89. >>> model = Mamba2Model(configuration)
  90. >>> # Accessing the model configuration
  91. >>> configuration = model.config
  92. ```"""
  93. model_type = "mamba2"
  94. def __init__(
  95. self,
  96. num_heads=128,
  97. head_dim=64,
  98. vocab_size=32768,
  99. hidden_size=4096,
  100. state_size=128,
  101. num_hidden_layers=64,
  102. layer_norm_epsilon=1e-5,
  103. pad_token_id=1,
  104. bos_token_id=0,
  105. eos_token_id=2,
  106. expand=2,
  107. conv_kernel=4,
  108. n_groups=8,
  109. use_bias=False,
  110. use_conv_bias=True,
  111. hidden_act="silu",
  112. initializer_range=0.1,
  113. residual_in_fp32=True,
  114. time_step_rank="auto",
  115. time_step_min=0.001,
  116. time_step_max=0.1,
  117. time_step_floor=1e-4,
  118. time_step_limit=(0.0, float("inf")),
  119. rescale_prenorm_residual=False,
  120. use_cache=True,
  121. rms_norm=True,
  122. chunk_size=256,
  123. tie_word_embeddings=False,
  124. **kwargs,
  125. ):
  126. if (hidden_size * expand) != (num_heads * head_dim):
  127. raise ValueError(
  128. "Inconsistent configuration: hidden_size * expand "
  129. f"({hidden_size * expand}) must equal num_heads * head_dim "
  130. f"({num_heads * head_dim})."
  131. )
  132. self.vocab_size = vocab_size
  133. self.hidden_size = hidden_size
  134. self.state_size = state_size
  135. self.num_hidden_layers = num_hidden_layers
  136. self.layer_norm_epsilon = layer_norm_epsilon
  137. self.conv_kernel = conv_kernel
  138. self.expand = expand
  139. self.bos_token_id = bos_token_id
  140. self.eos_token_id = eos_token_id
  141. self.pad_token_id = pad_token_id
  142. self.use_bias = use_bias
  143. self.use_conv_bias = use_conv_bias
  144. self.hidden_act = hidden_act
  145. self.initializer_range = initializer_range
  146. self.time_step_rank = math.ceil(self.hidden_size / 16) if time_step_rank == "auto" else time_step_rank
  147. self.time_step_min = time_step_min
  148. self.time_step_max = time_step_max
  149. self.time_step_floor = time_step_floor
  150. self.rescale_prenorm_residual = rescale_prenorm_residual
  151. self.residual_in_fp32 = residual_in_fp32
  152. self.use_cache = use_cache
  153. self.n_groups = n_groups
  154. self.num_heads = num_heads
  155. self.head_dim = head_dim
  156. self.rms_norm = rms_norm
  157. self.state_size = state_size
  158. self.chunk_size = chunk_size
  159. self.time_step_limit = time_step_limit
  160. self.tie_word_embeddings = tie_word_embeddings
  161. super().__init__(
  162. bos_token_id=bos_token_id,
  163. eos_token_id=eos_token_id,
  164. pad_token_id=pad_token_id,
  165. tie_word_embeddings=tie_word_embeddings,
  166. **kwargs,
  167. )
  168. __all__ = ["Mamba2Config"]