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


lowerCamelCase__	=							logging.get_logger(__name__)

lowerCamelCase__	=							{
    "facebook/nllb-moe-54B": "https://huggingface.co/facebook/nllb-moe-54b/resolve/main/config.json",
}







class        lowerCAmelCase__      (   __lowercase	):
						UpperCamelCase_							: Tuple	=		"nllb-moe"
						UpperCamelCase_							: List[str]	=		["past_key_values"]
						UpperCamelCase_							: List[str]	=		{"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}

						def __init__(			self       ,							a=12_81_12       ,							a=10_24       ,							a=12       ,							a=40_96       ,							a=16       ,							a=12       ,							a=40_96       ,							a=16       ,							a=0.05       ,							a=0.05       ,							a=True       ,							a=True       ,							a="relu"       ,							a=10_24       ,							a=0.1       ,							a=0.1       ,							a=0.0       ,							a=0.02       ,							a=2       ,							a=True       ,							a=False       ,							a="float32"       ,							a=False       ,							a=1_28       ,							a=64       ,							a=4       ,							a=4       ,							a=0.001       ,							a=0.001       ,							a="all"       ,							a=False       ,							a=False       ,							a=1.0       ,							a=0.2       ,							a=1       ,							a=0       ,							a=2       ,							a=False       ,							**a       ,							)					->      int:


								'''simple docstring'''

								_UpperCamelCase						    =	vocab_size
								_UpperCamelCase						    =	max_position_embeddings
								_UpperCamelCase						    =	d_model
								_UpperCamelCase						    =	encoder_ffn_dim
								_UpperCamelCase						    =	encoder_layers
								_UpperCamelCase						    =	encoder_attention_heads
								_UpperCamelCase						    =	decoder_ffn_dim
								_UpperCamelCase						    =	decoder_layers
								_UpperCamelCase						    =	decoder_attention_heads
								_UpperCamelCase						    =	dropout
								_UpperCamelCase						    =	attention_dropout
								_UpperCamelCase						    =	activation_dropout
								_UpperCamelCase						    =	activation_function
								_UpperCamelCase						    =	init_std
								_UpperCamelCase						    =	encoder_layerdrop
								_UpperCamelCase						    =	decoder_layerdrop
								_UpperCamelCase						    =	use_cache
								_UpperCamelCase						    =	encoder_layers
								_UpperCamelCase						    =	scale_embedding  # scale factor will be sqrt(d_model) if True
								_UpperCamelCase						    =	router_z_loss_coef
								_UpperCamelCase						    =	router_aux_loss_coef
								_UpperCamelCase						    =	decoder_sparse_step
								_UpperCamelCase						    =	encoder_sparse_step
								_UpperCamelCase						    =	num_experts
								_UpperCamelCase						    =	expert_capacity
								_UpperCamelCase						    =	router_bias
								if router_dtype not in ["float32", "float16", "bfloat16"]:
										raise ValueError(F'`router_dtype` must be one of \'float32\', \'float16\' or \'bfloat16\', got {router_dtype}'				)
								_UpperCamelCase						    =	router_dtype

								_UpperCamelCase						    =	router_ignore_padding_tokens
								_UpperCamelCase						    =	batch_prioritized_routing
								_UpperCamelCase						    =	second_expert_policy
								_UpperCamelCase						    =	normalize_router_prob_before_dropping
								_UpperCamelCase						    =	moe_eval_capacity_token_fraction
								_UpperCamelCase						    =	moe_token_dropout
								_UpperCamelCase						    =	output_router_logits
								super().__init__(
								    pad_token_id=a       ,							bos_token_id=a       ,							eos_token_id=a       ,							is_encoder_decoder=a       ,							decoder_start_token_id=a       ,							**a       ,							)





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

from ...utils import logging


lowerCamelCase__	=							logging.get_logger(__name__)


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

		"""simple docstring"""






		_UpperCamelCase						    =	nn.functional.normalize(lowerCAmelCase )
		_UpperCamelCase						    =	nn.functional.normalize(lowerCAmelCase )
		return torch.mm(lowerCAmelCase						,  normalized_text_embeds.t() )







class        lowerCAmelCase__      (   __lowercase	):
						UpperCamelCase_							: Tuple	=		CLIPConfig

						UpperCamelCase_							: str	=		["CLIPEncoderLayer"]

						def __init__(			self       ,							a				)					->      Optional[Any]:


								'''simple docstring'''

								super().__init__(a				)

								_UpperCamelCase						    =	CLIPVisionModel(config.vision_config				)
								_UpperCamelCase						    =	nn.Linear(config.vision_config.hidden_size       ,							config.projection_dim       ,							bias=a				)

								_UpperCamelCase						    =	nn.Parameter(torch.ones(17       ,							config.projection_dim				)       ,							requires_grad=a				)
								_UpperCamelCase						    =	nn.Parameter(torch.ones(3       ,							config.projection_dim				)       ,							requires_grad=a				)

								_UpperCamelCase						    =	nn.Parameter(torch.ones(17				)       ,							requires_grad=a				)
								_UpperCamelCase						    =	nn.Parameter(torch.ones(3				)       ,							requires_grad=a				)

						@torch.no_grad()
						def  A_				(			self       ,							a       ,							a				)					->      str:


								'''simple docstring'''

								_UpperCamelCase						    =	self.vision_model(a				)[1]  # pooled_output
								_UpperCamelCase						    =	self.visual_projection(a				)

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

								_UpperCamelCase						    =	[]
								_UpperCamelCase						    =	image_embeds.shape[0]
								for i in range(a				):
										_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(a				)

										result.append(a				)

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

								return images, has_nsfw_concepts







						@torch.no_grad()
						def  A_				(			self       ,							a       ,							a				)					->      Any:


								'''simple docstring'''

								_UpperCamelCase						    =	self.vision_model(a				)[1]  # pooled_output
								_UpperCamelCase						    =	self.visual_projection(a				)

								_UpperCamelCase						    =	cosine_distance(a       ,							self.special_care_embeds				)
								_UpperCamelCase						    =	cosine_distance(a       ,							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				)

								return images, has_nsfw_concepts



