

import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel




def        __UpperCamelCase     (      _A					:							List[str]      ,  _A					:							str      ,  _A					:							List[Any]		)				->List[Any]:



							"""simple docstring"""
							lowerCamelCase_	,   lowerCamelCase_	,   lowerCamelCase_								=pt_weights[f'{prefix}.in_proj_weight'].chunk(3      ,  dim=0		)
							lowerCamelCase_	,   lowerCamelCase_	,   lowerCamelCase_								=pt_weights[f'{prefix}.in_proj_bias'].chunk(3      ,  dim=0		)

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

							lowerCamelCase_								=q_proj
							lowerCamelCase_								=q_proj_bias

							lowerCamelCase_								=k_proj
							lowerCamelCase_								=k_proj_bias

							lowerCamelCase_								=v_proj
							lowerCamelCase_								=v_proj_bias

							lowerCamelCase_								=out_proj_weights
							lowerCamelCase_								=out_proj_bias




def        __UpperCamelCase     (      _A					:							Union[str, Any]      ,  _A					:							Tuple      ,  _A					:							List[str]		)				->Optional[Any]:



							"""simple docstring"""
							copy_linear(hf_mlp.fca      ,  _A      ,  f'{prefix}.c_fc'		)
							copy_linear(hf_mlp.fca      ,  _A      ,  f'{prefix}.c_proj'		)




def        __UpperCamelCase     (      _A					:							Any      ,  _A					:							str      ,  _A					:							List[str]		)				->Dict:



							"""simple docstring"""
							lowerCamelCase_								=pt_weights[f'{prefix}.weight'].data
							lowerCamelCase_								=pt_weights[f'{prefix}.bias'].data




def        __UpperCamelCase     (      _A					:							str      ,  _A					:							Optional[int]      ,  _A					:							Tuple		)				->Tuple:



							"""simple docstring"""
							# copy layer norms
							copy_linear(hf_layer.layer_norma      ,  _A      ,  f'{prefix}.ln_1'		)
							copy_linear(hf_layer.layer_norma      ,  _A      ,  f'{prefix}.ln_2'		)

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

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




def        __UpperCamelCase     (      _A					:							List[Any]      ,  _A					:							Tuple      ,  _A					:							Dict		)				->Union[str, Any]:



							"""simple docstring"""
							for layer_id, hf_layer in enumerate(_A		):
														copy_layer(_A      ,  _A      ,  f'{prefix}.{layer_id}'		)




def        __UpperCamelCase     (      _A					:							List[Any]      ,  _A					:							str		)				->int:



							"""simple docstring"""
							# copy projection
							lowerCamelCase_								=pt_weights["""text_projection"""].data.T

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




def        __UpperCamelCase     (      _A					:							Union[str, Any]      ,  _A					:							List[Any]		)				->Optional[Any]:



							"""simple docstring"""
							# copy projection
							lowerCamelCase_								=pt_weights["""visual.proj"""].data.T

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

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

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




@torch.no_grad()
def        __UpperCamelCase     (      _A					:							str      ,  _A					:							Union[str, Any]      ,  _A					:							Any=None		)				->Any:



							"""simple docstring"""
							assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
							lowerCamelCase_								=ChineseCLIPConfig.from_pretrained(_A		)

							lowerCamelCase_								=ChineseCLIPModel(_A		).eval()

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

							copy_text_model_and_projection(_A      ,  _A		)
							copy_vision_model_and_projection(_A      ,  _A		)
							lowerCamelCase_								=pt_weights["""logit_scale"""].data

							hf_model.save_pretrained(_A		)


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

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



from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__A	:   List[Any]	=					logging.get_logger(__name__)

__A	:   Tuple	=					{
    'google/electra-small-generator': 'https://huggingface.co/google/electra-small-generator/resolve/main/config.json',
    'google/electra-base-generator': 'https://huggingface.co/google/electra-base-generator/resolve/main/config.json',
    'google/electra-large-generator': 'https://huggingface.co/google/electra-large-generator/resolve/main/config.json',
    'google/electra-small-discriminator': (
        'https://huggingface.co/google/electra-small-discriminator/resolve/main/config.json'
    ),
    'google/electra-base-discriminator': (
        'https://huggingface.co/google/electra-base-discriminator/resolve/main/config.json'
    ),
    'google/electra-large-discriminator': (
        'https://huggingface.co/google/electra-large-discriminator/resolve/main/config.json'
    ),
}


class 			_SCREAMING_SNAKE_CASE      (  lowerCAmelCase__):
						_UpperCamelCase:Tuple	=		"electra"







						def __init__(			self     ,						_SCREAMING_SNAKE_CASE=3_0522     ,						_SCREAMING_SNAKE_CASE=128     ,						_SCREAMING_SNAKE_CASE=256     ,						_SCREAMING_SNAKE_CASE=12     ,						_SCREAMING_SNAKE_CASE=4     ,						_SCREAMING_SNAKE_CASE=1024     ,						_SCREAMING_SNAKE_CASE="gelu"     ,						_SCREAMING_SNAKE_CASE=0.1     ,						_SCREAMING_SNAKE_CASE=0.1     ,						_SCREAMING_SNAKE_CASE=512     ,						_SCREAMING_SNAKE_CASE=2     ,						_SCREAMING_SNAKE_CASE=0.0_2     ,						_SCREAMING_SNAKE_CASE=1E-12     ,						_SCREAMING_SNAKE_CASE="first"     ,						_SCREAMING_SNAKE_CASE=True     ,						_SCREAMING_SNAKE_CASE="gelu"     ,						_SCREAMING_SNAKE_CASE=0.1     ,						_SCREAMING_SNAKE_CASE=0     ,						_SCREAMING_SNAKE_CASE="absolute"     ,						_SCREAMING_SNAKE_CASE=True     ,						_SCREAMING_SNAKE_CASE=None     ,						**_SCREAMING_SNAKE_CASE     ,						)->							Optional[int]:
													super().__init__(pad_token_id=_SCREAMING_SNAKE_CASE     ,						**_SCREAMING_SNAKE_CASE							)

													lowerCamelCase_								=vocab_size
													lowerCamelCase_								=embedding_size
													lowerCamelCase_								=hidden_size
													lowerCamelCase_								=num_hidden_layers
													lowerCamelCase_								=num_attention_heads
													lowerCamelCase_								=intermediate_size
													lowerCamelCase_								=hidden_act
													lowerCamelCase_								=hidden_dropout_prob
													lowerCamelCase_								=attention_probs_dropout_prob
													lowerCamelCase_								=max_position_embeddings
													lowerCamelCase_								=type_vocab_size
													lowerCamelCase_								=initializer_range
													lowerCamelCase_								=layer_norm_eps

													lowerCamelCase_								=summary_type
													lowerCamelCase_								=summary_use_proj
													lowerCamelCase_								=summary_activation
													lowerCamelCase_								=summary_last_dropout
													lowerCamelCase_								=position_embedding_type
													lowerCamelCase_								=use_cache
													lowerCamelCase_								=classifier_dropout


class 			_SCREAMING_SNAKE_CASE      (  lowerCAmelCase__):







						@property
						def        _snake_case				(			self							)->							Mapping[str, Mapping[int, str]]:
													if self.task == "multiple-choice":
																				lowerCamelCase_								={0: """batch""", 1: """choice""", 2: """sequence"""}
													else:
																				lowerCamelCase_								={0: """batch""", 1: """sequence"""}
													return OrderedDict(
													    [
													        ("""input_ids""", dynamic_axis),
													        ("""attention_mask""", dynamic_axis),
													        ("""token_type_ids""", dynamic_axis),
													    ]							)
