



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


lowerCAmelCase__					  =  {
    '''configuration_clip''': [
        '''CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''CLIPConfig''',
        '''CLIPOnnxConfig''',
        '''CLIPTextConfig''',
        '''CLIPVisionConfig''',
    ],
    '''processing_clip''': ['''CLIPProcessor'''],
    '''tokenization_clip''': ['''CLIPTokenizer'''],
}

try:
						if not is_tokenizers_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowerCAmelCase__					  =  ['''CLIPTokenizerFast''']

try:
						if not is_vision_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowerCAmelCase__					  =  ['''CLIPFeatureExtractor''']
						lowerCAmelCase__					  =  ['''CLIPImageProcessor''']

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowerCAmelCase__					  =  [
						    '''CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''',
						    '''CLIPModel''',
						    '''CLIPPreTrainedModel''',
						    '''CLIPTextModel''',
						    '''CLIPTextModelWithProjection''',
						    '''CLIPVisionModel''',
						    '''CLIPVisionModelWithProjection''',
						]

try:
						if not is_tf_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowerCAmelCase__					  =  [
						    '''TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''',
						    '''TFCLIPModel''',
						    '''TFCLIPPreTrainedModel''',
						    '''TFCLIPTextModel''',
						    '''TFCLIPVisionModel''',
						]

try:
						if not is_flax_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowerCAmelCase__					  =  [
						    '''FlaxCLIPModel''',
						    '''FlaxCLIPPreTrainedModel''',
						    '''FlaxCLIPTextModel''',
						    '''FlaxCLIPTextPreTrainedModel''',
						    '''FlaxCLIPVisionModel''',
						    '''FlaxCLIPVisionPreTrainedModel''',
						]


if TYPE_CHECKING:
						from .configuration_clip import (
						    CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
						    CLIPConfig,
						    CLIPOnnxConfig,
						    CLIPTextConfig,
						    CLIPVisionConfig,
						)
						from .processing_clip import CLIPProcessor
						from .tokenization_clip import CLIPTokenizer

						try:
												if not is_tokenizers_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .tokenization_clip_fast import CLIPTokenizerFast

						try:
												if not is_vision_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .feature_extraction_clip import CLIPFeatureExtractor
												from .image_processing_clip import CLIPImageProcessor

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_clip import (
												    CLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
												    CLIPModel,
												    CLIPPreTrainedModel,
												    CLIPTextModel,
												    CLIPTextModelWithProjection,
												    CLIPVisionModel,
												    CLIPVisionModelWithProjection,
												)

						try:
												if not is_tf_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_tf_clip import (
												    TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
												    TFCLIPModel,
												    TFCLIPPreTrainedModel,
												    TFCLIPTextModel,
												    TFCLIPVisionModel,
												)

						try:
												if not is_flax_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_flax_clip import (
												    FlaxCLIPModel,
												    FlaxCLIPPreTrainedModel,
												    FlaxCLIPTextModel,
												    FlaxCLIPTextPreTrainedModel,
												    FlaxCLIPVisionModel,
												    FlaxCLIPVisionPreTrainedModel,
												)


else:
						import sys

						lowerCAmelCase__					  =  _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)









import copy
import os
from typing import Union

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


lowerCAmelCase__					  =  logging.get_logger(__name__)

lowerCAmelCase__					  =  {
    '''BridgeTower/bridgetower-base''': '''https://huggingface.co/BridgeTower/bridgetower-base/blob/main/config.json''',
    '''BridgeTower/bridgetower-base-itm-mlm''': (
        '''https://huggingface.co/BridgeTower/bridgetower-base-itm-mlm/blob/main/config.json'''
    ),
}

