
    gQ                     
   d Z ddlZddlmZ ddlmZmZmZmZm	Z	 erddl
mZ ddlmZ ddlmZ dd	lmZ dd
lmZ  ej        e          Z G d de          Z G d de          Z G d de          Z G d de          ZdS )zCLIP model configuration    NOrderedDict)TYPE_CHECKINGAnyMappingOptionalUnion   )ProcessorMixin)
TensorType)PretrainedConfig)
OnnxConfig)loggingc                        e Zd ZdZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd	Zedeee	j
        f         ddfd            Z xZS )CLIPTextConfiga  
    This is the configuration class to store the configuration of a [`CLIPTextModel`]. It is used to instantiate a CLIP
    text encoder according to the specified arguments, defining the model architecture. Instantiating a configuration
    with the defaults will yield a similar configuration to that of the text encoder of the CLIP
    [openai/clip-vit-base-patch32](https://huggingface.co/openai/clip-vit-base-patch32) 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 49408):
            Vocabulary size of the CLIP text model. Defines the number of different tokens that can be represented by
            the `inputs_ids` passed when calling [`CLIPModel`].
        hidden_size (`int`, *optional*, defaults to 512):
            Dimensionality of the encoder layers and the pooler layer.
        intermediate_size (`int`, *optional*, defaults to 2048):
            Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
        projection_dim (`int`, *optional*, defaults to 512):
            Dimensionality of text and vision projection layers.
        num_hidden_layers (`int`, *optional*, defaults to 12):
            Number of hidden layers in the Transformer encoder.
        num_attention_heads (`int`, *optional*, defaults to 8):
            Number of attention heads for each attention layer in the Transformer encoder.
        max_position_embeddings (`int`, *optional*, defaults to 77):
            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).
        hidden_act (`str` or `function`, *optional*, defaults to `"quick_gelu"`):
            The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
            `"relu"`, `"selu"` and `"gelu_new"` `"quick_gelu"` are supported.
        layer_norm_eps (`float`, *optional*, defaults to 1e-05):
            The epsilon used by the layer normalization layers.
        attention_dropout (`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.
        initializer_factor (`float`, *optional*, defaults to 1.0):
            A factor for initializing all weight matrices (should be kept to 1, used internally for initialization
            testing).
        pad_token_id (`int`, *optional*, defaults to 1):
            Padding token id.
        bos_token_id (`int`, *optional*, defaults to 49406):
            Beginning of stream token id.
        eos_token_id (`int`, *optional*, defaults to 49407):
            End of stream token id.

    Example:

    ```python
    >>> from transformers import CLIPTextConfig, CLIPTextModel

    >>> # Initializing a CLIPTextConfig with openai/clip-vit-base-patch32 style configuration
    >>> configuration = CLIPTextConfig()

    >>> # Initializing a CLIPTextModel (with random weights) from the openai/clip-vit-base-patch32 style configuration
    >>> model = CLIPTextModel(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```clip_text_model               M   
quick_geluh㈵>        {Gz?      ?       c                      t                      j        d|||d| || _        || _        || _        || _        || _        || _        || _        |	| _	        || _
        || _        || _        |
| _        d S )N)pad_token_idbos_token_ideos_token_id )super__init__
vocab_sizehidden_sizeintermediate_sizeprojection_dimnum_hidden_layersnum_attention_headsmax_position_embeddingslayer_norm_eps
hidden_actinitializer_rangeinitializer_factorattention_dropout)selfr(   r)   r*   r+   r,   r-   r.   r0   r/   r3   r1   r2   r"   r#   r$   kwargs	__class__s                    g/var/www/html/ai-engine/env/lib/python3.11/site-packages/transformers/models/clip/configuration_clip.pyr'   zCLIPTextConfig.__init__a   s    * 	sl\hsslrsss$&!2,!2#6 '>$,$!2"4!2    pretrained_model_name_or_pathreturnr   c                 N   |                      |            | j        |fi |\  }}|                    d          dk    r|d         }d|v rMt          | d          r=|d         | j        k    r,t
                              d|d          d| j         d            | j        |fi |S )N
model_typecliptext_configYou are using a model of type   to instantiate a model of type N. This is not supported for all configurations of models and can yield errors._set_token_in_kwargsget_config_dictgethasattrr<   loggerwarning	from_dictclsr9   r5   config_dicts       r7   from_pretrainedzCLIPTextConfig.from_pretrained   s      (((1c12OZZSYZZV ??<((F22%m4K;&&73+E+E&+VbJcgjguJuJuNNr\1J r r>r r r  
 s}[33F333r8   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    __name__
__module____qualname____doc__r<   r'   classmethodr	   strosPathLikerM   __classcell__r6   s   @r7   r   r   "   s        : :x #J  " %"3 "3 "3 "3 "3 "3H 4E#r{BR<S 4bt 4 4 4 [4 4 4 4 4r8   r   c                   |     e Zd ZdZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 d fd	Zedeee	j
        f         ddfd            Z xZS )CLIPVisionConfiga  
    This is the configuration class to store the configuration of a [`CLIPVisionModel`]. It is used to instantiate a
    CLIP vision encoder according to the specified arguments, defining the model architecture. Instantiating a
    configuration with the defaults will yield a similar configuration to that of the vision encoder of the CLIP
    [openai/clip-vit-base-patch32](https://huggingface.co/openai/clip-vit-base-patch32) 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.
        intermediate_size (`int`, *optional*, defaults to 3072):
            Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
        projection_dim (`int`, *optional*, defaults to 512):
            Dimensionality of text and vision projection layers.
        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.
        num_channels (`int`, *optional*, defaults to 3):
            The number of input channels.
        image_size (`int`, *optional*, defaults to 224):
            The size (resolution) of each image.
        patch_size (`int`, *optional*, defaults to 32):
            The size (resolution) of each patch.
        hidden_act (`str` or `function`, *optional*, defaults to `"quick_gelu"`):
            The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
            `"relu"`, `"selu"` and `"gelu_new"` `"quick_gelu"` are supported.
        layer_norm_eps (`float`, *optional*, defaults to 1e-05):
            The epsilon used by the layer normalization layers.
        attention_dropout (`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.
        initializer_factor (`float`, *optional*, defaults to 1.0):
            A factor for initializing all weight matrices (should be kept to 1, used internally for initialization
            testing).

    Example:

    ```python
    >>> from transformers import CLIPVisionConfig, CLIPVisionModel

    >>> # Initializing a CLIPVisionConfig with openai/clip-vit-base-patch32 style configuration
    >>> configuration = CLIPVisionConfig()

    >>> # Initializing a CLIPVisionModel (with random weights) from the openai/clip-vit-base-patch32 style configuration
    >>> model = CLIPVisionModel(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```clip_vision_model      r   r   r
          r   r   r   r   r   c                      t                      j        di | || _        || _        || _        || _        || _        || _        || _        || _	        || _
        || _        || _        |
| _        |	| _        d S )Nr%   )r&   r'   r)   r*   r+   r,   r-   num_channels
patch_size
image_sizer1   r2   r3   r/   r0   )r4   r)   r*   r+   r,   r-   ra   rc   rb   r0   r/   r3   r1   r2   r5   r6   s                  r7   r'   zCLIPVisionConfig.__init__   s    " 	""6"""&!2,!2#6 ($$!2"4!2,$r8   r9   r:   r   c                 N   |                      |            | j        |fi |\  }}|                    d          dk    r|d         }d|v rMt          | d          r=|d         | j        k    r,t
                              d|d          d| j         d            | j        |fi |S )Nr<   r=   vision_configr?   r@   rA   rB   rJ   s       r7   rM   z CLIPVisionConfig.from_pretrained   s      (((1c12OZZSYZZV ??<((F22%o6K;&&73+E+E&+VbJcgjguJuJuNNr\1J r r>r r r  
 s}[33F333r8   )r\   r]   r   r   r   r
   r^   r_   r   r   r   r   r   rN   rX   s   @r7   rZ   rZ      s        4 4l %J % % % % % %B 4E#r{BR<S 4bt 4 4 4 [4 4 4 4 4r8   rZ   c                   J     e Zd ZdZdZ	 d
 fd	Zededefd	            Z	 xZ
S )
CLIPConfigaN  
    [`CLIPConfig`] is the configuration class to store the configuration of a [`CLIPModel`]. It is used to instantiate
    a CLIP model according to the specified arguments, defining the text model and vision model configs. Instantiating
    a configuration with the defaults will yield a similar configuration to that of the CLIP
    [openai/clip-vit-base-patch32](https://huggingface.co/openai/clip-vit-base-patch32) 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 [`CLIPTextConfig`].
        vision_config (`dict`, *optional*):
            Dictionary of configuration options used to initialize [`CLIPVisionConfig`].
        projection_dim (`int`, *optional*, defaults to 512):
            Dimensionality of text and vision 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 CLIP implementation.
        kwargs (*optional*):
            Dictionary of keyword arguments.

    Example:

    ```python
    >>> from transformers import CLIPConfig, CLIPModel

    >>> # Initializing a CLIPConfig with openai/clip-vit-base-patch32 style configuration
    >>> configuration = CLIPConfig()

    >>> # Initializing a CLIPModel (with random weights) from the openai/clip-vit-base-patch32 style configuration
    >>> model = CLIPModel(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config

    >>> # We can also initialize a CLIPConfig from a CLIPTextConfig and a CLIPVisionConfig
    >>> from transformers import CLIPTextConfig, CLIPVisionConfig

    >>> # Initializing a CLIPText and CLIPVision configuration
    >>> config_text = CLIPTextConfig()
    >>> config_vision = CLIPVisionConfig()

    >>> config = CLIPConfig.from_text_vision_configs(config_text, config_vision)
    ```r=   Nr   /L
F@c                 "   |                     dd           }|                     dd           } t                      j        di | ||i }t          di |                                }|                                D ]G\  }	}
|	|v r>|
||	         k    r2|	dvr.|	|v r
d|	 d|	 d}nd|	 d}t                              |           H|                    |           ||i }t          di |                                }d	|v r'd
 |d	                                         D             |d	<   |                                D ]G\  }	}
