



"""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__     (				lowerCAmelCase__     ,      lowerCAmelCase__     ,      lowerCAmelCase__=1e-12				):
      UpperCAmelCase_      			=							jnp.divide(emb_a.T     ,      jnp.clip(jnp.linalg.norm(lowerCAmelCase__     ,      axis=1				)     ,      a_min=lowerCAmelCase__				)				).T
      UpperCAmelCase_      			=							jnp.divide(emb_a.T     ,      jnp.clip(jnp.linalg.norm(lowerCAmelCase__     ,      axis=1				)     ,      a_min=lowerCAmelCase__				)				).T
      return jnp.matmul(lowerCAmelCase__     ,      norm_emb_a.T				)
class 				lowercase__       (   nn.Module				):







       '''simple docstring'''


       UpperCamelCase						=					42
       UpperCamelCase						=					jnp.floataa



       def 					lowercase__    (							self      :  Optional[Any]   )			->						List[str]:


             '''simple docstring'''




             UpperCAmelCase_      			=							FlaxCLIPVisionModule(self.config.vision_config   )
             UpperCAmelCase_      			=							nn.Dense(self.config.projection_dim		,    use_bias=_UpperCAmelCase		,    dtype=self.dtype   )

             UpperCAmelCase_      			=							self.param("concept_embeds"		,    jax.nn.initializers.ones		,    (17, self.config.projection_dim)   )
             UpperCAmelCase_      			=							self.param(
                 "special_care_embeds"		,    jax.nn.initializers.ones		,    (3, self.config.projection_dim)   )

             UpperCAmelCase_      			=							self.param("concept_embeds_weights"		,    jax.nn.initializers.ones		,    (17,)   )
             UpperCAmelCase_      			=							self.param("special_care_embeds_weights"		,    jax.nn.initializers.ones		,    (3,)   )







       def __call__(							self      :  Any		,    _UpperCAmelCase      :  Optional[Any]   )			->						Optional[int]:


             '''simple docstring'''




             UpperCAmelCase_      			=							self.vision_model(_UpperCAmelCase   )[1]
             UpperCAmelCase_      			=							self.visual_projection(_UpperCAmelCase   )

             UpperCAmelCase_      			=							jax_cosine_distance(_UpperCAmelCase		,    self.special_care_embeds   )
             UpperCAmelCase_      			=							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
             UpperCAmelCase_      			=							0.0

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

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

             return has_nsfw_concepts



class 				lowercase__       (   SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					CLIPConfig
       UpperCamelCase						=					'''clip_input'''
       UpperCamelCase						=					FlaxStableDiffusionSafetyCheckerModule



       def __init__(							self      :  Tuple		,    _UpperCAmelCase      :  CLIPConfig		,    _UpperCAmelCase      :  Optional[Tuple] = None		,    _UpperCAmelCase      :  int = 0		,    _UpperCAmelCase      :  jnp.dtype = jnp.floataa		,    _UpperCAmelCase      :  bool = True		,    **_UpperCAmelCase      :  Union[str, Any]		,    )			->						Dict:


             '''simple docstring'''




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



       def 					lowercase__    (							self      :  str		,    _UpperCAmelCase      :  jax.random.KeyArray		,    _UpperCAmelCase      :  Tuple		,    _UpperCAmelCase      :  FrozenDict = None   )			->						FrozenDict:


             '''simple docstring'''




             UpperCAmelCase_      			=							jax.random.normal(_UpperCAmelCase		,    _UpperCAmelCase   )

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

             UpperCAmelCase_      			=							self.module.init(_UpperCAmelCase		,    _UpperCAmelCase   )["params"]

             return random_params







       def __call__(							self      :  int		,    _UpperCAmelCase      :  Optional[int]		,    _UpperCAmelCase      :  dict = None		,    )			->						Optional[int]:


             '''simple docstring'''




             UpperCAmelCase_      			=							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"""





import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow


if is_torch_available():
  import torch

  from transformers import XLMRobertaModel
@require_sentencepiece
@require_tokenizers
@require_torch
class 				lowercase__       (   unittest.TestCase				):







       '''simple docstring'''


       @slow
       def 					lowercase__    (							self      :  Tuple   )			->						str:


             '''simple docstring'''




             UpperCAmelCase_      			=							XLMRobertaModel.from_pretrained("xlm-roberta-base"   )
             UpperCAmelCase_      			=							torch.tensor([[0, 581, 10269, 83, 99942, 136, 60742, 23, 70, 80583, 18276, 2]]   )
             # The dog is cute and lives in the garden house

             UpperCAmelCase_      			=							torch.Size((1, 12, 768)   )  # batch_size, sequence_length, embedding_vector_dim
             UpperCAmelCase_      			=							torch.tensor(
                 [[-0.0101, 0.1218, -0.0803, 0.0801, 0.1327, 0.0776, -0.1215, 0.2383, 0.3338, 0.3106, 0.0300, 0.0252]]   )
             #  xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base')
             #  xlmr.eval()
             #  expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1]
             with torch.no_grad():
                   UpperCAmelCase_      			=							model(_UpperCAmelCase   )["last_hidden_state"].detach()
             self.assertEqual(output.shape		,    _UpperCAmelCase   )
             # compare the actual values for a slice of last dim
             self.assertTrue(torch.allclose(output[:, :, -1]		,    _UpperCAmelCase		,    atol=1e-3   )   )







       @slow
       def 					lowercase__    (							self      :  List[Any]   )			->						Optional[int]:


             '''simple docstring'''




             UpperCAmelCase_      			=							XLMRobertaModel.from_pretrained("xlm-roberta-large"   )
             UpperCAmelCase_      			=							torch.tensor([[0, 581, 10269, 83, 99942, 136, 60742, 23, 70, 80583, 18276, 2]]   )
             # The dog is cute and lives in the garden house

             UpperCAmelCase_      			=							torch.Size((1, 12, 1024)   )  # batch_size, sequence_length, embedding_vector_dim
             UpperCAmelCase_      			=							torch.tensor(
                 [[-0.0699, -0.0318, 0.0705, -0.1241, 0.0999, -0.0520, 0.1004, -0.1838, -0.4704, 0.1437, 0.0821, 0.0126]]   )
             #  xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large')
             #  xlmr.eval()
             #  expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1]
             with torch.no_grad():
                   UpperCAmelCase_      			=							model(_UpperCAmelCase   )["last_hidden_state"].detach()
             self.assertEqual(output.shape		,    _UpperCAmelCase   )
             # compare the actual values for a slice of last dim
             self.assertTrue(torch.allclose(output[:, :, -1]		,    _UpperCAmelCase		,    atol=1e-3   )   )




