a
    d"B                     @   s   d Z ddlZddlZddlmZ ddlmZ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eZdddd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dS )z OWL-ViT model configuration    NOrderedDict)TYPE_CHECKINGAnyDictMappingOptionalUnion   )ProcessorMixin)
TensorType)PretrainedConfig)
OnnxConfig)loggingzJhttps://huggingface.co/google/owlvit-base-patch32/resolve/main/config.jsonzJhttps://huggingface.co/google/owlvit-base-patch16/resolve/main/config.jsonzKhttps://huggingface.co/google/owlvit-large-patch14/resolve/main/config.json)zgoogle/owlvit-base-patch32zgoogle/owlvit-base-patch16zgoogle/owlvit-large-patch14c                       sD   e Zd ZdZdZd fdd	Zeeee	j
f ddddZ  ZS )OwlViTTextConfiga  
    This is the configuration class to store the configuration of an [`OwlViTTextModel`]. It is used to instantiate an
    OwlViT 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 OwlViT
    [google/owlvit-base-patch32](https://huggingface.co/google/owlvit-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 OWL-ViT text model. Defines the number of different tokens that can be represented
            by the `inputs_ids` passed when calling [`OwlViTTextModel`].
        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.
        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 16):
            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-5):
            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):
            A factor for initializing all weight matrices (should be kept to 1, used internally for initialization
            testing).

    Example:

    ```python
    >>> from transformers import OwlViTTextConfig, OwlViTTextModel

    >>> # Initializing a OwlViTTextModel with google/owlvit-base-patch32 style configuration
    >>> configuration = OwlViTTextConfig()

    >>> # Initializing a OwlViTTextConfig from the google/owlvit-base-patch32 style configuration
    >>> model = OwlViTTextModel(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```Zowlvit_text_model                  
quick_geluh㈵>        {Gz?      ?r       c                    s`   t  jf |||d| || _|| _|| _|| _|| _|| _|| _|| _	|	| _
|
| _|| _d S )N)pad_token_idbos_token_ideos_token_id)super__init__
vocab_sizehidden_sizeintermediate_sizenum_hidden_layersnum_attention_headsmax_position_embeddings
hidden_actlayer_norm_epsattention_dropoutinitializer_rangeinitializer_factor)selfr#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r   r   r    kwargs	__class__ x/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/transformers/models/owlvit/configuration_owlvit.pyr"   `   s    zOwlViTTextConfig.__init__r   pretrained_model_name_or_pathreturnc                 K   s|   | j |fi |\}}|ddkr,|d }d|v rjt| drj|d | jkrjtd|d  d| j d | j|fi |S )N
model_typeowlvit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.get_config_dictgethasattrr7   loggerwarning	from_dictclsr5   r/   config_dictr2   r2   r3   from_pretrained   s     z OwlViTTextConfig.from_pretrained)r   r   r   r   r   r   r   r   r   r   r   r   r   r   __name__
__module____qualname____doc__r7   r"   classmethodr	   strosPathLikerG   __classcell__r2   r2   r0   r3   r   )   s&   4               r   c                       sD   e Zd ZdZdZd fdd	Zeeee	j
f ddddZ  ZS )OwlViTVisionConfigag  
    This is the configuration class to store the configuration of an [`OwlViTVisionModel`]. It is used to instantiate
    an OWL-ViT image 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 OWL-ViT
    [google/owlvit-base-patch32](https://huggingface.co/google/owlvit-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.
        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):
            Number of channels in the input images.
        image_size (`int`, *optional*, defaults to 768):
            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-5):
            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):
            A factor for initializing all weight matrices (should be kept to 1, used internally for initialization
            testing).

    Example:

    ```python
    >>> from transformers import OwlViTVisionConfig, OwlViTVisionModel

    >>> # Initializing a OwlViTVisionModel with google/owlvit-base-patch32 style configuration
    >>> configuration = OwlViTVisionConfig()

    >>> # Initializing a OwlViTVisionModel model from the google/owlvit-base-patch32 style configuration
    >>> model = OwlViTVisionModel(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```Zowlvit_vision_model      r   r
       r   r   r   r   r   c                    s^   t  jf i | || _|| _|| _|| _|| _|| _|| _|| _	|	| _
|
| _|| _|| _d S )N)r!   r"   r$   r%   r&   r'   num_channels
image_size
patch_sizer)   r*   r+   r,   r-   )r.   r$   r%   r&   r'   rV   rW   rX   r)   r*   r+   r,   r-   r/   r0   r2   r3   r"      s    zOwlViTVisionConfig.__init__r   r4   c                 K   s|   | j |fi |\}}|ddkr,|d }d|v rjt| drj|d | jkrjtd|d  d| j d | j|fi |S )Nr7   r8   vision_configr:   r;   r<   r=   rD   r2   r2   r3   rG      s     z"OwlViTVisionConfig.from_pretrained)rS   rT   r   r   r
   rS   rU   r   r   r   r   r   rH   r2   r2   r0   r3   rR      s"   4            rR   c                       sd   e Zd ZdZdZdZd fdd	Zeee	e
