



"""simple docstring"""
import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel
def   A_    (     _lowercase,   _lowercase,   _lowercase      ):







						'''simple docstring'''


						snake_case_,			snake_case_,			snake_case_					:Optional[Any]		=     pt_weights[f"""{prefix}.in_proj_weight"""].chunk(3,   dim=0      )
						snake_case_,			snake_case_,			snake_case_					:Dict		=     pt_weights[f"""{prefix}.in_proj_bias"""].chunk(3,   dim=0      )

						snake_case_					:Optional[Any]		=     pt_weights[f"""{prefix}.out_proj.weight"""]
						snake_case_					:Tuple		=     pt_weights[f"""{prefix}.out_proj.bias"""]

						snake_case_					:int		=     q_proj
						snake_case_					:str		=     q_proj_bias

						snake_case_					:Tuple		=     k_proj
						snake_case_					:Optional[int]		=     k_proj_bias

						snake_case_					:Any		=     v_proj
						snake_case_					:Optional[int]		=     v_proj_bias

						snake_case_					:Optional[Any]		=     out_proj_weights
						snake_case_					:Dict		=     out_proj_bias
def   A_    (     _lowercase,   _lowercase,   _lowercase      ):







						'''simple docstring'''


						copy_linear(hf_mlp.fca,   _lowercase,   f"""{prefix}.c_fc"""      )
						copy_linear(hf_mlp.fca,   _lowercase,   f"""{prefix}.c_proj"""      )
def   A_    (     _lowercase,   _lowercase,   _lowercase      ):







						'''simple docstring'''


						snake_case_					:Tuple		=     pt_weights[f"""{prefix}.weight"""].data
						snake_case_					:str		=     pt_weights[f"""{prefix}.bias"""].data
def   A_    (     _lowercase,   _lowercase,   _lowercase      ):







						'''simple docstring'''


						copy_linear(hf_layer.layer_norma,   _lowercase,   f"""{prefix}.ln_1"""      )
						copy_linear(hf_layer.layer_norma,   _lowercase,   f"""{prefix}.ln_2"""      )

						# copy MLP
						copy_mlp(hf_layer.mlp,   _lowercase,   f"""{prefix}.mlp"""      )

						# copy attn
						copy_attn_layer(hf_layer.self_attn,   _lowercase,   f"""{prefix}.attn"""      )
def   A_    (     _lowercase,   _lowercase,   _lowercase      ):







						'''simple docstring'''


						for layer_id, hf_layer in enumerate(_lowercase      ):
												copy_layer(_lowercase,   _lowercase,   f"""{prefix}.{layer_id}"""      )
def   A_    (     _lowercase,   _lowercase      ):







						'''simple docstring'''


						snake_case_					:Union[str, Any]		=     pt_weights["""text_projection"""].data.T

						# copy text encoder
						for name, param in hf_model.text_model.named_parameters():
												snake_case_					:Tuple		=     pt_weights[f"""bert.{name}"""].data
def   A_    (     _lowercase,   _lowercase      ):







						'''simple docstring'''


						snake_case_					:str		=     pt_weights["""visual.proj"""].data.T

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

						# copy embeddings
						snake_case_					:int		=     pt_weights["""visual.conv1.weight"""].data
						snake_case_					:Union[str, Any]		=     pt_weights["""visual.class_embedding"""].data
						snake_case_					:Tuple		=     pt_weights["""visual.positional_embedding"""].data

						# copy encoder
						copy_layers(hf_model.vision_model.encoder.layers,   _lowercase,   """visual.transformer.resblocks"""      )
@torch.no_grad()
def   A_    (     _lowercase,   _lowercase,   _lowercase=None      ):







						'''simple docstring'''


						assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
						snake_case_					:List[Any]		=     ChineseCLIPConfig.from_pretrained(_lowercase      )

						snake_case_					:int		=     ChineseCLIPModel(_lowercase      ).eval()

						snake_case_					:Dict		=     torch.load(_lowercase,   map_location="""cpu"""      )["""state_dict"""]
						snake_case_					:Union[str, Any]		=     {(name[7:] if name.startswith("""module."""      ) else name): value for name, value in pt_weights.items()}

						copy_text_model_and_projection(_lowercase,   _lowercase      )
						copy_vision_model_and_projection(_lowercase,   _lowercase      )
						snake_case_					:Dict		=     pt_weights["""logit_scale"""].data

						hf_model.save_pretrained(_lowercase      )


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

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





'''simple docstring'''



def 	__lowerCAmelCase       (				lowerCamelCase :    list[list[int]]					,		lowerCamelCase :    list[list[int]]       ):






	'''simple docstring'''




	__lowerCAmelCase       				=	[]
	__lowerCAmelCase       				=	len(lowerCamelCase       )
	for i in range(lowerCamelCase       ):
		__lowerCAmelCase       				=	[]
		for j in range(lowerCamelCase       ):
			__lowerCAmelCase       				=	0
			for k in range(lowerCamelCase       ):
				__lowerCAmelCase       				=	val + matrix_a[i][k] * matrix_b[k][j]
			list_a.append(lowerCamelCase       )
		matrix_c.append(lowerCamelCase       )
	return matrix_c



def 	__lowerCAmelCase       (				lowerCamelCase :    int       ):






	'''simple docstring'''




	return [[int(row == column       ) for column in range(lowerCamelCase       )] for row in range(lowerCamelCase       )]



def 	__lowerCAmelCase       (				lowerCamelCase :    int       ):






	'''simple docstring'''




	if n <= 1:
		return n
	__lowerCAmelCase       				=	identity(2       )
	__lowerCAmelCase       				=	[[1, 1], [1, 0]]
	__lowerCAmelCase       				=	n - 1
	while n > 0:
		if n % 2 == 1:
			__lowerCAmelCase       				=	multiply(lowerCamelCase					,		lowerCamelCase       )
		__lowerCAmelCase       				=	multiply(lowerCamelCase					,		lowerCamelCase       )
		__lowerCAmelCase       				=	int(n / 2       )
	return res_matrix[0][0]



def 	__lowerCAmelCase       (				lowerCamelCase :    int       ):






	'''simple docstring'''




	if n <= 1:
		return n
	__lowerCAmelCase       				=	0
	__lowerCAmelCase       				=	1
	for _ in range(2					,		n + 1       ):
		__lowerCAmelCase				,							__lowerCAmelCase       				=	fiba, fiba + fiba
	return fiba



def 	__lowerCAmelCase       (				):






	'''simple docstring'''




	for ordinal in "0th 1st 2nd 3rd 10th 100th 1000th".split():
		__lowerCAmelCase       				=	int("".join(c for c in ordinal if c in "0123456789"       )       )  # 1000th --> 1000
		print(
		    f'''{ordinal} fibonacci number using matrix exponentiation is '''
		    f'''{nth_fibonacci_matrix(lowerCamelCase       )} and using bruteforce is '''
		    f'''{nth_fibonacci_bruteforce(lowerCamelCase       )}\n'''       )
	# from timeit import timeit
	# print(timeit("nth_fibonacci_matrix(1000000)",
	#              "from main import nth_fibonacci_matrix", number=5))
	# print(timeit("nth_fibonacci_bruteforce(1000000)",
	#              "from main import nth_fibonacci_bruteforce", number=5))
	# 2.3342058970001744
	# 57.256506615000035


if __name__ == "__main__":
	import doctest

	doctest.testmod()
	main()