





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


lowerCAmelCase_    =    logging.get_logger(__name__)

lowerCAmelCase_    =    {
    '''salesforce/blip2-opt-2.7b''': '''https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json''',
}



class  __lowerCAmelCase	(      _a					):
			lowerCamelCase_					:					Union[str, Any]	      =				'''blip_2_vision_model'''






			def __init__(self ,							__magic_name__=1408 ,							__magic_name__=6144 ,							__magic_name__=39 ,							__magic_name__=16 ,							__magic_name__=224 ,							__magic_name__=14 ,							__magic_name__="gelu" ,							__magic_name__=0.00_001 ,							__magic_name__=0.0 ,							__magic_name__=1e-10 ,							__magic_name__=True ,							**__magic_name__ ,							)					->       Any:


							'''simple docstring'''



							super().__init__(**__magic_name__   )

							snake_case_						:   Tuple           =  hidden_size
							snake_case_						:   Any           =  intermediate_size
							snake_case_						:   List[str]           =  num_hidden_layers
							snake_case_						:   Any           =  num_attention_heads
							snake_case_						:   Optional[Any]           =  patch_size
							snake_case_						:   Any           =  image_size
							snake_case_						:   Union[str, Any]           =  initializer_range
							snake_case_						:   int           =  attention_dropout
							snake_case_						:   List[str]           =  layer_norm_eps
							snake_case_						:   Optional[Any]           =  hidden_act
							snake_case_						:   str           =  qkv_bias






			@classmethod
			def       lowerCamelCase  (cls ,							__magic_name__ ,							**__magic_name__   )					->       "PretrainedConfig":


							'''simple docstring'''



							cls._set_token_in_kwargs(__magic_name__   )

							snake_case_      ,							snake_case_						:   int           =  cls.get_config_dict(__magic_name__ ,							**__magic_name__   )

							# get the vision config dict if we are loading from Blip2Config
							if config_dict.get('''model_type'''   ) == "blip-2":
											snake_case_						:   Any           =  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(__magic_name__ ,							**__magic_name__   )




class  __lowerCAmelCase	(      _a					):
			lowerCamelCase_					:					Dict	      =				'''blip_2_qformer'''






			def __init__(self ,							__magic_name__=3_0522 ,							__magic_name__=768 ,							__magic_name__=12 ,							__magic_name__=12 ,							__magic_name__=3072 ,							__magic_name__="gelu" ,							__magic_name__=0.1 ,							__magic_name__=0.1 ,							__magic_name__=512 ,							__magic_name__=0.02 ,							__magic_name__=1e-12 ,							__magic_name__=0 ,							__magic_name__="absolute" ,							__magic_name__=2 ,							__magic_name__=1408 ,							**__magic_name__ ,							)					->       List[Any]:


							'''simple docstring'''



							super().__init__(pad_token_id=__magic_name__ ,							**__magic_name__   )

							snake_case_						:   Tuple           =  vocab_size
							snake_case_						:   Optional[int]           =  hidden_size
							snake_case_						:   Union[str, Any]           =  num_hidden_layers
							snake_case_						:   Union[str, Any]           =  num_attention_heads
							snake_case_						:   Optional[Any]           =  hidden_act
							snake_case_						:   Optional[int]           =  intermediate_size
							snake_case_						:   List[Any]           =  hidden_dropout_prob
							snake_case_						:   Dict           =  attention_probs_dropout_prob
							snake_case_						:   Optional[int]           =  max_position_embeddings
							snake_case_						:   Union[str, Any]           =  initializer_range
							snake_case_						:   Dict           =  layer_norm_eps
							snake_case_						:   Tuple           =  position_embedding_type
							snake_case_						:   int           =  cross_attention_frequency
							snake_case_						:   Optional[Any]           =  encoder_hidden_size






			@classmethod
			def       lowerCamelCase  (cls ,							__magic_name__ ,							**__magic_name__   )					->       "PretrainedConfig":


							'''simple docstring'''



							cls._set_token_in_kwargs(__magic_name__   )

							snake_case_      ,							snake_case_						:   Union[str, Any]           =  cls.get_config_dict(__magic_name__ ,							**__magic_name__   )

							# get the qformer config dict if we are loading from Blip2Config
							if config_dict.get('''model_type'''   ) == "blip-2":
											snake_case_						:   Optional[int]           =  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(__magic_name__ ,							**__magic_name__   )




