configuration_gemma.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  2. # This file was automatically generated from src/transformers/models/gemma/modular_gemma.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_gemma.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
  23. class GemmaConfig(PretrainedConfig):
  24. r"""
  25. This is the configuration class to store the configuration of a [`GemmaModel`]. It is used to instantiate an Gemma
  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 Gemma-7B.
  28. e.g. [google/gemma-7b](https://huggingface.co/google/gemma-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 Gemma model. Defines the number of different tokens that can be represented by the
  34. `inputs_ids` passed when calling [`GemmaModel`]
  35. hidden_size (`int`, *optional*, defaults to 3072):
  36. Dimension of the hidden representations.
  37. intermediate_size (`int`, *optional*, defaults to 24576):
  38. Dimension of the MLP representations.
  39. num_hidden_layers (`int`, *optional*, defaults to 28):
  40. Number of hidden layers in the Transformer decoder.
  41. num_attention_heads (`int`, *optional*, defaults to 16):
  42. Number of attention heads for each attention layer in the Transformer decoder.
  43. num_key_value_heads (`int`, *optional*, defaults to 16):
  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_act (`str` or `function`, *optional*, defaults to `"gelu_pytorch_tanh"`):
  54. The legacy activation function. It is overwritten by the `hidden_activation`.
  55. hidden_activation (`str` or `function`, *optional*):
  56. The non-linear activation function (function or string) in the decoder. Will default to `"gelu_pytorch_tanh"`
  57. if not specified. `"gelu_pytorch_tanh"` uses an approximation of the `"gelu"` activation function.
  58. max_position_embeddings (`int`, *optional*, defaults to 8192):
  59. The maximum sequence length that this model might ever be used with.
  60. initializer_range (`float`, *optional*, defaults to 0.02):
  61. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  62. rms_norm_eps (`float`, *optional*, defaults to 1e-06):
  63. The epsilon used by the rms normalization layers.
  64. use_cache (`bool`, *optional*, defaults to `True`):
  65. Whether or not the model should return the last key/values attentions (not used by all models). Only
  66. relevant if `config.is_decoder=True`.
  67. pad_token_id (`int`, *optional*, defaults to 0):
  68. Padding token id.
  69. eos_token_id (`int`, *optional*, defaults to 1):
  70. End of stream token id.
  71. bos_token_id (`int`, *optional*, defaults to 2):
  72. Beginning of stream token id.
  73. tie_word_embeddings (`bool`, *optional*, defaults to `True`):
  74. Whether to tie weight embeddings
  75. rope_theta (`float`, *optional*, defaults to 10000.0):
  76. The base period of the RoPE embeddings.
  77. attention_bias (`bool`, defaults to `False`, *optional*, defaults to `False`):
  78. Whether to use a bias in the query, key, value and output projection layers during self-attention.
  79. attention_dropout (`float`, *optional*, defaults to 0.0):
  80. The dropout ratio for the attention probabilities.
  81. ```python
  82. >>> from transformers import GemmaModel, GemmaConfig
  83. >>> # Initializing a Gemma gemma-7b style configuration
  84. >>> configuration = GemmaConfig()
  85. >>> # Initializing a model from the gemma-7b style configuration
  86. >>> model = GemmaModel(configuration)
  87. >>> # Accessing the model configuration
  88. >>> configuration = model.config
  89. ```"""
  90. model_type = "gemma"
  91. keys_to_ignore_at_inference = ["past_key_values"]
  92. base_model_tp_plan = {
  93. "layers.*.self_attn.q_proj": "colwise",
  94. "layers.*.self_attn.k_proj": "colwise",
  95. "layers.*.self_attn.v_proj": "colwise",
  96. "layers.*.self_attn.o_proj": "rowwise",
  97. "layers.*.mlp.gate_proj": "colwise",
  98. "layers.*.mlp.up_proj": "colwise",
  99. "layers.*.mlp.down_proj": "rowwise",
  100. }
  101. base_model_pp_plan = {
  102. "embed_tokens": (["input_ids"], ["inputs_embeds"]),
  103. "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
  104. "norm": (["hidden_states"], ["hidden_states"]),
  105. }
  106. def __init__(
  107. self,
  108. vocab_size=256000,
  109. hidden_size=3072,
  110. intermediate_size=24576,
  111. num_hidden_layers=28,
  112. num_attention_heads=16,
  113. num_key_value_heads=16,
  114. head_dim=256,
  115. hidden_act="gelu_pytorch_tanh",
  116. hidden_activation=None,
  117. max_position_embeddings=8192,
  118. initializer_range=0.02,
  119. rms_norm_eps=1e-6,
  120. use_cache=True,
  121. pad_token_id=0,
  122. eos_token_id=1,
  123. bos_token_id=2,
  124. tie_word_embeddings=True,
  125. rope_theta=10000.0,
  126. attention_bias=False,
  127. attention_dropout=0.0,
  128. **kwargs,
  129. ):
  130. self.vocab_size = vocab_size
  131. self.max_position_embeddings = max_position_embeddings
  132. self.hidden_size = hidden_size
  133. self.intermediate_size = intermediate_size
  134. self.num_hidden_layers = num_hidden_layers
  135. self.num_attention_heads = num_attention_heads
  136. self.head_dim = head_dim
  137. self.num_key_value_heads = num_key_value_heads
  138. self.hidden_act = hidden_act
  139. self.hidden_activation = hidden_activation
  140. self.initializer_range = initializer_range
  141. self.rms_norm_eps = rms_norm_eps
  142. self.use_cache = use_cache
  143. self.rope_theta = rope_theta
  144. self.attention_bias = attention_bias
  145. self.attention_dropout = attention_dropout
  146. super().__init__(
  147. pad_token_id=pad_token_id,
  148. bos_token_id=bos_token_id,
  149. eos_token_id=eos_token_id,
  150. tie_word_embeddings=tie_word_embeddings,
  151. **kwargs,
  152. )
  153. __all__ = ["GemmaConfig"]