




import argparse

import torch
from clip import load

from transformers import CLIPConfig, CLIPModel

def 					SCREAMING_SNAKE_CASE							(   __UpperCamelCase		,					__UpperCamelCase)			->     List[str]:
     a  ,      a  ,      a      =      pt_attn_layer.in_proj_weight.chunk(3		,					dim=0)
     a  ,      a  ,      a      =      pt_attn_layer.in_proj_bias.chunk(3		,					dim=0)

     a      =      pt_attn_layer.out_proj.weight
     a      =      pt_attn_layer.out_proj.bias

     a      =      q_proj
     a      =      q_proj_bias

     a      =      k_proj
     a      =      k_proj_bias

     a      =      v_proj
     a      =      v_proj_bias

     a      =      out_proj_weights
     a      =      out_proj_bias

def 					SCREAMING_SNAKE_CASE							(   __UpperCamelCase		,					__UpperCamelCase)			->     Dict:
     copy_linear(hf_mlp.fca		,					pt_mlp.c_fc)
     copy_linear(hf_mlp.fca		,					pt_mlp.c_proj)

def 					SCREAMING_SNAKE_CASE							(   __UpperCamelCase		,					__UpperCamelCase)			->     Tuple:
     a      =      pt_linear.weight
     a      =      pt_linear.bias

def 					SCREAMING_SNAKE_CASE							(   __UpperCamelCase		,					__UpperCamelCase)			->     Optional[int]:
     # 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 					SCREAMING_SNAKE_CASE							(   __UpperCamelCase		,					__UpperCamelCase)			->     List[Any]:
     for hf_layer, pt_layer in zip(lowerCamelCase_		,					lowerCamelCase_):
          copy_layer(lowerCamelCase_		,					lowerCamelCase_)

def 					SCREAMING_SNAKE_CASE							(   __UpperCamelCase		,					__UpperCamelCase)			->     Optional[int]:
     # copy  embeds
     a      =      pt_model.token_embedding.weight
     a      =      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 					SCREAMING_SNAKE_CASE							(   __UpperCamelCase		,					__UpperCamelCase)			->     int:
     # copy projection
     a      =      pt_model.text_projection.data.T

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

def 					SCREAMING_SNAKE_CASE							(   __UpperCamelCase		,					__UpperCamelCase)			->     Optional[int]:
     # copy projection
     a      =      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
     a      =      pt_model.visual.conva.weight.data
     a      =      pt_model.visual.class_embedding
     a      =      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 					SCREAMING_SNAKE_CASE							(   __UpperCamelCase		,					__UpperCamelCase		,					__UpperCamelCase=None)			->     Tuple:
     if config_path is not None:
          a      =      CLIPConfig.from_pretrained(lowerCamelCase_)
     else:
          a      =      CLIPConfig(projection_dim=5_12		,					text_config={}		,					vision_config={})

     a      =      CLIPModel(lowerCamelCase_).eval()

     a  ,      a      =      load(lowerCamelCase_		,					device="cpu"		,					jit=lowerCamelCase_)
     a      =      pt_model.eval()

     copy_text_model_and_projection(lowerCamelCase_		,					lowerCamelCase_)
     copy_vison_model_and_projection(lowerCamelCase_		,					lowerCamelCase_)
     a      =      pt_model.logit_scale

     a      =      torch.arange(0		,					77).unsqueeze(0)
     a      =      torch.randn(1		,					3		,					2_24		,					2_24)

     a      =      hf_model(input_ids=lowerCamelCase_		,					pixel_values=lowerCamelCase_		,					return_dict=lowerCamelCase_)
     a      =      hf_outputs.logits_per_image
     a      =      hf_outputs.logits_per_text
     a  ,      a      =      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__":
   lowercase__      :			List[Any]           =					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")
   lowercase__      :			Dict           =					parser.parse_args()

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








'''simple docstring'''



import torch

from transformers import PreTrainedModel

from .custom_configuration import CustomConfig, NoSuperInitConfig






class 							__lowercase				(					lowerCAmelCase__  ):







	'''simple docstring'''

	a			: Union[str, Any]      =				CustomConfig


	def __init__(self   ,_lowerCamelCase       )							->							Tuple:







				'''simple docstring'''





				super().__init__(_lowerCamelCase       )
				__lowercase   =							torch.nn.Linear(config.hidden_size   ,config.hidden_size       )


	def   _UpperCAmelCase   (self   ,_lowerCamelCase       )							->							List[str]:







				'''simple docstring'''





				return self.linear(_lowerCamelCase       )


	def   _UpperCAmelCase   (self   ,_lowerCamelCase       )							->							List[Any]:







				'''simple docstring'''





				pass






class 							__lowercase				(					lowerCAmelCase__  ):







	'''simple docstring'''

	a			: Dict      =				NoSuperInitConfig


	def __init__(self   ,_lowerCamelCase       )							->							str:







				'''simple docstring'''





				super().__init__(_lowerCamelCase       )
				__lowercase   =							torch.nn.Linear(config.attribute   ,config.attribute       )


	def   _UpperCAmelCase   (self   ,_lowerCamelCase       )							->							List[str]:







				'''simple docstring'''





				return self.linear(_lowerCamelCase       )


	def   _UpperCAmelCase   (self   ,_lowerCamelCase       )							->							Tuple:







				'''simple docstring'''





				pass





