





import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel






def  SCREAMING_SNAKE_CASE__							(				UpperCamelCase__:							str    ,      UpperCamelCase__:							str    ,      UpperCamelCase__:							str			):
		SCREAMING_SNAKE_CASE__		,  SCREAMING_SNAKE_CASE__		,  SCREAMING_SNAKE_CASE__								=					pt_weights[f'''{prefix}.in_proj_weight'''].chunk(3    ,      dim=0			)
		SCREAMING_SNAKE_CASE__		,  SCREAMING_SNAKE_CASE__		,  SCREAMING_SNAKE_CASE__								=					pt_weights[f'''{prefix}.in_proj_bias'''].chunk(3    ,      dim=0			)

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

		SCREAMING_SNAKE_CASE__								=					q_proj
		SCREAMING_SNAKE_CASE__								=					q_proj_bias

		SCREAMING_SNAKE_CASE__								=					k_proj
		SCREAMING_SNAKE_CASE__								=					k_proj_bias

		SCREAMING_SNAKE_CASE__								=					v_proj
		SCREAMING_SNAKE_CASE__								=					v_proj_bias

		SCREAMING_SNAKE_CASE__								=					out_proj_weights
		SCREAMING_SNAKE_CASE__								=					out_proj_bias






def  SCREAMING_SNAKE_CASE__							(				UpperCamelCase__:							Any    ,      UpperCamelCase__:							Any    ,      UpperCamelCase__:							str			):
		copy_linear(hf_mlp.fca    ,      UpperCamelCase__    ,      f'''{prefix}.c_fc'''			)
		copy_linear(hf_mlp.fca    ,      UpperCamelCase__    ,      f'''{prefix}.c_proj'''			)






def  SCREAMING_SNAKE_CASE__							(				UpperCamelCase__:							Any    ,      UpperCamelCase__:							int    ,      UpperCamelCase__:							Any			):
		SCREAMING_SNAKE_CASE__								=					pt_weights[f'''{prefix}.weight'''].data
		SCREAMING_SNAKE_CASE__								=					pt_weights[f'''{prefix}.bias'''].data






def  SCREAMING_SNAKE_CASE__							(				UpperCamelCase__:							int    ,      UpperCamelCase__:							Dict    ,      UpperCamelCase__:							List[str]			):
		# copy layer norms
		copy_linear(hf_layer.layer_norma    ,      UpperCamelCase__    ,      f'''{prefix}.ln_1'''			)
		copy_linear(hf_layer.layer_norma    ,      UpperCamelCase__    ,      f'''{prefix}.ln_2'''			)

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

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






def  SCREAMING_SNAKE_CASE__							(				UpperCamelCase__:							Tuple    ,      UpperCamelCase__:							Optional[int]    ,      UpperCamelCase__:							Optional[int]			):
		for layer_id, hf_layer in enumerate(UpperCamelCase__			):
				copy_layer(UpperCamelCase__    ,      UpperCamelCase__    ,      f'''{prefix}.{layer_id}'''			)






def  SCREAMING_SNAKE_CASE__							(				UpperCamelCase__:							Any    ,      UpperCamelCase__:							Optional[int]			):
		# copy projection
		SCREAMING_SNAKE_CASE__								=					pt_weights["""text_projection"""].data.T

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






def  SCREAMING_SNAKE_CASE__							(				UpperCamelCase__:							Tuple    ,      UpperCamelCase__:							str			):
		# copy projection
		SCREAMING_SNAKE_CASE__								=					pt_weights["""visual.proj"""].data.T

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

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

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






@torch.no_grad()
def  SCREAMING_SNAKE_CASE__							(				UpperCamelCase__:							List[str]    ,      UpperCamelCase__:							int    ,      UpperCamelCase__:							int=None			):
		assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
		SCREAMING_SNAKE_CASE__								=					ChineseCLIPConfig.from_pretrained(UpperCamelCase__			)

		SCREAMING_SNAKE_CASE__								=					ChineseCLIPModel(UpperCamelCase__			).eval()

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

		copy_text_model_and_projection(UpperCamelCase__    ,      UpperCamelCase__			)
		copy_vision_model_and_projection(UpperCamelCase__    ,      UpperCamelCase__			)
		SCREAMING_SNAKE_CASE__								=					pt_weights["""logit_scale"""].data

		hf_model.save_pretrained(UpperCamelCase__			)


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






import unittest

from transformers import is_torch_available, load_tool

from .test_tools_common import ToolTesterMixin


if is_torch_available():
	import torch




class      UpperCamelCase_				(   unittest.TestCase ,     UpperCamelCase__			):


							def    _snake_case     (							self			:Dict						)      ->      Union[str, Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=					load_tool("""speech-to-text"""						)
									self.tool.setup()


							def    _snake_case     (							self			:str						)      ->      Union[str, Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=					self.tool(torch.ones(3000						)						)
									self.assertEqual(__A							, """ you"""						)


							def    _snake_case     (							self			:Union[str, Any]						)      ->      Union[str, Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=					self.tool(audio=torch.ones(3000						)						)
									self.assertEqual(__A							, """ you"""						)