
"""simple docstring"""




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

from ...utils import logging


_A     :			str  						=			logging.get_logger(__name__)


def     __magic_name__      ( __snake_case							:			Optional[Any]	,					__snake_case							:			str    )					->							Optional[Any]:
       lowercase       :							List[Any]									=				nn.functional.normalize(__snake_case    )
       lowercase       :							Tuple									=				nn.functional.normalize(__snake_case    )
       return torch.mm(__snake_case	,					normalized_text_embeds.t()    )

class 						a__		( a_		):
   __lowerCAmelCase				=							CLIPConfig

   __lowerCAmelCase				=							["""CLIPEncoderLayer"""]
   def __init__(     self      ,       _a							):
          super().__init__(_a							)

          lowercase       :							int									=				CLIPVisionModel(config.vision_config							)
          lowercase       :							Any									=				nn.Linear(config.vision_config.hidden_size      ,       config.projection_dim      ,       bias=_a							)

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

          lowercase       :							Tuple									=				nn.Parameter(torch.ones(17							)      ,       requires_grad=_a							)
          lowercase       :							Optional[Any]									=				nn.Parameter(torch.ones(3							)      ,       requires_grad=_a							)
   @torch.no_grad()
   def      __magic_name__     (     self      ,       _a      ,       _a							):
          lowercase       :							Optional[int]									=				self.vision_model(_a							)[1]  # pooled_output
          lowercase       :							Optional[int]									=				self.visual_projection(_a							)

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

          lowercase       :							str									=				[]
          lowercase       :							Dict									=				image_embeds.shape[0]
          for i in range(_a							):
                 lowercase       :							List[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
                 lowercase       :							Tuple									=				0.0

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

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

                 result.append(_a							)

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

          return images, has_nsfw_concepts

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

          lowercase       :							List[Any]									=				cosine_distance(_a      ,       self.special_care_embeds							)
          lowercase       :							int									=				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
          lowercase       :							List[Any]									=				0.0

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

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

          return images, has_nsfw_concepts




"""simple docstring"""




import os
from typing import List, Optional, Union

from ...tokenization_utils import PreTrainedTokenizer
from ...tokenization_utils_base import AddedToken
from ...utils import logging


_A     :			Tuple  						=			logging.get_logger(__name__)

_A     :			Dict  						=			{"""vocab_file""": """vocab.txt"""}

_A     :			Optional[Any]  						=			{
    """vocab_file""": {
        """facebook/esm2_t6_8M_UR50D""": """https://huggingface.co/facebook/esm2_t6_8M_UR50D/resolve/main/vocab.txt""",
        """facebook/esm2_t12_35M_UR50D""": """https://huggingface.co/facebook/esm2_t12_35M_UR50D/resolve/main/vocab.txt""",
    },
}

_A     :			Optional[Any]  						=			{
    """facebook/esm2_t6_8M_UR50D""": 10_24,
    """facebook/esm2_t12_35M_UR50D""": 10_24,
}


def     __magic_name__      ( __snake_case							:			Tuple    )					->							str:
       with open(__snake_case	,					"r"    ) as f:
              lowercase       :							Dict									=				f.read().splitlines()
              return [l.strip() for l in lines]

class 						a__		( a_		):
   __lowerCAmelCase				=							VOCAB_FILES_NAMES
   __lowerCAmelCase				=							PRETRAINED_VOCAB_FILES_MAP
   __lowerCAmelCase				=							PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
   __lowerCAmelCase				=							["""input_ids""", """attention_mask"""]
   def __init__(     self      ,       _a      ,       _a="<unk>"      ,       _a="<cls>"      ,       _a="<pad>"      ,       _a="<mask>"      ,       _a="<eos>"      ,       **_a      ,       ):
          super().__init__(**_a							)
          lowercase       :							Union[str, Any]									=				load_vocab_file(_a							)
          lowercase       :							Union[str, Any]									=				dict(enumerate(self.all_tokens							)							)
          lowercase       :							Any									=				{tok: ind for ind, tok in enumerate(self.all_tokens							)}
          lowercase       :							Dict									=				unk_token
          lowercase       :							Optional[int]									=				cls_token
          lowercase       :							Optional[int]									=				pad_token
          lowercase       :							Any									=				mask_token
          lowercase       :							List[str]									=				eos_token
          lowercase       :							Tuple									=				self.all_tokens
          self._create_trie(self.unique_no_split_tokens							)
   def      __magic_name__     (     self      ,       _a							):
          return self._id_to_token.get(_a      ,       self.unk_token							)
   def      __magic_name__     (     self      ,       _a							):
          return self._token_to_id.get(_a      ,       self._token_to_id.get(self.unk_token							)							)
   def      __magic_name__     (     self      ,       _a      ,       **_a							):
          return text.split()
   def      __magic_name__     (     self      ,       _a=False							):
          return len(self._id_to_token							)
   def      __magic_name__     (     self							):
          return {token: i for i, token in enumerate(self.all_tokens							)}
   def      __magic_name__     (     self      ,       _a							):
          return self._token_to_id.get(_a      ,       self._token_to_id.get(self.unk_token							)							)
   def      __magic_name__     (     self      ,       _a							):
          return self._id_to_token.get(_a      ,       self.unk_token							)
   def      __magic_name__     (     self      ,       _a      ,       _a = None							):
          lowercase       :							Optional[int]									=				[self.cls_token_id]
          lowercase       :							Dict									=				[self.eos_token_id]  # No sep token in ESM vocabulary
          if token_ids_a is None:
                 if self.eos_token_id is None:
                        return cls + token_ids_a
                 else:
                        return cls + token_ids_a + sep
          elif self.eos_token_id is None:
                 raise ValueError("Cannot tokenize multiple sequences when EOS token is not set!"							)
          return cls + token_ids_a + sep + token_ids_a + sep  # Multiple inputs always have an EOS token
   def      __magic_name__     (     self      ,       _a      ,       _a = None      ,       _a = False							):
          if already_has_special_tokens:
                 if token_ids_a is not None:
                        raise ValueError(
                            "You should not supply a second sequence if the provided sequence of "
                            "ids is already formatted with special tokens for the model."							)

                 return [1 if token in self.all_special_ids else 0 for token in token_ids_a]
          lowercase       :							Union[str, Any]									=				[1] + ([0] * len(_a							)) + [1]
          if token_ids_a is not None:
                 mask += [0] * len(_a							) + [1]
          return mask
   def      __magic_name__     (     self      ,       _a      ,       _a							):
          lowercase       :							Optional[Any]									=				os.path.join(_a      ,       (filename_prefix + "-" if filename_prefix else "") + "vocab.txt"							)
          with open(_a      ,       "w"							) as f:
                 f.write("\n".join(self.all_tokens							)							)
          return (vocab_file,)
   @property
   def      __magic_name__     (     self							):
          return self.get_vocab_size(with_added_tokens=_a							)

   def      __magic_name__     (     self      ,       _a      ,       _a = False							):
          return super()._add_tokens(_a      ,       special_tokens=_a							)


