






"""simple docstring"""





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

from ...utils import logging


__lowerCamelCase							=  logging.get_logger(__name__)



def   a    (     __UpperCAmelCase      :    Optional[Any]      ,   __UpperCAmelCase      :    List[Any]  )    ->							str:
						__magic_name__:	str							=       nn.functional.normalize(__UpperCAmelCase  )
						__magic_name__:	Optional[Any]							=       nn.functional.normalize(__UpperCAmelCase  )
						return torch.mm(__UpperCAmelCase      ,   normalized_text_embeds.t()  )



class       __A		(   SCREAMING_SNAKE_CASE_			):
					UpperCAmelCase__								=				CLIPConfig

					UpperCAmelCase__								=				["CLIPEncoderLayer"]

					def __init__(			self  :      str     ,		__snake_case  :      CLIPConfig					)			->							Tuple:
											super().__init__(__snake_case					)

											__magic_name__:	Union[str, Any]							=       CLIPVisionModel(config.vision_config					)
											__magic_name__:	Optional[Any]							=       nn.Linear(config.vision_config.hidden_size     ,		config.projection_dim     ,		bias=__snake_case					)

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

											__magic_name__:	Any							=       nn.Parameter(torch.ones(1_7					)     ,		requires_grad=__snake_case					)
											__magic_name__:	List[Any]							=       nn.Parameter(torch.ones(3					)     ,		requires_grad=__snake_case					)

					@torch.no_grad()
					def   lowerCamelCase__   (			self  :      Tuple     ,		__snake_case  :      int     ,		__snake_case  :      str					)			->							Optional[int]:
											__magic_name__:	Optional[int]							=       self.vision_model(__snake_case					)[1]  # pooled_output
											__magic_name__:	Tuple							=       self.visual_projection(__snake_case					)

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

											__magic_name__:	Optional[int]							=       []
											__magic_name__:	Tuple							=       image_embeds.shape[0]
											for i in range(__snake_case					):
																	__magic_name__:	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
																	__magic_name__:	List[str]							=       0.0

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

																	for concept_idx in range(len(cos_dist[0]					)					):
																							__magic_name__:	List[str]							=       cos_dist[i][concept_idx]
																							__magic_name__:	List[str]							=       self.concept_embeds_weights[concept_idx].item()
																							__magic_name__:	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					)

											__magic_name__:	int							=       [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					)			->							Optional[Any]:
											__magic_name__:	Tuple							=       self.vision_model(__snake_case					)[1]  # pooled_output
											__magic_name__:	List[Any]							=       self.visual_projection(__snake_case					)

											__magic_name__:	Dict							=       cosine_distance(__snake_case     ,		self.special_care_embeds					)
											__magic_name__:	Optional[int]							=       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
											__magic_name__:	int							=       0.0

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

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

											return images, has_nsfw_concepts











"""simple docstring"""





from __future__ import annotations



def   a    (     __UpperCAmelCase      :    list  )    ->							list:
						if len(__UpperCAmelCase  ) == 0:
												return []
						__magic_name__,			__magic_name__:	List[str]							=       min(__UpperCAmelCase  ), max(__UpperCAmelCase  )
						__magic_name__:	Optional[int]							=       int(max_value - min_value  ) + 1
						__magic_name__:	list[list]							=       [[] for _ in range(__UpperCAmelCase  )]

						for i in my_list:
												buckets[int(i - min_value  )].append(__UpperCAmelCase  )

						return [v for bucket in buckets for v in sorted(__UpperCAmelCase  )]


if __name__ == "__main__":
							from doctest import testmod

							testmod()
							assert bucket_sort([4, 5, 3, 2, 1]) == [1, 2, 3, 4, 5]
							assert bucket_sort([0, 1, -10, 15, 2, -2]) == [-10, -2, 0, 1, 2, 15]



