



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

from ...utils import logging


a_								=    logging.get_logger(__name__)





def 						a__	(						_UpperCamelCase	:       int       ,_UpperCamelCase	:       List[Any]							):
   __lowerCamelCase  							=			nn.functional.normalize(_UpperCamelCase							)
   __lowerCamelCase  							=			nn.functional.normalize(_UpperCamelCase							)
   return torch.mm(_UpperCamelCase       ,normalized_text_embeds.t()							)





class        __lowerCAmelCase   (		lowercase_      ):
 lowerCAmelCase__				    =     CLIPConfig

 lowerCAmelCase__				    =     ["""CLIPEncoderLayer"""]




 def __init__(  self		, __UpperCAmelCase ):



    '''simple docstring'''
    super().__init__(__UpperCAmelCase )

    __lowerCamelCase  							=			CLIPVisionModel(config.vision_config )
    __lowerCamelCase  							=			nn.Linear(config.vision_config.hidden_size		, config.projection_dim		, bias=__UpperCAmelCase )

    __lowerCamelCase  							=			nn.Parameter(torch.ones(17		, config.projection_dim )		, requires_grad=__UpperCAmelCase )
    __lowerCamelCase  							=			nn.Parameter(torch.ones(3		, config.projection_dim )		, requires_grad=__UpperCAmelCase )

    __lowerCamelCase  							=			nn.Parameter(torch.ones(17 )		, requires_grad=__UpperCAmelCase )
    __lowerCamelCase  							=			nn.Parameter(torch.ones(3 )		, requires_grad=__UpperCAmelCase )




 @torch.no_grad()
 def 				lowerCamelCase					(  self		, __UpperCAmelCase		, __UpperCAmelCase ):



    '''simple docstring'''
    __lowerCamelCase  							=			self.vision_model(__UpperCAmelCase )[1]  # pooled_output
    __lowerCamelCase  							=			self.visual_projection(__UpperCAmelCase )

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

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

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

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

       result.append(__UpperCAmelCase )

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

    return images, has_nsfw_concepts




 @torch.no_grad()
 def 				lowerCamelCase					(  self		, __UpperCAmelCase		, __UpperCAmelCase ):



    '''simple docstring'''
    __lowerCamelCase  							=			self.vision_model(__UpperCAmelCase )[1]  # pooled_output
    __lowerCamelCase  							=			self.visual_projection(__UpperCAmelCase )

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

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

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

    return images, has_nsfw_concepts










'''simple docstring'''

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCamelCase							:   int  						=	logging.get_logger(__name__)

lowerCamelCase							:   List[Any]  						=	{
    'google/realm-cc-news-pretrained-embedder': (
        'https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/config.json'
    ),
    'google/realm-cc-news-pretrained-encoder': (
        'https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/config.json'
    ),
    'google/realm-cc-news-pretrained-scorer': (
        'https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/config.json'
    ),
    'google/realm-cc-news-pretrained-openqa': (
        'https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/config.json'
    ),
    'google/realm-orqa-nq-openqa': 'https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/config.json',
    'google/realm-orqa-nq-reader': 'https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/config.json',
    'google/realm-orqa-wq-openqa': 'https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/config.json',
    'google/realm-orqa-wq-reader': 'https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/config.json',
    # See all REALM models at https://huggingface.co/models?filter=realm
}
class   __lowerCAmelCase						(lowercase_     ):





      '''simple docstring'''






      lowerCAmelCase__					:     Optional[int]  					=							"""realm"""



      def __init__(self    :    str   ,						UpperCamelCase    :    List[Any]=30522   ,						UpperCamelCase    :    List[Any]=768   ,						UpperCamelCase    :    int=128   ,						UpperCamelCase    :    Any=12   ,						UpperCamelCase    :    Tuple=12   ,						UpperCamelCase    :    List[Any]=8   ,						UpperCamelCase    :    Union[str, Any]=3072   ,						UpperCamelCase    :    List[str]="gelu_new"   ,						UpperCamelCase    :    Any=0.1   ,						UpperCamelCase    :    List[str]=0.1   ,						UpperCamelCase    :    Dict=512   ,						UpperCamelCase    :    Dict=2   ,						UpperCamelCase    :    List[Any]=0.02   ,						UpperCamelCase    :    List[Any]=1E-12   ,						UpperCamelCase    :    Dict=256   ,						UpperCamelCase    :    Union[str, Any]=10   ,						UpperCamelCase    :    Optional[int]=1E-3   ,						UpperCamelCase    :    Tuple=5   ,						UpperCamelCase    :    Optional[int]=320   ,						UpperCamelCase    :    List[str]=13353718   ,						UpperCamelCase    :    Optional[Any]=5000   ,						UpperCamelCase    :    str=1   ,						UpperCamelCase    :    Union[str, Any]=0   ,						UpperCamelCase    :    List[Any]=2   ,						**UpperCamelCase    :    int   ,						):

             '''simple docstring'''







             super().__init__(pad_token_id=UpperCamelCase   ,						bos_token_id=UpperCamelCase   ,						eos_token_id=UpperCamelCase   ,						**UpperCamelCase )

             # Common config
             lowercase__      			= vocab_size
             lowercase__      			= max_position_embeddings
             lowercase__      			= hidden_size
             lowercase__      			= retriever_proj_size
             lowercase__      			= num_hidden_layers
             lowercase__      			= num_attention_heads
             lowercase__      			= num_candidates
             lowercase__      			= intermediate_size
             lowercase__      			= hidden_act
             lowercase__      			= hidden_dropout_prob
             lowercase__      			= attention_probs_dropout_prob
             lowercase__      			= initializer_range
             lowercase__      			= type_vocab_size
             lowercase__      			= layer_norm_eps

             # Reader config
             lowercase__      			= span_hidden_size
             lowercase__      			= max_span_width
             lowercase__      			= reader_layer_norm_eps
             lowercase__      			= reader_beam_size
             lowercase__      			= reader_seq_len

             # Retrieval config
             lowercase__      			= num_block_records
             lowercase__      			= searcher_beam_size




