
    
io9                         d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	  ej                  e      Z G d de      Z G d	 d
e      Z G d de      Zg dZy)z InstructBLIP model configuration   )PreTrainedConfig)!MODEL_FOR_CAUSAL_LM_MAPPING_NAMES)logging   )CONFIG_MAPPING
AutoConfigc                   B     e Zd ZdZdZdZ	 	 	 	 	 	 	 	 	 	 	 d fd	Z xZS )InstructBlipVisionConfigaW  
    This is the configuration class to store the configuration of a [`InstructBlipVisionModel`]. It is used to
    instantiate a InstructBLIP vision encoder according to the specified arguments, defining the model architecture.
    Instantiating a configuration defaults will yield a similar configuration to that of the InstructBLIP
    [Salesforce/instruct-blip-flan-t5](https://huggingface.co/Salesforce/instruct-blip-flan-t5) 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 1408):
            Dimensionality of the encoder layers and the pooler layer.
        intermediate_size (`int`, *optional*, defaults to 6144):
            Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
        num_hidden_layers (`int`, *optional*, defaults to 39):
            Number of hidden layers in the Transformer encoder.
        num_attention_heads (`int`, *optional*, defaults to 16):
            Number of attention heads for each attention layer in the Transformer encoder.
        image_size (`int`, *optional*, defaults to 224):
            The size (resolution) of each image.
        patch_size (`int`, *optional*, defaults to 14):
            The size (resolution) of each patch.
        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"` `"gelu"` are supported. to 1e-5): The epsilon used by the layer
            normalization layers.
        layer_norm_eps (`float`, *optional*, defaults to 1e-06):
            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 1e-10):
            The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
        qkv_bias (`bool`, *optional*, defaults to `True`):
            Whether to add a bias to the queries and values in the self-attention layers.

    Example:

    ```python
    >>> from transformers import InstructBlipVisionConfig, InstructBlipVisionModel

    >>> # Initializing a InstructBlipVisionConfig with Salesforce/instruct-blip-flan-t5 style configuration
    >>> configuration = InstructBlipVisionConfig()

    >>> # Initializing a InstructBlipVisionModel (with random weights) from the Salesforce/instruct-blip-flan-t5 style configuration
    >>> model = InstructBlipVisionModel(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```instructblip_vision_modelvision_configc                     t        |   di | || _        || _        || _        || _        || _        || _        |
| _        |	| _	        || _
        || _        || _        y N )super__init__hidden_sizeintermediate_sizenum_hidden_layersnum_attention_heads
patch_size
image_sizeinitializer_rangeattention_dropoutlayer_norm_eps
hidden_actqkv_bias)selfr   r   r   r   r   r   r   r   r   r   r   kwargs	__class__s                w/mnt/e/genesis-system/.venv/lib/python3.12/site-packages/transformers/models/instructblip/configuration_instructblip.pyr   z!InstructBlipVisionConfig.__init__O   si     	"6"&!2!2#6 $$!2!2,$     )  i   '            gelugư>g        g|=T__name__
__module____qualname____doc__
model_typebase_config_keyr   __classcell__r   s   @r    r
   r
      sB    0d -J%O ! !r!   r
   c                   H     e Zd ZdZdZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd	Z xZS )InstructBlipQFormerConfiga7  
    This is the configuration class to store the configuration of a [`InstructBlipQFormerModel`]. It is used to
    instantiate a InstructBLIP Querying Transformer (Q-Former) 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 InstructBLIP [Salesforce/instruct-blip-flan-t5](https://huggingface.co/Salesforce/instruct-blip-flan-t5)
    architecture. Configuration objects inherit from [`PreTrainedConfig`] and can be used to control the model outputs.
    Read the documentation from [`PreTrainedConfig`] for more information.

    Note that [`InstructBlipQFormerModel`] is very similar to [`BertLMHeadModel`] with interleaved cross-attention.

    Args:
        vocab_size (`int`, *optional*, defaults to 30522):
            Vocabulary size of the Q-Former model. Defines the number of different tokens that can be represented by
            the `inputs_ids` passed when calling the model.
        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" (often named feed-forward) layer in the Transformer encoder.
        hidden_act (`str` or `Callable`, *optional*, defaults to `"gelu"`):
            The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
            `"relu"`, `"silu"` 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.
        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).
        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.
        pad_token_id (`int`, *optional*, defaults to 0):
            Token id used for padding sequences.
        cross_attention_frequency (`int`, *optional*, defaults to 2):
            The frequency of adding cross-attention to the Transformer layers.
        encoder_hidden_size (`int`, *optional*, defaults to 1408):
            The hidden size of the hidden states for cross-attention.

    Examples:

    ```python
    >>> from transformers import InstructBlipQFormerConfig, InstructBlipQFormerModel

    >>> # Initializing a InstructBLIP Salesforce/instruct-blip-flan-t5 style configuration
    >>> configuration = InstructBlipQFormerConfig()

    >>> # Initializing a model (with random weights) from the Salesforce/instruct-blip-flan-t5 style configuration
    >>> model = InstructBlipQFormerModel(configuration)
    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```instructblip_qformerqformer_configc                     t        |   di | || _        || _        || _        || _        || _        || _        || _        || _	        || _
        |	| _        |
