






import math
from collections import OrderedDict

import torch
from packaging import version
from torch import Tensor, nn

from .utils import logging


_a       =    logging.get_logger(__name__)





class __lowerCamelCase					( nn.Module):





	"""simple docstring"""






	def __init__(				self       ):
							"""simple docstring"""







							super().__init__()
							if version.parse(torch.__version__       ) < version.parse('1.12.0'       ):
													raise ImportError(
													    F"""You are using torch=={torch.__version__}, but torch>=1.12.0 is required to use """
													    'PytorchGELUTanh. Please upgrade torch.'       )

	def 			UpperCamelCase       (				self			,       UpperCAmelCase       ):
							"""simple docstring"""







							return nn.functional.gelu(lowercase_			,       approximate='tanh'       )





class __lowerCamelCase					( nn.Module):





	"""simple docstring"""






	def 			UpperCamelCase       (				self			,       UpperCAmelCase       ):
							"""simple docstring"""







							return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi       ) * (input + 0.04_47_15 * torch.pow(lowercase_			,       3.0       ))       ))





class __lowerCamelCase					( nn.Module):





	"""simple docstring"""






	def __init__(				self			,       UpperCAmelCase = False       ):
							"""simple docstring"""







							super().__init__()
							if use_gelu_python:
													_UpperCAmelCase							   =     self._gelu_python
							else:
													_UpperCAmelCase							   =     nn.functional.gelu

	def 			UpperCamelCase       (				self			,       UpperCAmelCase       ):
							"""simple docstring"""







							return input * 0.5 * (1.0 + torch.erf(input / math.sqrt(2.0       )       ))

	def 			UpperCamelCase       (				self			,       UpperCAmelCase       ):
							"""simple docstring"""







							return self.act(lowercase_       )





class __lowerCamelCase					( nn.Module):





	"""simple docstring"""






	def 			UpperCamelCase       (				self			,       UpperCAmelCase       ):
							"""simple docstring"""







							return 0.5 * input * (1.0 + torch.tanh(input * 0.79_78_84_56_08 * (1.0 + 0.04_47_15 * input * input)       ))





class __lowerCamelCase					( nn.Module):





	"""simple docstring"""






	def 			UpperCamelCase       (				self			,       UpperCAmelCase       ):
							"""simple docstring"""







							return input * torch.sigmoid(1.7_02 * input       )





class __lowerCamelCase					( nn.Module):





	"""simple docstring"""






	def __init__(				self			,       UpperCAmelCase			,       UpperCAmelCase       ):
							"""simple docstring"""







							if min > max:
													raise ValueError(F"""min should be < max (got min: {min}, max: {max})"""       )

							super().__init__()
							_UpperCAmelCase							   =     min
							_UpperCAmelCase							   =     max

	def 			UpperCamelCase       (				self			,       UpperCAmelCase       ):
							"""simple docstring"""







							return torch.clip(gelu(lowercase_       )			,       self.min			,       self.max       )





class __lowerCamelCase					( nn.Module):





	"""simple docstring"""






	def __init__(				self       ):
							"""simple docstring"""







							super().__init__()
							_UpperCAmelCase							   =     math.sqrt(2 / math.pi       )

	def 			UpperCamelCase       (				self			,       UpperCAmelCase       ):
							"""simple docstring"""







							return 0.5 * input * (1 + torch.tanh(self.precomputed_constant * (input + 0.04_47_15 * torch.pow(lowercase_			,       3       ))       ))





class __lowerCamelCase					( nn.Module):





	"""simple docstring"""






	def 			UpperCamelCase       (				self			,       UpperCAmelCase       ):
							"""simple docstring"""







							return nn.functional.silu(lowercase_       )





