configuration_exaone4.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  2. # This file was automatically generated from src/transformers/models/exaone4/modular_exaone4.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_exaone4.py file directly. One of our CI enforces this.
  6. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  7. # coding=utf-8
  8. # Copyright 2025 The LG AI Research and 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, layer_type_validation
  23. class Exaone4Config(PretrainedConfig):
  24. r"""
  25. This is the configuration class to store the configuration of a [`Exaone4Model`]. It is used to
  26. instantiate a EXAONE 4.0 model according to the specified arguments, defining the model architecture. Instantiating a
  27. configuration with the defaults will yield a similar configuration to that of the EXAONE-4.0-32B [LGAI-EXAONE/EXAONE-4.0-32B](https://huggingface.co/LGAI-EXAONE/EXAONE-4.0-32B)
  28. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model
  29. outputs. Read the documentation from [`PretrainedConfig`] for more information.
  30. Args:
  31. vocab_size (`int`, *optional*, defaults to 102400):
  32. Vocabulary size of the EXAONE 4.0 model. Defines the number of different tokens that can be represented by the
  33. `inputs_ids` passed when calling [`Exaone4Model`].
  34. hidden_size (`int`, *optional*, defaults to 4096):
  35. Dimension of the hidden representations.
  36. intermediate_size (`int`, *optional*, defaults to `hidden_size * 4`):
  37. Dimensionality of the MLP representations.
  38. num_hidden_layers (`int`, *optional*, defaults to 32):
  39. Number of hidden layers in the Transformer encoder.
  40. num_attention_heads (`int`, *optional*, defaults to 32):
  41. Number of attention heads for each attention layer in the Transformer decoder.
  42. num_key_value_heads (`int`, *optional*):
  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 checkout [this
  48. paper](https://huggingface.co/papers/2305.13245). If it is not specified, will default to
  49. `num_attention_heads`.
  50. hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
  51. The non-linear activation function (function or string) in the decoder.
  52. max_position_embeddings (`int`, *optional*, defaults to 2048):
  53. The maximum sequence length that this model might ever be used with. Typically set this to something large
  54. just in case (e.g., 32768 for EXAONE 3.5).
  55. initializer_range (`float`, *optional*, defaults to 0.02):
  56. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  57. rms_norm_eps (`float`, *optional*, defaults to 1e-05):
  58. The epsilon used by the layer normalization layers.
  59. use_cache (`bool`, *optional*, defaults to `True`):
  60. Whether or not the model should return the last key/values attentions (not used by all models). Only
  61. relevant if ``config.is_decoder=True``.
  62. bos_token_id (`int`, *optional*, defaults to 0):
  63. Beginning of stream token id.
  64. eos_token_id (`int`, *optional*, defaults to 2):
  65. End of stream token id.
  66. tie_word_embeddings (`bool`, *optional*, defaults to `False`):
  67. Whether to tie weight embeddings
  68. rope_theta (`float`, *optional*, defaults to 10000.0):
  69. The base period of the RoPE embeddings.
  70. rope_scaling (`Dict`, *optional*):
  71. Dictionary containing the scaling configuration for the RoPE embeddings. NOTE: if you apply new rope type
  72. and you expect the model to work on longer `max_position_embeddings`, we recommend you to update this value
  73. accordingly.
  74. Expected contents:
  75. `rope_type` (`str`):
  76. The sub-variant of RoPE to use. Can be one of ['default', 'linear', 'dynamic', 'yarn', 'longrope',
  77. 'llama3'], with 'default' being the original RoPE implementation.
  78. `factor` (`float`, *optional*):
  79. Used with all rope types except 'default'. The scaling factor to apply to the RoPE embeddings. In
  80. most scaling types, a `factor` of x will enable the model to handle sequences of length x *
  81. original maximum pre-trained length.
  82. `original_max_position_embeddings` (`int`, *optional*):
  83. Used with 'dynamic', 'longrope' and 'llama3'. The original max position embeddings used during
  84. pretraining.
  85. `attention_factor` (`float`, *optional*):
  86. Used with 'yarn' and 'longrope'. The scaling factor to be applied on the attention
  87. computation. If unspecified, it defaults to value recommended by the implementation, using the
  88. `factor` field to infer the suggested value.
  89. `beta_fast` (`float`, *optional*):
  90. Only used with 'yarn'. Parameter to set the boundary for extrapolation (only) in the linear
  91. ramp function. If unspecified, it defaults to 32.
  92. `beta_slow` (`float`, *optional*):
  93. Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear
  94. ramp function. If unspecified, it defaults to 1.
  95. `short_factor` (`List[float]`, *optional*):
  96. Only used with 'longrope'. The scaling factor to be applied to short contexts (<
  97. `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden
  98. size divided by the number of attention heads divided by 2
  99. `long_factor` (`List[float]`, *optional*):
  100. Only used with 'longrope'. The scaling factor to be applied to long contexts (<
  101. `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden
  102. size divided by the number of attention heads divided by 2
  103. `low_freq_factor` (`float`, *optional*):
  104. Only used with 'llama3'. Scaling factor applied to low frequency components of the RoPE
  105. `high_freq_factor` (`float`, *optional*):
  106. Only used with 'llama3'. Scaling factor applied to high frequency components of the RoPE
  107. attention_dropout (`float`, *optional*, defaults to 0.0):
  108. The dropout ratio for the attention probabilities.
  109. sliding_window (`int`, *optional*):
  110. The size of the sliding window for the sliding window attention.
  111. sliding_window_pattern (`str`, *optional*):
  112. The pattern to use for sliding window attention. Can be one of:
  113. - `None`: No sliding window attention is used
  114. - `int`: Every `sliding_window` layers, use global attention, else use local attention.
  115. - `str`: A sequence of "L" (local attention) and "G" (global attention) characters that defines the
  116. attention pattern. The pattern starts from layer 0 and repeats every `sliding_window` layers. The
  117. final layer always uses global attention regardless of the pattern.
  118. For instance, sliding_window_pattern="LLLG" same as sliding_window=4, which means:
  119. - Layer 0, 1, 2: local attention,
  120. - Layer 3: global attention,
  121. ...(repeated)
  122. layer_types (`list`, *optional*):
  123. Attention pattern for each layer. Prioritized over `sliding_window_pattern`.
  124. Example:
  125. ```python
  126. >>> from transformers import Exaone4Model, Exaone4Config
  127. >>> # Initializing a EXAONE configuration
  128. >>> configuration = Exaone4Config()
  129. >>> # Initializing a model from configuration
  130. >>> model = Exaone4Model(configuration)
  131. >>> # Accessing the model configuration
  132. >>> configuration = model.config
  133. ```"""
  134. model_type = "exaone4"
  135. keys_to_ignore_at_inference = ["past_key_values"]
  136. # Default tensor parallel plan for base model `LlamaModel`
  137. base_model_tp_plan = {
  138. "layers.*.self_attn.q_proj": "colwise",
  139. "layers.*.self_attn.k_proj": "colwise",
  140. "layers.*.self_attn.v_proj": "colwise",
  141. "layers.*.self_attn.o_proj": "rowwise",
  142. "layers.*.mlp.gate_proj": "colwise",
  143. "layers.*.mlp.up_proj": "colwise",
  144. "layers.*.mlp.down_proj": "rowwise",
  145. }
  146. base_model_pp_plan = {
  147. "embed_tokens": (["input_ids"], ["inputs_embeds"]),
  148. "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
  149. "norm": (["hidden_states"], ["hidden_states"]),
  150. }
  151. def __init__(
  152. self,
  153. vocab_size=102400,
  154. hidden_size=4096,
  155. intermediate_size=16384,
  156. num_hidden_layers=32,
  157. num_attention_heads=32,
  158. num_key_value_heads=32,
  159. hidden_act="silu",
  160. max_position_embeddings=2048,
  161. initializer_range=0.02,
  162. rms_norm_eps=1e-5,
  163. use_cache=True,
  164. bos_token_id=0,
  165. eos_token_id=2,
  166. tie_word_embeddings=False,
  167. rope_theta=10000.0,
  168. rope_scaling=None,
  169. attention_dropout=0.0,
  170. sliding_window=4096,
  171. sliding_window_pattern=4,
  172. layer_types=None,
  173. **kwargs,
  174. ):
  175. self.vocab_size = vocab_size
  176. self.hidden_size = hidden_size
  177. self.num_hidden_layers = num_hidden_layers
  178. self.num_attention_heads = num_attention_heads
  179. self.num_key_value_heads = num_key_value_heads
  180. self.intermediate_size = intermediate_size
  181. self.hidden_act = hidden_act
  182. self.max_position_embeddings = max_position_embeddings
  183. self.initializer_range = initializer_range
  184. self.rms_norm_eps = rms_norm_eps
  185. self.use_cache = use_cache
  186. self.attention_dropout = attention_dropout
  187. self.rope_theta = rope_theta
  188. self.rope_scaling = rope_scaling
  189. self.sliding_window = sliding_window
  190. self.sliding_window_pattern = sliding_window_pattern
  191. self.layer_types = layer_types
  192. if self.sliding_window is None:
  193. sliding_window_pattern = 0
  194. if self.layer_types is None:
  195. self.layer_types = [
  196. "sliding_attention"
  197. if ((i + 1) % (sliding_window_pattern) != 0 and i < self.num_hidden_layers)
  198. else "full_attention"
  199. for i in range(self.num_hidden_layers)
  200. ]
  201. if "sliding_window" in self.layer_types:
  202. self.cache_implementation = "hybrid"
  203. layer_type_validation(self.layer_types, self.num_hidden_layers)
  204. super().__init__(
  205. bos_token_id=bos_token_id, eos_token_id=eos_token_id, tie_word_embeddings=tie_word_embeddings, **kwargs
  206. )
  207. __all__ = ["Exaone4Config"]