|
| 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 | + |
| 22 | +from ...configuration_utils import PretrainedConfig, layer_type_validation |
| 23 | + |
| 24 | + |
| 25 | +class VaultGemmaConfig(PretrainedConfig): |
| 26 | + r""" |
| 27 | + This is the configuration class to store the configuration of a [`VaultGemmaModel`]. It is used to instantiate an VaultGemma |
| 28 | + model according to the specified arguments, defining the model architecture. Instantiating a configuration with the |
| 29 | + defaults will yield a similar configuration to that of the VaultGemma-7B. |
| 30 | + e.g. [google/vaultgemma-7b](https://huggingface.co/google/vaultgemma-7b) |
| 31 | + Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the |
| 32 | + documentation from [`PretrainedConfig`] for more information. |
| 33 | + Args: |
| 34 | + vocab_size (`int`, *optional*, defaults to 256000): |
| 35 | + Vocabulary size of the VaultGemma model. Defines the number of different tokens that can be represented by the |
| 36 | + `inputs_ids` passed when calling [`VaultGemmaModel`] |
| 37 | + hidden_size (`int`, *optional*, defaults to 2304): |
| 38 | + Dimension of the hidden representations. |
| 39 | + intermediate_size (`int`, *optional*, defaults to 9216): |
| 40 | + Dimension of the MLP representations. |
| 41 | + num_hidden_layers (`int`, *optional*, defaults to 26): |
| 42 | + Number of hidden layers in the Transformer decoder. |
| 43 | + num_attention_heads (`int`, *optional*, defaults to 8): |
| 44 | + Number of attention heads for each attention layer in the Transformer decoder. |
| 45 | + num_key_value_heads (`int`, *optional*, defaults to 4): |
| 46 | + This is the number of key_value heads that should be used to implement Grouped Query Attention. If |
| 47 | + `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if |
| 48 | + `num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When |
| 49 | + converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed |
| 50 | + by meanpooling all the original heads within that group. For more details, check out [this |
| 51 | + paper](https://huggingface.co/papers/2305.13245). If it is not specified, will default to |
| 52 | + `num_attention_heads`. |
| 53 | + head_dim (`int`, *optional*, defaults to 256): |
| 54 | + The attention head dimension. |
| 55 | + hidden_activation (`str` or `function`, *optional*, defaults to `"gelu_pytorch_tanh"`): |
| 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 | + query_pre_attn_scalar (`float`, *optional*, defaults to 256): |
| 82 | + scaling factor used on the attention scores |
| 83 | + sliding_window (`int`, *optional*, defaults to 4096): |
| 84 | + in VaultGemma, every other layer uses sliding window attention. This is the size of the sliding window. |
| 85 | + layer_types (`list`, *optional*): |
| 86 | + Attention pattern for each layer. |
| 87 | + final_logit_softcapping (`float`, *optional*, defaults to 30.0): |
| 88 | + scaling factor when applying tanh softcapping on the logits. |
| 89 | + attn_logit_softcapping (`float`, *optional*, defaults to 50.0): |
| 90 | + scaling factor when applying tanh softcapping on the attention scores. |
| 91 | +
|
| 92 | + ```python |
| 93 | + >>> from transformers import VaultGemmaModel, VaultGemmaConfig |
| 94 | + >>> # Initializing a VaultGemma vaultgemma-7b style configuration |
| 95 | + >>> configuration = VaultGemmaConfig() |
| 96 | + >>> # Initializing a model from the vaultgemma-7b style configuration |
| 97 | + >>> model = VaultGemmaModel(configuration) |
| 98 | + >>> # Accessing the model configuration |
| 99 | + >>> configuration = model.config |
| 100 | + ```""" |
| 101 | + |
| 102 | + model_type = "vaultgemma" |
| 103 | + keys_to_ignore_at_inference = ["past_key_values"] |
| 104 | + base_model_tp_plan = { |
| 105 | + "layers.*.self_attn.q_proj": "colwise", |
| 106 | + "layers.*.self_attn.k_proj": "colwise", |
| 107 | + "layers.*.self_attn.v_proj": "colwise", |
| 108 | + "layers.*.self_attn.o_proj": "rowwise", |
| 109 | + "layers.*.mlp.gate_proj": "colwise", |
| 110 | + "layers.*.mlp.up_proj": "colwise", |
| 111 | + "layers.*.mlp.down_proj": "rowwise", |
| 112 | + } |
| 113 | + base_model_pp_plan = { |
| 114 | + "embed_tokens": (["input_ids"], ["inputs_embeds"]), |
| 115 | + "layers": (["hidden_states", "attention_mask"], ["hidden_states"]), |
| 116 | + "norm": (["hidden_states"], ["hidden_states"]), |
| 117 | + } |
| 118 | + |
| 119 | + def __init__( |
| 120 | + self, |
| 121 | + vocab_size=256000, |
| 122 | + hidden_size=2304, |
| 123 | + intermediate_size=9216, |
| 124 | + num_hidden_layers=26, |
| 125 | + num_attention_heads=8, |
| 126 | + num_key_value_heads=4, |
| 127 | + head_dim=256, |
| 128 | + hidden_activation="gelu_pytorch_tanh", |
| 129 | + max_position_embeddings=8192, |
| 130 | + initializer_range=0.02, |
| 131 | + rms_norm_eps=1e-6, |
| 132 | + use_cache=True, |
| 133 | + pad_token_id=0, |
| 134 | + eos_token_id=1, |
| 135 | + bos_token_id=2, |
| 136 | + tie_word_embeddings=True, |
| 137 | + rope_theta=10000.0, |
| 138 | + attention_bias=False, |
| 139 | + attention_dropout=0.0, |
| 140 | + query_pre_attn_scalar=256, |
| 141 | + sliding_window=4096, |
| 142 | + layer_types=None, |
| 143 | + final_logit_softcapping=30.0, |
| 144 | + attn_logit_softcapping=50.0, |
| 145 | + **kwargs, |
| 146 | + ): |
| 147 | + super().__init__( |
| 148 | + pad_token_id=pad_token_id, |
| 149 | + bos_token_id=bos_token_id, |
| 150 | + eos_token_id=eos_token_id, |
| 151 | + tie_word_embeddings=tie_word_embeddings, |
| 152 | + **kwargs, |
| 153 | + ) |
| 154 | + self.vocab_size = vocab_size |
| 155 | + self.max_position_embeddings = max_position_embeddings |
| 156 | + self.hidden_size = hidden_size |
| 157 | + self.intermediate_size = intermediate_size |
| 158 | + self.num_hidden_layers = num_hidden_layers |
| 159 | + self.num_attention_heads = num_attention_heads |
| 160 | + self.head_dim = head_dim |
| 161 | + self.num_key_value_heads = num_key_value_heads |
| 162 | + self.initializer_range = initializer_range |
| 163 | + self.rms_norm_eps = rms_norm_eps |
| 164 | + self.use_cache = use_cache |
| 165 | + self.rope_theta = rope_theta |
| 166 | + self.attention_bias = attention_bias |
| 167 | + self.attention_dropout = attention_dropout |
| 168 | + self.hidden_activation = hidden_activation |
| 169 | + self.query_pre_attn_scalar = query_pre_attn_scalar |
| 170 | + self.sliding_window = sliding_window |
| 171 | + self.final_logit_softcapping = final_logit_softcapping |
| 172 | + self.attn_logit_softcapping = attn_logit_softcapping |
| 173 | + self.layer_types = layer_types |
| 174 | + |
| 175 | + if self.layer_types is None: |
| 176 | + self.layer_types = [ |
| 177 | + "sliding_attention" if bool((i + 1) % 2) else "full_attention" for i in range(self.num_hidden_layers) |
| 178 | + ] |
| 179 | + layer_type_validation(self.layer_types) |
| 180 | + |
| 181 | + |
| 182 | +__all__ = ["VaultGemmaConfig"] |
0 commit comments