


"""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    _snake_case  (  _snake_case :	Any       ,      _snake_case :	List[Any]       ,      _snake_case :	List[Any]=1E-12	):
      lowerCAmelCase    :		List[str]				=      jnp.divide(emb_a.T       ,      jnp.clip(jnp.linalg.norm(__snake_case       ,      axis=1	)       ,      a_min=__snake_case	)	).T
      lowerCAmelCase    :		List[str]				=      jnp.divide(emb_a.T       ,      jnp.clip(jnp.linalg.norm(__snake_case       ,      axis=1	)       ,      a_min=__snake_case	)	).T
      return jnp.matmul(__snake_case       ,      norm_emb_a.T	)



class 						snake_case_(				nn.Module	):
     __UpperCamelCase     =    42
     __UpperCamelCase     =    jnp.floataa






     def    lowerCamelCase__   (		self     :    List[Any] ):
           lowerCAmelCase    :		Tuple				=      FlaxCLIPVisionModule(self.config.vision_config )
           lowerCAmelCase    :		int				=      nn.Dense(self.config.projection_dim    , use_bias=_UpperCAmelCase    , dtype=self.dtype )

           lowerCAmelCase    :		str				=      self.param('''concept_embeds'''    , jax.nn.initializers.ones    , (1_7, self.config.projection_dim) )
           lowerCAmelCase    :		Tuple				=      self.param(
               '''special_care_embeds'''    , jax.nn.initializers.ones    , (3, self.config.projection_dim) )

           lowerCAmelCase    :		Tuple				=      self.param('''concept_embeds_weights'''    , jax.nn.initializers.ones    , (1_7,) )
           lowerCAmelCase    :		List[Any]				=      self.param('''special_care_embeds_weights'''    , jax.nn.initializers.ones    , (3,) )






     def __call__(		self     :    Dict    , UpperCamelCase_     :    Optional[Any] ):
           lowerCAmelCase    :		Union[str, Any]				=      self.vision_model(_UpperCAmelCase )[1]
           lowerCAmelCase    :		str				=      self.visual_projection(_UpperCAmelCase )

           lowerCAmelCase    :		List[str]				=      jax_cosine_distance(_UpperCAmelCase    , self.special_care_embeds )
           lowerCAmelCase    :		Optional[Any]				=      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    :		Union[str, Any]				=      0.0

           lowerCAmelCase    :		int				=      special_cos_dist - self.special_care_embeds_weights[None, :] + adjustment
           lowerCAmelCase    :		Optional[Any]				=      jnp.round(_UpperCAmelCase    , 3 )
           lowerCAmelCase    :		Optional[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.01

           lowerCAmelCase    :		Any				=      cos_dist - self.concept_embeds_weights[None, :] + special_adjustment
           lowerCAmelCase    :		List[Any]				=      jnp.round(_UpperCAmelCase    , 3 )
           lowerCAmelCase    :		int				=      jnp.any(concept_scores > 0    , axis=1 )

           return has_nsfw_concepts



class 						snake_case_(				a__	):
     __UpperCamelCase     =    CLIPConfig
     __UpperCamelCase     =    '''clip_input'''
     __UpperCamelCase     =    FlaxStableDiffusionSafetyCheckerModule






     def __init__(		self     :    List[str]    , UpperCamelCase_     :    Dict    , UpperCamelCase_     :    List[Any] = None    , UpperCamelCase_     :    Dict = 0    , UpperCamelCase_     :    List[str] = jnp.floataa    , UpperCamelCase_     :    Optional[int] = True    , **UpperCamelCase_     :    str    , ):






           if input_shape is None:
                 lowerCAmelCase    :		str				=      (1, 2_2_4, 2_2_4, 3)
           lowerCAmelCase    :		str				=      self.module_class(config=_UpperCAmelCase    , dtype=_UpperCAmelCase    , **_UpperCAmelCase )
           super().__init__(_UpperCAmelCase    , _UpperCAmelCase    , input_shape=_UpperCAmelCase    , seed=_UpperCAmelCase    , dtype=_UpperCAmelCase    , _do_init=_do_init )






     def    lowerCamelCase__   (		self     :    List[str]    , UpperCamelCase_     :    List[Any]    , UpperCamelCase_     :    str    , UpperCamelCase_     :    int = None ):
           lowerCAmelCase    :		List[str]				=      jax.random.normal(_UpperCAmelCase    , _UpperCAmelCase )

           lowerCAmelCase    :		int				=      jax.random.split(_UpperCAmelCase )
           lowerCAmelCase    :		Optional[int]				=      {'params': params_rng, 'dropout': dropout_rng}

           lowerCAmelCase    :		int				=      self.module.init(_UpperCAmelCase    , _UpperCAmelCase )['params']

           return random_params






     def __call__(		self     :    Optional[Any]    , UpperCamelCase_     :    str    , UpperCamelCase_     :    Optional[int] = None    , ):
           lowerCAmelCase    :		str				=      jnp.transpose(_UpperCAmelCase    , (0, 2, 3, 1) )

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









'''simple docstring'''




from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowercase__    :      List[Any]						=    logging.get_logger(__name__)

lowercase__    :      str						=    {}


class  SCREAMING_SNAKE_CASE    (a__							):
     lowerCAmelCase					=       '''llama'''
     lowerCAmelCase					=       ['''past_key_values''']


     def __init__(						self						,	_UpperCAmelCase=3_2000						,	_UpperCAmelCase=4096						,	_UpperCAmelCase=1_1008						,	_UpperCAmelCase=32						,	_UpperCAmelCase=32						,	_UpperCAmelCase=None						,	_UpperCAmelCase="silu"						,	_UpperCAmelCase=2048						,	_UpperCAmelCase=0.02						,	_UpperCAmelCase=1e-6						,	_UpperCAmelCase=True						,	_UpperCAmelCase=0						,	_UpperCAmelCase=1						,	_UpperCAmelCase=2						,	_UpperCAmelCase=1						,	_UpperCAmelCase=False						,	_UpperCAmelCase=None						,	**_UpperCAmelCase						,	):

          '''simple docstring'''






          __A						: Union[str, Any]      =       vocab_size
          __A						: Union[str, Any]      =       max_position_embeddings
          __A						: Any      =       hidden_size
          __A						: Optional[Any]      =       intermediate_size
          __A						: str      =       num_hidden_layers
          __A						: Optional[Any]      =       num_attention_heads

          # for backward compatibility
          if num_key_value_heads is None:
               __A						: List[Any]      =       num_attention_heads

          __A						: int      =       num_key_value_heads
          __A						: List[Any]      =       hidden_act
          __A						: Union[str, Any]      =       initializer_range
          __A						: List[Any]      =       rms_norm_eps
          __A						: Any      =       pretraining_tp
          __A						: Optional[Any]      =       use_cache
          __A						: Dict      =       rope_scaling
          self._rope_scaling_validation()

          super().__init__(
              pad_token_id=_UpperCAmelCase						,	bos_token_id=_UpperCAmelCase						,	eos_token_id=_UpperCAmelCase						,	tie_word_embeddings=_UpperCAmelCase						,	**_UpperCAmelCase						,	)


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          if self.rope_scaling is None:
               return

          if not isinstance(self.rope_scaling						,	_UpperCAmelCase) or len(self.rope_scaling) != 2:
               raise ValueError(
                   '`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, '
                   F'got {self.rope_scaling}')
          __A						: Optional[Any]      =       self.rope_scaling.get('type'						,	_UpperCAmelCase)
          __A						: Tuple      =       self.rope_scaling.get('factor'						,	_UpperCAmelCase)
          if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
               raise ValueError(
                   F'`rope_scaling`\'s name field must be one of [\'linear\', \'dynamic\'], got {rope_scaling_type}')
          if rope_scaling_factor is None or not isinstance(_UpperCAmelCase						,	_UpperCAmelCase) or rope_scaling_factor <= 1.0:
               raise ValueError(F'`rope_scaling`\'s factor field must be an float > 1, got {rope_scaling_factor}')