class 				snake_case__(_UpperCamelCase      ):

					"""simple docstring"""






					lowercase_								=		"""bridgetower_vision_model"""







					def __init__(							self :     str				,       SCREAMING_SNAKE_CASE :     Dict=768				,       SCREAMING_SNAKE_CASE :     Union[str, Any]=12				,       SCREAMING_SNAKE_CASE :     Dict=3				,       SCREAMING_SNAKE_CASE :     Dict=16				,       SCREAMING_SNAKE_CASE :     List[Any]=288				,       SCREAMING_SNAKE_CASE :     List[Any]=1				,       SCREAMING_SNAKE_CASE :     List[str]=1E-0_5				,       SCREAMING_SNAKE_CASE :     str=False				,       SCREAMING_SNAKE_CASE :     Any=True				,       SCREAMING_SNAKE_CASE :     Tuple=False				,       **SCREAMING_SNAKE_CASE :     List[str]				,       ):
									super().__init__(**SCREAMING_SNAKE_CASE					)
									lowercase__					:   Union[str, Any]													=					hidden_size
									lowercase__					:   str													=					num_hidden_layers
									lowercase__					:   str													=					num_channels
									lowercase__					:   Optional[int]													=					patch_size
									lowercase__					:   Dict													=					image_size
									lowercase__					:   List[Any]													=					initializer_factor
									lowercase__					:   int													=					layer_norm_eps
									lowercase__					:   List[str]													=					stop_gradient
									lowercase__					:   Optional[int]													=					share_layernorm
									lowercase__					:   Optional[int]													=					remove_last_layer







					@classmethod
					def 			snake_case					(							cls :     Tuple				,       SCREAMING_SNAKE_CASE :     Union[str, os.PathLike]				,       **SCREAMING_SNAKE_CASE :     str					):
									lowercase__							,							lowercase__					:   Optional[Any]													=					cls.get_config_dict(SCREAMING_SNAKE_CASE				,       **SCREAMING_SNAKE_CASE					)

									if config_dict.get("model_type"					) == "bridgetower":
													lowercase__					:   Optional[int]													=					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(SCREAMING_SNAKE_CASE				,       **SCREAMING_SNAKE_CASE					)






