a
    ½ÀhýD  ã                   @   sT   d dl mZ d dlmZ G dd„ deƒZG dd„ deƒZG dd„ deƒZddgZd	S )
é   )ÚPretrainedConfig)Úrope_config_validationc                       s*   e Zd ZdZdZdZd‡ fdd„	Z‡  ZS )ÚGlm4vVisionConfiga  
    This is the configuration class to store the configuration of a [`Glm4vVisionModel`]. It is used to instantiate an Glm4vVisionModel
    model according to the specified arguments, defining the model architecture. Instantiating a configuration with the defaults will yield
    a similar configuration to that of
    GLM-4.1V-9B-Thinking [THUDM/GLM-4.1V-9B-Thinking](https://huggingface.co/THUDM/GLM-4.1V-9B-Thinking).

    Args:
        hidden_size (`int`, *optional*, defaults to 1536):
            Dimensionality of the encoder layers and the pooler layer.
        depth (`int`, *optional*, defaults to 24):
            Number of layers (depth) in the model.
        attention_bias (`bool`, *optional*, defaults to `False`):
            Whether to add a bias to the queries, keys and values.
        intermediate_size (`int`, *optional*, defaults to 13696):
            Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
        hidden_act (`str` or `function`, *optional*, defaults to `"selu"`):
            The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
            `"relu"`, `"selu"` and `"gelu_new"` are supported.
        hidden_dropout_prob (`float`, *optional*, defaults to 0.0):
            The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
        attention_dropout (`float`, *optional*, defaults to 0.0):
            Dropout probability for attention weights.
        projection_dropout (`float`, *optional*, defaults to 0.0):
            Dropout probability for the projection layer.
        initializer_range (`float`, *optional*, defaults to 0.02):
            The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
        image_size (`int` or `list[int]`, *optional*, defaults to `[336, 336]`):
            The size (resolution) of each image.
        patch_size (`int`, *optional*, defaults to `14`):
            The size (resolution) of each patch.
        num_channels (`int`, *optional*, defaults to 3):
            The number of input channels.
        out_hidden_size (`int`, *optional*, defaults to 4096):
            The output hidden size of the vision model.
        rms_norm_eps (`float`, *optional*, defaults to 1e-05):
            The epsilon used by the rms normalization layers.
        spatial_merge_size (`int`, *optional*, defaults to 2):
            The size used for merging spatial dimensions.
        temporal_patch_size (`int`, *optional*, defaults to 2):
            The size used for patches along the temporal dimension.
    Example:

    ```python
    >>> from transformers import Glm4vVisionConfig, Glm4vVisionModel

    >>> # Initializing a Glm4vVisionConfig GLM-4.1V-9B style configuration
    >>> configuration = Glm4vVisionConfig()

    >>> # Initializing a model (with random weights) from the GLM-4.1V-9B configuration
    >>> model = Glm4vVisionModel(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```Úglm4vÚvision_configé   é   ÚsiluFç        é   r   éP  é   çñhãˆµøä>é   é   é€5  ç{®Gáz”?c                    sp   t ƒ jf i |¤Ž || _|| _|| _|| _|| _|| _|	| _|| _	|| _
|| _|| _|| _|
| _|| _|| _d S )N)ÚsuperÚ__init__ÚdepthÚhidden_sizeÚ
hidden_actÚ	num_headsÚin_channelsÚ
image_sizeÚ
patch_sizeÚspatial_merge_sizeÚtemporal_patch_sizeÚout_hidden_sizeÚintermediate_sizeÚinitializer_rangeÚrms_norm_epsÚattention_biasÚattention_dropout)Úselfr   r   r   r"   r#   r   r   r   r   r!   r   r   r   r   r    Úkwargs©Ú	__class__© úi/var/www/html/assistant/venv/lib/python3.9/site-packages/transformers/models/glm4v/configuration_glm4v.pyr   T   s     zGlm4vVisionConfig.__init__)r   r   r	   Fr
   r   r   r   r   r   r   r   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú
model_typeÚbase_config_keyr   Ú__classcell__r(   r(   r&   r)   r      s&   7               ðr   c                       sh   e Zd ZdZdZdZdgZddddddd	œZd
gdgfddgdgfdgdgfdœZd ‡ fdd„	Z	‡  Z
S )!ÚGlm4vTextConfigaû  
    This is the configuration class to store the configuration of a [`Glm4vModel`]. It is used to instantiate a
    GLM-4.1V model according to the specified arguments, defining the model architecture. Instantiating a
    configuration with the defaults will yield a similar configuration to that of
    GLM-4.1V-9B-Thinking [THUDM/GLM-4.1V-9B-Thinking](https://huggingface.co/THUDM/GLM-4.1V-9B-Thinking).

    Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
    documentation from [`PretrainedConfig`] for more information.

    Args:
        vocab_size (`int`, *optional*, defaults to 151552):
            Vocabulary size of the Glm4v model. Defines the number of different tokens that can be represented by the
            `inputs_ids` passed when calling [`Glm4vModel`]
        hidden_size (`int`, *optional*, defaults to 4096):
            Dimension of the hidden representations.
        intermediate_size (`int`, *optional*, defaults to 13696):
            Dimension of the MLP representations.
        num_hidden_layers (`int`, *optional*, defaults to 40):
            Number of hidden layers in the Transformer encoder.
        num_attention_heads (`int`, *optional*, defaults to 32):
            Number of attention heads for each attention layer in the Transformer encoder.
        num_key_value_heads (`int`, *optional*, defaults to 2):
            This is the number of key_value heads that should be used to implement Grouped Query Attention. If
            `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
            `num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When
            converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
            by meanpooling all the original heads within that group. For more details checkout [this
            paper](https://huggingface.co/papers/2305.13245). If it is not specified, will default to `32`.
        hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
            The non-linear activation function (function or string) in the decoder.
        max_position_embeddings (`int`, *optional*, defaults to 32768):
            The maximum sequence length that this model might ever be used with.
        initializer_range (`float`, *optional*, defaults to 0.02):
            The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
        rms_norm_eps (`float`, *optional*, defaults to 1e-05):
            The epsilon used by the rms normalization layers.
        use_cache (`bool`, *optional*, defaults to `True`):
            Whether or not the model should return the last key/values attentions (not used by all models). Only
            relevant if `config.is_decoder=True`.
        tie_word_embeddings (`bool`, *optional*, defaults to `False`):
            Whether the model's input and output word embeddings should be tied.
        rope_theta (`float`, *optional*, defaults to 10000.0):
            The base period of the RoPE embeddings.
        attention_dropout (`float`, *optional*, defaults to 0.0):
            The dropout ratio for the attention probabilities.
        rope_scaling (`Dict`, *optional*):
            Dictionary containing the scaling configuration for the RoPE embeddings. NOTE: if you apply new rope type
            and you expect the model to work on longer `max_position_embeddings`, we recommend you to update this value
            accordingly.
            Expected contents:
                `rope_type` (`str`):
                    The sub-variant of RoPE to use. Can be one of ['default', 'linear', 'dynamic', 'yarn', 'longrope',
                    'llama3'], with 'default' being the original RoPE implementation.
                `factor` (`float`, *optional*):
                    Used with all rope types except 'default'. The scaling factor to apply to the RoPE embeddings. In
                    most scaling types, a `factor` of x will enable the model to handle sequences of length x *
                    original maximum pre-trained length.
                `original_max_position_embeddings` (`int`, *optional*):
                    Used with 'dynamic', 'longrope' and 'llama3'. The original max position embeddings used during
                    pretraining.
                `attention_factor` (`float`, *optional*):
                    Used with 'yarn' and 'longrope'. The scaling factor to be applied on the attention
                    computation. If unspecified, it defaults to value recommended by the implementation, using the
                    `factor` field to infer the suggested value.
        image_token_id (`int`, *optional*):
            Token index used as placeholder for image embeddings.
        video_token_id (`int`, *optional*):
            Token index used as placeholder for video embeddings.

    ```python
    >>> from transformers import Glm4vTextModel, Glm4vConfig

    >>> # Initializing a GLM-4.1V style configuration
    >>> configuration = Glm4vConfig()

    >>> # Initializing a model from the GLM-4.1V style configuration
    >>> model = Glm4vTextModel(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```Z
glm4v_textÚtext_configÚpast_key_valuesZcolwiseZrowwiseZcolwise_repZrowwise_rep)zlayers.*.self_attn.q_projzlayers.*.self_attn.k_projzlayers.*.self_attn.v_projzlayers.*.self_attn.o_projzlayers.*.mlp.gate_up_projzlayers.*.mlp.down_projZ	input_idsZinputs_embedsZhidden_statesZattention_mask)Zembed_tokensZlayersZnormé P r   r   é(   é    r   r	   é €  r   r   TFç     ˆÃ@r
   Nc                    s¸   || _ || _|| _|| _|| _|| _|d u r0|}|| _|| _|	| _|
