

import argparse

import torch
from clip import load

from transformers import CLIPConfig, CLIPModel



def 					__lowerCAmelCase					(  A				,   A			):
	UpperCAmelCase_				 =	pt_attn_layer.in_proj_weight.chunk(3				,   dim=0			)
	UpperCAmelCase_				 =	pt_attn_layer.in_proj_bias.chunk(3				,   dim=0			)

	UpperCAmelCase_				 =	pt_attn_layer.out_proj.weight
	UpperCAmelCase_				 =	pt_attn_layer.out_proj.bias

	UpperCAmelCase_				 =	q_proj
	UpperCAmelCase_				 =	q_proj_bias

	UpperCAmelCase_				 =	k_proj
	UpperCAmelCase_				 =	k_proj_bias

	UpperCAmelCase_				 =	v_proj
	UpperCAmelCase_				 =	v_proj_bias

	UpperCAmelCase_				 =	out_proj_weights
	UpperCAmelCase_				 =	out_proj_bias



def 					__lowerCAmelCase					(  A				,   A			):
	copy_linear(hf_mlp.fca				,   pt_mlp.c_fc			)
	copy_linear(hf_mlp.fca				,   pt_mlp.c_proj			)



def 					__lowerCAmelCase					(  A				,   A			):
	UpperCAmelCase_				 =	pt_linear.weight
	UpperCAmelCase_				 =	pt_linear.bias



def 					__lowerCAmelCase					(  A				,   A			):
	# 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 					__lowerCAmelCase					(  A				,   A			):
	for hf_layer, pt_layer in zip(__lowerCamelCase				,   __lowerCamelCase			):
		copy_layer(__lowerCamelCase				,   __lowerCamelCase			)



def 					__lowerCAmelCase					(  A				,   A			):
	# copy  embeds
	UpperCAmelCase_				 =	pt_model.token_embedding.weight
	UpperCAmelCase_				 =	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 					__lowerCAmelCase					(  A				,   A			):
	# copy projection
	UpperCAmelCase_				 =	pt_model.text_projection.data.T

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



def 					__lowerCAmelCase					(  A				,   A			):
	# copy projection
	UpperCAmelCase_				 =	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_				 =	pt_model.visual.conva.weight.data
	UpperCAmelCase_				 =	pt_model.visual.class_embedding
	UpperCAmelCase_				 =	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 					__lowerCAmelCase					(  A				,   A				,   A=None			):
	if config_path is not None:
		UpperCAmelCase_				 =	CLIPConfig.from_pretrained(__lowerCamelCase			)
	else:
		UpperCAmelCase_				 =	CLIPConfig(projection_dim=512				,   text_config={}				,   vision_config={}			)

	UpperCAmelCase_				 =	CLIPModel(__lowerCamelCase			).eval()

	UpperCAmelCase_				 =	load(__lowerCamelCase				,   device="cpu"				,   jit=__lowerCamelCase			)
	UpperCAmelCase_				 =	pt_model.eval()

	copy_text_model_and_projection(__lowerCamelCase				,   __lowerCamelCase			)
	copy_vison_model_and_projection(__lowerCamelCase				,   __lowerCamelCase			)
	UpperCAmelCase_				 =	pt_model.logit_scale

	UpperCAmelCase_				 =	torch.arange(0				,   77			).unsqueeze(0			)
	UpperCAmelCase_				 =	torch.randn(1				,   3				,   224				,   224			)

	UpperCAmelCase_				 =	hf_model(input_ids=__lowerCamelCase				,   pixel_values=__lowerCamelCase				,   return_dict=__lowerCamelCase			)
	UpperCAmelCase_				 =	hf_outputs.logits_per_image
	UpperCAmelCase_				 =	hf_outputs.logits_per_text
	UpperCAmelCase_				 =	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__":
				_a:			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""")
				_a:			Optional[int]       							=	parser.parse_args()

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






import sys

sys.set_int_max_str_digits(0)  # type: ignore






def 			lowerCamelCase__	(__lowerCamelCase		):
     _SCREAMING_SNAKE_CASE				:		int  			=							[0] * 11
     _SCREAMING_SNAKE_CASE				:		Optional[Any]  			=							[0] * 11

     # mark last 9 numbers
     for _ in range(9		):
          _SCREAMING_SNAKE_CASE				:		Optional[Any]  			=							1
          _SCREAMING_SNAKE_CASE				:		str  			=							number // 10
     # flag
     _SCREAMING_SNAKE_CASE				:		int  			=							True

     # check last 9 numbers for pandigitality

     for x in range(9		):
          if not check_last[x + 1]:
               _SCREAMING_SNAKE_CASE				:		Any  			=							False
     if not f:
          return f

     # mark first 9 numbers
     _SCREAMING_SNAKE_CASE				:		str  			=							int(str(__lowerCamelCase		)[:9]		)

     for _ in range(9		):
          _SCREAMING_SNAKE_CASE				:		str  			=							1
          _SCREAMING_SNAKE_CASE				:		List[Any]  			=							number // 10

     # check first 9 numbers for pandigitality

     for x in range(9		):
          if not check_front[x + 1]:
               _SCREAMING_SNAKE_CASE				:		Any  			=							False
     return f






def 			lowerCamelCase__	(__lowerCamelCase		):
     _SCREAMING_SNAKE_CASE				:		List[str]  			=							[0] * 11

     # mark last 9 numbers
     for _ in range(9		):
          _SCREAMING_SNAKE_CASE				:		Dict  			=							1
          _SCREAMING_SNAKE_CASE				:		List[str]  			=							number // 10
     # flag
     _SCREAMING_SNAKE_CASE				:		List[Any]  			=							True

     # check last 9 numbers for pandigitality

     for x in range(9		):
          if not check_last[x + 1]:
               _SCREAMING_SNAKE_CASE				:		str  			=							False
     return f






def 			lowerCamelCase__	():
     _SCREAMING_SNAKE_CASE				:		Dict  			=							1
     _SCREAMING_SNAKE_CASE				:		Union[str, Any]  			=							1
     _SCREAMING_SNAKE_CASE				:		List[str]  			=							2
     # temporary Fibonacci numbers

     _SCREAMING_SNAKE_CASE				:		Any  			=							1
     _SCREAMING_SNAKE_CASE				:		List[str]  			=							1
     _SCREAMING_SNAKE_CASE				:		str  			=							2
     # temporary Fibonacci numbers mod 1e9

     # mod m=1e9, done for fast optimisation
     _SCREAMING_SNAKE_CASE				:		Optional[Any]  			=							[0] * 1000000
     _SCREAMING_SNAKE_CASE				:		Optional[int]  			=							1000000000

     for x in range(1000000		):
          _SCREAMING_SNAKE_CASE				:		Union[str, Any]  			=							(aa + ba) % m
          _SCREAMING_SNAKE_CASE				:		int  			=							ba % m
          _SCREAMING_SNAKE_CASE				:		List[str]  			=							ca % m
          if checka(__lowerCamelCase		):
               _SCREAMING_SNAKE_CASE				:		List[Any]  			=							1

     for x in range(1000000		):
          _SCREAMING_SNAKE_CASE				:		Any  			=							a + b
          _SCREAMING_SNAKE_CASE				:		Dict  			=							b
          _SCREAMING_SNAKE_CASE				:		int  			=							c
          # perform check only if in tocheck
          if tocheck[x + 3] and check(__lowerCamelCase		):
               return x + 3  # first 2 already done
     return -1


if __name__ == "__main__":
       print(f"{solution() = }")