'''simple docstring'''




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


__lowercase          =  {
    '''configuration_xlm_roberta_xl''': [
        '''XLM_ROBERTA_XL_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''XLMRobertaXLConfig''',
        '''XLMRobertaXLOnnxConfig''',
    ],
}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						__lowercase          =  [
						    '''XLM_ROBERTA_XL_PRETRAINED_MODEL_ARCHIVE_LIST''',
						    '''XLMRobertaXLForCausalLM''',
						    '''XLMRobertaXLForMaskedLM''',
						    '''XLMRobertaXLForMultipleChoice''',
						    '''XLMRobertaXLForQuestionAnswering''',
						    '''XLMRobertaXLForSequenceClassification''',
						    '''XLMRobertaXLForTokenClassification''',
						    '''XLMRobertaXLModel''',
						    '''XLMRobertaXLPreTrainedModel''',
						]

if TYPE_CHECKING:
						from .configuration_xlm_roberta_xl import (
						    XLM_ROBERTA_XL_PRETRAINED_CONFIG_ARCHIVE_MAP,
						    XLMRobertaXLConfig,
						    XLMRobertaXLOnnxConfig,
						)

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_xlm_roberta_xl import (
												    XLM_ROBERTA_XL_PRETRAINED_MODEL_ARCHIVE_LIST,
												    XLMRobertaXLForCausalLM,
												    XLMRobertaXLForMaskedLM,
												    XLMRobertaXLForMultipleChoice,
												    XLMRobertaXLForQuestionAnswering,
												    XLMRobertaXLForSequenceClassification,
												    XLMRobertaXLForTokenClassification,
												    XLMRobertaXLModel,
												    XLMRobertaXLPreTrainedModel,
												)

else:
						import sys

						__lowercase          =  _LazyModule(__name__, globals()['''__file__'''], _import_structure)





'''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__							(			_A:					Union[str, Any]      ,   _A:					Tuple      ,   _A:					Any=1e-12 )    ->	str:

		'''simple docstring'''


		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	):


			'''simple docstring'''






			UpperCAmelCase_			:     CLIPConfig
			UpperCAmelCase_			:     jnp.dtype      =       jnp.floataa



			def 							a_       (   self):




					"""simple docstring"""


					lowerCAmelCase   					=  FlaxCLIPVisionModule(self.config.vision_config)
					lowerCAmelCase   					=  nn.Dense(self.config.projection_dim       ,    use_bias=__lowerCAmelCase       ,    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       ,    __lowerCAmelCase):




					"""simple docstring"""


					lowerCAmelCase   					=  self.vision_model(__lowerCAmelCase)[1]
					lowerCAmelCase   					=  self.visual_projection(__lowerCAmelCase)

					lowerCAmelCase   					=  jax_cosine_distance(__lowerCAmelCase       ,    self.special_care_embeds)
					lowerCAmelCase   					=  jax_cosine_distance(__lowerCAmelCase       ,    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(__lowerCAmelCase       ,    3)
					lowerCAmelCase   					=  jnp.any(special_scores > 0       ,    axis=1       ,    keepdims=__lowerCAmelCase)
					# 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(__lowerCAmelCase       ,    3)
					lowerCAmelCase   					=  jnp.any(concept_scores > 0       ,    axis=1)

					return has_nsfw_concepts







class 				a__(					lowerCAmelCase__	):


			'''simple docstring'''






			UpperCAmelCase_			:     int      =       CLIPConfig
			UpperCAmelCase_			:     Any      =       '''clip_input'''
			UpperCAmelCase_			:     List[str]      =       FlaxStableDiffusionSafetyCheckerModule



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




					"""simple docstring"""


					if input_shape is None:
							lowerCAmelCase   					=  (1, 224, 224, 3)
					lowerCAmelCase   					=  self.module_class(config=__lowerCAmelCase       ,    dtype=__lowerCAmelCase       ,    **__lowerCAmelCase)
					super().__init__(__lowerCAmelCase       ,    __lowerCAmelCase       ,    input_shape=__lowerCAmelCase       ,    seed=__lowerCAmelCase       ,    dtype=__lowerCAmelCase       ,    _do_init=_do_init)



			def 							a_       (   self       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase = None):




					"""simple docstring"""


					lowerCAmelCase   					=  jax.random.normal(__lowerCAmelCase       ,    __lowerCAmelCase)

					lowerCAmelCase					,     lowerCAmelCase   					=  jax.random.split(__lowerCAmelCase)
					lowerCAmelCase   					=  {"""params""": params_rng, """dropout""": dropout_rng}

					lowerCAmelCase   					=  self.module.init(__lowerCAmelCase       ,    __lowerCAmelCase)["""params"""]

					return random_params







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




					"""simple docstring"""


					lowerCAmelCase   					=  jnp.transpose(__lowerCAmelCase       ,    (0, 2, 3, 1))

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




