
import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel






def 							UpperCAmelCase_		(							__SCREAMING_SNAKE_CASE			,							__SCREAMING_SNAKE_CASE			,							__SCREAMING_SNAKE_CASE							):
	lowercase  , lowercase  , lowercase					  =   pt_weights[F'''{prefix}.in_proj_weight'''].chunk(3			,							dim=0							)
	lowercase  , lowercase  , lowercase					  =   pt_weights[F'''{prefix}.in_proj_bias'''].chunk(3			,							dim=0							)

	lowercase					  =   pt_weights[F'''{prefix}.out_proj.weight''']
	lowercase					  =   pt_weights[F'''{prefix}.out_proj.bias''']

	lowercase					  =   q_proj
	lowercase					  =   q_proj_bias

	lowercase					  =   k_proj
	lowercase					  =   k_proj_bias

	lowercase					  =   v_proj
	lowercase					  =   v_proj_bias

	lowercase					  =   out_proj_weights
	lowercase					  =   out_proj_bias






def 							UpperCAmelCase_		(							__SCREAMING_SNAKE_CASE			,							__SCREAMING_SNAKE_CASE			,							__SCREAMING_SNAKE_CASE							):
	copy_linear(hf_mlp.fca			,							__SCREAMING_SNAKE_CASE			,							F'''{prefix}.c_fc'''							)
	copy_linear(hf_mlp.fca			,							__SCREAMING_SNAKE_CASE			,							F'''{prefix}.c_proj'''							)






def 							UpperCAmelCase_		(							__SCREAMING_SNAKE_CASE			,							__SCREAMING_SNAKE_CASE			,							__SCREAMING_SNAKE_CASE							):
	lowercase					  =   pt_weights[F'''{prefix}.weight'''].data
	lowercase					  =   pt_weights[F'''{prefix}.bias'''].data






def 							UpperCAmelCase_		(							__SCREAMING_SNAKE_CASE			,							__SCREAMING_SNAKE_CASE			,							__SCREAMING_SNAKE_CASE							):
	# copy layer norms
	copy_linear(hf_layer.layer_norma			,							__SCREAMING_SNAKE_CASE			,							F'''{prefix}.ln_1'''							)
	copy_linear(hf_layer.layer_norma			,							__SCREAMING_SNAKE_CASE			,							F'''{prefix}.ln_2'''							)

	# copy MLP
	copy_mlp(hf_layer.mlp			,							__SCREAMING_SNAKE_CASE			,							F'''{prefix}.mlp'''							)

	# copy attn
	copy_attn_layer(hf_layer.self_attn			,							__SCREAMING_SNAKE_CASE			,							F'''{prefix}.attn'''							)






def 							UpperCAmelCase_		(							__SCREAMING_SNAKE_CASE			,							__SCREAMING_SNAKE_CASE			,							__SCREAMING_SNAKE_CASE							):
	for layer_id, hf_layer in enumerate(__SCREAMING_SNAKE_CASE							):
		copy_layer(__SCREAMING_SNAKE_CASE			,							__SCREAMING_SNAKE_CASE			,							F'''{prefix}.{layer_id}'''							)






def 							UpperCAmelCase_		(							__SCREAMING_SNAKE_CASE			,							__SCREAMING_SNAKE_CASE							):
	# copy projection
	lowercase					  =   pt_weights['text_projection'].data.T

	# copy text encoder
	for name, param in hf_model.text_model.named_parameters():
		lowercase					  =   pt_weights[F'''bert.{name}'''].data






def 							UpperCAmelCase_		(							__SCREAMING_SNAKE_CASE			,							__SCREAMING_SNAKE_CASE							):
	# copy projection
	lowercase					  =   pt_weights['visual.proj'].data.T

	# copy layer norms
	copy_linear(hf_model.vision_model.pre_layrnorm			,							__SCREAMING_SNAKE_CASE			,							'visual.ln_pre'							)
	copy_linear(hf_model.vision_model.post_layernorm			,							__SCREAMING_SNAKE_CASE			,							'visual.ln_post'							)

	# copy embeddings
	lowercase					  =   pt_weights['visual.conv1.weight'].data
	lowercase					  =   pt_weights['visual.class_embedding'].data
	lowercase					  =   pt_weights['visual.positional_embedding'].data

	# copy encoder
	copy_layers(hf_model.vision_model.encoder.layers			,							__SCREAMING_SNAKE_CASE			,							'visual.transformer.resblocks'							)






