

import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel


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

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

						snake_case_     =			q_proj
						snake_case_     =			q_proj_bias

						snake_case_     =			k_proj
						snake_case_     =			k_proj_bias

						snake_case_     =			v_proj
						snake_case_     =			v_proj_bias

						snake_case_     =			out_proj_weights
						snake_case_     =			out_proj_bias


def 						__SCREAMING_SNAKE_CASE      (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 						__SCREAMING_SNAKE_CASE      (SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ ):
						snake_case_     =			pt_weights[F'''{prefix}.weight'''].data
						snake_case_     =			pt_weights[F'''{prefix}.bias'''].data


def 						__SCREAMING_SNAKE_CASE      (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 						__SCREAMING_SNAKE_CASE      (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 						__SCREAMING_SNAKE_CASE      (SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ ):
						# copy projection
						snake_case_     =			pt_weights['''text_projection'''].data.T

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


def 						__SCREAMING_SNAKE_CASE      (SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ ):
						# copy projection
						snake_case_     =			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
						snake_case_     =			pt_weights['''visual.conv1.weight'''].data
						snake_case_     =			pt_weights['''visual.class_embedding'''].data
						snake_case_     =			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 						__SCREAMING_SNAKE_CASE      (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."
						snake_case_     =			ChineseCLIPConfig.from_pretrained(SCREAMING_SNAKE_CASE__ )

						snake_case_     =			ChineseCLIPModel(SCREAMING_SNAKE_CASE__ ).eval()

						snake_case_     =			torch.load(SCREAMING_SNAKE_CASE__						,	map_location='''cpu''' )['''state_dict''']
						snake_case_     =			{(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__ )
						snake_case_     =			pt_weights['''logit_scale'''].data

						hf_model.save_pretrained(SCREAMING_SNAKE_CASE__ )


if __name__ == "__main__":
					lowerCAmelCase_ 			=			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.'''
					)
					lowerCAmelCase_ 			=			parser.parse_args()

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


lowerCAmelCase_ 			=			(1, 1_53, 3_70, 3_71, 16_34, 24_67_80_51, 1_15_13_22_19_01_87_63_99_25_65_09_55_97_97_39_71_52_24_01)
lowerCAmelCase_ 			=			(-1_53, -1, 0, 1.2, 2_00, "A", [], {}, None)


def 						__SCREAMING_SNAKE_CASE      (SCREAMING_SNAKE_CASE__ ):
						if not isinstance(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ ) or n < 1:
												return False

						# Initialization of sum and number of digits.
						snake_case_     =			0
						snake_case_     =			0
						snake_case_     =			n
						# Calculation of digits of the number
						while temp > 0:
												number_of_digits += 1
												temp //= 10
						# Dividing number into separate digits and find Armstrong number
						snake_case_     =			n
						while temp > 0:
												snake_case_     =			temp % 10
												total += rem**number_of_digits
												temp //= 10
						return n == total


def 						__SCREAMING_SNAKE_CASE      (SCREAMING_SNAKE_CASE__ ):
						if not isinstance(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ ) or n < 1:
												return False

						# Init a "histogram" of the digits
						snake_case_     =			[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
						snake_case_     =			0
						snake_case_     =			0
						snake_case_     =			n
						while temp > 0:
												snake_case_,					snake_case_     =			divmod(SCREAMING_SNAKE_CASE__						,	10 )
												digit_histogram[rem] += 1
												digit_total += 1

						for cnt, i in zip(SCREAMING_SNAKE_CASE__						,	range(len(SCREAMING_SNAKE_CASE__ ) ) ):
												total += cnt * i**digit_total

						return n == total


def 						__SCREAMING_SNAKE_CASE      (SCREAMING_SNAKE_CASE__ ):
						if not isinstance(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ ) or n < 1:
												return False
						snake_case_     =			len(str(SCREAMING_SNAKE_CASE__ ) )  # the power that all digits will be raised to
						# check if sum of each digit multiplied expo times is equal to number
						return n == sum(int(SCREAMING_SNAKE_CASE__ ) ** expo for i in str(SCREAMING_SNAKE_CASE__ ) )


def 						__SCREAMING_SNAKE_CASE      ():
						snake_case_     =			int(input('''Enter an integer to see if it is an Armstrong number: ''' ).strip() )
						print(F'''{num} is {'' if armstrong_number(SCREAMING_SNAKE_CASE__ ) else 'not '}an Armstrong number.''' )
						print(F'''{num} is {'' if narcissistic_number(SCREAMING_SNAKE_CASE__ ) else 'not '}an Armstrong number.''' )
						print(F'''{num} is {'' if pluperfect_number(SCREAMING_SNAKE_CASE__ ) else 'not '}an Armstrong number.''' )


if __name__ == "__main__":
					import doctest

					doctest.testmod()
					main()