o
    ei                     @   s   d Z ddlmZ ddlmZ ddlmZ eeZ	G dd deZ
G dd	 d	eZG d
d deZG dd deZG dd deZg dZdS )zFLAVA model configurations    )Any   )PreTrainedConfig)loggingc                       s   e Zd ZdZdZdZ										
						d!dededededededededededededededef fdd Z	  Z
S )"FlavaImageConfiga  
    This is the configuration class to store the configuration of a [`FlavaImageModel`]. It is used to instantiate an
    FLAVA model according to the specified arguments, defining the model architecture.

    Instantiating a configuration with the defaults will yield a similar configuration to that of the FLAVA
    [facebook/flava-full](https://huggingface.co/facebook/flava-full) architecture.

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


    Args:
        hidden_size (`int`, *optional*, defaults to 768):
            Dimensionality of the encoder layers and the pooler layer.
        num_hidden_layers (`int`, *optional*, defaults to 12):
            Number of hidden layers in the Transformer encoder.
        num_attention_heads (`int`, *optional*, defaults to 12):
            Number of attention heads for each attention layer in the Transformer encoder.
        intermediate_size (`int`, *optional*, defaults to 3072):
            Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
        hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`):
            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_probs_dropout_prob (`float`, *optional*, defaults to 0.0):
            The dropout ratio for the attention probabilities.
        initializer_range (`float`, *optional*, defaults to 0.02):
            The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
        layer_norm_eps (`float`, *optional*, defaults to 1e-12):
            The epsilon used by the layer normalization layers.
        image_size (`int`, *optional*, defaults to 224):
            The size (resolution) of each image.
        patch_size (`int`, *optional*, defaults to 16):
            The size (resolution) of each patch.
        num_channels (`int`, *optional*, defaults to 3):
            The number of input channels.
        qkv_bias (`bool`, *optional*, defaults to `True`):
            Whether to add a bias to the queries, keys and values.
        mask_token (`bool`, *optional*, defaults to `True`):
            Whether to use a mask token or not. Used in MIM (Masked Image Modeling) loss for FLAVA.
        vocab_size (`int`, *optional*, defaults to 8192):
            Vocabulary size of the [`FlavaImageCodebook`] used in conjunction with [`FlavaImageModel`] for MIM (Masked
            Image Modeling) loss for FLAVA.

    Example:

    ```python
    >>> from transformers import FlavaImageConfig, FlavaImageModel

    >>> # Initializing a FlavaImageModel with  style configuration
    >>> configuration = FlavaImageConfig()

    >>> # Initializing a FlavaImageModel model (with random weights) from the style configuration
    >>> model = FlavaImageModel(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```flava_image_modelimage_config         gelu        {Gz?-q=      r   T    hidden_sizenum_hidden_layersnum_attention_headsintermediate_size
hidden_acthidden_dropout_probattention_probs_dropout_probinitializer_rangelayer_norm_eps
image_size
patch_sizenum_channelsqkv_bias
mask_token
vocab_sizec                    sp   t  jdi | || _|| _|| _|| _|| _|| _|| _|| _	|	| _
|
| _|| _|| _|| _|| _|| _d S N )super__init__r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   )selfr   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   kwargs	__class__r#   k/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/transformers/models/flava/configuration_flava.pyr%   Y   s    
zFlavaImageConfig.__init__)r	   r
   r
   r   r   r   r   r   r   r   r   r   TTr   __name__
__module____qualname____doc__
model_typebase_config_keyintfloatboolr%   __classcell__r#   r#   r(   r*   r      sd    <	
r   c                       s   e Zd ZdZdZdZ									
						d dededededededededededededede	f fddZ
  ZS )!FlavaTextConfiga  
    This is the configuration class to store the configuration of a [`FlavaTextModel`]. It is used to instantiate an
    FLAVA model according to the specified arguments, defining the model architecture.

    Instantiating a configuration with the defaults will yield a similar configuration to that of the FLAVA
    [facebook/flava-full](https://huggingface.co/facebook/flava-full) architecture.

    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 30522):
            Vocabulary size of the BERT model. Defines the number of different tokens that can be represented by the
            `inputs_ids` passed when calling [`FlavaTextModel`].
        type_vocab_size (`int`, *optional*, defaults to 2):
            The vocabulary size of the `token_type_ids` passed when calling [`FlavaTextModel`]. Note that even though
            text encoder allows `token_type_ids`'s value as 2, for text-only pretraining and fine-tuning, only 1 is
            used similar to RoBERTa.
        max_position_embeddings (`int`, *optional*, defaults to 512):
            The maximum sequence length that this model might ever be used with. Typically set this to something large
            just in case (e.g., 512 or 1024 or 2048). For VL, max_length passed to model is 77.
        hidden_size (`int`, *optional*, defaults to 768):
            Dimensionality of the encoder layers and the pooler layer.
        num_hidden_layers (`int`, *optional*, defaults to 12):
            Number of hidden layers in the Transformer encoder.
        num_attention_heads (`int`, *optional*, defaults to 12):
            Number of attention heads for each attention layer in the Transformer encoder.
        intermediate_size (`int`, *optional*, defaults to 3072):
            Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
        hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`):
            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.1):
            The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
        attention_probs_dropout_prob (`float`, *optional*, defaults to 0.1):
            The dropout ratio for the attention probabilities.
        initializer_range (`float`, *optional*, defaults to 0.02):
            The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
        layer_norm_eps (`float`, *optional*, defaults to 1e-12):
            The epsilon used by the layer normalization layers.
        image_size (`int`, *optional*, defaults to 224):
            The size (resolution) of each image.
        patch_size (`int`, *optional*, defaults to 16):
            The size (resolution) of each patch.
        num_channels (`int`, *optional*, defaults to 3):
            The number of input channels.
        qkv_bias (`bool`, *optional*, defaults to `True`):
            Whether to add a bias to the queries, keys and values.

    Example:

    ```python
    >>> from transformers import FlavaTextConfig, FlavaTextModel

    >>> # Initializing a FlavaTextModel with  style configuration
    >>> configuration = FlavaTextConfig()

    >>> # Initializing a FlavaTextModel model (with random weights) from the style configuration
    >>> model = FlavaTextModel(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```flava_text_modeltext_config:w        r	   r
   r   r   r   r   r   r   Tr!   type_vocab_sizemax_position_embeddingsr   r   r   r   r   r   r   r   r   pad_token_idr   c                    sj   t  jdi | || _|| _|| _|| _|| _|| _|| _|| _	|	| _
