






'''simple docstring'''




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

from ...utils import logging


lowerCamelCase_							      =      logging.get_logger(__name__)

def       __lowercase		(    __lowercase     ,						__lowercase ) ->		Any:

      '''simple docstring'''


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


class      _UpperCAmelCase    (  UpperCamelCase_		):




   """simple docstring"""

   snake_case					       =		CLIPConfig

   snake_case					       =		['''CLIPEncoderLayer''']

   def __init__(	self				:			Optional[Any]   ,   __UpperCAmelCase				:			Any	):





         '''simple docstring'''





         super().__init__(__UpperCamelCase	)

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

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

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

   @torch.no_grad()
   def 		lowerCAmelCase				(	self				:			Tuple   ,   __UpperCAmelCase				:			List[str]   ,   __UpperCAmelCase				:			int	):





         '''simple docstring'''





         _A      =			self.vision_model(__UpperCamelCase	)[1]  # pooled_output
         _A      =			self.visual_projection(__UpperCamelCase	)

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

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

               result.append(__UpperCamelCase	)

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

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

         if any(__UpperCamelCase	):
               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 		lowerCAmelCase				(	self				:			Any   ,   __UpperCAmelCase				:			Dict   ,   __UpperCAmelCase				:			Any	):





         '''simple docstring'''





         _A      =			self.vision_model(__UpperCamelCase	)[1]  # pooled_output
         _A      =			self.visual_projection(__UpperCamelCase	)

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

         _A      =			0.0  # black image

         return images, has_nsfw_concepts













'''simple docstring'''




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

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


if is_sentencepiece_available():
    from .tokenization_reformer import ReformerTokenizer
else:
    lowerCamelCase_							      =      None


lowerCamelCase_							      =      logging.get_logger(__name__)


lowerCamelCase_							      =      '''▁'''

lowerCamelCase_							      =      {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''}

lowerCamelCase_							      =      {
    '''vocab_file''': {
        '''google/reformer-crime-and-punishment''': (
            '''https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/spiece.model'''
        )
    },
    '''tokenizer_file''': {
        '''google/reformer-crime-and-punishment''': (
            '''https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/tokenizer.json'''
        )
    },
}

lowerCamelCase_							      =      {
    '''google/reformer-crime-and-punishment''': 52_42_88,
}

class      _UpperCAmelCase    (  snake_case_		):




   """simple docstring"""

   snake_case					       =		VOCAB_FILES_NAMES
   snake_case					       =		PRETRAINED_VOCAB_FILES_MAP
   snake_case					       =		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
   snake_case					       =		['''input_ids''', '''attention_mask''']
   snake_case					       =		ReformerTokenizer

   def __init__(	self				:			List[str]   ,   __UpperCAmelCase				:			List[str]=None   ,   __UpperCAmelCase				:			Any=None   ,   __UpperCAmelCase				:			List[Any]="</s>"   ,   __UpperCAmelCase				:			Optional[Any]="<unk>"   ,   __UpperCAmelCase				:			List[Any]=[]   ,   **__UpperCAmelCase				:			int   ,   ):





         '''simple docstring'''





         super().__init__(
             __UpperCAmelCase   ,   tokenizer_file=__UpperCAmelCase   ,   eos_token=__UpperCAmelCase   ,   unk_token=__UpperCAmelCase   ,   additional_special_tokens=__UpperCAmelCase   ,   **__UpperCAmelCase   ,   )

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

   def 		lowerCAmelCase				(	self				:			Any   ,   __UpperCAmelCase				:			str   ,   __UpperCAmelCase				:			Optional[str] = None	):





         '''simple docstring'''





         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(__UpperCAmelCase	):
               logger.error(f'''Vocabulary path ({save_directory}) should be a directory'''	)
               return
         _A      =			os.path.join(
             __UpperCAmelCase   ,   (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]	)

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

         return (out_vocab_file,)





