configuration_jetmoe.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright 2024 JetMoe AI and the HuggingFace Inc. 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. """JetMoe model configuration"""
  15. from huggingface_hub.dataclasses import strict
  16. from ...configuration_utils import PreTrainedConfig
  17. from ...modeling_rope_utils import RopeParameters
  18. from ...utils import auto_docstring
  19. @auto_docstring(checkpoint="jetmoe/jetmoe-8b")
  20. @strict
  21. class JetMoeConfig(PreTrainedConfig):
  22. r"""
  23. kv_channels (`int`, *optional*, defaults to 128):
  24. Defines the number of channels for the key and value tensors.
  25. num_local_experts (`int`, *optional*, defaults to 8):
  26. Defines the number of experts in the MoE and MoA.
  27. ```python
  28. >>> from transformers import JetMoeModel, JetMoeConfig
  29. >>> # Initializing a JetMoe 4B style configuration
  30. >>> configuration = JetMoeConfig()
  31. >>> # Initializing a model from the JetMoe 4B style configuration
  32. >>> model = JetMoeModel(configuration)
  33. >>> # Accessing the model configuration
  34. >>> configuration = model.config
  35. ```
  36. """
  37. model_type = "jetmoe"
  38. keys_to_ignore_at_inference = ["past_key_values"]
  39. attribute_map = {"head_dim": "kv_channels"}
  40. vocab_size: int = 32000
  41. hidden_size: int = 2048
  42. num_hidden_layers: int = 12
  43. num_key_value_heads: int = 16
  44. kv_channels: int = 128
  45. intermediate_size: int = 5632
  46. max_position_embeddings: int = 4096
  47. activation_function: str = "silu"
  48. num_local_experts: int = 8
  49. num_experts_per_tok: int = 2
  50. output_router_logits: bool = False
  51. aux_loss_coef: float = 0.01
  52. use_cache: bool = True
  53. bos_token_id: int | None = 1
  54. eos_token_id: int | list[int] | None = 2
  55. pad_token_id: int | None = None
  56. tie_word_embeddings: bool = True
  57. rope_parameters: RopeParameters | dict | None = None
  58. rms_norm_eps: float = 1e-6
  59. initializer_range: float = 0.01
  60. attention_dropout: float | int = 0.0
  61. def __post_init__(self, **kwargs):
  62. self.num_attention_heads = self.num_key_value_heads * self.num_experts_per_tok
  63. super().__post_init__(**kwargs)
  64. def validate_architecture(self):
  65. """Part of `@strict`-powered validation. Validates the architecture of the config."""
  66. if self.num_experts_per_tok > self.num_local_experts:
  67. raise ValueError("`num_experts_per_tok` must be less than or equal to `num_local_experts`")
  68. __all__ = ["JetMoeConfig"]