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     (							UpperCAmelCase,		UpperCAmelCase,		UpperCAmelCase=1e-12							)->							int:



			"""simple docstring"""







			lowercase       		=		jnp.divide(emb_a.T,		jnp.clip(jnp.linalg.norm(lowercase__,		axis=1							),		a_min=lowercase__							)							).T
			lowercase       		=		jnp.divide(emb_a.T,		jnp.clip(jnp.linalg.norm(lowercase__,		axis=1							),		a_min=lowercase__							)							).T
			return jnp.matmul(lowercase__,		norm_emb_a.T							)






class   __lowercase       (     nn.Module     ):
						lowercase       			=  42
						lowercase       			=  jnp.floataa

						def 							__a		(						self					:   int )				->		Any:


									'''simple docstring'''

									lowercase       		=		FlaxCLIPVisionModule(self.config.vision_config )
									lowercase       		=		nn.Dense(self.config.projection_dim				,				use_bias=__lowercase				,				dtype=self.dtype )

									lowercase       		=		self.param('''concept_embeds'''				,				jax.nn.initializers.ones				,				(17, 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				,				(17,) )
									lowercase       		=		self.param('''special_care_embeds_weights'''				,				jax.nn.initializers.ones				,				(3,) )



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


									'''simple docstring'''

									lowercase       		=		self.vision_model(__lowercase )[1]
									lowercase       		=		self.visual_projection(__lowercase )

									lowercase       		=		jax_cosine_distance(__lowercase				,				self.special_care_embeds )
									lowercase       		=		jax_cosine_distance(__lowercase				,				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(__lowercase				,				3 )
									lowercase       		=		jnp.any(special_scores > 0				,				axis=1				,				keepdims=__lowercase )
									# 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(__lowercase				,				3 )
									lowercase       		=		jnp.any(concept_scores > 0				,				axis=1 )

									return has_nsfw_concepts






class   __lowercase       (     _A     ):
						lowercase       			=  CLIPConfig
						lowercase       			=  'clip_input'
						lowercase       			=  FlaxStableDiffusionSafetyCheckerModule

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


									'''simple docstring'''

									if input_shape is None:
												lowercase       		=		(1, 2_24, 2_24, 3)
									lowercase       		=		self.module_class(config=__lowercase				,				dtype=__lowercase				,				**__lowercase )
									super().__init__(__lowercase				,				__lowercase				,				input_shape=__lowercase				,				seed=__lowercase				,				dtype=__lowercase				,				_do_init=_do_init )

						def 							__a		(						self					:   Tuple				,				__lowerCamelCase					:   jax.random.KeyArray				,				__lowerCamelCase					:   Tuple				,				__lowerCamelCase					:   FrozenDict = None )				->		Dict:


									'''simple docstring'''

									lowercase       		=		jax.random.normal(__lowercase				,				__lowercase )

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

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

									return random_params



						def __call__(						self					:   Union[str, Any]				,				__lowerCamelCase					:   Optional[int]				,				__lowerCamelCase					:   dict = None				,				)				->		Optional[int]:


									'''simple docstring'''

									lowercase       		=		jnp.transpose(__lowercase				,				(0, 2, 3, 1) )

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








'''simple docstring'''

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


UpperCAmelCase					 =	logging.get_logger(__name__)

UpperCAmelCase					 =	{
    '''shi-labs/dinat-mini-in1k-224''': '''https://huggingface.co/shi-labs/dinat-mini-in1k-224/resolve/main/config.json''',
    # See all Dinat models at https://huggingface.co/models?filter=dinat
}


class 	lowerCAmelCase  (   A ,      A ):
       lowerCAmelCase_												=			"dinat"

       lowerCAmelCase_												=			{
           "num_attention_heads": "num_heads",
           "num_hidden_layers": "num_layers",
       }







       def __init__(				self      :			int    ,   __lowercase      :			str=4    ,   __lowercase      :			str=3    ,   __lowercase      :			int=64    ,   __lowercase      :			int=[3, 4, 6, 5]    ,   __lowercase      :			Union[str, Any]=[2, 4, 8, 16]    ,   __lowercase      :			Optional[int]=7    ,   __lowercase      :			str=[[1, 8, 1], [1, 4, 1, 4], [1, 2, 1, 2, 1, 2], [1, 1, 1, 1, 1]]    ,   __lowercase      :			Any=3.0    ,   __lowercase      :			Optional[Any]=True    ,   __lowercase      :			List[Any]=0.0    ,   __lowercase      :			Optional[Any]=0.0    ,   __lowercase      :			int=0.1    ,   __lowercase      :			Union[str, Any]="gelu"    ,   __lowercase      :			Any=0.0_2    ,   __lowercase      :			int=1E-5    ,   __lowercase      :			Optional[int]=0.0    ,   __lowercase      :			Optional[int]=None    ,   __lowercase      :			str=None    ,   **__lowercase      :			Any    ,   ):







            """simple docstring"""







            super().__init__(**__lowercase   )

            __lowercase      		=patch_size
            __lowercase      		=num_channels
            __lowercase      		=embed_dim
            __lowercase      		=depths
            __lowercase      		=len(__lowercase   )
            __lowercase      		=num_heads
            __lowercase      		=kernel_size
            __lowercase      		=dilations
            __lowercase      		=mlp_ratio
            __lowercase      		=qkv_bias
            __lowercase      		=hidden_dropout_prob
            __lowercase      		=attention_probs_dropout_prob
            __lowercase      		=drop_path_rate
            __lowercase      		=hidden_act
            __lowercase      		=layer_norm_eps
            __lowercase      		=initializer_range
            # we set the hidden_size attribute in order to make Dinat work with VisionEncoderDecoderModel
            # this indicates the channel dimension after the last stage of the model
            __lowercase      		=int(embed_dim * 2 ** (len(__lowercase   ) - 1)   )
            __lowercase      		=layer_scale_init_value
            __lowercase      		=['stem'] + [f'''stage{idx}''' for idx in range(1    ,   len(__lowercase   ) + 1   )]
            __lowercase		,  __lowercase      		=get_aligned_output_features_output_indices(
                out_features=__lowercase    ,   out_indices=__lowercase    ,   stage_names=self.stage_names   )


