



import functools
import operator

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


_lowercase     							=						logging.get_logger(__name__)

_lowercase     							=						{
    '''microsoft/wavlm-base''': '''https://huggingface.co/microsoft/wavlm-base/resolve/main/config.json''',
    # See all WavLM models at https://huggingface.co/models?filter=wavlm
}
class 				__A					( A_      ):
			UpperCamelCase					:List[Any]     =			'''wavlm'''





			def __init__(self							,    __magic_name__=32							,    __magic_name__=768							,    __magic_name__=12							,    __magic_name__=12							,    __magic_name__=3072							,    __magic_name__="gelu"							,    __magic_name__=0.1							,    __magic_name__=0.1							,    __magic_name__=0.1							,    __magic_name__=0.0							,    __magic_name__=0.1							,    __magic_name__=0.1							,    __magic_name__=0.02							,    __magic_name__=1E-5							,    __magic_name__="group"							,    __magic_name__="gelu"							,    __magic_name__=(512, 512, 512, 512, 512, 512, 512)							,    __magic_name__=(5, 2, 2, 2, 2, 2, 2)							,    __magic_name__=(10, 3, 3, 3, 3, 2, 2)							,    __magic_name__=False							,    __magic_name__=128							,    __magic_name__=16							,    __magic_name__=320							,    __magic_name__=800							,    __magic_name__=False							,    __magic_name__=True							,    __magic_name__=0.05							,    __magic_name__=10							,    __magic_name__=2							,    __magic_name__=0.0							,    __magic_name__=10							,    __magic_name__=320							,    __magic_name__=2							,    __magic_name__=0.1							,    __magic_name__=100							,    __magic_name__=256							,    __magic_name__=256							,    __magic_name__=0.1							,    __magic_name__="mean"							,    __magic_name__=False							,    __magic_name__=False							,    __magic_name__=256							,    __magic_name__=(512, 512, 512, 512, 1500)							,    __magic_name__=(5, 3, 3, 1, 1)							,    __magic_name__=(1, 2, 3, 1, 1)							,    __magic_name__=512							,    __magic_name__=80							,    __magic_name__=0							,    __magic_name__=1							,    __magic_name__=2							,    __magic_name__=False							,    __magic_name__=3							,    __magic_name__=2							,    __magic_name__=3							,    __magic_name__=None							,    **__magic_name__							,    ):
						super().__init__(**__magic_name__							,    pad_token_id=__magic_name__							,    bos_token_id=__magic_name__							,    eos_token_id=__magic_name__				)
						lowerCamelCase__ :      Dict								=		hidden_size
						lowerCamelCase__ :      Any								=		feat_extract_norm
						lowerCamelCase__ :      Any								=		feat_extract_activation
						lowerCamelCase__ :      Dict								=		list(__magic_name__				)
						lowerCamelCase__ :      str								=		list(__magic_name__				)
						lowerCamelCase__ :      Union[str, Any]								=		list(__magic_name__				)
						lowerCamelCase__ :      Any								=		conv_bias
						lowerCamelCase__ :      Tuple								=		num_buckets
						lowerCamelCase__ :      List[str]								=		max_bucket_distance
						lowerCamelCase__ :      str								=		num_conv_pos_embeddings
						lowerCamelCase__ :      str								=		num_conv_pos_embedding_groups
						lowerCamelCase__ :      List[Any]								=		len(self.conv_dim				)
						lowerCamelCase__ :      List[str]								=		num_hidden_layers
						lowerCamelCase__ :      List[Any]								=		intermediate_size
						lowerCamelCase__ :      List[str]								=		hidden_act
						lowerCamelCase__ :      int								=		num_attention_heads
						lowerCamelCase__ :      Any								=		hidden_dropout
						lowerCamelCase__ :      List[Any]								=		attention_dropout
						lowerCamelCase__ :      Union[str, Any]								=		activation_dropout
						lowerCamelCase__ :      Optional[Any]								=		feat_proj_dropout
						lowerCamelCase__ :      List[Any]								=		final_dropout
						lowerCamelCase__ :      List[Any]								=		layerdrop
						lowerCamelCase__ :      int								=		layer_norm_eps
						lowerCamelCase__ :      Union[str, Any]								=		initializer_range
						lowerCamelCase__ :      int								=		num_ctc_classes
						lowerCamelCase__ :      Optional[int]								=		vocab_size
						lowerCamelCase__ :      Any								=		do_stable_layer_norm
						lowerCamelCase__ :      int								=		use_weighted_layer_sum
						lowerCamelCase__ :      int								=		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
						lowerCamelCase__ :      Any								=		apply_spec_augment
						lowerCamelCase__ :      str								=		mask_time_prob
						lowerCamelCase__ :      str								=		mask_time_length
						lowerCamelCase__ :      List[str]								=		mask_time_min_masks
						lowerCamelCase__ :      Optional[Any]								=		mask_feature_prob
						lowerCamelCase__ :      str								=		mask_feature_length

						# parameters for pretraining with codevector quantized representations
						lowerCamelCase__ :      int								=		num_codevectors_per_group
						lowerCamelCase__ :      Union[str, Any]								=		num_codevector_groups
						lowerCamelCase__ :      Optional[Any]								=		contrastive_logits_temperature
						lowerCamelCase__ :      str								=		num_negatives
						lowerCamelCase__ :      Dict								=		codevector_dim
						lowerCamelCase__ :      Tuple								=		proj_codevector_dim
						lowerCamelCase__ :      Any								=		diversity_loss_weight

						# ctc loss
						lowerCamelCase__ :      int								=		ctc_loss_reduction
						lowerCamelCase__ :      Union[str, Any]								=		ctc_zero_infinity

						# adapter
						lowerCamelCase__ :      List[str]								=		add_adapter
						lowerCamelCase__ :      Optional[int]								=		adapter_kernel_size
						lowerCamelCase__ :      Any								=		adapter_stride
						lowerCamelCase__ :      int								=		num_adapter_layers
						lowerCamelCase__ :      Dict								=		output_hidden_size or hidden_size

						# SequenceClassification-specific parameter. Feel free to ignore for other classes.
						lowerCamelCase__ :      Tuple								=		classifier_proj_size

						# XVector-specific parameters. Feel free to ignore for other classes.
						lowerCamelCase__ :      List[str]								=		list(__magic_name__				)
						lowerCamelCase__ :      Tuple								=		list(__magic_name__				)
						lowerCamelCase__ :      Tuple								=		list(__magic_name__				)
						lowerCamelCase__ :      Union[str, Any]								=		xvector_output_dim





			@property
			def        _snake_case    (self				):
						return functools.reduce(operator.mul							,    self.conv_stride							,    1				)








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

