





'''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 							UpperCAmelCase_  (						__lowerCamelCase					:					str	,__lowerCamelCase					:					List[Any]	,__lowerCamelCase					:					Union[str, Any]=1e-12 ):
   lowercase_			:List[str]    			= jnp.divide(emb_a.T	,jnp.clip(jnp.linalg.norm(__lowerCamelCase	,axis=1 )	,a_min=__lowerCamelCase ) ).T
   lowercase_			:List[Any]    			= 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       a_   (				nn.Module							):
     __A      =			42
     __A      =			jnp.floataa





     def 							lowercase__      ( self		:					List[str]						):





        """simple docstring"""




        lowercase_			:int    			= FlaxCLIPVisionModule(self.config.vision_config						)
        lowercase_			:str    			= nn.Dense(self.config.projection_dim	,      use_bias=lowercase	,      dtype=self.dtype						)

        lowercase_			:Optional[Any]    			= self.param("concept_embeds"	,      jax.nn.initializers.ones	,      (17, self.config.projection_dim)						)
        lowercase_			:Tuple    			= self.param(
            "special_care_embeds"	,      jax.nn.initializers.ones	,      (3, self.config.projection_dim)						)

        lowercase_			:Tuple    			= self.param("concept_embeds_weights"	,      jax.nn.initializers.ones	,      (17,)						)
        lowercase_			:str    			= self.param("special_care_embeds_weights"	,      jax.nn.initializers.ones	,      (3,)						)







     def __call__( self		:					Dict	,      lowercase		:					Tuple						):





        """simple docstring"""




        lowercase_			:Optional[Any]    			= self.vision_model(lowercase						)[1]
        lowercase_			:Optional[int]    			= self.visual_projection(lowercase						)

        lowercase_			:Tuple    			= jax_cosine_distance(lowercase	,      self.special_care_embeds						)
        lowercase_			:List[str]    			= jax_cosine_distance(lowercase	,      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_			:Union[str, Any]    			= 0.0

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

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

        return has_nsfw_concepts







class       a_   (				_lowerCAmelCase							):
     __A      =			CLIPConfig
     __A      =			"clip_input"
     __A      =			FlaxStableDiffusionSafetyCheckerModule





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





        """simple docstring"""




        if input_shape is None:
           lowercase_			:Tuple    			= (1, 224, 224, 3)
        lowercase_			:Union[str, Any]    			= self.module_class(config=lowercase	,      dtype=lowercase	,      **lowercase						)
        super().__init__(lowercase	,      lowercase	,      input_shape=lowercase	,      seed=lowercase	,      dtype=lowercase	,      _do_init=_do_init						)





     def 							lowercase__      ( self		:					Optional[int]	,      lowercase		:					jax.random.KeyArray	,      lowercase		:					Tuple	,      lowercase		:					FrozenDict = None						):





        """simple docstring"""




        lowercase_			:Union[str, Any]    			= jax.random.normal(lowercase	,      lowercase						)

        lowercase_			,							lowercase_			:Optional[Any]    			= jax.random.split(lowercase						)
        lowercase_			:List[Any]    			= {"params": params_rng, "dropout": dropout_rng}

        lowercase_			:Dict    			= self.module.init(lowercase	,      lowercase						)["params"]

        return random_params







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





        """simple docstring"""




        lowercase_			:int    			= jnp.transpose(lowercase	,      (0, 2, 3, 1)						)

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









'''simple docstring'''


from typing import List, Optional

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase					:     Optional[int]												=logging.get_logger(__name__)

lowerCAmelCase					:     Tuple												={
    '''huggingface/autoformer-tourism-monthly''': '''https://huggingface.co/huggingface/autoformer-tourism-monthly/resolve/main/config.json''',
}
class       a_   (				_lowerCAmelCase							):
     __A      =			"autoformer"
     __A      =			{
         "hidden_size": "d_model",
         "num_attention_heads": "encoder_attention_heads",
         "num_hidden_layers": "encoder_layers",
     }





     def __init__( self		:					Any	,      lowercase		:					Optional[int] = None	,      lowercase		:					Optional[int] = None	,      lowercase		:					str = "student_t"	,      lowercase		:					str = "nll"	,      lowercase		:					int = 1	,      lowercase		:					List[int] = [1, 2, 3, 4, 5, 6, 7]	,      lowercase		:					bool = True	,      lowercase		:					int = 0	,      lowercase		:					int = 0	,      lowercase		:					int = 0	,      lowercase		:					int = 0	,      lowercase		:					Optional[List[int]] = None	,      lowercase		:					Optional[List[int]] = None	,      lowercase		:					int = 64	,      lowercase		:					int = 2	,      lowercase		:					int = 2	,      lowercase		:					int = 2	,      lowercase		:					int = 2	,      lowercase		:					int = 32	,      lowercase		:					int = 32	,      lowercase		:					str = "gelu"	,      lowercase		:					float = 0.1	,      lowercase		:					float = 0.1	,      lowercase		:					float = 0.1	,      lowercase		:					float = 0.1	,      lowercase		:					float = 0.1	,      lowercase		:					int = 100	,      lowercase		:					float = 0.02	,      lowercase		:					bool = True	,      lowercase		:					int=True	,      lowercase		:					int = 10	,      lowercase		:					int = 25	,      lowercase		:					int = 3	,      **lowercase		:					Dict	,      ):





        """simple docstring"""




        lowercase_			:str    			= prediction_length
        lowercase_			:Dict    			= context_length if context_length is not None else prediction_length
        lowercase_			:Any    			= distribution_output
        lowercase_			:Tuple    			= loss
        lowercase_			:Dict    			= input_size
        lowercase_			:Tuple    			= num_time_features
        lowercase_			:int    			= lags_sequence
        lowercase_			:Tuple    			= scaling
        lowercase_			:List[Any]    			= num_dynamic_real_features
        lowercase_			:Union[str, Any]    			= num_static_real_features
        lowercase_			:str    			= num_static_categorical_features
        if cardinality is not None and num_static_categorical_features > 0:
           if len(lowercase						) != num_static_categorical_features:
              raise ValueError(
                  "The cardinality should be a list of the same length as `num_static_categorical_features`"						)
           lowercase_			:Optional[int]    			= cardinality
        else:
           lowercase_			:Optional[int]    			= [0]
        if embedding_dimension is not None and num_static_categorical_features > 0:
           if len(lowercase						) != num_static_categorical_features:
              raise ValueError(
                  "The embedding dimension should be a list of the same length as `num_static_categorical_features`"						)
           lowercase_			:Tuple    			= embedding_dimension
        else:
           lowercase_			:Dict    			= [min(50	,      (cat + 1) // 2						) for cat in self.cardinality]
        lowercase_			:Any    			= num_parallel_samples

        # Transformer architecture configuration
        lowercase_			:Tuple    			= input_size * len(self.lags_sequence						) + self._number_of_features
        lowercase_			:Union[str, Any]    			= d_model
        lowercase_			:Optional[Any]    			= encoder_attention_heads
        lowercase_			:Optional[Any]    			= decoder_attention_heads
        lowercase_			:Optional[int]    			= encoder_ffn_dim
        lowercase_			:int    			= decoder_ffn_dim
        lowercase_			:Any    			= encoder_layers
        lowercase_			:Optional[Any]    			= decoder_layers

        lowercase_			:Dict    			= dropout
        lowercase_			:Dict    			= attention_dropout
        lowercase_			:str    			= activation_dropout
        lowercase_			:int    			= encoder_layerdrop
        lowercase_			:Dict    			= decoder_layerdrop

        lowercase_			:List[str]    			= activation_function
        lowercase_			:int    			= init_std

        lowercase_			:Optional[int]    			= use_cache

        # Autoformer
        lowercase_			:List[str]    			= label_length
        lowercase_			:List[str]    			= moving_average
        lowercase_			:Optional[int]    			= autocorrelation_factor

        super().__init__(is_encoder_decoder=lowercase	,      **lowercase						)







     @property
     def 							lowercase__      ( self		:					int						):





        """simple docstring"""




        return (
            sum(self.embedding_dimension						)
            + self.num_dynamic_real_features
            + self.num_time_features
            + self.num_static_real_features
            + self.input_size * 2  # the log1p(abs(loc)) and log(scale) features
        )


