code: 
  
  import argparse
  
  import torch
  from clip import load
  
  from transformers import CLIPConfig, CLIPModel
  def        __SCREAMING_SNAKE_CASE    (      a__						:      Union[str, Any] ,a__						:      Tuple					)				->							Optional[Any]:
  	__A						,							__A						,							__A   : List[str]								= pt_attn_layer.in_proj_weight.chunk(3 ,dim=0					)
  	__A						,							__A						,							__A   : List[Any]								= pt_attn_layer.in_proj_bias.chunk(3 ,dim=0					)
  
  	__A   : Union[str, Any]								= pt_attn_layer.out_proj.weight
  	__A   : Optional[int]								= pt_attn_layer.out_proj.bias
  
  	__A   : Dict								= q_proj
  	__A   : Tuple								= q_proj_bias
  
  	__A   : Optional[Any]								= k_proj
  	__A   : str								= k_proj_bias
  
  	__A   : List[Any]								= v_proj
  	__A   : Any								= v_proj_bias
  
  	__A   : Dict								= out_proj_weights
  	__A   : Optional[Any]								= out_proj_bias
  def        __SCREAMING_SNAKE_CASE    (      a__						:      Dict ,a__						:      List[Any]					)				->							Union[str, Any]:
  	copy_linear(hf_mlp.fca ,pt_mlp.c_fc					)
  	copy_linear(hf_mlp.fca ,pt_mlp.c_proj					)
  def        __SCREAMING_SNAKE_CASE    (      a__						:      Any ,a__						:      List[Any]					)				->							int:
  	__A   : str								= pt_linear.weight
  	__A   : Dict								= pt_linear.bias
  def        __SCREAMING_SNAKE_CASE    (      a__						:      List[str] ,a__						:      Optional[int]					)				->							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    (      a__						:      int ,a__						:      Any					)				->							List[Any]:
  	for hf_layer, pt_layer in zip(a__ ,a__					):
  		copy_layer(a__ ,a__					)
  def        __SCREAMING_SNAKE_CASE    (      a__						:      List[Any] ,a__						:      Tuple					)				->							Dict:
  	# copy  embeds
  	__A   : Any								= pt_model.token_embedding.weight
  	__A   : List[str]								= 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    (      a__						:      Union[str, Any] ,a__						:      Tuple					)				->							List[str]:
  	# copy projection
  	__A   : List[Any]								= pt_model.text_projection.data.T
  
  	# copy text encoder
  	copy_encoder(hf_model.text_model ,a__					)
  def        __SCREAMING_SNAKE_CASE    (      a__						:      Optional[Any] ,a__						:      Any					)				->							Any:
  	# copy projection
  	__A   : Dict								= 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   : List[str]								= pt_model.visual.conva.weight.data
  	__A   : Optional[Any]								= pt_model.visual.class_embedding
  	__A   : Dict								= 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    (      a__						:      str ,a__						:      List[Any] ,a__						:      List[str]=None					)				->							Tuple:
  	if config_path is not None:
  		__A   : int								= CLIPConfig.from_pretrained(a__					)
  	else:
  		__A   : Optional[Any]								= CLIPConfig(projection_dim=512 ,text_config={} ,vision_config={}					)
  
  	__A   : Union[str, Any]								= CLIPModel(a__					).eval()
  
  	__A						,							__A   : Any								= load(a__ ,device="""cpu""" ,jit=a__					)
  	__A   : Tuple								= pt_model.eval()
  
  	copy_text_model_and_projection(a__ ,a__					)
  	copy_vison_model_and_projection(a__ ,a__					)
  	__A   : int								= pt_model.logit_scale
  
  	__A   : List[Any]								= torch.arange(0 ,77					).unsqueeze(0					)
  	__A   : str								= torch.randn(1 ,3 ,224 ,224					)
  
  	__A   : Optional[int]								= hf_model(input_ids=a__ ,pixel_values=a__ ,return_dict=a__					)
  	__A   : int								= hf_outputs.logits_per_image
  	__A   : str								= hf_outputs.logits_per_text
  	__A						,							__A   : List[Any]								= pt_model(a__ ,a__					)
  
  	assert torch.allclose(a__ ,a__ ,atol=1E-3					)
  	assert torch.allclose(a__ ,a__ ,atol=1E-3					)
  
  	hf_model.save_pretrained(a__					)
  
  
  if __name__ == "__main__":
  			UpperCAmelCase_       :				Union[str, 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''')
  			UpperCAmelCase_       :				Optional[int]		   =      parser.parse_args()
  
  			convert_clip_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
  
  
  
  
  
  
style_context: 
  
  import unittest
  from pathlib import Path
  
  from transformers import is_vision_available, load_tool
  from transformers.testing_utils import get_tests_dir
  
  from .test_tools_common import ToolTesterMixin
  
  
  if is_vision_available():
  			from PIL import Image
  
  
  
  
  
  
  class     lowerCamelCase_			(  unittest.TestCase	,			_lowercase  ):
  
  
  
  
  	def 					lowerCAmelCase_					(    self	:    Dict       ):
  		__A   : str								= load_tool("""image-captioning"""       )
  		self.tool.setup()
  		__A   : Dict								= load_tool("""image-captioning"""				,			remote=__A       )
  
  
  
  
  	def 					lowerCAmelCase_					(    self	:    Optional[int]       ):
  		__A   : List[Any]								= Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""       )       ) / """000000039769.png"""       )
  		__A   : Optional[Any]								= self.tool(__A       )
  		self.assertEqual(__A				,			"""two cats sleeping on a couch"""       )
  
  
  
  
  	def 					lowerCAmelCase_					(    self	:    Dict       ):
  		__A   : Any								= Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""       )       ) / """000000039769.png"""       )
  		__A   : List[str]								= self.remote_tool(__A       )
  		self.assertEqual(__A				,			"""two cats sleeping on a couch"""       )
  
  
  
  
  	def 					lowerCAmelCase_					(    self	:    Union[str, Any]       ):
  		__A   : Optional[Any]								= Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""       )       ) / """000000039769.png"""       )
  		__A   : str								= self.tool(image=__A       )
  		self.assertEqual(__A				,			"""two cats sleeping on a couch"""       )
  
  
  
  
  	def 					lowerCAmelCase_					(    self	:    str       ):
  		__A   : Dict								= Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""       )       ) / """000000039769.png"""       )
  		__A   : List[Any]								= self.remote_tool(image=__A       )
  		self.assertEqual(__A				,			"""two cats sleeping on a couch"""       )
  
  
  
  
  
  