configuration_phimoe.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright 2024 Microsoft 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. """PyTorch Phi-MoE model."""
  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="microsoft/Phi-3.5-MoE-instruct")
  20. @strict
  21. class PhimoeConfig(PreTrainedConfig):
  22. r"""
  23. num_local_experts (`int`, *optional*, defaults to 16):
  24. Number of experts per Sparse MLP layer.
  25. input_jitter_noise (`float`, *optional*, defaults to 0.0):
  26. Input jitter noise
  27. lm_head_bias (`bool`, *optional*, defaults to `False`):
  28. LM head bias
  29. Example:
  30. ```python
  31. >>> from transformers import PhimoeModel, PhimoeConfig
  32. >>> # Initializing a Phi-3 style configuration
  33. >>> configuration = PhimoeConfig.from_pretrained("microsoft/Phi-3.5-MoE-instruct")
  34. >>> # Initializing a model from the configuration
  35. >>> model = PhimoeModel(configuration)
  36. >>> # Accessing the model configuration
  37. >>> configuration = model.config
  38. ```"""
  39. model_type = "phimoe"
  40. keys_to_ignore_at_inference = ["past_key_values"]
  41. default_theta = 1000000.0
  42. vocab_size: int = 32064
  43. hidden_size: int = 4096
  44. intermediate_size: int = 6400
  45. num_hidden_layers: int = 32
  46. num_attention_heads: int = 32
  47. num_key_value_heads: int = 8
  48. hidden_act: str = "silu"
  49. max_position_embeddings: int = 4096 * 32
  50. initializer_range: float = 0.02
  51. rms_norm_eps: float = 1e-5
  52. use_cache: bool = True
  53. pad_token_id: int | None = None
  54. bos_token_id: int | None = 1
  55. eos_token_id: int | list[int] | None = 2
  56. tie_word_embeddings: bool = False
  57. rope_parameters: RopeParameters | dict | None = None
  58. sliding_window: int | None = None
  59. attention_dropout: float | int = 0.0
  60. num_experts_per_tok: int = 2
  61. num_local_experts: int = 16
  62. output_router_logits: bool = False
  63. router_aux_loss_coef: float = 0.001
  64. router_jitter_noise: float = 0.01
  65. input_jitter_noise: float = 0.0
  66. attention_bias: bool = False
  67. lm_head_bias: bool = False
  68. def __post_init__(self, **kwargs):
  69. if self.num_key_value_heads is None:
  70. self.num_key_value_heads = self.num_attention_heads
  71. super().__post_init__(**kwargs)
  72. def validate_rope(self):
  73. """
  74. Validate the `rope_parameters` configuration.
  75. """
  76. super().validate_rope()
  77. # Run model-specific rope validation
  78. if self.rope_parameters["rope_type"] != "default":
  79. if "original_max_position_embeddings" in self.rope_parameters:
  80. self.original_max_position_embeddings = self.rope_parameters["original_max_position_embeddings"]
  81. rope_parameters_short_mscale = self.rope_parameters.get("short_mscale", None)
  82. rope_parameters_long_mscale = self.rope_parameters.get("long_mscale", None)
  83. if not isinstance(rope_parameters_short_mscale, (int, float)):
  84. raise TypeError(
  85. f"`rope_parameters`'s short_mscale field must be a number, got {rope_parameters_short_mscale}"
  86. )
  87. if not isinstance(rope_parameters_long_mscale, (int, float)):
  88. raise TypeError(
  89. f"`rope_parameters`'s long_mscale field must be a number, got {rope_parameters_long_mscale}"
  90. )
  91. __all__ = ["PhimoeConfig"]