

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

from ...utils import logging


lowerCAmelCase						:   List[Any]										=    logging.get_logger(__name__)





def 			_lowercase    (      __UpperCamelCase     :		str   ,       __UpperCamelCase     :		int ):
      snake_case__					=   nn.functional.normalize(__UpperCamelCase )
      snake_case__					=   nn.functional.normalize(__UpperCamelCase )
      return torch.mm(__UpperCamelCase   ,       normalized_text_embeds.t() )
class     SCREAMING_SNAKE_CASE__     (						__a		):





       '''simple docstring'''




       UpperCamelCase__ :     Union[str, Any]				    =       CLIPConfig

       UpperCamelCase__ :     Any				    =       ['''CLIPEncoderLayer''']

       def __init__(						self       :						Optional[int]    ,		lowerCAmelCase__       :						CLIPConfig							)			->    Optional[int]:
             super().__init__(lowerCAmelCase__							)

             snake_case__					=   CLIPVisionModel(config.vision_config							)
             snake_case__					=   nn.Linear(config.vision_config.hidden_size    ,		config.projection_dim    ,		bias=lowerCAmelCase__							)

             snake_case__					=   nn.Parameter(torch.ones(17    ,		config.projection_dim							)    ,		requires_grad=lowerCAmelCase__							)
             snake_case__					=   nn.Parameter(torch.ones(3    ,		config.projection_dim							)    ,		requires_grad=lowerCAmelCase__							)

             snake_case__					=   nn.Parameter(torch.ones(17							)    ,		requires_grad=lowerCAmelCase__							)
             snake_case__					=   nn.Parameter(torch.ones(3							)    ,		requires_grad=lowerCAmelCase__							)

       @torch.no_grad()
       def UpperCAmelCase_						(						self       :						Any    ,		lowerCAmelCase__       :						int    ,		lowerCAmelCase__       :						List[str]							)			->    str:
             snake_case__					=   self.vision_model(lowerCAmelCase__							)[1]  # pooled_output
             snake_case__					=   self.visual_projection(lowerCAmelCase__							)

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

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

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

                   for concept_idx in range(len(cos_dist[0]							)							):
                         snake_case__					=   cos_dist[i][concept_idx]
                         snake_case__					=   self.concept_embeds_weights[concept_idx].item()
                         snake_case__					=   round(concept_cos - concept_threshold + adjustment    ,		3							)
                         if result_img["concept_scores"][concept_idx] > 0:
                               result_img["bad_concepts"].append(lowerCAmelCase__							)

                   result.append(lowerCAmelCase__							)

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

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

             if any(lowerCAmelCase__							):
                   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 UpperCAmelCase_						(						self       :						Tuple    ,		lowerCAmelCase__       :						torch.FloatTensor    ,		lowerCAmelCase__       :						torch.FloatTensor							)			->    int:
             snake_case__					=   self.vision_model(lowerCAmelCase__							)[1]  # pooled_output
             snake_case__					=   self.visual_projection(lowerCAmelCase__							)

             snake_case__					=   cosine_distance(lowerCAmelCase__    ,		self.special_care_embeds							)
             snake_case__					=   cosine_distance(lowerCAmelCase__    ,		self.concept_embeds							)

             # increase this value to create a stronger `nsfw` filter
             # at the cost of increasing the possibility of filtering benign images
             snake_case__					=   0.0

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

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

             snake_case__					=   0.0  # black image

             return images, has_nsfw_concepts









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						:   Union[str, Any]										=    None


lowerCAmelCase						:   str										=    logging.get_logger(__name__)


lowerCAmelCase						:   Any										=    '''▁'''

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

lowerCAmelCase						:   Optional[Any]										=    {
    '''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						:   Union[str, Any]										=    {
    '''google/reformer-crime-and-punishment''': 52_4288,
}
class     SCREAMING_SNAKE_CASE__     (						__a		):





       '''simple docstring'''




       UpperCamelCase__ :     Union[str, Any]				    =       VOCAB_FILES_NAMES
       UpperCamelCase__ :     str				    =       PRETRAINED_VOCAB_FILES_MAP
       UpperCamelCase__ :     str				    =       PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
       UpperCamelCase__ :     str				    =       ['''input_ids''', '''attention_mask''']
       UpperCamelCase__ :     Optional[Any]				    =       ReformerTokenizer

       def __init__(						self       :						List[str]    ,		lowerCAmelCase__       :						Tuple=None    ,		lowerCAmelCase__       :						Optional[Any]=None    ,		lowerCAmelCase__       :						Dict="</s>"    ,		lowerCAmelCase__       :						Tuple="<unk>"    ,		lowerCAmelCase__       :						Dict=[]    ,		**lowerCAmelCase__       :						Optional[Any]    ,		)			->    Dict:
             super().__init__(
                 lowerCAmelCase__    ,		tokenizer_file=lowerCAmelCase__    ,		eos_token=lowerCAmelCase__    ,		unk_token=lowerCAmelCase__    ,		additional_special_tokens=lowerCAmelCase__    ,		**lowerCAmelCase__    ,		)

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

       def UpperCAmelCase_						(						self       :						int    ,		lowerCAmelCase__       :						str    ,		lowerCAmelCase__       :						Optional[str] = None							)			->    Tuple[str]:
             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(lowerCAmelCase__							):
                   logger.error(f'''Vocabulary path ({save_directory}) should be a directory'''							)
                   return
             snake_case__					=   os.path.join(
                 lowerCAmelCase__    ,		(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]							)

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

             return (out_vocab_file,)






