




"""simple docstring"""







import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel





def 							lowercase   (snake_case__				:	List[str] ,    snake_case__				:	Tuple ,    snake_case__				:	Optional[Any]	)       ->					Optional[int]:


				'''simple docstring'''






				lowerCAmelCase	,  lowerCAmelCase	,  lowerCAmelCase		=			pt_weights[f'''{prefix}.in_proj_weight'''].chunk(3 ,    dim=0	)
				lowerCAmelCase	,  lowerCAmelCase	,  lowerCAmelCase		=			pt_weights[f'''{prefix}.in_proj_bias'''].chunk(3 ,    dim=0	)

				lowerCAmelCase		=			pt_weights[f'''{prefix}.out_proj.weight''']
				lowerCAmelCase		=			pt_weights[f'''{prefix}.out_proj.bias''']

				lowerCAmelCase		=			q_proj
				lowerCAmelCase		=			q_proj_bias

				lowerCAmelCase		=			k_proj
				lowerCAmelCase		=			k_proj_bias

				lowerCAmelCase		=			v_proj
				lowerCAmelCase		=			v_proj_bias

				lowerCAmelCase		=			out_proj_weights
				lowerCAmelCase		=			out_proj_bias





def 							lowercase   (snake_case__				:	Union[str, Any] ,    snake_case__				:	Optional[Any] ,    snake_case__				:	str	)       ->					Tuple:


				'''simple docstring'''






				copy_linear(hf_mlp.fca ,    snake_case__ ,    f'''{prefix}.c_fc'''	)
				copy_linear(hf_mlp.fca ,    snake_case__ ,    f'''{prefix}.c_proj'''	)





def 							lowercase   (snake_case__				:	Union[str, Any] ,    snake_case__				:	int ,    snake_case__				:	Tuple	)       ->					Union[str, Any]:


				'''simple docstring'''






				lowerCAmelCase		=			pt_weights[f'''{prefix}.weight'''].data
				lowerCAmelCase		=			pt_weights[f'''{prefix}.bias'''].data





def 							lowercase   (snake_case__				:	int ,    snake_case__				:	str ,    snake_case__				:	str	)       ->					List[str]:


				'''simple docstring'''






				copy_linear(hf_layer.layer_norma ,    snake_case__ ,    f'''{prefix}.ln_1'''	)
				copy_linear(hf_layer.layer_norma ,    snake_case__ ,    f'''{prefix}.ln_2'''	)

				# copy MLP
				copy_mlp(hf_layer.mlp ,    snake_case__ ,    f'''{prefix}.mlp'''	)

				# copy attn
				copy_attn_layer(hf_layer.self_attn ,    snake_case__ ,    f'''{prefix}.attn'''	)





def 							lowercase   (snake_case__				:	Tuple ,    snake_case__				:	Dict ,    snake_case__				:	int	)       ->					Union[str, Any]:


				'''simple docstring'''






				for layer_id, hf_layer in enumerate(snake_case__	):
								copy_layer(snake_case__ ,    snake_case__ ,    f'''{prefix}.{layer_id}'''	)





def 							lowercase   (snake_case__				:	Optional[int] ,    snake_case__				:	Dict	)       ->					Dict:


				'''simple docstring'''






				lowerCAmelCase		=			pt_weights["""text_projection"""].data.T

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





def 							lowercase   (snake_case__				:	Union[str, Any] ,    snake_case__				:	List[str]	)       ->					Optional[Any]:


				'''simple docstring'''






				lowerCAmelCase		=			pt_weights["""visual.proj"""].data.T

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

				# copy embeddings
				lowerCAmelCase		=			pt_weights["""visual.conv1.weight"""].data
				lowerCAmelCase		=			pt_weights["""visual.class_embedding"""].data
				lowerCAmelCase		=			pt_weights["""visual.positional_embedding"""].data

				# copy encoder
				copy_layers(hf_model.vision_model.encoder.layers ,    snake_case__ ,    """visual.transformer.resblocks"""	)





@torch.no_grad()
def 							lowercase   (snake_case__				:	int ,    snake_case__				:	Any ,    snake_case__				:	Tuple=None	)       ->					str:


				'''simple docstring'''






				assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
				lowerCAmelCase		=			ChineseCLIPConfig.from_pretrained(snake_case__	)

				lowerCAmelCase		=			ChineseCLIPModel(snake_case__	).eval()

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

				copy_text_model_and_projection(snake_case__ ,    snake_case__	)
				copy_vision_model_and_projection(snake_case__ ,    snake_case__	)
				lowerCAmelCase		=			pt_weights["""logit_scale"""].data

				hf_model.save_pretrained(snake_case__	)


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 							lowercase   (snake_case__				:	list	)       ->					bool:


				'''simple docstring'''






				if not isinstance(snake_case__ ,    snake_case__	):
								raise ValueError("""Input series is not valid, valid series - [2, 4, 8]"""	)
				if len(snake_case__	) == 0:
								raise ValueError("""Input list must be a non empty list"""	)
				if len(snake_case__	) == 1:
								return True
				try:
								lowerCAmelCase		=			series[1] / series[0]
								for index in range(len(snake_case__	) - 1	):
												if series[index + 1] / series[index] != common_ratio:
																return False
				except ZeroDivisionError:
								return False
				return True





def 							lowercase   (snake_case__				:	list	)       ->					float:


				'''simple docstring'''






				if not isinstance(snake_case__ ,    snake_case__	):
								raise ValueError("""Input series is not valid, valid series - [2, 4, 8]"""	)
				if len(snake_case__	) == 0:
								raise ValueError("""Input list must be a non empty list"""	)
				lowerCAmelCase		=			1
				for value in series:
								answer *= value
				return pow(snake_case__ ,    1 / len(snake_case__	)	)


if __name__ == "__main__":
		import doctest

		doctest.testmod()





