






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_ (      _A      ,   _A      ,   _A=1e-12  )			->			Any:







				"""simple docstring"""
				snake_case__									=     jnp.divide(emb_a.T      ,   jnp.clip(jnp.linalg.norm(_A      ,   axis=1  )      ,   a_min=_A  )  ).T
				snake_case__									=     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   __SCREAMING_SNAKE_CASE(	nn.Module	):
							_UpperCAmelCase     =       4_2
							_UpperCAmelCase     =       jnp.floataa
							def lowerCAmelCase_ (     self:      Union[str, Any]						)		->		Tuple:
											snake_case__									=     FlaxCLIPVisionModule(self.config.vision_config						)
											snake_case__									=     nn.Dense(self.config.projection_dim       ,	use_bias=UpperCamelCase       ,	dtype=self.dtype						)

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

											snake_case__									=     self.param('concept_embeds_weights'       ,	jax.nn.initializers.ones       ,	(17,)						)
											snake_case__									=     self.param('special_care_embeds_weights'       ,	jax.nn.initializers.ones       ,	(3,)						)


							def __call__(     self:      Any       ,	UpperCamelCase:      Any						)		->		Optional[Any]:
											snake_case__									=     self.vision_model(UpperCamelCase						)[1]
											snake_case__									=     self.visual_projection(UpperCamelCase						)

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

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

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

											return has_nsfw_concepts


class   __SCREAMING_SNAKE_CASE(	a_	):
							_UpperCAmelCase     =       CLIPConfig
							_UpperCAmelCase     =       "clip_input"
							_UpperCAmelCase     =       FlaxStableDiffusionSafetyCheckerModule
							def __init__(     self:      int       ,	UpperCamelCase:      CLIPConfig       ,	UpperCamelCase:      Optional[Tuple] = None       ,	UpperCamelCase:      int = 0       ,	UpperCamelCase:      jnp.dtype = jnp.floataa       ,	UpperCamelCase:      bool = True       ,	**UpperCamelCase:      Any       ,	)		->		Optional[int]:
											if input_shape is None:
															snake_case__									=     (1, 2_24, 2_24, 3)
											snake_case__									=     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:      Optional[Any]       ,	UpperCamelCase:      jax.random.KeyArray       ,	UpperCamelCase:      Tuple       ,	UpperCamelCase:      FrozenDict = None						)		->		FrozenDict:
											# init input tensor
											snake_case__									=     jax.random.normal(UpperCamelCase       ,	UpperCamelCase						)

											snake_case__							,						snake_case__									=     jax.random.split(UpperCamelCase						)
											snake_case__									=     {'params': params_rng, 'dropout': dropout_rng}

											snake_case__									=     self.module.init(UpperCamelCase       ,	UpperCamelCase						)['params']

											return random_params


							def __call__(     self:      int       ,	UpperCamelCase:      str       ,	UpperCamelCase:      dict = None       ,	)		->		Any:
											snake_case__									=     jnp.transpose(UpperCamelCase       ,	(0, 2, 3, 1)						)

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












from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available


__UpperCamelCase					:		str	  =						{}

try:
							if not is_sentencepiece_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__UpperCamelCase					:		Dict	  =						["""BartphoTokenizer"""]

if TYPE_CHECKING:
							try:
														if not is_sentencepiece_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .tokenization_bartpho import BartphoTokenizer

else:
							import sys

							__UpperCamelCase					:		Any	  =						_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)




