
'''simple docstring'''







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 				SCREAMING_SNAKE_CASE__	(   __A			,				__A			,				__A=1e-12	)       ->	List[Any]:
	_snake_case   						=		jnp.divide(emb_a.T			,				jnp.clip(jnp.linalg.norm(a_			,				axis=1	)			,				a_min=a_	)	).T
	_snake_case   						=		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 						__UpperCAmelCase      (					nn.Module						):
							__lowercase      		=       42
							__lowercase      		=       jnp.floataa


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		FlaxCLIPVisionModule(self.config.vision_config  )
								_snake_case   						=		nn.Dense(self.config.projection_dim    ,   use_bias=SCREAMING_SNAKE_CASE__    ,   dtype=self.dtype  )

								_snake_case   						=		self.param('concept_embeds'    ,   jax.nn.initializers.ones    ,   (17, self.config.projection_dim)  )
								_snake_case   						=		self.param(
								    'special_care_embeds'    ,   jax.nn.initializers.ones    ,   (3, self.config.projection_dim)  )

								_snake_case   						=		self.param('concept_embeds_weights'    ,   jax.nn.initializers.ones    ,   (17,)  )
								_snake_case   						=		self.param('special_care_embeds_weights'    ,   jax.nn.initializers.ones    ,   (3,)  )




							def __call__(       self    ,   lowerCAmelCase_  ):





								"""simple docstring"""






								_snake_case   						=		self.vision_model(SCREAMING_SNAKE_CASE__  )[1]
								_snake_case   						=		self.visual_projection(SCREAMING_SNAKE_CASE__  )

								_snake_case   						=		jax_cosine_distance(SCREAMING_SNAKE_CASE__    ,   self.special_care_embeds  )
								_snake_case   						=		jax_cosine_distance(SCREAMING_SNAKE_CASE__    ,   self.concept_embeds  )

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

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

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

								return has_nsfw_concepts


class 						__UpperCAmelCase      (					_lowerCamelCase						):
							__lowercase      		=       CLIPConfig
							__lowercase      		=       '''clip_input'''
							__lowercase      		=       FlaxStableDiffusionSafetyCheckerModule


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





								"""simple docstring"""






								if input_shape is None:
									_snake_case   						=		(1, 2_24, 2_24, 3)
								_snake_case   						=		self.module_class(config=SCREAMING_SNAKE_CASE__    ,   dtype=SCREAMING_SNAKE_CASE__    ,   **SCREAMING_SNAKE_CASE__  )
								super().__init__(SCREAMING_SNAKE_CASE__    ,   SCREAMING_SNAKE_CASE__    ,   input_shape=SCREAMING_SNAKE_CASE__    ,   seed=SCREAMING_SNAKE_CASE__    ,   dtype=SCREAMING_SNAKE_CASE__    ,   _do_init=_do_init  )


							def  lowerCamelCase				(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_ = None  ):





								"""simple docstring"""






								_snake_case   						=		jax.random.normal(SCREAMING_SNAKE_CASE__    ,   SCREAMING_SNAKE_CASE__  )

								_snake_case   						=		jax.random.split(SCREAMING_SNAKE_CASE__  )
								_snake_case   						=		{'''params''': params_rng, '''dropout''': dropout_rng}

								_snake_case   						=		self.module.init(SCREAMING_SNAKE_CASE__    ,   SCREAMING_SNAKE_CASE__  )['''params''']

								return random_params




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





								"""simple docstring"""






								_snake_case   						=		jnp.transpose(SCREAMING_SNAKE_CASE__    ,   (0, 2, 3, 1)  )

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








"""simple docstring"""
from typing import Any



def      __lowerCamelCase   (							a_     :		list							)			->      list[Any]:
      if not input_list:
            return []
      __SCREAMING_SNAKE_CASE   :int						=  [input_list.count(a_							) for value in input_list]
      __SCREAMING_SNAKE_CASE   :str						=  max(a_							)  # Gets the maximum count in the input list.
      # Gets values of modes
      return sorted({input_list[i] for i, value in enumerate(a_							) if value == y}							)


if __name__ == "__main__":
  import doctest

  doctest.testmod()