

'''simple docstring'''
import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel


def       a__    (  _SCREAMING_SNAKE_CASE					:    Any     ,							_SCREAMING_SNAKE_CASE					:    List[str]     ,							_SCREAMING_SNAKE_CASE					:    Dict							)			-> Any:


	"""simple docstring"""





	UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_			:	Union[str, Any]      =   pt_weights[F'''{prefix}.in_proj_weight'''].chunk(3     ,							dim=0							)
	UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_			:	List[Any]      =   pt_weights[F'''{prefix}.in_proj_bias'''].chunk(3     ,							dim=0							)

	UpperCAmelCase_			:	int      =   pt_weights[F'''{prefix}.out_proj.weight''']
	UpperCAmelCase_			:	List[str]      =   pt_weights[F'''{prefix}.out_proj.bias''']

	UpperCAmelCase_			:	List[str]      =   q_proj
	UpperCAmelCase_			:	Any      =   q_proj_bias

	UpperCAmelCase_			:	Tuple      =   k_proj
	UpperCAmelCase_			:	str      =   k_proj_bias

	UpperCAmelCase_			:	Any      =   v_proj
	UpperCAmelCase_			:	int      =   v_proj_bias

	UpperCAmelCase_			:	Union[str, Any]      =   out_proj_weights
	UpperCAmelCase_			:	List[str]      =   out_proj_bias


def       a__    (  _SCREAMING_SNAKE_CASE					:    Dict     ,							_SCREAMING_SNAKE_CASE					:    List[Any]     ,							_SCREAMING_SNAKE_CASE					:    Dict							)			-> List[str]:


	"""simple docstring"""





	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       a__    (  _SCREAMING_SNAKE_CASE					:    int     ,							_SCREAMING_SNAKE_CASE					:    Optional[Any]     ,							_SCREAMING_SNAKE_CASE					:    Union[str, Any]							)			-> List[str]:


	"""simple docstring"""





	UpperCAmelCase_			:	Any      =   pt_weights[F'''{prefix}.weight'''].data
	UpperCAmelCase_			:	List[Any]      =   pt_weights[F'''{prefix}.bias'''].data


def       a__    (  _SCREAMING_SNAKE_CASE					:    int     ,							_SCREAMING_SNAKE_CASE					:    str     ,							_SCREAMING_SNAKE_CASE					:    Optional[Any]							)			-> List[str]:


	"""simple docstring"""





	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       a__    (  _SCREAMING_SNAKE_CASE					:    str     ,							_SCREAMING_SNAKE_CASE					:    Any     ,							_SCREAMING_SNAKE_CASE					:    Any							)			-> str:


	"""simple docstring"""





	for layer_id, hf_layer in enumerate(_SCREAMING_SNAKE_CASE							):
		copy_layer(_SCREAMING_SNAKE_CASE     ,							_SCREAMING_SNAKE_CASE     ,							F'''{prefix}.{layer_id}'''							)


def       a__    (  _SCREAMING_SNAKE_CASE					:    Tuple     ,							_SCREAMING_SNAKE_CASE					:    int							)			-> Optional[int]:


	"""simple docstring"""





	UpperCAmelCase_			:	Dict      =   pt_weights["text_projection"].data.T

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


def       a__    (  _SCREAMING_SNAKE_CASE					:    List[str]     ,							_SCREAMING_SNAKE_CASE					:    List[str]							)			-> Optional[int]:


	"""simple docstring"""





	UpperCAmelCase_			:	List[str]      =   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
	UpperCAmelCase_			:	List[str]      =   pt_weights["visual.conv1.weight"].data
	UpperCAmelCase_			:	List[str]      =   pt_weights["visual.class_embedding"].data
	UpperCAmelCase_			:	Union[str, Any]      =   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       a__    (  _SCREAMING_SNAKE_CASE					:    int     ,							_SCREAMING_SNAKE_CASE					:    Union[str, Any]     ,							_SCREAMING_SNAKE_CASE					:    Dict=None							)			-> Optional[Any]:


	"""simple docstring"""





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

	UpperCAmelCase_			:	Optional[int]      =   ChineseCLIPModel(_SCREAMING_SNAKE_CASE							).eval()

	UpperCAmelCase_			:	List[Any]      =   torch.load(_SCREAMING_SNAKE_CASE     ,							map_location="cpu"							)["state_dict"]
	UpperCAmelCase_			:	List[Any]      =   {(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							)
	UpperCAmelCase_			:	List[Any]      =   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!""")







'''simple docstring'''
import os
import unittest

import pytest
from parameterized import parameterized

from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device


# skipping in unittest tests

_lowerCamelCase	 =			[(1,)]


def       a__    (  )			-> str:


	"""simple docstring"""





	UpperCAmelCase_			:	Optional[int]      =   bool(os.getenv("RUN_SLOW"     ,							0							)							)
	if run_slow:
		assert True
	else:
		assert False, "should have been skipped"


def       a__    (  )			-> str:


	"""simple docstring"""





	UpperCAmelCase_			:	Optional[Any]      =   bool(os.getenv("RUN_SLOW"     ,							0							)							)
	if run_slow and torch_device == "cuda":
		assert True
	else:
		assert False, "should have been skipped"
@require_torch
class  _snake_case						(unittest.TestCase):






			@slow
			@require_torch_gpu
			def 						UpperCamelCase__				(   self			):
				check_slow_torch_cuda()






			@require_torch_gpu
			@slow
			def 						UpperCamelCase__				(   self			):
				check_slow_torch_cuda()






			@parameterized.expand(_snake_case			)
			@slow
			def 						UpperCamelCase__				(   self       ,_snake_case=None			):
				check_slow()




@slow
@require_torch_gpu
def       a__    (  )			-> List[Any]:


	"""simple docstring"""





	check_slow_torch_cuda()


@require_torch_gpu
@slow
def       a__    (  )			-> List[str]:


	"""simple docstring"""





	check_slow_torch_cuda()


@slow
@pytest.mark.parametrize("param"     ,							[1]							)
def       a__    (  _SCREAMING_SNAKE_CASE					:    int							)			-> List[Any]:


	"""simple docstring"""





	check_slow()





@pytest.mark.parametrize("param"     ,							[1]							)
@slow
def       a__    (  _SCREAMING_SNAKE_CASE					:    List[str]							)			-> Any:


	"""simple docstring"""





	check_slow()




