configuration_gemma2.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. # Copyright 2024 Google Inc. HuggingFace Inc. team. All rights reserved.
  8. #
  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 huggingface_hub.dataclasses import strict
  22. from ...configuration_utils import PreTrainedConfig
  23. from ...modeling_rope_utils import RopeParameters
  24. from ...utils import auto_docstring
  25. @auto_docstring(checkpoint="google/gemma2-7b")
  26. @strict
  27. class Gemma2Config(PreTrainedConfig):
  28. r"""
  29. query_pre_attn_scalar (`float`, *optional*, defaults to 256):
  30. scaling factor used on the attention scores
  31. final_logit_softcapping (`float`, *optional*, defaults to 30.0):
  32. scaling factor when applying tanh softcapping on the logits.
  33. attn_logit_softcapping (`float`, *optional*, defaults to 50.0):
  34. scaling factor when applying tanh softcapping on the attention scores.
  35. use_bidirectional_attention (`bool`, *optional*):
  36. If True, the model will attend to all text tokens instead of using a causal mask.
  37. ```python
  38. >>> from transformers import Gemma2Model, Gemma2Config
  39. >>> # Initializing a Gemma2 gemma2-7b style configuration
  40. >>> configuration = Gemma2Config()
  41. >>> # Initializing a model from the gemma2-7b style configuration
  42. >>> model = Gemma2Model(configuration)
  43. >>> # Accessing the model configuration
  44. >>> configuration = model.config
  45. ```"""
  46. model_type = "gemma2"
  47. keys_to_ignore_at_inference = ["past_key_values"]
  48. base_model_tp_plan = {
  49. "layers.*.self_attn.q_proj": "colwise",
  50. "layers.*.self_attn.k_proj": "colwise",
  51. "layers.*.self_attn.v_proj": "colwise",
  52. "layers.*.self_attn.o_proj": "rowwise",
  53. "layers.*.mlp.gate_proj": "colwise",
  54. "layers.*.mlp.up_proj": "colwise",
  55. "layers.*.mlp.down_proj": "rowwise",
  56. }
  57. base_model_pp_plan = {
  58. "embed_tokens": (["input_ids"], ["inputs_embeds"]),
  59. "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
  60. "norm": (["hidden_states"], ["hidden_states"]),
  61. }
  62. vocab_size: int = 256000
  63. hidden_size: int = 2304
  64. intermediate_size: int = 9216
  65. num_hidden_layers: int = 26
  66. num_attention_heads: int = 8
  67. num_key_value_heads: int = 4
  68. head_dim: int = 256
  69. hidden_activation: str = "gelu_pytorch_tanh"
  70. max_position_embeddings: int = 8192
  71. initializer_range: float = 0.02
  72. rms_norm_eps: float = 1e-6
  73. use_cache: bool = True
  74. pad_token_id: int | None = 0
  75. eos_token_id: int | list[int] | None = 1
  76. bos_token_id: int | None = 2
  77. tie_word_embeddings: bool = True
  78. rope_parameters: RopeParameters | dict | None = None
  79. attention_bias: bool = False
  80. attention_dropout: int | float | None = 0.0
  81. query_pre_attn_scalar: int = 256
  82. sliding_window: int | None = 4096
  83. layer_types: list[str] | None = None
  84. final_logit_softcapping: float | None = 30.0
  85. attn_logit_softcapping: float | None = 50.0
  86. use_bidirectional_attention: bool | None = None
  87. def __post_init__(self, **kwargs):
  88. if self.layer_types is None:
  89. self.layer_types = [
  90. "sliding_attention" if bool((i + 1) % 2) else "full_attention" for i in range(self.num_hidden_layers)
  91. ]
  92. super().__post_init__(**kwargs)
  93. def validate_architecture(self):
  94. """Part of `@strict`-powered validation. Validates the architecture of the config."""
  95. if self.hidden_size % self.num_attention_heads != 0:
  96. raise ValueError(
  97. f"The hidden size ({self.hidden_size}) is not a multiple of the number of attention "
  98. f"heads ({self.num_attention_heads})."
  99. )
  100. __all__ = ["Gemma2Config"]