


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

from ...utils import logging


__lowerCAmelCase	: Dict  				=logging.get_logger(__name__)


def        _UpperCamelCase							(      lowercase__    ,       lowercase__      ):
						__SCREAMING_SNAKE_CASE						:     List[Any]					=		nn.functional.normalize(lowercase__      )
						__SCREAMING_SNAKE_CASE						:     Tuple					=		nn.functional.normalize(lowercase__      )
						return torch.mm(lowercase__    ,       normalized_text_embeds.t()      )
class        _lowercase      (			A__       ):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE__				:			Optional[Any]						=		CLIPConfig

				SCREAMING_SNAKE_CASE__				:			List[str]						=		['''CLIPEncoderLayer''']


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

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

										__SCREAMING_SNAKE_CASE						:     Any					=		nn.Parameter(torch.ones(17			,			config.projection_dim	)			,			requires_grad=lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		nn.Parameter(torch.ones(3			,			config.projection_dim	)			,			requires_grad=lowerCAmelCase__	)

										__SCREAMING_SNAKE_CASE						:     Dict					=		nn.Parameter(torch.ones(17	)			,			requires_grad=lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     str					=		nn.Parameter(torch.ones(3	)			,			requires_grad=lowerCAmelCase__	)


				@torch.no_grad()
				def 		__magic_name__(       self			:Optional[int]			,			lowerCAmelCase__			:int			,			lowerCAmelCase__			:Optional[int]	)		->			Dict:
										__SCREAMING_SNAKE_CASE						:     int					=		self.vision_model(lowerCAmelCase__	)[1]  # pooled_output
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		self.visual_projection(lowerCAmelCase__	)

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

										__SCREAMING_SNAKE_CASE						:     List[Any]					=		[]
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		image_embeds.shape[0]
										for i in range(lowerCAmelCase__	):
																__SCREAMING_SNAKE_CASE						:     Optional[int]					=		{'''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
																__SCREAMING_SNAKE_CASE						:     List[Any]					=		0.0

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

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

																result.append(lowerCAmelCase__	)

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

										return images, has_nsfw_concepts



				@torch.no_grad()
				def 		__magic_name__(       self			:Optional[int]			,			lowerCAmelCase__			:torch.FloatTensor			,			lowerCAmelCase__			:torch.FloatTensor	)		->			Any:
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		self.vision_model(lowerCAmelCase__	)[1]  # pooled_output
										__SCREAMING_SNAKE_CASE						:     List[str]					=		self.visual_projection(lowerCAmelCase__	)

										__SCREAMING_SNAKE_CASE						:     Dict					=		cosine_distance(lowerCAmelCase__			,			self.special_care_embeds	)
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		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
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		0.0

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

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

										return images, has_nsfw_concepts



import unittest

from .lib import (
    Matrix,
    Vector,
    axpy,
    square_zero_matrix,
    unit_basis_vector,
    zero_vector,
)







class   __lowercase      (unittest.TestCase ):
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						List[str]					=					Vector([1, 2, 3]					)
       self.assertEqual(x.component(0					)      ,							1					)
       self.assertEqual(x.component(2					)      ,							3					)
       __lowerCAmelCase					:						Dict					=					Vector()
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Any					=					Vector([0, 0, 0, 0, 0, 1]					)
       self.assertEqual(str(A_					)      ,							'''(0,0,0,0,0,1)'''					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Union[str, Any]					=					Vector([1, 2, 3, 4]					)
       self.assertEqual(len(A_					)      ,							4					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Dict					=					Vector([1, 2]					)
       __lowerCAmelCase					:						Optional[int]					=					Vector([1, 2, 3, 4, 5]					)
       __lowerCAmelCase					:						Optional[Any]					=					Vector([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]					)
       __lowerCAmelCase					:						str					=					Vector([1, -1, 1, -1, 2, -3, 4, -5]					)
       self.assertAlmostEqual(x.euclidean_length()      ,							2.236      ,							3					)
       self.assertAlmostEqual(y.euclidean_length()      ,							7.416      ,							3					)
       self.assertEqual(z.euclidean_length()      ,							0					)
       self.assertAlmostEqual(w.euclidean_length()      ,							7.616      ,							3					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Tuple					=					Vector([1, 2, 3]					)
       __lowerCAmelCase					:						List[str]					=					Vector([1, 1, 1]					)
       self.assertEqual((x + y).component(0					)      ,							2					)
       self.assertEqual((x + y).component(1					)      ,							3					)
       self.assertEqual((x + y).component(2					)      ,							4					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Any					=					Vector([1, 2, 3]					)
       __lowerCAmelCase					:						List[str]					=					Vector([1, 1, 1]					)
       self.assertEqual((x - y).component(0					)      ,							0					)
       self.assertEqual((x - y).component(1					)      ,							1					)
       self.assertEqual((x - y).component(2					)      ,							2					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						str					=					Vector([1, 2, 3]					)
       __lowerCAmelCase					:						List[Any]					=					Vector([2, -1, 4]					)  # for test of dot product
       __lowerCAmelCase					:						Optional[int]					=					Vector([1, -2, -1]					)
       self.assertEqual(str(x * 3.0					)      ,							'''(3.0,6.0,9.0)'''					)
       self.assertEqual((a * b)      ,							0					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       self.assertEqual(str(zero_vector(10					)					).count('''0'''					)      ,							10					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       self.assertEqual(str(unit_basis_vector(3      ,							1					)					)      ,							'''(0,1,0)'''					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						str					=					Vector([1, 2, 3]					)
       __lowerCAmelCase					:						Any					=					Vector([1, 0, 1]					)
       self.assertEqual(str(axpy(2      ,							A_      ,							A_					)					)      ,							'''(3,4,7)'''					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Tuple					=					Vector([1, 0, 0, 0, 0, 0]					)
       __lowerCAmelCase					:						Optional[Any]					=					x.copy()
       self.assertEqual(str(A_					)      ,							str(A_					)					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						List[str]					=					Vector([1, 0, 0]					)
       x.change_component(0      ,							0					)
       x.change_component(1      ,							1					)
       self.assertEqual(str(A_					)      ,							'''(0,1,0)'''					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Union[str, Any]					=					Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]      ,							3      ,							3					)
       self.assertEqual('''|1,2,3|\n|2,4,5|\n|6,7,8|\n'''      ,							str(A_					)					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						List[Any]					=					Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]      ,							3      ,							3					)
       __lowerCAmelCase					:						str					=					[[-3, -14, -10], [-5, -10, -5], [-2, -1, 0]]
       for x in range(a.height()					):
             for y in range(a.width()					):
                   self.assertEqual(minors[x][y]      ,							a.minor(A_      ,							A_					)					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Optional[int]					=					Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]      ,							3      ,							3					)
       __lowerCAmelCase					:						Tuple					=					[[-3, 14, -10], [5, -10, 5], [-2, 1, 0]]
       for x in range(a.height()					):
             for y in range(a.width()					):
                   self.assertEqual(cofactors[x][y]      ,							a.cofactor(A_      ,							A_					)					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Dict					=					Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]      ,							3      ,							3					)
       self.assertEqual(-5      ,							a.determinant()					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Tuple					=					Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]      ,							3      ,							3					)
       __lowerCAmelCase					:						Union[str, Any]					=					Vector([1, 2, 3]					)
       self.assertEqual('''(14,32,50)'''      ,							str(a * x					)					)
       self.assertEqual('''|2,4,6|\n|8,10,12|\n|14,16,18|\n'''      ,							str(a * 2					)					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Any					=					Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]      ,							3      ,							3					)
       a.change_component(0      ,							2      ,							5					)
       self.assertEqual('''|1,2,5|\n|2,4,5|\n|6,7,8|\n'''      ,							str(A_					)					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Tuple					=					Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]      ,							3      ,							3					)
       self.assertEqual(7      ,							a.component(2      ,							1					)      ,							0.01					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Any					=					Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]      ,							3      ,							3					)
       __lowerCAmelCase					:						Dict					=					Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]]      ,							3      ,							3					)
       self.assertEqual('''|2,4,10|\n|4,8,10|\n|12,14,18|\n'''      ,							str(a + b					)					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Dict					=					Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]      ,							3      ,							3					)
       __lowerCAmelCase					:						str					=					Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]]      ,							3      ,							3					)
       self.assertEqual('''|0,0,-4|\n|0,0,0|\n|0,0,-2|\n'''      ,							str(a - b					)					)





 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       self.assertEqual(
           '''|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n'''      ,							str(square_zero_matrix(5					)					)      ,							)


if __name__ == "__main__":
 unittest.main()