| _	|| _
|| _|| _|| _| jd ur„d| jv r„| jd | jd< t| dhd || _|| _tƒ jf d|i|¤Ž d S )NÚtypeZ	rope_typeZmrope_section)Zignore_keysÚtie_word_embeddings)Ú
vocab_sizeÚmax_position_embeddingsr   r   Únum_hidden_layersÚnum_attention_headsÚnum_key_value_headsr   r    r!   Ú	use_cacheÚ
rope_thetar#   Úrope_scalingr   Úimage_token_idÚvideo_token_idr   r   )r$   r;   r   r   r=   r>   r?   r   r<   r    r!   r@   r:   rA   r#   rB   rC   rD   r%   r&   r(   r)   r   ß   s,    zGlm4vTextConfig.__init__)r4   r   r   r5   r6   r   r	   r7   r   r   TFr8   r
   NNN)r*   r+   r,   r-   r.   r/   Úkeys_to_ignore_at_inferenceZbase_model_tp_planZbase_model_pp_planr   r0   r(   r(   r&   r)   r1   z   sB   Rú	

ý                 îr1   c                       s6   e Zd ZdZdZeedœZdgZd‡ fdd„	Z	‡  Z
S )ÚGlm4vConfiga\  
    This is the configuration class to store the configuration of a [`Glm4vModel`]. It is used to instantiate a
    GLM-4.1V model according to the specified arguments, defining the model architecture. Instantiating a
    configuration with the defaults will yield a similar configuration to that of
    GLM-4.1V-9B-Thinking [THUDM/GLM-4.1V-9B-Thinking](https://huggingface.co/THUDM/GLM-4.1V-9B-Thinking).

    Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
    documentation from [`PretrainedConfig`] for more information.


    Args:
        text_config (`Union[PreTrainedConfig, dict]`, *optional*, defaults to `Glm4vTextConfig`):
            The config object or dictionary of the text backbone.
        vision_config (`Union[PreTrainedConfig, dict]`,  *optional*, defaults to `Glm4vVisionConfig`):
            The config object or dictionary of the vision backbone.
        image_token_id (`int`, *optional*, defaults to 151343):
            The image token index to encode the image prompt.
        video_token_id (`int`, *optional*, defaults to 151344):
            The video token index to encode the image prompt.
        image_start_token_id (`int`, *optional*, defaults to 151339):
            The image start token index to encode the start of image.
        image_end_token_id (`int`, *optional*, defaults to 151340):
            The image end token index to encode the end of image.
        video_start_token_id (`int`, *optional*, defaults to 151341):
            The video start token index to encode the start of video.
        video_end_token_id (`int`, *optional*, defaults to 151342):
            The video end token index to encode the end of video.

    ```python
    >>> from transformers import Glm4vForConditionalGeneration, Glm4vConfig

    >>> # Initializing a GLM-4.1V style configuration
    >>> configuration = Glm4vConfig()

    >>> # Initializing a model from the GLM-4.1V style configuration
    >>> model = Glm4vForConditionalGeneration(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```r   )r   r2   r3   Né/O é0O é+O é,O é-O é.O c	           
         s²   t ƒ jf i |	¤Ž t|tƒr4| jd f i |¤Ž| _n|d u rJ| jd ƒ | _t|tƒrl| jd f i |¤Ž| _n|d u rŠ| jd f i |	¤Ž| _|| _|| _|| _	|| _
|| _|| _d S )Nr   r2   )r   r   Ú
isinstanceÚdictÚsub_configsr   r2   rC   rD   Úvideo_start_token_idÚvideo_end_token_idÚimage_start_token_idÚimage_end_token_id)
r$   r2   r   rC   rD   rR   rS   rP   rQ   r%   r&   r(   r)   r   A  s    

zGlm4vConfig.__init__)NNrG   rH   rI   rJ   rK   rL   )r*   r+   r,   r-   r.   r   r1   rO   rE   r   r0   r(   r(   r&   r)   rF     s   )
        ÷rF   N)Zconfiguration_utilsr   Zmodeling_rope_utilsr   r   r1   rF   Ú__all__r(   r(   r(   r)   Ú<module>   s   a N