






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

from ...utils import logging


lowercase    				=   logging.get_logger(__name__)





def 		__UpperCAmelCase     (	a_				,	a_):
							snake_case_           =				nn.functional.normalize(a_)
							snake_case_           =				nn.functional.normalize(a_)
							return torch.mm(a_				,	normalized_text_embeds.t())




class 					UpperCamelCase_ (     snake_case_							):



	'''simple docstring'''




	lowerCAmelCase						=					CLIPConfig

	lowerCAmelCase						=					['''CLIPEncoderLayer''']




	def __init__(			self			,     a   )			->       List[str]:
								super().__init__(a   )

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

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

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




	@torch.no_grad()
	def 				_UpperCamelCase     (			self			,     a			,     a   )			->       Optional[int]:
								snake_case_           =				self.vision_model(a   )[1]  # pooled_output
								snake_case_           =				self.visual_projection(a   )

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

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

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

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

															result.append(a   )

								snake_case_           =				[len(res['bad_concepts']   ) > 0 for res in result]

								return images, has_nsfw_concepts






	@torch.no_grad()
	def 				_UpperCamelCase     (			self			,     a			,     a   )			->       Dict:
								snake_case_           =				self.vision_model(a   )[1]  # pooled_output
								snake_case_           =				self.visual_projection(a   )

								snake_case_           =				cosine_distance(a			,     self.special_care_embeds   )
								snake_case_           =				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
								snake_case_           =				0.0

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

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

								return images, has_nsfw_concepts













def 		__UpperCAmelCase     (	a_ = 10**9):
							snake_case_           =				1
							snake_case_           =				2

							snake_case_           =				0
							snake_case_           =				0
							snake_case_           =				0
							while perimeter <= max_perimeter:
														perimeters_sum += perimeter

														prev_value += 2 * value
														value += prev_value

														snake_case_           =				2 * value + 2 if i % 2 == 0 else 2 * value - 2
														i += 1

							return perimeters_sum


if __name__ == "__main__":
	print(f'{solution() = }')





