



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						(       _A			:				List[Any]			,							_A			:				List[Any]			,							_A			:				int=1E-12    )->       List[Any]:





      """simple docstring"""





      A__							=       jnp.divide(emb_a.T			,							jnp.clip(jnp.linalg.norm(lowerCAmelCase__			,							axis=1    )			,							a_min=lowerCAmelCase__    )    ).T
      A__							=       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      UpperCamelCase    (						nn.Module     ):
 lowerCAmelCase	:   CLIPConfig
 lowerCAmelCase	:   jnp.dtype				=	jnp.floataa



 def 			__A							(       self		):
       A__							=       FlaxCLIPVisionModule(self.config.vision_config		)
       A__							=       nn.Dense(self.config.projection_dim			,	use_bias=A__			,	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			,	UpperCAmelCase__		):
       A__							=       self.vision_model(A__		)[1]
       A__							=       self.visual_projection(A__		)

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

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

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

       return has_nsfw_concepts






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



 def __init__(       self			,	UpperCAmelCase__			,	UpperCAmelCase__ = None			,	UpperCAmelCase__ = 0			,	UpperCAmelCase__ = jnp.floataa			,	UpperCAmelCase__ = True			,	**UpperCAmelCase__			,	):
       if input_shape is None:
             A__							=       (1, 224, 224, 3)
       A__							=       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			,	UpperCAmelCase__			,	UpperCAmelCase__			,	UpperCAmelCase__ = None		):
       # init input tensor
       A__							=       jax.random.normal(A__			,	A__		)

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

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

       return random_params



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

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










from __future__ import annotations






class      UpperCamelCase    :



	def __init__(       self			,	UpperCAmelCase__=None		):
							A__							=       data
							A__							=       None



	def __repr__(       self		):
							A__							=       []
							A__							=       self
							while temp:
													string_rep.append(F"""{temp.data}"""		)
													A__							=       temp.next
							return "->".join(UpperCAmelCase__		)





def  UpperCamelCase						(       _A			:				list    )->       Dict:





						"""simple docstring"""





						if not elements_list:
												raise Exception("The Elements List is empty"    )

						A__							=       A__							=       Node(elements_list[0]    )
						for i in range(1			,							len(_A    )    ):
												A__							=       Node(elements_list[i]    )
												A__							=       current.next
						return head
def  UpperCamelCase						(       _A			:				Node    )->       None:





						"""simple docstring"""





						if head_node is not None and isinstance(_A			,							_A    ):
												print_reverse(head_node.next    )
												print(head_node.data    )
def  UpperCamelCase						(       )->       Tuple:





						"""simple docstring"""





						from doctest import testmod

						testmod()

						A__							=       make_linked_list([14, 52, 14, 12, 43]    )
						print("Linked List:"    )
						print(_A    )
						print("Elements in Reverse:"    )
						print_reverse(_A    )


if __name__ == "__main__":
						main()





