
'''simple docstring'''







from __future__ import annotations

from collections.abc import Callable
from typing import Generic, TypeVar

lowercase    :							Union[str, Any]      =					TypeVar("T")
lowercase    :							int      =					TypeVar("U")


class 						__UpperCAmelCase      (					Generic[T, U]						):


							def __init__(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_  ):





								"""simple docstring"""






								_snake_case   						=		key
								_snake_case   						=		val
								_snake_case   						=		None
								_snake_case   						=		None




							def __repr__(       self  ):





								"""simple docstring"""






								return (
								    F'Node: key: {self.key}, val: {self.val}, '
								    F'has next: {bool(self.next  )}, has prev: {bool(self.prev  )}'
								)


class 						__UpperCAmelCase      (					Generic[T, U]						):


							def __init__(       self  ):





								"""simple docstring"""






								_snake_case   						=		DoubleLinkedListNode(lowerCAmelCase_    ,   lowerCAmelCase_  )
								_snake_case   						=		DoubleLinkedListNode(lowerCAmelCase_    ,   lowerCAmelCase_  )
								_snake_case						, _snake_case   						=		self.rear, self.head


							def __repr__(       self  ):





								"""simple docstring"""






								_snake_case   						=		['DoubleLinkedList']
								_snake_case   						=		self.head
								while node.next is not None:
									rep.append(str(lowerCAmelCase_  )  )
									_snake_case   						=		node.next
								rep.append(str(self.rear  )  )
								return ",\n    ".join(lowerCAmelCase_  )


							def  lowerCamelCase				(       self    ,   lowerCAmelCase_  ):





								"""simple docstring"""






								_snake_case   						=		self.rear.prev

								# All nodes other than self.head are guaranteed to have non-None previous
								assert previous is not None

								_snake_case   						=		node
								_snake_case   						=		previous
								_snake_case   						=		node
								_snake_case   						=		self.rear




							def  lowerCamelCase				(       self    ,   lowerCAmelCase_  ):





								"""simple docstring"""






								if node.prev is None or node.next is None:
									return None

								_snake_case   						=		node.next
								_snake_case   						=		node.prev
								_snake_case   						=		None
								_snake_case   						=		None
								return node


class 						__UpperCAmelCase      (					Generic[T, U]						):
							__lowercase      		=       {}


							def __init__(       self    ,   lowerCAmelCase_  ):





								"""simple docstring"""






								_snake_case   						=		DoubleLinkedList()
								_snake_case   						=		capacity
								_snake_case   						=		0
								_snake_case   						=		0
								_snake_case   						=		0
								_snake_case   						=		{}


							def __repr__(       self  ):





								"""simple docstring"""






								return (
								    F'CacheInfo(hits={self.hits}, misses={self.miss}, '
								    F'capacity={self.capacity}, current size={self.num_keys})'
								)


							def __contains__(       self    ,   lowerCAmelCase_  ):





								"""simple docstring"""






								return key in self.cache


							def  lowerCamelCase				(       self    ,   lowerCAmelCase_  ):





								"""simple docstring"""






								if key in self.cache:
									self.hits += 1
									_snake_case   						=		self.cache[key]
									_snake_case   						=		self.list.remove(self.cache[key]  )
									assert node == value_node

									# node is guaranteed not None because it is in self.cache
									assert node is not None
									self.list.add(lowerCAmelCase_  )
									return node.val
								self.miss += 1
								return None


							def  lowerCamelCase				(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_  ):





								"""simple docstring"""






								if key not in self.cache:
									if self.num_keys >= self.capacity:
										# delete first node (oldest) when over capacity
										_snake_case   						=		self.list.head.next

										# guaranteed to have a non-None first node when num_keys > 0
										# explain to type checker via assertions
										assert first_node is not None
										assert first_node.key is not None
										assert (
										    self.list.remove(lowerCAmelCase_  ) is not None
										)  # node guaranteed to be in list assert node.key is not None

										del self.cache[first_node.key]
										self.num_keys -= 1
									_snake_case   						=		DoubleLinkedListNode(lowerCAmelCase_    ,   lowerCAmelCase_  )
									self.list.add(self.cache[key]  )
									self.num_keys += 1

								else:
									# bump node to the end of the list, update value
									_snake_case   						=		self.list.remove(self.cache[key]  )
									assert node is not None  # node guaranteed to be in list
									_snake_case   						=		value
									self.list.add(lowerCAmelCase_  )




							@classmethod
							def  lowerCamelCase				(       cls    ,   lowerCAmelCase_ = 1_28  ):





								"""simple docstring"""






								def cache_decorator_inner(lowerCAmelCase_  ) -> Callable[..., U]:
									def cache_decorator_wrapper(*lowerCAmelCase_  ) -> U:
										if func not in cls.decorator_function_to_instance_map:
											_snake_case   						=		LRUCache(lowerCAmelCase_  )

										_snake_case   						=		cls.decorator_function_to_instance_map[func].get(args[0]  )
										if result is None:
											_snake_case   						=		func(*lowerCAmelCase_  )
											cls.decorator_function_to_instance_map[func].put(args[0]    ,   lowerCAmelCase_  )
										return result

									def cache_info() -> LRUCache[T, U]:
										return cls.decorator_function_to_instance_map[func]

									setattr(lowerCAmelCase_    ,   'cache_info'    ,   lowerCAmelCase_  )  # noqa: B010

									return cache_decorator_wrapper

								return cache_decorator_inner


