configuration_olmoe.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # Licensed under the Apache License, Version 2.0 (the "License");
  2. # you may not use this file except in compliance with the License.
  3. # You may obtain a copy of the License at
  4. #
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. """OLMoE model configuration"""
  13. from ...configuration_utils import PretrainedConfig
  14. from ...modeling_rope_utils import rope_config_validation
  15. class OlmoeConfig(PretrainedConfig):
  16. r"""
  17. This is the configuration class to store the configuration of a [`OlmoeModel`]. It is used to instantiate an OLMoE
  18. model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
  19. defaults will yield a similar configuration to that of the [allenai/OLMoE-1B-7B-0924](https://huggingface.co/allenai/OLMoE-1B-7B-0924).
  20. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  21. documentation from [`PretrainedConfig`] for more information.
  22. Args:
  23. vocab_size (`int`, *optional*, defaults to 50304):
  24. Vocabulary size of the OLMoE model. Defines the number of different tokens that can be represented by the
  25. `inputs_ids` passed when calling [`OlmoeModel`]
  26. hidden_size (`int`, *optional*, defaults to 2048):
  27. Dimension of the hidden representations.
  28. intermediate_size (`int`, *optional*, defaults to 2048):
  29. Dimension of the MLP representations.
  30. num_hidden_layers (`int`, *optional*, defaults to 16):
  31. Number of hidden layers in the Transformer decoder.
  32. num_attention_heads (`int`, *optional*, defaults to 16):
  33. Number of attention heads for each attention layer in the Transformer decoder.
  34. num_key_value_heads (`int`, *optional*):
  35. This is the number of key_value heads that should be used to implement Grouped Query Attention. If
  36. `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
  37. `num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When
  38. converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
  39. by meanpooling all the original heads within that group. For more details, check out [this
  40. paper](https://huggingface.co/papers/2305.13245). If it is not specified, will default to
  41. `num_attention_heads`.
  42. hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
  43. The non-linear activation function (function or string) in the decoder.
  44. max_position_embeddings (`int`, *optional*, defaults to 4096):
  45. The maximum sequence length that this model might ever be used with.
  46. initializer_range (`float`, *optional*, defaults to 0.02):
  47. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  48. rms_norm_eps (`float`, *optional*, defaults to 1e-05):
  49. The epsilon used by the rms normalization layers.
  50. use_cache (`bool`, *optional*, defaults to `True`):
  51. Whether or not the model should return the last key/values attentions (not used by all models). Only
  52. relevant if `config.is_decoder=True`.
  53. pad_token_id (`int`, *optional*, defaults to 1):
  54. Padding token id.
  55. bos_token_id (`int`, *optional*):
  56. Beginning of stream token id.
  57. eos_token_id (`int`, *optional*, defaults to 50279):
  58. End of stream token id.
  59. tie_word_embeddings (`bool`, *optional*, defaults to `False`):
  60. Whether to tie weight embeddings
  61. rope_theta (`float`, *optional*, defaults to 10000.0):
  62. The base period of the RoPE embeddings.
  63. rope_scaling (`Dict`, *optional*):
  64. Dictionary containing the scaling configuration for the RoPE embeddings. Currently supports two scaling
  65. strategies: linear and dynamic. Their scaling factor must be a float greater than 1. The expected format is
  66. `{"type": strategy name, "factor": scaling factor}`. When using this flag, don't update
  67. `max_position_embeddings` to the expected new maximum. See the following thread for more information on how
  68. these scaling strategies behave:
  69. https://www.reddit.com/r/LocalLLaMA/comments/14mrgpr/dynamically_scaled_rope_further_increases/. This is an
  70. experimental feature, subject to breaking API changes in future versions.
  71. attention_bias (`bool`, defaults to `False`, *optional*, defaults to `False`):
  72. Whether to use a bias in the query, key, value and output projection layers during self-attention.
  73. attention_dropout (`float`, *optional*, defaults to 0.0):
  74. The dropout ratio for the attention probabilities.
  75. clip_qkv (`float`, *optional*):
  76. If not `None`, elements of query, key and value attention states are clipped so that their
  77. absolute value does not exceed this value.
  78. num_experts_per_tok (`int`, *optional*, defaults to 8):
  79. Number of selected experts.
  80. num_experts (`int`, *optional*, defaults to 64):
  81. Number of routed experts.
  82. output_router_logits (`bool`, *optional*, defaults to `False`):
  83. Whether or not the router logits should be returned by the model. Enabling this will also
  84. allow the model to output the auxiliary loss, including load balancing loss and router z-loss.
  85. router_aux_loss_coef (`float`, *optional*, defaults to 0.01):
  86. The aux loss factor for the total loss.
  87. norm_topk_prob (`bool`, *optional*, defaults to `False`):
  88. Whether to normalize the topk probabilities.
  89. ```python
  90. >>> from transformers import OlmoeModel, OlmoeConfig
  91. >>> # Initializing a OLMoE 7B A1B style configuration
  92. >>> configuration = OlmoeConfig()
  93. >>> # Initializing a model from the OLMoE 7B A1B style configuration
  94. >>> model = OlmoeModel(configuration)
  95. >>> # Accessing the model configuration
  96. >>> configuration = model.config
  97. ```"""
  98. model_type = "olmoe"
  99. keys_to_ignore_at_inference = ["past_key_values"]
  100. def __init__(
  101. self,
  102. vocab_size=50304,
  103. hidden_size=2048,
  104. intermediate_size=2048,
  105. num_hidden_layers=16,
  106. num_attention_heads=16,
  107. num_key_value_heads=None,
  108. hidden_act="silu",
  109. max_position_embeddings=4096,
  110. initializer_range=0.02,
  111. rms_norm_eps=1e-05,
  112. use_cache=True,
  113. pad_token_id=1,
  114. bos_token_id=None,
  115. eos_token_id=50279,
  116. tie_word_embeddings=False,
  117. rope_theta=10000.0,
  118. rope_scaling=None,
  119. attention_bias=False,
  120. attention_dropout=0.0,
  121. clip_qkv=None,
  122. num_experts_per_tok=8,
  123. num_experts=64,
  124. output_router_logits=False,
  125. router_aux_loss_coef=0.01,
  126. norm_topk_prob=False,
  127. **kwargs,
  128. ):
  129. self.vocab_size = vocab_size
  130. self.max_position_embeddings = max_position_embeddings
  131. self.hidden_size = hidden_size
  132. self.intermediate_size = intermediate_size
  133. self.num_hidden_layers = num_hidden_layers
  134. self.num_attention_heads = num_attention_heads
  135. # for backward compatibility
  136. if num_key_value_heads is None:
  137. num_key_value_heads = num_attention_heads
  138. self.num_key_value_heads = num_key_value_heads
  139. self.hidden_act = hidden_act
  140. self.initializer_range = initializer_range
  141. self.rms_norm_eps = rms_norm_eps
  142. self.use_cache = use_cache
  143. self.rope_theta = rope_theta
  144. self.rope_scaling = rope_scaling
  145. self.attention_bias = attention_bias
  146. self.attention_dropout = attention_dropout
  147. self.clip_qkv = clip_qkv
  148. self.num_experts_per_tok = num_experts_per_tok
  149. self.num_experts = num_experts
  150. self.output_router_logits = output_router_logits
  151. self.router_aux_loss_coef = router_aux_loss_coef
  152. self.norm_topk_prob = norm_topk_prob
  153. # Validate the correctness of rotary position embeddings parameters
  154. # BC: if there is a 'type' field, move it to 'rope_type'.
  155. if self.rope_scaling is not None and "type" in self.rope_scaling:
  156. self.rope_scaling["rope_type"] = self.rope_scaling["type"]
  157. rope_config_validation(self)
  158. super().__init__(
  159. pad_token_id=pad_token_id,
  160. bos_token_id=bos_token_id,
  161. eos_token_id=eos_token_id,
  162. tie_word_embeddings=tie_word_embeddings,
  163. **kwargs,
  164. )
  165. __all__ = ["OlmoeConfig"]