configuration_mistral.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright 2023 Mistral 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. """Mistral 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, logging
  19. logger = logging.get_logger(__name__)
  20. @auto_docstring(checkpoint="mistralai/Mistral-7B-v0.1")
  21. @strict
  22. class MistralConfig(PreTrainedConfig):
  23. r"""
  24. Example:
  25. ```python
  26. >>> from transformers import MistralModel, MistralConfig
  27. >>> # Initializing a Mistral 7B style configuration
  28. >>> configuration = MistralConfig()
  29. >>> # Initializing a model from the Mistral 7B style configuration
  30. >>> model = MistralModel(configuration)
  31. >>> # Accessing the model configuration
  32. >>> configuration = model.config
  33. ```"""
  34. model_type = "mistral"
  35. keys_to_ignore_at_inference = ["past_key_values"]
  36. # Default tensor parallel plan for base model `MistralModel`
  37. base_model_tp_plan = {
  38. "layers.*.self_attn.q_proj": "colwise",
  39. "layers.*.self_attn.k_proj": "colwise",
  40. "layers.*.self_attn.v_proj": "colwise",
  41. "layers.*.self_attn.o_proj": "rowwise",
  42. "layers.*.mlp.gate_proj": "colwise",
  43. "layers.*.mlp.up_proj": "colwise",
  44. "layers.*.mlp.down_proj": "rowwise",
  45. }
  46. base_model_pp_plan = {
  47. "embed_tokens": (["input_ids"], ["inputs_embeds"]),
  48. "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
  49. "norm": (["hidden_states"], ["hidden_states"]),
  50. }
  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-6
  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. rope_parameters: RopeParameters | dict | None = None
  68. sliding_window: int | None = 4096
  69. attention_dropout: float | int = 0.0
  70. def __post_init__(self, **kwargs):
  71. self.head_dim = self.head_dim if self.head_dim is not None else self.hidden_size // self.num_attention_heads
  72. if self.num_key_value_heads is None:
  73. self.num_key_value_heads = self.num_attention_heads
  74. if "layer_types" in kwargs:
  75. logger.warning_once(
  76. "Detected Mistral model with layer_types. Consider using AutoModel or Ministral classes instead to enable alternating attention compatibility."
  77. )
  78. return super().__post_init__(**kwargs)
  79. __all__ = ["MistralConfig"]