
"""simple docstring"""




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

from ...utils import logging


UpperCAmelCase__   :					Union[str, Any]												=				logging.get_logger(__name__)

def lowercase_						(     _snake_case     ,_snake_case		):
       SCREAMING_SNAKE_CASE__		: List[str]     =     nn.functional.normalize(_snake_case		)
       SCREAMING_SNAKE_CASE__		: Tuple     =     nn.functional.normalize(_snake_case		)
       return torch.mm(_snake_case     ,normalized_text_embeds.t()		)



class 			lowerCAmelCase_		(a__						):


    """simple docstring"""



    __UpperCamelCase   :		Optional[Any]       =     CLIPConfig

    __UpperCamelCase   :		Union[str, Any]       =     ['''CLIPEncoderLayer''']



    def __init__(self     , SCREAMING_SNAKE_CASE__					)      ->	Tuple:



           """simple docstring"""




           super().__init__(SCREAMING_SNAKE_CASE__					)

           SCREAMING_SNAKE_CASE__		: Optional[Any]     =     CLIPVisionModel(config.vision_config					)
           SCREAMING_SNAKE_CASE__		: Dict     =     nn.Linear(config.vision_config.hidden_size     , config.projection_dim     , bias=SCREAMING_SNAKE_CASE__					)

           SCREAMING_SNAKE_CASE__		: Any     =     nn.Parameter(torch.ones(17     , config.projection_dim					)     , requires_grad=SCREAMING_SNAKE_CASE__					)
           SCREAMING_SNAKE_CASE__		: Any     =     nn.Parameter(torch.ones(3     , config.projection_dim					)     , requires_grad=SCREAMING_SNAKE_CASE__					)

           SCREAMING_SNAKE_CASE__		: str     =     nn.Parameter(torch.ones(17					)     , requires_grad=SCREAMING_SNAKE_CASE__					)
           SCREAMING_SNAKE_CASE__		: Optional[int]     =     nn.Parameter(torch.ones(3					)     , requires_grad=SCREAMING_SNAKE_CASE__					)



    @torch.no_grad()
    def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__					)      ->	str:



           """simple docstring"""




           SCREAMING_SNAKE_CASE__		: Optional[Any]     =     self.vision_model(SCREAMING_SNAKE_CASE__					)[1]  # pooled_output
           SCREAMING_SNAKE_CASE__		: Dict     =     self.visual_projection(SCREAMING_SNAKE_CASE__					)

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

           SCREAMING_SNAKE_CASE__		: str     =     []
           SCREAMING_SNAKE_CASE__		: Tuple     =     image_embeds.shape[0]
           for i in range(SCREAMING_SNAKE_CASE__					):
                  SCREAMING_SNAKE_CASE__		: List[Any]     =     {"""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__		: Any     =     0.0

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

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

                  result.append(SCREAMING_SNAKE_CASE__					)

           SCREAMING_SNAKE_CASE__		: Any     =     [len(res["""bad_concepts"""]					) > 0 for res in result]

           return images, has_nsfw_concepts



    @torch.no_grad()
    def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__					)      ->	str:



           """simple docstring"""




           SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     self.vision_model(SCREAMING_SNAKE_CASE__					)[1]  # pooled_output
           SCREAMING_SNAKE_CASE__		: List[Any]     =     self.visual_projection(SCREAMING_SNAKE_CASE__					)

           SCREAMING_SNAKE_CASE__		: List[str]     =     cosine_distance(SCREAMING_SNAKE_CASE__     , self.special_care_embeds					)
           SCREAMING_SNAKE_CASE__		: int     =     cosine_distance(SCREAMING_SNAKE_CASE__     , 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__		: Tuple     =     special_cos_dist - self.special_care_embeds_weights + adjustment
           # special_scores = special_scores.round(decimals=3)
           SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     torch.any(special_scores > 0     , dim=1					)
           SCREAMING_SNAKE_CASE__		: Optional[Any]     =     special_care * 0.01
           SCREAMING_SNAKE_CASE__		: Optional[Any]     =     special_adjustment.unsqueeze(1					).expand(-1     , cos_dist.shape[1]					)

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

           return images, has_nsfw_concepts






"""simple docstring"""




import functools
import operator

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


UpperCAmelCase__   :					str												=				logging.get_logger(__name__)

UpperCAmelCase__   :					Dict												=				{
    'microsoft/unispeech-large-1500h-cv': (
        'https://huggingface.co/microsoft/unispeech-large-1500h-cv/resolve/main/config.json'
    ),
    # See all UniSpeech models at https://huggingface.co/models?filter=unispeech
}



class 			lowerCAmelCase_		(a__						):


				"""simple docstring"""



				__UpperCamelCase   :		List[str]       =     '''unispeech'''



				def __init__(self     , SCREAMING_SNAKE_CASE__=32     , SCREAMING_SNAKE_CASE__=7_68     , SCREAMING_SNAKE_CASE__=12     , SCREAMING_SNAKE_CASE__=12     , SCREAMING_SNAKE_CASE__=30_72     , SCREAMING_SNAKE_CASE__="gelu"     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__=0.0     , SCREAMING_SNAKE_CASE__=0.0     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__=0.02     , SCREAMING_SNAKE_CASE__=1E-5     , SCREAMING_SNAKE_CASE__="group"     , SCREAMING_SNAKE_CASE__="gelu"     , SCREAMING_SNAKE_CASE__=(5_12, 5_12, 5_12, 5_12, 5_12, 5_12, 5_12)     , SCREAMING_SNAKE_CASE__=(5, 2, 2, 2, 2, 2, 2)     , SCREAMING_SNAKE_CASE__=(10, 3, 3, 3, 3, 2, 2)     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__=1_28     , SCREAMING_SNAKE_CASE__=16     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__=0.05     , SCREAMING_SNAKE_CASE__=10     , SCREAMING_SNAKE_CASE__=2     , SCREAMING_SNAKE_CASE__=0.0     , SCREAMING_SNAKE_CASE__=10     , SCREAMING_SNAKE_CASE__=0     , SCREAMING_SNAKE_CASE__=3_20     , SCREAMING_SNAKE_CASE__=2     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__=1_00     , SCREAMING_SNAKE_CASE__=2_56     , SCREAMING_SNAKE_CASE__=2_56     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__="mean"     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__=2_56     , SCREAMING_SNAKE_CASE__=80     , SCREAMING_SNAKE_CASE__=0     , SCREAMING_SNAKE_CASE__=1     , SCREAMING_SNAKE_CASE__=2     , SCREAMING_SNAKE_CASE__=0.5     , **SCREAMING_SNAKE_CASE__     , )      ->	List[Any]:



											"""simple docstring"""




											super().__init__(**SCREAMING_SNAKE_CASE__     , pad_token_id=SCREAMING_SNAKE_CASE__     , bos_token_id=SCREAMING_SNAKE_CASE__     , eos_token_id=SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     hidden_size
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     feat_extract_norm
											SCREAMING_SNAKE_CASE__		: Any     =     feat_extract_activation
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     list(SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: str     =     list(SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: Tuple     =     list(SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: List[str]     =     conv_bias
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     num_conv_pos_embeddings
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     num_conv_pos_embedding_groups
											SCREAMING_SNAKE_CASE__		: Dict     =     len(self.conv_dim					)
											SCREAMING_SNAKE_CASE__		: List[str]     =     num_hidden_layers
											SCREAMING_SNAKE_CASE__		: str     =     intermediate_size
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     hidden_act
											SCREAMING_SNAKE_CASE__		: Any     =     num_attention_heads
											SCREAMING_SNAKE_CASE__		: Dict     =     hidden_dropout
											SCREAMING_SNAKE_CASE__		: List[str]     =     attention_dropout
											SCREAMING_SNAKE_CASE__		: Any     =     activation_dropout
											SCREAMING_SNAKE_CASE__		: int     =     feat_proj_dropout
											SCREAMING_SNAKE_CASE__		: List[Any]     =     final_dropout
											SCREAMING_SNAKE_CASE__		: str     =     layerdrop
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     layer_norm_eps
											SCREAMING_SNAKE_CASE__		: Dict     =     initializer_range
											SCREAMING_SNAKE_CASE__		: Dict     =     num_ctc_classes
											SCREAMING_SNAKE_CASE__		: List[Any]     =     vocab_size
											SCREAMING_SNAKE_CASE__		: str     =     do_stable_layer_norm
											SCREAMING_SNAKE_CASE__		: Dict     =     use_weighted_layer_sum
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     classifier_proj_size

											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
											SCREAMING_SNAKE_CASE__		: Tuple     =     apply_spec_augment
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     mask_time_prob
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     mask_time_length
											SCREAMING_SNAKE_CASE__		: List[str]     =     mask_time_min_masks
											SCREAMING_SNAKE_CASE__		: Tuple     =     mask_feature_prob
											SCREAMING_SNAKE_CASE__		: Any     =     mask_feature_length
											SCREAMING_SNAKE_CASE__		: int     =     mask_feature_min_masks

											# parameters for pretraining with codevector quantized representations
											SCREAMING_SNAKE_CASE__		: Dict     =     num_codevectors_per_group
											SCREAMING_SNAKE_CASE__		: Any     =     num_codevector_groups
											SCREAMING_SNAKE_CASE__		: str     =     contrastive_logits_temperature
											SCREAMING_SNAKE_CASE__		: str     =     feat_quantizer_dropout
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     num_negatives
											SCREAMING_SNAKE_CASE__		: Tuple     =     codevector_dim
											SCREAMING_SNAKE_CASE__		: List[Any]     =     proj_codevector_dim
											SCREAMING_SNAKE_CASE__		: Tuple     =     diversity_loss_weight

											# ctc loss
											SCREAMING_SNAKE_CASE__		: int     =     ctc_loss_reduction
											SCREAMING_SNAKE_CASE__		: str     =     ctc_zero_infinity

											# pretraining loss
											SCREAMING_SNAKE_CASE__		: List[str]     =     replace_prob



				@property
				def 			__magic_name__					(self					)      ->	Tuple:



											"""simple docstring"""




											return functools.reduce(operator.mul     , self.conv_stride     , 1					)