| _        || _        || _        || _        y r   )r   r   pad_token_id
vocab_sizer   r   r   r   r   hidden_dropout_probattention_probs_dropout_probmax_position_embeddingsr   r   cross_attention_frequencyencoder_hidden_size)r   r7   r   r   r   r   r   r8   r9   r:   r   r   r6   r;   r<   r   r   s                   r    r   z"InstructBlipQFormerConfig.__init__   s    $ 	"6"($&!2#6 $!2#6 ,H)'>$!2,)B&#6 r!   )i:w  i      r=   i   r'   皙?r>   i   {Gz?g-q=    r   r"   r(   r0   s   @r    r2   r2   m   sK    7r (J&O %( #"# !7 !7r!   r2   c                   F     e Zd ZdZdZddiZeeedZ		 	 	 	 	 d fd	Z
 xZS )InstructBlipConfigaP
  
    [`InstructBlipConfig`] is the configuration class to store the configuration of a
    [`InstructBlipForConditionalGeneration`]. It is used to instantiate a InstructBLIP model according to the specified
    arguments, defining the vision model, Q-Former model and language model configs. Instantiating a configuration with
    the defaults will yield a similar configuration to that of the InstructBLIP
    [Salesforce/instruct-blip-flan-t5](https://huggingface.co/Salesforce/instruct-blip-flan-t5) architecture.

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

    Args:
        vision_config (`dict`, *optional*):
            Dictionary of configuration options used to initialize [`InstructBlipVisionConfig`].
        qformer_config (`dict`, *optional*):
            Dictionary of configuration options used to initialize [`InstructBlipQFormerConfig`].
        text_config (`dict`, *optional*):
            Dictionary of configuration options used to initialize any [`PreTrainedConfig`].
        num_query_tokens (`int`, *optional*, defaults to 32):
            The number of query tokens passed through the Transformer.

        image_token_index (`int`, *optional*):
            Token index of special image token.
        kwargs (*optional*):
            Dictionary of keyword arguments.

    Example:

    ```python
    >>> from transformers import (
    ...     InstructBlipVisionConfig,
    ...     InstructBlipQFormerConfig,
    ...     OPTConfig,
    ...     InstructBlipConfig,
    ...     InstructBlipForConditionalGeneration,
    ... )

    >>> # Initializing a InstructBlipConfig with Salesforce/instruct-blip-flan-t5 style configuration
    >>> configuration = InstructBlipConfig()

    >>> # Initializing a InstructBlipForConditionalGeneration (with random weights) from the Salesforce/instruct-blip-flan-t5 style configuration
    >>> model = InstructBlipForConditionalGeneration(configuration)

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

    >>> # We can also initialize a InstructBlipConfig from a InstructBlipVisionConfig, InstructBlipQFormerConfig and any PreTrainedConfig

    >>> # Initializing InstructBLIP vision, InstructBLIP Q-Former and language model configurations
    >>> vision_config = InstructBlipVisionConfig()
    >>> qformer_config = InstructBlipQFormerConfig()
    >>> text_config = OPTConfig()

    >>> config = InstructBlipConfig(vision_config=vision_config, qformer_config=qformer_config, text_config=text_config)
    ```instructblipimage_token_idimage_token_index)text_configr4   r   c                    |#t        d          }t        j                  d       n0t        |t              r |j                  dd      }t        |   di |}| t               }t        j                  d       nt        |t              rt        di |}| t               }t        j                  d       nt        |t              rt        di |}|| _        || _	        || _
        || _        || _        | j                  j                  | j                  _        | j                  j                  t         v | _        d| _        d| _        t)        | T  di | y )	NoptzTtext_config is None. Initializing the text config with default values (`OPTConfig`).r-   zWqformer_config is None. Initializing the InstructBlipQFormerConfig with default values.z[`vision_config` is `None`. initializing the `InstructBlipVisionConfig` with default values.g      ?r?   r   )r   loggerinfo
isinstancedictgetr2   r
   rF   r   r4   num_query_tokensrE   r   r<   r-   r   use_decoder_only_language_modelinitializer_factorr   r   r   )	r   r   r4   rF   rN   rE   r   text_model_typer   s	           r    r   zInstructBlipConfig.__init__  s1    (/1KKKnoT*)oolEBO(9HKHK!68NKKqr-6HHN 46MKKuvt,4E}EM&*, 0!2262D2D2P2P//3/?/?/J/JNo/o,"%!%"6"r!   )NNN    N)r)   r*   r+   r,   r-   attribute_mapr   r2   r
   sub_configsr   r/   r0   s   @r    rB   rB      sJ    5n  J-M "31K &# &#r!   rB   )rB   r2   r
   N)r,   configuration_utilsr   models.auto.modeling_autor   utilsr   autor   r   
get_loggerr)   rI   r
   r2   rB   __all__r   r!   r    <module>r[      sb    ' 3 J  - 
		H	%Q!/ Q!h^7 0 ^7Bh#) h#V Zr!   