




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

from ...utils import logging


a_ 		=  logging.get_logger(__name__)




def        __SCREAMING_SNAKE_CASE (	lowercase_       ,  lowercase_   )							->						Optional[int]:

    """simple docstring"""


    __UpperCamelCase			=    nn.functional.normalize(lowercase_   )
    __UpperCamelCase			=    nn.functional.normalize(lowercase_   )
    return torch.mm(lowercase_       ,  normalized_text_embeds.t()   )


class 			_lowerCamelCase    (      _SCREAMING_SNAKE_CASE ):

      """simple docstring"""

      lowerCAmelCase__      :			Tuple     =       CLIPConfig

      lowerCAmelCase__      :			Dict     =       ["CLIPEncoderLayer"]







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

          __UpperCamelCase			=    CLIPVisionModel(config.vision_config					)
          __UpperCamelCase			=    nn.Linear(config.vision_config.hidden_size			,							config.projection_dim			,							bias=snake_case					)

          __UpperCamelCase			=    nn.Parameter(torch.ones(17			,							config.projection_dim					)			,							requires_grad=snake_case					)
          __UpperCamelCase			=    nn.Parameter(torch.ones(3			,							config.projection_dim					)			,							requires_grad=snake_case					)

          __UpperCamelCase			=    nn.Parameter(torch.ones(17					)			,							requires_grad=snake_case					)
          __UpperCamelCase			=    nn.Parameter(torch.ones(3					)			,							requires_grad=snake_case					)







      @torch.no_grad()
      def    snake_case						(  self       :			List[Any]			,							snake_case       :			Tuple			,							snake_case       :			List[str]					):
          __UpperCamelCase			=    self.vision_model(snake_case					)[1]  # pooled_output
          __UpperCamelCase			=    self.visual_projection(snake_case					)

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

          __UpperCamelCase			=    []
          __UpperCamelCase			=    image_embeds.shape[0]
          for i in range(snake_case					):
              __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(snake_case					)

              result.append(snake_case					)

          __UpperCamelCase			=    [len(res['''bad_concepts''']					) > 0 for res in result]

          for idx, has_nsfw_concept in enumerate(snake_case					):
              if has_nsfw_concept:
                  if torch.is_tensor(snake_case					) or torch.is_tensor(images[0]					):
                      __UpperCamelCase			=    torch.zeros_like(images[idx]					)  # black image
                  else:
                      __UpperCamelCase			=    np.zeros(images[idx].shape					)  # black image

          if any(snake_case					):
              logger.warning(
                  '''Potential NSFW content was detected in one or more images. A black image will be returned instead.'''
                  ''' Try again with a different prompt and/or seed.'''					)

          return images, has_nsfw_concepts







      @torch.no_grad()
      def    snake_case						(  self       :			int			,							snake_case       :			torch.FloatTensor			,							snake_case       :			torch.FloatTensor					):
          __UpperCamelCase			=    self.vision_model(snake_case					)[1]  # pooled_output
          __UpperCamelCase			=    self.visual_projection(snake_case					)

          __UpperCamelCase			=    cosine_distance(snake_case			,							self.special_care_embeds					)
          __UpperCamelCase			=    cosine_distance(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
          __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					)

          __UpperCamelCase			=    0.0  # black image

          return images, has_nsfw_concepts












import os
from shutil import copyfile
from typing import List, Optional, Tuple

from ...tokenization_utils import AddedToken
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
 from .tokenization_xlm_roberta import XLMRobertaTokenizer
else:
 a_ 		=  None


a_ 		=  logging.get_logger(__name__)

a_ 		=  {"vocab_file": "sentencepiece.bpe.model", "tokenizer_file": "tokenizer.json"}

