



'''simple docstring'''






import argparse

import torch
from clip import load

from transformers import CLIPConfig, CLIPModel


def 			snake_case_					( _lowerCAmelCase  :						Union[str, Any]       ,				_lowerCAmelCase  :						List[Any]  )				->       List[str]:
							UpperCAmelCase							,		UpperCAmelCase							,		UpperCAmelCase			:	str										=		pt_attn_layer.in_proj_weight.chunk(3       ,				dim=0  )
							UpperCAmelCase							,		UpperCAmelCase							,		UpperCAmelCase			:	Union[str, Any]										=		pt_attn_layer.in_proj_bias.chunk(3       ,				dim=0  )

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

							UpperCAmelCase			:	Tuple										=		q_proj
							UpperCAmelCase			:	List[Any]										=		q_proj_bias

							UpperCAmelCase			:	Optional[int]										=		k_proj
							UpperCAmelCase			:	List[str]										=		k_proj_bias

							UpperCAmelCase			:	Optional[Any]										=		v_proj
							UpperCAmelCase			:	Tuple										=		v_proj_bias

							UpperCAmelCase			:	Optional[Any]										=		out_proj_weights
							UpperCAmelCase			:	List[Any]										=		out_proj_bias


def 			snake_case_					( _lowerCAmelCase  :						Union[str, Any]       ,				_lowerCAmelCase  :						str  )				->       int:
							copy_linear(hf_mlp.fca       ,				pt_mlp.c_fc  )
							copy_linear(hf_mlp.fca       ,				pt_mlp.c_proj  )


def 			snake_case_					( _lowerCAmelCase  :						Optional[Any]       ,				_lowerCAmelCase  :						Union[str, Any]  )				->       Optional[Any]:
							UpperCAmelCase			:	List[Any]										=		pt_linear.weight
							UpperCAmelCase			:	str										=		pt_linear.bias


def 			snake_case_					( _lowerCAmelCase  :						Any       ,				_lowerCAmelCase  :						Optional[int]  )				->       Optional[Any]:
							# 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 			snake_case_					( _lowerCAmelCase  :						List[Any]       ,				_lowerCAmelCase  :						Union[str, Any]  )				->       Any:
							for hf_layer, pt_layer in zip(_lowerCAmelCase       ,				_lowerCAmelCase  ):
														copy_layer(_lowerCAmelCase       ,				_lowerCAmelCase  )


def 			snake_case_					( _lowerCAmelCase  :						Optional[int]       ,				_lowerCAmelCase  :						Union[str, Any]  )				->       Dict:
							# copy  embeds
							UpperCAmelCase			:	List[str]										=		pt_model.token_embedding.weight
							UpperCAmelCase			:	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 			snake_case_					( _lowerCAmelCase  :						List[str]       ,				_lowerCAmelCase  :						str  )				->       int:
							# copy projection
							UpperCAmelCase			:	int										=		pt_model.text_projection.data.T

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


def 			snake_case_					( _lowerCAmelCase  :						List[Any]       ,				_lowerCAmelCase  :						str  )				->       str:
							# copy projection
							UpperCAmelCase			:	Optional[Any]										=		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
							UpperCAmelCase			:	Dict										=		pt_model.visual.conva.weight.data
							UpperCAmelCase			:	Optional[Any]										=		pt_model.visual.class_embedding
							UpperCAmelCase			:	Tuple										=		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 			snake_case_					( _lowerCAmelCase  :						Union[str, Any]       ,				_lowerCAmelCase  :						Any       ,				_lowerCAmelCase  :						Dict=None  )				->       str:
							if config_path is not None:
														UpperCAmelCase			:	str										=		CLIPConfig.from_pretrained(_lowerCAmelCase  )
							else:
														UpperCAmelCase			:	List[str]										=		CLIPConfig(projection_dim=512       ,				text_config={}       ,				vision_config={}  )

							UpperCAmelCase			:	str										=		CLIPModel(_lowerCAmelCase  ).eval()

							UpperCAmelCase							,		UpperCAmelCase			:	str										=		load(_lowerCAmelCase       ,				device='''cpu'''       ,				jit=_lowerCAmelCase  )
							UpperCAmelCase			:	Optional[Any]										=		pt_model.eval()

							copy_text_model_and_projection(_lowerCAmelCase       ,				_lowerCAmelCase  )
							copy_vison_model_and_projection(_lowerCAmelCase       ,				_lowerCAmelCase  )
							UpperCAmelCase			:	List[str]										=		pt_model.logit_scale

							UpperCAmelCase			:	int										=		torch.arange(0       ,				77  ).unsqueeze(0  )
							UpperCAmelCase			:	Optional[Any]										=		torch.randn(1       ,				3       ,				224       ,				224  )

							UpperCAmelCase			:	str										=		hf_model(input_ids=_lowerCAmelCase       ,				pixel_values=_lowerCAmelCase       ,				return_dict=_lowerCAmelCase  )
							UpperCAmelCase			:	List[Any]										=		hf_outputs.logits_per_image
							UpperCAmelCase			:	Optional[int]										=		hf_outputs.logits_per_text
							UpperCAmelCase							,		UpperCAmelCase			:	int										=		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__":
						UpperCamelCase__:       int							     =      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__:       int							     =      parser.parse_args()

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










'''simple docstring'''






UpperCamelCase__:       str							     =      [0.485, 0.456, 0.406]
UpperCamelCase__:       List[str]							     =      [0.229, 0.224, 0.225]
UpperCamelCase__:       List[str]							     =      [0.5, 0.5, 0.5]
UpperCamelCase__:       Optional[Any]							     =      [0.5, 0.5, 0.5]
UpperCamelCase__:       List[str]							     =      [0.48145466, 0.4578275, 0.40821073]
UpperCamelCase__:       List[str]							     =      [0.26862954, 0.26130258, 0.27577711]





