
"""simple docstring"""



import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel
def 					lowerCamelCase   (     _snake_case    ,_snake_case    ,_snake_case ):
    UpperCAmelCase__	:      Any     							=					('dense.weight', 'attention.self.query', 'attention.self.key', 'attention.self.value')

    UpperCAmelCase__	:      Tuple     							=					(
        ('layer.', 'layer_'),
        ('word_embeddings.weight', 'word_embeddings'),
        ('position_embeddings.weight', 'position_embeddings'),
        ('token_type_embeddings.weight', 'token_type_embeddings'),
        ('.', '/'),
        ('LayerNorm/weight', 'LayerNorm/gamma'),
        ('LayerNorm/bias', 'LayerNorm/beta'),
        ('weight', 'kernel'),
    )

    if not os.path.isdir(A__ ):
        os.makedirs(A__ )

    UpperCAmelCase__	:      Any     							=					model.state_dict()

    def to_tf_var_name(_snake_case ):
        for patt, repl in iter(A__ ):
            UpperCAmelCase__	:      Tuple     							=					name.replace(A__    ,A__ )
        return F'''bert/{name}'''

    def create_tf_var(_snake_case    ,_snake_case    ,_snake_case ):
        UpperCAmelCase__	:      Tuple     							=					tf.dtypes.as_dtype(tensor.dtype )
        UpperCAmelCase__	:      Optional[int]     							=					tf.get_variable(dtype=A__    ,shape=tensor.shape    ,name=A__    ,initializer=tf.zeros_initializer() )
        session.run(tf.variables_initializer([tf_var] ) )
        session.run(A__ )
        return tf_var

    tf.reset_default_graph()
    with tf.Session() as session:
        for var_name in state_dict:
            UpperCAmelCase__	:      Union[str, Any]     							=					to_tf_var_name(A__ )
            UpperCAmelCase__	:      List[str]     							=					state_dict[var_name].numpy()
            if any(x in var_name for x in tensors_to_transpose ):
                UpperCAmelCase__	:      Any     							=					torch_tensor.T
            UpperCAmelCase__	:      Optional[int]     							=					create_tf_var(tensor=A__    ,name=A__    ,session=A__ )
            tf.keras.backend.set_value(A__    ,A__ )
            UpperCAmelCase__	:      Tuple     							=					session.run(A__ )
            print(F'''Successfully created {tf_name}: {np.allclose(A__    ,A__ )}''' )

        UpperCAmelCase__	:      List[str]     							=					tf.train.Saver(tf.trainable_variables() )
        saver.save(A__    ,os.path.join(A__    ,model_name.replace('-'    ,'_' ) + '.ckpt' ) )
def 					lowerCamelCase   (     _snake_case=None ):
    UpperCAmelCase__	:      Optional[Any]     							=					argparse.ArgumentParser()
    parser.add_argument('--model_name'    ,type=A__    ,required=A__    ,help='model name e.g. bert-base-uncased' )
    parser.add_argument(
        '--cache_dir'    ,type=A__    ,default=A__    ,required=A__    ,help='Directory containing pytorch model' )
    parser.add_argument('--pytorch_model_path'    ,type=A__    ,required=A__    ,help='/path/to/<pytorch-model-name>.bin' )
    parser.add_argument('--tf_cache_dir'    ,type=A__    ,required=A__    ,help='Directory in which to save tensorflow model' )
    UpperCAmelCase__	:      int     							=					parser.parse_args(A__ )

    UpperCAmelCase__	:      Optional[Any]     							=					BertModel.from_pretrained(
        pretrained_model_name_or_path=args.model_name    ,state_dict=torch.load(args.pytorch_model_path )    ,cache_dir=args.cache_dir    ,)

    convert_pytorch_checkpoint_to_tf(model=A__    ,ckpt_dir=args.tf_cache_dir    ,model_name=args.model_name )


if __name__ == "__main__":
    main()




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

from ...utils import logging


lowercase_   							=  logging.get_logger(__name__)
def 	a (     A__      :  Optional[int] ,  A__      :  Union[str, Any]	)       -> int:





      """simple docstring"""



      _lowercase      			=nn.functional.normalize(A__	)
      _lowercase      			=nn.functional.normalize(A__	)
      return torch.mm(A__ ,  normalized_text_embeds.t()	)







class 					__lowerCAmelCase	(   SCREAMING_SNAKE_CASE				):
  _a		     =    CLIPConfig

  _a		     =    ["""CLIPEncoderLayer"""]




  def __init__(							self				,  lowerCAmelCase   )    ->					Union[str, Any]:
        '''simple docstring'''






        super().__init__(lowerCAmelCase   )

        _lowercase      			=CLIPVisionModel(config.vision_config   )
        _lowercase      			=nn.Linear(config.vision_config.hidden_size				,  config.projection_dim				,  bias=lowerCAmelCase   )

        _lowercase      			=nn.Parameter(torch.ones(17				,  config.projection_dim   )				,  requires_grad=lowerCAmelCase   )
        _lowercase      			=nn.Parameter(torch.ones(3				,  config.projection_dim   )				,  requires_grad=lowerCAmelCase   )

        _lowercase      			=nn.Parameter(torch.ones(17   )				,  requires_grad=lowerCAmelCase   )
        _lowercase      			=nn.Parameter(torch.ones(3   )				,  requires_grad=lowerCAmelCase   )




  @torch.no_grad()
  def     A__	(							self				,  lowerCAmelCase				,  lowerCAmelCase   )    ->					Optional[int]:
        '''simple docstring'''






        _lowercase      			=self.vision_model(lowerCAmelCase   )[1]  # pooled_output
        _lowercase      			=self.visual_projection(lowerCAmelCase   )

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

        _lowercase      			=[]
        _lowercase      			=image_embeds.shape[0]
        for i in range(lowerCAmelCase   ):
              _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(lowerCAmelCase   )

              result.append(lowerCAmelCase   )

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

        return images, has_nsfw_concepts




  @torch.no_grad()
  def     A__	(							self				,  lowerCAmelCase				,  lowerCAmelCase   )    ->					Tuple:
        '''simple docstring'''






        _lowercase      			=self.vision_model(lowerCAmelCase   )[1]  # pooled_output
        _lowercase      			=self.visual_projection(lowerCAmelCase   )

        _lowercase      			=cosine_distance(lowerCAmelCase				,  self.special_care_embeds   )
        _lowercase      			=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
        _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


