





"""simple docstring"""





import torch
import torch.nn as nn
from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel

from ...utils import logging


SCREAMING_SNAKE_CASE__					=   logging.get_logger(__name__)


def 			UpperCAmelCase__							(  SCREAMING_SNAKE_CASE     :						Union[str, Any]							,						SCREAMING_SNAKE_CASE     :						List[Any]			):







      '''simple docstring'''




      lowerCAmelCase  						=       nn.functional.normalize(SCREAMING_SNAKE_CASE			)
      lowerCAmelCase  						=       nn.functional.normalize(SCREAMING_SNAKE_CASE			)
      return torch.mm(SCREAMING_SNAKE_CASE							,						normalized_text_embeds.t()			)






class 		lowercase					(					_UpperCAmelCase					):
      _SCREAMING_SNAKE_CASE					=      CLIPConfig

      _SCREAMING_SNAKE_CASE					=      ['CLIPEncoderLayer']





      def __init__(       self      ,						lowercase )		->     Optional[int]:
            super().__init__(lowercase )

            lowerCAmelCase  						=       CLIPVisionModel(config.vision_config )
            lowerCAmelCase  						=       nn.Linear(config.vision_config.hidden_size      ,						config.projection_dim      ,						bias=lowercase )

            lowerCAmelCase  						=       nn.Parameter(torch.ones(17      ,						config.projection_dim )      ,						requires_grad=lowercase )
            lowerCAmelCase  						=       nn.Parameter(torch.ones(3      ,						config.projection_dim )      ,						requires_grad=lowercase )

            lowerCAmelCase  						=       nn.Parameter(torch.ones(17 )      ,						requires_grad=lowercase )
            lowerCAmelCase  						=       nn.Parameter(torch.ones(3 )      ,						requires_grad=lowercase )





      @torch.no_grad()
      def     _snake_case			(       self      ,						lowercase      ,						lowercase )		->     Optional[Any]:
            lowerCAmelCase  						=       self.vision_model(lowercase )[1]  # pooled_output
            lowerCAmelCase  						=       self.visual_projection(lowercase )

            # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
            lowerCAmelCase  						=       cosine_distance(lowercase      ,						self.special_care_embeds ).cpu().float().numpy()
            lowerCAmelCase  						=       cosine_distance(lowercase      ,						self.concept_embeds ).cpu().float().numpy()

            lowerCAmelCase  						=       []
            lowerCAmelCase  						=       image_embeds.shape[0]
            for i in range(lowercase ):
                  lowerCAmelCase  						=       {"""special_scores""": {}, """special_care""": [], """concept_scores""": {}, """bad_concepts""": []}

                  # increase this value to create a stronger `nfsw` filter
                  # at the cost of increasing the possibility of filtering benign images
                  lowerCAmelCase  						=       0.0

                  for concept_idx in range(len(special_cos_dist[0] ) ):
                        lowerCAmelCase  						=       special_cos_dist[i][concept_idx]
                        lowerCAmelCase  						=       self.special_care_embeds_weights[concept_idx].item()
                        lowerCAmelCase  						=       round(concept_cos - concept_threshold + adjustment      ,						3 )
                        if result_img["special_scores"][concept_idx] > 0:
                              result_img["special_care"].append({concept_idx, result_img["""special_scores"""][concept_idx]} )
                              lowerCAmelCase  						=       0.01

                  for concept_idx in range(len(cos_dist[0] ) ):
                        lowerCAmelCase  						=       cos_dist[i][concept_idx]
                        lowerCAmelCase  						=       self.concept_embeds_weights[concept_idx].item()
                        lowerCAmelCase  						=       round(concept_cos - concept_threshold + adjustment      ,						3 )
                        if result_img["concept_scores"][concept_idx] > 0:
                              result_img["bad_concepts"].append(lowercase )

                  result.append(lowercase )

            lowerCAmelCase  						=       [len(res["""bad_concepts"""] ) > 0 for res in result]

            return images, has_nsfw_concepts





      @torch.no_grad()
      def     _snake_case			(       self      ,						lowercase      ,						lowercase )		->     Union[str, Any]:
            lowerCAmelCase  						=       self.vision_model(lowercase )[1]  # pooled_output
            lowerCAmelCase  						=       self.visual_projection(lowercase )

            lowerCAmelCase  						=       cosine_distance(lowercase      ,						self.special_care_embeds )
            lowerCAmelCase  						=       cosine_distance(lowercase      ,						self.concept_embeds )

            # increase this value to create a stronger `nsfw` filter
            # at the cost of increasing the possibility of filtering benign images
            lowerCAmelCase  						=       0.0

            lowerCAmelCase  						=       special_cos_dist - self.special_care_embeds_weights + adjustment
            # special_scores = special_scores.round(decimals=3)
            lowerCAmelCase  						=       torch.any(special_scores > 0      ,						dim=1 )
            lowerCAmelCase  						=       special_care * 0.01
            lowerCAmelCase  						=       special_adjustment.unsqueeze(1 ).expand(-1      ,						cos_dist.shape[1] )

            lowerCAmelCase  						=       (cos_dist - self.concept_embeds_weights) + special_adjustment
            # concept_scores = concept_scores.round(decimals=3)
            lowerCAmelCase  						=       torch.any(concept_scores > 0      ,						dim=1 )

            return images, has_nsfw_concepts









