






"""simple docstring"""
import argparse

import torch
from clip import load

from transformers import CLIPConfig, CLIPModel






def        lowerCamelCase_  (						_lowerCamelCase			,   _lowerCamelCase				):
 lowerCamelCase__				,     lowerCamelCase__				,     lowerCamelCase__     :			Optional[Any]     				=						pt_attn_layer.in_proj_weight.chunk(3			,   dim=0				)
 lowerCamelCase__				,     lowerCamelCase__				,     lowerCamelCase__     :			int     				=						pt_attn_layer.in_proj_bias.chunk(3			,   dim=0				)

 lowerCamelCase__     :			Any     				=						pt_attn_layer.out_proj.weight
 lowerCamelCase__     :			Tuple     				=						pt_attn_layer.out_proj.bias

 lowerCamelCase__     :			Union[str, Any]     				=						q_proj
 lowerCamelCase__     :			List[Any]     				=						q_proj_bias

 lowerCamelCase__     :			Dict     				=						k_proj
 lowerCamelCase__     :			Dict     				=						k_proj_bias

 lowerCamelCase__     :			Optional[Any]     				=						v_proj
 lowerCamelCase__     :			List[str]     				=						v_proj_bias

 lowerCamelCase__     :			Optional[Any]     				=						out_proj_weights
 lowerCamelCase__     :			Dict     				=						out_proj_bias






def        lowerCamelCase_  (						_lowerCamelCase			,   _lowerCamelCase				):
 copy_linear(hf_mlp.fca			,   pt_mlp.c_fc				)
 copy_linear(hf_mlp.fca			,   pt_mlp.c_proj				)






def        lowerCamelCase_  (						_lowerCamelCase			,   _lowerCamelCase				):
 lowerCamelCase__     :			Any     				=						pt_linear.weight
 lowerCamelCase__     :			Any     				=						pt_linear.bias






def        lowerCamelCase_  (						_lowerCamelCase			,   _lowerCamelCase				):
 # copy layer norms
 copy_linear(hf_layer.layer_norma			,   pt_layer.ln_a				)
 copy_linear(hf_layer.layer_norma			,   pt_layer.ln_a				)

 # copy MLP
 copy_mlp(hf_layer.mlp			,   pt_layer.mlp				)

 # copy attn
 copy_attn_layer(hf_layer.self_attn			,   pt_layer.attn				)






def        lowerCamelCase_  (						_lowerCamelCase			,   _lowerCamelCase				):
 for hf_layer, pt_layer in zip(_lowerCamelCase			,   _lowerCamelCase				):
  copy_layer(_lowerCamelCase			,   _lowerCamelCase				)






def        lowerCamelCase_  (						_lowerCamelCase			,   _lowerCamelCase				):
 # copy  embeds
 lowerCamelCase__     :			Optional[int]     				=						pt_model.token_embedding.weight
 lowerCamelCase__     :			List[Any]     				=						pt_model.positional_embedding

 # copy layer norm
 copy_linear(hf_encoder.final_layer_norm			,   pt_model.ln_final				)

 # copy hidden layers
 copy_layers(hf_encoder.encoder.layers			,   pt_model.transformer.resblocks				)






def        lowerCamelCase_  (						_lowerCamelCase			,   _lowerCamelCase				):
 # copy projection
 lowerCamelCase__     :			Optional[int]     				=						pt_model.text_projection.data.T

 # copy text encoder
 copy_encoder(hf_model.text_model			,   _lowerCamelCase				)






def        lowerCamelCase_  (						_lowerCamelCase			,   _lowerCamelCase				):
 # copy projection
 lowerCamelCase__     :			str     				=						pt_model.visual.proj.data.T

 # copy layer norms
 copy_linear(hf_model.vision_model.pre_layrnorm			,   pt_model.visual.ln_pre				)
 copy_linear(hf_model.vision_model.post_layernorm			,   pt_model.visual.ln_post				)

 # copy embeds
 lowerCamelCase__     :			Optional[int]     				=						pt_model.visual.conva.weight.data
 lowerCamelCase__     :			Optional[Any]     				=						pt_model.visual.class_embedding
 lowerCamelCase__     :			Any     				=						pt_model.visual.positional_embedding.data

 # copy encoder
 copy_layers(hf_model.vision_model.encoder.layers			,   pt_model.visual.transformer.resblocks				)






