




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__     (   lowerCamelCase,	lowerCamelCase,	lowerCamelCase=1e-12      ):
  lowercase			:Any							=  jnp.divide(emb_a.T,	jnp.clip(jnp.linalg.norm(lowerCamelCase,	axis=1      ),	a_min=lowerCamelCase      )      ).T
  lowercase			:Tuple							=  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       __lowerCAmelCase				(							nn.Module):
       _a           =  42
       _a           =  jnp.floataa






       def     SCREAMING_SNAKE_CASE			(      self:    Union[str, Any] ):
         lowercase			:Dict							=  FlaxCLIPVisionModule(self.config.vision_config )
         lowercase			:List[Any]							=  nn.Dense(self.config.projection_dim				,    use_bias=_lowerCAmelCase				,    dtype=self.dtype )

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

         lowercase			:int							=  self.param("concept_embeds_weights"				,    jax.nn.initializers.ones				,    (17,) )
         lowercase			:Optional[Any]							=  self.param("special_care_embeds_weights"				,    jax.nn.initializers.ones				,    (3,) )






       def __call__(      self:    Optional[Any]				,    _lowerCAmelCase:    Optional[Any] ):
         lowercase			:List[str]							=  self.vision_model(_lowerCAmelCase )[1]
         lowercase			:Dict							=  self.visual_projection(_lowerCAmelCase )

         lowercase			:Optional[Any]							=  jax_cosine_distance(_lowerCAmelCase				,    self.special_care_embeds )
         lowercase			:int							=  jax_cosine_distance(_lowerCAmelCase				,    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			:List[Any]							=  0.0

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

         lowercase			:Union[str, Any]							=  cos_dist - self.concept_embeds_weights[None, :] + special_adjustment
         lowercase			:Any							=  jnp.round(_lowerCAmelCase				,    3 )
         lowercase			:List[str]							=  jnp.any(concept_scores > 0				,    axis=1 )

         return has_nsfw_concepts







class       __lowerCAmelCase				(							lowerCAmelCase):
       _a           =  CLIPConfig
       _a           =  '''clip_input'''
       _a           =  FlaxStableDiffusionSafetyCheckerModule






       def __init__(      self:    Tuple				,    _lowerCAmelCase:    CLIPConfig				,    _lowerCAmelCase:    Optional[Tuple] = None				,    _lowerCAmelCase:    int = 0				,    _lowerCAmelCase:    jnp.dtype = jnp.floataa				,    _lowerCAmelCase:    bool = True				,    **_lowerCAmelCase:    Tuple				,    ):
         if input_shape is None:
           lowercase			:Union[str, Any]							=  (1, 2_24, 2_24, 3)
         lowercase			:Tuple							=  self.module_class(config=_lowerCAmelCase				,    dtype=_lowerCAmelCase				,    **_lowerCAmelCase )
         super().__init__(_lowerCAmelCase				,    _lowerCAmelCase				,    input_shape=_lowerCAmelCase				,    seed=_lowerCAmelCase				,    dtype=_lowerCAmelCase				,    _do_init=_do_init )






       def     SCREAMING_SNAKE_CASE			(      self:    Optional[Any]				,    _lowerCAmelCase:    jax.random.KeyArray				,    _lowerCAmelCase:    Tuple				,    _lowerCAmelCase:    FrozenDict = None ):
         # init input tensor
         lowercase			:Tuple							=  jax.random.normal(_lowerCAmelCase				,    _lowerCAmelCase )

         lowercase		,			lowercase			:Union[str, Any]							=  jax.random.split(_lowerCAmelCase )
         lowercase			:Tuple							=  {"params": params_rng, "dropout": dropout_rng}

         lowercase			:List[str]							=  self.module.init(_lowerCAmelCase				,    _lowerCAmelCase )["params"]

         return random_params






       def __call__(      self:    Dict				,    _lowerCAmelCase:    int				,    _lowerCAmelCase:    dict = None				,    ):
         lowercase			:Optional[int]							=  jnp.transpose(_lowerCAmelCase				,    (0, 2, 3, 1) )

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









import os
from math import logaa



def 					UpperCAmelCase__     (   lowerCamelCase = "base_exp.txt"      ):
  lowercase			:float							=  0
  lowercase			:str							=  0
  for i, line in enumerate(open(os.path.join(os.path.dirname(lowerCamelCase      ),	lowerCamelCase      )      )      ):
    lowercase		,			lowercase			:str							=  list(map(lowerCamelCase,	line.split(","      )      )      )
    if x * logaa(lowerCamelCase      ) > largest:
      lowercase			:Optional[Any]							=  x * logaa(lowerCamelCase      )
      lowercase			:Any							=  i + 1
  return result


if __name__ == "__main__":
     print(solution())