|	|v r>|
||	         k    r2|	dvr.|	|v r
d|	 d|	 d}nd|	 d}t                              |           H|                    |           |i }t                              d           |i }t                              d           t          di || _
        t          di || _        || _        || _        d| _        d S )Ntext_config_dictvision_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.zj`text_config_dict` is provided which will be used to initialize `CLIPTextConfig`. The value `text_config["z"]` will be overridden.id2labelc                 4    i | ]\  }}t          |          |S r%   )rT   ).0keyvalues      r7   
<dictcomp>z'CLIPConfig.__init__.<locals>.<dictcomp>f  s1     3 3 3(2UCHHe3 3 3r8   zv` is found in both `vision_config_dict` and `vision_config` but with different values. The value `vision_config_dict["zp`vision_config_dict` is provided which will be used to initialize `CLIPVisionConfig`. The value `vision_config["zO`text_config` is `None`. Initializing the `CLIPTextConfig` with default values.zS`vision_config` is `None`. initializing the `CLIPVisionConfig` with default values.r   r%   )popr&   r'   r   to_dictitemsrG   infoupdaterZ   r>   re   r+   logit_scale_init_valuer2   )r4   r>   re   r+   ry   r5   rj   rk   _text_config_dictrq   rr   message_vision_config_dictr6   s                r7   r'   zCLIPConfig.__init__5  s    "::&8$??#ZZ(<dCC""6"""
 '"  !/ B B1A B B J J L L 05577 ) )
