

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

from ...utils import logging


SCREAMING_SNAKE_CASE						:Dict      =			logging.get_logger(__name__)




def        lowerCAmelCase(	SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       )->		List[str]:



    """simple docstring"""






    UpperCamelCase_          =		nn.functional.normalize(SCREAMING_SNAKE_CASE_       )
    UpperCamelCase_          =		nn.functional.normalize(SCREAMING_SNAKE_CASE_       )
    return torch.mm(SCREAMING_SNAKE_CASE_       ,		normalized_text_embeds.t()       )






class 		__magic_name__ (				snake_case							):
     UpperCamelCase_     :Dict									=	CLIPConfig

     UpperCamelCase_     :Optional[int]									=	["""CLIPEncoderLayer"""]






     def __init__(							self						,		_lowercase     )->			str:
         super().__init__(_lowercase     )

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

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

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






     @torch.no_grad()
     def      UpperCAmelCase_							(							self						,		_lowercase						,		_lowercase     )->			Any:
         UpperCamelCase_          =		self.vision_model(_lowercase     )[1]  # pooled_output
         UpperCamelCase_          =		self.visual_projection(_lowercase     )

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

         UpperCamelCase_          =		[]
         UpperCamelCase_          =		image_embeds.shape[0]
         for i in range(_lowercase     ):
             UpperCamelCase_          =		{"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
             UpperCamelCase_          =		0.0

             for concept_idx in range(len(special_cos_dist[0]     )     ):
                 UpperCamelCase_          =		special_cos_dist[i][concept_idx]
                 UpperCamelCase_          =		self.special_care_embeds_weights[concept_idx].item()
                 UpperCamelCase_          =		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]}     )
                     UpperCamelCase_          =		0.01

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

             result.append(_lowercase     )

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

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

         if any(_lowercase     ):
             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      UpperCAmelCase_							(							self						,		_lowercase						,		_lowercase     )->			int:
         UpperCamelCase_          =		self.vision_model(_lowercase     )[1]  # pooled_output
         UpperCamelCase_          =		self.visual_projection(_lowercase     )

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

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

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

         UpperCamelCase_          =		0.0  # black image

         return images, has_nsfw_concepts








"""simple docstring"""







import unittest

import numpy as np

from transformers import ElectraConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
 from transformers.models.electra.modeling_flax_electra import (
     FlaxElectraForCausalLM,
     FlaxElectraForMaskedLM,
     FlaxElectraForMultipleChoice,
     FlaxElectraForPreTraining,
     FlaxElectraForQuestionAnswering,
     FlaxElectraForSequenceClassification,
     FlaxElectraForTokenClassification,
     FlaxElectraModel,
 )





class      UpperCamelCase_       (				unittest.TestCase     ):



  def __init__(					self       ,						snake_case__       ,						snake_case__=13       ,						snake_case__=7       ,						snake_case__=True       ,						snake_case__=True       ,						snake_case__=True       ,						snake_case__=True       ,						snake_case__=99       ,						snake_case__=24       ,						snake_case__=32       ,						snake_case__=5       ,						snake_case__=4       ,						snake_case__=37       ,						snake_case__="gelu"       ,						snake_case__=0.1       ,						snake_case__=0.1       ,						snake_case__=5_12       ,						snake_case__=16       ,						snake_case__=2       ,						snake_case__=0.02       ,						snake_case__=4       ,						)	->	int:



         """simple docstring"""





         UpperCAmelCase					=	parent
         UpperCAmelCase					=	batch_size
         UpperCAmelCase					=	seq_length
         UpperCAmelCase					=	is_training
         UpperCAmelCase					=	use_attention_mask
         UpperCAmelCase					=	use_token_type_ids
         UpperCAmelCase					=	use_labels
         UpperCAmelCase					=	vocab_size
         UpperCAmelCase					=	hidden_size
         UpperCAmelCase					=	embedding_size
         UpperCAmelCase					=	num_hidden_layers
         UpperCAmelCase					=	num_attention_heads
         UpperCAmelCase					=	intermediate_size
         UpperCAmelCase					=	hidden_act
         UpperCAmelCase					=	hidden_dropout_prob
         UpperCAmelCase					=	attention_probs_dropout_prob
         UpperCAmelCase					=	max_position_embeddings
         UpperCAmelCase					=	type_vocab_size
         UpperCAmelCase					=	type_sequence_label_size
         UpperCAmelCase					=	initializer_range
         UpperCAmelCase					=	num_choices



  def 						UpperCamelCase_ (					self )	->	Optional[Any]:



         """simple docstring"""





         UpperCAmelCase					=	ids_tensor([self.batch_size, self.seq_length]       ,						self.vocab_size )

         UpperCAmelCase					=	None
         if self.use_attention_mask:
                UpperCAmelCase					=	random_attention_mask([self.batch_size, self.seq_length] )

         UpperCAmelCase					=	None
         if self.use_token_type_ids:
                UpperCAmelCase					=	ids_tensor([self.batch_size, self.seq_length]       ,						self.type_vocab_size )

         UpperCAmelCase					=	ElectraConfig(
             vocab_size=self.vocab_size       ,						hidden_size=self.hidden_size       ,						embedding_size=self.embedding_size       ,						num_hidden_layers=self.num_hidden_layers       ,						num_attention_heads=self.num_attention_heads       ,						intermediate_size=self.intermediate_size       ,						hidden_act=self.hidden_act       ,						hidden_dropout_prob=self.hidden_dropout_prob       ,						attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,						max_position_embeddings=self.max_position_embeddings       ,						type_vocab_size=self.type_vocab_size       ,						initializer_range=self.initializer_range       ,						)

         return config, input_ids, token_type_ids, attention_mask



  def 						UpperCamelCase_ (					self )	->	List[str]:



         """simple docstring"""





         UpperCAmelCase					=	self.prepare_config_and_inputs()
         UpperCAmelCase			,				UpperCAmelCase			,				UpperCAmelCase			,				UpperCAmelCase					=	config_and_inputs
         UpperCAmelCase					=	{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask}
         return config, inputs_dict





@require_flax
class      UpperCamelCase_       (				a_      ,    unittest.TestCase     ):
  _A				:    str =		True

  _A				:    List[Any] =		(
      (
          FlaxElectraModel,
          FlaxElectraForCausalLM,
          FlaxElectraForMaskedLM,
          FlaxElectraForPreTraining,
          FlaxElectraForTokenClassification,
          FlaxElectraForQuestionAnswering,
          FlaxElectraForMultipleChoice,
          FlaxElectraForSequenceClassification,
      )
      if is_flax_available()
      else ()
  )



  def 						UpperCamelCase_ (					self )	->	str:



         """simple docstring"""





         UpperCAmelCase					=	FlaxElectraModelTester(self )



  @slow
  def 						UpperCamelCase_ (					self )	->	Tuple:



         """simple docstring"""





         for model_class_name in self.all_model_classes:
                if model_class_name == FlaxElectraForMaskedLM:
                       UpperCAmelCase					=	model_class_name.from_pretrained("""google/electra-small-generator""" )
                else:
                       UpperCAmelCase					=	model_class_name.from_pretrained("""google/electra-small-discriminator""" )
                UpperCAmelCase					=	model(np.ones((1, 1) ) )
                self.assertIsNotNone(snake_case__ )
