






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

from ...utils import logging


UpperCAmelCase_ :						str      =      logging.get_logger(__name__)

def    SCREAMING_SNAKE_CASE_							(							__magic_name__					:  Dict	,			__magic_name__					:  Any					)		->			List[str]:






	"""simple docstring"""



	UpperCamelCase     :Any          =	nn.functional.normalize(__magic_name__					)
	UpperCamelCase     :Union[str, Any]          =	nn.functional.normalize(__magic_name__					)
	return torch.mm(__magic_name__	,			normalized_text_embeds.t()					)

class     _SCREAMING_SNAKE_CASE       (			_a      ):
	snake_case__							:     Dict     	=  CLIPConfig

	snake_case__							:     Union[str, Any]     	=  ["""CLIPEncoderLayer"""]





	def __init__(		self				:					Optional[Any] ,  __lowerCamelCase				:					CLIPConfig	):
		super().__init__(__lowerCamelCase	)

		UpperCamelCase     :Optional[Any]          =	CLIPVisionModel(config.vision_config	)
		UpperCamelCase     :Optional[Any]          =	nn.Linear(config.vision_config.hidden_size ,  config.projection_dim ,  bias=__lowerCamelCase	)

		UpperCamelCase     :Tuple          =	nn.Parameter(torch.ones(17 ,  config.projection_dim	) ,  requires_grad=__lowerCamelCase	)
		UpperCamelCase     :Union[str, Any]          =	nn.Parameter(torch.ones(3 ,  config.projection_dim	) ,  requires_grad=__lowerCamelCase	)

		UpperCamelCase     :List[str]          =	nn.Parameter(torch.ones(17	) ,  requires_grad=__lowerCamelCase	)
		UpperCamelCase     :Any          =	nn.Parameter(torch.ones(3	) ,  requires_grad=__lowerCamelCase	)





	@torch.no_grad()
	def 						_A (		self				:					Any ,  __lowerCamelCase				:					Optional[Any] ,  __lowerCamelCase				:					List[Any]	):
		UpperCamelCase     :Tuple          =	self.vision_model(__lowerCamelCase	)[1]  # pooled_output
		UpperCamelCase     :Optional[Any]          =	self.visual_projection(__lowerCamelCase	)

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

		UpperCamelCase     :Any          =	[]
		UpperCamelCase     :Any          =	image_embeds.shape[0]
		for i in range(__lowerCamelCase	):
			UpperCamelCase     :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     :Dict          =	0.0

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

		UpperCamelCase     :Dict          =	[len(res["""bad_concepts"""]	) > 0 for res in result]

		return images, has_nsfw_concepts





	@torch.no_grad()
	def 						_A (		self				:					List[str] ,  __lowerCamelCase				:					torch.FloatTensor ,  __lowerCamelCase				:					torch.FloatTensor	):
		UpperCamelCase     :int          =	self.vision_model(__lowerCamelCase	)[1]  # pooled_output
		UpperCamelCase     :Dict          =	self.visual_projection(__lowerCamelCase	)

		UpperCamelCase     :str          =	cosine_distance(__lowerCamelCase ,  self.special_care_embeds	)
		UpperCamelCase     :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
		UpperCamelCase     :List[str]          =	0.0

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

		UpperCamelCase     :List[str]          =	(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








import os
import unittest

from transformers.models.transfo_xl.tokenization_transfo_xl import VOCAB_FILES_NAMES, TransfoXLTokenizer

from ...test_tokenization_common import TokenizerTesterMixin

class     _SCREAMING_SNAKE_CASE       (			_a		,   unittest.TestCase      ):
	snake_case__							:     Optional[Any]     	=  TransfoXLTokenizer
	snake_case__							:     List[Any]     	=  False
	snake_case__							:     Tuple     	=  False





	def 						_A (		self				:					str	):
		super().setUp()

		UpperCamelCase     :Dict          =	[
		    """<unk>""",
		    """[CLS]""",
		    """[SEP]""",
		    """want""",
		    """unwanted""",
		    """wa""",
		    """un""",
		    """running""",
		    """,""",
		    """low""",
		    """l""",
		]
		UpperCamelCase     :str          =	os.path.join(self.tmpdirname ,  VOCAB_FILES_NAMES["""vocab_file"""]	)
		with open(self.vocab_file ,  """w""" ,  encoding="""utf-8"""	) as vocab_writer:
			vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]	)	)





	def 						_A (		self				:					List[str] ,  **__lowerCamelCase				:					Any	):
		UpperCamelCase     :Any          =	True
		return TransfoXLTokenizer.from_pretrained(self.tmpdirname ,  **__lowerCamelCase	)





	def 						_A (		self				:					Any ,  __lowerCamelCase				:					int	):
		UpperCamelCase     :List[Any]          =	"""<unk> UNwanted , running"""
		UpperCamelCase     :int          =	"""<unk> unwanted, running"""
		return input_text, output_text





	def 						_A (		self				:					Tuple	):
		UpperCamelCase     :List[str]          =	TransfoXLTokenizer(vocab_file=self.vocab_file ,  lower_case=__lowerCamelCase	)

		UpperCamelCase     :Any          =	tokenizer.tokenize("""<unk> UNwanted , running"""	)
		self.assertListEqual(__lowerCamelCase ,  ["""<unk>""", """unwanted""", """,""", """running"""]	)

		self.assertListEqual(tokenizer.convert_tokens_to_ids(__lowerCamelCase	) ,  [0, 4, 8, 7]	)





	def 						_A (		self				:					Optional[Any]	):
		UpperCamelCase     :List[Any]          =	TransfoXLTokenizer(lower_case=__lowerCamelCase	)

		self.assertListEqual(
		    tokenizer.tokenize(""" \tHeLLo ! how  \n Are yoU ?  """	) ,  ["""hello""", """!""", """how""", """are""", """you""", """?"""]	)





	def 						_A (		self				:					Union[str, Any]	):
		UpperCamelCase     :int          =	TransfoXLTokenizer(lower_case=__lowerCamelCase	)

		self.assertListEqual(
		    tokenizer.tokenize(""" \tHeLLo ! how  \n Are yoU ?  """	) ,  ["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?"""]	)





	def 						_A (		self				:					Tuple	):
		UpperCamelCase     :Any          =	TransfoXLTokenizer(lower_case=__lowerCamelCase	)
		UpperCamelCase     :Optional[int]          =	"""Hello (bracket) and side-scrolled [and] Henry's $5,000 with 3.34 m. What's up!?"""
		UpperCamelCase     :Optional[int]          =	[
		    """Hello""",
		    """(""",
		    """bracket""",
		    """)""",
		    """and""",
		    """side""",
		    """@-@""",
		    """scrolled""",
		    """[""",
		    """and""",
		    """]""",
		    """Henry""",
		    """'s""",
		    """$""",
		    """5""",
		    """@,@""",
		    """000""",
		    """with""",
		    """3""",
		    """@.@""",
		    """34""",
		    """m""",
		    """.""",
		    """What""",
		    """'s""",
		    """up""",
		    """!""",
		    """?""",
		]

		self.assertListEqual(tokenizer.tokenize(__lowerCamelCase	) ,  __lowerCamelCase	)

		self.assertEqual(tokenizer.convert_tokens_to_string(__lowerCamelCase	) ,  __lowerCamelCase	)





	def 						_A (		self				:					List[Any]	):
		UpperCamelCase     :Any          =	self.get_tokenizer()
		UpperCamelCase     :List[str]          =	len(__lowerCamelCase	)

		tokenizer.add_tokens(["""new1""", """new2"""]	)
		tokenizer.move_added_token("""new1""" ,  1	)

		# Check that moved token is not copied (duplicate)
		self.assertEqual(len(__lowerCamelCase	) ,  original_len + 2	)
		# Check that token is moved to specified id
		self.assertEqual(tokenizer.encode("""new1"""	) ,  [1]	)
		self.assertEqual(tokenizer.decode([1]	) ,  """new1"""	)