U+%%%;s3C*C*CSkHkHk...[ [ [<?[ [ [  P36P P P   KK((( 0111)$ " #3"H"H5G"H"H"P"P"R"R0003 36I*6U6[6[6]6]3 3 3#J/
 27799 ) )
U-''E]35G,G,GCWoLoLo000e e eFIe e e  V9<V V V   KK(((   !4555KKKijjj MKKmnnn)88K88->>>>,&<#"%r8   r>   re   c                 `     | d|                                 |                                 d|S )z
        Instantiate a [`CLIPConfig`] (or a derived class) from clip text model configuration and clip vision model
        configuration.

        Returns:
            [`CLIPConfig`]: An instance of a configuration object
        )r>   re   r%   )ru   )rK   r>   re   r5   s       r7   from_text_vision_configsz#CLIPConfig.from_text_vision_configs  s:     sf{2244MDYDYD[D[ff_efffr8   )NNr   rh   )rO   rP   rQ   rR   r<   r'   rS   r   rZ   r~   rW   rX   s   @r7   rg   rg     s        + +Z J `fV& V& V& V& V& V&p 	g> 	gRb 	g 	g 	g [	g 	g 	g 	g 	gr8   rg   c                       e Zd Zedeeeeef         f         fd            Zedeeeeef         f         fd            Zede	fd            Z
	 	 	 dddd	ed
eded         deeef         f
 fdZedefd            Z xZS )CLIPOnnxConfigr:   c           	      H    t          ddddfdddddd	fd
dddfg          S )N	input_idsbatchsequence)r   r   pixel_valuesra   heightwidth)r   r      r
   attention_maskr   r4   s    r7   inputszCLIPOnnxConfig.inputs  sN    'j99:WHQX!Y!YZ!w:#>#>?
 
 	
