

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__       (    _a							,	_a							,	_a=1E-12):
	SCREAMING_SNAKE_CASE      :					Optional[Any]    =				jnp.divide(emb_a.T							,	jnp.clip(jnp.linalg.norm(_a							,	axis=1)							,	a_min=_a)).T
	SCREAMING_SNAKE_CASE      :					Any    =				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      ):
			'''simple docstring'''







			lowerCamelCase__							=42
			lowerCamelCase__							=jnp.floataa
			def 							__UpperCamelCase       (	self							:       List[Any]		)							->				Dict:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE      :					Dict    =				FlaxCLIPVisionModule(self.config.vision_config		)
				SCREAMING_SNAKE_CASE      :					Any    =				nn.Dense(self.config.projection_dim					,					use_bias=a					,					dtype=self.dtype		)

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

				SCREAMING_SNAKE_CASE      :					List[Any]    =				self.param("concept_embeds_weights"					,					jax.nn.initializers.ones					,					(17,)		)
				SCREAMING_SNAKE_CASE      :					Any    =				self.param("special_care_embeds_weights"					,					jax.nn.initializers.ones					,					(3,)		)






			def __call__(	self							:       Optional[Any]					,					a							:       List[str]		)							->				List[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE      :					Union[str, Any]    =				self.vision_model(a		)[1]
				SCREAMING_SNAKE_CASE      :					Optional[Any]    =				self.visual_projection(a		)

				SCREAMING_SNAKE_CASE      :					List[Any]    =				jax_cosine_distance(a					,					self.special_care_embeds		)
				SCREAMING_SNAKE_CASE      :					str    =				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      :					Optional[int]    =				0.0

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

				SCREAMING_SNAKE_CASE      :					Union[str, Any]    =				cos_dist - self.concept_embeds_weights[None, :] + special_adjustment
				SCREAMING_SNAKE_CASE      :					Dict    =				jnp.round(a					,					3		)
				SCREAMING_SNAKE_CASE      :					int    =				jnp.any(concept_scores > 0					,					axis=1		)

				return has_nsfw_concepts






class _UpperCamelCase							(							__A      ):
			'''simple docstring'''







			lowerCamelCase__							=CLIPConfig
			lowerCamelCase__							='clip_input'
			lowerCamelCase__							=FlaxStableDiffusionSafetyCheckerModule
			def __init__(	self							:       str					,					a							:       CLIPConfig					,					a							:       Optional[Tuple] = None					,					a							:       int = 0					,					a							:       jnp.dtype = jnp.floataa					,					a							:       bool = True					,					**a							:       str					,					)							->				int:

				"""simple docstring"""

				if input_shape is None:
					SCREAMING_SNAKE_CASE      :					List[Any]    =				(1, 224, 224, 3)
				SCREAMING_SNAKE_CASE      :					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 							__UpperCamelCase       (	self							:       str					,					a							:       jax.random.KeyArray					,					a							:       Tuple					,					a							:       FrozenDict = None		)							->				FrozenDict:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE      :					Union[str, Any]    =				jax.random.normal(a					,					a		)

				SCREAMING_SNAKE_CASE   ,SCREAMING_SNAKE_CASE      :					Tuple    =				jax.random.split(a		)
				SCREAMING_SNAKE_CASE      :					Union[str, Any]    =				{"params": params_rng, "dropout": dropout_rng}

				SCREAMING_SNAKE_CASE      :					int    =				self.module.init(a					,					a		)["params"]

				return random_params






			def __call__(	self							:       List[Any]					,					a							:       Optional[int]					,					a							:       dict = None					,					)							->				Tuple:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE      :					Any    =				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_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


lowercase  :					str						    =							{
    """configuration_funnel""": ["""FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP""", """FunnelConfig"""],
    """convert_funnel_original_tf_checkpoint_to_pytorch""": [],
    """tokenization_funnel""": ["""FunnelTokenizer"""],
}

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

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase  :					List[str]						    =							[
      """FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """FunnelBaseModel""",
      """FunnelForMaskedLM""",
      """FunnelForMultipleChoice""",
      """FunnelForPreTraining""",
      """FunnelForQuestionAnswering""",
      """FunnelForSequenceClassification""",
      """FunnelForTokenClassification""",
      """FunnelModel""",
      """FunnelPreTrainedModel""",
      """load_tf_weights_in_funnel""",
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase  :					Optional[Any]						    =							[
      """TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """TFFunnelBaseModel""",
      """TFFunnelForMaskedLM""",
      """TFFunnelForMultipleChoice""",
      """TFFunnelForPreTraining""",
      """TFFunnelForQuestionAnswering""",
      """TFFunnelForSequenceClassification""",
      """TFFunnelForTokenClassification""",
      """TFFunnelModel""",
      """TFFunnelPreTrainedModel""",
  ]


if TYPE_CHECKING:
  from .configuration_funnel import FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP, FunnelConfig
  from .tokenization_funnel import FunnelTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_funnel_fast import FunnelTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_funnel import (
        FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST,
        FunnelBaseModel,
        FunnelForMaskedLM,
        FunnelForMultipleChoice,
        FunnelForPreTraining,
        FunnelForQuestionAnswering,
        FunnelForSequenceClassification,
        FunnelForTokenClassification,
        FunnelModel,
        FunnelPreTrainedModel,
        load_tf_weights_in_funnel,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_funnel import (
        TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFFunnelBaseModel,
        TFFunnelForMaskedLM,
        TFFunnelForMultipleChoice,
        TFFunnelForPreTraining,
        TFFunnelForQuestionAnswering,
        TFFunnelForSequenceClassification,
        TFFunnelForTokenClassification,
        TFFunnelModel,
        TFFunnelPreTrainedModel,
    )

else:
  import sys

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