Source code for easydel.modules.mistral3.mistral3_configuration

# Copyright 2025 The EasyDeL Author @erfanzar (Erfan Zare Chavoshi).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import typing as tp

from eformer.common_types import ColumnWise, Replicated, RowWise

from easydel.infra.base_module import EasyDeLBaseConfig
from easydel.infra.factory import register_config, registry


[docs]@register_config("mistral3") class Mistral3Config(EasyDeLBaseConfig): """ Configuration objects inherit from [`EasyDeLBaseConfig`] and can be used to control the model outputs. Read the documentation from [`EasyDeLBaseConfig`] for more information. Args: vision_config (`Union[AutoConfig, dict]`, *optional*, defaults to `PixtralVisionConfig`): The config object or dictionary of the vision backbone. text_config (`Union[AutoConfig, dict]`, *optional*, defaults to `MistralConfig`): The config object or dictionary of the text backbone. image_token_index (`int`, *optional*, defaults to 10): The image token index to encode the image prompt. projector_hidden_act (`str`, *optional*, defaults to `"gelu"`): The activation function used by the multimodal projector. vision_feature_layer (`Union[int, list[int]]`, *optional*, defaults to -1): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. multimodal_projector_bias (`bool`, *optional*, defaults to `False`): Whether to use bias in the multimodal projector. spatial_merge_size (`int`, *optional*, defaults to 2): The downsampling factor for the spatial merge operation. """ model_type = "mistral3" attribute_map: tp.ClassVar = {"image_token_id": "image_token_index"} sub_configs: tp.ClassVar = {"text_config": EasyDeLBaseConfig, "vision_config": EasyDeLBaseConfig} is_composition = True def __init__( self, vision_config=None, text_config=None, image_token_index=10, projector_hidden_act="gelu", vision_feature_layer=-1, multimodal_projector_bias=False, spatial_merge_size=2, **kwargs, ): super().__init__(**kwargs) self.image_token_index = image_token_index self.projector_hidden_act = projector_hidden_act self.vision_feature_layer = vision_feature_layer if isinstance(vision_config, dict): vision_config["model_type"] = vision_config["model_type"] if "model_type" in vision_config else "pixtral" vision_config = registry.get_config(vision_config["model_type"])(**vision_config) elif vision_config is None: vision_config = registry.get_config("pixtral")( intermediate_size=4096, hidden_size=1024, patch_size=14, image_size=1540, num_hidden_layers=24, num_attention_heads=16, vocab_size=32000, head_dim=64, hidden_act="gelu", ) self.vision_config = vision_config if isinstance(text_config, dict): text_config["model_type"] = text_config["model_type"] if "model_type" in text_config else "mistral" text_config = registry.get_config(text_config["model_type"])(**text_config) elif text_config is None: text_config = registry.get_config("mistral")( attention_dropout=0.0, head_dim=128, hidden_act="silu", hidden_size=5120, initializer_range=0.02, intermediate_size=32768, max_position_embeddings=131072, model_type="mistral", num_attention_heads=32, num_hidden_layers=40, num_key_value_heads=8, rms_norm_eps=1e-05, rope_theta=1000000000.0, sliding_window=None, use_cache=True, vocab_size=131072, ) self.text_config = text_config self.multimodal_projector_bias = multimodal_projector_bias self.spatial_merge_size = spatial_merge_size
[docs] def get_partition_rules(self, *args, **kwargs): """ Get the partition rules for the model. Returns: `tp.Tuple[tp.Tuple[str, PartitionSpec]]`: The partition rules. """ pmag = self.partition_manager return ( (r"embed_tokens/embedding", pmag.resolve(ColumnWise)), (r"self_attn/(q_proj|k_proj|v_proj)/kernel", pmag.resolve(ColumnWise)), (r"self_attn/o_proj/kernel", pmag.resolve(RowWise)), (r"self_attn/.*proj/bias", pmag.resolve(Replicated)), (r"mlp/(gate_proj|up_proj)/kernel", pmag.resolve(ColumnWise)), (r"mlp/down_proj/kernel", pmag.resolve(RowWise)), (r"mlp/.*proj/bias", pmag.resolve(Replicated)), (r".*(input_layernorm|post_attention_layernorm|norm)/kernel", pmag.resolve(Replicated)), (r"lm_head/kernel", pmag.resolve(ColumnWise)), (r"score/kernel", pmag.resolve(RowWise)), (r".*bias", pmag.resolve(Replicated)), (r".*", pmag.resolve(Replicated)), )