





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

from ...utils import logging


UpperCamelCase__											= logging.get_logger(__name__)





def     _UpperCamelCase     (a__   :Dict   ,			a__   :Optional[Any]       ):






							"""simple docstring"""





							UpperCamelCase__				     =			nn.functional.normalize(a__       )
							UpperCamelCase__				     =			nn.functional.normalize(a__       )
							return torch.mm(a__   ,			normalized_text_embeds.t()       )

class        __SCREAMING_SNAKE_CASE					(     _a       ):
							snake_case     :      Optional[Any]						     =       CLIPConfig

							snake_case     :      Tuple						     =       ["""CLIPEncoderLayer"""]






							def __init__(						self  , __lowerCAmelCase  ):
														super().__init__(__lowerCAmelCase  )

														UpperCamelCase__				     =			CLIPVisionModel(config.vision_config  )
														UpperCamelCase__				     =			nn.Linear(config.vision_config.hidden_size  , config.projection_dim  , bias=__lowerCAmelCase  )

														UpperCamelCase__				     =			nn.Parameter(torch.ones(17  , config.projection_dim  )  , requires_grad=__lowerCAmelCase  )
														UpperCamelCase__				     =			nn.Parameter(torch.ones(3  , config.projection_dim  )  , requires_grad=__lowerCAmelCase  )

														UpperCamelCase__				     =			nn.Parameter(torch.ones(17  )  , requires_grad=__lowerCAmelCase  )
														UpperCamelCase__				     =			nn.Parameter(torch.ones(3  )  , requires_grad=__lowerCAmelCase  )






							@torch.no_grad()
							def 				_lowerCamelCase		(						self  , __lowerCAmelCase  , __lowerCAmelCase  ):
														UpperCamelCase__				     =			self.vision_model(__lowerCAmelCase  )[1]  # pooled_output
														UpperCamelCase__				     =			self.visual_projection(__lowerCAmelCase  )

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

														UpperCamelCase__				     =			[]
														UpperCamelCase__				     =			image_embeds.shape[0]
														for i in range(__lowerCAmelCase  ):
																					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.01

																					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(__lowerCAmelCase  )

																					result.append(__lowerCAmelCase  )

														UpperCamelCase__				     =			[len(res["""bad_concepts"""]  ) > 0 for res in result]

														return images, has_nsfw_concepts







							@torch.no_grad()
							def 				_lowerCamelCase		(						self  , __lowerCAmelCase  , __lowerCAmelCase  ):
														UpperCamelCase__				     =			self.vision_model(__lowerCAmelCase  )[1]  # pooled_output
														UpperCamelCase__				     =			self.visual_projection(__lowerCAmelCase  )

														UpperCamelCase__				     =			cosine_distance(__lowerCAmelCase  , self.special_care_embeds  )
														UpperCamelCase__				     =			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__				     =			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.01
														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  )

														return images, has_nsfw_concepts











import math





def     _UpperCamelCase     (a__   :int       ):






							"""simple docstring"""





							UpperCamelCase__				     =			[True] * n
							UpperCamelCase__				     =			False
							UpperCamelCase__				     =			False
							UpperCamelCase__				     =			True

							for i in range(3   ,			int(n**0.5 + 1       )   ,			2       ):
														UpperCamelCase__				     =			i * 2
														while index < n:
																					UpperCamelCase__				     =			False
																					UpperCamelCase__				     =			index + i

							UpperCamelCase__				     =			[2]

							for i in range(3   ,			a__   ,			2       ):
														if is_prime[i]:
																					primes.append(a__       )

							return primes





def     _UpperCamelCase     (a__   :int = 9999_6666_3333       ):






							"""simple docstring"""





							UpperCamelCase__				     =			math.floor(math.sqrt(a__       )       ) + 100
							UpperCamelCase__				     =			prime_sieve(a__       )

							UpperCamelCase__				     =			0
							UpperCamelCase__				     =			0
							UpperCamelCase__				     =			primes[prime_index]

							while (last_prime**2) <= limit:
														UpperCamelCase__				     =			primes[prime_index + 1]

														UpperCamelCase__				     =			last_prime**2
														UpperCamelCase__				     =			next_prime**2

														# Get numbers divisible by lps(current)
														UpperCamelCase__				     =			lower_bound + last_prime
														while upper_bound > current <= limit:
																					matches_sum += current
																					current += last_prime

														# Reset the upper_bound
														while (upper_bound - next_prime) > limit:
																					upper_bound -= next_prime

														# Add the numbers divisible by ups(current)
														UpperCamelCase__				     =			upper_bound - next_prime
														while current > lower_bound:
																					matches_sum += current
																					current -= next_prime

														# Remove the numbers divisible by both ups and lps
														UpperCamelCase__				     =			0
														while upper_bound > current <= limit:
																					if current <= lower_bound:
																												# Increment the current number
																												current += last_prime * next_prime
																												continue

																					if current > limit:
																												break

																					# Remove twice since it was added by both ups and lps
																					matches_sum -= current * 2

																					# Increment the current number
																					current += last_prime * next_prime

														# Setup for next pair
														UpperCamelCase__				     =			next_prime
														prime_index += 1

							return matches_sum


if __name__ == "__main__":
					print(solution())




