


'''simple docstring'''




import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


__snake_case    						=logging.get_logger(__name__)

__snake_case    						={
    """Salesforce/blip-vqa-base""": """https://huggingface.co/Salesforce/blip-vqa-base/resolve/main/config.json""",
    """Salesforce/blip-vqa-capfit-large""": (
        """https://huggingface.co/Salesforce/blip-vqa-base-capfit/resolve/main/config.json"""
    ),
    """Salesforce/blip-image-captioning-base""": (
        """https://huggingface.co/Salesforce/blip-image-captioning-base/resolve/main/config.json"""
    ),
    """Salesforce/blip-image-captioning-large""": (
        """https://huggingface.co/Salesforce/blip-image-captioning-large/resolve/main/config.json"""
    ),
    """Salesforce/blip-itm-base-coco""": """https://huggingface.co/Salesforce/blip-itm-base-coco/resolve/main/config.json""",
    """Salesforce/blip-itm-large-coco""": """https://huggingface.co/Salesforce/blip-itm-large-coco/resolve/main/config.json""",
    """Salesforce/blip-itm-base-flikr""": """https://huggingface.co/Salesforce/blip-itm-base-flikr/resolve/main/config.json""",
    """Salesforce/blip-itm-large-flikr""": (
        """https://huggingface.co/Salesforce/blip-itm-large-flikr/resolve/main/config.json"""
    ),
}

class 		UpperCAmelCase_      (  __lowercase		):
     lowerCamelCase				:	Dict		=     '''blip_text_model'''





     def __init__( self       :      Tuple    ,      UpperCAmelCase__       :      Optional[int]=3_0_5_2_4    ,      UpperCAmelCase__       :      List[Any]=7_6_8    ,      UpperCAmelCase__       :      Tuple=7_6_8    ,      UpperCAmelCase__       :      Optional[Any]=3_0_7_2    ,      UpperCAmelCase__       :      Union[str, Any]=7_6_8    ,      UpperCAmelCase__       :      Dict=1_2    ,      UpperCAmelCase__       :      Dict=8    ,      UpperCAmelCase__       :      List[str]=5_1_2    ,      UpperCAmelCase__       :      List[str]="gelu"    ,      UpperCAmelCase__       :      Dict=1E-12    ,      UpperCAmelCase__       :      List[str]=0.0    ,      UpperCAmelCase__       :      str=0.0    ,      UpperCAmelCase__       :      Union[str, Any]=0.02    ,      UpperCAmelCase__       :      Tuple=3_0_5_2_2    ,      UpperCAmelCase__       :      Tuple=2    ,      UpperCAmelCase__       :      List[Any]=0    ,      UpperCAmelCase__       :      str=1_0_2    ,      UpperCAmelCase__       :      int=True    ,      UpperCAmelCase__       :      Optional[int]=True    ,      **UpperCAmelCase__       :      List[str]    ,      )				->  Union[str, Any]:
        super().__init__(
            pad_token_id=UpperCAmelCase__    ,      bos_token_id=UpperCAmelCase__    ,      eos_token_id=UpperCAmelCase__    ,      sep_token_id=UpperCAmelCase__    ,      **UpperCAmelCase__    ,      )

        lowerCAmelCase     =			vocab_size
        lowerCAmelCase     =			hidden_size
        lowerCAmelCase     =			encoder_hidden_size
        lowerCAmelCase     =			intermediate_size
        lowerCAmelCase     =			projection_dim
        lowerCAmelCase     =			hidden_dropout_prob
        lowerCAmelCase     =			num_hidden_layers
        lowerCAmelCase     =			num_attention_heads
        lowerCAmelCase     =			max_position_embeddings
        lowerCAmelCase     =			layer_norm_eps
        lowerCAmelCase     =			hidden_act
        lowerCAmelCase     =			initializer_range
        lowerCAmelCase     =			attention_probs_dropout_prob
        lowerCAmelCase     =			is_decoder
        lowerCAmelCase     =			use_cache





     @classmethod
     def 							__UpperCAmelCase      ( cls       :      int    ,      UpperCAmelCase__       :      Union[str, os.PathLike]    ,      **UpperCAmelCase__       :      List[Any]    )				->  "PretrainedConfig":
        cls._set_token_in_kwargs(UpperCAmelCase__    )

        lowerCAmelCase     ,  lowerCAmelCase     =			cls.get_config_dict(UpperCAmelCase__    ,      **UpperCAmelCase__    )

        # get the text config dict if we are loading from BlipConfig
        if config_dict.get('model_type'    ) == "blip":
           lowerCAmelCase     =			config_dict['text_config']

        if "model_type" in config_dict and hasattr(cls    ,      'model_type'    ) and config_dict["model_type"] != cls.model_type:
           logger.warning(
               F'''You are using a model of type {config_dict['model_type']} to instantiate a model of type '''
               F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''    )

        return cls.from_dict(UpperCAmelCase__    ,      **UpperCAmelCase__    )




