configuration_afmoe.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Copyright 2025 Arcee 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. """AFMoE 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. @strict
  20. @auto_docstring(
  21. custom_intro="""
  22. AFMoE is an Adaptive Feedforward MoE (Mixture of Experts) model with token-choice routing, shared experts, and a
  23. hybrid attention mechanism combining sliding window and full attention patterns.
  24. """,
  25. checkpoint="arcee-ai/Trinity-Mini",
  26. )
  27. class AfmoeConfig(PreTrainedConfig):
  28. r"""
  29. global_attn_every_n_layers (`int`, *optional*, defaults to 4):
  30. The frequency of full attention layers. Every Nth layer will use full attention, while others use sliding
  31. window attention.
  32. mup_enabled (`bool`, *optional*, defaults to `False`):
  33. Whether to enable muP (Maximal Update Parametrization) input scaling. When enabled, input embeddings
  34. are scaled by `sqrt(hidden_size)`.
  35. Example:
  36. ```python
  37. >>> from transformers import AfmoeModel, AfmoeConfig
  38. >>> # Initializing an AFMoE configuration
  39. >>> configuration = AfmoeConfig()
  40. >>> # Initializing a model from the afmoe-small-sft-v1 style configuration
  41. >>> model = AfmoeModel(configuration)
  42. >>> # Accessing the model configuration
  43. >>> configuration = model.config
  44. ```
  45. """
  46. model_type = "afmoe"
  47. keys_to_ignore_at_inference = ["past_key_values"]
  48. # Default pipeline parallel plan for base model
  49. base_model_pp_plan = {
  50. "embed_tokens": (["input_ids"], ["inputs_embeds"]),
  51. "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
  52. "norm": (["hidden_states"], ["hidden_states"]),
  53. }
  54. vocab_size: int = 200192
  55. hidden_size: int = 2048
  56. intermediate_size: int = 6144
  57. moe_intermediate_size: int = 1408
  58. num_hidden_layers: int = 32
  59. num_dense_layers: int | None = 1
  60. num_attention_heads: int = 16
  61. num_key_value_heads: int | None = None
  62. head_dim: int | None = 128
  63. hidden_act: str = "silu"
  64. max_position_embeddings: int = 16384
  65. initializer_range: float = 0.02
  66. rms_norm_eps: float = 1e-5
  67. use_cache: bool = True
  68. tie_word_embeddings: bool = False
  69. rope_parameters: RopeParameters | dict | None = None
  70. num_experts: int | None = 64
  71. num_experts_per_tok: int | None = 6
  72. num_shared_experts: int | None = 2
  73. route_scale: float | None = 1.0
  74. output_router_logits: bool = False
  75. global_attn_every_n_layers: int | None = 4
  76. sliding_window: int | None = 1024
  77. layer_types: list[str] | None = None
  78. attention_dropout: float | int | None = 0.0
  79. mup_enabled: bool | None = False
  80. eos_token_id: int | list[int] | None = None
  81. pad_token_id: int | None = None
  82. bos_token_id: int | None = None
  83. attention_bias: bool = False
  84. def __post_init__(self, **kwargs):
  85. if self.layer_types is None:
  86. self.layer_types = [
  87. "sliding_attention" if bool((i + 1) % self.global_attn_every_n_layers) else "full_attention"
  88. for i in range(self.num_hidden_layers)
  89. ]
  90. if self.num_key_value_heads is None:
  91. self.num_key_value_heads = self.num_attention_heads
  92. super().__post_init__(**kwargs)
  93. __all__ = ["AfmoeConfig"]