configuration_mixtral.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Copyright 2023 Mixtral 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. """Mixtral 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="mistralai/Mixtral-8x7B-v0.1")
  20. @strict
  21. class MixtralConfig(PreTrainedConfig):
  22. r"""
  23. Example:
  24. ```python
  25. >>> from transformers import MixtralModel, MixtralConfig
  26. >>> # Initializing a Mixtral 7B style configuration
  27. >>> configuration = MixtralConfig()
  28. >>> # Initializing a model from the Mixtral 7B style configuration
  29. >>> model = MixtralModel(configuration)
  30. >>> # Accessing the model configuration
  31. >>> configuration = model.config
  32. ```"""
  33. model_type = "mixtral"
  34. keys_to_ignore_at_inference = ["past_key_values"]
  35. default_theta = 1000000.0
  36. base_model_tp_plan = {
  37. "layers.*.self_attn.q_proj": "colwise",
  38. "layers.*.self_attn.k_proj": "colwise",
  39. "layers.*.self_attn.v_proj": "colwise",
  40. "layers.*.self_attn.o_proj": "rowwise",
  41. "layers.*.mlp.experts.gate_up_proj": "packed_colwise",
  42. "layers.*.mlp.experts.down_proj": "rowwise",
  43. "layers.*.mlp.experts": "moe_tp_experts",
  44. }
  45. base_model_pp_plan = {
  46. "embed_tokens": (["input_ids"], ["inputs_embeds"]),
  47. "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
  48. "norm": (["hidden_states"], ["hidden_states"]),
  49. }
  50. attribute_map = {"num_experts": "num_local_experts"}
  51. vocab_size: int = 32000
  52. hidden_size: int = 4096
  53. intermediate_size: int = 14336
  54. num_hidden_layers: int = 32
  55. num_attention_heads: int = 32
  56. num_key_value_heads: int = 8
  57. head_dim: int | None = None
  58. hidden_act: str = "silu"
  59. max_position_embeddings: int = 4096 * 32
  60. initializer_range: float = 0.02
  61. rms_norm_eps: float = 1e-5
  62. use_cache: bool = True
  63. pad_token_id: int | None = None
  64. bos_token_id: int | None = 1
  65. eos_token_id: int | list[int] | None = 2
  66. tie_word_embeddings: bool = False
  67. sliding_window: int | None = None
  68. attention_dropout: float | int = 0.0
  69. num_experts_per_tok: int = 2
  70. num_local_experts: int = 8
  71. output_router_logits: bool = False
  72. router_aux_loss_coef: float = 0.001
  73. router_jitter_noise: float = 0.0
  74. rope_parameters: RopeParameters | dict | None = None
  75. def __post_init__(self, **kwargs):
  76. if self.num_key_value_heads is None:
  77. self.num_key_value_heads = self.num_attention_heads
  78. super().__post_init__(**kwargs)
  79. __all__ = ["MixtralConfig"]