'''simple docstring'''
from typing import List, Optional, Union

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


lowerCamelCase :      List[Any]					 =     logging.get_logger(__name__)

lowerCamelCase :      str					 =     {
    "huggingface/time-series-transformer-tourism-monthly": (
        "https://huggingface.co/huggingface/time-series-transformer-tourism-monthly/resolve/main/config.json"
    ),
    # See all TimeSeriesTransformer models at https://huggingface.co/models?filter=time_series_transformer
}







class 							A__    (       A__  ):
  A__		 =				'time_series_transformer'
  A__		 =				{
      'hidden_size': 'd_model',
      'num_attention_heads': 'encoder_attention_heads',
      'num_hidden_layers': 'encoder_layers',
  }





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





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =prediction_length
       _SCREAMING_SNAKE_CASE							    =context_length or prediction_length
       _SCREAMING_SNAKE_CASE							    =distribution_output
       _SCREAMING_SNAKE_CASE							    =loss
       _SCREAMING_SNAKE_CASE							    =input_size
       _SCREAMING_SNAKE_CASE							    =num_time_features
       _SCREAMING_SNAKE_CASE							    =lags_sequence
       _SCREAMING_SNAKE_CASE							    =scaling
       _SCREAMING_SNAKE_CASE							    =num_dynamic_real_features
       _SCREAMING_SNAKE_CASE							    =num_static_real_features
       _SCREAMING_SNAKE_CASE							    =num_static_categorical_features
       if cardinality and num_static_categorical_features > 0:
            if len(_a ) != num_static_categorical_features:
                 raise ValueError(
                     'The cardinality should be a list of the same length as `num_static_categorical_features`' )
            _SCREAMING_SNAKE_CASE							    =cardinality
       else:
            _SCREAMING_SNAKE_CASE							    =[0]
       if embedding_dimension and num_static_categorical_features > 0:
            if len(_a ) != num_static_categorical_features:
                 raise ValueError(
                     'The embedding dimension should be a list of the same length as `num_static_categorical_features`' )
            _SCREAMING_SNAKE_CASE							    =embedding_dimension
       else:
            _SCREAMING_SNAKE_CASE							    =[min(50							,					(cat + 1) // 2 ) for cat in self.cardinality]
       _SCREAMING_SNAKE_CASE							    =num_parallel_samples

       # Transformer architecture configuration
       _SCREAMING_SNAKE_CASE							    =input_size * len(_a ) + self._number_of_features
       _SCREAMING_SNAKE_CASE							    =d_model
       _SCREAMING_SNAKE_CASE							    =encoder_attention_heads
       _SCREAMING_SNAKE_CASE							    =decoder_attention_heads
       _SCREAMING_SNAKE_CASE							    =encoder_ffn_dim
       _SCREAMING_SNAKE_CASE							    =decoder_ffn_dim
       _SCREAMING_SNAKE_CASE							    =encoder_layers
       _SCREAMING_SNAKE_CASE							    =decoder_layers

       _SCREAMING_SNAKE_CASE							    =dropout
       _SCREAMING_SNAKE_CASE							    =attention_dropout
       _SCREAMING_SNAKE_CASE							    =activation_dropout
       _SCREAMING_SNAKE_CASE							    =encoder_layerdrop
       _SCREAMING_SNAKE_CASE							    =decoder_layerdrop

       _SCREAMING_SNAKE_CASE							    =activation_function
       _SCREAMING_SNAKE_CASE							    =init_std

       _SCREAMING_SNAKE_CASE							    =use_cache

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





  @property
  def 						A  (		self      :	List[Any] )	->    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
       )
