configuration_jetmoe.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # coding=utf-8
  2. # Copyright 2024 JetMoe AI and the HuggingFace Inc. team. All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """JetMoe model configuration"""
  16. from ...configuration_utils import PretrainedConfig
  17. from ...utils import logging
  18. logger = logging.get_logger(__name__)
  19. class JetMoeConfig(PretrainedConfig):
  20. r"""
  21. This is the configuration class to store the configuration of a [`JetMoeModel`]. It is used to instantiate a
  22. JetMoe model according to the specified arguments, defining the model architecture. Instantiating a configuration
  23. with the defaults will yield a configuration of the JetMoe-4B.
  24. [jetmoe/jetmoe-8b](https://huggingface.co/jetmoe/jetmoe-8b)
  25. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  26. documentation from [`PretrainedConfig`] for more information.
  27. Args:
  28. vocab_size (`int`, *optional*, defaults to 32000):
  29. Vocabulary size of the JetMoe model. Defines the number of different tokens that can be represented by the
  30. `inputs_ids` passed when calling [`JetMoeModel`]
  31. hidden_size (`int`, *optional*, defaults to 2048):
  32. Dimension of the hidden representations.
  33. num_hidden_layers (`int`, *optional*, defaults to 12):
  34. Number of hidden layers in the Transformer encoder.
  35. num_key_value_heads (`int`, *optional*, defaults to 16):
  36. Number of attention heads for each key and value in the Transformer encoder.
  37. kv_channels (`int`, *optional*, defaults to 128):
  38. Defines the number of channels for the key and value tensors.
  39. intermediate_size (`int`, *optional*, defaults to 5632):
  40. Dimension of the MLP representations.
  41. max_position_embeddings (`int`, *optional*, defaults to 4096):
  42. The maximum sequence length that this model might ever be used with. JetMoe's attention allows sequence of
  43. up to 4096 tokens.
  44. activation_function (`string`, *optional*, defaults to `"silu"`):
  45. Defines the activation function for MLP experts.
  46. num_local_experts (`int`, *optional*, defaults to 8):
  47. Defines the number of experts in the MoE and MoA.
  48. num_experts_per_tok (`int, *optional*, defaults to 2):
  49. The number of experts to route per-token and for MoE and MoA.
  50. output_router_logits (`bool`, *optional*, defaults to `False`):
  51. Whether or not the router logits should be returned by the model. Enabling this will also
  52. allow the model to output the auxiliary loss.
  53. aux_loss_coef (`float`, *optional*, defaults to 0.01):
  54. The coefficient for the auxiliary loss.
  55. use_cache (`bool`, *optional*, defaults to `True`):
  56. Whether or not the model should return the last key/values attentions (not used by all models). Only
  57. relevant if `config.is_decoder=True`.
  58. bos_token_id (`int`, *optional*, defaults to 1):
  59. The id of the "beginning-of-sequence" token.
  60. eos_token_id (`int`, *optional*, defaults to 2):
  61. The id of the "end-of-sequence" token.
  62. tie_word_embeddings (`bool`, *optional*, defaults to `True`):
  63. Whether the model's input and output word embeddings should be tied.
  64. rope_theta (`float`, *optional*, defaults to 10000.0):
  65. The base period of the RoPE embeddings.
  66. rms_norm_eps (`float`, *optional*, defaults to 1e-06):
  67. The epsilon used by the rms normalization layers.
  68. initializer_range (`float`, *optional*, defaults to 0.01):
  69. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  70. attention_dropout (`float`, *optional*, defaults to 0.0):
  71. The dropout ratio for the attention probabilities.
  72. ```python
  73. >>> from transformers import JetMoeModel, JetMoeConfig
  74. >>> # Initializing a JetMoe 4B style configuration
  75. >>> configuration = JetMoeConfig()
  76. >>> # Initializing a model from the JetMoe 4B style configuration
  77. >>> model = JetMoeModel(configuration)
  78. >>> # Accessing the model configuration
  79. >>> configuration = model.config
  80. ```"""
  81. model_type = "jetmoe"
  82. keys_to_ignore_at_inference = ["past_key_values"]
  83. attribute_map = {"head_dim": "kv_channels"}
  84. def __init__(
  85. self,
  86. vocab_size=32000,
  87. hidden_size=2048,
  88. num_hidden_layers=12,
  89. num_key_value_heads=16,
  90. kv_channels=128,
  91. intermediate_size=5632,
  92. max_position_embeddings=4096,
  93. activation_function="silu",
  94. num_local_experts=8,
  95. num_experts_per_tok=2,
  96. output_router_logits=False,
  97. aux_loss_coef=0.01,
  98. use_cache=True,
  99. bos_token_id=1,
  100. eos_token_id=2,
  101. tie_word_embeddings=True,
  102. rope_theta=10000.0,
  103. rms_norm_eps=1e-6,
  104. initializer_range=0.01,
  105. attention_dropout=0.0,
  106. **kwargs,
  107. ):
  108. if num_experts_per_tok > num_local_experts:
  109. raise ValueError("`num_experts_per_tok` must be less than or equal to `num_local_experts`")
  110. self.vocab_size = vocab_size
  111. self.hidden_size = hidden_size
  112. self.num_hidden_layers = num_hidden_layers
  113. self.num_attention_heads = num_key_value_heads * num_experts_per_tok
  114. self.num_key_value_heads = num_key_value_heads
  115. self.kv_channels = kv_channels
  116. self.intermediate_size = intermediate_size
  117. self.max_position_embeddings = max_position_embeddings
  118. self.activation_function = activation_function
  119. self.num_local_experts = num_local_experts
  120. self.num_experts_per_tok = num_experts_per_tok
  121. self.output_router_logits = output_router_logits
  122. self.aux_loss_coef = aux_loss_coef
  123. self.use_cache = use_cache
  124. self.initializer_range = initializer_range
  125. self.attention_dropout = attention_dropout
  126. self.bos_token_id = bos_token_id
  127. self.eos_token_id = eos_token_id
  128. self.rope_theta = rope_theta
  129. self.rms_norm_eps = rms_norm_eps
  130. super().__init__(
  131. bos_token_id=bos_token_id, eos_token_id=eos_token_id, tie_word_embeddings=tie_word_embeddings, **kwargs
  132. )
  133. __all__ = ["JetMoeConfig"]