class  __lowerCAmelCase	(      _a					):
			lowerCamelCase_					:					List[Any]	      =				'''blip-2'''
			lowerCamelCase_					:					Union[str, Any]	      =				True






			def __init__(self ,							__magic_name__=None ,							__magic_name__=None ,							__magic_name__=None ,							__magic_name__=32 ,							**__magic_name__   )					->       Dict:


							'''simple docstring'''



							super().__init__(**__magic_name__   )

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

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

							if text_config is None:
											snake_case_						:   Union[str, Any]           =  {}
											logger.info('''text_config is None. Initializing the text config with default values (`OPTConfig`).'''   )

							snake_case_						:   List[str]           =  BlipaVisionConfig(**__magic_name__   )
							snake_case_						:   Dict           =  BlipaQFormerConfig(**__magic_name__   )
							snake_case_						:   int           =  text_config['''model_type'''] if '''model_type''' in text_config else '''opt'''
							snake_case_						:   Tuple           =  CONFIG_MAPPING[text_model_type](**__magic_name__   )

							snake_case_						:   Optional[Any]           =  self.text_config.tie_word_embeddings
							snake_case_						:   List[Any]           =  self.text_config.is_encoder_decoder

							snake_case_						:   str           =  num_query_tokens
							snake_case_						:   Union[str, Any]           =  self.vision_config.hidden_size
							snake_case_						:   Any           =  self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
							snake_case_						:   Optional[int]           =  1.0
							snake_case_						:   str           =  0.02






			@classmethod
			def       lowerCamelCase  (cls ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							**__magic_name__ ,							)					->       str:


							'''simple docstring'''



							return cls(
							    vision_config=vision_config.to_dict() ,							qformer_config=qformer_config.to_dict() ,							text_config=text_config.to_dict() ,							**__magic_name__ ,							)






			def       lowerCamelCase  (self   )					->       List[str]:


							'''simple docstring'''



							snake_case_						:   Tuple           =  copy.deepcopy(self.__dict__   )
							snake_case_						:   int           =  self.vision_config.to_dict()
							snake_case_						:   Union[str, Any]           =  self.qformer_config.to_dict()
							snake_case_						:   List[str]           =  self.text_config.to_dict()
							snake_case_						:   Dict           =  self.__class__.model_type
							return output







from collections.abc import Iterator, MutableMapping
from dataclasses import dataclass
from typing import Generic, TypeVar

lowerCAmelCase_    =    TypeVar('''KEY''')
lowerCAmelCase_    =    TypeVar('''VAL''')



@dataclass(frozen=_a,					slots=_a					)
class  __lowerCAmelCase	(      Generic[KEY, VAL]					):
			lowerCamelCase_					:					KEY
			lowerCamelCase_					:					VAL




class  __lowerCAmelCase	(      _Item					):






			def __init__(self   )					->       None:


							'''simple docstring'''



							super().__init__(__magic_name__ ,							__magic_name__   )






			def __bool__(self   )					->       bool:


							'''simple docstring'''



							return False




lowerCAmelCase_    =    _DeletedItem()



