


'''simple docstring'''

from __future__ import annotations

from collections.abc import Iterator
from typing import Any
class     __lowerCAmelCase		:





      '''simple docstring'''






      def __init__(self       :       List[Any]  ,		UpperCamelCase       :       Any					):

             '''simple docstring'''







             lowercase__      =					data
             lowercase__      =					None






class     __lowerCAmelCase		:





      '''simple docstring'''






      def __init__(self       :       Dict					):

             '''simple docstring'''







             lowercase__      =					None
             lowercase__      =					None



      def __iter__(self       :       Optional[int]					):

             '''simple docstring'''







             lowercase__      =					self.head
             while self.head:
                    yield node.data
                    lowercase__      =					node.next
                    if node == self.head:
                           break



      def __len__(self       :       Any					):

             '''simple docstring'''







             return sum(1 for _ in self					)



      def __repr__(self       :       Optional[Any]					):

             '''simple docstring'''







             return "->".join(str(UpperCamelCase					) for item in iter(self					)					)



      def  UpperCamelCase__						(self       :       List[str]  ,		UpperCamelCase       :       Any					):

             '''simple docstring'''







             self.insert_nth(len(self					)  ,		UpperCamelCase					)



      def  UpperCamelCase__						(self       :       int  ,		UpperCamelCase       :       Any					):

             '''simple docstring'''







             self.insert_nth(0  ,		UpperCamelCase					)



      def  UpperCamelCase__						(self       :       List[str]  ,		UpperCamelCase       :       int  ,		UpperCamelCase       :       Any					):

             '''simple docstring'''







             if index < 0 or index > len(self					):
                    raise IndexError('''list index out of range.'''					)
             lowercase__      =					Node(UpperCamelCase					)
             if self.head is None:
                    lowercase__      =					new_node  # first node points itself
                    lowercase__      =					lowercase__      =					new_node
             elif index == 0:  # insert at head
                    lowercase__      =					self.head
                    lowercase__      =					lowercase__      =					new_node
             else:
                    lowercase__      =					self.head
                    for _ in range(index - 1					):
                           lowercase__      =					temp.next
                    lowercase__      =					temp.next
                    lowercase__      =					new_node
                    if index == len(self					) - 1:  # insert at tail
                           lowercase__      =					new_node



      def  UpperCamelCase__						(self       :       Dict					):

             '''simple docstring'''







             return self.delete_nth(0					)



      def  UpperCamelCase__						(self       :       List[str]					):

             '''simple docstring'''







             return self.delete_nth(len(self					) - 1					)



      def  UpperCamelCase__						(self       :       str  ,		UpperCamelCase       :       int = 0					):

             '''simple docstring'''







             if not 0 <= index < len(self					):
                    raise IndexError('''list index out of range.'''					)
             lowercase__      =					self.head
             if self.head == self.tail:  # just one node
                    lowercase__      =					lowercase__      =					None
             elif index == 0:  # delete head node
                    lowercase__      =					self.tail.next.next
                    lowercase__      =					self.head.next
             else:
                    lowercase__      =					self.head
                    for _ in range(index - 1					):
                           lowercase__      =					temp.next
                    lowercase__      =					temp.next
                    lowercase__      =					temp.next.next
                    if index == len(self					) - 1:  # delete at tail
                           lowercase__      =					temp
             return delete_node.data






      def  UpperCamelCase__						(self       :       Union[str, Any]					):

             '''simple docstring'''







             return len(self					) == 0






