configuration_mamba.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. """MAMBA 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/mamba-2.8b")
  20. @strict
  21. class MambaConfig(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. use_bias (`bool`, *optional*, defaults to `False`):
  28. Whether or not to use bias in ["in_proj", "out_proj"] of the mixer block
  29. use_conv_bias (`bool`, *optional*, defaults to `True`):
  30. Whether or not to use bias in the convolution layer of the mixer block.
  31. residual_in_fp32 (`bool`, *optional*, defaults to `True`):
  32. Whether or not residuals should be in `float32`. If set to `False` residuals will keep the same `dtype` as the rest of the model
  33. rescale_prenorm_residual (`bool`, *optional*, defaults to `False`):
  34. Whether or not to rescale `out_proj` weights when initializing.
  35. use_mambapy (`bool`, *optional*, defaults to `False`):
  36. Determines the fallback strategy during training if the CUDA-based official implementation of Mamba is not available. If `True`,
  37. the mamba.py implementation is used. If `False`, the naive and slower implementation is used. Consider switching to the naive
  38. version if memory is limited.
  39. use_associative_scan (`bool`, *optional*, defaults to `True`):
  40. Whether to use PyTorch's `torch._higher_order_ops.associative_scan` for the parallel scan instead of the naive
  41. sequential implementation. The associative scan is only active during `torch.compile` tracing and
  42. requires torch >= 2.9.0. Both paths are tested to produce numerically identical results (see
  43. `test_associative_scan_matches_sequential`). Set to `False` to fall back to the sequential loop.
  44. Example:
  45. ```python
  46. >>> from transformers import MambaConfig, MambaModel
  47. >>> # Initializing a Mamba configuration
  48. >>> configuration = MambaConfig()
  49. >>> # Initializing a model (with random weights) from the configuration
  50. >>> model = MambaModel(configuration)
  51. >>> # Accessing the model configuration
  52. >>> configuration = model.config
  53. ```"""
  54. model_type = "mamba"
  55. vocab_size: int = 50280
  56. hidden_size: int = 768
  57. state_size: int = 16
  58. num_hidden_layers: int = 32
  59. layer_norm_epsilon: float = 1e-5
  60. pad_token_id: int | None = 0
  61. bos_token_id: int | None = 0
  62. eos_token_id: int | list[int] | None = 0
  63. expand: int = 2
  64. conv_kernel: int = 4
  65. use_bias: bool = False
  66. use_conv_bias: bool = True
  67. hidden_act: str = "silu"
  68. initializer_range: float = 0.1
  69. residual_in_fp32: bool = True
  70. time_step_rank: str | int = "auto"
  71. time_step_scale: float = 1.0
  72. time_step_min: float = 0.001
  73. time_step_max: float = 0.1
  74. time_step_init_scheme: str = "random"
  75. time_step_floor: float = 1e-4
  76. rescale_prenorm_residual: bool = False
  77. use_cache: bool = True
  78. use_mambapy: bool = False
  79. use_associative_scan: bool = True
  80. tie_word_embeddings: bool = True
  81. def __post_init__(self, **kwargs):
  82. self.intermediate_size = int(self.expand * self.hidden_size)
  83. self.time_step_rank = (
  84. math.ceil(self.hidden_size / 16) if self.time_step_rank == "auto" else self.time_step_rank
  85. )
  86. super().__post_init__(**kwargs)
  87. @property
  88. def layer_types(self):
  89. return ["mamba"] * self.num_hidden_layers
  90. __all__ = ["MambaConfig"]