
"""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 				A_  (__a					, __a					, __a=1e-12					):

					'''simple docstring'''





					A_								=    jnp.divide(emb_a.T					, jnp.clip(jnp.linalg.norm(__a					, axis=1					)					, a_min=__a					)					).T
					A_								=    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 __lowerCAmelCase					(       nn.Module   ):




			"""simple docstring"""







			snake_case						   =					42
			snake_case						   =					jnp.floataa




			def   lowerCamelCase__  (					self    :       Optional[int]						)   ->			Dict:







								"""simple docstring"""
								A_								=    FlaxCLIPVisionModule(self.config.vision_config						)
								A_								=    nn.Dense(self.config.projection_dim							, use_bias=_snake_case							, dtype=self.dtype						)

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

								A_								=    self.param("concept_embeds_weights"							, jax.nn.initializers.ones							, (17,)						)
								A_								=    self.param("special_care_embeds_weights"							, jax.nn.initializers.ones							, (3,)						)






			def __call__(					self    :       Optional[int]							, _snake_case    :       Optional[Any]						)   ->			Any:







								"""simple docstring"""
								A_								=    self.vision_model(_snake_case						)[1]
								A_								=    self.visual_projection(_snake_case						)

								A_								=    jax_cosine_distance(_snake_case							, self.special_care_embeds						)
								A_								=    jax_cosine_distance(_snake_case							, self.concept_embeds						)

								# increase this value to create a stronger `nfsw` filter
								# at the cost of increasing the possibility of filtering benign image inputs
								A_								=    0.0

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

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

								return has_nsfw_concepts





class __lowerCAmelCase					(       _lowercase   ):




			"""simple docstring"""







			snake_case						   =					CLIPConfig
			snake_case						   =					"clip_input"
			snake_case						   =					FlaxStableDiffusionSafetyCheckerModule




			def __init__(					self    :       List[Any]							, _snake_case    :       CLIPConfig							, _snake_case    :       Optional[Tuple] = None							, _snake_case    :       int = 0							, _snake_case    :       jnp.dtype = jnp.floataa							, _snake_case    :       bool = True							, **_snake_case    :       List[str]							, )   ->			List[str]:







								"""simple docstring"""
								if input_shape is None:
													A_								=    (1, 224, 224, 3)
								A_								=    self.module_class(config=_snake_case							, dtype=_snake_case							, **_snake_case						)
								super().__init__(_snake_case							, _snake_case							, input_shape=_snake_case							, seed=_snake_case							, dtype=_snake_case							, _do_init=_do_init						)




			def   lowerCamelCase__  (					self    :       str							, _snake_case    :       jax.random.KeyArray							, _snake_case    :       Tuple							, _snake_case    :       FrozenDict = None						)   ->			FrozenDict:







								"""simple docstring"""
								# init input tensor
								A_								=    jax.random.normal(_snake_case							, _snake_case						)

								A_			,     A_								=    jax.random.split(_snake_case						)
								A_								=    {"params": params_rng, "dropout": dropout_rng}

								A_								=    self.module.init(_snake_case							, _snake_case						)["params"]

								return random_params






			def __call__(					self    :       List[Any]							, _snake_case    :       List[str]							, _snake_case    :       dict = None							, )   ->			str:







								"""simple docstring"""
								A_								=    jnp.transpose(_snake_case							, (0, 2, 3, 1)						)

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






"""simple docstring"""





from ..utils import is_flax_available, is_torch_available


if is_torch_available():
		from .autoencoder_kl import AutoencoderKL
		from .controlnet import ControlNetModel
		from .dual_transformer_ad import DualTransformeraDModel
		from .modeling_utils import ModelMixin
		from .prior_transformer import PriorTransformer
		from .ta_film_transformer import TaFilmDecoder
		from .transformer_ad import TransformeraDModel
		from .unet_ad import UNetaDModel
		from .unet_ad import UNetaDModel
		from .unet_ad_condition import UNetaDConditionModel
		from .unet_ad_condition import UNetaDConditionModel
		from .vq_model import VQModel

if is_flax_available():
		from .controlnet_flax import FlaxControlNetModel
		from .unet_ad_condition_flax import FlaxUNetaDConditionModel
		from .vae_flax import FlaxAutoencoderKL




