


import math

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


UpperCAmelCase__   	=							logging.get_logger(__name__)

UpperCAmelCase__   	=							{
    '''facebook/data2vec-base-960h''': '''https://huggingface.co/facebook/data2vec-audio-base-960h/resolve/main/config.json''',
    # See all Data2VecAudio models at https://huggingface.co/models?filter=data2vec-audio
}







class       lowerCamelCase__   (       lowerCAmelCase):
     SCREAMING_SNAKE_CASE__      			=			'''data2vec-audio'''





     def __init__(self		,     UpperCAmelCase=3_2		,     UpperCAmelCase=7_6_8		,     UpperCAmelCase=1_2		,     UpperCAmelCase=1_2		,     UpperCAmelCase=3_0_7_2		,     UpperCAmelCase="gelu"		,     UpperCAmelCase=0.1		,     UpperCAmelCase=0.1		,     UpperCAmelCase=0.1		,     UpperCAmelCase=0.0		,     UpperCAmelCase=0.1		,     UpperCAmelCase=0.1		,     UpperCAmelCase=0.02		,     UpperCAmelCase=1e-5		,     UpperCAmelCase="gelu"		,     UpperCAmelCase=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2)		,     UpperCAmelCase=(5, 2, 2, 2, 2, 2, 2)		,     UpperCAmelCase=(1_0, 3, 3, 3, 3, 2, 2)		,     UpperCAmelCase=False		,     UpperCAmelCase=1_6		,     UpperCAmelCase=1_9		,     UpperCAmelCase=5		,     UpperCAmelCase=0.05		,     UpperCAmelCase=1_0		,     UpperCAmelCase=2		,     UpperCAmelCase=0.0		,     UpperCAmelCase=1_0		,     UpperCAmelCase=0		,     UpperCAmelCase="sum"		,     UpperCAmelCase=False		,     UpperCAmelCase=False		,     UpperCAmelCase=2_5_6		,     UpperCAmelCase=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 1_5_0_0)		,     UpperCAmelCase=(5, 3, 3, 1, 1)		,     UpperCAmelCase=(1, 2, 3, 1, 1)		,     UpperCAmelCase=5_1_2		,     UpperCAmelCase=0		,     UpperCAmelCase=1		,     UpperCAmelCase=2		,     UpperCAmelCase=False		,     UpperCAmelCase=3		,     UpperCAmelCase=2		,     UpperCAmelCase=3		,     UpperCAmelCase=None		,     **UpperCAmelCase		,     )     ->   List[Any]:
      super().__init__(**UpperCAmelCase		,     pad_token_id=UpperCAmelCase		,     bos_token_id=UpperCAmelCase		,     eos_token_id=UpperCAmelCase       )
      _lowercase							      =hidden_size
      _lowercase							      =feat_extract_activation
      _lowercase							      =list(UpperCAmelCase       )
      _lowercase							      =list(UpperCAmelCase       )
      _lowercase							      =list(UpperCAmelCase       )
      _lowercase							      =conv_bias
      _lowercase							      =num_conv_pos_embeddings
      _lowercase							      =num_conv_pos_embedding_groups
      _lowercase							      =conv_pos_kernel_size
      _lowercase							      =len(self.conv_dim       )
      _lowercase							      =num_hidden_layers
      _lowercase							      =intermediate_size
      _lowercase							      =hidden_act
      _lowercase							      =num_attention_heads
      _lowercase							      =hidden_dropout
      _lowercase							      =attention_dropout
      _lowercase							      =activation_dropout
      _lowercase							      =feat_proj_dropout
      _lowercase							      =final_dropout
      _lowercase							      =layerdrop
      _lowercase							      =layer_norm_eps
      _lowercase							      =initializer_range
      _lowercase							      =vocab_size
      _lowercase							      =use_weighted_layer_sum

      if (
          (len(self.conv_stride       ) != self.num_feat_extract_layers)
          or (len(self.conv_kernel       ) != self.num_feat_extract_layers)
          or (len(self.conv_dim       ) != self.num_feat_extract_layers)
      ):
       raise ValueError(
           '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =='''
           ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ='''
           f" {len(self.conv_dim       )}`, `len(config.conv_stride) = {len(self.conv_stride       )}`,"
           f" `len(config.conv_kernel) = {len(self.conv_kernel       )}`."       )

      # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
      _lowercase							      =mask_time_prob
      _lowercase							      =mask_time_length
      _lowercase							      =mask_time_min_masks
      _lowercase							      =mask_feature_prob
      _lowercase							      =mask_feature_length
      _lowercase							      =mask_feature_min_masks

      # ctc loss
      _lowercase							      =ctc_loss_reduction
      _lowercase							      =ctc_zero_infinity

      # adapter
      _lowercase							      =add_adapter
      _lowercase							      =adapter_kernel_size
      _lowercase							      =adapter_stride
      _lowercase							      =num_adapter_layers
      _lowercase							      =output_hidden_size or hidden_size

      # SequenceClassification-specific parameter. Feel free to ignore for other classes.
      _lowercase							      =classifier_proj_size

      # XVector-specific parameters. Feel free to ignore for other classes.
      _lowercase							      =list(UpperCAmelCase       )
      _lowercase							      =list(UpperCAmelCase       )
      _lowercase							      =list(UpperCAmelCase       )
      _lowercase							      =xvector_output_dim





     @property
     def      __A					(self       )     ->   int:
      return math.prod(self.conv_stride       )





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

from ...utils import logging


__lowerCAmelCase	: Dict  				=logging.get_logger(__name__)


def        _UpperCamelCase							(      lowercase__    ,       lowercase__      ):
						__SCREAMING_SNAKE_CASE						:     List[Any]					=		nn.functional.normalize(lowercase__      )
						__SCREAMING_SNAKE_CASE						:     Tuple					=		nn.functional.normalize(lowercase__      )
						return torch.mm(lowercase__    ,       normalized_text_embeds.t()      )
class        _lowercase      (			A__       ):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE__				:			Optional[Any]						=		CLIPConfig

				SCREAMING_SNAKE_CASE__				:			List[str]						=		['''CLIPEncoderLayer''']


				def __init__(       self			:str			,			lowerCAmelCase__			:CLIPConfig	)		->			Tuple:
										super().__init__(lowerCAmelCase__	)

										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		CLIPVisionModel(config.vision_config	)
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		nn.Linear(config.vision_config.hidden_size			,			config.projection_dim			,			bias=lowerCAmelCase__	)

										__SCREAMING_SNAKE_CASE						:     Any					=		nn.Parameter(torch.ones(17			,			config.projection_dim	)			,			requires_grad=lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		nn.Parameter(torch.ones(3			,			config.projection_dim	)			,			requires_grad=lowerCAmelCase__	)

										__SCREAMING_SNAKE_CASE						:     Dict					=		nn.Parameter(torch.ones(17	)			,			requires_grad=lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     str					=		nn.Parameter(torch.ones(3	)			,			requires_grad=lowerCAmelCase__	)


				@torch.no_grad()
				def 		__magic_name__(       self			:Optional[int]			,			lowerCAmelCase__			:int			,			lowerCAmelCase__			:Optional[int]	)		->			Dict:
										__SCREAMING_SNAKE_CASE						:     int					=		self.vision_model(lowerCAmelCase__	)[1]  # pooled_output
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		self.visual_projection(lowerCAmelCase__	)

										# we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		cosine_distance(lowerCAmelCase__			,			self.special_care_embeds	).cpu().float().numpy()
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		cosine_distance(lowerCAmelCase__			,			self.concept_embeds	).cpu().float().numpy()

										__SCREAMING_SNAKE_CASE						:     List[Any]					=		[]
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		image_embeds.shape[0]
										for i in range(lowerCAmelCase__	):
																__SCREAMING_SNAKE_CASE						:     Optional[int]					=		{'''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
																__SCREAMING_SNAKE_CASE						:     List[Any]					=		0.0

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

																for concept_idx in range(len(cos_dist[0]	)	):
																						__SCREAMING_SNAKE_CASE						:     int					=		cos_dist[i][concept_idx]
																						__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		self.concept_embeds_weights[concept_idx].item()
																						__SCREAMING_SNAKE_CASE						:     Tuple					=		round(concept_cos - concept_threshold + adjustment			,			3	)
																						if result_img["concept_scores"][concept_idx] > 0:
																												result_img["bad_concepts"].append(lowerCAmelCase__	)

																result.append(lowerCAmelCase__	)

										__SCREAMING_SNAKE_CASE						:     List[str]					=		[len(res['''bad_concepts''']	) > 0 for res in result]

										return images, has_nsfw_concepts



				@torch.no_grad()
				def 		__magic_name__(       self			:Optional[int]			,			lowerCAmelCase__			:torch.FloatTensor			,			lowerCAmelCase__			:torch.FloatTensor	)		->			Any:
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		self.vision_model(lowerCAmelCase__	)[1]  # pooled_output
										__SCREAMING_SNAKE_CASE						:     List[str]					=		self.visual_projection(lowerCAmelCase__	)

										__SCREAMING_SNAKE_CASE						:     Dict					=		cosine_distance(lowerCAmelCase__			,			self.special_care_embeds	)
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		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
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		0.0

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

										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		(cos_dist - self.concept_embeds_weights) + special_adjustment
										# concept_scores = concept_scores.round(decimals=3)
										__SCREAMING_SNAKE_CASE						:     Any					=		torch.any(concept_scores > 0			,			dim=1	)

										return images, has_nsfw_concepts
