configuration_vaultgemma.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  2. # This file was automatically generated from src/transformers/models/vaultgemma/modular_vaultgemma.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_vaultgemma.py file directly. One of our CI enforces this.
  6. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  7. # coding=utf-8
  8. # Copyright 2025 the HuggingFace Team. All rights reserved.
  9. #
  10. # Licensed under the Apache License, Version 2.0 (the "License");
  11. # you may not use this file except in compliance with the License.
  12. # You may obtain a copy of the License at
  13. #
  14. # http://www.apache.org/licenses/LICENSE-2.0
  15. #
  16. # Unless required by applicable law or agreed to in writing, software
  17. # distributed under the License is distributed on an "AS IS" BASIS,
  18. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. # See the License for the specific language governing permissions and
  20. # limitations under the License.
  21. from ...configuration_utils import PretrainedConfig, layer_type_validation
  22. class VaultGemmaConfig(PretrainedConfig):
  23. r"""
  24. This is the configuration class to store the configuration of a [`VaultGemmaModel`]. It is used to instantiate an VaultGemma
  25. model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
  26. defaults will yield a similar configuration to that of the VaultGemma-7B.
  27. e.g. [google/vaultgemma-7b](https://huggingface.co/google/vaultgemma-7b)
  28. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  29. documentation from [`PretrainedConfig`] for more information.
  30. Args:
  31. vocab_size (`int`, *optional*, defaults to 256000):
  32. Vocabulary size of the VaultGemma model. Defines the number of different tokens that can be represented by the
  33. `inputs_ids` passed when calling [`VaultGemmaModel`]
  34. hidden_size (`int`, *optional*, defaults to 2304):
  35. Dimension of the hidden representations.
  36. intermediate_size (`int`, *optional*, defaults to 9216):
  37. Dimension of the MLP representations.
  38. num_hidden_layers (`int`, *optional*, defaults to 26):
  39. Number of hidden layers in the Transformer decoder.
  40. num_attention_heads (`int`, *optional*, defaults to 8):
  41. Number of attention heads for each attention layer in the Transformer decoder.
  42. num_key_value_heads (`int`, *optional*, defaults to 4):
  43. This is the number of key_value heads that should be used to implement Grouped Query Attention. If
  44. `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
  45. `num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When
  46. converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
  47. by meanpooling all the original heads within that group. For more details, check out [this
  48. paper](https://huggingface.co/papers/2305.13245). If it is not specified, will default to
  49. `num_attention_heads`.
  50. head_dim (`int`, *optional*, defaults to 256):
  51. The attention head dimension.
  52. hidden_activation (`str` or `function`, *optional*, defaults to `"gelu_pytorch_tanh"`):
  53. The non-linear activation function (function or string) in the decoder. Will default to `"gelu_pytorch_tanh"`
  54. if not specified. `"gelu_pytorch_tanh"` uses an approximation of the `"gelu"` activation function.
  55. max_position_embeddings (`int`, *optional*, defaults to 8192):
  56. The maximum sequence length that this model might ever be used with.
  57. initializer_range (`float`, *optional*, defaults to 0.02):
  58. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  59. rms_norm_eps (`float`, *optional*, defaults to 1e-06):
  60. The epsilon used by the rms normalization layers.
  61. use_cache (`bool`, *optional*, defaults to `True`):
  62. Whether or not the model should return the last key/values attentions (not used by all models). Only
  63. relevant if `config.is_decoder=True`.
  64. pad_token_id (`int`, *optional*, defaults to 0):
  65. Padding token id.
  66. eos_token_id (`int`, *optional*, defaults to 1):
  67. End of stream token id.
  68. bos_token_id (`int`, *optional*, defaults to 2):
  69. Beginning of stream token id.
  70. tie_word_embeddings (`bool`, *optional*, defaults to `True`):
  71. Whether to tie weight embeddings
  72. rope_theta (`float`, *optional*, defaults to 10000.0):
  73. The base period of the RoPE embeddings.
  74. attention_bias (`bool`, defaults to `False`, *optional*, defaults to `False`):
  75. Whether to use a bias in the query, key, value and output projection layers during self-attention.
  76. attention_dropout (`float`, *optional*, defaults to 0.0):
  77. The dropout ratio for the attention probabilities.
  78. query_pre_attn_scalar (`float`, *optional*, defaults to 256):
  79. scaling factor used on the attention scores
  80. sliding_window (`int`, *optional*, defaults to 4096):
  81. in VaultGemma, every other layer uses sliding window attention. This is the size of the sliding window.
  82. layer_types (`list`, *optional*):
  83. Attention pattern for each layer.
  84. final_logit_softcapping (`float`, *optional*, defaults to 30.0):
  85. scaling factor when applying tanh softcapping on the logits.
  86. attn_logit_softcapping (`float`, *optional*, defaults to 50.0):
  87. scaling factor when applying tanh softcapping on the attention scores.
  88. ```python
  89. >>> from transformers import VaultGemmaModel, VaultGemmaConfig
  90. >>> # Initializing a VaultGemma vaultgemma-7b style configuration
  91. >>> configuration = VaultGemmaConfig()
  92. >>> # Initializing a model from the vaultgemma-7b style configuration
  93. >>> model = VaultGemmaModel(configuration)
  94. >>> # Accessing the model configuration
  95. >>> configuration = model.config
  96. ```"""
  97. model_type = "vaultgemma"
  98. keys_to_ignore_at_inference = ["past_key_values"]
  99. base_model_tp_plan = {
  100. "layers.*.self_attn.q_proj": "colwise",
  101. "layers.*.self_attn.k_proj": "colwise",
  102. "layers.*.self_attn.v_proj": "colwise",
  103. "layers.*.self_attn.o_proj": "rowwise",
  104. "layers.*.mlp.gate_proj": "colwise",
  105. "layers.*.mlp.up_proj": "colwise",
  106. "layers.*.mlp.down_proj": "rowwise",
  107. }
  108. base_model_pp_plan = {
  109. "embed_tokens": (["input_ids"], ["inputs_embeds"]),
  110. "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
  111. "norm": (["hidden_states"], ["hidden_states"]),
  112. }
  113. def __init__(
  114. self,
  115. vocab_size=256000,
  116. hidden_size=2304,
  117. intermediate_size=9216,
  118. num_hidden_layers=26,
  119. num_attention_heads=8,
  120. num_key_value_heads=4,
  121. head_dim=256,
  122. hidden_activation="gelu_pytorch_tanh",
  123. max_position_embeddings=8192,
  124. initializer_range=0.02,
  125. rms_norm_eps=1e-6,
  126. use_cache=True,
  127. pad_token_id=0,
  128. eos_token_id=1,
  129. bos_token_id=2,
  130. tie_word_embeddings=True,
  131. rope_theta=10000.0,
  132. attention_bias=False,
  133. attention_dropout=0.0,
  134. query_pre_attn_scalar=256,
  135. sliding_window=4096,
  136. layer_types=None,
  137. final_logit_softcapping=30.0,
  138. attn_logit_softcapping=50.0,
  139. **kwargs,
  140. ):
  141. super().__init__(
  142. pad_token_id=pad_token_id,
  143. bos_token_id=bos_token_id,
  144. eos_token_id=eos_token_id,
  145. tie_word_embeddings=tie_word_embeddings,
  146. **kwargs,
  147. )
  148. self.vocab_size = vocab_size
  149. self.max_position_embeddings = max_position_embeddings
  150. self.hidden_size = hidden_size
  151. self.intermediate_size = intermediate_size
  152. self.num_hidden_layers = num_hidden_layers
  153. self.num_attention_heads = num_attention_heads
  154. self.head_dim = head_dim
  155. self.num_key_value_heads = num_key_value_heads
  156. self.initializer_range = initializer_range
  157. self.rms_norm_eps = rms_norm_eps
  158. self.use_cache = use_cache
  159. self.rope_theta = rope_theta
  160. self.attention_bias = attention_bias
  161. self.attention_dropout = attention_dropout
  162. self.hidden_activation = hidden_activation
  163. self.query_pre_attn_scalar = query_pre_attn_scalar
  164. self.sliding_window = sliding_window
  165. self.final_logit_softcapping = final_logit_softcapping
  166. self.attn_logit_softcapping = attn_logit_softcapping
  167. self.layer_types = layer_types
  168. if self.layer_types is None:
  169. self.layer_types = [
  170. "sliding_attention" if bool((i + 1) % 2) else "full_attention" for i in range(self.num_hidden_layers)
  171. ]
  172. layer_type_validation(self.layer_types, self.num_hidden_layers)
  173. __all__ = ["VaultGemmaConfig"]