class  __lowerCAmelCase	(      MutableMapping[KEY, VAL]					):






			def __init__(self ,							__magic_name__ = 8 ,							__magic_name__ = 0.75   )					->       None:


							'''simple docstring'''



							snake_case_						:   List[Any]           =  initial_block_size
							snake_case_						:   list[_Item | None]           =  [None] * initial_block_size
							assert 0.0 < capacity_factor < 1.0
							snake_case_						:   List[str]           =  capacity_factor
							snake_case_						:   int           =  0






			def       lowerCamelCase  (self ,							__magic_name__   )					->       int:


							'''simple docstring'''



							return hash(__magic_name__   ) % len(self._buckets   )






			def       lowerCamelCase  (self ,							__magic_name__   )					->       int:


							'''simple docstring'''



							return (ind + 1) % len(self._buckets   )






			def       lowerCamelCase  (self ,							__magic_name__ ,							__magic_name__ ,							__magic_name__   )					->       bool:


							'''simple docstring'''



							snake_case_						:   Optional[int]           =  self._buckets[ind]
							if not stored:
											snake_case_						:   Optional[Any]           =  _Item(__magic_name__ ,							__magic_name__   )
											self._len += 1
											return True
							elif stored.key == key:
											snake_case_						:   List[Any]           =  _Item(__magic_name__ ,							__magic_name__   )
											return True
							else:
											return False






			def       lowerCamelCase  (self   )					->       bool:


							'''simple docstring'''



							snake_case_						:   Any           =  len(self._buckets   ) * self._capacity_factor
							return len(self   ) >= int(__magic_name__   )






			def       lowerCamelCase  (self   )					->       bool:


							'''simple docstring'''



							if len(self._buckets   ) <= self._initial_block_size:
											return False
							snake_case_						:   int           =  len(self._buckets   ) * self._capacity_factor / 2
							return len(self   ) < limit






			def       lowerCamelCase  (self ,							__magic_name__   )					->       None:


							'''simple docstring'''



							snake_case_						:   List[str]           =  self._buckets
							snake_case_						:   int           =  [None] * new_size
							snake_case_						:   Optional[int]           =  0
							for item in old_buckets:
											if item:
															self._add_item(item.key ,							item.val   )






			def       lowerCamelCase  (self   )					->       None:


							'''simple docstring'''



							self._resize(len(self._buckets   ) * 2   )






			def       lowerCamelCase  (self   )					->       None:


							'''simple docstring'''



							self._resize(len(self._buckets   ) // 2   )






			def       lowerCamelCase  (self ,							__magic_name__   )					->       Iterator[int]:


							'''simple docstring'''



							snake_case_						:   Dict           =  self._get_bucket_index(__magic_name__   )
							for _ in range(len(self._buckets   )   ):
											yield ind
											snake_case_						:   Tuple           =  self._get_next_ind(__magic_name__   )






			def       lowerCamelCase  (self ,							__magic_name__ ,							__magic_name__   )					->       None:


							'''simple docstring'''



							for ind in self._iterate_buckets(__magic_name__   ):
											if self._try_set(__magic_name__ ,							__magic_name__ ,							__magic_name__   ):
															break






			def __setitem__(self ,							__magic_name__ ,							__magic_name__   )					->       None:


							'''simple docstring'''



							if self._is_full():
											self._size_up()

							self._add_item(__magic_name__ ,							__magic_name__   )






			def __delitem__(self ,							__magic_name__   )					->       None:


							'''simple docstring'''



							for ind in self._iterate_buckets(__magic_name__   ):
											snake_case_						:   Optional[Any]           =  self._buckets[ind]
											if item is None:
															raise KeyError(__magic_name__   )
											if item is _deleted:
															continue
											if item.key == key:
															snake_case_						:   Union[str, Any]           =  _deleted
															self._len -= 1
															break
							if self._is_sparse():
											self._size_down()






			def __getitem__(self ,							__magic_name__   )					->       VAL:


							'''simple docstring'''



							for ind in self._iterate_buckets(__magic_name__   ):
											snake_case_						:   List[Any]           =  self._buckets[ind]
											if item is None:
															break
											if item is _deleted:
															continue
											if item.key == key:
															return item.val
							raise KeyError(__magic_name__   )






			def __len__(self   )					->       int:


							'''simple docstring'''



							return self._len






			def __iter__(self   )					->       Iterator[KEY]:


							'''simple docstring'''



							yield from (item.key for item in self._buckets if item)






			def __repr__(self   )					->       str:


							'''simple docstring'''



							snake_case_						:   List[str]           =  ''' ,'''.join(
							    F'''{item.key}: {item.val}''' for item in self._buckets if item   )
							return F'''HashMap({val_string})'''
