






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




'''simple docstring'''


def        __lowerCAmelCase       (__lowerCAmelCase = 50      ):
		_UpperCAmelCase						:				Tuple	    =					[[0] * 3 for _ in range(length + 1      )]

		for row_length in range(length + 1      ):
				for tile_length in range(2					,       5      ):
						for tile_start in range(row_length - tile_length + 1      ):
								different_colour_ways_number[row_length][tile_length - 2] += (
								    different_colour_ways_number[row_length - tile_start - tile_length][
								        tile_length - 2
								    ]
								    + 1
								)

		return sum(different_colour_ways_number[length]      )


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



