




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 						SCREAMING_SNAKE_CASE__ (   lowercase						,lowercase						,lowercase=1E-12					)  ->       Union[str, Any]:
   snake_case				:    Tuple           =	jnp.divide(emb_a.T						,jnp.clip(jnp.linalg.norm(lowercase						,axis=1					)						,a_min=lowercase					)					).T
   snake_case				:    Any           =	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							):




      """simple docstring"""





      _snake_case												=						42
      _snake_case												=						jnp.floataa

      def      UpperCAmelCase				(				self     )		->    Optional[int]:
         snake_case				:    Any           =	FlaxCLIPVisionModule(self.config.vision_config     )
         snake_case				:    int           =	nn.Dense(self.config.projection_dim    ,				use_bias=A    ,				dtype=self.dtype     )

         snake_case				:    str           =	self.param("""concept_embeds"""    ,				jax.nn.initializers.ones    ,				(1_7, self.config.projection_dim)     )
         snake_case				:    List[str]           =	self.param(
             """special_care_embeds"""    ,				jax.nn.initializers.ones    ,				(3, self.config.projection_dim)     )

         snake_case				:    List[str]           =	self.param("""concept_embeds_weights"""    ,				jax.nn.initializers.ones    ,				(1_7,)     )
         snake_case				:    List[Any]           =	self.param("""special_care_embeds_weights"""    ,				jax.nn.initializers.ones    ,				(3,)     )

      def __call__(				self    ,				A     )		->    Optional[int]:
         snake_case				:    Optional[Any]           =	self.vision_model(A     )[1]
         snake_case				:    Optional[int]           =	self.visual_projection(A     )

         snake_case				:    Optional[int]           =	jax_cosine_distance(A    ,				self.special_care_embeds     )
         snake_case				:    Dict           =	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
         snake_case				:    Optional[Any]           =	0.0

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

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

         return has_nsfw_concepts




class 						__lowercase		(UpperCamelCase__							):




      """simple docstring"""





      _snake_case												=						CLIPConfig
      _snake_case												=						"""clip_input"""
      _snake_case												=						FlaxStableDiffusionSafetyCheckerModule

      def __init__(				self    ,				A    ,				A = None    ,				A = 0    ,				A = jnp.floataa    ,				A = True    ,				**A    ,				)		->    List[Any]:
         if input_shape is None:
            snake_case				:    Dict           =	(1, 2_2_4, 2_2_4, 3)
         snake_case				:    Optional[Any]           =	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    ,				A    ,				A    ,				A = None     )		->    FrozenDict:
         # init input tensor
         snake_case				:    int           =	jax.random.normal(A    ,				A     )

         snake_case     , snake_case				:    Dict           =	jax.random.split(A     )
         snake_case				:    str           =	{"""params""": params_rng, """dropout""": dropout_rng}

         snake_case				:    List[str]           =	self.module.init(A    ,				A     )["""params"""]

         return random_params

      def __call__(				self    ,				A    ,				A = None    ,				)		->    Any:
         snake_case				:    Tuple           =	jnp.transpose(A    ,				(0, 2, 3, 1)     )

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











# using dfs for finding eulerian path traversal




def 						SCREAMING_SNAKE_CASE__ (   lowercase						,lowercase						,lowercase						,lowercase=None					)  ->       Any:
   snake_case				:    Union[str, Any]           =	(path or []) + [u]
   for v in graph[u]:
      if visited_edge[u][v] is False:
         snake_case     , snake_case				:    int           =	True, True
         snake_case				:    List[Any]           =	dfs(lowercase						,lowercase						,lowercase						,lowercase					)
   return path




def 						SCREAMING_SNAKE_CASE__ (   lowercase						,lowercase					)  ->       Tuple:
   snake_case				:    Union[str, Any]           =	0
   snake_case				:    Union[str, Any]           =	-1
   for i in range(lowercase					):
      if i not in graph.keys():
         continue
      if len(graph[i]					) % 2 == 1:
         odd_degree_nodes += 1
         snake_case				:    str           =	i
   if odd_degree_nodes == 0:
      return 1, odd_node
   if odd_degree_nodes == 2:
      return 2, odd_node
   return 3, odd_node




def 						SCREAMING_SNAKE_CASE__ (   lowercase						,lowercase					)  ->       List[str]:
   snake_case				:    Tuple           =	[[False for _ in range(max_node + 1					)] for _ in range(max_node + 1					)]
   snake_case     , snake_case				:    Any           =	check_circuit_or_path(lowercase						,lowercase					)
   if check == 3:
      print("""graph is not Eulerian"""					)
      print("""no path"""					)
      return
   snake_case				:    str           =	1
   if check == 2:
      snake_case				:    Optional[int]           =	odd_node
      print("""graph has a Euler path"""					)
   if check == 1:
      print("""graph has a Euler cycle"""					)
   snake_case				:    Dict           =	dfs(lowercase						,lowercase						,lowercase					)
   print(lowercase					)




def 						SCREAMING_SNAKE_CASE__ (   )  ->       Optional[Any]:
   snake_case				:    Union[str, Any]           =	{1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [4]}
   snake_case				:    Optional[int]           =	{1: [2, 3, 4, 5], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [1, 4]}
   snake_case				:    Optional[int]           =	{1: [2, 3, 4], 2: [1, 3, 4], 3: [1, 2], 4: [1, 2, 5], 5: [4]}
   snake_case				:    List[Any]           =	{1: [2, 3], 2: [1, 3], 3: [1, 2]}
   snake_case				:    Tuple           =	{
       1: [],
       2: []
       # all degree is zero
   }
   snake_case				:    Tuple           =	10
   check_euler(lowercase						,lowercase					)
   check_euler(lowercase						,lowercase					)
   check_euler(lowercase						,lowercase					)
   check_euler(lowercase						,lowercase					)
   check_euler(lowercase						,lowercase					)


if __name__ == "__main__":
       main()





