



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

from ...utils import logging


UpperCAmelCase_          =				logging.get_logger(__name__)
def 				UpperCAmelCase__		(					_SCREAMING_SNAKE_CASE :						int  ,					_SCREAMING_SNAKE_CASE :						Tuple					)->Union[str, Any]:
      _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 		UpperCAmelCase    (    snake_case_					):
   SCREAMING_SNAKE_CASE__					      =  CLIPConfig

   SCREAMING_SNAKE_CASE__					      =  ['''CLIPEncoderLayer''']





   def __init__(						self							,  _lowerCAmelCase      ):
         super().__init__(_lowerCAmelCase      )

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

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

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





   @torch.no_grad()
   def 						__lowerCAmelCase				(						self							,  _lowerCAmelCase							,  _lowerCAmelCase      ):
         _lowerCAmelCase			      =	self.vision_model(_lowerCAmelCase      )[1]  # pooled_output
         _lowerCAmelCase			      =	self.visual_projection(_lowerCAmelCase      )

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

         _lowerCAmelCase			      =	[]
         _lowerCAmelCase			      =	image_embeds.shape[0]
         for i in range(_lowerCAmelCase      ):
               _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(_lowerCAmelCase      )

               result.append(_lowerCAmelCase      )

         _lowerCAmelCase			      =	[len(res['''bad_concepts''']      ) > 0 for res in result]

         for idx, has_nsfw_concept in enumerate(_lowerCAmelCase      ):
               if has_nsfw_concept:
                     if torch.is_tensor(_lowerCAmelCase      ) or torch.is_tensor(images[0]      ):
                           _lowerCAmelCase			      =	torch.zeros_like(images[idx]      )  # black image
                     else:
                           _lowerCAmelCase			      =	np.zeros(images[idx].shape      )  # black image

         if any(_lowerCAmelCase      ):
               logger.warning(
                   '''Potential NSFW content was detected in one or more images. A black image will be returned instead.'''
                   ''' Try again with a different prompt and/or seed.'''      )

         return images, has_nsfw_concepts





   @torch.no_grad()
   def 						__lowerCAmelCase				(						self							,  _lowerCAmelCase							,  _lowerCAmelCase      ):
         _lowerCAmelCase			      =	self.vision_model(_lowerCAmelCase      )[1]  # pooled_output
         _lowerCAmelCase			      =	self.visual_projection(_lowerCAmelCase      )

         _lowerCAmelCase			      =	cosine_distance(_lowerCAmelCase							,  self.special_care_embeds      )
         _lowerCAmelCase			      =	cosine_distance(_lowerCAmelCase							,  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      )

         _lowerCAmelCase			      =	0.0  # black image

         return images, has_nsfw_concepts




from typing import List, Union

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


UpperCAmelCase_          =				logging.get_logger(__name__)

UpperCAmelCase_          =				{
    "allenai/led-base-16384": "https://huggingface.co/allenai/led-base-16384/resolve/main/config.json",
    # See all LED models at https://huggingface.co/models?filter=led
}





class 		UpperCAmelCase    (    snake_case_					):
   SCREAMING_SNAKE_CASE__					      =  '''led'''
   SCREAMING_SNAKE_CASE__					      =  {
       '''num_attention_heads''': '''encoder_attention_heads''',
       '''hidden_size''': '''d_model''',
       '''attention_probs_dropout_prob''': '''attention_dropout''',
       '''initializer_range''': '''init_std''',
   }





   def __init__(						self							,  _lowerCAmelCase=50_265							,  _lowerCAmelCase=16_384							,  _lowerCAmelCase=1_024							,  _lowerCAmelCase=12							,  _lowerCAmelCase=4_096							,  _lowerCAmelCase=16							,  _lowerCAmelCase=12							,  _lowerCAmelCase=4_096							,  _lowerCAmelCase=16							,  _lowerCAmelCase=0.0							,  _lowerCAmelCase=0.0							,  _lowerCAmelCase=True							,  _lowerCAmelCase=True							,  _lowerCAmelCase="gelu"							,  _lowerCAmelCase=1_024							,  _lowerCAmelCase=0.1							,  _lowerCAmelCase=0.0							,  _lowerCAmelCase=0.0							,  _lowerCAmelCase=0.02							,  _lowerCAmelCase=2							,  _lowerCAmelCase=0.0							,  _lowerCAmelCase=1							,  _lowerCAmelCase=0							,  _lowerCAmelCase=2							,  _lowerCAmelCase = 512							,  **_lowerCAmelCase							,  ):
         _lowerCAmelCase			      =	vocab_size
         _lowerCAmelCase			      =	max_encoder_position_embeddings
         _lowerCAmelCase			      =	max_decoder_position_embeddings
         _lowerCAmelCase			      =	d_model
         _lowerCAmelCase			      =	encoder_ffn_dim
         _lowerCAmelCase			      =	encoder_layers
         _lowerCAmelCase			      =	encoder_attention_heads
         _lowerCAmelCase			      =	decoder_ffn_dim
         _lowerCAmelCase			      =	decoder_layers
         _lowerCAmelCase			      =	decoder_attention_heads
         _lowerCAmelCase			      =	dropout
         _lowerCAmelCase			      =	attention_dropout
         _lowerCAmelCase			      =	activation_dropout
         _lowerCAmelCase			      =	activation_function
         _lowerCAmelCase			      =	init_std
         _lowerCAmelCase			      =	encoder_layerdrop
         _lowerCAmelCase			      =	decoder_layerdrop
         _lowerCAmelCase			      =	classifier_dropout
         _lowerCAmelCase			      =	use_cache
         _lowerCAmelCase			      =	encoder_layers
         _lowerCAmelCase			      =	attention_window

         super().__init__(
             pad_token_id=_lowerCAmelCase							,  bos_token_id=_lowerCAmelCase							,  eos_token_id=_lowerCAmelCase							,  is_encoder_decoder=_lowerCAmelCase							,  decoder_start_token_id=_lowerCAmelCase							,  **_lowerCAmelCase							,  )