





import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
from ...utils import logging
from ..auto import CONFIG_MAPPING


snake_case__					:							Tuple													=     logging.get_logger(__name__)

snake_case__					:							Union[str, Any]													=     {
    'salesforce/blip2-opt-2.7b': 'https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json',
}





class A_					(			_lowerCamelCase   ):
   lowerCAmelCase__        = """blip_2_vision_model"""




   def __init__(self	:Tuple    ,   _UpperCamelCase	:List[str]=1408    ,   _UpperCamelCase	:int=6144    ,   _UpperCamelCase	:Optional[Any]=39    ,   _UpperCamelCase	:Optional[Any]=16    ,   _UpperCamelCase	:Optional[int]=224    ,   _UpperCamelCase	:Optional[Any]=14    ,   _UpperCamelCase	:Optional[Any]="gelu"    ,   _UpperCamelCase	:List[str]=0.0_0_0_0_1    ,   _UpperCamelCase	:List[Any]=0.0    ,   _UpperCamelCase	:Union[str, Any]=1e-10    ,   _UpperCamelCase	:List[str]=True    ,   **_UpperCamelCase	:Any    ,   )->					List[Any]:
         super().__init__(**_UpperCamelCase )

         __A          =				hidden_size
         __A          =				intermediate_size
         __A          =				num_hidden_layers
         __A          =				num_attention_heads
         __A          =				patch_size
         __A          =				image_size
         __A          =				initializer_range
         __A          =				attention_dropout
         __A          =				layer_norm_eps
         __A          =				hidden_act
         __A          =				qkv_bias






   @classmethod
   def _lowerCAmelCase      (cls	:str    ,   _UpperCamelCase	:Union[str, os.PathLike]    ,   **_UpperCamelCase	:Optional[Any] )->					"PretrainedConfig":
         cls._set_token_in_kwargs(_UpperCamelCase )

         __A   ,						__A          =				cls.get_config_dict(_UpperCamelCase    ,   **_UpperCamelCase )

         # get the vision config dict if we are loading from Blip2Config
         if config_dict.get('''model_type''' ) == "blip-2":
               __A          =				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 A_					(			_lowerCamelCase   ):
   lowerCAmelCase__        = """blip_2_qformer"""




   def __init__(self	:List[Any]    ,   _UpperCamelCase	:Optional[int]=3_0522    ,   _UpperCamelCase	:int=768    ,   _UpperCamelCase	:Tuple=12    ,   _UpperCamelCase	:List[Any]=12    ,   _UpperCamelCase	:Union[str, Any]=3072    ,   _UpperCamelCase	:int="gelu"    ,   _UpperCamelCase	:List[Any]=0.1    ,   _UpperCamelCase	:Optional[Any]=0.1    ,   _UpperCamelCase	:Union[str, Any]=512    ,   _UpperCamelCase	:str=0.0_2    ,   _UpperCamelCase	:Dict=1e-12    ,   _UpperCamelCase	:str=0    ,   _UpperCamelCase	:List[Any]="absolute"    ,   _UpperCamelCase	:List[Any]=2    ,   _UpperCamelCase	:Union[str, Any]=1408    ,   **_UpperCamelCase	:List[str]    ,   )->					Optional[Any]:
         super().__init__(pad_token_id=_UpperCamelCase    ,   **_UpperCamelCase )

         __A          =				vocab_size
         __A          =				hidden_size
         __A          =				num_hidden_layers
         __A          =				num_attention_heads
         __A          =				hidden_act
         __A          =				intermediate_size
         __A          =				hidden_dropout_prob
         __A          =				attention_probs_dropout_prob
         __A          =				max_position_embeddings
         __A          =				initializer_range
         __A          =				layer_norm_eps
         __A          =				position_embedding_type
         __A          =				cross_attention_frequency
         __A          =				encoder_hidden_size






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

         __A   ,						__A          =				cls.get_config_dict(_UpperCamelCase    ,   **_UpperCamelCase )

         # get the qformer config dict if we are loading from Blip2Config
         if config_dict.get('''model_type''' ) == "blip-2":
               __A          =				config_dict['''qformer_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 A_					(			_lowerCamelCase   ):
   lowerCAmelCase__        = """blip-2"""
   lowerCAmelCase__        = True




   def __init__(self	:List[Any]    ,   _UpperCamelCase	:Optional[int]=None    ,   _UpperCamelCase	:List[str]=None    ,   _UpperCamelCase	:int=None    ,   _UpperCamelCase	:List[Any]=32    ,   **_UpperCamelCase	:List[Any] )->					Any:
         super().__init__(**_UpperCamelCase )

         if vision_config is None:
               __A          =				{}
               logger.info('''vision_config is None. initializing the Blip2VisionConfig with default values.''' )

         if qformer_config is None:
               __A          =				{}
               logger.info('''qformer_config is None. Initializing the Blip2QFormerConfig with default values.''' )

         if text_config is None:
               __A          =				{}
               logger.info('''text_config is None. Initializing the text config with default values (`OPTConfig`).''' )

         __A          =				BlipaVisionConfig(**_UpperCamelCase )
         __A          =				BlipaQFormerConfig(**_UpperCamelCase )
         __A          =				text_config['''model_type'''] if '''model_type''' in text_config else '''opt'''
         __A          =				CONFIG_MAPPING[text_model_type](**_UpperCamelCase )

         __A          =				self.text_config.tie_word_embeddings
         __A          =				self.text_config.is_encoder_decoder

         __A          =				num_query_tokens
         __A          =				self.vision_config.hidden_size
         __A          =				self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
         __A          =				1.0
         __A          =				0.0_2




   @classmethod
   def _lowerCAmelCase      (cls	:Union[str, Any]    ,   _UpperCamelCase	:BlipaVisionConfig    ,   _UpperCamelCase	:BlipaQFormerConfig    ,   _UpperCamelCase	:PretrainedConfig    ,   **_UpperCamelCase	:Tuple    ,   )->					Dict:
         return cls(
             vision_config=vision_config.to_dict()    ,   qformer_config=qformer_config.to_dict()    ,   text_config=text_config.to_dict()    ,   **_UpperCamelCase    ,   )






   def _lowerCAmelCase      (self	:int )->					List[Any]:
         __A          =				copy.deepcopy(self.__dict__ )
         __A          =				self.vision_config.to_dict()
         __A          =				self.qformer_config.to_dict()
         __A          =				self.text_config.to_dict()
         __A          =				self.__class__.model_type
         return output












def _a				(	lowerCamelCase:   dict )	->						bool:







      '''simple docstring'''






      __A          =				set()
      # To detect a back edge, keep track of vertices currently in the recursion stack
      __A          =				set()
      return any(
          node not in visited and depth_first_search(lowerCamelCase					,				lowerCamelCase					,				lowerCamelCase					,				lowerCamelCase )
          for node in graph )



def _a				(	lowerCamelCase:   dict					,				lowerCamelCase:   int					,				lowerCamelCase:   set					,				lowerCamelCase:   set )	->						bool:







      '''simple docstring'''






      visited.add(lowerCamelCase )
      rec_stk.add(lowerCamelCase )

      for node in graph[vertex]:
            if node not in visited:
                  if depth_first_search(lowerCamelCase					,				lowerCamelCase					,				lowerCamelCase					,				lowerCamelCase ):
                        return True
            elif node in rec_stk:
                  return True

    # The node needs to be removed from recursion stack before function ends
      rec_stk.remove(lowerCamelCase )
      return False


if __name__ == "__main__":
       from doctest import testmod

       testmod()





