



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

from ...utils import logging


UpperCAmelCase__  =			logging.get_logger(__name__)





def 				A      (	_UpperCAmelCase							:  str				,  _UpperCAmelCase							:  Optional[Any]       )							->					str:







			'''simple docstring'''
			_UpperCAmelCase									=  nn.functional.normalize(_UpperCAmelCase       )
			_UpperCAmelCase									=  nn.functional.normalize(_UpperCAmelCase       )
			return torch.mm(_UpperCAmelCase				,  normalized_text_embeds.t()       )







class      __lowerCAmelCase       (     A		):
			UpperCamelCase     =	CLIPConfig

			UpperCamelCase     =	['''CLIPEncoderLayer''']



			def __init__(		self  :    str					,						A  :    CLIPConfig)  ->       Optional[Any]:



						"""simple docstring"""






						super().__init__(A)

						_UpperCAmelCase									=  CLIPVisionModel(config.vision_config)
						_UpperCAmelCase									=  nn.Linear(config.vision_config.hidden_size					,						config.projection_dim					,						bias=A)

						_UpperCAmelCase									=  nn.Parameter(torch.ones(17					,						config.projection_dim)					,						requires_grad=A)
						_UpperCAmelCase									=  nn.Parameter(torch.ones(3					,						config.projection_dim)					,						requires_grad=A)

						_UpperCAmelCase									=  nn.Parameter(torch.ones(17)					,						requires_grad=A)
						_UpperCAmelCase									=  nn.Parameter(torch.ones(3)					,						requires_grad=A)



			@torch.no_grad()
			def        _lowerCamelCase			(		self  :    int					,						A  :    int					,						A  :    Tuple)  ->       Tuple:



						"""simple docstring"""






						_UpperCAmelCase									=  self.vision_model(A)[1]  # pooled_output
						_UpperCAmelCase									=  self.visual_projection(A)

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

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

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

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

									result.append(A)

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

						for idx, has_nsfw_concept in enumerate(A):
									if has_nsfw_concept:
												if torch.is_tensor(A) or torch.is_tensor(images[0]):
															_UpperCAmelCase									=  torch.zeros_like(images[idx])  # black image
												else:
															_UpperCAmelCase									=  np.zeros(images[idx].shape)  # black image

						if any(A):
									logger.warning(
									    'Potential NSFW content was detected in one or more images. A black image will be returned instead.'
									    ' Try again with a different prompt and/or seed.')

						return images, has_nsfw_concepts



			@torch.no_grad()
			def        _lowerCamelCase			(		self  :    List[Any]					,						A  :    torch.FloatTensor					,						A  :    torch.FloatTensor)  ->       Any:



						"""simple docstring"""






						_UpperCAmelCase									=  self.vision_model(A)[1]  # pooled_output
						_UpperCAmelCase									=  self.visual_projection(A)

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

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

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

						_UpperCAmelCase									=  0.0  # black image

						return images, has_nsfw_concepts





import sys

sys.set_int_max_str_digits(0)  # type: ignore





def 				A      (	_UpperCAmelCase							:  int       )							->					bool:







			'''simple docstring'''
			_UpperCAmelCase									=  [0] * 11
			_UpperCAmelCase									=  [0] * 11

			# mark last 9 numbers
			for _ in range(9       ):
						_UpperCAmelCase									=  1
						_UpperCAmelCase									=  number // 10
			# flag
			_UpperCAmelCase									=  True

			# check last 9 numbers for pandigitality

			for x in range(9       ):
						if not check_last[x + 1]:
									_UpperCAmelCase									=  False
			if not f:
						return f

			# mark first 9 numbers
			_UpperCAmelCase									=  int(str(_UpperCAmelCase       )[:9]       )

			for _ in range(9       ):
						_UpperCAmelCase									=  1
						_UpperCAmelCase									=  number // 10

			# check first 9 numbers for pandigitality

			for x in range(9       ):
						if not check_front[x + 1]:
									_UpperCAmelCase									=  False
			return f





def 				A      (	_UpperCAmelCase							:  int       )							->					bool:







			'''simple docstring'''
			_UpperCAmelCase									=  [0] * 11

			# mark last 9 numbers
			for _ in range(9       ):
						_UpperCAmelCase									=  1
						_UpperCAmelCase									=  number // 10
			# flag
			_UpperCAmelCase									=  True

			# check last 9 numbers for pandigitality

			for x in range(9       ):
						if not check_last[x + 1]:
									_UpperCAmelCase									=  False
			return f





def 				A      (	)							->					int:







			'''simple docstring'''
			_UpperCAmelCase									=  1
			_UpperCAmelCase									=  1
			_UpperCAmelCase									=  2
			# temporary Fibonacci numbers

			_UpperCAmelCase									=  1
			_UpperCAmelCase									=  1
			_UpperCAmelCase									=  2
			# temporary Fibonacci numbers mod 1e9

			# mod m=1e9, done for fast optimisation
			_UpperCAmelCase									=  [0] * 1_000_000
			_UpperCAmelCase									=  1_000_000_000

			for x in range(1_000_000       ):
						_UpperCAmelCase									=  (aa + ba) % m
						_UpperCAmelCase									=  ba % m
						_UpperCAmelCase									=  ca % m
						if checka(_UpperCAmelCase       ):
									_UpperCAmelCase									=  1

			for x in range(1_000_000       ):
						_UpperCAmelCase									=  a + b
						_UpperCAmelCase									=  b
						_UpperCAmelCase									=  c
						# perform check only if in tocheck
						if tocheck[x + 3] and check(_UpperCAmelCase       ):
									return x + 3  # first 2 already done
			return -1


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