
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        lowerCAmelCase__      (   __lowercase	):
						UpperCamelCase_							: str	=		"bridgetower_vision_model"

						def __init__(			self       ,							a=7_68       ,							a=12       ,							a=3       ,							a=16       ,							a=2_88       ,							a=1       ,							a=1e-05       ,							a=False       ,							a=True       ,							a=False       ,							**a       ,							)					->      Any:


								'''simple docstring'''

								super().__init__(**a				)
								_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  A_				(			cls       ,							a       ,							**a				)					->      "PretrainedConfig":


								'''simple docstring'''

								_UpperCamelCase    ,	_UpperCamelCase						    =	cls.get_config_dict(a       ,							**a				)

								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(a       ,							**a				)







class        lowerCAmelCase__      (   __lowercase	):
						UpperCamelCase_							: List[str]	=		"bridgetower_text_model"

						def __init__(			self       ,							a=5_02_65       ,							a=7_68       ,							a=12       ,							a=12       ,							a=1       ,							a=30_72       ,							a="gelu"       ,							a=0.1       ,							a=0.1       ,							a=5_14       ,							a=1       ,							a=1e-05       ,							a=1       ,							a=0       ,							a=2       ,							a="absolute"       ,							a=True       ,							**a       ,							)					->      Tuple:


								'''simple docstring'''

								super().__init__(**a				)

								_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  A_				(			cls       ,							a       ,							**a				)					->      "PretrainedConfig":


								'''simple docstring'''

								_UpperCamelCase    ,	_UpperCamelCase						    =	cls.get_config_dict(a       ,							**a				)

								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(a       ,							**a				)







class        lowerCAmelCase__      (   __lowercase	):
						UpperCamelCase_							: Optional[int]	=		"bridgetower"

						def __init__(			self       ,							a=True       ,							a="gelu"       ,							a=7_68       ,							a=1       ,							a=1e-05       ,							a=False       ,							a="add"       ,							a=12       ,							a=6       ,							a=False       ,							a=False       ,							a=None       ,							a=None       ,							**a       ,							)					->      Union[str, Any]:


								'''simple docstring'''

								_UpperCamelCase						    =	kwargs.pop("""text_config_dict"""       ,							a				)
								_UpperCamelCase						    =	kwargs.pop("""vision_config_dict"""       ,							a				)

								super().__init__(**a				)
								_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(**a				)
								_UpperCamelCase						    =	BridgeTowerVisionConfig(**a				)

						@classmethod
						def  A_				(			cls       ,							a       ,							a       ,							**a				)					->      Optional[int]:


								'''simple docstring'''

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







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


								'''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


lowerCamelCase__	=							logging.get_logger(__name__)


def 							__A(lowerCAmelCase						,  lowerCAmelCase )		->				List[str]:

		"""simple docstring"""






		_UpperCamelCase						    =	nn.functional.normalize(lowerCAmelCase )
		_UpperCamelCase						    =	nn.functional.normalize(lowerCAmelCase )
		return torch.mm(lowerCAmelCase						,  normalized_text_embeds.t() )







class        lowerCAmelCase__      (   __lowercase	):
						UpperCamelCase_							: Tuple	=		CLIPConfig

						UpperCamelCase_							: str	=		["CLIPEncoderLayer"]

						def __init__(			self       ,							a				)					->      Optional[Any]:


								'''simple docstring'''

								super().__init__(a				)

								_UpperCamelCase						    =	CLIPVisionModel(config.vision_config				)
								_UpperCamelCase						    =	nn.Linear(config.vision_config.hidden_size       ,							config.projection_dim       ,							bias=a				)

								_UpperCamelCase						    =	nn.Parameter(torch.ones(17       ,							config.projection_dim				)       ,							requires_grad=a				)
								_UpperCamelCase						    =	nn.Parameter(torch.ones(3       ,							config.projection_dim				)       ,							requires_grad=a				)

								_UpperCamelCase						    =	nn.Parameter(torch.ones(17				)       ,							requires_grad=a				)
								_UpperCamelCase						    =	nn.Parameter(torch.ones(3				)       ,							requires_grad=a				)

						@torch.no_grad()
						def  A_				(			self       ,							a       ,							a				)					->      str:


								'''simple docstring'''

								_UpperCamelCase						    =	self.vision_model(a				)[1]  # pooled_output
								_UpperCamelCase						    =	self.visual_projection(a				)

								# we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
								_UpperCamelCase						    =	cosine_distance(a       ,							self.special_care_embeds				).cpu().float().numpy()
								_UpperCamelCase						    =	cosine_distance(a       ,							self.concept_embeds				).cpu().float().numpy()

								_UpperCamelCase						    =	[]
								_UpperCamelCase						    =	image_embeds.shape[0]
								for i in range(a				):
										_UpperCamelCase						    =	{"""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
										_UpperCamelCase						    =	0.0

										for concept_idx in range(len(special_cos_dist[0]				)				):
												_UpperCamelCase						    =	special_cos_dist[i][concept_idx]
												_UpperCamelCase						    =	self.special_care_embeds_weights[concept_idx].item()
												_UpperCamelCase						    =	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]}				)
														_UpperCamelCase						    =	0.01

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

										result.append(a				)

								_UpperCamelCase						    =	[len(res["""bad_concepts"""]				) > 0 for res in result]

								return images, has_nsfw_concepts







						@torch.no_grad()
						def  A_				(			self       ,							a       ,							a				)					->      Any:


								'''simple docstring'''

								_UpperCamelCase						    =	self.vision_model(a				)[1]  # pooled_output
								_UpperCamelCase						    =	self.visual_projection(a				)

								_UpperCamelCase						    =	cosine_distance(a       ,							self.special_care_embeds				)
								_UpperCamelCase						    =	cosine_distance(a       ,							self.concept_embeds				)

								# increase this value to create a stronger `nsfw` filter
								# at the cost of increasing the possibility of filtering benign images
								_UpperCamelCase						    =	0.0

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

								_UpperCamelCase						    =	(cos_dist - self.concept_embeds_weights) + special_adjustment
								# concept_scores = concept_scores.round(decimals=3)
								_UpperCamelCase						    =	torch.any(concept_scores > 0       ,							dim=1				)

								return images, has_nsfw_concepts