r8   c                 H    t          dddifdddifdddifdddifg          S )Nlogits_per_imager   r   logits_per_texttext_embedsimage_embedsr   r   s    r7   outputszCLIPOnnxConfig.outputs  sI    #a\2"QL1G-!W.	
 
 	
r8   c                     dS )Ng-C6?r%   r   s    r7   atol_for_validationz"CLIPOnnxConfig.atol_for_validation  s    tr8   N	processorr   
batch_size
seq_length	frameworkr   c                     t                                          |j        |||          }t                                          |j        ||          }i ||S )N)r   r   r   )r   r   )r&   generate_dummy_inputs	tokenizerimage_processor)r4   r   r   r   r   text_input_dictimage_input_dictr6   s          r7   r   z$CLIPOnnxConfig.generate_dummy_inputs  sn      ''77J:Yb 8 
 
 !7788%*	 9 
 
 7/6%566r8   c                     dS )N   r%   r   s    r7   default_onnx_opsetz!CLIPOnnxConfig.default_onnx_opset  s    rr8   )r   r   N)rO   rP   rQ   propertyr   rT   intr   r   floatr   r   r   r   r   rW   rX   s   @r7   r   r     sK       
WS#X%6 67 
 
 
 X
 
gc3h&7!78 
 
 
 X
 U    X ,07 7#7 7 	7
 L)7 
c	7 7 7 7 7 7 C    X    r8   r   )rR   rU   collectionsr   typingr   r   r   r   r	   processing_utilsr   utilsr   configuration_utilsr   onnxr   r   
get_loggerrO   rG   r   rZ   rg   r   r%   r8   r7   <module>r      s     				 # # # # # # ? ? ? ? ? ? ? ? ? ? ? ? ? ?  $222222###### 3 3 3 3 3 3             
	H	%	%s4 s4 s4 s4 s4% s4 s4 s4lj4 j4 j4 j4 j4' j4 j4 j4ZRg Rg Rg Rg Rg! Rg Rg Rgj+ + + + +Z + + + + +r8   