configuration_mistral3.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # Copyright 2025 HuggingFace Inc. team. All rights reserved.
  2. #
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from huggingface_hub.dataclasses import strict
  16. from ...configuration_utils import PreTrainedConfig
  17. from ...utils import auto_docstring
  18. from ..auto import CONFIG_MAPPING, AutoConfig
  19. @auto_docstring(checkpoint="mistralai/Mistral-Small-3.1-24B-Instruct-2503")
  20. @strict
  21. class Mistral3Config(PreTrainedConfig):
  22. r"""
  23. Example:
  24. ```python
  25. >>> from transformers import Mistral3ForConditionalGeneration, Mistral3Config, PixtralVisionConfig, MistralConfig
  26. >>> # Initializing a Pixtral-vision config
  27. >>> vision_config = PixtralVisionConfig()
  28. >>> # Initializing a Mistral config
  29. >>> text_config = MistralConfig()
  30. >>> # Initializing a Mistral3 configuration
  31. >>> configuration = Mistral3Config(vision_config, text_config)
  32. >>> # Initializing a model from the mistral3.1 configuration
  33. >>> model = Mistral3ForConditionalGeneration(configuration)
  34. >>> # Accessing the model configuration
  35. >>> configuration = model.config
  36. ```"""
  37. model_type = "mistral3"
  38. attribute_map = {
  39. "image_token_id": "image_token_index",
  40. }
  41. sub_configs = {"text_config": AutoConfig, "vision_config": AutoConfig}
  42. is_composition = True
  43. vision_config: dict | PreTrainedConfig | None = None
  44. text_config: dict | PreTrainedConfig | None = None
  45. image_token_index: int = 10
  46. projector_hidden_act: str = "gelu"
  47. vision_feature_layer: int | list[int] = -1
  48. multimodal_projector_bias: bool = False
  49. spatial_merge_size: int = 2
  50. tie_word_embeddings: bool = True
  51. def __post_init__(self, **kwargs):
  52. if isinstance(self.vision_config, dict):
  53. self.vision_config["model_type"] = self.vision_config.get("model_type", "pixtral")
  54. self.vision_config = CONFIG_MAPPING[self.vision_config["model_type"]](**self.vision_config)
  55. elif self.vision_config is None:
  56. self.vision_config = CONFIG_MAPPING["pixtral"](
  57. intermediate_size=4096,
  58. hidden_size=1024,
  59. patch_size=14,
  60. image_size=1540,
  61. num_hidden_layers=24,
  62. num_attention_heads=16,
  63. vocab_size=32000,
  64. head_dim=64,
  65. hidden_act="gelu",
  66. )
  67. if isinstance(self.text_config, dict):
  68. self.text_config["model_type"] = self.text_config.get("model_type", "mistral")
  69. self.text_config = CONFIG_MAPPING[self.text_config["model_type"]](**self.text_config)
  70. elif self.text_config is None:
  71. self.text_config = CONFIG_MAPPING["mistral"](
  72. attention_dropout=0.0,
  73. head_dim=128,
  74. hidden_act="silu",
  75. hidden_size=5120,
  76. initializer_range=0.02,
  77. intermediate_size=32768,
  78. max_position_embeddings=131072,
  79. model_type="mistral",
  80. num_attention_heads=32,
  81. num_hidden_layers=40,
  82. num_key_value_heads=8,
  83. rms_norm_eps=1e-05,
  84. rope_theta=1000000000.0,
  85. sliding_window=None,
  86. use_cache=True,
  87. vocab_size=131072,
  88. )
  89. super().__post_init__(**kwargs)
  90. __all__ = ["Mistral3Config"]