class __lowerCamelCase					( nn.Module):





	"""simple docstring"""






	def __init__(				self       ):
							"""simple docstring"""







							super().__init__()
							if version.parse(torch.__version__       ) < version.parse('1.9.0'       ):
													_UpperCAmelCase							   =     self._mish_python
							else:
													_UpperCAmelCase							   =     nn.functional.mish

	def 			UpperCamelCase       (				self			,       UpperCAmelCase       ):
							"""simple docstring"""







							return input * torch.tanh(nn.functional.softplus(lowercase_       )       )

	def 			UpperCamelCase       (				self			,       UpperCAmelCase       ):
							"""simple docstring"""







							return self.act(lowercase_       )





class __lowerCamelCase					( nn.Module):





	"""simple docstring"""






	def 			UpperCamelCase       (				self			,       UpperCAmelCase       ):
							"""simple docstring"""







							return input





class __lowerCamelCase					( nn.Module):





	"""simple docstring"""






	def 			UpperCamelCase       (				self			,       UpperCAmelCase			,       UpperCAmelCase=0.70_71_07			,       UpperCAmelCase=0.28_20_95       ):
							"""simple docstring"""







							_UpperCAmelCase							   =     (input - mu).div(sigma * math.sqrt(2.0       )       )
							return 0.5 * (1.0 + torch.erf(lowercase_       ))





class __lowerCamelCase					( nn.Module):





	"""simple docstring"""






	def 			UpperCamelCase       (				self			,       UpperCAmelCase       ):
							"""simple docstring"""







							_UpperCAmelCase							   =     nn.functional.relu(lowercase_       )
							_UpperCAmelCase							   =     torch.square(lowercase_       )
							return squared





class __lowerCamelCase					( _snake_case):





	"""simple docstring"""






	def __getitem__(				self			,       UpperCAmelCase       ):
							"""simple docstring"""







							_UpperCAmelCase							   =     super().__getitem__(lowercase_       )
							_UpperCAmelCase							   =     content if isinstance(lowercase_			,       lowercase_       ) else (content, {})
							return cls(**lowercase_       )



_a       =    {
    '''gelu''': GELUActivation,
    '''gelu_10''': (ClippedGELUActivation, {'''min''': -10, '''max''': 10}),
    '''gelu_fast''': FastGELUActivation,
    '''gelu_new''': NewGELUActivation,
    '''gelu_python''': (GELUActivation, {'''use_gelu_python''': True}),
    '''gelu_pytorch_tanh''': PytorchGELUTanh,
    '''gelu_accurate''': AccurateGELUActivation,
    '''laplace''': LaplaceActivation,
    '''linear''': LinearActivation,
    '''mish''': MishActivation,
    '''quick_gelu''': QuickGELUActivation,
    '''relu''': nn.ReLU,
    '''relu2''': ReLUSquaredActivation,
    '''relu6''': nn.ReLUa,
    '''sigmoid''': nn.Sigmoid,
    '''silu''': SiLUActivation,
    '''swish''': SiLUActivation,
    '''tanh''': nn.Tanh,
}
_a       =    ClassInstantier(ACTaCLS)


def __A       ( __lowerCAmelCase      )->    Union[str, Any]:





						"""simple docstring"""



						if activation_string in ACTaFN:
												return ACTaFN[activation_string]
						else:
												raise KeyError(F"""function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()      )}"""      )


# For backwards compatibility with: from activations import gelu_python
_a       =    get_activation('''gelu_python''')
_a       =    get_activation('''gelu_new''')
_a       =    get_activation('''gelu''')
_a       =    get_activation('''gelu_fast''')
_a       =    get_activation('''quick_gelu''')
_a       =    get_activation('''silu''')
_a       =    get_activation('''mish''')

_a       =    get_activation('''linear''')



'''simple docstring'''

import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel




def  UpperCamelCase(	UpperCAmelCase_    ,			UpperCAmelCase_    ,			UpperCAmelCase_      ):
 UpperCAmelCase   ,					UpperCAmelCase   ,					UpperCAmelCase			:      Optional[Any]		   =					pt_weights[F"""{prefix}.in_proj_weight"""].chunk(3    ,			dim=0      )
 UpperCAmelCase   ,					UpperCAmelCase   ,					UpperCAmelCase			:      str		   =					pt_weights[F"""{prefix}.in_proj_bias"""].chunk(3    ,			dim=0      )

 UpperCAmelCase			:      Dict		   =					pt_weights[F"""{prefix}.out_proj.weight"""]
 UpperCAmelCase			:      Tuple		   =					pt_weights[F"""{prefix}.out_proj.bias"""]

 UpperCAmelCase			:      Dict		   =					q_proj
 UpperCAmelCase			:      Optional[Any]		   =					q_proj_bias

 UpperCAmelCase			:      Union[str, Any]		   =					k_proj
 UpperCAmelCase			:      Optional[Any]		   =					k_proj_bias

 UpperCAmelCase			:      Dict		   =					v_proj
 UpperCAmelCase			:      Tuple		   =					v_proj_bias

 UpperCAmelCase			:      List[Any]		   =					out_proj_weights
 UpperCAmelCase			:      Any		   =					out_proj_bias




def  UpperCamelCase(	UpperCAmelCase_    ,			UpperCAmelCase_    ,			UpperCAmelCase_      ):
 copy_linear(hf_mlp.fca    ,			UpperCAmelCase_    ,			F"""{prefix}.c_fc"""      )
 copy_linear(hf_mlp.fca    ,			UpperCAmelCase_    ,			F"""{prefix}.c_proj"""      )




def  UpperCamelCase(	UpperCAmelCase_    ,			UpperCAmelCase_    ,			UpperCAmelCase_      ):
 UpperCAmelCase			:      Tuple		   =					pt_weights[F"""{prefix}.weight"""].data
 UpperCAmelCase			:      Dict		   =					pt_weights[F"""{prefix}.bias"""].data




def  UpperCamelCase(	UpperCAmelCase_    ,			UpperCAmelCase_    ,			UpperCAmelCase_      ):
 # 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  UpperCamelCase(	UpperCAmelCase_    ,			UpperCAmelCase_    ,			UpperCAmelCase_      ):
 for layer_id, hf_layer in enumerate(UpperCAmelCase_      ):
  copy_layer(UpperCAmelCase_    ,			UpperCAmelCase_    ,			F"""{prefix}.{layer_id}"""      )




def  UpperCamelCase(	UpperCAmelCase_    ,			UpperCAmelCase_      ):
 # copy projection
 UpperCAmelCase			:      Optional[int]		   =					pt_weights['text_projection'].data.T

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




def  UpperCamelCase(	UpperCAmelCase_    ,			UpperCAmelCase_      ):
 # copy projection
 UpperCAmelCase			:      Optional[int]		   =					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
 UpperCAmelCase			:      List[str]		   =					pt_weights['visual.conv1.weight'].data
 UpperCAmelCase			:      Tuple		   =					pt_weights['visual.class_embedding'].data
 UpperCAmelCase			:      str		   =					pt_weights['visual.positional_embedding'].data

 # copy encoder
 copy_layers(hf_model.vision_model.encoder.layers    ,			UpperCAmelCase_    ,			'visual.transformer.resblocks'      )




@torch.no_grad()
def  UpperCamelCase(	UpperCAmelCase_    ,			UpperCAmelCase_    ,			UpperCAmelCase_=None      ):
 assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
 UpperCAmelCase			:      str		   =					ChineseCLIPConfig.from_pretrained(UpperCAmelCase_      )

 UpperCAmelCase			:      List[Any]		   =					ChineseCLIPModel(UpperCAmelCase_      ).eval()

 UpperCAmelCase			:      Optional[Any]		   =					torch.load(UpperCAmelCase_    ,			map_location='cpu'      )['state_dict']
 UpperCAmelCase			:      Any		   =					{(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_      )
 UpperCAmelCase			:      Optional[int]		   =					pt_weights['logit_scale'].data

 hf_model.save_pretrained(UpperCAmelCase_      )


if __name__ == "__main__":
 lowercase__            =     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."
 )
 lowercase__            =     parser.parse_args()

 convert_chinese_clip_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
 print("The conversion is finished!")