if __name__ == "__main__":
					import doctest

					doctest.testmod()



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


UpperCAmelCase	      = logging.get_logger(__name__)

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







class        A_   (			__lowerCamelCase   ):




	'''simple docstring'''


	_UpperCamelCase     :     Optional[Any]							 =       """blip_2_vision_model"""





	def __init__(       self		,    snake_case=1408		,    snake_case=6144		,    snake_case=39		,    snake_case=16		,    snake_case=224		,    snake_case=14		,    snake_case="gelu"		,    snake_case=0.00_001		,    snake_case=0.0		,    snake_case=1E-10		,    snake_case=True		,    **snake_case		,    ):
		super().__init__(**snake_case       )

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





	@classmethod
	def 	SCREAMING_SNAKE_CASE__						(       cls		,    snake_case		,    **snake_case       ):
		cls._set_token_in_kwargs(snake_case       )

		lowercase  ,					lowercase       =    cls.get_config_dict(snake_case		,    **snake_case       )

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







class        A_   (			__lowerCamelCase   ):




	'''simple docstring'''


	_UpperCamelCase     :     int							 =       """blip_2_qformer"""





	def __init__(       self		,    snake_case=3_0522		,    snake_case=768		,    snake_case=12		,    snake_case=12		,    snake_case=3072		,    snake_case="gelu"		,    snake_case=0.1		,    snake_case=0.1		,    snake_case=512		,    snake_case=0.02		,    snake_case=1E-12		,    snake_case=0		,    snake_case="absolute"		,    snake_case=2		,    snake_case=1408		,    **snake_case		,    ):
		super().__init__(pad_token_id=snake_case		,    **snake_case       )

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





	@classmethod
	def 	SCREAMING_SNAKE_CASE__						(       cls		,    snake_case		,    **snake_case       ):
		cls._set_token_in_kwargs(snake_case       )

		lowercase  ,					lowercase       =    cls.get_config_dict(snake_case		,    **snake_case       )

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







class        A_   (			__lowerCamelCase   ):




	'''simple docstring'''


	_UpperCamelCase     :     List[Any]							 =       """blip-2"""
	_UpperCamelCase     :     str							 =       True





	def __init__(       self		,    snake_case=None		,    snake_case=None		,    snake_case=None		,    snake_case=32		,    **snake_case       ):
		super().__init__(**snake_case       )

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

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

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

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

		lowercase       =    self.text_config.tie_word_embeddings
		lowercase       =    self.text_config.is_encoder_decoder

		lowercase       =    num_query_tokens
		lowercase       =    self.vision_config.hidden_size
		lowercase       =    self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
		lowercase       =    1.0
		lowercase       =    0.02





	@classmethod
	def 	SCREAMING_SNAKE_CASE__						(       cls		,    snake_case		,    snake_case		,    snake_case		,    **snake_case		,    ):
		return cls(
		    vision_config=vision_config.to_dict()		,    qformer_config=qformer_config.to_dict()		,    text_config=text_config.to_dict()		,    **snake_case		,    )





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    copy.deepcopy(self.__dict__       )
		lowercase       =    self.vision_config.to_dict()
		lowercase       =    self.qformer_config.to_dict()
		lowercase       =    self.text_config.to_dict()
		lowercase       =    self.__class__.model_type
		return output

