





"""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 					__A  (_SCREAMING_SNAKE_CASE	,  _SCREAMING_SNAKE_CASE	,  _SCREAMING_SNAKE_CASE=1e-12     )   ->Tuple:





  """simple docstring"""


  lowerCAmelCase__     :List[Any]											=				jnp.divide(emb_a.T	,  jnp.clip(jnp.linalg.norm(_SCREAMING_SNAKE_CASE	,  axis=1     )	,  a_min=_SCREAMING_SNAKE_CASE     )     ).T
  lowerCAmelCase__     :Any											=				jnp.divide(emb_a.T	,  jnp.clip(jnp.linalg.norm(_SCREAMING_SNAKE_CASE	,  axis=1     )	,  a_min=_SCREAMING_SNAKE_CASE     )     ).T
  return jnp.matmul(_SCREAMING_SNAKE_CASE	,  norm_emb_a.T     )






class    _lowerCAmelCase  (	nn.Module			):
       """simple docstring"""




       __magic_name__	:CLIPConfig
       __magic_name__	:jnp.dtype						    =   jnp.floataa






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Optional[int]											=				FlaxCLIPVisionModule(self.config.vision_config )
         lowerCAmelCase__     :str											=				nn.Dense(self.config.projection_dim       ,					use_bias=__UpperCAmelCase       ,					dtype=self.dtype )

         lowerCAmelCase__     :Tuple											=				self.param('concept_embeds'       ,					jax.nn.initializers.ones       ,					(1_7, 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       ,					(1_7,) )
         lowerCAmelCase__     :str											=				self.param('special_care_embeds_weights'       ,					jax.nn.initializers.ones       ,					(3,) )






       def __call__(       self       ,					__UpperCAmelCase ):



         '''simple docstring'''

         lowerCAmelCase__     :Optional[int]											=				self.vision_model(__UpperCAmelCase )[1]
         lowerCAmelCase__     :int											=				self.visual_projection(__UpperCAmelCase )

         lowerCAmelCase__     :Any											=				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__     :str											=				0.0

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

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

         return has_nsfw_concepts






class    _lowerCAmelCase  (	a			):
       """simple docstring"""




       __magic_name__	:Optional[int]						    =   CLIPConfig
       __magic_name__	:Any						    =   """clip_input"""
       __magic_name__	:Tuple						    =   FlaxStableDiffusionSafetyCheckerModule






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



         '''simple docstring'''

         if input_shape is None:
           lowerCAmelCase__     :Dict											=				(1, 2_2_4, 2_2_4, 3)
         lowerCAmelCase__     :Optional[int]											=				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       ,					__UpperCAmelCase       ,					__UpperCAmelCase       ,					__UpperCAmelCase = None ):



         '''simple docstring'''

         lowerCAmelCase__     :Optional[Any]											=				jax.random.normal(__UpperCAmelCase       ,					__UpperCAmelCase )

         lowerCAmelCase__				,							lowerCAmelCase__     :Optional[Any]											=				jax.random.split(__UpperCAmelCase )
         lowerCAmelCase__     :Union[str, Any]											=				{'params': params_rng, 'dropout': dropout_rng}

         lowerCAmelCase__     :Optional[int]											=				self.module.init(__UpperCAmelCase       ,					__UpperCAmelCase )['params']

         return random_params






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



         '''simple docstring'''

         lowerCAmelCase__     :Optional[int]											=				jnp.transpose(__UpperCAmelCase       ,					(0, 2, 3, 1) )

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







from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


SCREAMING_SNAKE_CASE__							:      Union[str, Any]         =    {
    """configuration_autoformer""": [
        """AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """AutoformerConfig""",
    ],
}

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  SCREAMING_SNAKE_CASE__							:      Tuple         =    [
      """AUTOFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """AutoformerForPrediction""",
      """AutoformerModel""",
      """AutoformerPreTrainedModel""",
  ]


if TYPE_CHECKING:
  from .configuration_autoformer import (
      AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
      AutoformerConfig,
  )

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_autoformer import (
        AUTOFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
        AutoformerForPrediction,
        AutoformerModel,
        AutoformerPreTrainedModel,
    )

else:
  import sys

  SCREAMING_SNAKE_CASE__							:      Any         =    _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)