|
| _|| _|| _|| _|| _d S r"   )r$   r%   r!   r<   r=   r   r   r   r   r   r   r   r   r   r   r>   )r&   r!   r<   r=   r   r   r   r   r   r   r   r   r   r>   r   r'   r(   r#   r*   r%      s   
zFlavaTextConfig.__init__)r9   r:   r;   r	   r
   r
   r   r   r   r   r   r   r   T)r,   r-   r.   r/   r0   r1   r2   strr3   r4   r%   r5   r#   r#   r(   r*   r6      s^    A	
r6   c                       sn   e Zd ZdZdZdZ										
			ddededededededededededef fddZ	  Z
S )FlavaMultimodalConfiga  
    This is the configuration class to store the configuration of a [`FlavaMultimodalModel`]. It is used to instantiate
    an FLAVA model according to the specified arguments, defining the model architecture.

    Instantiating a configuration with the defaults will yield a similar configuration to that of the FLAVA
    [facebook/flava-full](https://huggingface.co/facebook/flava-full) architecture.

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


    Args:
        hidden_size (`int`, *optional*, defaults to 768):
            Dimensionality of the encoder layers and the pooler layer.
        num_hidden_layers (`int`, *optional*, defaults to 6):
            Number of hidden layers in the Transformer encoder.
        num_attention_heads (`int`, *optional*, defaults to 12):
            Number of attention heads for each attention layer in the Transformer encoder.
        intermediate_size (`int`, *optional*, defaults to 3072):
            Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
        hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`):
            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_probs_dropout_prob (`float`, *optional*, defaults to 0.0):
            The dropout ratio for the attention probabilities.
        initializer_range (`float`, *optional*, defaults to 0.02):
            The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
        layer_norm_eps (`float`, *optional*, defaults to 1e-12):
            The epsilon used by the layer normalization layers.
        qkv_bias (`bool`, *optional*, defaults to `True`):
            Whether to add a bias to the queries, keys and values.
        use_cls_token (`bool`, *optional*, defaults to `True`):
            Whether to use an extra CLS token for multimodal settings. Usually needed by the FLAVA model.


    Example:

    ```python
    >>> from transformers import FlavaMultimodalConfig, FlavaMultimodalModel

    >>> # Initializing a FlavaMultimodalModel with  style configuration
    >>> configuration = FlavaMultimodalConfig()

    >>> # Initializing a FlavaMultimodalModel model (with random weights) from the style configuration
    >>> model = FlavaMultimodalModel(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```flava_multimodal_modelmultimodal_configr	      r
   r   r   r   r   r   Tr   r   r   r   r   r   r   r   r   r   use_cls_tokenc                    sX   t  jdi | || _|| _|| _|| _|| _|| _|| _|| _	|	| _
|
| _|| _d S r"   )r$   r%   r   r   r   r   r   r   r   r   r   r   rD   )r&   r   r   r   r   r   r   r   r   r   r   rD   r'   r(   r#   r*   r%      s   
zFlavaMultimodalConfig.__init__)r	   rC   r
   r   r   r   r   r   r   TTr+   r#   r#   r(   r*   r@      sL    4	
r@   c                       sT   e Zd ZdZdZ	 								dd
ededededededef fddZ  ZS )FlavaImageCodebookConfigflava_image_codebookimage_codebook_config   r   r:      r   Tr   
num_groupsinput_channelsnum_blocks_per_groupr   r!   freezer   c           	         s@   t  jdi | || _|| _|| _|| _|| _|| _|| _d S r"   )	r$   r%   rJ   rK   rL   r   r!   rM   r   )	r&   rJ   rK   rL   r   r!   rM   r   r'   r(   r#   r*   r%   m  s   
z!FlavaImageCodebookConfig.__init__)rH   r   r:   rI   r   Tr   )	r,   r-   r.   r0   r1   r2   r3   r%   r5   r#   r#   r(   r*   rE   >  s4    -rE   c                ,       s   e Zd ZdZdZeeeedZ													
										d#de
eef dB de
eef dB de
eef dB de
eef dB dedededededededededededededededed edB f* fd!d"Z  ZS )$FlavaConfiga$  
    [`FlavaConfig`] is the configuration class to store the configuration of a [`FlavaModel`]. It is used to
    instantiate FLAVA model according to the specified arguments, defining the text model, image model, image codebook
    and multimodal model configs. Instantiating a configuration with the defaults will yield a similar configuration to
    that of the FLAVA [facebook/flava-full](https://huggingface.co/facebook/flava-full) architecture.

    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 (`dict`, *optional*):
            Dictionary of configuration options used to initialize [`FlavaTextConfig`].
        image_config (`dict`, *optional*):
            Dictionary of configuration options used to initialize [`FlavaImageConfig`].
        multimodal_config (`dict`, *optional*):
            Dictionary of configuration options used to initialize [`FlavaMultimodalConfig`].
        hidden_size (`int`, *optional*, defaults to 768):
            Dimensionality of the encoder layers and the pooler layer.
        layer_norm_eps (`float`, *optional*, defaults to 1e-12):
            The epsilon used by the layer normalization layers.
        projection_dim (`int`, *optional*, defaults to 512):
            Dimensionality of text and image projection layers.
        logit_scale_init_value (`float`, *optional*, defaults to 2.6592):
            The initial value of the *logit_scale* parameter. Default is used as per the original FLAVA/CLIP
            implementation.
        initializer_range (`float`, *optional*, defaults to 0.02):
            The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
        ce_ignore_index (`int`, *optional*, defaults to -100):
            Cross entropy index to ignore.
        mim_weight (`float`, *optional*, defaults to 1.0):
            Weight to be assigned to MIM (Masked Image Modeling) unimodal loss
        mlm_weight (`float`, *optional*, defaults to 1.0):
            Weight to be assigned to MLM (Masked Language Modeling) unimodal loss
        global_contrastive_weight (`float`, *optional*, defaults to 1.0):
            Weight to be assigned to global contrastive cross-alignment loss.
        itm_weight (`float`, *optional*, defaults to 1.0):
            Weight to be assigned to image-text matching multimodal loss.
        mmm_image_weight (`float`, *optional*, defaults to 1.0):
            Weight to be assigned to MMM loss's image part.
        mmm_text_weight (`float`, *optional*, defaults to 1.0):
            Weight to be assigned to MMM loss's text part.
        global_backprop_contrastive (`bool`, *optional*, defaults to `True`):
            Whether to use global backpropgation through all workers in contrastive loss.
        skip_unmasked_multimodal_encoder (`bool`, *optional*, defaults to `True`):
            Whether to skip running unmasked multimodal encoder whose outputs are not used by FLAVA losses.
        return_loss (`bool`, *optional*, defaults to `True`):
            Whether to return loss or not
        tie_word_embeddings (`bool`, *optional*, defaults to `True`):
            Whether to tie weight embeddings

    Example:

    ```python
    >>> from transformers import FlavaConfig, FlavaModel, FlavaForPreTraining

    >>> # Initializing a FlavaConfig with style configuration
    >>> configuration = FlavaConfig()

    >>> # Initializing a FlavaModel and FlavaForPreTraining model (with random weights) from the style configuration
    >>> model = FlavaModel(configuration)
    >>> model_pre = FlavaForPreTraining(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config
    >>> configuration_pre = model_pre.config
    ```
    flava)r8   r   rB   rG   Nr	   r   T/L
F@r         ?r   r8   rB   rG   r   r   projection_diminit_codebooklogit_scale_init_valuer   ce_ignore_index
mim_weight
mlm_weightglobal_contrastive_weight
itm_weightmmm_image_weightmmm_text_weightglobal_backprop_contrastive skip_unmasked_multimodal_encoderreturn_losstie_word_embeddingsc           "         s  | dd }| dd }| dd }| dd }|d ur`|d u r"i }tdi | }| D ]+\}}||v rZ||| krZ|dkrZ||v rOd| d| d}nd	| d
}t| q/|| |d ur|d u rji }tdi | }d|v rdd |d  D |d< | D ]+\}}||v r||| kr|dkr||v rd| d| d}nd| d
}t| q|| |d ur|d u ri }tdi | } |  D ]+\}}||v r||| kr|dkr||v rd| d| d}nd| d
}t| q||  |d urQ|d u ri }t	di | }!|! D ]0\}}||v rJ||| krJ|dkrJ||v r?d| d| d}nd| d
}t| q||! |d u r_t }td nt
|trltdi |}|d u rzt }td nt
|trtdi |}|d u rt }td nt
|trtdi |}|d u rt	 }td nt
|trt	di |}|| _|| _|| _|| _|| _|| _|| _|| _|
| _|	| _d| _|| _|| _|| _|| _|| _|| _|| _|| _|| _|| _ || _!t" j#di | d S )Ntext_config_dictimage_config_dictmultimodal_config_dictimage_codebook_config_dicttransformers_version`zp` is found in both `text_config_dict` and `text_config` but with different values. The value `text_config_dict["z"]` will be used instead.zk`text_config_dict` is provided which will be used to initialize `FlavaTextConfig`. The value `text_config["z"]` will be overridden.id2labelc                 S   s   i | ]	\}}t ||qS r#   )r?   ).0keyvaluer#   r#   r*   
<dictcomp>  s    z(FlavaConfig.__init__.<locals>.<dictcomp>zs` is found in both `image_config_dict` and `image_config` but with different values. The value `image_config_dict["zn`image_config_dict` is provided which will be used to initialize `FlavaImageConfig`. The value `image_config["z` is found in both `multimodal_config_dict` and `multimodal_config` but with different values. The value `multimodal_config_dict["z}`multimodal_config_dict` is provided which will be used to initialize `FlavaMultimodalConfig`. The value `multimodal_config["z` is found in both `image_codebook_config_dict` and `image_codebook_config` but with different values. The value `image_codebook_config_dict["z`image_codebook_config_dict` is provided which will be used to initialize `FlavaImageCodebookConfig`. The value `image_codebook_config["zP`text_config` is `None`. initializing the `FlavaTextConfig` with default values.zR`image_config` is `None`. initializing the `FlavaImageConfig` with default values.zW`image_config` is `None`. initializing the `FlavaMultimodalConfig` with default values.zZ`image_config` is `None`. initializing the `FlavaImageCodebookConfig` with default values.rR   r#   )$popr6   to_dictitemsloggerinfoupdater   r@   rE   
isinstancedictr8   r   rB   rG   rS   rT   r   r   r   rU   initializer_factorrV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   r$   r%   )"r&   r   r8   rB   rG   r   r   rS   rT   rU   r   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   r'   ra   rb   rc   rd   _text_config_dictri   rj   message_image_config_dict_multimodal_config_dict_image_codebook_config_dictr(   r#   r*   r%     s   



















zFlavaConfig.__init__)NNNNr	   r   r	   TrP   r   rQ   rR   rR   rR   rR   rR   rR   TTTT)r,   r-   r.   r/   r0   r6   r   r@   rE   sub_configsrs   r?   r   r2   r3   r4   r%   r5   r#   r#   r(   r*   rN     s    D		
rN   )rN   rE   r   r@   r6   N)r/   typingr   configuration_utilsr   utilsr   
get_loggerr,   ro   r   r6   r@   rE   rN   __all__r#   r#   r#   r*   <module>   s   
fiVD  