






"""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__			,A__			,A__=1e-12		):
			UpperCAmelCase_  :				Optional[Any]      =      jnp.divide(emb_a.T			,jnp.clip(jnp.linalg.norm(A__			,axis=1		)			,a_min=A__		)		).T
			UpperCAmelCase_  :				int      =      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     UpperCamelCase_ (nn.Module  ):
	__magic_name__			   =       42
	__magic_name__			   =       jnp.floataa





	def   _SCREAMING_SNAKE_CASE   (   self      :				Dict		)		->    Optional[int]:
				UpperCAmelCase_  :				int      =      FlaxCLIPVisionModule(self.config.vision_config		)
				UpperCAmelCase_  :				str      =      nn.Dense(self.config.projection_dim			,			use_bias=A_			,			dtype=self.dtype		)

				UpperCAmelCase_  :				Optional[int]      =      self.param("concept_embeds"			,			jax.nn.initializers.ones			,			(17, self.config.projection_dim)		)
				UpperCAmelCase_  :				str      =      self.param(
				    "special_care_embeds"			,			jax.nn.initializers.ones			,			(3, self.config.projection_dim)		)

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





	def __call__(   self      :				List[Any]			,			lowerCAmelCase_      :				List[str]		)		->    List[Any]:
				UpperCAmelCase_  :				List[Any]      =      self.vision_model(A_		)[1]
				UpperCAmelCase_  :				Dict      =      self.visual_projection(A_		)

				UpperCAmelCase_  :				Dict      =      jax_cosine_distance(A_			,			self.special_care_embeds		)
				UpperCAmelCase_  :				Optional[Any]      =      jax_cosine_distance(A_			,			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_  :				Union[str, Any]      =      0.0

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

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

				return has_nsfw_concepts







class     UpperCamelCase_ (_UpperCAmelCase  ):
	__magic_name__			   =       CLIPConfig
	__magic_name__			   =       '''clip_input'''
	__magic_name__			   =       FlaxStableDiffusionSafetyCheckerModule





	def __init__(   self      :				str			,			lowerCAmelCase_      :				CLIPConfig			,			lowerCAmelCase_      :				Optional[Tuple] = None			,			lowerCAmelCase_      :				int = 0			,			lowerCAmelCase_      :				jnp.dtype = jnp.floataa			,			lowerCAmelCase_      :				bool = True			,			**lowerCAmelCase_      :				Optional[Any]			,			)		->    Union[str, Any]:
				if input_shape is None:
							UpperCAmelCase_  :				Any      =      (1, 224, 224, 3)
				UpperCAmelCase_  :				Optional[int]      =      self.module_class(config=A_			,			dtype=A_			,			**A_		)
				super().__init__(A_			,			A_			,			input_shape=A_			,			seed=A_			,			dtype=A_			,			_do_init=_do_init		)





	def   _SCREAMING_SNAKE_CASE   (   self      :				List[str]			,			lowerCAmelCase_      :				jax.random.KeyArray			,			lowerCAmelCase_      :				Tuple			,			lowerCAmelCase_      :				FrozenDict = None		)		->    FrozenDict:
				# init input tensor
				UpperCAmelCase_  :				Union[str, Any]      =      jax.random.normal(A_			,			A_		)

				UpperCAmelCase_  ,			UpperCAmelCase_  :				Optional[int]      =      jax.random.split(A_		)
				UpperCAmelCase_  :				str      =      {"params": params_rng, "dropout": dropout_rng}

				UpperCAmelCase_  :				str      =      self.module.init(A_			,			A_		)["params"]

				return random_params





	def __call__(   self      :				Optional[Any]			,			lowerCAmelCase_      :				Dict			,			lowerCAmelCase_      :				dict = None			,			)		->    Optional[Any]:
				UpperCAmelCase_  :				List[str]      =      jnp.transpose(A_			,			(0, 2, 3, 1)		)

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












"""simple docstring"""
from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow


if is_tf_available():
			import numpy as np
			import tensorflow as tf

			from transformers import TFCamembertModel







@require_tf
@require_sentencepiece
@require_tokenizers
class     UpperCamelCase_ (unittest.TestCase  ):





	@slow
	def   _SCREAMING_SNAKE_CASE   (   self      :				Optional[Any]		)		->    Optional[Any]:
				UpperCAmelCase_  :				List[Any]      =      TFCamembertModel.from_pretrained("jplu/tf-camembert-base"		)

				UpperCAmelCase_  :				Tuple      =      tf.convert_to_tensor(
				    [[5, 121, 11, 660, 16, 730, 25_543, 110, 83, 6]]			,			dtype=tf.intaa			,			)  # J'aime le camembert !"

				UpperCAmelCase_  :				Any      =      model(lowerCAmelCase_		)["last_hidden_state"]
				UpperCAmelCase_  :				str      =      tf.TensorShape((1, 10, 768)		)
				self.assertEqual(output.shape			,			lowerCAmelCase_		)
				# compare the actual values for a slice.
				UpperCAmelCase_  :				Tuple      =      tf.convert_to_tensor(
				    [[[-0.0_2_5_4, 0.0_2_3_5, 0.1_0_2_7], [0.0_6_0_6, -0.1_8_1_1, -0.0_4_1_8], [-0.1_5_6_1, -0.1_1_2_7, 0.2_6_8_7]]]			,			dtype=tf.floataa			,			)
				# camembert = torch.hub.load('pytorch/fairseq', 'camembert.v0')
				# camembert.eval()
				# expected_slice = roberta.model.forward(input_ids)[0][:, :3, :3].detach()

				self.assertTrue(np.allclose(output[:, :3, :3].numpy()			,			expected_slice.numpy()			,			atol=1e-4		)		)



