



import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_convbert import ConvBertTokenizer


_lowercase     							=						logging.get_logger(__name__)

_lowercase     							=						{'''vocab_file''': '''vocab.txt'''}

_lowercase     							=						{
    '''vocab_file''': {
        '''YituTech/conv-bert-base''': '''https://huggingface.co/YituTech/conv-bert-base/resolve/main/vocab.txt''',
        '''YituTech/conv-bert-medium-small''': (
            '''https://huggingface.co/YituTech/conv-bert-medium-small/resolve/main/vocab.txt'''
        ),
        '''YituTech/conv-bert-small''': '''https://huggingface.co/YituTech/conv-bert-small/resolve/main/vocab.txt''',
    }
}

_lowercase     							=						{
    '''YituTech/conv-bert-base''': 5_12,
    '''YituTech/conv-bert-medium-small''': 5_12,
    '''YituTech/conv-bert-small''': 5_12,
}


_lowercase     							=						{
    '''YituTech/conv-bert-base''': {'''do_lower_case''': True},
    '''YituTech/conv-bert-medium-small''': {'''do_lower_case''': True},
    '''YituTech/conv-bert-small''': {'''do_lower_case''': True},
}
class 				__A					(__lowerCAmelCase      ):
			UpperCamelCase					:Union[str, Any]     =			VOCAB_FILES_NAMES
			UpperCamelCase					:Union[str, Any]     =			PRETRAINED_VOCAB_FILES_MAP
			UpperCamelCase					:int     =			PRETRAINED_INIT_CONFIGURATION
			UpperCamelCase					:List[Any]     =			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			UpperCamelCase					:List[Any]     =			ConvBertTokenizer





			def __init__(self							,    __magic_name__=None							,    __magic_name__=None							,    __magic_name__=True							,    __magic_name__="[UNK]"							,    __magic_name__="[SEP]"							,    __magic_name__="[PAD]"							,    __magic_name__="[CLS]"							,    __magic_name__="[MASK]"							,    __magic_name__=True							,    __magic_name__=None							,    **__magic_name__							,    ):
						super().__init__(
						    lowerCAmelCase_							,    tokenizer_file=lowerCAmelCase_							,    do_lower_case=lowerCAmelCase_							,    unk_token=lowerCAmelCase_							,    sep_token=lowerCAmelCase_							,    pad_token=lowerCAmelCase_							,    cls_token=lowerCAmelCase_							,    mask_token=lowerCAmelCase_							,    tokenize_chinese_chars=lowerCAmelCase_							,    strip_accents=lowerCAmelCase_							,    **lowerCAmelCase_							,    )

						lowerCamelCase__ :      str								=		json.loads(self.backend_tokenizer.normalizer.__getstate__()				)
						if (
						    normalizer_state.get("""lowercase"""							,    lowerCAmelCase_				) != do_lower_case
						    or normalizer_state.get("""strip_accents"""							,    lowerCAmelCase_				) != strip_accents
						    or normalizer_state.get("""handle_chinese_chars"""							,    lowerCAmelCase_				) != tokenize_chinese_chars
						):
									lowerCamelCase__ :      Optional[int]								=		getattr(lowerCAmelCase_							,    normalizer_state.pop("""type"""				)				)
									lowerCamelCase__ :      Dict								=		do_lower_case
									lowerCamelCase__ :      int								=		strip_accents
									lowerCamelCase__ :      Optional[Any]								=		tokenize_chinese_chars
									lowerCamelCase__ :      Optional[Any]								=		normalizer_class(**lowerCAmelCase_				)

						lowerCamelCase__ :      Dict								=		do_lower_case





			def        _snake_case    (self							,    __magic_name__							,    __magic_name__=None				):
						lowerCamelCase__ :      List[Any]								=		[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        _snake_case    (self							,    __magic_name__							,    __magic_name__ = None				):
						lowerCamelCase__ :      Any								=		[self.sep_token_id]
						lowerCamelCase__ :      Optional[int]								=		[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        _snake_case    (self							,    __magic_name__							,    __magic_name__ = None				):
						lowerCamelCase__ :      Optional[Any]								=		self._tokenizer.model.save(lowerCAmelCase_							,    name=lowerCAmelCase_				)
						return tuple(lowerCAmelCase_				)








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

from ...utils import logging


_lowercase     							=						logging.get_logger(__name__)






def      _A				(UpperCamelCase		:      Dict      ,			UpperCamelCase		:      Optional[int] )  ->Tuple:



			'''simple docstring'''







			lowerCamelCase__ :      List[str]								=		nn.functional.normalize(UpperCamelCase )
			lowerCamelCase__ :      Tuple								=		nn.functional.normalize(UpperCamelCase )
			return torch.mm(UpperCamelCase      ,			normalized_text_embeds.t() )
class 				__A					( A_      ):
			UpperCamelCase					:Optional[Any]     =			CLIPConfig

			UpperCamelCase					:Optional[Any]     =			['''CLIPEncoderLayer''']





			def __init__(self							,    __magic_name__				):
						super().__init__(__magic_name__				)

						lowerCamelCase__ :      Union[str, Any]								=		CLIPVisionModel(config.vision_config				)
						lowerCamelCase__ :      Any								=		nn.Linear(config.vision_config.hidden_size							,    config.projection_dim							,    bias=__magic_name__				)

						lowerCamelCase__ :      Any								=		nn.Parameter(torch.ones(17							,    config.projection_dim				)							,    requires_grad=__magic_name__				)
						lowerCamelCase__ :      Union[str, Any]								=		nn.Parameter(torch.ones(3							,    config.projection_dim				)							,    requires_grad=__magic_name__				)

						lowerCamelCase__ :      Any								=		nn.Parameter(torch.ones(17				)							,    requires_grad=__magic_name__				)
						lowerCamelCase__ :      Optional[Any]								=		nn.Parameter(torch.ones(3				)							,    requires_grad=__magic_name__				)





			@torch.no_grad()
			def        _snake_case    (self							,    __magic_name__							,    __magic_name__				):
						lowerCamelCase__ :      Any								=		self.vision_model(__magic_name__				)[1]  # pooled_output
						lowerCamelCase__ :      Tuple								=		self.visual_projection(__magic_name__				)

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

						lowerCamelCase__ :      int								=		[]
						lowerCamelCase__ :      str								=		image_embeds.shape[0]
						for i in range(__magic_name__				):
									lowerCamelCase__ :      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
									lowerCamelCase__ :      Optional[Any]								=		0.0

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

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

									result.append(__magic_name__				)

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

						return images, has_nsfw_concepts





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

						lowerCamelCase__ :      Optional[Any]								=		cosine_distance(__magic_name__							,    self.special_care_embeds				)
						lowerCamelCase__ :      Dict								=		cosine_distance(__magic_name__							,    self.concept_embeds				)

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

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

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

						return images, has_nsfw_concepts



