




from typing import Optional, Tuple

import jax
import jax.numpy as jnp
from flax import linen as nn
from flax.core.frozen_dict import FrozenDict
from transformers import CLIPConfig, FlaxPreTrainedModel
from transformers.models.clip.modeling_flax_clip import FlaxCLIPVisionModule







def      _a				(						SCREAMING_SNAKE_CASE_							:    Optional[int]				,				SCREAMING_SNAKE_CASE_							:    Dict				,				SCREAMING_SNAKE_CASE_							:    List[str]=1E-12					):
					__lowerCAmelCase           =	jnp.divide(emb_a.T				,				jnp.clip(jnp.linalg.norm(lowerCAmelCase__				,				axis=1					)				,				a_min=lowerCAmelCase__					)					).T
					__lowerCAmelCase           =	jnp.divide(emb_a.T				,				jnp.clip(jnp.linalg.norm(lowerCAmelCase__				,				axis=1					)				,				a_min=lowerCAmelCase__					)					).T
					return jnp.matmul(lowerCAmelCase__				,				norm_emb_a.T					)
class       a__   ( nn.Module				):
						_a							:   CLIPConfig
						_a							:   jnp.dtype =   jnp.floataa






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	FlaxCLIPVisionModule(self.config.vision_config      )
											__lowerCAmelCase           =	nn.Dense(self.config.projection_dim		,      use_bias=A__		,      dtype=self.dtype      )

											__lowerCAmelCase           =	self.param("concept_embeds"		,      jax.nn.initializers.ones		,      (1_7, self.config.projection_dim)      )
											__lowerCAmelCase           =	self.param(
											    "special_care_embeds"		,      jax.nn.initializers.ones		,      (3, self.config.projection_dim)      )

											__lowerCAmelCase           =	self.param("concept_embeds_weights"		,      jax.nn.initializers.ones		,      (1_7,)      )
											__lowerCAmelCase           =	self.param("special_care_embeds_weights"		,      jax.nn.initializers.ones		,      (3,)      )






						def __call__(       self		,      _A      ):




											"""simple docstring"""



											__lowerCAmelCase           =	self.vision_model(A__      )[1]
											__lowerCAmelCase           =	self.visual_projection(A__      )

											__lowerCAmelCase           =	jax_cosine_distance(A__		,      self.special_care_embeds      )
											__lowerCAmelCase           =	jax_cosine_distance(A__		,      self.concept_embeds      )

											# increase this value to create a stronger `nfsw` filter
											# at the cost of increasing the possibility of filtering benign image inputs
											__lowerCAmelCase           =	0.0

											__lowerCAmelCase           =	special_cos_dist - self.special_care_embeds_weights[None, :] + adjustment
											__lowerCAmelCase           =	jnp.round(A__		,      3      )
											__lowerCAmelCase           =	jnp.any(special_scores > 0		,      axis=1		,      keepdims=A__      )
											# Use a lower threshold if an image has any special care concept
											__lowerCAmelCase           =	is_special_care * 0.01

											__lowerCAmelCase           =	cos_dist - self.concept_embeds_weights[None, :] + special_adjustment
											__lowerCAmelCase           =	jnp.round(A__		,      3      )
											__lowerCAmelCase           =	jnp.any(concept_scores > 0		,      axis=1      )

											return has_nsfw_concepts






class       a__   ( SCREAMING_SNAKE_CASE__				):
						_a							:   List[Any] =   CLIPConfig
						_a							:   Optional[int] =   '''clip_input'''
						_a							:   str =   FlaxStableDiffusionSafetyCheckerModule






						def __init__(       self		,      _A		,      _A = None		,      _A = 0		,      _A = jnp.floataa		,      _A = True		,      **_A		,      ):




											"""simple docstring"""



											if input_shape is None:
																__lowerCAmelCase           =	(1, 2_2_4, 2_2_4, 3)
											__lowerCAmelCase           =	self.module_class(config=A__		,      dtype=A__		,      **A__      )
											super().__init__(A__		,      A__		,      input_shape=A__		,      seed=A__		,      dtype=A__		,      _do_init=_do_init      )






						def     __SCREAMING_SNAKE_CASE(       self		,      _A		,      _A		,      _A = None      ):




											"""simple docstring"""



											__lowerCAmelCase           =	jax.random.normal(A__		,      A__      )

											__lowerCAmelCase       ,							__lowerCAmelCase           =	jax.random.split(A__      )
											__lowerCAmelCase           =	{"params": params_rng, "dropout": dropout_rng}

											__lowerCAmelCase           =	self.module.init(A__		,      A__      )["params"]

											return random_params






						def __call__(       self		,      _A		,      _A = None		,      ):




											"""simple docstring"""



											__lowerCAmelCase           =	jnp.transpose(A__		,      (0, 2, 3, 1)      )

											return self.module.apply(
											    {"params": params or self.params}		,      jnp.array(A__		,      dtype=jnp.floataa      )		,      rngs={}		,      )












import math







def      _a				(						SCREAMING_SNAKE_CASE_							:    int					):

					if 1 < number < 4:
										# 2 and 3 are primes
										return True
					elif number < 2 or number % 2 == 0 or number % 3 == 0:
										# Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
										return False

					# All primes number are in format of 6k +/- 1
					for i in range(5				,				int(math.sqrt(SCREAMING_SNAKE_CASE_					) + 1					)				,				6					):
										if number % i == 0 or number % (i + 2) == 0:
															return False
					return True







def      _a				(						SCREAMING_SNAKE_CASE_							:    int = 1_00_01					):

					try:
										__lowerCAmelCase           =	int(SCREAMING_SNAKE_CASE_					)
					except (TypeError, ValueError):
										raise TypeError("Parameter nth must be int or castable to int."					) from None
					if nth <= 0:
										raise ValueError("Parameter nth must be greater than or equal to one."					)
					__lowerCAmelCase           =	[]
					__lowerCAmelCase           =	2
					while len(SCREAMING_SNAKE_CASE_					) < nth:
										if is_prime(SCREAMING_SNAKE_CASE_					):
															primes.append(SCREAMING_SNAKE_CASE_					)
															num += 1
										else:
															num += 1
					return primes[len(SCREAMING_SNAKE_CASE_					) - 1]


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