class 				snake_case__(_UpperCamelCase      ):

					"""simple docstring"""






					lowercase_								=		"""bridgetower_text_model"""







					def __init__(							self :     Tuple				,       SCREAMING_SNAKE_CASE :     Union[str, Any]=50_265				,       SCREAMING_SNAKE_CASE :     Optional[int]=768				,       SCREAMING_SNAKE_CASE :     List[str]=12				,       SCREAMING_SNAKE_CASE :     Union[str, Any]=12				,       SCREAMING_SNAKE_CASE :     List[Any]=1				,       SCREAMING_SNAKE_CASE :     Any=3_072				,       SCREAMING_SNAKE_CASE :     Union[str, Any]="gelu"				,       SCREAMING_SNAKE_CASE :     Optional[int]=0.1				,       SCREAMING_SNAKE_CASE :     List[str]=0.1				,       SCREAMING_SNAKE_CASE :     Tuple=514				,       SCREAMING_SNAKE_CASE :     List[str]=1				,       SCREAMING_SNAKE_CASE :     Dict=1E-0_5				,       SCREAMING_SNAKE_CASE :     List[str]=1				,       SCREAMING_SNAKE_CASE :     str=0				,       SCREAMING_SNAKE_CASE :     List[Any]=2				,       SCREAMING_SNAKE_CASE :     Optional[Any]="absolute"				,       SCREAMING_SNAKE_CASE :     int=True				,       **SCREAMING_SNAKE_CASE :     int				,       ):
									super().__init__(**SCREAMING_SNAKE_CASE					)

									lowercase__					:   Any													=					vocab_size
									lowercase__					:   Optional[Any]													=					hidden_size
									lowercase__					:   Optional[int]													=					num_hidden_layers
									lowercase__					:   Optional[int]													=					num_attention_heads
									lowercase__					:   int													=					hidden_act
									lowercase__					:   Optional[Any]													=					initializer_factor
									lowercase__					:   Dict													=					intermediate_size
									lowercase__					:   Tuple													=					hidden_dropout_prob
									lowercase__					:   List[Any]													=					attention_probs_dropout_prob
									lowercase__					:   Optional[int]													=					max_position_embeddings
									lowercase__					:   str													=					type_vocab_size
									lowercase__					:   str													=					layer_norm_eps
									lowercase__					:   Dict													=					position_embedding_type
									lowercase__					:   Optional[Any]													=					use_cache
									lowercase__					:   List[str]													=					pad_token_id
									lowercase__					:   Optional[Any]													=					bos_token_id
									lowercase__					:   Optional[Any]													=					eos_token_id







					@classmethod
					def 			snake_case					(							cls :     Optional[int]				,       SCREAMING_SNAKE_CASE :     Union[str, os.PathLike]				,       **SCREAMING_SNAKE_CASE :     str					):
									lowercase__							,							lowercase__					:   Optional[Any]													=					cls.get_config_dict(SCREAMING_SNAKE_CASE				,       **SCREAMING_SNAKE_CASE					)

									if config_dict.get("model_type"					) == "bridgetower":
													lowercase__					:   List[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(SCREAMING_SNAKE_CASE				,       **SCREAMING_SNAKE_CASE					)






class 				snake_case__(_UpperCamelCase      ):

					"""simple docstring"""






					lowercase_								=		"""bridgetower"""







					def __init__(							self :     Tuple				,       SCREAMING_SNAKE_CASE :     Optional[int]=True				,       SCREAMING_SNAKE_CASE :     Dict="gelu"				,       SCREAMING_SNAKE_CASE :     Any=768				,       SCREAMING_SNAKE_CASE :     Dict=1				,       SCREAMING_SNAKE_CASE :     Union[str, Any]=1E-0_5				,       SCREAMING_SNAKE_CASE :     Tuple=False				,       SCREAMING_SNAKE_CASE :     List[Any]="add"				,       SCREAMING_SNAKE_CASE :     Any=12				,       SCREAMING_SNAKE_CASE :     Any=6				,       SCREAMING_SNAKE_CASE :     Any=False				,       SCREAMING_SNAKE_CASE :     List[Any]=False				,       SCREAMING_SNAKE_CASE :     str=None				,       SCREAMING_SNAKE_CASE :     Dict=None				,       **SCREAMING_SNAKE_CASE :     List[Any]				,       ):
									# TODO: remove this once the Hub files are updated.
									lowercase__					:   int													=					kwargs.pop("text_config_dict"				,       SCREAMING_SNAKE_CASE					)
									lowercase__					:   int													=					kwargs.pop("vision_config_dict"				,       SCREAMING_SNAKE_CASE					)

									super().__init__(**SCREAMING_SNAKE_CASE					)
									lowercase__					:   Dict													=					share_cross_modal_transformer_layers
									lowercase__					:   int													=					hidden_act
									lowercase__					:   int													=					hidden_size
									lowercase__					:   Optional[int]													=					initializer_factor
									lowercase__					:   Optional[int]													=					layer_norm_eps
									lowercase__					:   str													=					share_link_tower_layers
									lowercase__					:   Optional[int]													=					link_tower_type
									lowercase__					:   List[Any]													=					num_attention_heads
									lowercase__					:   Dict													=					num_hidden_layers
									lowercase__					:   Dict													=					tie_word_embeddings
									lowercase__					:   Optional[Any]													=					init_layernorm_from_vision_encoder

									if text_config is None:
													lowercase__					:   Optional[Any]													=					{}
													logger.info("`text_config` is `None`. Initializing the `BridgeTowerTextConfig` with default values."					)

									if vision_config is None:
													lowercase__					:   List[Any]													=					{}
													logger.info("`vision_config` is `None`. Initializing the `BridgeTowerVisionConfig` with default values."					)

									lowercase__					:   Any													=					BridgeTowerTextConfig(**SCREAMING_SNAKE_CASE					)
									lowercase__					:   Dict													=					BridgeTowerVisionConfig(**SCREAMING_SNAKE_CASE					)







					@classmethod
					def 			snake_case					(							cls :     Optional[int]				,       SCREAMING_SNAKE_CASE :     BridgeTowerTextConfig				,       SCREAMING_SNAKE_CASE :     BridgeTowerVisionConfig				,       **SCREAMING_SNAKE_CASE :     List[str]					):
									return cls(text_config=text_config.to_dict()				,       vision_config=vision_config.to_dict()				,       **SCREAMING_SNAKE_CASE					)







					def 			snake_case					(							self :     Optional[int]					):
									lowercase__					:   List[str]													=					copy.deepcopy(self.__dict__					)
									lowercase__					:   Dict													=					self.text_config.to_dict()
									lowercase__					:   Tuple													=					self.vision_config.to_dict()
									lowercase__					:   Optional[Any]													=					self.__class__.model_type
									return output