class 		UpperCAmelCase_      (  __lowercase		):
     lowerCamelCase				:	Optional[int]		=     '''blip_vision_model'''





     def __init__( self       :      Optional[int]    ,      UpperCAmelCase__       :      Tuple=7_6_8    ,      UpperCAmelCase__       :      Optional[Any]=3_0_7_2    ,      UpperCAmelCase__       :      Optional[Any]=5_1_2    ,      UpperCAmelCase__       :      Optional[Any]=1_2    ,      UpperCAmelCase__       :      List[str]=1_2    ,      UpperCAmelCase__       :      int=3_8_4    ,      UpperCAmelCase__       :      int=1_6    ,      UpperCAmelCase__       :      List[Any]="gelu"    ,      UpperCAmelCase__       :      List[Any]=1E-5    ,      UpperCAmelCase__       :      Any=0.0    ,      UpperCAmelCase__       :      Tuple=1E-10    ,      **UpperCAmelCase__       :      Optional[int]    ,      )				->  int:
        super().__init__(**UpperCAmelCase__    )

        lowerCAmelCase     =			hidden_size
        lowerCAmelCase     =			intermediate_size
        lowerCAmelCase     =			projection_dim
        lowerCAmelCase     =			num_hidden_layers
        lowerCAmelCase     =			num_attention_heads
        lowerCAmelCase     =			patch_size
        lowerCAmelCase     =			image_size
        lowerCAmelCase     =			initializer_range
        lowerCAmelCase     =			attention_dropout
        lowerCAmelCase     =			layer_norm_eps
        lowerCAmelCase     =			hidden_act





     @classmethod
     def 							__UpperCAmelCase      ( cls       :      str    ,      UpperCAmelCase__       :      Union[str, os.PathLike]    ,      **UpperCAmelCase__       :      List[Any]    )				->  "PretrainedConfig":
        cls._set_token_in_kwargs(UpperCAmelCase__    )

        lowerCAmelCase     ,  lowerCAmelCase     =			cls.get_config_dict(UpperCAmelCase__    ,      **UpperCAmelCase__    )

        # get the vision config dict if we are loading from BlipConfig
        if config_dict.get('model_type'    ) == "blip":
           lowerCAmelCase     =			config_dict['vision_config']

        if "model_type" in config_dict and hasattr(cls    ,      'model_type'    ) and config_dict["model_type"] != cls.model_type:
           logger.warning(
               F'''You are using a model of type {config_dict['model_type']} to instantiate a model of type '''
               F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''    )

        return cls.from_dict(UpperCAmelCase__    ,      **UpperCAmelCase__    )




