





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


_a						=   logging.get_logger(__name__)

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






class       __A					(					lowerCAmelCase				):

			'''simple docstring'''







			lowerCAmelCase_							  =		"""blip_2_vision_model"""





			def __init__(					self ,						__lowerCAmelCase=1_4_0_8 ,						__lowerCAmelCase=6_1_4_4 ,						__lowerCAmelCase=3_9 ,						__lowerCAmelCase=1_6 ,						__lowerCAmelCase=2_2_4 ,						__lowerCAmelCase=1_4 ,						__lowerCAmelCase="gelu" ,						__lowerCAmelCase=0.0_0001 ,						__lowerCAmelCase=0.0 ,						__lowerCAmelCase=1E-10 ,						__lowerCAmelCase=True ,						**__lowerCAmelCase ,						):



				'''simple docstring'''







				super().__init__(**__lowerCAmelCase							)

				lowerCamelCase__              =	hidden_size
				lowerCamelCase__              =	intermediate_size
				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
				lowerCamelCase__              =	qkv_bias





			@classmethod
			def 	__lowerCamelCase       (					cls ,						__lowerCAmelCase ,						**__lowerCAmelCase							):



				'''simple docstring'''







				cls._set_token_in_kwargs(__lowerCAmelCase							)

				lowerCamelCase__       ,			lowerCamelCase__              =	cls.get_config_dict(__lowerCAmelCase ,						**__lowerCAmelCase							)

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






class       __A					(					lowerCAmelCase				):

			'''simple docstring'''







			lowerCAmelCase_							  =		"""blip_2_qformer"""





			def __init__(					self ,						__lowerCAmelCase=3_0_5_2_2 ,						__lowerCAmelCase=7_6_8 ,						__lowerCAmelCase=1_2 ,						__lowerCAmelCase=1_2 ,						__lowerCAmelCase=3_0_7_2 ,						__lowerCAmelCase="gelu" ,						__lowerCAmelCase=0.1 ,						__lowerCAmelCase=0.1 ,						__lowerCAmelCase=5_1_2 ,						__lowerCAmelCase=0.02 ,						__lowerCAmelCase=1E-12 ,						__lowerCAmelCase=0 ,						__lowerCAmelCase="absolute" ,						__lowerCAmelCase=2 ,						__lowerCAmelCase=1_4_0_8 ,						**__lowerCAmelCase ,						):



				'''simple docstring'''







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

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





			@classmethod
			def 	__lowerCamelCase       (					cls ,						__lowerCAmelCase ,						**__lowerCAmelCase							):



				'''simple docstring'''







				cls._set_token_in_kwargs(__lowerCAmelCase							)

				lowerCamelCase__       ,			lowerCamelCase__              =	cls.get_config_dict(__lowerCAmelCase ,						**__lowerCAmelCase							)

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






class       __A					(					lowerCAmelCase				):

			'''simple docstring'''







			lowerCAmelCase_							  =		"""blip-2"""
			lowerCAmelCase_							  =		True





			def __init__(					self ,						__lowerCAmelCase=None ,						__lowerCAmelCase=None ,						__lowerCAmelCase=None ,						__lowerCAmelCase=3_2 ,						**__lowerCAmelCase							):



				'''simple docstring'''







				super().__init__(**__lowerCAmelCase							)

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

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

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

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

				lowerCamelCase__              =	self.text_config.tie_word_embeddings
				lowerCamelCase__              =	self.text_config.is_encoder_decoder

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





			@classmethod
			def 	__lowerCamelCase       (					cls ,						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase ,						**__lowerCAmelCase ,						):



				'''simple docstring'''







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





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	copy.deepcopy(self.__dict__							)
				lowerCamelCase__              =	self.vision_config.to_dict()
				lowerCamelCase__              =	self.qformer_config.to_dict()
				lowerCamelCase__              =	self.text_config.to_dict()
				lowerCamelCase__              =	self.__class__.model_type
				return output










from __future__ import annotations





def lowerCAmelCase__(__snake_case ,__snake_case ,__snake_case							)       ->   float:







	'''simple docstring'''







	if days_between_payments <= 0:
		raise ValueError('''days_between_payments must be > 0'''							)
	if daily_interest_rate < 0:
		raise ValueError('''daily_interest_rate must be >= 0'''							)
	if principal <= 0:
		raise ValueError('''principal must be > 0'''							)
	return principal * daily_interest_rate * days_between_payments





def lowerCAmelCase__(__snake_case ,__snake_case ,__snake_case ,)       ->   float:







	'''simple docstring'''







	if number_of_compounding_periods <= 0:
		raise ValueError('''number_of_compounding_periods must be > 0'''							)
	if nominal_annual_interest_rate_percentage < 0:
		raise ValueError('''nominal_annual_interest_rate_percentage must be >= 0'''							)
	if principal <= 0:
		raise ValueError('''principal must be > 0'''							)

	return principal * (
	    (1 + nominal_annual_interest_rate_percentage) ** number_of_compounding_periods
	    - 1
	)





def lowerCAmelCase__(__snake_case ,__snake_case ,__snake_case ,)       ->   float:







	'''simple docstring'''







	if number_of_years <= 0:
		raise ValueError('''number_of_years must be > 0'''							)
	if nominal_annual_percentage_rate < 0:
		raise ValueError('''nominal_annual_percentage_rate must be >= 0'''							)
	if principal <= 0:
		raise ValueError('''principal must be > 0'''							)

	return compound_interest(
	    __snake_case ,nominal_annual_percentage_rate / 365 ,number_of_years * 365							)


if __name__ == "__main__":
			import doctest

			doctest.testmod()