@torch.no_grad()
def        lowerCamelCase_  (						_lowerCamelCase			,   _lowerCamelCase			,   _lowerCamelCase=None				):
 if config_path is not None:
  lowerCamelCase__     :			Optional[Any]     				=						CLIPConfig.from_pretrained(_lowerCamelCase				)
 else:
  lowerCamelCase__     :			int     				=						CLIPConfig(projection_dim=512			,   text_config={}			,   vision_config={}				)

 lowerCamelCase__     :			Any     				=						CLIPModel(_lowerCamelCase				).eval()

 lowerCamelCase__				,     lowerCamelCase__     :			Optional[Any]     				=						load(_lowerCamelCase			,   device='cpu'			,   jit=_lowerCamelCase				)
 lowerCamelCase__     :			List[Any]     				=						pt_model.eval()

 copy_text_model_and_projection(_lowerCamelCase			,   _lowerCamelCase				)
 copy_vison_model_and_projection(_lowerCamelCase			,   _lowerCamelCase				)
 lowerCamelCase__     :			Tuple     				=						pt_model.logit_scale

 lowerCamelCase__     :			int     				=						torch.arange(0			,   77				).unsqueeze(0				)
 lowerCamelCase__     :			Union[str, Any]     				=						torch.randn(1			,   3			,   224			,   224				)

 lowerCamelCase__     :			Optional[Any]     				=						hf_model(input_ids=_lowerCamelCase			,   pixel_values=_lowerCamelCase			,   return_dict=_lowerCamelCase				)
 lowerCamelCase__     :			Tuple     				=						hf_outputs.logits_per_image
 lowerCamelCase__     :			Dict     				=						hf_outputs.logits_per_text
 lowerCamelCase__				,     lowerCamelCase__     :			Optional[Any]     				=						pt_model(_lowerCamelCase			,   _lowerCamelCase				)

 assert torch.allclose(_lowerCamelCase			,   _lowerCamelCase			,   atol=1e-3				)
 assert torch.allclose(_lowerCamelCase			,   _lowerCamelCase			,   atol=1e-3				)

 hf_model.save_pretrained(_lowerCamelCase				)


if __name__ == "__main__":
      A_			: str				=			argparse.ArgumentParser()
      parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.")
      parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to fairseq checkpoint")
      parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert")
      A_			: Tuple				=			parser.parse_args()

      convert_clip_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)









"""simple docstring"""
class 		a_      :







      '''simple docstring'''

      class 		a_      :







            '''simple docstring'''

            lowerCamelCase__       :				List[Any] =	'_prev', '_data', '_next'




            def __init__(self,     lowerCamelCase_,     lowerCamelCase_,     lowerCamelCase_					):

             '''simple docstring'''



             lowerCamelCase__     :			Optional[int]     				=						link_p
             lowerCamelCase__     :			Optional[Any]     				=						element
             lowerCamelCase__     :			List[Any]     				=						link_n





            def 			a__							(self					):

             '''simple docstring'''



             return (
                 f''' Prev -> {self._prev is not None}, Next -> {self._next is not None}'''
             )




      def __init__(self					):

       '''simple docstring'''



       lowerCamelCase__     :			Any     				=						self._Node(lowerCamelCase_,     lowerCamelCase_,     lowerCamelCase_					)
       lowerCamelCase__     :			Dict     				=						self._Node(lowerCamelCase_,     lowerCamelCase_,     lowerCamelCase_					)
       lowerCamelCase__     :			List[str]     				=						self._trailer
       lowerCamelCase__     :			Union[str, Any]     				=						self._header
       lowerCamelCase__     :			Any     				=						0




      def __len__(self					):

       '''simple docstring'''



       return self._size




      def 			a__							(self					):

       '''simple docstring'''



       return self.__len__() == 0




      def 			a__							(self,     lowerCamelCase_,     lowerCamelCase_,     lowerCamelCase_					):

       '''simple docstring'''



       lowerCamelCase__     :			int     				=						self._Node(lowerCamelCase_,     lowerCamelCase_,     lowerCamelCase_					)
       lowerCamelCase__     :			Optional[int]     				=						new_node
       lowerCamelCase__     :			List[Any]     				=						new_node
       self._size += 1
       return self





      def 			a__							(self,     lowerCamelCase_					):

       '''simple docstring'''



       lowerCamelCase__     :			Optional[Any]     				=						node._prev
       lowerCamelCase__     :			List[str]     				=						node._next

       lowerCamelCase__     :			Tuple     				=						successor
       lowerCamelCase__     :			Tuple     				=						predecessor
       self._size -= 1
       lowerCamelCase__     :			int     				=						node._data
       lowerCamelCase__     :			Dict     				=						None
       del node
       return temp



class 		a_      (	_DoublyLinkedBase ):







      '''simple docstring'''

      def 			a__							(self					):

       '''simple docstring'''



       if self.is_empty():
        raise Exception('List is empty'					)
       return self._header._next._data




      def 			a__							(self					):

       '''simple docstring'''



       if self.is_empty():
        raise Exception('List is empty'					)
       return self._trailer._prev._data




      def 			a__							(self,     lowerCamelCase_					):

       '''simple docstring'''



       return self._insert(self._header,     lowerCamelCase_,     self._header._next					)




      def 			a__							(self,     lowerCamelCase_					):

       '''simple docstring'''



       return self._insert(self._trailer._prev,     lowerCamelCase_,     self._trailer					)




      def 			a__							(self					):

       '''simple docstring'''



       if self.is_empty():
        raise IndexError('remove_first from empty list'					)
       return self._delete(self._header._next					)





      def 			a__							(self					):

       '''simple docstring'''



       if self.is_empty():
        raise IndexError('remove_first from empty list'					)
       return self._delete(self._trailer._prev					)