jf d	d
ddZeeedddZdd Z  ZS )OwlViTConfiga   
    [`OwlViTConfig`] is the configuration class to store the configuration of an [`OwlViTModel`]. It is used to
    instantiate an OWL-ViT 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 OWL-ViT
    [google/owlvit-base-patch32](https://huggingface.co/google/owlvit-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 [`OwlViTTextConfig`].
        vision_config (`dict`, *optional*):
            Dictionary of configuration options used to initialize [`OwlViTVisionConfig`].
        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 inital value of the *logit_scale* parameter. Default is used as per the original OWL-ViT
            implementation.
        kwargs (*optional*):
            Dictionary of keyword arguments.
    r8   TNr   /L
F@c                    sz   t  jf i | |d u r(i }td |d u r>i }td tf i || _tf i || _|| _|| _	|| _
d| _d S )NzKtext_config is None. Initializing the OwlViTTextConfig with default values.zOvision_config is None. initializing the OwlViTVisionConfig with default values.r   )r!   r"   rA   infor   r9   rR   rY   projection_dimlogit_scale_init_valuereturn_dictr-   )r.   r9   rY   r]   r^   r_   r/   r0   r2   r3   r"     s    	

zOwlViTConfig.__init__r   r4   c                 K   sf   | j |fi |\}}d|v rTt| drT|d | jkrTtd|d  d| j d | j|fi |S )Nr7   r:   r;   r<   )r>   r@   r7   rA   rB   rC   rD   r2   r2   r3   rG   .  s     zOwlViTConfig.from_pretrained)r9   rY   c                 K   s&   i }||d< ||d< | j |fi |S )z
        Instantiate a [`OwlViTConfig`] (or a derived class) from owlvit text model configuration and owlvit vision
        model configuration.

        Returns:
            [`OwlViTConfig`]: An instance of a configuration object
        r9   rY   )rC   )rE   r9   rY   r/   rF   r2   r2   r3   from_text_vision_configs:  s    	z%OwlViTConfig.from_text_vision_configsc                 C   s8   t | j}| j |d< | j |d< | jj|d< |S )z
        Serializes this instance to a Python dictionary. Override the default [`~PretrainedConfig.to_dict`].

        Returns:
            `Dict[str, any]`: Dictionary of all the attributes that make up this configuration instance,
        r9   rY   r7   )copydeepcopy__dict__r9   to_dictrY   r1   r7   )r.   outputr2   r2   r3   rd   I  s
    zOwlViTConfig.to_dict)NNr   r[   T)rI   rJ   rK   rL   r7   Zis_compositionr"   rM   r	   rN   rO   rP   rG   r   r`   rd   rQ   r2   r2   r0   r3   rZ      s        rZ   c                       s   e Zd Zeeeeeef f dddZeeeeeef f dddZee	dddZ
dd
eeed eeef d fddZeedddZ  ZS )OwlViTOnnxConfig)r6   c                 C   s0   t ddddfdddddd	fd
dddfgS )NZ	input_idsbatchsequence)r      Zpixel_valuesrV   heightwidth)r   ri      r
   Zattention_maskr   r.   r2   r2   r3   inputsX  s    zOwlViTOnnxConfig.inputsc                 C   s0   t dddifdddifdddifdddifgS )NZlogits_per_imager   rg   Zlogits_per_textZtext_embedsZimage_embedsr   rm   r2   r2   r3   outputsb  s    



zOwlViTOnnxConfig.outputsc                 C   s   dS )Ng-C6?r2   rm   r2   r2   r3   atol_for_validationm  s    z$OwlViTOnnxConfig.atol_for_validationNr   r   )	processor
batch_size
seq_length	frameworkr6   c                    s6   t  j|j|||d}t  j|j||d}i ||S )N)rs   rt   ru   )rs   ru   )r!   generate_dummy_inputs	tokenizerZfeature_extractor)r.   rr   rs   rt   ru   Ztext_input_dictZimage_input_dictr0   r2   r3   rv   q  s    
z&OwlViTOnnxConfig.generate_dummy_inputsc                 C   s   dS )N   r2   rm   r2   r2   r3   default_onnx_opset  s    z#OwlViTOnnxConfig.default_onnx_opset)rq   rq   N)rI   rJ   rK   propertyr   rN   intrn   ro   floatrp   r   r   rv   ry   rQ   r2   r2   r0   r3   rf   W  s$    	 
   
rf   )rL   ra   rO   collectionsr   typingr   r   r   r   r   r	   Zprocessing_utilsr   utilsr   Zconfiguration_utilsr   Zonnxr   r   Z
get_loggerrI   rA   Z$OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAPr   rR   rZ   rf   r2   r2   r2   r3   <module>   s&    
hg_