


import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel

def 			__SCREAMING_SNAKE_CASE		(		UpperCamelCase		:      List[str]     ,					UpperCamelCase		:      Any     ,					UpperCamelCase		:      Dict	)     ->	Dict:



	"""simple docstring"""






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

	a_     			=   pt_weights[F"""{prefix}.out_proj.weight"""]
	a_     			=   pt_weights[F"""{prefix}.out_proj.bias"""]

	a_     			=   q_proj
	a_     			=   q_proj_bias

	a_     			=   k_proj
	a_     			=   k_proj_bias

	a_     			=   v_proj
	a_     			=   v_proj_bias

	a_     			=   out_proj_weights
	a_     			=   out_proj_bias

def 			__SCREAMING_SNAKE_CASE		(		UpperCamelCase		:      Tuple     ,					UpperCamelCase		:      Dict     ,					UpperCamelCase		:      Any	)     ->	Optional[int]:



	"""simple docstring"""






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

def 			__SCREAMING_SNAKE_CASE		(		UpperCamelCase		:      Optional[Any]     ,					UpperCamelCase		:      Dict     ,					UpperCamelCase		:      List[Any]	)     ->	Tuple:



	"""simple docstring"""






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

def 			__SCREAMING_SNAKE_CASE		(		UpperCamelCase		:      str     ,					UpperCamelCase		:      Any     ,					UpperCamelCase		:      str	)     ->	Union[str, Any]:



	"""simple docstring"""






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

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

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

def 			__SCREAMING_SNAKE_CASE		(		UpperCamelCase		:      Optional[Any]     ,					UpperCamelCase		:      Union[str, Any]     ,					UpperCamelCase		:      int	)     ->	int:



	"""simple docstring"""






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

def 			__SCREAMING_SNAKE_CASE		(		UpperCamelCase		:      Optional[Any]     ,					UpperCamelCase		:      Optional[int]	)     ->	List[str]:



	"""simple docstring"""






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

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

def 			__SCREAMING_SNAKE_CASE		(		UpperCamelCase		:      List[Any]     ,					UpperCamelCase		:      Union[str, Any]	)     ->	List[str]:



	"""simple docstring"""






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

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

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

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

@torch.no_grad()
def 			__SCREAMING_SNAKE_CASE		(		UpperCamelCase		:      str     ,					UpperCamelCase		:      str     ,					UpperCamelCase		:      Optional[int]=None	)     ->	str:



	"""simple docstring"""






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

	a_     			=   ChineseCLIPModel(__A	).eval()

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

	copy_text_model_and_projection(__A     ,					__A	)
	copy_vision_model_and_projection(__A     ,					__A	)
	a_     			=   pt_weights["""logit_scale"""].data

	hf_model.save_pretrained(__A	)


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"""



import warnings

from ...utils import logging
from .image_processing_vit import ViTImageProcessor


_snake_case							:   Tuple	  =       logging.get_logger(__name__)




class 							__lowercase(							_UpperCamelCase	):






						def __init__(       self     ,    *_SCREAMING_SNAKE_CASE     ,    **_SCREAMING_SNAKE_CASE)      ->	None:




											"""simple docstring"""





											warnings.warn(
											    'The class ViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please'
											    ' use ViTImageProcessor instead.'     ,    _SCREAMING_SNAKE_CASE     ,    )
											super().__init__(*_SCREAMING_SNAKE_CASE     ,    **_SCREAMING_SNAKE_CASE)