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__  (					__A   , __A   , __A=1E-12	):







    '''simple docstring'''
    _lowerCamelCase     :		Tuple 						=						jnp.divide(emb_a.T   , jnp.clip(jnp.linalg.norm(__A   , axis=1	)   , a_min=__A	)	).T
    _lowerCamelCase     :		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 	__snake_case	( nn.Module		):





      '''simple docstring'''






      _snake_case    		=       42
      _snake_case    		=       jnp.floataa



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


          """simple docstring"""


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

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

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




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


          """simple docstring"""


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

          _lowerCamelCase     :		int 						=						jax_cosine_distance(_UpperCamelCase    ,  self.special_care_embeds)
          _lowerCamelCase     :		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
          _lowerCamelCase     :		Optional[Any] 						=						0.0

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

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

          return has_nsfw_concepts





class 	__snake_case	( __lowerCAmelCase		):





      '''simple docstring'''






      _snake_case    		=       CLIPConfig
      _snake_case    		=       'clip_input'
      _snake_case    		=       FlaxStableDiffusionSafetyCheckerModule



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


          """simple docstring"""


          if input_shape is None:
              _lowerCamelCase     :		int 						=						(1, 224, 224, 3)
          _lowerCamelCase     :		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 	_SCREAMING_SNAKE_CASE    (					self     :				str    ,  _UpperCamelCase     :				jax.random.KeyArray    ,  _UpperCamelCase     :				Tuple    ,  _UpperCamelCase     :				FrozenDict = None)				->FrozenDict:


          """simple docstring"""


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

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

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

          return random_params




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


          """simple docstring"""


          _lowerCamelCase     :		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 						A__  (					__A	):







    '''simple docstring'''
    _lowerCamelCase     :		Tuple 						=						0
    for ch in input_str:
        _lowerCamelCase     :		Optional[Any] 						=						ord(__A	)
        _lowerCamelCase     :		List[str] 						=						pow(2   , __A	)

        # If we already turned on bit for current character's unicode
        if bitmap >> ch_unicode & 1 == 1:
            return False
        bitmap |= ch_bit_index_on
    return True


if __name__ == "__main__":
    import doctest

    doctest.testmod()




