





"""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_  (			snake_case  :   List[Any]		,    snake_case  :   int		,    snake_case  :   Optional[int]=1e-12						)->						List[str]:
       _lowerCamelCase             =   jnp.divide(emb_a.T		,    jnp.clip(jnp.linalg.norm(_A		,    axis=1						)		,    a_min=_A						)						).T
       _lowerCamelCase             =   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       __a  (    nn.Module						):
     SCREAMING_SNAKE_CASE__					: Tuple									=     42
     SCREAMING_SNAKE_CASE__					: Tuple									=     jnp.floataa



     def 		snake_case_     (			self  ):
            _lowerCamelCase             =   FlaxCLIPVisionModule(self.config.vision_config  )
            _lowerCamelCase             =   nn.Dense(self.config.projection_dim   ,	use_bias=__snake_case   ,	dtype=self.dtype  )

            _lowerCamelCase             =   self.param('concept_embeds'   ,	jax.nn.initializers.ones   ,	(17, 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   ,	(17,)  )
            _lowerCamelCase             =   self.param('special_care_embeds_weights'   ,	jax.nn.initializers.ones   ,	(3,)  )



     def __call__(			self   ,	a__  ):
            _lowerCamelCase             =   self.vision_model(__snake_case  )[1]
            _lowerCamelCase             =   self.visual_projection(__snake_case  )

            _lowerCamelCase             =   jax_cosine_distance(__snake_case   ,	self.special_care_embeds  )
            _lowerCamelCase             =   jax_cosine_distance(__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
            _lowerCamelCase             =   0.0

            _lowerCamelCase             =   special_cos_dist - self.special_care_embeds_weights[None, :] + adjustment
            _lowerCamelCase             =   jnp.round(__snake_case   ,	3  )
            _lowerCamelCase             =   jnp.any(special_scores > 0   ,	axis=1   ,	keepdims=__snake_case  )
            # 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(__snake_case   ,	3  )
            _lowerCamelCase             =   jnp.any(concept_scores > 0   ,	axis=1  )

            return has_nsfw_concepts







class       __a  (    lowerCamelCase__						):
     SCREAMING_SNAKE_CASE__					: Dict									=     CLIPConfig
     SCREAMING_SNAKE_CASE__					: str									=     "clip_input"
     SCREAMING_SNAKE_CASE__					: List[str]									=     FlaxStableDiffusionSafetyCheckerModule



     def __init__(			self   ,	a__   ,	a__ = None   ,	a__ = 0   ,	a__ = jnp.floataa   ,	a__ = True   ,	**a__   ,	):
            if input_shape is None:
                   _lowerCamelCase             =   (1, 2_24, 2_24, 3)
            _lowerCamelCase             =   self.module_class(config=__snake_case   ,	dtype=__snake_case   ,	**__snake_case  )
            super().__init__(__snake_case   ,	__snake_case   ,	input_shape=__snake_case   ,	seed=__snake_case   ,	dtype=__snake_case   ,	_do_init=_do_init  )



     def 		snake_case_     (			self   ,	a__   ,	a__   ,	a__ = None  ):
            # init input tensor
            _lowerCamelCase             =   jax.random.normal(__snake_case   ,	__snake_case  )

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

            _lowerCamelCase             =   self.module.init(__snake_case   ,	__snake_case  )['params']

            return random_params



     def __call__(			self   ,	a__   ,	a__ = None   ,	):
            _lowerCamelCase             =   jnp.transpose(__snake_case   ,	(0, 2, 3, 1)  )

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











'''simple docstring'''






import numpy as np
def  lowerCamelCase__			(		_A							):
  return 1 / (1 + np.exp(-vector							))
def  lowerCamelCase__			(		_A							):
  return vector * sigmoid(1.702 * vector							)


if __name__ == "__main__":
     import doctest

     doctest.testmod()