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

from ...utils import logging


lowerCamelCase_    :			Optional[int]    =	logging.get_logger(__name__)







def 							A__       (    lowerCamelCase  ,     lowerCamelCase    )	->						List[str]:
				UpperCamelCase_:				Optional[int]						=    nn.functional.normalize(lowerCamelCase    )
				UpperCamelCase_:				Any						=    nn.functional.normalize(lowerCamelCase    )
				return torch.mm(lowerCamelCase  ,     normalized_text_embeds.t()    )





class 							_UpperCamelCase					(       _A	):






			'''simple docstring'''

			__UpperCamelCase   :    Union[str, Any]          =			CLIPConfig

			__UpperCamelCase   :    Dict          =			["""CLIPEncoderLayer"""]

			def __init__(							self			:							List[Any]    ,						snake_case_			:							CLIPConfig   ):
							super().__init__(snake_case_   )

							UpperCamelCase_:				Optional[Any]						=    CLIPVisionModel(config.vision_config   )
							UpperCamelCase_:				List[Any]						=    nn.Linear(config.vision_config.hidden_size    ,						config.projection_dim    ,						bias=snake_case_   )

							UpperCamelCase_:				Tuple						=    nn.Parameter(torch.ones(17    ,						config.projection_dim   )    ,						requires_grad=snake_case_   )
							UpperCamelCase_:				Tuple						=    nn.Parameter(torch.ones(3    ,						config.projection_dim   )    ,						requires_grad=snake_case_   )

							UpperCamelCase_:				Dict						=    nn.Parameter(torch.ones(17   )    ,						requires_grad=snake_case_   )
							UpperCamelCase_:				Union[str, Any]						=    nn.Parameter(torch.ones(3   )    ,						requires_grad=snake_case_   )

			@torch.no_grad()
			def    lowerCAmelCase__						(							self			:							Dict    ,						snake_case_			:							List[str]    ,						snake_case_			:							int   ):
							UpperCamelCase_:				Dict						=    self.vision_model(snake_case_   )[1]  # pooled_output
							UpperCamelCase_:				Dict						=    self.visual_projection(snake_case_   )

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

							UpperCamelCase_:				Dict						=    []
							UpperCamelCase_:				List[Any]						=    image_embeds.shape[0]
							for i in range(snake_case_   ):
											UpperCamelCase_:				Optional[Any]						=    {"""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_:				Union[str, Any]						=    0.0

											for concept_idx in range(len(special_cos_dist[0]   )   ):
															UpperCamelCase_:				List[Any]						=    special_cos_dist[i][concept_idx]
															UpperCamelCase_:				Any						=    self.special_care_embeds_weights[concept_idx].item()
															UpperCamelCase_:				List[str]						=    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_:				Dict						=    0.01

											for concept_idx in range(len(cos_dist[0]   )   ):
															UpperCamelCase_:				Optional[Any]						=    cos_dist[i][concept_idx]
															UpperCamelCase_:				Union[str, Any]						=    self.concept_embeds_weights[concept_idx].item()
															UpperCamelCase_:				int						=    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_   )

							UpperCamelCase_:				Optional[Any]						=    [len(res["""bad_concepts"""]   ) > 0 for res in result]

							return images, has_nsfw_concepts




			@torch.no_grad()
			def    lowerCAmelCase__						(							self			:							Tuple    ,						snake_case_			:							torch.FloatTensor    ,						snake_case_			:							torch.FloatTensor   ):
							UpperCamelCase_:				Optional[int]						=    self.vision_model(snake_case_   )[1]  # pooled_output
							UpperCamelCase_:				List[str]						=    self.visual_projection(snake_case_   )

							UpperCamelCase_:				Tuple						=    cosine_distance(snake_case_    ,						self.special_care_embeds   )
							UpperCamelCase_:				Dict						=    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
							UpperCamelCase_:				Union[str, Any]						=    0.0

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

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

							return images, has_nsfw_concepts








from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


lowerCamelCase_    :			Union[str, Any]    =	{
    """configuration_tapas""": ["""TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP""", """TapasConfig"""],
    """tokenization_tapas""": ["""TapasTokenizer"""],
}

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							lowerCamelCase_    :			List[Any]    =	[
							    """TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST""",
							    """TapasForMaskedLM""",
							    """TapasForQuestionAnswering""",
							    """TapasForSequenceClassification""",
							    """TapasModel""",
							    """TapasPreTrainedModel""",
							    """load_tf_weights_in_tapas""",
							]
try:
							if not is_tf_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							lowerCamelCase_    :			Optional[Any]    =	[
							    """TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST""",
							    """TFTapasForMaskedLM""",
							    """TFTapasForQuestionAnswering""",
							    """TFTapasForSequenceClassification""",
							    """TFTapasModel""",
							    """TFTapasPreTrainedModel""",
							]


if TYPE_CHECKING:
							from .configuration_tapas import TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP, TapasConfig
							from .tokenization_tapas import TapasTokenizer

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_tapas import (
														    TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST,
														    TapasForMaskedLM,
														    TapasForQuestionAnswering,
														    TapasForSequenceClassification,
														    TapasModel,
														    TapasPreTrainedModel,
														    load_tf_weights_in_tapas,
														)

							try:
														if not is_tf_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_tf_tapas import (
														    TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST,
														    TFTapasForMaskedLM,
														    TFTapasForQuestionAnswering,
														    TFTapasForSequenceClassification,
														    TFTapasModel,
														    TFTapasPreTrainedModel,
														)


else:
							import sys

							lowerCamelCase_    :			Any    =	_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)






