





'''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        _lowerCAmelCase			(  __snake_case  : Any ,							__snake_case  : List[Any] ,							__snake_case  : List[Any]=1e-1_2			)     ->     Optional[int]:
     __A						: List[str]      =       jnp.divide(emb_a.T ,							jnp.clip(jnp.linalg.norm(__snake_case ,							axis=1			) ,							a_min=__snake_case			)			).T
     __A						: 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  SCREAMING_SNAKE_CASE    (nn.Module							):
     lowerCAmelCase					=       42
     lowerCAmelCase					=       jnp.floataa


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Tuple      =       FlaxCLIPVisionModule(self.config.vision_config)
          __A						: int      =       nn.Dense(self.config.projection_dim						,	use_bias=_UpperCAmelCase						,	dtype=self.dtype)

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

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


     def __call__(						self						,	_UpperCAmelCase):

          '''simple docstring'''






          __A						: Union[str, Any]      =       self.vision_model(_UpperCAmelCase)[1]
          __A						: str      =       self.visual_projection(_UpperCAmelCase)

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

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

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

          return has_nsfw_concepts




class  SCREAMING_SNAKE_CASE    (a__							):
     lowerCAmelCase					=       CLIPConfig
     lowerCAmelCase					=       '''clip_input'''
     lowerCAmelCase					=       FlaxStableDiffusionSafetyCheckerModule


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

          '''simple docstring'''






          if input_shape is None:
               __A						: str      =       (1, 224, 224, 3)
          __A						: 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 							SCREAMING_SNAKE_CASE  (						self						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase = None):

          '''simple docstring'''






          __A						: List[str]      =       jax.random.normal(_UpperCAmelCase						,	_UpperCAmelCase)

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

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

          return random_params


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

          '''simple docstring'''






          __A						: 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}')