


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 				lowerCamelCase__	(      a	:   str	,  a	:   str	,  a	:   Optional[int]=1e-12				)    ->	Dict:





			"""simple docstring"""
			a__ :Tuple						  =    jnp.divide(emb_a.T	,  jnp.clip(jnp.linalg.norm(__A	,  axis=1				)	,  a_min=__A				)				).T
			a__ :Any						  =    jnp.divide(emb_a.T	,  jnp.clip(jnp.linalg.norm(__A	,  axis=1				)	,  a_min=__A				)				).T
			return jnp.matmul(__A	,  norm_emb_a.T				)


class 					lowerCAmelCase_ (       nn.Module):
						lowerCamelCase_         =     42
						lowerCamelCase_         =     jnp.floataa




						def  _snake_case					(	self						:   List[Any]						)      ->List[Any]:






									"""simple docstring"""






									a__ :List[str]						  =    FlaxCLIPVisionModule(self.config.vision_config						)
									a__ :Dict						  =    nn.Dense(self.config.projection_dim   ,     use_bias=_UpperCamelCase   ,     dtype=self.dtype						)

									a__ :str						  =    self.param("concept_embeds"   ,     jax.nn.initializers.ones   ,     (17, self.config.projection_dim)						)
									a__ :int						  =    self.param(
									    "special_care_embeds"   ,     jax.nn.initializers.ones   ,     (3, self.config.projection_dim)						)

									a__ :Any						  =    self.param("concept_embeds_weights"   ,     jax.nn.initializers.ones   ,     (17,)						)
									a__ :Dict						  =    self.param("special_care_embeds_weights"   ,     jax.nn.initializers.ones   ,     (3,)						)






						def __call__(	self						:   Any   ,     __A						:   Tuple						)      ->Any:






									"""simple docstring"""






									a__ :Optional[Any]						  =    self.vision_model(_UpperCamelCase						)[1]
									a__ :int						  =    self.visual_projection(_UpperCamelCase						)

									a__ :int						  =    jax_cosine_distance(_UpperCamelCase   ,     self.special_care_embeds						)
									a__ :Dict						  =    jax_cosine_distance(_UpperCamelCase   ,     self.concept_embeds						)

									# increase this value to create a stronger `nfsw` filter
									# at the cost of increasing the possibility of filtering benign image inputs
									a__ :Optional[Any]						  =    0.0

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

									a__ :str						  =    cos_dist - self.concept_embeds_weights[None, :] + special_adjustment
									a__ :Optional[Any]						  =    jnp.round(_UpperCamelCase   ,     3						)
									a__ :Union[str, Any]						  =    jnp.any(concept_scores > 0   ,     axis=1						)

									return has_nsfw_concepts






class 					lowerCAmelCase_ (       __lowerCAmelCase):
						lowerCamelCase_         =     CLIPConfig
						lowerCamelCase_         =     'clip_input'
						lowerCamelCase_         =     FlaxStableDiffusionSafetyCheckerModule




						def __init__(	self						:   int   ,     __A						:   CLIPConfig   ,     __A						:   Optional[Tuple] = None   ,     __A						:   int = 0   ,     __A						:   jnp.dtype = jnp.floataa   ,     __A						:   bool = True   ,     **__A						:   List[Any]   ,     )      ->Dict:






									"""simple docstring"""






									if input_shape is None:
												a__ :int						  =    (1, 224, 224, 3)
									a__ :Union[str, Any]						  =    self.module_class(config=_UpperCamelCase   ,     dtype=_UpperCamelCase   ,     **_UpperCamelCase						)
									super().__init__(_UpperCamelCase   ,     _UpperCamelCase   ,     input_shape=_UpperCamelCase   ,     seed=_UpperCamelCase   ,     dtype=_UpperCamelCase   ,     _do_init=_do_init						)




						def  _snake_case					(	self						:   str   ,     __A						:   jax.random.KeyArray   ,     __A						:   Tuple   ,     __A						:   FrozenDict = None						)      ->FrozenDict:






									"""simple docstring"""






									a__ :List[Any]						  =    jax.random.normal(_UpperCamelCase   ,     _UpperCamelCase						)

									a__ :Optional[Any]						  =    jax.random.split(_UpperCamelCase						)
									a__ :Tuple						  =    {"""params""": params_rng, """dropout""": dropout_rng}

									a__ :Tuple						  =    self.module.init(_UpperCamelCase   ,     _UpperCamelCase						)["""params"""]

									return random_params






						def __call__(	self						:   Union[str, Any]   ,     __A						:   Tuple   ,     __A						:   dict = None   ,     )      ->Union[str, Any]:






									"""simple docstring"""






									a__ :List[Any]						  =    jnp.transpose(_UpperCamelCase   ,     (0, 2, 3, 1)						)

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






def 				lowerCamelCase__	(      a	:   int = 1_000_000				)    ->	int:





			"""simple docstring"""
			a__ :int						  =    [i - 1 for i in range(limit + 1				)]

			for i in range(2	,  limit + 1				):
						if phi[i] == i - 1:
									for j in range(2 * i	,  limit + 1	,  a				):
												phi[j] -= phi[j] // i

			return sum(phi[2 : limit + 1]				)


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


