configuration_zamba2.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  2. # This file was automatically generated from src/transformers/models/zamba2/modular_zamba2.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_zamba2.py file directly. One of our CI enforces this.
  6. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  7. # coding=utf-8
  8. # Copyright 2024 Zyphra Technologies and the HuggingFace Inc. team. 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
  23. class Zamba2Config(PretrainedConfig):
  24. r"""
  25. This is the configuration class to store the configuration of a [`Zamba2Model`]. It is used to instantiate a
  26. Zamba2 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 Zamba2 model.
  28. [Zyphra/Zamba2-2.7B](https://huggingface.co/Zyphra/Zamba2-2.7B)
  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 Zamba2 model. Defines the number of different tokens that can be represented by the
  34. `inputs_ids` passed when calling [`Zamba2Model`]
  35. max_position_embeddings (`int`, *optional*, defaults to 4096):
  36. The maximum sequence length that this model might ever be used with.
  37. hidden_size (`int`, *optional*, defaults to 2560):
  38. Dimension of the hidden representations.
  39. num_hidden_layers (`int`, *optional*, defaults to 54):
  40. Number of hidden layers in the model.
  41. layers_block_type (`list`, *optional*):
  42. List of layer types, which can be either "mamba" or "hybrid".
  43. mamba_d_state (`int`, *optional*, defaults to 64): shape of the state space latents.
  44. mamba_d_conv (`int`, *optional*, defaults to 4): Size of the convolution kernel.
  45. mamba_expand (`int`, *optional*, defaults to 2): Expanding factor used to determine the intermediate size.
  46. mamba_ngroups (`int`, *optional*, defaults to 1):
  47. Number of groups for the evolution matrices of mamba 2.
  48. time_step_min (`float`, *optional*, defaults to 0.001):
  49. Minimum `time_step` used to bound `dt_proj.bias`.
  50. time_step_max (`float`, *optional*, defaults to 0.1):
  51. Maximum `time_step` used to bound `dt_proj.bias`.
  52. time_step_floor (`float`, *optional*, defaults to 0.0001):
  53. Minimum clamping value of the `dt_proj.bias` layer initialization.
  54. time_step_limit (`tuple`, *optional*):
  55. Accepted range of time step values.
  56. n_mamba_heads (`int`, *optional*, defaults to 8):
  57. Number of heads for the evolution matrices of mamba 2.
  58. use_conv_bias (`bool`, *optional*, defaults to `True`):
  59. Whether or not to use bias in the convolution layer of the mixer block.
  60. chunk_size (`int`, *optional*, defaults to 256):
  61. Size of the chunks that will comprise the sequence.
  62. use_mem_eff_path (`bool`, *optional*, defaults to `False`):
  63. Whether or not to use the fused conv1d and scan in mamba2 layers.
  64. add_bias_linear (`bool`, *optional*, defaults to `False`):
  65. Flag indicating whether or not to use bias in various layers
  66. intermediate_size (`int`, *optional*, defaults to 4 * hidden_size):
  67. Dimension of the MLP representations.
  68. hidden_act (`str`, *optional*, defaults to `"gelu"`):
  69. The non-linear activation function (function or string) in the MLP.
  70. num_attention_heads (`int`, *optional*, defaults to 32):
  71. Number of attention heads for each attention layer in the Transformer decoder.
  72. num_key_value_heads (`int`, *optional*):
  73. This is the number of key_value heads that should be used to implement Grouped Query Attention. If
  74. `num_key_value_heads=None`, the model will use Multi Head Attention (MHA), if
  75. `num_key_value_heads=1 the model will use Multi Query Attention (MQA) otherwise GQA is used. When
  76. converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
  77. by meanpooling all the original heads within that group. For more details, check out [this
  78. paper](https://huggingface.co/papers/2305.13245).
  79. attention_dropout (`float`, *optional*, defaults to 0.0):
  80. The dropout ratio for the attention probabilities.
  81. num_mem_blocks (`int`, *optional*, defaults to 1):
  82. Number of unshared transformer blocks.
  83. use_shared_attention_adapter (`bool`, *optional*, defaults to `False`):
  84. If True, unshared adapters (formally the same as LoRA but used in the base model) will be added to the q, k, v projectors in the shared attention layers.
  85. adapter_rank (`int`, *optional*, defaults to 128):
  86. Rank of the adapter in the shared MLP and shared attention layers.
  87. use_mem_rope (`bool`, *optional*, defaults to `False`):
  88. If True, includes RoPE in the shared attention layers.
  89. rope_theta (`float`, *optional*, defaults to `10000.0`):
  90. The base period of the RoPE embeddings.
  91. initializer_range (`float`, *optional*, defaults to 0.02):
  92. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  93. rms_norm_eps (`float`, *optional*, defaults to 1e-05):
  94. The epsilon used by the rms normalization layers.
  95. use_cache (`bool`, *optional*, defaults to `True`):
  96. Whether or not the model should return the last key/values attentions (not used by all models). Only
  97. relevant if `config.is_decoder=True`.
  98. num_logits_to_keep (`int` or `None`, *optional*, defaults to 1):
  99. Number of prompt logits to calculate during generation. If `None`, all logits will be calculated. If an
  100. integer value, only last `num_logits_to_keep` logits will be calculated. Default is 1 because only the
  101. logits of the last prompt token are needed for generation. For long sequences, the logits for the entire
  102. sequence may use a lot of memory so, setting `num_logits_to_keep=1` will reduce memory footprint
  103. significantly.
  104. pad_token_id (`int`, *optional*, defaults to 0):
  105. The id of the padding token.
  106. bos_token_id (`int`, *optional*, defaults to 1):
  107. The id of the "beginning-of-sequence" token.
  108. eos_token_id (`int`, *optional*, defaults to 2):
  109. The id of the "end-of-sequence" token.
  110. use_long_context (`bool`, *optional*, defaults to `False`):
  111. Activates the context-extended version of Zamba by modifying RoPE.
  112. ```python
  113. >>> from transformers import Zamba2Model, Zamba2Config
  114. >>> # Initializing a Zamba2-2.7B style configuration
  115. >>> configuration = Zamba2Config()
  116. >>> # Initializing a model from the Zamba2-2.7B style configuration
  117. >>> model = Zamba2Model(configuration)
  118. >>> # Accessing the model configuration
  119. >>> configuration = model.config
  120. ```"""
  121. model_type = "zamba2"
  122. attribute_map = {"head_dim": "attention_head_dim"}
  123. keys_to_ignore_at_inference = ["past_key_values"]
  124. def __init__(
  125. self,
  126. vocab_size=32000,
  127. max_position_embeddings=4096,
  128. hidden_size=2560,
  129. num_hidden_layers=54,
  130. layers_block_type=None,
  131. mamba_d_state=64,
  132. mamba_d_conv=4,
  133. mamba_expand=2,
  134. mamba_ngroups=1,
  135. time_step_min=0.001,
  136. time_step_max=0.1,
  137. time_step_floor=1e-4,
  138. time_step_limit=None,
  139. n_mamba_heads=8,
  140. use_conv_bias=True,
  141. chunk_size=256,
  142. use_mem_eff_path=False,
  143. add_bias_linear=False,
  144. intermediate_size=None,
  145. hidden_act="gelu",
  146. num_attention_heads=32,
  147. num_key_value_heads=None,
  148. attention_dropout=0.0,
  149. num_mem_blocks=1,
  150. use_shared_attention_adapter=False,
  151. adapter_rank=128,
  152. use_mem_rope=False,
  153. rope_theta=10000,
  154. initializer_range=0.02,
  155. rms_norm_eps=1e-5,
  156. use_cache=True,
  157. num_logits_to_keep=1,
  158. pad_token_id=0,
  159. bos_token_id=1,
  160. eos_token_id=2,
  161. use_long_context=False,
  162. **kwargs,
  163. ):
  164. super().__init__(
  165. pad_token_id=pad_token_id,
  166. bos_token_id=bos_token_id,
  167. eos_token_id=eos_token_id,
  168. **kwargs,
  169. )
  170. self.vocab_size = vocab_size
  171. self.max_position_embeddings = max_position_embeddings
  172. self.hidden_size = hidden_size
  173. if intermediate_size is None:
  174. self.intermediate_size = 4 * hidden_size
  175. else:
  176. self.intermediate_size = intermediate_size
  177. self.hidden_act = hidden_act
  178. self.num_hidden_layers = num_hidden_layers
  179. self.num_attention_heads = num_attention_heads
  180. self.num_mem_blocks = num_mem_blocks
  181. self.attention_hidden_size = 2 * hidden_size
  182. self.attention_head_dim = 2 * self.hidden_size // self.num_attention_heads
  183. self.attention_dropout = attention_dropout
  184. self.use_mem_rope = use_mem_rope
  185. self.use_long_context = use_long_context
  186. if use_mem_rope and use_long_context:
  187. a = 8
  188. rope_theta = rope_theta * a ** (self.attention_head_dim / (self.attention_head_dim - 2))
  189. self.rope_theta = rope_theta
  190. self.mamba_d_state = mamba_d_state
  191. self.mamba_d_conv = mamba_d_conv
  192. self.mamba_expand = mamba_expand
  193. self.add_bias_linear = add_bias_linear
  194. self.mamba_ngroups = mamba_ngroups
  195. self.n_mamba_heads = n_mamba_heads
  196. self.mamba_headdim = int(mamba_expand * hidden_size) // n_mamba_heads
  197. self.use_conv_bias = use_conv_bias
  198. self.chunk_size = chunk_size
  199. self.time_step_limit = time_step_limit
  200. self.use_shared_attention_adapter = use_shared_attention_adapter
  201. self.adapter_rank = adapter_rank
  202. self.time_step_min = time_step_min
  203. self.time_step_max = time_step_max
  204. self.time_step_floor = time_step_floor
  205. if use_long_context:
  206. self.max_position_embeddings = 16384
  207. if num_key_value_heads is None:
  208. num_key_value_heads = num_attention_heads
  209. self.num_key_value_heads = num_key_value_heads
  210. self.num_attention_heads = num_attention_heads
  211. self.kv_channels = self.hidden_size // self.num_attention_heads
  212. self.num_query_groups = self.num_attention_heads
  213. # Below, "mamba" stands for mamba layer, "hybrid" stands for hybrid layer (composed by a shared transformer followed by mamba layer)
  214. if layers_block_type is None:
  215. self.layers_block_type = (
  216. ["mamba"]
  217. + (["mamba"] * 5 + ["hybrid"]) * 7
  218. + ["mamba"] * 4
  219. + ["hybrid"]
  220. + ["mamba"] * 3
  221. + ["hybrid"]
  222. + ["mamba"] * 2
  223. )
  224. else:
  225. self.layers_block_type = layers_block_type
  226. self.initializer_range = initializer_range
  227. self.rms_norm_eps = rms_norm_eps
  228. self.use_cache = use_cache
  229. self.num_logits_to_keep = num_logits_to_keep
  230. self.hybrid_layer_ids = [index for index, type in enumerate(self.layers_block_type) if type == "hybrid"]
  231. self.use_mem_eff_path = use_mem_eff_path
  232. __all__ = ["Zamba2Config"]