


"""simple docstring"""






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

from ...utils import logging


UpperCAmelCase    :     Dict   						=		logging.get_logger(__name__)
def      lowerCamelCase       (     _UpperCamelCase		:      int       ,			_UpperCamelCase		:      Dict							)		->       Optional[int]:

							'''simple docstring'''



							__UpperCAmelCase   :						List[str]      							=					nn.functional.normalize(_UpperCamelCase							)
							__UpperCAmelCase   :						int      							=					nn.functional.normalize(_UpperCamelCase							)
							return torch.mm(_UpperCamelCase       ,			normalized_text_embeds.t()							)





class      lowerCamelCase__   (     A				):







					"""simple docstring"""


					__a	       =							CLIPConfig

					__a	       =							["""CLIPEncoderLayer"""]






					def __init__(       self    : Union[str, Any] ,       UpperCamelCase    : CLIPConfig  ):
												'''simple docstring'''






												super().__init__(UpperCamelCase  )

												__UpperCAmelCase   :						List[str]      							=					CLIPVisionModel(config.vision_config  )
												__UpperCAmelCase   :						Optional[int]      							=					nn.Linear(config.vision_config.hidden_size ,       config.projection_dim ,       bias=UpperCamelCase  )

												__UpperCAmelCase   :						Tuple      							=					nn.Parameter(torch.ones(17 ,       config.projection_dim  ) ,       requires_grad=UpperCamelCase  )
												__UpperCAmelCase   :						Tuple      							=					nn.Parameter(torch.ones(3 ,       config.projection_dim  ) ,       requires_grad=UpperCamelCase  )

												__UpperCAmelCase   :						Dict      							=					nn.Parameter(torch.ones(17  ) ,       requires_grad=UpperCamelCase  )
												__UpperCAmelCase   :						Dict      							=					nn.Parameter(torch.ones(3  ) ,       requires_grad=UpperCamelCase  )






					@torch.no_grad()
					def lowerCamelCase__							(       self    : Union[str, Any] ,       UpperCamelCase    : str ,       UpperCamelCase    : str  ):
												'''simple docstring'''






												__UpperCAmelCase   :						Any      							=					self.vision_model(UpperCamelCase  )[1]  # pooled_output
												__UpperCAmelCase   :						str      							=					self.visual_projection(UpperCamelCase  )

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

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

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

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

																			result.append(UpperCamelCase  )

												__UpperCAmelCase   :						Optional[Any]      							=					[len(res["""bad_concepts"""]  ) > 0 for res in result]

												return images, has_nsfw_concepts






					@torch.no_grad()
					def lowerCamelCase__							(       self    : int ,       UpperCamelCase    : torch.FloatTensor ,       UpperCamelCase    : torch.FloatTensor  ):
												'''simple docstring'''






												__UpperCAmelCase   :						Optional[Any]      							=					self.vision_model(UpperCamelCase  )[1]  # pooled_output
												__UpperCAmelCase   :						Tuple      							=					self.visual_projection(UpperCamelCase  )

												__UpperCAmelCase   :						Tuple      							=					cosine_distance(UpperCamelCase ,       self.special_care_embeds  )
												__UpperCAmelCase   :						Optional[Any]      							=					cosine_distance(UpperCamelCase ,       self.concept_embeds  )

												# increase this value to create a stronger `nsfw` filter
												# at the cost of increasing the possibility of filtering benign images
												__UpperCAmelCase   :						Optional[int]      							=					0.0

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

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

												return images, has_nsfw_concepts




"""simple docstring"""






import math
def      lowerCamelCase       (     _UpperCamelCase		:      int							)		->       list[int]:

							'''simple docstring'''



							__UpperCAmelCase   :						List[Any]      							=					[]
							__UpperCAmelCase   :						Dict      							=					2
							__UpperCAmelCase   :						Union[str, Any]      							=					int(math.sqrt(_UpperCamelCase							)							)  # Size of every segment
							__UpperCAmelCase   :						Tuple      							=					[True] * (end + 1)
							__UpperCAmelCase   :						int      							=					[]

							while start <= end:
														if temp[start] is True:
																					in_prime.append(_UpperCamelCase							)
																					for i in range(start * start       ,			end + 1       ,			_UpperCamelCase							):
																												__UpperCAmelCase   :						Dict      							=					False
														start += 1
							prime += in_prime

							__UpperCAmelCase   :						Optional[int]      							=					end + 1
							__UpperCAmelCase   :						Dict      							=					min(2 * end       ,			_UpperCamelCase							)

							while low <= n:
														__UpperCAmelCase   :						Union[str, Any]      							=					[True] * (high - low + 1)
														for each in in_prime:
																					__UpperCAmelCase   :						Dict      							=					math.floor(low / each							) * each
																					if t < low:
																												t += each

																					for j in range(_UpperCamelCase       ,			high + 1       ,			_UpperCamelCase							):
																												__UpperCAmelCase   :						Tuple      							=					False

														for j in range(len(_UpperCamelCase							)							):
																					if temp[j] is True:
																												prime.append(j + low							)

														__UpperCAmelCase   :						Tuple      							=					high + 1
														__UpperCAmelCase   :						Optional[int]      							=					min(high + end       ,			_UpperCamelCase							)

							return prime
print(sieve(10**6))
