

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

from ...utils import logging


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







def 			A_      (    _UpperCAmelCase				,	_UpperCAmelCase    ):
						SCREAMING_SNAKE_CASE_: Optional[Any]  		=       nn.functional.normalize(_UpperCAmelCase    )
						SCREAMING_SNAKE_CASE_: List[Any]  		=       nn.functional.normalize(_UpperCAmelCase    )
						return torch.mm(_UpperCAmelCase				,	normalized_text_embeds.t()    )



class   __lowercase				( UpperCAmelCase_	):




			"""simple docstring"""







			_UpperCAmelCase   :							str										=     CLIPConfig

			_UpperCAmelCase   :							Optional[int]										=     ['''CLIPEncoderLayer''']



			def __init__( self  :     List[Any]   ,						lowerCAmelCase__  :     CLIPConfig):
									super().__init__(lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_: str  		=       CLIPVisionModel(config.vision_config)
									SCREAMING_SNAKE_CASE_: List[str]  		=       nn.Linear(config.vision_config.hidden_size   ,						config.projection_dim   ,						bias=lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_: str  		=       nn.Parameter(torch.ones(17   ,						config.projection_dim)   ,						requires_grad=lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_: List[str]  		=       nn.Parameter(torch.ones(3   ,						config.projection_dim)   ,						requires_grad=lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_: str  		=       nn.Parameter(torch.ones(17)   ,						requires_grad=lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_: Tuple  		=       nn.Parameter(torch.ones(3)   ,						requires_grad=lowerCAmelCase__)



			@torch.no_grad()
			def        _SCREAMING_SNAKE_CASE			( self  :     int   ,						lowerCAmelCase__  :     List[Any]   ,						lowerCAmelCase__  :     int):
									SCREAMING_SNAKE_CASE_: List[Any]  		=       self.vision_model(lowerCAmelCase__)[1]  # pooled_output
									SCREAMING_SNAKE_CASE_: Tuple  		=       self.visual_projection(lowerCAmelCase__)

									# we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
									SCREAMING_SNAKE_CASE_: Dict  		=       cosine_distance(lowerCAmelCase__   ,						self.special_care_embeds).cpu().float().numpy()
									SCREAMING_SNAKE_CASE_: Optional[Any]  		=       cosine_distance(lowerCAmelCase__   ,						self.concept_embeds).cpu().float().numpy()

									SCREAMING_SNAKE_CASE_: Optional[int]  		=       []
									SCREAMING_SNAKE_CASE_: List[Any]  		=       image_embeds.shape[0]
									for i in range(lowerCAmelCase__):
															SCREAMING_SNAKE_CASE_: Dict  		=       {"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
															SCREAMING_SNAKE_CASE_: List[Any]  		=       0.0

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

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

															result.append(lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_: Tuple  		=       [len(res["bad_concepts"]) > 0 for res in result]

									return images, has_nsfw_concepts




			@torch.no_grad()
			def        _SCREAMING_SNAKE_CASE			( self  :     List[Any]   ,						lowerCAmelCase__  :     torch.FloatTensor   ,						lowerCAmelCase__  :     torch.FloatTensor):
									SCREAMING_SNAKE_CASE_: Optional[Any]  		=       self.vision_model(lowerCAmelCase__)[1]  # pooled_output
									SCREAMING_SNAKE_CASE_: Tuple  		=       self.visual_projection(lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_: Any  		=       cosine_distance(lowerCAmelCase__   ,						self.special_care_embeds)
									SCREAMING_SNAKE_CASE_: str  		=       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
									SCREAMING_SNAKE_CASE_: Dict  		=       0.0

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

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

									return images, has_nsfw_concepts








import json
from typing import List, Optional, Tuple

from tokenizers import normalizers
from tokenizers.pre_tokenizers import BertPreTokenizer, PreTokenizer

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_roformer import RoFormerTokenizer
from .tokenization_utils import JiebaPreTokenizer


lowercase__       =logging.get_logger(__name__)

lowercase__       ={'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'}

lowercase__       ={
    'vocab_file': {
        'junnyu/roformer_chinese_small': 'https://huggingface.co/junnyu/roformer_chinese_small/resolve/main/vocab.txt',
        'junnyu/roformer_chinese_base': 'https://huggingface.co/junnyu/roformer_chinese_base/resolve/main/vocab.txt',
        'junnyu/roformer_chinese_char_small': (
            'https://huggingface.co/junnyu/roformer_chinese_char_small/resolve/main/vocab.txt'
        ),
        'junnyu/roformer_chinese_char_base': (
            'https://huggingface.co/junnyu/roformer_chinese_char_base/resolve/main/vocab.txt'
        ),
        'junnyu/roformer_small_discriminator': (
            'https://huggingface.co/junnyu/roformer_small_discriminator/resolve/main/vocab.txt'
        ),
        'junnyu/roformer_small_generator': (
            'https://huggingface.co/junnyu/roformer_small_generator/resolve/main/vocab.txt'
        ),
    }
}

lowercase__       ={
    'junnyu/roformer_chinese_small': 1536,
    'junnyu/roformer_chinese_base': 1536,
    'junnyu/roformer_chinese_char_small': 512,
    'junnyu/roformer_chinese_char_base': 512,
    'junnyu/roformer_small_discriminator': 128,
    'junnyu/roformer_small_generator': 128,
}


lowercase__       ={
    'junnyu/roformer_chinese_small': {'do_lower_case': True},
    'junnyu/roformer_chinese_base': {'do_lower_case': True},
    'junnyu/roformer_chinese_char_small': {'do_lower_case': True},
    'junnyu/roformer_chinese_char_base': {'do_lower_case': True},
    'junnyu/roformer_small_discriminator': {'do_lower_case': True},
    'junnyu/roformer_small_generator': {'do_lower_case': True},
}



class 		UpperCamelCase__  (       __lowercase    ):
   _SCREAMING_SNAKE_CASE      :							Optional[Any]	       =      VOCAB_FILES_NAMES
   _SCREAMING_SNAKE_CASE      :							List[Any]	       =      PRETRAINED_VOCAB_FILES_MAP
   _SCREAMING_SNAKE_CASE      :							List[str]	       =      PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
   _SCREAMING_SNAKE_CASE      :							Optional[Any]	       =      PRETRAINED_INIT_CONFIGURATION
   _SCREAMING_SNAKE_CASE      :							Optional[int]	       =      RoFormerTokenizer
   def __init__(self :					List[str]					,				snake_case_ :					Optional[int]=None					,				snake_case_ :					str=None					,				snake_case_ :					Optional[Any]=True					,				snake_case_ :					str="[UNK]"					,				snake_case_ :					Dict="[SEP]"					,				snake_case_ :					Any="[PAD]"					,				snake_case_ :					str="[CLS]"					,				snake_case_ :					List[Any]="[MASK]"					,				snake_case_ :					Any=True					,				snake_case_ :					List[str]=None					,				**snake_case_ :					Optional[int]					,				):
    super().__init__(
        snake_case_					,				tokenizer_file=snake_case_					,				do_lower_case=snake_case_					,				unk_token=snake_case_					,				sep_token=snake_case_					,				pad_token=snake_case_					,				cls_token=snake_case_					,				mask_token=snake_case_					,				tokenize_chinese_chars=snake_case_					,				strip_accents=snake_case_					,				**snake_case_					,				)

    __a :  Optional[int]						=  json.loads(self.backend_tokenizer.normalizer.__getstate__()   )
    if (
        pre_tok_state.get('''lowercase'''					,				snake_case_   ) != do_lower_case
        or pre_tok_state.get('''strip_accents'''					,				snake_case_   ) != strip_accents
    ):
     __a :  List[str]						=  getattr(snake_case_					,				pre_tok_state.pop('''type'''   )   )
     __a :  Optional[Any]						=  do_lower_case
     __a :  Optional[int]						=  strip_accents
     __a :  List[str]						=  pre_tok_class(**snake_case_   )

    __a :  Optional[Any]						=  do_lower_case
   def __getstate__(self :					Union[str, Any]   ):
    __a :  Any						=  self.__dict__.copy()
    __a :  Union[str, Any]						=  BertPreTokenizer()
    return state
   def __setstate__(self :					Tuple					,				snake_case_ :					Optional[Any]   ):
    __a :  Dict						=  d
    __a :  str						=  self.__dict__['''_tokenizer'''].get_vocab()
    __a :  Optional[Any]						=  PreTokenizer.custom(JiebaPreTokenizer(snake_case_   )   )
   def       lowerCAmelCase			(self :					Optional[int]					,				snake_case_ :					List[Any]					,				snake_case_ :					Optional[Any]=None   ):
    __a :  Optional[int]						=  [self.cls_token_id] + token_ids_a + [self.sep_token_id]

    if token_ids_a:
     output += token_ids_a + [self.sep_token_id]

    return output
   def       lowerCAmelCase			(self :					Optional[int]					,				snake_case_ :					List[int]					,				snake_case_ :					Optional[List[int]] = None   ):
    __a :  int						=  [self.sep_token_id]
    __a :  List[Any]						=  [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   ) * [0] + len(token_ids_a + sep   ) * [1]
   def       lowerCAmelCase			(self :					int					,				snake_case_ :					str					,				snake_case_ :					Optional[str] = None   ):
    __a :  Optional[Any]						=  self._tokenizer.model.save(snake_case_					,				name=snake_case_   )
    return tuple(snake_case_   )







   def       lowerCAmelCase			(self :					Dict					,				snake_case_ :					Dict					,				snake_case_ :					Tuple=None					,				snake_case_ :					Optional[Any]=None					,				snake_case_ :					Union[str, Any]=False					,				**snake_case_ :					Tuple					,				):
    __a :  List[str]						=  BertPreTokenizer()
    return super().save_pretrained(snake_case_					,				snake_case_					,				snake_case_					,				snake_case_					,				**snake_case_   )
