






'''simple docstring'''





import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel



def 			__UpperCAmelCase				(lowercase__	,lowercase__	,lowercase__				)		->				Optional[Any]:

						'''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 			__UpperCAmelCase				(lowercase__	,lowercase__	,lowercase__				)		->				int:

						'''simple docstring'''


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



def 			__UpperCAmelCase				(lowercase__	,lowercase__	,lowercase__				)		->				int:

						'''simple docstring'''


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



def 			__UpperCAmelCase				(lowercase__	,lowercase__	,lowercase__				)		->				int:

						'''simple docstring'''


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

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

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



def 			__UpperCAmelCase				(lowercase__	,lowercase__	,lowercase__				)		->				List[str]:

						'''simple docstring'''


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



def 			__UpperCAmelCase				(lowercase__	,lowercase__				)		->				Tuple:

						'''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 			__UpperCAmelCase				(lowercase__	,lowercase__				)		->				List[str]:

						'''simple docstring'''


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

						# copy layer norms
						copy_linear(hf_model.vision_model.pre_layrnorm	,lowercase__	,"visual.ln_pre"				)
						copy_linear(hf_model.vision_model.post_layernorm	,lowercase__	,"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	,lowercase__	,"visual.transformer.resblocks"				)



@torch.no_grad()
def 			__UpperCAmelCase				(lowercase__	,lowercase__	,lowercase__=None				)		->				Any:

						'''simple docstring'''


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

						a_				     =   ChineseCLIPModel(lowercase__				).eval()

						a_				     =   torch.load(lowercase__	,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(lowercase__	,lowercase__				)
						copy_vision_model_and_projection(lowercase__	,lowercase__				)
						a_				     =   pt_weights["logit_scale"].data

						hf_model.save_pretrained(lowercase__				)


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 typing import List, Optional, Tuple, Union

from . import logging
from .import_utils import is_torch_available, is_torch_version


if is_torch_available():
							import torch

a_      = logging.get_logger(__name__)  # pylint: disable=invalid-name

try:
							from torch._dynamo import allow_in_graph as maybe_allow_in_graph
except (ImportError, ModuleNotFoundError):



							def 			__UpperCAmelCase				(cls				)		->				Optional[Any]:

													'''simple docstring'''


													return cls



def 			__UpperCAmelCase				(lowercase__	,lowercase__ = None	,lowercase__ = None	,lowercase__ = None	,lowercase__ = None	,)		->				Union[str, Any]:

						'''simple docstring'''


						a_				     =   device
						a_				     =   shape[0]

						a_				     =   layout or torch.strided
						a_				     =   device or torch.device("cpu"				)

						if generator is not None:
												a_				     =   generator.device.type if not isinstance(lowercase__	,lowercase__				) else generator[0].device.type
												if gen_device_type != device.type and gen_device_type == "cpu":
																		a_				     =   "cpu"
																		if device != "mps":
																								logger.info(
																								    F"""The passed generator was created on 'cpu' even though a tensor on {device} was expected."""
																								    F""" Tensors will be created on 'cpu' and then moved to {device}. Note that one can probably"""
																								    F""" slighly speed up this function by passing a generator that was created on the {device} device."""				)
												elif gen_device_type != device.type and gen_device_type == "cuda":
																		raise ValueError(F"""Cannot generate a {device} tensor from a generator of type {gen_device_type}."""				)

						if isinstance(lowercase__	,lowercase__				):
												a_				     =   (1,) + shape[1:]
												a_				     =   [
												    torch.randn(lowercase__	,generator=generator[i]	,device=lowercase__	,dtype=lowercase__	,layout=lowercase__				)
												    for i in range(lowercase__				)
												]
												a_				     =   torch.cat(lowercase__	,dim=0				).to(lowercase__				)
						else:
												a_				     =   torch.randn(lowercase__	,generator=lowercase__	,device=lowercase__	,dtype=lowercase__	,layout=lowercase__				).to(lowercase__				)

						return latents



def 			__UpperCAmelCase				(lowercase__				)		->				Tuple:

						'''simple docstring'''


						if is_torch_version("<"	,"2.0.0"				) or not hasattr(lowercase__	,"_dynamo"				):
												return False
						return isinstance(lowercase__	,torch._dynamo.eval_frame.OptimizedModule				)