a_ 		=  {
    "vocab_file": {
        "xlm-roberta-base": "https://huggingface.co/xlm-roberta-base/resolve/main/sentencepiece.bpe.model",
        "xlm-roberta-large": "https://huggingface.co/xlm-roberta-large/resolve/main/sentencepiece.bpe.model",
        "xlm-roberta-large-finetuned-conll02-dutch": (
            "https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/sentencepiece.bpe.model"
        ),
        "xlm-roberta-large-finetuned-conll02-spanish": (
            "https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/sentencepiece.bpe.model"
        ),
        "xlm-roberta-large-finetuned-conll03-english": (
            "https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/sentencepiece.bpe.model"
        ),
        "xlm-roberta-large-finetuned-conll03-german": (
            "https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/sentencepiece.bpe.model"
        ),
    },
    "tokenizer_file": {
        "xlm-roberta-base": "https://huggingface.co/xlm-roberta-base/resolve/main/tokenizer.json",
        "xlm-roberta-large": "https://huggingface.co/xlm-roberta-large/resolve/main/tokenizer.json",
        "xlm-roberta-large-finetuned-conll02-dutch": (
            "https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/tokenizer.json"
        ),
        "xlm-roberta-large-finetuned-conll02-spanish": (
            "https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/tokenizer.json"
        ),
        "xlm-roberta-large-finetuned-conll03-english": (
            "https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/tokenizer.json"
        ),
        "xlm-roberta-large-finetuned-conll03-german": (
            "https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/tokenizer.json"
        ),
    },
}

a_ 		=  {
    "xlm-roberta-base": 512,
    "xlm-roberta-large": 512,
    "xlm-roberta-large-finetuned-conll02-dutch": 512,
    "xlm-roberta-large-finetuned-conll02-spanish": 512,
    "xlm-roberta-large-finetuned-conll03-english": 512,
    "xlm-roberta-large-finetuned-conll03-german": 512,
}

class 			_lowerCamelCase    (      _SCREAMING_SNAKE_CASE ):

      """simple docstring"""

      lowerCAmelCase__      :			List[Any]     =       VOCAB_FILES_NAMES
      lowerCAmelCase__      :			List[Any]     =       PRETRAINED_VOCAB_FILES_MAP
      lowerCAmelCase__      :			Optional[Any]     =       PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      lowerCAmelCase__      :			Optional[int]     =       ["input_ids", "attention_mask"]
      lowerCAmelCase__      :			Union[str, Any]     =       XLMRobertaTokenizer







      def __init__(  self       :			str			,							snake_case       :			Union[str, Any]=None			,							snake_case       :			Any=None			,							snake_case       :			Union[str, Any]="<s>"			,							snake_case       :			Tuple="</s>"			,							snake_case       :			List[Any]="</s>"			,							snake_case       :			Any="<s>"			,							snake_case       :			str="<unk>"			,							snake_case       :			int="<pad>"			,							snake_case       :			Any="<mask>"			,							**snake_case       :			str			,							):
          # Mask token behave like a normal word, i.e. include the space before it
          __UpperCamelCase			=    AddedToken(snake_case			,							lstrip=snake_case			,							rstrip=snake_case					) if isinstance(snake_case			,							snake_case					) else mask_token

          super().__init__(
              snake_case			,							tokenizer_file=snake_case			,							bos_token=snake_case			,							eos_token=snake_case			,							sep_token=snake_case			,							cls_token=snake_case			,							unk_token=snake_case			,							pad_token=snake_case			,							mask_token=snake_case			,							**snake_case			,							)

          __UpperCamelCase			=    vocab_file
          __UpperCamelCase			=    False if not self.vocab_file else True







      def    snake_case						(  self       :			List[Any]			,							snake_case       :			List[int]			,							snake_case       :			Optional[List[int]] = None					):

          if token_ids_a is None:
              return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
          __UpperCamelCase			=    [self.cls_token_id]
          __UpperCamelCase			=    [self.sep_token_id]
          return cls + token_ids_a + sep + sep + token_ids_a + sep







      def    snake_case						(  self       :			Any			,							snake_case       :			List[int]			,							snake_case       :			Optional[List[int]] = None					):
          __UpperCamelCase			=    [self.sep_token_id]
          __UpperCamelCase			=    [self.cls_token_id]

          if token_ids_a is None:
              return len(cls + token_ids_a + sep					) * [0]
          return len(cls + token_ids_a + sep + sep + token_ids_a + sep					) * [0]







      def    snake_case						(  self       :			Dict			,							snake_case       :			str			,							snake_case       :			Optional[str] = None					):
          if not self.can_save_slow_tokenizer:
              raise ValueError(
                  '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow '''
                  '''tokenizer.'''					)

          if not os.path.isdir(snake_case					):
              logger.error(F"Vocabulary path ({save_directory}) should be a directory."					)
              return
          __UpperCamelCase			=    os.path.join(
              snake_case			,							(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']					)

          if os.path.abspath(self.vocab_file					) != os.path.abspath(snake_case					):
              copyfile(self.vocab_file			,							snake_case					)

          return (out_vocab_file,)






