configuration_mistral3.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # coding=utf-8
  2. # Copyright 2025 HuggingFace Inc. team. All rights reserved.
  3. #
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from ...configuration_utils import PretrainedConfig
  17. from ..auto import CONFIG_MAPPING, AutoConfig
  18. class Mistral3Config(PretrainedConfig):
  19. r"""
  20. This is the configuration class to store the configuration of a [`Mistral3ForConditionalGeneration`]. It is used to instantiate an
  21. Mistral3 model according to the specified arguments, defining the model architecture. Instantiating a configuration
  22. with the defaults will yield a similar configuration to that of
  23. [mistralai/Mistral-Small-3.1-24B-Instruct-2503](https://huggingface.co/mistralai/Mistral-Small-3.1-24B-Instruct-2503)
  24. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  25. documentation from [`PretrainedConfig`] for more information.
  26. Args:
  27. vision_config (`Union[AutoConfig, dict]`, *optional*, defaults to `PixtralVisionConfig`):
  28. The config object or dictionary of the vision backbone.
  29. text_config (`Union[AutoConfig, dict]`, *optional*, defaults to `MistralConfig`):
  30. The config object or dictionary of the text backbone.
  31. image_token_index (`int`, *optional*, defaults to 10):
  32. The image token index to encode the image prompt.
  33. projector_hidden_act (`str`, *optional*, defaults to `"gelu"`):
  34. The activation function used by the multimodal projector.
  35. vision_feature_layer (`Union[int, list[int]]`, *optional*, defaults to -1):
  36. The index of the layer to select the vision feature. If multiple indices are provided,
  37. the vision feature of the corresponding indices will be concatenated to form the
  38. vision features.
  39. multimodal_projector_bias (`bool`, *optional*, defaults to `False`):
  40. Whether to use bias in the multimodal projector.
  41. spatial_merge_size (`int`, *optional*, defaults to 2):
  42. The downsampling factor for the spatial merge operation.
  43. Example:
  44. ```python
  45. >>> from transformers import Mistral3ForConditionalGeneration, Mistral3Config, PixtralVisionConfig, MistralConfig
  46. >>> # Initializing a Pixtral-vision config
  47. >>> vision_config = PixtralVisionConfig()
  48. >>> # Initializing a Mistral config
  49. >>> text_config = MistralConfig()
  50. >>> # Initializing a Mistral3 configuration
  51. >>> configuration = Mistral3Config(vision_config, text_config)
  52. >>> # Initializing a model from the mistral3.1 configuration
  53. >>> model = Mistral3ForConditionalGeneration(configuration)
  54. >>> # Accessing the model configuration
  55. >>> configuration = model.config
  56. ```"""
  57. model_type = "mistral3"
  58. attribute_map = {
  59. "image_token_id": "image_token_index",
  60. }
  61. sub_configs = {"text_config": AutoConfig, "vision_config": AutoConfig}
  62. is_composition = True
  63. def __init__(
  64. self,
  65. vision_config=None,
  66. text_config=None,
  67. image_token_index=10,
  68. projector_hidden_act="gelu",
  69. vision_feature_layer=-1,
  70. multimodal_projector_bias=False,
  71. spatial_merge_size=2,
  72. **kwargs,
  73. ):
  74. self.image_token_index = image_token_index
  75. self.projector_hidden_act = projector_hidden_act
  76. self.vision_feature_layer = vision_feature_layer
  77. if isinstance(vision_config, dict):
  78. vision_config["model_type"] = vision_config.get("model_type", "pixtral")
  79. vision_config = CONFIG_MAPPING[vision_config["model_type"]](**vision_config)
  80. elif vision_config is None:
  81. vision_config = CONFIG_MAPPING["pixtral"](
  82. intermediate_size=4096,
  83. hidden_size=1024,
  84. patch_size=14,
  85. image_size=1540,
  86. num_hidden_layers=24,
  87. num_attention_heads=16,
  88. vocab_size=32000,
  89. head_dim=64,
  90. hidden_act="gelu",
  91. )
  92. self.vision_config = vision_config
  93. if isinstance(text_config, dict):
  94. text_config["model_type"] = text_config.get("model_type", "mistral")
  95. text_config = CONFIG_MAPPING[text_config["model_type"]](**text_config)
  96. elif text_config is None:
  97. text_config = CONFIG_MAPPING["mistral"](
  98. attention_dropout=0.0,
  99. head_dim=128,
  100. hidden_act="silu",
  101. hidden_size=5120,
  102. initializer_range=0.02,
  103. intermediate_size=32768,
  104. max_position_embeddings=131072,
  105. model_type="mistral",
  106. num_attention_heads=32,
  107. num_hidden_layers=40,
  108. num_key_value_heads=8,
  109. rms_norm_eps=1e-05,
  110. rope_theta=1000000000.0,
  111. sliding_window=None,
  112. use_cache=True,
  113. vocab_size=131072,
  114. )
  115. self.text_config = text_config
  116. self.multimodal_projector_bias = multimodal_projector_bias
  117. self.spatial_merge_size = spatial_merge_size
  118. super().__init__(**kwargs)
  119. __all__ = ["Mistral3Config"]