






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 		lowercase__  (				_UpperCamelCase       ,							_UpperCamelCase       ,							_UpperCamelCase=1e-12) ->			Tuple:
      """simple docstring"""

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





class 					A__     (     nn.Module ):

     '''simple docstring'''






     snake_case__					=					42
     snake_case__					=					jnp.floataa







     def   _SCREAMING_SNAKE_CASE     ( self      :    List[str] ):






           """simple docstring"""




           UpperCamelCase					       =  FlaxCLIPVisionModule(self.config.vision_config )
           UpperCamelCase					       =  nn.Dense(self.config.projection_dim   ,						use_bias=_SCREAMING_SNAKE_CASE   ,						dtype=self.dtype )

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

           UpperCamelCase					       =  self.param('concept_embeds_weights'   ,						jax.nn.initializers.ones   ,						(17,) )
           UpperCamelCase					       =  self.param('special_care_embeds_weights'   ,						jax.nn.initializers.ones   ,						(3,) )







     def __call__( self      :    Optional[Any]   ,						_SCREAMING_SNAKE_CASE      :    int ):






           """simple docstring"""




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

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

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

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

           return has_nsfw_concepts





class 					A__     (     __snake_case ):

     '''simple docstring'''






     snake_case__					=					CLIPConfig
     snake_case__					=					"""clip_input"""
     snake_case__					=					FlaxStableDiffusionSafetyCheckerModule







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






           """simple docstring"""




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







     def   _SCREAMING_SNAKE_CASE     ( self      :    str   ,						_SCREAMING_SNAKE_CASE      :    jax.random.KeyArray   ,						_SCREAMING_SNAKE_CASE      :    Tuple   ,						_SCREAMING_SNAKE_CASE      :    FrozenDict = None ):






           """simple docstring"""




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

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

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

           return random_params







     def __call__( self      :    Dict   ,						_SCREAMING_SNAKE_CASE      :    List[str]   ,						_SCREAMING_SNAKE_CASE      :    dict = None   ,						):






           """simple docstring"""




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

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











from __future__ import annotations

from collections.abc import MutableSequence





class 					A__     :

     '''simple docstring'''






     def __init__( self      :    Dict   ,						_SCREAMING_SNAKE_CASE      :    int   ,						_SCREAMING_SNAKE_CASE      :    MutableSequence[float] ):






           """simple docstring"""




           if len(_SCREAMING_SNAKE_CASE ) != degree + 1:
                 raise ValueError(
                     'The number of coefficients should be equal to the degree + 1.' )

           UpperCamelCase					       =  list(_SCREAMING_SNAKE_CASE )
           UpperCamelCase					       =  degree







     def __add__( self      :    Any   ,						_SCREAMING_SNAKE_CASE      :    Polynomial ):






           """simple docstring"""




           if self.degree > polynomial_a.degree:
                 UpperCamelCase					       =  self.coefficients[:]
                 for i in range(polynomial_a.degree + 1 ):
                       coefficients[i] += polynomial_a.coefficients[i]
                 return Polynomial(self.degree   ,						_SCREAMING_SNAKE_CASE )
           else:
                 UpperCamelCase					       =  polynomial_a.coefficients[:]
                 for i in range(self.degree + 1 ):
                       coefficients[i] += self.coefficients[i]
                 return Polynomial(polynomial_a.degree   ,						_SCREAMING_SNAKE_CASE )







     def __sub__( self      :    Optional[int]   ,						_SCREAMING_SNAKE_CASE      :    Polynomial ):






           """simple docstring"""




           return self + polynomial_a * Polynomial(0   ,						[-1] )







     def __neg__( self      :    Any ):






           """simple docstring"""




           return Polynomial(self.degree   ,						[-c for c in self.coefficients] )







     def __mul__( self      :    Union[str, Any]   ,						_SCREAMING_SNAKE_CASE      :    Polynomial ):






           """simple docstring"""




           UpperCamelCase					       =  [0] * (self.degree + polynomial_a.degree + 1)
           for i in range(self.degree + 1 ):
                 for j in range(polynomial_a.degree + 1 ):
                       coefficients[i + j] += (
                           self.coefficients[i] * polynomial_a.coefficients[j]
                       )

           return Polynomial(self.degree + polynomial_a.degree   ,						_SCREAMING_SNAKE_CASE )







     def   _SCREAMING_SNAKE_CASE     ( self      :    Optional[int]   ,						_SCREAMING_SNAKE_CASE      :    int | float ):






           """simple docstring"""




           UpperCamelCase					       =  0
           for i in range(self.degree + 1 ):
                 result += self.coefficients[i] * (substitution**i)
           return result







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






           """simple docstring"""




           UpperCamelCase					       =  ''
           for i in range(self.degree   ,						-1   ,						-1 ):
                 if self.coefficients[i] == 0:
                       continue
                 elif self.coefficients[i] > 0:
                       if polynomial:
                             polynomial += " + "
                 else:
                       polynomial += " - "

                 if i == 0:
                       polynomial += str(abs(self.coefficients[i] ) )
                 elif i == 1:
                       polynomial += str(abs(self.coefficients[i] ) ) + "x"
                 else:
                       polynomial += str(abs(self.coefficients[i] ) ) + "x^" + str(_SCREAMING_SNAKE_CASE )

           return polynomial







     def __repr__( self      :    str ):






           """simple docstring"""




           return self.__str__()







     def   _SCREAMING_SNAKE_CASE     ( self      :    Optional[Any] ):






           """simple docstring"""




           UpperCamelCase					       =  [0] * self.degree
           for i in range(self.degree ):
                 UpperCamelCase					       =  self.coefficients[i + 1] * (i + 1)
           return Polynomial(self.degree - 1   ,						_SCREAMING_SNAKE_CASE )







     def   _SCREAMING_SNAKE_CASE     ( self      :    Optional[int]   ,						_SCREAMING_SNAKE_CASE      :    int | float = 0 ):






           """simple docstring"""




           UpperCamelCase					       =  [0] * (self.degree + 2)
           UpperCamelCase					       =  constant
           for i in range(self.degree + 1 ):
                 UpperCamelCase					       =  self.coefficients[i] / (i + 1)
           return Polynomial(self.degree + 1   ,						_SCREAMING_SNAKE_CASE )







     def __eq__( self      :    Any   ,						_SCREAMING_SNAKE_CASE      :    object ):






           """simple docstring"""




           if not isinstance(_SCREAMING_SNAKE_CASE   ,						_SCREAMING_SNAKE_CASE ):
                 return False

           if self.degree != polynomial_a.degree:
                 return False

           for i in range(self.degree + 1 ):
                 if self.coefficients[i] != polynomial_a.coefficients[i]:
                       return False

           return True







     def __ne__( self      :    str   ,						_SCREAMING_SNAKE_CASE      :    object ):






           """simple docstring"""




           return not self.__eq__(_SCREAMING_SNAKE_CASE )