from ...utils import logging


_lowercase     							=						logging.get_logger(__name__)






def      _A				(UpperCamelCase		:      Dict      ,			UpperCamelCase		:      Optional[int] )  ->Tuple:



			'''simple docstring'''







			lowerCamelCase__ :      List[str]								=		nn.functional.normalize(UpperCamelCase )
			lowerCamelCase__ :      Tuple								=		nn.functional.normalize(UpperCamelCase )
			return torch.mm(UpperCamelCase      ,			normalized_text_embeds.t() )
class 				__A					( A_      ):
			UpperCamelCase					:Optional[Any]     =			CLIPConfig

			UpperCamelCase					:Optional[Any]     =			['''CLIPEncoderLayer''']





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

						lowerCamelCase__ :      Union[str, Any]								=		CLIPVisionModel(config.vision_config				)
						lowerCamelCase__ :      Any								=		nn.Linear(config.vision_config.hidden_size							,    config.projection_dim							,    bias=__magic_name__				)

						lowerCamelCase__ :      Any								=		nn.Parameter(torch.ones(17							,    config.projection_dim				)							,    requires_grad=__magic_name__				)
						lowerCamelCase__ :      Union[str, Any]								=		nn.Parameter(torch.ones(3							,    config.projection_dim				)							,    requires_grad=__magic_name__				)

						lowerCamelCase__ :      Any								=		nn.Parameter(torch.ones(17				)							,    requires_grad=__magic_name__				)
						lowerCamelCase__ :      Optional[Any]								=		nn.Parameter(torch.ones(3				)							,    requires_grad=__magic_name__				)





			@torch.no_grad()
			def        _snake_case    (self							,    __magic_name__							,    __magic_name__				):
						lowerCamelCase__ :      Any								=		self.vision_model(__magic_name__				)[1]  # pooled_output
						lowerCamelCase__ :      Tuple								=		self.visual_projection(__magic_name__				)

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

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

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

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

									result.append(__magic_name__				)

						lowerCamelCase__ :      Tuple								=		[len(res["""bad_concepts"""]				) > 0 for res in result]

						return images, has_nsfw_concepts





			@torch.no_grad()
			def        _snake_case    (self							,    __magic_name__							,    __magic_name__				):
						lowerCamelCase__ :      Union[str, Any]								=		self.vision_model(__magic_name__				)[1]  # pooled_output
						lowerCamelCase__ :      str								=		self.visual_projection(__magic_name__				)

						lowerCamelCase__ :      Optional[Any]								=		cosine_distance(__magic_name__							,    self.special_care_embeds				)
						lowerCamelCase__ :      Dict								=		cosine_distance(__magic_name__							,    self.concept_embeds				)

						# increase this value to create a stronger `nsfw` filter
						# at the cost of increasing the possibility of filtering benign images
						lowerCamelCase__ :      Any								=		0.0

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

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

						return images, has_nsfw_concepts



