configuration_gemma2.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  2. # This file was automatically generated from src/transformers/models/gemma2/modular_gemma2.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_gemma2.py file directly. One of our CI enforces this.
  6. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  7. # coding=utf-8
  8. # Copyright 2024 Google Inc. 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, layer_type_validation
  23. class Gemma2Config(PretrainedConfig):
  24. r"""
  25. This is the configuration class to store the configuration of a [`Gemma2Model`]. It is used to instantiate an Gemma2
  26. model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
  27. defaults will yield a similar configuration to that of the Gemma2-7B.
  28. e.g. [google/gemma2-7b](https://huggingface.co/google/gemma2-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 256000):
  33. Vocabulary size of the Gemma2 model. Defines the number of different tokens that can be represented by the
  34. `inputs_ids` passed when calling [`Gemma2Model`]
  35. hidden_size (`int`, *optional*, defaults to 2304):
  36. Dimension of the hidden representations.
  37. intermediate_size (`int`, *optional*, defaults to 9216):
  38. Dimension of the MLP representations.
  39. num_hidden_layers (`int`, *optional*, defaults to 26):
  40. Number of hidden layers in the Transformer decoder.
  41. num_attention_heads (`int`, *optional*, defaults to 8):
  42. Number of attention heads for each attention layer in the Transformer decoder.
  43. num_key_value_heads (`int`, *optional*, defaults to 4):
  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
  50. `num_attention_heads`.
  51. head_dim (`int`, *optional*, defaults to 256):
  52. The attention head dimension.
  53. hidden_activation (`str` or `function`, *optional*, defaults to `"gelu_pytorch_tanh"`):
  54. The non-linear activation function (function or string) in the decoder. Will default to `"gelu_pytorch_tanh"`
  55. if not specified. `"gelu_pytorch_tanh"` uses an approximation of the `"gelu"` activation function.
  56. max_position_embeddings (`int`, *optional*, defaults to 8192):
  57. The maximum sequence length that this model might ever be used with.
  58. initializer_range (`float`, *optional*, defaults to 0.02):
  59. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  60. rms_norm_eps (`float`, *optional*, defaults to 1e-06):
  61. The epsilon used by the rms normalization layers.
  62. use_cache (`bool`, *optional*, defaults to `True`):
  63. Whether or not the model should return the last key/values attentions (not used by all models). Only
  64. relevant if `config.is_decoder=True`.
  65. pad_token_id (`int`, *optional*, defaults to 0):
  66. Padding token id.
  67. eos_token_id (`int`, *optional*, defaults to 1):
  68. End of stream token id.
  69. bos_token_id (`int`, *optional*, defaults to 2):
  70. Beginning of stream token id.
  71. tie_word_embeddings (`bool`, *optional*, defaults to `True`):
  72. Whether to tie weight embeddings
  73. rope_theta (`float`, *optional*, defaults to 10000.0):
  74. The base period of the RoPE embeddings.
  75. attention_bias (`bool`, defaults to `False`, *optional*, defaults to `False`):
  76. Whether to use a bias in the query, key, value and output projection layers during self-attention.
  77. attention_dropout (`float`, *optional*, defaults to 0.0):
  78. The dropout ratio for the attention probabilities.
  79. query_pre_attn_scalar (`float`, *optional*, defaults to 256):
  80. scaling factor used on the attention scores
  81. sliding_window (`int`, *optional*, defaults to 4096):
  82. in Gemma2, every other layer uses sliding window attention. This is the size of the sliding window.
  83. layer_types (`list`, *optional*):
  84. Attention pattern for each layer.
  85. final_logit_softcapping (`float`, *optional*, defaults to 30.0):
  86. scaling factor when applying tanh softcapping on the logits.
  87. attn_logit_softcapping (`float`, *optional*, defaults to 50.0):
  88. scaling factor when applying tanh softcapping on the attention scores.
  89. ```python
  90. >>> from transformers import Gemma2Model, Gemma2Config
  91. >>> # Initializing a Gemma2 gemma2-7b style configuration
  92. >>> configuration = Gemma2Config()
  93. >>> # Initializing a model from the gemma2-7b style configuration
  94. >>> model = Gemma2Model(configuration)
  95. >>> # Accessing the model configuration
  96. >>> configuration = model.config
  97. ```"""
  98. model_type = "gemma2"
  99. keys_to_ignore_at_inference = ["past_key_values"]
  100. base_model_tp_plan = {
  101. "layers.*.self_attn.q_proj": "colwise",
  102. "layers.*.self_attn.k_proj": "colwise",
  103. "layers.*.self_attn.v_proj": "colwise",
  104. "layers.*.self_attn.o_proj": "rowwise",
  105. "layers.*.mlp.gate_proj": "colwise",
  106. "layers.*.mlp.up_proj": "colwise",
  107. "layers.*.mlp.down_proj": "rowwise",
  108. }
  109. base_model_pp_plan = {
  110. "embed_tokens": (["input_ids"], ["inputs_embeds"]),
  111. "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
  112. "norm": (["hidden_states"], ["hidden_states"]),
  113. }
  114. def __init__(
  115. self,
  116. vocab_size=256000,
  117. hidden_size=2304,
  118. intermediate_size=9216,
  119. num_hidden_layers=26,
  120. num_attention_heads=8,
  121. num_key_value_heads=4,
  122. head_dim=256,
  123. hidden_activation="gelu_pytorch_tanh",
  124. max_position_embeddings=8192,
  125. initializer_range=0.02,
  126. rms_norm_eps=1e-6,
  127. use_cache=True,
  128. pad_token_id=0,
  129. eos_token_id=1,
  130. bos_token_id=2,
  131. tie_word_embeddings=True,
  132. rope_theta=10000.0,
  133. attention_bias=False,
  134. attention_dropout=0.0,
  135. query_pre_attn_scalar=256,
  136. sliding_window=4096,
  137. layer_types=None,
  138. final_logit_softcapping=30.0,
  139. attn_logit_softcapping=50.0,
  140. **kwargs,
  141. ):
  142. super().__init__(
  143. pad_token_id=pad_token_id,
  144. bos_token_id=bos_token_id,
  145. eos_token_id=eos_token_id,
  146. tie_word_embeddings=tie_word_embeddings,
  147. **kwargs,
  148. )
  149. self.vocab_size = vocab_size
  150. self.max_position_embeddings = max_position_embeddings
  151. self.hidden_size = hidden_size
  152. self.intermediate_size = intermediate_size
  153. self.num_hidden_layers = num_hidden_layers
  154. self.num_attention_heads = num_attention_heads
  155. self.head_dim = head_dim
  156. self.num_key_value_heads = num_key_value_heads
  157. self.initializer_range = initializer_range
  158. self.rms_norm_eps = rms_norm_eps
  159. self.use_cache = use_cache
  160. self.rope_theta = rope_theta
  161. self.attention_bias = attention_bias
  162. self.attention_dropout = attention_dropout
  163. self.hidden_activation = hidden_activation
  164. self.query_pre_attn_scalar = query_pre_attn_scalar
  165. self.sliding_window = sliding_window
  166. self.final_logit_softcapping = final_logit_softcapping
  167. self.attn_logit_softcapping = attn_logit_softcapping
  168. self.layer_types = layer_types
  169. if self.layer_types is None:
  170. self.layer_types = [
  171. "sliding_attention" if bool((i + 1) % 2) else "full_attention" for i in range(self.num_hidden_layers)
  172. ]
  173. layer_type_validation(self.layer_types, self.num_hidden_layers)
  174. __all__ = ["Gemma2Config"]