class 		UpperCAmelCase_      (  __lowercase		):
     lowerCamelCase				:	Dict		=     '''blip'''
     lowerCamelCase				:	Tuple		=     True





     def __init__( self       :      Union[str, Any]    ,      UpperCAmelCase__       :      str=None    ,      UpperCAmelCase__       :      Union[str, Any]=None    ,      UpperCAmelCase__       :      Optional[int]=5_1_2    ,      UpperCAmelCase__       :      str=2.6_592    ,      UpperCAmelCase__       :      str=2_5_6    ,      **UpperCAmelCase__       :      List[Any]    ,      )				->  Dict:
        super().__init__(**UpperCAmelCase__    )

        if text_config is None:
           lowerCAmelCase     =			{}
           logger.info('`text_config` is `None`. Initializing the `BlipTextConfig` with default values.'    )

        if vision_config is None:
           lowerCAmelCase     =			{}
           logger.info('`vision_config` is `None`. Initializing the `BlipVisionConfig` with default values.'    )

        lowerCAmelCase     =			BlipTextConfig(**UpperCAmelCase__    )
        lowerCAmelCase     =			BlipVisionConfig(**UpperCAmelCase__    )

        lowerCAmelCase     =			self.vision_config.hidden_size

        lowerCAmelCase     =			projection_dim
        lowerCAmelCase     =			logit_scale_init_value
        lowerCAmelCase     =			1.0
        lowerCAmelCase     =			0.02
        lowerCAmelCase     =			image_text_hidden_size





     @classmethod
     def 							__UpperCAmelCase      ( cls       :      Dict    ,      UpperCAmelCase__       :      BlipTextConfig    ,      UpperCAmelCase__       :      BlipVisionConfig    ,      **UpperCAmelCase__       :      str    )				->  Optional[Any]:
        return cls(text_config=text_config.to_dict()    ,      vision_config=vision_config.to_dict()    ,      **UpperCAmelCase__    )





     def 							__UpperCAmelCase      ( self       :      Dict    )				->  List[str]:
        lowerCAmelCase     =			copy.deepcopy(self.__dict__    )
        lowerCAmelCase     =			self.text_config.to_dict()
        lowerCAmelCase     =			self.vision_config.to_dict()
        lowerCAmelCase     =			self.__class__.model_type
        return output









'''simple docstring'''




import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow


if is_torch_available():
       import torch

       from transformers import XLMRobertaModel

@require_sentencepiece
@require_tokenizers
@require_torch
class 		UpperCAmelCase_      (  unittest.TestCase		):





     @slow
     def 							__UpperCAmelCase      ( self       :      str    )				->  List[str]:
        lowerCAmelCase     =			XLMRobertaModel.from_pretrained('xlm-roberta-base'    )
        lowerCAmelCase     =			torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]]    )
        # The dog is cute and lives in the garden house

        lowerCAmelCase     =			torch.Size((1, 1_2, 7_6_8)    )  # batch_size, sequence_length, embedding_vector_dim
        lowerCAmelCase     =			torch.tensor(
            [[-0.0_101, 0.1_218, -0.0_803, 0.0_801, 0.1_327, 0.0_776, -0.1_215, 0.2_383, 0.3_338, 0.3_106, 0.0_300, 0.0_252]]    )
        #  xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base')
        #  xlmr.eval()
        #  expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1]
        with torch.no_grad():
           lowerCAmelCase     =			model(UpperCAmelCase__    )['last_hidden_state'].detach()
        self.assertEqual(output.shape    ,      UpperCAmelCase__    )
        # compare the actual values for a slice of last dim
        self.assertTrue(torch.allclose(output[:, :, -1]    ,      UpperCAmelCase__    ,      atol=1E-3    )    )





     @slow
     def 							__UpperCAmelCase      ( self       :      List[Any]    )				->  Tuple:
        lowerCAmelCase     =			XLMRobertaModel.from_pretrained('xlm-roberta-large'    )
        lowerCAmelCase     =			torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]]    )
        # The dog is cute and lives in the garden house

        lowerCAmelCase     =			torch.Size((1, 1_2, 1_0_2_4)    )  # batch_size, sequence_length, embedding_vector_dim
        lowerCAmelCase     =			torch.tensor(
            [[-0.0_699, -0.0_318, 0.0_705, -0.1_241, 0.0_999, -0.0_520, 0.1_004, -0.1_838, -0.4_704, 0.1_437, 0.0_821, 0.0_126]]    )
        #  xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large')
        #  xlmr.eval()
        #  expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1]
        with torch.no_grad():
           lowerCAmelCase     =			model(UpperCAmelCase__    )['last_hidden_state'].detach()
        self.assertEqual(output.shape    ,      UpperCAmelCase__    )
        # compare the actual values for a slice of last dim
        self.assertTrue(torch.allclose(output[:, :, -1]    ,      UpperCAmelCase__    ,      atol=1E-3    )    )





