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

from ...utils import logging


SCREAMING_SNAKE_CASE			:						Tuple				  =		logging.get_logger(__name__)



def 	__A		(      _A				,					_A       ):



 """simple docstring"""


 __a							=	nn.functional.normalize(_A       )
 __a							=	nn.functional.normalize(_A       )
 return torch.mm(_A				,					normalized_text_embeds.t()       )






class  A_			(							a_			):
  _SCREAMING_SNAKE_CASE     					=      CLIPConfig

  _SCREAMING_SNAKE_CASE     					=      ["""CLIPEncoderLayer"""]

  def __init__(		self			:   List[str]  ,				__SCREAMING_SNAKE_CASE			:   CLIPConfig							):
   super().__init__(__SCREAMING_SNAKE_CASE							)

   __a							=	CLIPVisionModel(config.vision_config							)
   __a							=	nn.Linear(config.vision_config.hidden_size  ,				config.projection_dim  ,				bias=__SCREAMING_SNAKE_CASE							)

   __a							=	nn.Parameter(torch.ones(17  ,				config.projection_dim							)  ,				requires_grad=__SCREAMING_SNAKE_CASE							)
   __a							=	nn.Parameter(torch.ones(3  ,				config.projection_dim							)  ,				requires_grad=__SCREAMING_SNAKE_CASE							)

   __a							=	nn.Parameter(torch.ones(17							)  ,				requires_grad=__SCREAMING_SNAKE_CASE							)
   __a							=	nn.Parameter(torch.ones(3							)  ,				requires_grad=__SCREAMING_SNAKE_CASE							)

  @torch.no_grad()
  def 		_UpperCAmelCase (		self			:   Dict  ,				__SCREAMING_SNAKE_CASE			:   str  ,				__SCREAMING_SNAKE_CASE			:   str							):
   __a							=	self.vision_model(__SCREAMING_SNAKE_CASE							)[1]  # pooled_output
   __a							=	self.visual_projection(__SCREAMING_SNAKE_CASE							)

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

   __a							=	[]
   __a							=	image_embeds.shape[0]
   for i in range(__SCREAMING_SNAKE_CASE							):
    __a							=	{"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
    __a							=	0.0

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

    for concept_idx in range(len(cos_dist[0]							)							):
     __a							=	cos_dist[i][concept_idx]
     __a							=	self.concept_embeds_weights[concept_idx].item()
     __a							=	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							)

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

   return images, has_nsfw_concepts

  @torch.no_grad()
  def 		_UpperCAmelCase (		self			:   Tuple  ,				__SCREAMING_SNAKE_CASE			:   torch.FloatTensor  ,				__SCREAMING_SNAKE_CASE			:   torch.FloatTensor							):
   __a							=	self.vision_model(__SCREAMING_SNAKE_CASE							)[1]  # pooled_output
   __a							=	self.visual_projection(__SCREAMING_SNAKE_CASE							)

   __a							=	cosine_distance(__SCREAMING_SNAKE_CASE  ,				self.special_care_embeds							)
   __a							=	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
   __a							=	0.0

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

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

   return images, has_nsfw_concepts



import argparse
import json

import numpy
import torch

from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()



def 	__A		(      _A				,					_A       ):



 """simple docstring"""


 __a							=	torch.load(_A				,					map_location="cpu"       )

 __a							=	chkpt["model"]

 # We have the base model one level deeper than the original XLM repository
 __a							=	{}
 for k, v in state_dict.items():
  if "pred_layer" in k:
   __a							=	v
  else:
   __a							=	v

 __a							=	chkpt["params"]
 __a							=	{n: v for n, v in config.items() if not isinstance(_A				,					(torch.FloatTensor, numpy.ndarray)       )}

 __a							=	chkpt["dico_word2id"]
 __a							=	{s + "</w>" if s.find("@@"       ) == -1 and i > 13 else s.replace("@@"				,					""       ): i for s, i in vocab.items()}

 # Save pytorch-model
 __a							=	pytorch_dump_folder_path + "/" + WEIGHTS_NAME
 __a							=	pytorch_dump_folder_path + "/" + CONFIG_NAME
 __a							=	pytorch_dump_folder_path + "/" + VOCAB_FILES_NAMES["vocab_file"]

 print(f"""Save PyTorch model to {pytorch_weights_dump_path}"""       )
 torch.save(_A				,					_A       )

 print(f"""Save configuration file to {pytorch_config_dump_path}"""       )
 with open(_A				,					"w"				,					encoding="utf-8"       ) as f:
  f.write(json.dumps(_A				,					indent=2       ) + "\n"       )

 print(f"""Save vocab file to {pytorch_config_dump_path}"""       )
 with open(_A				,					"w"				,					encoding="utf-8"       ) as f:
  f.write(json.dumps(_A				,					indent=2       ) + "\n"       )


if __name__ == "__main__":
  SCREAMING_SNAKE_CASE			:						Union[str, Any]				  =		argparse.ArgumentParser()
  # Required parameters
  parser.add_argument(
      """--xlm_checkpoint_path""", default=None, type=str, required=True, help="""Path the official PyTorch dump."""
  )
  parser.add_argument(
      """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model."""
  )
  SCREAMING_SNAKE_CASE			:						Any				  =		parser.parse_args()
  convert_xlm_checkpoint_to_pytorch(args.xlm_checkpoint_path, args.pytorch_dump_folder_path)


