"""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_(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE=1e-12  )->    Optional[int]:
							_SCREAMING_SNAKE_CASE		:						Dict			  =    jnp.divide(emb_a.T     ,	jnp.clip(jnp.linalg.norm(__SCREAMING_SNAKE_CASE     ,	axis=1  )     ,	a_min=__SCREAMING_SNAKE_CASE  )  ).T
							_SCREAMING_SNAKE_CASE		:						Any			  =    jnp.divide(emb_a.T     ,	jnp.clip(jnp.linalg.norm(__SCREAMING_SNAKE_CASE     ,	axis=1  )     ,	a_min=__SCREAMING_SNAKE_CASE  )  ).T
							return jnp.matmul(__SCREAMING_SNAKE_CASE     ,	norm_emb_a.T  )




class  _snake_case		(    nn.Module ):







				"""simple docstring"""






				a			=						42
				a			=						jnp.floataa





				def 		_lowerCAmelCase		(      self							:							Union[str, Any]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    FlaxCLIPVisionModule(self.config.vision_config)
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    nn.Dense(self.config.projection_dim      ,    use_bias=_A      ,    dtype=self.dtype)

											_SCREAMING_SNAKE_CASE		:						Dict			  =    self.param("""concept_embeds"""      ,    jax.nn.initializers.ones      ,    (1_7, self.config.projection_dim))
											_SCREAMING_SNAKE_CASE		:						Any			  =    self.param(
											    """special_care_embeds"""      ,    jax.nn.initializers.ones      ,    (3, self.config.projection_dim))

											_SCREAMING_SNAKE_CASE		:						Dict			  =    self.param("""concept_embeds_weights"""      ,    jax.nn.initializers.ones      ,    (1_7,))
											_SCREAMING_SNAKE_CASE		:						int			  =    self.param("""special_care_embeds_weights"""      ,    jax.nn.initializers.ones      ,    (3,))





				def __call__(      self							:							List[Any]      ,    _A							:							str):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Dict			  =    self.vision_model(_A)[1]
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    self.visual_projection(_A)

											_SCREAMING_SNAKE_CASE		:						str			  =    jax_cosine_distance(_A      ,    self.special_care_embeds)
											_SCREAMING_SNAKE_CASE		:						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
											_SCREAMING_SNAKE_CASE		:						Dict			  =    0.0

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

											_SCREAMING_SNAKE_CASE		:						Dict			  =    cos_dist - self.concept_embeds_weights[None, :] + special_adjustment
											_SCREAMING_SNAKE_CASE		:						int			  =    jnp.round(_A      ,    3)
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    jnp.any(concept_scores > 0      ,    axis=1)

											return has_nsfw_concepts




class  _snake_case		(    __snake_case ):







				"""simple docstring"""






				a			=						CLIPConfig
				a			=						"clip_input"
				a			=						FlaxStableDiffusionSafetyCheckerModule





				def __init__(      self							:							Optional[int]      ,    _A							:							CLIPConfig      ,    _A							:							Optional[Tuple] = None      ,    _A							:							int = 0      ,    _A							:							jnp.dtype = jnp.floataa      ,    _A							:							bool = True      ,    **_A							:							Optional[int]      ,    ):



											"""simple docstring"""







											if input_shape is None:
																		_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    (1, 2_2_4, 2_2_4, 3)
											_SCREAMING_SNAKE_CASE		:						Any			  =    self.module_class(config=_A      ,    dtype=_A      ,    **_A)
											super().__init__(_A      ,    _A      ,    input_shape=_A      ,    seed=_A      ,    dtype=_A      ,    _do_init=_do_init)





				def 		_lowerCAmelCase		(      self							:							Dict      ,    _A							:							jax.random.KeyArray      ,    _A							:							Tuple      ,    _A							:							FrozenDict = None):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Dict			  =    jax.random.normal(_A      ,    _A)

											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						List[Any]			  =    jax.random.split(_A)
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    {"""params""": params_rng, """dropout""": dropout_rng}

											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    self.module.init(_A      ,    _A)["""params"""]

											return random_params





				def __call__(      self							:							Tuple      ,    _A							:							Union[str, Any]      ,    _A							:							dict = None      ,    ):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Tuple			  =    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 typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_torch_available,
)


lowerCAmelCase_     =	{'''configuration_unispeech''': ['''UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''UniSpeechConfig''']}

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			lowerCAmelCase_     =	[
			    '''UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST''',
			    '''UniSpeechForCTC''',
			    '''UniSpeechForPreTraining''',
			    '''UniSpeechForSequenceClassification''',
			    '''UniSpeechModel''',
			    '''UniSpeechPreTrainedModel''',
			]

if TYPE_CHECKING:
			from .configuration_unispeech import UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP, UniSpeechConfig

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_unispeech import (
						    UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST,
						    UniSpeechForCTC,
						    UniSpeechForPreTraining,
						    UniSpeechForSequenceClassification,
						    UniSpeechModel,
						    UniSpeechPreTrainedModel,
						)

else:
			import sys

			lowerCAmelCase_     =	_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)


