
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 	UpperCamelCase   (							lowerCAmelCase__			,      lowerCAmelCase__			,      lowerCAmelCase__=1E-12		):
					'''simple docstring'''
					lowercase						     =						jnp.divide(emb_a.T			,      jnp.clip(jnp.linalg.norm(lowerCAmelCase__			,      axis=1		)			,      a_min=lowerCAmelCase__		)		).T
					lowercase						     =						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		):
					lowercase_	:	CLIPConfig
					lowercase_	:	jnp.dtype	 =jnp.floataa




					def 			A__				(    self):
										lowercase						     =						FlaxCLIPVisionModule(self.config.vision_config)
										lowercase						     =						nn.Dense(self.config.projection_dim       ,use_bias=A__       ,dtype=self.dtype)

										lowercase						     =						self.param('''concept_embeds'''       ,jax.nn.initializers.ones       ,(1_7, self.config.projection_dim))
										lowercase						     =						self.param(
										    '''special_care_embeds'''       ,jax.nn.initializers.ones       ,(3, self.config.projection_dim))

										lowercase						     =						self.param('''concept_embeds_weights'''       ,jax.nn.initializers.ones       ,(1_7,))
										lowercase						     =						self.param('''special_care_embeds_weights'''       ,jax.nn.initializers.ones       ,(3,))





					def __call__(    self       ,A__):
										lowercase						     =						self.vision_model(A__)[1]
										lowercase						     =						self.visual_projection(A__)

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

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

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

										return has_nsfw_concepts







class     lowercase		( SCREAMING_SNAKE_CASE__		):
					lowercase_	:	List[Any]	 =CLIPConfig
					lowercase_	:	Optional[int]	 ='''clip_input'''
					lowercase_	:	str	 =FlaxStableDiffusionSafetyCheckerModule




					def __init__(    self       ,A__       ,A__ = None       ,A__ = 0       ,A__ = jnp.floataa       ,A__ = True       ,**A__       ,):
										if input_shape is None:
															lowercase						     =						(1, 2_2_4, 2_2_4, 3)
										lowercase						     =						self.module_class(config=A__       ,dtype=A__       ,**A__)
										super().__init__(A__       ,A__       ,input_shape=A__       ,seed=A__       ,dtype=A__       ,_do_init=_do_init)




					def 			A__				(    self       ,A__       ,A__       ,A__ = None):
										# init input tensor
										lowercase						     =						jax.random.normal(A__       ,A__)

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

										lowercase						     =						self.module.init(A__       ,A__)['''params''']

										return random_params





					def __call__(    self       ,A__       ,A__ = None       ,):
										lowercase						     =						jnp.transpose(A__       ,(0, 2, 3, 1))

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






from typing import TYPE_CHECKING

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


lowercase__		:Any			  =   {
    "configuration_distilbert": [
        "DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "DistilBertConfig",
        "DistilBertOnnxConfig",
    ],
    "tokenization_distilbert": ["DistilBertTokenizer"],
}

try:
							if not is_tokenizers_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							lowercase__		:Tuple			  =   ["DistilBertTokenizerFast"]

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							lowercase__		:Union[str, Any]			  =   [
							    "DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST",
							    "DistilBertForMaskedLM",
							    "DistilBertForMultipleChoice",
							    "DistilBertForQuestionAnswering",
							    "DistilBertForSequenceClassification",
							    "DistilBertForTokenClassification",
							    "DistilBertModel",
							    "DistilBertPreTrainedModel",
							]

try:
							if not is_tf_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							lowercase__		:Optional[int]			  =   [
							    "TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST",
							    "TFDistilBertForMaskedLM",
							    "TFDistilBertForMultipleChoice",
							    "TFDistilBertForQuestionAnswering",
							    "TFDistilBertForSequenceClassification",
							    "TFDistilBertForTokenClassification",
							    "TFDistilBertMainLayer",
							    "TFDistilBertModel",
							    "TFDistilBertPreTrainedModel",
							]

try:
							if not is_flax_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							lowercase__		:Union[str, Any]			  =   [
							    "FlaxDistilBertForMaskedLM",
							    "FlaxDistilBertForMultipleChoice",
							    "FlaxDistilBertForQuestionAnswering",
							    "FlaxDistilBertForSequenceClassification",
							    "FlaxDistilBertForTokenClassification",
							    "FlaxDistilBertModel",
							    "FlaxDistilBertPreTrainedModel",
							]


if TYPE_CHECKING:
							from .configuration_distilbert import (
							    DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP,
							    DistilBertConfig,
							    DistilBertOnnxConfig,
							)
							from .tokenization_distilbert import DistilBertTokenizer

							try:
														if not is_tokenizers_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .tokenization_distilbert_fast import DistilBertTokenizerFast

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_distilbert import (
														    DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    DistilBertForMaskedLM,
														    DistilBertForMultipleChoice,
														    DistilBertForQuestionAnswering,
														    DistilBertForSequenceClassification,
														    DistilBertForTokenClassification,
														    DistilBertModel,
														    DistilBertPreTrainedModel,
														)

							try:
														if not is_tf_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_tf_distilbert import (
														    TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    TFDistilBertForMaskedLM,
														    TFDistilBertForMultipleChoice,
														    TFDistilBertForQuestionAnswering,
														    TFDistilBertForSequenceClassification,
														    TFDistilBertForTokenClassification,
														    TFDistilBertMainLayer,
														    TFDistilBertModel,
														    TFDistilBertPreTrainedModel,
														)

							try:
														if not is_flax_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_flax_distilbert import (
														    FlaxDistilBertForMaskedLM,
														    FlaxDistilBertForMultipleChoice,
														    FlaxDistilBertForQuestionAnswering,
														    FlaxDistilBertForSequenceClassification,
														    FlaxDistilBertForTokenClassification,
														    FlaxDistilBertModel,
														    FlaxDistilBertPreTrainedModel,
														)

else:
							import sys

							lowercase__		:List[str]			  =   _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)




