






import copy
import os
from typing import Union

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


__magic_name__   :  Any        =							logging.get_logger(__name__)

__magic_name__   :  int        =							{
    """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 					A__     (     __snake_case ):

     '''simple docstring'''






     snake_case__					=					"""bridgetower_vision_model"""







     def __init__( self      :    Union[str, Any]   ,						_SCREAMING_SNAKE_CASE      :    Dict=768   ,						_SCREAMING_SNAKE_CASE      :    str=12   ,						_SCREAMING_SNAKE_CASE      :    List[Any]=3   ,						_SCREAMING_SNAKE_CASE      :    Any=16   ,						_SCREAMING_SNAKE_CASE      :    Any=288   ,						_SCREAMING_SNAKE_CASE      :    Any=1   ,						_SCREAMING_SNAKE_CASE      :    Any=1E-0_5   ,						_SCREAMING_SNAKE_CASE      :    str=False   ,						_SCREAMING_SNAKE_CASE      :    str=True   ,						_SCREAMING_SNAKE_CASE      :    Optional[int]=False   ,						**_SCREAMING_SNAKE_CASE      :    List[str]   ,						):






           """simple docstring"""




           super().__init__(**_SCREAMING_SNAKE_CASE )
           UpperCamelCase					       =  hidden_size
           UpperCamelCase					       =  num_hidden_layers
           UpperCamelCase					       =  num_channels
           UpperCamelCase					       =  patch_size
           UpperCamelCase					       =  image_size
           UpperCamelCase					       =  initializer_factor
           UpperCamelCase					       =  layer_norm_eps
           UpperCamelCase					       =  stop_gradient
           UpperCamelCase					       =  share_layernorm
           UpperCamelCase					       =  remove_last_layer







     @classmethod
     def   _SCREAMING_SNAKE_CASE     ( cls      :    Tuple   ,						_SCREAMING_SNAKE_CASE      :    Union[str, os.PathLike]   ,						**_SCREAMING_SNAKE_CASE      :    Dict ):






           """simple docstring"""




           UpperCamelCase					       =  cls.get_config_dict(_SCREAMING_SNAKE_CASE   ,						**_SCREAMING_SNAKE_CASE )

           if config_dict.get('model_type' ) == "bridgetower":
                 UpperCamelCase					       =  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 					A__     (     __snake_case ):

     '''simple docstring'''






     snake_case__					=					"""bridgetower_text_model"""







     def __init__( self      :    Any   ,						_SCREAMING_SNAKE_CASE      :    Optional[Any]=5_0265   ,						_SCREAMING_SNAKE_CASE      :    Tuple=768   ,						_SCREAMING_SNAKE_CASE      :    Any=12   ,						_SCREAMING_SNAKE_CASE      :    Dict=12   ,						_SCREAMING_SNAKE_CASE      :    Dict=1   ,						_SCREAMING_SNAKE_CASE      :    str=3072   ,						_SCREAMING_SNAKE_CASE      :    Dict="gelu"   ,						_SCREAMING_SNAKE_CASE      :    Dict=0.1   ,						_SCREAMING_SNAKE_CASE      :    List[Any]=0.1   ,						_SCREAMING_SNAKE_CASE      :    Any=514   ,						_SCREAMING_SNAKE_CASE      :    Tuple=1   ,						_SCREAMING_SNAKE_CASE      :    List[Any]=1E-0_5   ,						_SCREAMING_SNAKE_CASE      :    Any=1   ,						_SCREAMING_SNAKE_CASE      :    str=0   ,						_SCREAMING_SNAKE_CASE      :    Union[str, Any]=2   ,						_SCREAMING_SNAKE_CASE      :    Optional[Any]="absolute"   ,						_SCREAMING_SNAKE_CASE      :    Optional[Any]=True   ,						**_SCREAMING_SNAKE_CASE      :    Any   ,						):






           """simple docstring"""




           super().__init__(**_SCREAMING_SNAKE_CASE )

           UpperCamelCase					       =  vocab_size
           UpperCamelCase					       =  hidden_size
           UpperCamelCase					       =  num_hidden_layers
           UpperCamelCase					       =  num_attention_heads
           UpperCamelCase					       =  hidden_act
           UpperCamelCase					       =  initializer_factor
           UpperCamelCase					       =  intermediate_size
           UpperCamelCase					       =  hidden_dropout_prob
           UpperCamelCase					       =  attention_probs_dropout_prob
           UpperCamelCase					       =  max_position_embeddings
           UpperCamelCase					       =  type_vocab_size
           UpperCamelCase					       =  layer_norm_eps
           UpperCamelCase					       =  position_embedding_type
           UpperCamelCase					       =  use_cache
           UpperCamelCase					       =  pad_token_id
           UpperCamelCase					       =  bos_token_id
           UpperCamelCase					       =  eos_token_id







     @classmethod
     def   _SCREAMING_SNAKE_CASE     ( cls      :    Optional[int]   ,						_SCREAMING_SNAKE_CASE      :    Union[str, os.PathLike]   ,						**_SCREAMING_SNAKE_CASE      :    Tuple ):






           """simple docstring"""




           UpperCamelCase					       =  cls.get_config_dict(_SCREAMING_SNAKE_CASE   ,						**_SCREAMING_SNAKE_CASE )

           if config_dict.get('model_type' ) == "bridgetower":
                 UpperCamelCase					       =  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 					A__     (     __snake_case ):

     '''simple docstring'''






     snake_case__					=					"""bridgetower"""







     def __init__( self      :    Optional[int]   ,						_SCREAMING_SNAKE_CASE      :    Union[str, Any]=True   ,						_SCREAMING_SNAKE_CASE      :    int="gelu"   ,						_SCREAMING_SNAKE_CASE      :    List[Any]=768   ,						_SCREAMING_SNAKE_CASE      :    Optional[Any]=1   ,						_SCREAMING_SNAKE_CASE      :    List[Any]=1E-0_5   ,						_SCREAMING_SNAKE_CASE      :    List[str]=False   ,						_SCREAMING_SNAKE_CASE      :    Optional[int]="add"   ,						_SCREAMING_SNAKE_CASE      :    str=12   ,						_SCREAMING_SNAKE_CASE      :    Any=6   ,						_SCREAMING_SNAKE_CASE      :    str=False   ,						_SCREAMING_SNAKE_CASE      :    Tuple=False   ,						_SCREAMING_SNAKE_CASE      :    List[Any]=None   ,						_SCREAMING_SNAKE_CASE      :    List[Any]=None   ,						**_SCREAMING_SNAKE_CASE      :    Dict   ,						):






           """simple docstring"""




           UpperCamelCase					       =  kwargs.pop('text_config_dict'   ,						_SCREAMING_SNAKE_CASE )
           UpperCamelCase					       =  kwargs.pop('vision_config_dict'   ,						_SCREAMING_SNAKE_CASE )

           super().__init__(**_SCREAMING_SNAKE_CASE )
           UpperCamelCase					       =  share_cross_modal_transformer_layers
           UpperCamelCase					       =  hidden_act
           UpperCamelCase					       =  hidden_size
           UpperCamelCase					       =  initializer_factor
           UpperCamelCase					       =  layer_norm_eps
           UpperCamelCase					       =  share_link_tower_layers
           UpperCamelCase					       =  link_tower_type
           UpperCamelCase					       =  num_attention_heads
           UpperCamelCase					       =  num_hidden_layers
           UpperCamelCase					       =  tie_word_embeddings
           UpperCamelCase					       =  init_layernorm_from_vision_encoder

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

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

           UpperCamelCase					       =  BridgeTowerTextConfig(**_SCREAMING_SNAKE_CASE )
           UpperCamelCase					       =  BridgeTowerVisionConfig(**_SCREAMING_SNAKE_CASE )







     @classmethod
     def   _SCREAMING_SNAKE_CASE     ( cls      :    str   ,						_SCREAMING_SNAKE_CASE      :    BridgeTowerTextConfig   ,						_SCREAMING_SNAKE_CASE      :    BridgeTowerVisionConfig   ,						**_SCREAMING_SNAKE_CASE      :    Any ):






           """simple docstring"""




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







     def   _SCREAMING_SNAKE_CASE     ( self      :    Tuple ):






           """simple docstring"""




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





import torch
import torch.nn as nn
from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel

from ...utils import logging


UpperCamelCase_						:     int	       =							logging.get_logger(__name__)



def 		UpperCamelCase     (       _UpperCAmelCase  :   Optional[int] ,			_UpperCAmelCase  :   Dict  )   ->			Any:






						'''simple docstring'''

						_lowercase       :	List[Any]           =   nn.functional.normalize(_UpperCAmelCase  )
						_lowercase       :	List[str]           =   nn.functional.normalize(_UpperCAmelCase  )
						return torch.mm(_UpperCAmelCase ,			normalized_text_embeds.t()  )




class   __lowercase						(						__snake_case       ):
	_A       =   CLIPConfig

	_A       =   ["CLIPEncoderLayer"]






	def __init__(self				: List[str]      ,	snake_case				: CLIPConfig    ) ->			Optional[int]:
							super().__init__(snake_case    )

							_lowercase       :	Tuple           =   CLIPVisionModel(config.vision_config    )
							_lowercase       :	Any           =   nn.Linear(config.vision_config.hidden_size      ,	config.projection_dim      ,	bias=snake_case    )

							_lowercase       :	int           =   nn.Parameter(torch.ones(17      ,	config.projection_dim    )      ,	requires_grad=snake_case    )
							_lowercase       :	str           =   nn.Parameter(torch.ones(3      ,	config.projection_dim    )      ,	requires_grad=snake_case    )

							_lowercase       :	Any           =   nn.Parameter(torch.ones(17    )      ,	requires_grad=snake_case    )
							_lowercase       :	Dict           =   nn.Parameter(torch.ones(3    )      ,	requires_grad=snake_case    )






	@torch.no_grad()
	def       _a(self				: int      ,	snake_case				: Tuple      ,	snake_case				: Optional[Any]    ) ->			Union[str, Any]:
							_lowercase       :	str           =   self.vision_model(snake_case    )[1]  # pooled_output
							_lowercase       :	List[Any]           =   self.visual_projection(snake_case    )

							# we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
							_lowercase       :	str           =   cosine_distance(snake_case      ,	self.special_care_embeds    ).cpu().float().numpy()
							_lowercase       :	Optional[int]           =   cosine_distance(snake_case      ,	self.concept_embeds    ).cpu().float().numpy()

							_lowercase       :	List[str]           =   []
							_lowercase       :	int           =   image_embeds.shape[0]
							for i in range(snake_case    ):
													_lowercase       :	int           =   {"special_scores": {}, "special_care": [], "concept_scores": {}, "bad_concepts": []}

													# increase this value to create a stronger `nfsw` filter
													# at the cost of increasing the possibility of filtering benign images
													_lowercase       :	Optional[int]           =   0.0

													for concept_idx in range(len(special_cos_dist[0]    )    ):
																			_lowercase       :	Union[str, Any]           =   special_cos_dist[i][concept_idx]
																			_lowercase       :	Optional[int]           =   self.special_care_embeds_weights[concept_idx].item()
																			_lowercase       :	Optional[int]           =   round(concept_cos - concept_threshold + adjustment      ,	3    )
																			if result_img["special_scores"][concept_idx] > 0:
																									result_img["special_care"].append({concept_idx, result_img["special_scores"][concept_idx]}    )
																									_lowercase       :	Union[str, Any]           =   0.01

													for concept_idx in range(len(cos_dist[0]    )    ):
																			_lowercase       :	Tuple           =   cos_dist[i][concept_idx]
																			_lowercase       :	Union[str, Any]           =   self.concept_embeds_weights[concept_idx].item()
																			_lowercase       :	str           =   round(concept_cos - concept_threshold + adjustment      ,	3    )
																			if result_img["concept_scores"][concept_idx] > 0:
																									result_img["bad_concepts"].append(snake_case    )

													result.append(snake_case    )

							_lowercase       :	Dict           =   [len(res["bad_concepts"]    ) > 0 for res in result]

							return images, has_nsfw_concepts






	@torch.no_grad()
	def       _a(self				: str      ,	snake_case				: torch.FloatTensor      ,	snake_case				: torch.FloatTensor    ) ->			List[str]:
							_lowercase       :	Dict           =   self.vision_model(snake_case    )[1]  # pooled_output
							_lowercase       :	Any           =   self.visual_projection(snake_case    )

							_lowercase       :	Optional[Any]           =   cosine_distance(snake_case      ,	self.special_care_embeds    )
							_lowercase       :	Optional[Any]           =   cosine_distance(snake_case      ,	self.concept_embeds    )

							# increase this value to create a stronger `nsfw` filter
							# at the cost of increasing the possibility of filtering benign images
							_lowercase       :	Optional[int]           =   0.0

							_lowercase       :	Optional[int]           =   special_cos_dist - self.special_care_embeds_weights + adjustment
							# special_scores = special_scores.round(decimals=3)
							_lowercase       :	List[str]           =   torch.any(special_scores > 0      ,	dim=1    )
							_lowercase       :	List[Any]           =   special_care * 0.01
							_lowercase       :	Optional[Any]           =   special_adjustment.unsqueeze(1    ).expand(-1      ,	cos_dist.shape[1]    )

							_lowercase       :	Any           =   (cos_dist - self.concept_embeds_weights) + special_adjustment
							# concept_scores = concept_scores.round(decimals=3)
							_lowercase       :	List[Any]           =   torch.any(concept_scores > 0      ,	dim=1    )

							return images, has_nsfw_concepts




