






import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel


def __A   (				__lowerCAmelCase			,		__lowerCAmelCase			,		__lowerCAmelCase     )->  Tuple:





						"""simple docstring"""



						_UpperCAmelCase ,			_UpperCAmelCase ,			_UpperCAmelCase     =      pt_weights[F"""{prefix}.in_proj_weight"""].chunk(3			,		dim=0     )
						_UpperCAmelCase ,			_UpperCAmelCase ,			_UpperCAmelCase     =      pt_weights[F"""{prefix}.in_proj_bias"""].chunk(3			,		dim=0     )

						_UpperCAmelCase     =      pt_weights[F"""{prefix}.out_proj.weight"""]
						_UpperCAmelCase     =      pt_weights[F"""{prefix}.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 __A   (				__lowerCAmelCase			,		__lowerCAmelCase			,		__lowerCAmelCase     )->  Tuple:





						"""simple docstring"""



						copy_linear(hf_mlp.fca			,		__lowerCAmelCase			,		F"""{prefix}.c_fc"""     )
						copy_linear(hf_mlp.fca			,		__lowerCAmelCase			,		F"""{prefix}.c_proj"""     )


def __A   (				__lowerCAmelCase			,		__lowerCAmelCase			,		__lowerCAmelCase     )->  List[Any]:





						"""simple docstring"""



						_UpperCAmelCase     =      pt_weights[F"""{prefix}.weight"""].data
						_UpperCAmelCase     =      pt_weights[F"""{prefix}.bias"""].data


def __A   (				__lowerCAmelCase			,		__lowerCAmelCase			,		__lowerCAmelCase     )->  Dict:





						"""simple docstring"""



						copy_linear(hf_layer.layer_norma			,		__lowerCAmelCase			,		F"""{prefix}.ln_1"""     )
						copy_linear(hf_layer.layer_norma			,		__lowerCAmelCase			,		F"""{prefix}.ln_2"""     )

						# copy MLP
						copy_mlp(hf_layer.mlp			,		__lowerCAmelCase			,		F"""{prefix}.mlp"""     )

						# copy attn
						copy_attn_layer(hf_layer.self_attn			,		__lowerCAmelCase			,		F"""{prefix}.attn"""     )


def __A   (				__lowerCAmelCase			,		__lowerCAmelCase			,		__lowerCAmelCase     )->  List[Any]:





						"""simple docstring"""



						for layer_id, hf_layer in enumerate(__lowerCAmelCase     ):
												copy_layer(__lowerCAmelCase			,		__lowerCAmelCase			,		F"""{prefix}.{layer_id}"""     )


def __A   (				__lowerCAmelCase			,		__lowerCAmelCase     )->  Optional[Any]:





						"""simple docstring"""



						_UpperCAmelCase     =      pt_weights['text_projection'].data.T

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


def __A   (				__lowerCAmelCase			,		__lowerCAmelCase     )->  Union[str, Any]:





						"""simple docstring"""



						_UpperCAmelCase     =      pt_weights['visual.proj'].data.T

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

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

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


@torch.no_grad()
def __A   (				__lowerCAmelCase			,		__lowerCAmelCase			,		__lowerCAmelCase=None     )->  Dict:





						"""simple docstring"""



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

						_UpperCAmelCase     =      ChineseCLIPModel(__lowerCAmelCase     ).eval()

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

						copy_text_model_and_projection(__lowerCAmelCase			,		__lowerCAmelCase     )
						copy_vision_model_and_projection(__lowerCAmelCase			,		__lowerCAmelCase     )
						_UpperCAmelCase     =      pt_weights['logit_scale'].data

						hf_model.save_pretrained(__lowerCAmelCase     )


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!''')








from ..utils import DummyObject, requires_backends





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )





class __lowerCamelCase			(				metaclass=snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=				["tokenizers"]

	def __init__(							self	,   *UpperCAmelCase	,   **UpperCAmelCase   ):
							"""simple docstring"""







							requires_backends(self	,   ['tokenizers']   )