@torch.no_grad()
def 							UpperCAmelCase_		(							__SCREAMING_SNAKE_CASE			,							__SCREAMING_SNAKE_CASE			,							__SCREAMING_SNAKE_CASE=None							):
	assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
	lowercase					  =   ChineseCLIPConfig.from_pretrained(__SCREAMING_SNAKE_CASE							)

	lowercase					  =   ChineseCLIPModel(__SCREAMING_SNAKE_CASE							).eval()

	lowercase					  =   torch.load(__SCREAMING_SNAKE_CASE			,							map_location='cpu'							)['state_dict']
	lowercase					  =   {(name[7:] if name.startswith('module.'							) else name): value for name, value in pt_weights.items()}

	copy_text_model_and_projection(__SCREAMING_SNAKE_CASE			,							__SCREAMING_SNAKE_CASE							)
	copy_vision_model_and_projection(__SCREAMING_SNAKE_CASE			,							__SCREAMING_SNAKE_CASE							)
	lowercase					  =   pt_weights['logit_scale'].data

	hf_model.save_pretrained(__SCREAMING_SNAKE_CASE							)


if __name__ == "__main__":
	UpperCAmelCase     				=			argparse.ArgumentParser()
	parser.add_argument(
	    '''--pytorch_dump_folder_path''',
	    default=None,
	    type=str,
	    help='''Path to the output folder storing converted hf PyTorch model.''',
	)
	parser.add_argument(
	    '''--checkpoint_path''', default=None, type=str, help='''Path to original github format ChineseCLIP checkpoint.'''
	)
	parser.add_argument(
	    '''--config_path''', default=None, required=True, type=str, help='''Path to hf config.json of model to convert.'''
	)
	UpperCAmelCase     				=			parser.parse_args()

	convert_chinese_clip_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
	print('''The conversion is finished!''')



import unittest
from timeit import timeit






def 							UpperCAmelCase_		(							__SCREAMING_SNAKE_CASE			,							__SCREAMING_SNAKE_CASE							):
	lowercase					  =   first_num if first_num >= second_num else second_num
	lowercase					  =   max_num
	while (common_mult % first_num > 0) or (common_mult % second_num > 0):
		common_mult += max_num
	return common_mult






def 							UpperCAmelCase_		(							__SCREAMING_SNAKE_CASE			,							__SCREAMING_SNAKE_CASE							):
	return b if a == 0 else greatest_common_divisor(b % a			,							__SCREAMING_SNAKE_CASE							)






def 							UpperCAmelCase_		(							__SCREAMING_SNAKE_CASE			,							__SCREAMING_SNAKE_CASE							):
	return first_num // greatest_common_divisor(__SCREAMING_SNAKE_CASE			,							__SCREAMING_SNAKE_CASE							) * second_num






def 							UpperCAmelCase_		(							):
	lowercase					  =   (
	    'from __main__ import least_common_multiple_slow, least_common_multiple_fast'
	)
	print(
	    'least_common_multiple_slow():'			,							timeit('least_common_multiple_slow(1000, 999)'			,							setup=__SCREAMING_SNAKE_CASE							)			,							)
	print(
	    'least_common_multiple_fast():'			,							timeit('least_common_multiple_fast(1000, 999)'			,							setup=__SCREAMING_SNAKE_CASE							)			,							)







class 	A_       (      unittest.TestCase       ):




	'''simple docstring'''


	_UpperCamelCase					:					Any										=	(
	    (10, 20),
	    (13, 15),
	    (4, 31),
	    (10, 42),
	    (43, 34),
	    (5, 12),
	    (12, 25),
	    (10, 25),
	    (6, 9),
	)
	_UpperCamelCase					:					int										=	(20, 195, 124, 210, 1462, 60, 300, 50, 18)





	def      SCREAMING_SNAKE_CASE__						(	self	):
		for i, (first_num, second_num) in enumerate(self.test_inputs	):
			lowercase					  =   least_common_multiple_slow(snake_case		,   snake_case	)
			lowercase					  =   least_common_multiple_fast(snake_case		,   snake_case	)
			with self.subTest(i=snake_case	):
				self.assertEqual(snake_case		,   self.expected_results[i]	)
				self.assertEqual(snake_case		,   self.expected_results[i]	)


if __name__ == "__main__":
	benchmark()
	unittest.main()

