


"""simple docstring"""
import argparse

import torch
from clip import load

from transformers import CLIPConfig, CLIPModel






def 						lowercase				(	_snake_case		: Any	,  _snake_case		: Optional[int]	)	->Union[str, Any]:
	"""simple docstring"""





	__snake_case      ,				__snake_case      ,				__snake_case      :		Dict         =							pt_attn_layer.in_proj_weight.chunk(3	,  dim=0	)
	__snake_case      ,				__snake_case      ,				__snake_case      :		Tuple         =							pt_attn_layer.in_proj_bias.chunk(3	,  dim=0	)

	__snake_case      :		Dict         =							pt_attn_layer.out_proj.weight
	__snake_case      :		Any         =							pt_attn_layer.out_proj.bias

	__snake_case      :		str         =							q_proj
	__snake_case      :		int         =							q_proj_bias

	__snake_case      :		List[Any]         =							k_proj
	__snake_case      :		Optional[Any]         =							k_proj_bias

	__snake_case      :		Optional[Any]         =							v_proj
	__snake_case      :		Any         =							v_proj_bias

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






def 						lowercase				(	_snake_case		: str	,  _snake_case		: Dict	)	->Optional[Any]:
	"""simple docstring"""





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






def 						lowercase				(	_snake_case		: Tuple	,  _snake_case		: Union[str, Any]	)	->Optional[int]:
	"""simple docstring"""





	__snake_case      :		Dict         =							pt_linear.weight
	__snake_case      :		Optional[int]         =							pt_linear.bias






def 						lowercase				(	_snake_case		: Union[str, Any]	,  _snake_case		: Dict	)	->str:
	"""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 						lowercase				(	_snake_case		: List[str]	,  _snake_case		: Tuple	)	->List[str]:
	"""simple docstring"""





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






def 						lowercase				(	_snake_case		: Any	,  _snake_case		: Optional[Any]	)	->List[Any]:
	"""simple docstring"""





	__snake_case      :		int         =							pt_model.token_embedding.weight
	__snake_case      :		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 						lowercase				(	_snake_case		: int	,  _snake_case		: Tuple	)	->List[Any]:
	"""simple docstring"""





	__snake_case      :		Union[str, Any]         =							pt_model.text_projection.data.T

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






def 						lowercase				(	_snake_case		: List[Any]	,  _snake_case		: Optional[int]	)	->Tuple:
	"""simple docstring"""





	__snake_case      :		Tuple         =							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
	__snake_case      :		Any         =							pt_model.visual.conva.weight.data
	__snake_case      :		Union[str, Any]         =							pt_model.visual.class_embedding
	__snake_case      :		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 						lowercase				(	_snake_case		: Tuple	,  _snake_case		: str	,  _snake_case		: List[Any]=None	)	->int:
	"""simple docstring"""





	if config_path is not None:
		__snake_case      :		Union[str, Any]         =							CLIPConfig.from_pretrained(_snake_case	)
	else:
		__snake_case      :		List[str]         =							CLIPConfig(projection_dim=512	,  text_config={}	,  vision_config={}	)

	__snake_case      :		str         =							CLIPModel(_snake_case	).eval()

	__snake_case      ,				__snake_case      :		Union[str, Any]         =							load(_snake_case	,  device='''cpu'''	,  jit=_snake_case	)
	__snake_case      :		List[Any]         =							pt_model.eval()

	copy_text_model_and_projection(_snake_case	,  _snake_case	)
	copy_vison_model_and_projection(_snake_case	,  _snake_case	)
	__snake_case      :		Tuple         =							pt_model.logit_scale

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

	__snake_case      :		List[str]         =							hf_model(input_ids=_snake_case	,  pixel_values=_snake_case	,  return_dict=_snake_case	)
	__snake_case      :		Tuple         =							hf_outputs.logits_per_image
	__snake_case      :		int         =							hf_outputs.logits_per_text
	__snake_case      ,				__snake_case      :		Optional[Any]         =							pt_model(_snake_case	,  _snake_case	)

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

	hf_model.save_pretrained(_snake_case	)


if __name__ == "__main__":
			SCREAMING_SNAKE_CASE					:       List[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""")
			SCREAMING_SNAKE_CASE					:       Optional[Any]							   =							parser.parse_args()

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










'''simple docstring'''






from __future__ import annotations

A_				:					Tuple							=      """Alexander Joslin"""




def        snake_case_   (	lowerCAmelCase_						)->						int:





       '''simple docstring'''


       if number <= 0:
              _UpperCAmelCase    :		Union[str, Any]     	=					F'''n must be greater than 0. Got n = {number}'''
              raise ValueError(lowerCAmelCase_						)

       _UpperCAmelCase    :		List[str]     	=					[number + 1] * (number + 1)

       # starting position
       _UpperCAmelCase    :		Dict     	=					0
       for i in range(1			,  lowerCAmelCase_						):
              _UpperCAmelCase    :		int     	=					min(table[i + 1]			,  table[i] + 1						)
              # check if out of bounds
              if i * 2 <= number:
                     _UpperCAmelCase    :		Dict     	=					min(table[i * 2]			,  table[i] + 1						)
              # check if out of bounds
              if i * 3 <= number:
                     _UpperCAmelCase    :		Tuple     	=					min(table[i * 3]			,  table[i] + 1						)
       return table[number]


if __name__ == "__main__":
   import doctest

   doctest.testmod()



