configuration_mamba2.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # Copyright 2024 The HuggingFace Inc. team.
  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. """MAMBA2 configuration"""
  15. import math
  16. from huggingface_hub.dataclasses import strict
  17. from ...configuration_utils import PreTrainedConfig
  18. from ...utils import auto_docstring
  19. @auto_docstring(checkpoint="state-spaces/mamba2-2.8b")
  20. @strict
  21. class Mamba2Config(PreTrainedConfig):
  22. r"""
  23. layer_norm_epsilon (`float`, *optional*, defaults to 1e-05):
  24. The epsilon to use in the layer normalization layers..
  25. expand (`int`, *optional*, defaults to 2):
  26. Expanding factor used to determine the intermediate size.
  27. n_groups (`int`, *optional*, defaults to 8):
  28. Number of groups for the evolution matrices of mamba 2.
  29. use_bias (`bool`, *optional*, defaults to `False`):
  30. Whether or not to use bias in ["in_proj", "out_proj"] of the mixer block
  31. use_conv_bias (`bool`, *optional*, defaults to `True`):
  32. Whether or not to use bias in the convolution layer of the mixer block.
  33. residual_in_fp32 (`bool`, *optional*, defaults to `True`):
  34. Whether or not residuals should be in `float32`. If set to `False` residuals will keep the same `dtype` as the rest of the model
  35. rescale_prenorm_residual (`bool`, *optional*, defaults to `False`):
  36. Whether or not to rescale `out_proj` weights when initializing.
  37. chunk_size (`int`, *optional*, defaults to 256):
  38. Size of the chunks that will comprise the sequence.
  39. Example:
  40. ```python
  41. >>> from transformers import Mamba2Config, Mamba2Model
  42. >>> # Initializing a Mamba2 configuration
  43. >>> configuration = Mamba2Config()
  44. >>> # Initializing a model (with random weights) from the configuration
  45. >>> model = Mamba2Model(configuration)
  46. >>> # Accessing the model configuration
  47. >>> configuration = model.config
  48. ```"""
  49. model_type = "mamba2"
  50. num_heads: int = 128
  51. head_dim: int = 64
  52. vocab_size: int = 32768
  53. hidden_size: int = 4096
  54. state_size: int = 128
  55. num_hidden_layers: int = 64
  56. layer_norm_epsilon: float = 1e-5
  57. pad_token_id: int | None = 1
  58. bos_token_id: int | None = 0
  59. eos_token_id: int | list[int] | None = 2
  60. expand: int = 2
  61. conv_kernel: int = 4
  62. n_groups: int = 8
  63. use_bias: bool = False
  64. use_conv_bias: bool = True
  65. hidden_act: str = "silu"
  66. initializer_range: float = 0.1
  67. residual_in_fp32: bool = True
  68. time_step_rank: str | int = "auto"
  69. time_step_min: float = 0.001
  70. time_step_max: float = 0.1
  71. time_step_floor: float = 1e-4
  72. time_step_limit: list[float] | tuple[float, ...] = (0.0, float("inf"))
  73. rescale_prenorm_residual: bool = False
  74. use_cache: bool = True
  75. rms_norm: bool = True
  76. chunk_size: int = 256
  77. tie_word_embeddings: bool = False
  78. def __post_init__(self, **kwargs):
  79. self.time_step_rank = (
  80. math.ceil(self.hidden_size / 16) if self.time_step_rank == "auto" else self.time_step_rank
  81. )
  82. super().__post_init__(**kwargs)
  83. def validate_architecture(self):
  84. """Part of `@strict`-powered validation. Validates the architecture of the config."""
  85. if (self.hidden_size * self.expand) != (self.num_heads * self.head_dim):
  86. raise ValueError(
  87. "Inconsistent configuration: hidden_size * expand "
  88. f"({self.hidden_size * self.expand}) must equal num_heads * head_dim "
  89. f"({self.num_heads * self.head_dim})."
  90. )
  91. @property
  92. def layer_types(self):
  93. return ["mamba"] * self.num_hidden_layers
  94. __all__ = ["Mamba2Config"]