def _SCREAMING_SNAKE_CASE ()			->			None:






       """simple docstring"""







       lowercase__      =					CircularLinkedList()
       assert len(A			) == 0
       assert circular_linked_list.is_empty() is True
       assert str(A			) == ""

       try:
              circular_linked_list.delete_front()
              raise AssertionError  # This should not happen
       except IndexError:
              assert True  # This should happen

       try:
              circular_linked_list.delete_tail()
              raise AssertionError  # This should not happen
       except IndexError:
              assert True  # This should happen

       try:
              circular_linked_list.delete_nth(-1			)
              raise AssertionError
       except IndexError:
              assert True

       try:
              circular_linked_list.delete_nth(0			)
              raise AssertionError
       except IndexError:
              assert True

       assert circular_linked_list.is_empty() is True
       for i in range(5			):
              assert len(A			) == i
              circular_linked_list.insert_nth(A    ,					i + 1			)
       assert str(A			) == "->".join(str(A			) for i in range(1    ,					6			)			)

       circular_linked_list.insert_tail(6			)
       assert str(A			) == "->".join(str(A			) for i in range(1    ,					7			)			)
       circular_linked_list.insert_head(0			)
       assert str(A			) == "->".join(str(A			) for i in range(0    ,					7			)			)

       assert circular_linked_list.delete_front() == 0
       assert circular_linked_list.delete_tail() == 6
       assert str(A			) == "->".join(str(A			) for i in range(1    ,					6			)			)
       assert circular_linked_list.delete_nth(2			) == 3

       circular_linked_list.insert_nth(2    ,					3			)
       assert str(A			) == "->".join(str(A			) for i in range(1    ,					6			)			)

       assert circular_linked_list.is_empty() is False


if __name__ == "__main__":
 import doctest

 doctest.testmod()








'''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 _SCREAMING_SNAKE_CASE (A    ,					A    ,					A=1E-12			)			->			str:






       """simple docstring"""







       lowercase__      =					jnp.divide(emb_a.T    ,					jnp.clip(jnp.linalg.norm(A    ,					axis=1			)    ,					a_min=A			)			).T
       lowercase__      =					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'''






      lowerCAmelCase__   :	CLIPConfig
      lowerCAmelCase__   :	jnp.dtype       =				jnp.floataa



      def  UpperCamelCase__						(self       :       Union[str, Any]					):

             '''simple docstring'''







             lowercase__      =					FlaxCLIPVisionModule(self.config.vision_config					)
             lowercase__      =					nn.Dense(self.config.projection_dim  ,		use_bias=UpperCamelCase  ,		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       :       Union[str, Any]  ,		UpperCamelCase       :       Tuple					):

             '''simple docstring'''







             lowercase__      =					self.vision_model(UpperCamelCase					)[1]
             lowercase__      =					self.visual_projection(UpperCamelCase					)

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

             return has_nsfw_concepts






class     __lowerCAmelCase		(lowercase_	):





      '''simple docstring'''






      lowerCAmelCase__   :	Union[str, Any]       =				CLIPConfig
      lowerCAmelCase__   :	Optional[int]       =				"""clip_input"""
      lowerCAmelCase__   :	Any       =				FlaxStableDiffusionSafetyCheckerModule



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

             '''simple docstring'''







             if input_shape is None:
                    lowercase__      =					(1, 224, 224, 3)
             lowercase__      =					self.module_class(config=UpperCamelCase  ,		dtype=UpperCamelCase  ,		**UpperCamelCase					)
             super().__init__(UpperCamelCase  ,		UpperCamelCase  ,		input_shape=UpperCamelCase  ,		seed=UpperCamelCase  ,		dtype=UpperCamelCase  ,		_do_init=_do_init					)



      def  UpperCamelCase__						(self       :       Any  ,		UpperCamelCase       :       jax.random.KeyArray  ,		UpperCamelCase       :       Tuple  ,		UpperCamelCase       :       FrozenDict = None					):

             '''simple docstring'''







             lowercase__      =					jax.random.normal(UpperCamelCase  ,		UpperCamelCase					)

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

             lowercase__      =					self.module.init(UpperCamelCase  ,		UpperCamelCase					)['''params''']

             return random_params






      def __call__(self       :       Optional[Any]  ,		UpperCamelCase       :       Tuple  ,		UpperCamelCase       :       dict = None  ,		):

             '''simple docstring'''







             lowercase__      =					jnp.transpose(UpperCamelCase  ,		(0, 2, 3, 1)					)

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




