


'''simple docstring'''

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

from ...utils import logging


lowerCamelCase							:   int  						=	logging.get_logger(__name__)

def _SCREAMING_SNAKE_CASE      (A  , A       )       ->       Optional[int]:






       """simple docstring"""







       lowercase__      			= nn.functional.normalize(A       )
       lowercase__      			= nn.functional.normalize(A       )
       return torch.mm(A  , normalized_text_embeds.t()       )

class   __lowerCAmelCase						(lowercase_     ):





      '''simple docstring'''






      lowerCAmelCase__					:     Any  					=							CLIPConfig

      lowerCAmelCase__					:     Union[str, Any]  					=							["""CLIPEncoderLayer"""]



      def __init__(self    :    List[str]   ,						UpperCamelCase    :    CLIPConfig ):

             '''simple docstring'''







             super().__init__(UpperCamelCase )

             lowercase__      			= CLIPVisionModel(config.vision_config )
             lowercase__      			= nn.Linear(config.vision_config.hidden_size   ,						config.projection_dim   ,						bias=UpperCamelCase )

             lowercase__      			= nn.Parameter(torch.ones(17   ,						config.projection_dim )   ,						requires_grad=UpperCamelCase )
             lowercase__      			= nn.Parameter(torch.ones(3   ,						config.projection_dim )   ,						requires_grad=UpperCamelCase )

             lowercase__      			= nn.Parameter(torch.ones(17 )   ,						requires_grad=UpperCamelCase )
             lowercase__      			= nn.Parameter(torch.ones(3 )   ,						requires_grad=UpperCamelCase )



      @torch.no_grad()
      def       UpperCamelCase__		(self    :    Union[str, Any]   ,						UpperCamelCase    :    Dict   ,						UpperCamelCase    :    int ):

             '''simple docstring'''







             lowercase__      			= self.vision_model(UpperCamelCase )[1]  # pooled_output
             lowercase__      			= self.visual_projection(UpperCamelCase )

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

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

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

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

                    result.append(UpperCamelCase )

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

             return images, has_nsfw_concepts






      @torch.no_grad()
      def       UpperCamelCase__		(self    :    Tuple   ,						UpperCamelCase    :    torch.FloatTensor   ,						UpperCamelCase    :    torch.FloatTensor ):

             '''simple docstring'''







             lowercase__      			= self.vision_model(UpperCamelCase )[1]  # pooled_output
             lowercase__      			= self.visual_projection(UpperCamelCase )

             lowercase__      			= cosine_distance(UpperCamelCase   ,						self.special_care_embeds )
             lowercase__      			= 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
             lowercase__      			= 0.0

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

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

             return images, has_nsfw_concepts





'''simple docstring'''




from __future__ import annotations

from typing import Dict

from ...configuration_utils import PretrainedConfig


__lowercase          =  {
    '''susnato/ernie-m-base_pytorch''': '''https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/config.json''',
    '''susnato/ernie-m-large_pytorch''': '''https://huggingface.co/susnato/ernie-m-large_pytorch/blob/main/config.json''',
}






class 				a__(					lowerCAmelCase__	):


			'''simple docstring'''






			UpperCAmelCase_			:     List[str]      =       '''ernie_m'''
			UpperCAmelCase_			:     Dict[str, str]      =       {"dropout": "classifier_dropout", "num_classes": "num_labels"}



			def __init__(   self       ,    __lowerCAmelCase = 250002       ,    __lowerCAmelCase = 768       ,    __lowerCAmelCase = 12       ,    __lowerCAmelCase = 12       ,    __lowerCAmelCase = 3072       ,    __lowerCAmelCase = "gelu"       ,    __lowerCAmelCase = 0.1       ,    __lowerCAmelCase = 0.1       ,    __lowerCAmelCase = 514       ,    __lowerCAmelCase = 0.02       ,    __lowerCAmelCase = 1       ,    __lowerCAmelCase = 1E-0_5       ,    __lowerCAmelCase=None       ,    __lowerCAmelCase=False       ,    __lowerCAmelCase=0.0       ,    **__lowerCAmelCase       ,    ):




					"""simple docstring"""


					super().__init__(pad_token_id=__lowerCAmelCase       ,    **__lowerCAmelCase)
					lowerCAmelCase   					=  vocab_size
					lowerCAmelCase   					=  hidden_size
					lowerCAmelCase   					=  num_hidden_layers
					lowerCAmelCase   					=  num_attention_heads
					lowerCAmelCase   					=  intermediate_size
					lowerCAmelCase   					=  hidden_act
					lowerCAmelCase   					=  hidden_dropout_prob
					lowerCAmelCase   					=  attention_probs_dropout_prob
					lowerCAmelCase   					=  max_position_embeddings
					lowerCAmelCase   					=  initializer_range
					lowerCAmelCase   					=  layer_norm_eps
					lowerCAmelCase   					=  classifier_dropout
					lowerCAmelCase   					=  is_decoder
					lowerCAmelCase   					=  act_dropout




