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

from ...utils import logging


__SCREAMING_SNAKE_CASE			:   int         =     logging.get_logger(__name__)
def     UpperCAmelCase__							(       __magic_name__						:     int							,  __magic_name__						:     Any  ):


					'''simple docstring'''

					lowerCAmelCase		:					Optional[int]  	=     nn.functional.normalize(__magic_name__  )
					lowerCAmelCase		:					List[str]  	=     nn.functional.normalize(__magic_name__  )
					return torch.mm(__magic_name__							,  normalized_text_embeds.t()  )


class 		__magic_name__   (						snake_case    ):
		_lowerCAmelCase		=	CLIPConfig

		_lowerCAmelCase		=	["CLIPEncoderLayer"]





		def __init__(							self    :	Optional[int]						,  lowerCamelCase__    :	CLIPConfig				):
							super().__init__(lowerCamelCase__				)

							lowerCAmelCase		:					Union[str, Any]  	=     CLIPVisionModel(config.vision_config				)
							lowerCAmelCase		:					int  	=     nn.Linear(config.vision_config.hidden_size						,  config.projection_dim						,  bias=lowerCamelCase__				)

							lowerCAmelCase		:					Any  	=     nn.Parameter(torch.ones(1_7						,  config.projection_dim				)						,  requires_grad=lowerCamelCase__				)
							lowerCAmelCase		:					List[str]  	=     nn.Parameter(torch.ones(3						,  config.projection_dim				)						,  requires_grad=lowerCamelCase__				)

							lowerCAmelCase		:					List[Any]  	=     nn.Parameter(torch.ones(1_7				)						,  requires_grad=lowerCamelCase__				)
							lowerCAmelCase		:					Tuple  	=     nn.Parameter(torch.ones(3				)						,  requires_grad=lowerCamelCase__				)





		@torch.no_grad()
		def       _A							(							self    :	int						,  lowerCamelCase__    :	str						,  lowerCamelCase__    :	Tuple				):
							lowerCAmelCase		:					Optional[int]  	=     self.vision_model(lowerCamelCase__				)[1]  # pooled_output
							lowerCAmelCase		:					Optional[int]  	=     self.visual_projection(lowerCamelCase__				)

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

							lowerCAmelCase		:					Optional[Any]  	=     []
							lowerCAmelCase		:					int  	=     image_embeds.shape[0]
							for i in range(lowerCamelCase__				):
												lowerCAmelCase		:					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
												lowerCAmelCase		:					int  	=     0.0

												for concept_idx in range(len(special_cos_dist[0]				)				):
																	lowerCAmelCase		:					List[str]  	=     special_cos_dist[i][concept_idx]
																	lowerCAmelCase		:					int  	=     self.special_care_embeds_weights[concept_idx].item()
																	lowerCAmelCase		:					Dict  	=     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]}				)
																						lowerCAmelCase		:					Any  	=     0.0_1

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

												result.append(lowerCamelCase__				)

							lowerCAmelCase		:					str  	=     [len(res['''bad_concepts''']				) > 0 for res in result]

							for idx, has_nsfw_concept in enumerate(lowerCamelCase__				):
												if has_nsfw_concept:
																	if torch.is_tensor(lowerCamelCase__				) or torch.is_tensor(images[0]				):
																						lowerCAmelCase		:					str  	=     torch.zeros_like(images[idx]				)  # black image
																	else:
																						lowerCAmelCase		:					List[Any]  	=     np.zeros(images[idx].shape				)  # black image

							if any(lowerCamelCase__				):
												logger.warning(
												    '''Potential NSFW content was detected in one or more images. A black image will be returned instead.'''
												    ''' Try again with a different prompt and/or seed.'''				)

							return images, has_nsfw_concepts





		@torch.no_grad()
		def       _A							(							self    :	Optional[Any]						,  lowerCamelCase__    :	torch.FloatTensor						,  lowerCamelCase__    :	torch.FloatTensor				):
							lowerCAmelCase		:					int  	=     self.vision_model(lowerCamelCase__				)[1]  # pooled_output
							lowerCAmelCase		:					Any  	=     self.visual_projection(lowerCamelCase__				)

							lowerCAmelCase		:					Union[str, Any]  	=     cosine_distance(lowerCamelCase__						,  self.special_care_embeds				)
							lowerCAmelCase		:					Union[str, Any]  	=     cosine_distance(lowerCamelCase__						,  self.concept_embeds				)

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

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

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

							lowerCAmelCase		:					List[Any]  	=     0.0  # black image

							return images, has_nsfw_concepts


def     UpperCAmelCase__							(       __magic_name__						:     Tuple							,  __magic_name__						:     int							,  __magic_name__						:     Dict							,  __magic_name__						:     str  ):


					'''simple docstring'''

					if start == end:
										return start if lst[start] > item else start + 1
					if start > end:
										return start

					lowerCAmelCase		:					Optional[Any]  	=     (start + end) // 2
					if lst[mid] < item:
										return binary_search(__magic_name__							,  __magic_name__							,  mid + 1							,  __magic_name__  )
					elif lst[mid] > item:
										return binary_search(__magic_name__							,  __magic_name__							,  __magic_name__							,  mid - 1  )
					else:
										return mid
def     UpperCAmelCase__							(       __magic_name__						:     List[Any]  ):


					'''simple docstring'''

					lowerCAmelCase		:					Union[str, Any]  	=     len(__magic_name__  )

					for index in range(1							,  __magic_name__  ):
										lowerCAmelCase		:					Optional[int]  	=     lst[index]
										lowerCAmelCase		:					Dict  	=     binary_search(__magic_name__							,  __magic_name__							,  0							,  index - 1  )
										lowerCAmelCase		:					Union[str, Any]  	=     lst[:pos] + [value] + lst[pos:index] + lst[index + 1 :]

					return lst
def     UpperCAmelCase__							(       __magic_name__						:     Optional[int]							,  __magic_name__						:     List[str]  ):


					'''simple docstring'''

					if not left:
										return right

					if not right:
										return left

					if left[0] < right[0]:
										return [left[0], *merge(left[1:]							,  __magic_name__  )]

					return [right[0], *merge(__magic_name__							,  right[1:]  )]
def     UpperCAmelCase__							(       __magic_name__						:     str  ):


					'''simple docstring'''

					lowerCAmelCase		:					str  	=     len(__magic_name__  )
					lowerCAmelCase	,							lowerCAmelCase		:					Union[str, Any]  	=     [], []
					lowerCAmelCase		:					Union[str, Any]  	=     [lst[0]]
					lowerCAmelCase		:					Optional[Any]  	=     []
					lowerCAmelCase		:					List[Any]  	=     1
					while i < length:
										if lst[i] < lst[i - 1]:
															runs.append(__magic_name__  )
															lowerCAmelCase		:					Any  	=     [lst[i]]
										else:
															new_run.append(lst[i]  )
										i += 1
					runs.append(__magic_name__  )

					for run in runs:
										sorted_runs.append(insertion_sort(__magic_name__  )  )
					for run in sorted_runs:
										lowerCAmelCase		:					int  	=     merge(__magic_name__							,  __magic_name__  )

					return sorted_array
def     UpperCAmelCase__							(       ):


					'''simple docstring'''

					lowerCAmelCase		:					Tuple  	=     [5, 9, 10, 3, -4, 5, 1_78, 92, 46, -18, 0, 7]
					lowerCAmelCase		:					Optional[Any]  	=     tim_sort(__magic_name__  )
					print(__magic_name__  )


if __name__ == "__main__":
		main()
