





"""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	:		Any				,		SCREAMING_SNAKE_CASE	:		Dict     ):







      '''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					)						->	Union[str, Any]:
            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					)						->	List[str]:
            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					)						->	int:
            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 ...configuration_utils import PretrainedConfig
from ...utils import logging


SCREAMING_SNAKE_CASE__   =  logging.get_logger(__name__)

SCREAMING_SNAKE_CASE__   =  {
    "facebook/s2t-wav2vec2-large-en-de": (
        "https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/config.json"
    ),
    # See all Speech2Text models at https://huggingface.co/models?filter=speech2text2
}

class 						lowercase   (   _UpperCAmelCase   ):
      _SCREAMING_SNAKE_CASE							      = 'speech_to_text_2'
      _SCREAMING_SNAKE_CASE							      = ['past_key_values']
      _SCREAMING_SNAKE_CASE							      = {'num_attention_heads': 'decoder_attention_heads', 'hidden_size': 'd_model'}





      def __init__(			self ,		lowercase=10_000 ,		lowercase=6 ,		lowercase=2_048 ,		lowercase=4 ,		lowercase=0.0 ,		lowercase=True ,		lowercase="relu" ,		lowercase=256 ,		lowercase=0.1 ,		lowercase=0.0 ,		lowercase=0.0 ,		lowercase=0.02 ,		lowercase=2 ,		lowercase=True ,		lowercase=1 ,		lowercase=0 ,		lowercase=2 ,		lowercase=1_024 ,		**lowercase ,		)						->	Optional[int]:
            lowerCAmelCase    		=			vocab_size
            lowerCAmelCase    		=			d_model
            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    		=			decoder_layerdrop
            lowerCAmelCase    		=			use_cache
            lowerCAmelCase    		=			decoder_layers
            lowerCAmelCase    		=			scale_embedding  # scale factor will be sqrt(d_model) if True
            lowerCAmelCase    		=			max_target_positions

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






