configuration_gemma4.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. # Copyright 2026 the HuggingFace Team. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Any, Literal
  15. from huggingface_hub.dataclasses import strict
  16. from ...configuration_utils import PreTrainedConfig
  17. from ...utils import auto_docstring, logging
  18. from ...utils.type_validators import interval
  19. logger = logging.get_logger(__name__)
  20. @auto_docstring(checkpoint="google/gemma-4-e2b-it")
  21. @strict
  22. class Gemma4AudioConfig(PreTrainedConfig):
  23. r"""
  24. subsampling_conv_channels (`list[int]`, defaults to `[128, 32]`):
  25. Channel sizes for the convolutional layers in the Sub-sample Convolution Projection.
  26. residual_weight (`float`, defaults to `0.5`):
  27. Scaling applied to hidden_states prior to combining with the residual in the feedforward.
  28. attention_chunk_size (`int`, defaults to `12`):
  29. The sub-sequence size for attention processing.
  30. attention_context_left (`int`, defaults to `13`):
  31. The leftward context size for the attention chunk.
  32. attention_context_right (`int`, defaults to `0`):
  33. The rightward context size for the attention chunk.
  34. attention_logit_cap (`float`, defaults to `50.0`):
  35. Cap applied to attention weights.
  36. attention_invalid_logits_value (`float`, defaults to `1e-9`):
  37. Value to use for invalid logits in attention.
  38. use_clipped_linears (`bool`, defaults to `True`):
  39. If true, apply clipping to the Linear layers, drawing bounds from the model checkpoint.
  40. gradient_clipping (`float`, defaults to `1e10`):
  41. Clipping value used to stabilize extremely large gradient values.
  42. output_proj_dims (`int`, defaults to `1536`):
  43. Dimension of the final linear projection from `hidden_size` to the model's output.
  44. """
  45. model_type = "gemma4_audio"
  46. hidden_size: int = 1024
  47. num_hidden_layers: int = 12
  48. num_attention_heads: int = 8
  49. hidden_act: str = "silu"
  50. # subsampling parameters
  51. subsampling_conv_channels: list[int] | tuple[int, int] = (128, 32)
  52. # conformer parameters
  53. conv_kernel_size: int = 5
  54. residual_weight: float = 0.5
  55. attention_chunk_size: int = 12
  56. attention_context_left: int = 13
  57. attention_context_right: int = 0
  58. attention_logit_cap: float = 50.0
  59. attention_invalid_logits_value: float = -1.0e9
  60. use_clipped_linears: bool = True
  61. rms_norm_eps: float = 1e-6
  62. gradient_clipping: float = 1e10
  63. output_proj_dims: int = 1536
  64. initializer_range: float = interval(min=0.0, max=1.0)(default=0.02)
  65. def __post_init__(self, **kwargs):
  66. # JSON serialization converts tuples to lists, convert back
  67. if isinstance(self.subsampling_conv_channels, tuple):
  68. self.subsampling_conv_channels = list(self.subsampling_conv_channels)
  69. super().__post_init__(**kwargs)
  70. @auto_docstring(checkpoint="google/gemma-4-e2b-it")
  71. @strict
  72. class Gemma4TextConfig(PreTrainedConfig):
  73. r"""
  74. use_bidirectional_attention (`str`, *optional*):
  75. Controls bidirectional attention behavior. When set to `"vision"`, vision tokens
  76. attend bidirectionally while text tokens use causal attention. When set to `"all"`,
  77. all tokens use bidirectional attention.
  78. vocab_size_per_layer_input (`int`, defaults to 262144):
  79. Vocabulary size for the per-layer input embeddings. Used by models with per-layer
  80. residual streams where a smaller embedding is added at each decoder layer.
  81. hidden_size_per_layer_input (`int`, defaults to 256):
  82. Hidden dimension for the per-layer input embeddings. Controls the width of the
  83. per-layer residual embedding vectors.
  84. num_global_key_value_heads (`int`, *optional*):
  85. Number of key-value heads for global (full) attention layers. If `None`, defaults
  86. to `num_key_value_heads`.
  87. global_head_dim (`int`, defaults to 512):
  88. Dimension of each attention head in global (full) attention layers.
  89. attention_k_eq_v (`bool`, defaults to `False`):
  90. Whether keys and values share the same projection weights. When `True`, the key
  91. projection output is reused as the value projection.
  92. num_kv_shared_layers (`int`, defaults to 0):
  93. Number of consecutive decoder layers that share the same key-value projections.
  94. A value of 0 means no sharing (each layer has independent KV projections).
  95. enable_moe_block (`bool`, defaults to `False`):
  96. Whether to enable Mixture-of-Experts (MoE) blocks in the decoder layers. When
  97. `True`, eligible layers will use a sparse MoE feed-forward network.
  98. use_double_wide_mlp (`bool`, defaults to `False`):
  99. Whether to use a double-width MLP with fused gate and up projections.
  100. top_k_experts (`int`, *optional*):
  101. Number of experts activated per token in MoE layers. Only used when
  102. `enable_moe_block=True`.
  103. moe_intermediate_size (`int`, *optional*):
  104. Intermediate (hidden) size of each expert's feed-forward network in MoE layers.
  105. Only used when `enable_moe_block=True`.
  106. """
  107. model_type = "gemma4_text"
  108. keys_to_ignore_at_inference = ["past_key_values"]
  109. base_model_tp_plan = {
  110. "layers.*.self_attn.q_proj": "colwise",
  111. "layers.*.self_attn.k_proj": "colwise",
  112. "layers.*.self_attn.v_proj": "colwise",
  113. "layers.*.self_attn.q_norm": "replicated_with_grad_allreduce",
  114. "layers.*.self_attn.k_norm": "replicated_with_grad_allreduce",
  115. "layers.*.self_attn.o_proj": "rowwise",
  116. "layers.*.mlp.gate_proj": "colwise",
  117. "layers.*.mlp.up_proj": "colwise",
  118. "layers.*.mlp.down_proj": "rowwise",
  119. "layers.*.experts.gate_up_proj": "packed_colwise",
  120. "layers.*.experts.down_proj": "rowwise",
  121. "layers.*.experts": "moe_tp_experts",
  122. }
  123. base_model_pp_plan = {
  124. "embed_tokens": (["input_ids"], ["inputs_embeds"]),
  125. "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
  126. "norm": (["hidden_states"], ["hidden_states"]),
  127. }
  128. vocab_size: int = 262_144
  129. hidden_size: int = 2304
  130. intermediate_size: int = 9216
  131. num_hidden_layers: int = 30
  132. num_attention_heads: int = 8
  133. num_key_value_heads: int = 4
  134. head_dim: int = 256
  135. hidden_activation: str = "gelu_pytorch_tanh"
  136. max_position_embeddings: int = 131_072
  137. initializer_range: float = 0.02
  138. rms_norm_eps: float = 1e-6
  139. use_cache: bool = True
  140. pad_token_id: int | None = 0
  141. eos_token_id: int | list[int] | None = 1
  142. bos_token_id: int | None = 2
  143. tie_word_embeddings: bool = True
  144. rope_parameters: dict | None = None
  145. attention_bias: bool = False
  146. attention_dropout: int | float | None = 0.0
  147. sliding_window: int = 512
  148. layer_types: list[str] | None = None
  149. final_logit_softcapping: float | None = None
  150. use_bidirectional_attention: Literal["all", "vision"] | None = None
  151. vocab_size_per_layer_input: int = 262_144
  152. hidden_size_per_layer_input: int = 256
  153. num_global_key_value_heads: int | None = None
  154. global_head_dim: int = 512
  155. attention_k_eq_v: bool = False
  156. num_kv_shared_layers: int = 0
  157. enable_moe_block: bool = False
  158. use_double_wide_mlp: bool = False
  159. num_experts: int | None = None
  160. top_k_experts: int | None = None
  161. moe_intermediate_size: int | None = None
  162. def __post_init__(self, **kwargs):
  163. if self.use_bidirectional_attention == "all":
  164. self.sliding_window = (self.sliding_window // 2) + 1 # due to fa we set exclusive bounds
  165. if self.layer_types is None:
  166. sliding_window_pattern = 6 # by default 5:1
  167. self.layer_types = [
  168. "sliding_attention" if bool((i + 1) % sliding_window_pattern) else "full_attention"
  169. for i in range(self.num_hidden_layers)
  170. ]
  171. if self.layer_types and (last_layer_type := self.layer_types[-1]) != "full_attention":
  172. logger.warning(
  173. f"Last layer must use `full_attention`, but got `{last_layer_type}`. Forcing last layer to `full_attention`."
  174. )
  175. self.layer_types[-1] = "full_attention"
  176. default_rope_params: dict[Literal["full_attention", "sliding_attention"] : dict[str, Any]] = {
  177. "sliding_attention": {"rope_type": "default", "rope_theta": 10_000.0},
  178. "full_attention": {"rope_type": "proportional", "partial_rotary_factor": 0.25, "rope_theta": 1_000_000.0},
  179. }
  180. if self.rope_parameters is None:
  181. self.rope_parameters = default_rope_params
  182. super().__post_init__(**kwargs)
  183. def convert_rope_params_to_dict(self, **kwargs):
  184. # No need to handle BC for new models, because they have no old-format `rope_scaling`
  185. return kwargs
  186. @auto_docstring(checkpoint="google/gemma-4-e2b-it")
  187. @strict
  188. class Gemma4VisionConfig(PreTrainedConfig):
  189. r"""
  190. pooling_kernel_size (`int`, *optional*):
  191. Spatial pooling kernel size applied after patchification.
  192. position_embedding_size (`int`, defaults to 10240):
  193. Maximum number of position embeddings for the vision encoder. Controls the size of
  194. the learned 2D position embedding table used by the patch embedder.
  195. use_clipped_linears (`bool`, defaults to `False`):
  196. Whether to use weight-clipped linear layers. When enabled, linear layer weights are
  197. clamped to a fixed range during the forward pass to improve numerical stability.
  198. standardize (`bool`, defaults to `False`):
  199. If true, applies a bias and scale to the soft tokens returned from the pooler.
  200. """
  201. model_type = "gemma4_vision"
  202. base_model_tp_plan = {
  203. "encoder.layers.*.self_attn.q_proj": "colwise",
  204. "encoder.layers.*.self_attn.k_proj": "colwise",
  205. "encoder.layers.*.self_attn.v_proj": "colwise",
  206. "encoder.layers.*.self_attn.q_norm": "replicated_with_grad_allreduce",
  207. "encoder.layers.*.self_attn.k_norm": "replicated_with_grad_allreduce",
  208. "encoder.layers.*.self_attn.o_proj": "rowwise",
  209. "encoder.layers.*.mlp.gate_proj": "colwise",
  210. "encoder.layers.*.mlp.up_proj": "colwise",
  211. "encoder.layers.*.mlp.down_proj": "rowwise",
  212. }
  213. default_theta = 100.0
  214. hidden_size: int = 768
  215. intermediate_size: int = 3072
  216. num_hidden_layers: int = 16
  217. num_attention_heads: int = 12
  218. num_key_value_heads: int = 12
  219. head_dim: int = 64
  220. hidden_activation: str = "gelu_pytorch_tanh"
  221. rms_norm_eps: float = 1e-6
  222. max_position_embeddings: int = 131_072
  223. attention_bias: bool | None = False
  224. attention_dropout: float | None = 0.0
  225. rope_parameters: dict | None = None
  226. pooling_kernel_size: int = 3
  227. patch_size: int = 16
  228. position_embedding_size: int = 10 * 1024
  229. use_clipped_linears: bool = False
  230. standardize: bool = False
  231. initializer_range: float = 0.02
  232. def __post_init__(self, **kwargs):
  233. if self.rope_parameters is None:
  234. self.rope_parameters = {"rope_type": "default", "rope_theta": 100.0}
  235. super().__post_init__(**kwargs)
  236. @auto_docstring(checkpoint="google/gemma-4-e2b-it")
  237. @strict
  238. class Gemma4Config(PreTrainedConfig):
  239. r"""
  240. boi_token_id (`int`, *optional*, defaults to 255999):
  241. The begin-of-image token index to wrap the image prompt.
  242. eoi_token_id (`int`, *optional*, defaults to 258882):
  243. The end-of-image token index to wrap the image prompt.
  244. boa_token_id (`int`, *optional*, defaults to 256000):
  245. The begin-of-audio token index to wrap the audio prompt.
  246. eoa_token_index (`int`, *optional*, defaults to 258883):
  247. The end-of-audio token index to wrap the audio prompt.
  248. Example:
  249. ```python
  250. >>> from transformers import (
  251. >>> Gemma4AudioConfig,
  252. >>> Gemma4Config,
  253. >>> Gemma4ForConditionalGeneration,
  254. >>> Gemma4TextConfig,
  255. >>> Gemma4VisionConfig,
  256. >>> )
  257. >>> # Initializing a Gemma 4 Audio config.
  258. >>> audio_config = Gemma4AudioConfig()
  259. >>> # Initializing a Gemma 4 Text config.
  260. >>> text_config = Gemma4TextConfig()
  261. >>> # Initializing a Gemma 4 vision config.
  262. >>> vision_config = Gemma4VisionConfig()
  263. >>> # Initializing a Gemma 4 config similar to google/gemma-4-e2b-it
  264. >>> configuration = Gemma4Config(text_config, vision_config, audio_config)
  265. >>> # Initializing a model from the google/gemma-4-e2b-it configuration
  266. >>> model = Gemma4ForConditionalGeneration(configuration)
  267. >>> # Accessing the model configuration
  268. >>> configuration = model.config
  269. ```"""
  270. model_type = "gemma4"
  271. sub_configs = {
  272. "text_config": Gemma4TextConfig,
  273. "vision_config": Gemma4VisionConfig,
  274. "audio_config": Gemma4AudioConfig,
  275. }
  276. text_config: Gemma4TextConfig | dict[str, Any] | None = None
  277. vision_config: Gemma4VisionConfig | dict[str, Any] | None = None
  278. audio_config: Gemma4AudioConfig | dict[str, Any] | None = None
  279. boi_token_id: int | None = 255_999
  280. eoi_token_id: int | None = 258_882
  281. image_token_id: int | None = 258_880
  282. video_token_id: int | None = 258_884
  283. boa_token_id: int | None = 256_000
  284. eoa_token_index: int | None = 258_883
  285. audio_token_id: int | None = 258_881
  286. initializer_range: float | None = 0.02
  287. tie_word_embeddings: bool = True
  288. def __post_init__(self, **kwargs):
  289. if self.text_config is None:
  290. self.text_config = Gemma4TextConfig()
  291. logger.info("text_config is None. Using default Gemma4TextConfig.")
  292. elif isinstance(self.text_config, dict):
  293. self.text_config = Gemma4TextConfig(**self.text_config)
  294. if self.vision_config is None:
  295. logger.info("vision_config is None. Gemma4Model.vision_tower will not be initialized.")
  296. if isinstance(self.vision_config, dict):
  297. self.vision_config = Gemma4VisionConfig(**self.vision_config)
  298. if self.audio_config is None:
  299. logger.info("audio_config is None. Gemma4Model.audio_tower will not be initialized.")
  300. if isinstance(self.audio_config, dict):
  301. self.audio_config = Gemma4AudioConfig(**self.audio_config)
  302. super().__post_init__(**kwargs)
  303. __all__ = ["Gemma4AudioConfig", "Gemma4Config", "Gemma4TextConfig", "Gemma4VisionConfig"]