

import argparse

import torch
from clip import load

from transformers import CLIPConfig, CLIPModel







def _a			(	SCREAMING_SNAKE_CASE		:      Optional[Any]   , SCREAMING_SNAKE_CASE		:      str		)   ->   Any:

 """simple docstring"""






 __lowerCAmelCase     ,							__lowerCAmelCase     ,							__lowerCAmelCase:       Any            =				pt_attn_layer.in_proj_weight.chunk(3   , dim=0		)
 __lowerCAmelCase     ,							__lowerCAmelCase     ,							__lowerCAmelCase:       List[Any]            =				pt_attn_layer.in_proj_bias.chunk(3   , dim=0		)

 __lowerCAmelCase:       List[Any]            =				pt_attn_layer.out_proj.weight
 __lowerCAmelCase:       Optional[int]            =				pt_attn_layer.out_proj.bias

 __lowerCAmelCase:       str            =				q_proj
 __lowerCAmelCase:       Union[str, Any]            =				q_proj_bias

 __lowerCAmelCase:       int            =				k_proj
 __lowerCAmelCase:       Optional[Any]            =				k_proj_bias

 __lowerCAmelCase:       Optional[Any]            =				v_proj
 __lowerCAmelCase:       str            =				v_proj_bias

 __lowerCAmelCase:       Optional[int]            =				out_proj_weights
 __lowerCAmelCase:       List[Any]            =				out_proj_bias







def _a			(	SCREAMING_SNAKE_CASE		:      Union[str, Any]   , SCREAMING_SNAKE_CASE		:      Optional[int]		)   ->   str:

 """simple docstring"""






 copy_linear(hf_mlp.fca   , pt_mlp.c_fc		)
 copy_linear(hf_mlp.fca   , pt_mlp.c_proj		)







def _a			(	SCREAMING_SNAKE_CASE		:      Optional[Any]   , SCREAMING_SNAKE_CASE		:      Dict		)   ->   Union[str, Any]:

 """simple docstring"""






 __lowerCAmelCase:       int            =				pt_linear.weight
 __lowerCAmelCase:       Optional[int]            =				pt_linear.bias







def _a			(	SCREAMING_SNAKE_CASE		:      Any   , SCREAMING_SNAKE_CASE		:      Optional[Any]		)   ->   Union[str, Any]:

 """simple docstring"""






 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 _a			(	SCREAMING_SNAKE_CASE		:      int   , SCREAMING_SNAKE_CASE		:      Tuple		)   ->   int:

 """simple docstring"""






 for hf_layer, pt_layer in zip(__lowercase   , __lowercase		):
  copy_layer(__lowercase   , __lowercase		)







def _a			(	SCREAMING_SNAKE_CASE		:      Optional[Any]   , SCREAMING_SNAKE_CASE		:      str		)   ->   List[Any]:

 """simple docstring"""






 __lowerCAmelCase:       Optional[int]            =				pt_model.token_embedding.weight
 __lowerCAmelCase:       Optional[int]            =				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 _a			(	SCREAMING_SNAKE_CASE		:      Dict   , SCREAMING_SNAKE_CASE		:      int		)   ->   List[str]:

 """simple docstring"""






 __lowerCAmelCase:       Optional[int]            =				pt_model.text_projection.data.T

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







def _a			(	SCREAMING_SNAKE_CASE		:      Tuple   , SCREAMING_SNAKE_CASE		:      str		)   ->   str:

 """simple docstring"""






 __lowerCAmelCase:       int            =				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[Any]            =				pt_model.visual.conva.weight.data
 __lowerCAmelCase:       int            =				pt_model.visual.class_embedding
 __lowerCAmelCase:       List[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 _a			(	SCREAMING_SNAKE_CASE		:      Union[str, Any]   , SCREAMING_SNAKE_CASE		:      List[str]   , SCREAMING_SNAKE_CASE		:      Tuple=None		)   ->   int:

 """simple docstring"""






 if config_path is not None:
  __lowerCAmelCase:       Optional[Any]            =				CLIPConfig.from_pretrained(__lowercase		)
 else:
  __lowerCAmelCase:       Tuple            =				CLIPConfig(projection_dim=5_12   , text_config={}   , vision_config={}		)

 __lowerCAmelCase:       Any            =				CLIPModel(__lowercase		).eval()

 __lowerCAmelCase     ,							__lowerCAmelCase:       List[Any]            =				load(__lowercase   , device='cpu'   , jit=__lowercase		)
 __lowerCAmelCase:       int            =				pt_model.eval()

 copy_text_model_and_projection(__lowercase   , __lowercase		)
 copy_vison_model_and_projection(__lowercase   , __lowercase		)
 __lowerCAmelCase:       Tuple            =				pt_model.logit_scale

 __lowerCAmelCase:       Optional[int]            =				torch.arange(0   , 77		).unsqueeze(0		)
 __lowerCAmelCase:       Tuple            =				torch.randn(1   , 3   , 2_24   , 2_24		)

 __lowerCAmelCase:       str            =				hf_model(input_ids=__lowercase   , pixel_values=__lowercase   , return_dict=__lowercase		)
 __lowerCAmelCase:       Dict            =				hf_outputs.logits_per_image
 __lowerCAmelCase:       Union[str, Any]            =				hf_outputs.logits_per_text
 __lowerCAmelCase     ,							__lowerCAmelCase:       Dict            =				pt_model(__lowercase   , __lowercase		)

 assert torch.allclose(__lowercase   , __lowercase   , atol=1E-3		)
 assert torch.allclose(__lowercase   , __lowercase   , atol=1E-3		)

 hf_model.save_pretrained(__lowercase		)


if __name__ == "__main__":
  _a		  =       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		  =       parser.parse_args()

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








import torch

from transformers import PreTrainedModel

from .custom_configuration import CustomConfig, NoSuperInitConfig






class 							A_			(		snake_case__ ):
     _lowercase      :					Any       =     CustomConfig


     def __init__(							self      :		List[Any]      ,  UpperCAmelCase      :		Optional[Any] )							->     int:
      super().__init__(UpperCAmelCase )
      __lowerCAmelCase:       Tuple            =				torch.nn.Linear(config.hidden_size      ,  config.hidden_size )


     def   UpperCAmelCase  (							self      :		Optional[int]      ,  UpperCAmelCase      :		Tuple )							->     Union[str, Any]:
      return self.linear(UpperCAmelCase )


     def   UpperCAmelCase  (							self      :		str      ,  UpperCAmelCase      :		str )							->     Optional[int]:
      pass







class 							A_			(		snake_case__ ):
     _lowercase      :					str       =     NoSuperInitConfig


     def __init__(							self      :		List[Any]      ,  UpperCAmelCase      :		Union[str, Any] )							->     int:
      super().__init__(UpperCAmelCase )
      __lowerCAmelCase:       int            =				torch.nn.Linear(config.attribute      ,  config.attribute )


     def   UpperCAmelCase  (							self      :		Tuple      ,  UpperCAmelCase      :		Tuple )							->     Tuple:
      return self.linear(UpperCAmelCase )


     def   UpperCAmelCase  (							self      :		Optional[Any]      ,  UpperCAmelCase      :		Any )							->     str:
      pass





