



'''simple docstring'''





import copy
import os
from typing import Union

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


__snake_case       :	List[str]            =logging.get_logger(__name__)

__snake_case       :	Tuple            ={
    '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  lowerCamelCase__    (							lowerCamelCase__):




		'''simple docstring'''



		snake_case_			="""blip_text_model"""



		def __init__(self	,__lowerCamelCase=3_05_24	,__lowerCamelCase=7_68	,__lowerCamelCase=7_68	,__lowerCamelCase=30_72	,__lowerCamelCase=7_68	,__lowerCamelCase=12	,__lowerCamelCase=8	,__lowerCamelCase=5_12	,__lowerCamelCase="gelu"	,__lowerCamelCase=1e-12	,__lowerCamelCase=0.0	,__lowerCamelCase=0.0	,__lowerCamelCase=0.02	,__lowerCamelCase=3_05_22	,__lowerCamelCase=2	,__lowerCamelCase=0	,__lowerCamelCase=1_02	,__lowerCamelCase=True	,__lowerCamelCase=True	,**__lowerCamelCase	,)  ->    Dict:

						"""simple docstring"""






						super().__init__(
						    pad_token_id=__lowerCamelCase	,bos_token_id=__lowerCamelCase	,eos_token_id=__lowerCamelCase	,sep_token_id=__lowerCamelCase	,**__lowerCamelCase	,)

						lowerCAmelCase__  :       Any				=  vocab_size
						lowerCAmelCase__  :       List[Any]				=  hidden_size
						lowerCAmelCase__  :       Dict				=  encoder_hidden_size
						lowerCAmelCase__  :       List[str]				=  intermediate_size
						lowerCAmelCase__  :       List[Any]				=  projection_dim
						lowerCAmelCase__  :       Any				=  hidden_dropout_prob
						lowerCAmelCase__  :       Optional[int]				=  num_hidden_layers
						lowerCAmelCase__  :       List[Any]				=  num_attention_heads
						lowerCAmelCase__  :       Optional[int]				=  max_position_embeddings
						lowerCAmelCase__  :       List[Any]				=  layer_norm_eps
						lowerCAmelCase__  :       Union[str, Any]				=  hidden_act
						lowerCAmelCase__  :       Optional[int]				=  initializer_range
						lowerCAmelCase__  :       Tuple				=  attention_probs_dropout_prob
						lowerCAmelCase__  :       List[str]				=  is_decoder
						lowerCAmelCase__  :       List[Any]				=  use_cache



		@classmethod
		def 	lowerCAmelCase__   (cls	,__lowerCamelCase	,**__lowerCamelCase				)  ->    "PretrainedConfig":

						"""simple docstring"""






						cls._set_token_in_kwargs(__lowerCamelCase				)

						lowerCAmelCase__  :       List[Any]				=  cls.get_config_dict(__lowerCamelCase	,**__lowerCamelCase				)

						# get the text config dict if we are loading from BlipConfig
						if config_dict.get('''model_type'''				) == "blip":
										lowerCAmelCase__  :       Union[str, Any]				=  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(__lowerCamelCase	,**__lowerCamelCase				)



class  lowerCamelCase__    (							lowerCamelCase__):




		'''simple docstring'''



		snake_case_			="""blip_vision_model"""



		def __init__(self	,__lowerCamelCase=7_68	,__lowerCamelCase=30_72	,__lowerCamelCase=5_12	,__lowerCamelCase=12	,__lowerCamelCase=12	,__lowerCamelCase=3_84	,__lowerCamelCase=16	,__lowerCamelCase="gelu"	,__lowerCamelCase=1e-5	,__lowerCamelCase=0.0	,__lowerCamelCase=1e-10	,**__lowerCamelCase	,)  ->    List[Any]:

						"""simple docstring"""






						super().__init__(**__lowerCamelCase				)

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



		@classmethod
		def 	lowerCAmelCase__   (cls	,__lowerCamelCase	,**__lowerCamelCase				)  ->    "PretrainedConfig":

						"""simple docstring"""






						cls._set_token_in_kwargs(__lowerCamelCase				)

						lowerCAmelCase__  :       str				=  cls.get_config_dict(__lowerCamelCase	,**__lowerCamelCase				)

						# get the vision config dict if we are loading from BlipConfig
						if config_dict.get('''model_type'''				) == "blip":
										lowerCAmelCase__  :       List[str]				=  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  lowerCamelCase__    (							lowerCamelCase__):




		'''simple docstring'''



		snake_case_			="""blip"""
		snake_case_			=True



		def __init__(self	,__lowerCamelCase=None	,__lowerCamelCase=None	,__lowerCamelCase=5_12	,__lowerCamelCase=2.6592	,__lowerCamelCase=2_56	,**__lowerCamelCase	,)  ->    Dict:

						"""simple docstring"""






						super().__init__(**__lowerCamelCase				)

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

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

						lowerCAmelCase__  :       int				=  BlipTextConfig(**__lowerCamelCase				)
						lowerCAmelCase__  :       Optional[Any]				=  BlipVisionConfig(**__lowerCamelCase				)

						lowerCAmelCase__  :       List[str]				=  self.vision_config.hidden_size

						lowerCAmelCase__  :       List[Any]				=  projection_dim
						lowerCAmelCase__  :       Optional[Any]				=  logit_scale_init_value
						lowerCAmelCase__  :       Union[str, Any]				=  1.0
						lowerCAmelCase__  :       Optional[Any]				=  0.02
						lowerCAmelCase__  :       int				=  image_text_hidden_size



		@classmethod
		def 	lowerCAmelCase__   (cls	,__lowerCamelCase	,__lowerCamelCase	,**__lowerCamelCase				)  ->    Optional[int]:

						"""simple docstring"""






						return cls(text_config=text_config.to_dict()	,vision_config=vision_config.to_dict()	,**__lowerCamelCase				)



		def 	lowerCAmelCase__   (self				)  ->    Optional[int]:

						"""simple docstring"""






						lowerCAmelCase__  :       Optional[Any]				=  copy.deepcopy(self.__dict__				)
						lowerCAmelCase__  :       Tuple				=  self.text_config.to_dict()
						lowerCAmelCase__  :       Union[str, Any]				=  self.vision_config.to_dict()
						lowerCAmelCase__  :       str				=  self.__class__.model_type
						return output






from bisect import bisect
from itertools import accumulate






def 			lowerCAmelCase__	(					lowerCamelCase_	:	List[str] ,lowerCamelCase_	:	Optional[Any] ,lowerCamelCase_	:	List[str] ,lowerCamelCase_	:	Optional[int]):





				'''simple docstring'''

				lowerCAmelCase__  :       Any				=  sorted(zip(lowerCamelCase_ ,lowerCamelCase_) ,key=lambda lowerCamelCase_: x[0] / x[1] ,reverse=lowerCamelCase_)
				lowerCAmelCase__		,	lowerCAmelCase__  :       List[str]				=  [i[0] for i in r], [i[1] for i in r]
				lowerCAmelCase__  :       Tuple				=  list(accumulate(lowerCamelCase_))
				lowerCAmelCase__  :       str				=  bisect(lowerCamelCase_ ,lowerCamelCase_)
				return (
				    0
				    if k == 0
				    else sum(vl[:k]) + (w - acc[k - 1]) * (vl[k]) / (wt[k])
				    if k != n
				    else sum(vl[:k])
				)


if __name__ == "__main__":
							import doctest

							doctest.testmod()

