'''simple docstring'''

import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel


def        UpperCAmelCase__   (				_lowerCAmelCase    :			Any,      _lowerCAmelCase    :			List[Any],      _lowerCAmelCase    :			Optional[Any])    ->		List[str]:



						"""simple docstring"""

						snake_case				,					snake_case				,					snake_case :       Union[str, Any]				=    pt_weights[f"""{prefix}.in_proj_weight"""].chunk(3,      dim=0)
						snake_case				,					snake_case				,					snake_case :       Optional[int]				=    pt_weights[f"""{prefix}.in_proj_bias"""].chunk(3,      dim=0)

						snake_case :       List[Any]				=    pt_weights[f"""{prefix}.out_proj.weight"""]
						snake_case :       List[Any]				=    pt_weights[f"""{prefix}.out_proj.bias"""]

						snake_case :       Dict				=    q_proj
						snake_case :       Tuple				=    q_proj_bias

						snake_case :       Any				=    k_proj
						snake_case :       str				=    k_proj_bias

						snake_case :       Optional[Any]				=    v_proj
						snake_case :       Tuple				=    v_proj_bias

						snake_case :       Optional[int]				=    out_proj_weights
						snake_case :       str				=    out_proj_bias


def        UpperCAmelCase__   (				_lowerCAmelCase    :			Optional[Any],      _lowerCAmelCase    :			Any,      _lowerCAmelCase    :			Any)    ->		str:



						"""simple docstring"""

						copy_linear(hf_mlp.fca,      _lowerCAmelCase,      f"""{prefix}.c_fc""")
						copy_linear(hf_mlp.fca,      _lowerCAmelCase,      f"""{prefix}.c_proj""")


def        UpperCAmelCase__   (				_lowerCAmelCase    :			int,      _lowerCAmelCase    :			Union[str, Any],      _lowerCAmelCase    :			List[str])    ->		Any:



						"""simple docstring"""

						snake_case :       int				=    pt_weights[f"""{prefix}.weight"""].data
						snake_case :       Optional[Any]				=    pt_weights[f"""{prefix}.bias"""].data


def        UpperCAmelCase__   (				_lowerCAmelCase    :			str,      _lowerCAmelCase    :			Optional[Any],      _lowerCAmelCase    :			List[Any])    ->		str:



						"""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        UpperCAmelCase__   (				_lowerCAmelCase    :			Union[str, Any],      _lowerCAmelCase    :			List[Any],      _lowerCAmelCase    :			List[Any])    ->		int:



						"""simple docstring"""

						for layer_id, hf_layer in enumerate(_lowerCAmelCase):
												copy_layer(_lowerCAmelCase,      _lowerCAmelCase,      f"""{prefix}.{layer_id}""")


def        UpperCAmelCase__   (				_lowerCAmelCase    :			str,      _lowerCAmelCase    :			Dict)    ->		int:



						"""simple docstring"""

						snake_case :       str				=    pt_weights["text_projection"].data.T

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


def        UpperCAmelCase__   (				_lowerCAmelCase    :			Any,      _lowerCAmelCase    :			int)    ->		str:



						"""simple docstring"""

						snake_case :       List[str]				=    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
						snake_case :       List[str]				=    pt_weights["visual.conv1.weight"].data
						snake_case :       Dict				=    pt_weights["visual.class_embedding"].data
						snake_case :       Tuple				=    pt_weights["visual.positional_embedding"].data

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


@torch.no_grad()
def        UpperCAmelCase__   (				_lowerCAmelCase    :			Tuple,      _lowerCAmelCase    :			Optional[int],      _lowerCAmelCase    :			Tuple=None)    ->		str:



						"""simple docstring"""

						assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
						snake_case :       Optional[int]				=    ChineseCLIPConfig.from_pretrained(_lowerCAmelCase)

						snake_case :       List[Any]				=    ChineseCLIPModel(_lowerCAmelCase).eval()

						snake_case :       Dict				=    torch.load(_lowerCAmelCase,      map_location="cpu")["state_dict"]
						snake_case :       Any				=    {(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)
						snake_case :       int				=    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!")






'''simple docstring'''

from __future__ import annotations


def        UpperCAmelCase__   (				_lowerCAmelCase    :			list[int | float])    ->		int | float:



						"""simple docstring"""

						if len(_lowerCAmelCase) == 0:
												raise ValueError("find_max() arg is an empty sequence")
						snake_case :       Any				=    nums[0]
						for x in nums:
												if x > max_num:
																		snake_case :       int				=    x
						return max_num


if __name__ == "__main__":
						import doctest

						doctest.testmod(verbose=True)





