




'''simple docstring'''




import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel




def 						snake_case__    ( lowerCamelCase__				:   Tuple			,		lowerCamelCase__				:   Optional[Any]			,		lowerCamelCase__				:   Any			)   ->   Optional[Any]:
					A_ :     Union[str, Any] =							pt_weights[f'{prefix}.in_proj_weight'].chunk(3			,		dim=0			)
					A_ :     int =							pt_weights[f'{prefix}.in_proj_bias'].chunk(3			,		dim=0			)

					A_ :     Union[str, Any] =							pt_weights[f'{prefix}.out_proj.weight']
					A_ :     List[Any] =							pt_weights[f'{prefix}.out_proj.bias']

					A_ :     Dict =							q_proj
					A_ :     Any =							q_proj_bias

					A_ :     str =							k_proj
					A_ :     Tuple =							k_proj_bias

					A_ :     List[Any] =							v_proj
					A_ :     Tuple =							v_proj_bias

					A_ :     List[Any] =							out_proj_weights
					A_ :     Optional[Any] =							out_proj_bias




def 						snake_case__    ( lowerCamelCase__				:   Optional[Any]			,		lowerCamelCase__				:   List[str]			,		lowerCamelCase__				:   List[Any]			)   ->   List[str]:
					copy_linear(hf_mlp.fca			,		UpperCAmelCase_			,		f'{prefix}.c_fc'			)
					copy_linear(hf_mlp.fca			,		UpperCAmelCase_			,		f'{prefix}.c_proj'			)




def 						snake_case__    ( lowerCamelCase__				:   List[Any]			,		lowerCamelCase__				:   List[str]			,		lowerCamelCase__				:   Any			)   ->   int:
					A_ :     str =							pt_weights[f'{prefix}.weight'].data
					A_ :     Union[str, Any] =							pt_weights[f'{prefix}.bias'].data




def 						snake_case__    ( lowerCamelCase__				:   int			,		lowerCamelCase__				:   Any			,		lowerCamelCase__				:   Optional[Any]			)   ->   Union[str, Any]:
					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 						snake_case__    ( lowerCamelCase__				:   int			,		lowerCamelCase__				:   List[Any]			,		lowerCamelCase__				:   List[str]			)   ->   Optional[Any]:






					for layer_id, hf_layer in enumerate(UpperCAmelCase_			):
										copy_layer(UpperCAmelCase_			,		UpperCAmelCase_			,		f'{prefix}.{layer_id}'			)




def 						snake_case__    ( lowerCamelCase__				:   Optional[int]			,		lowerCamelCase__				:   Union[str, Any]			)   ->   int:
					A_ :     Any =							pt_weights['''text_projection'''].data.T

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




def 						snake_case__    ( lowerCamelCase__				:   Dict			,		lowerCamelCase__				:   Optional[int]			)   ->   Union[str, Any]:
					A_ :     List[Any] =							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
					A_ :     Union[str, Any] =							pt_weights['''visual.conv1.weight'''].data
					A_ :     Dict =							pt_weights['''visual.class_embedding'''].data
					A_ :     Optional[int] =							pt_weights['''visual.positional_embedding'''].data

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




@torch.no_grad()
def 						snake_case__    ( lowerCamelCase__				:   Optional[Any]			,		lowerCamelCase__				:   int			,		lowerCamelCase__				:   Tuple=None			)   ->   Union[str, Any]:
					assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
					A_ :     Dict =							ChineseCLIPConfig.from_pretrained(UpperCAmelCase_			)

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

					A_ :     Dict =							torch.load(UpperCAmelCase_			,		map_location='''cpu'''			)['''state_dict''']
					A_ :     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_			)
					A_ :     Union[str, Any] =							pt_weights['''logit_scale'''].data

					hf_model.save_pretrained(UpperCAmelCase_			)


if __name__ == "__main__":
							snake_case__          =						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."""
							)
							snake_case__          =						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 ...configuration_utils import PretrainedConfig
from ...utils import logging


snake_case__          =						logging.get_logger(__name__)

snake_case__          =						{
    """bert-base-uncased""": """https://huggingface.co/bert-base-uncased/resolve/main/config.json""",
    # QDQBERT models can be loaded from any BERT checkpoint, available at https://huggingface.co/models?filter=bert
}






class     UpperCamelCase_     (a__     ):







						"""simple docstring"""


						_lowerCAmelCase    						=					'qdqbert'







						def __init__(     self      :       Union[str, Any]							, _lowerCamelCase      :       List[Any]=30522							, _lowerCamelCase      :       str=768							, _lowerCamelCase      :       Optional[Any]=12							, _lowerCamelCase      :       str=12							, _lowerCamelCase      :       List[Any]=3072							, _lowerCamelCase      :       Union[str, Any]="gelu"							, _lowerCamelCase      :       Dict=0.1							, _lowerCamelCase      :       Union[str, Any]=0.1							, _lowerCamelCase      :       Optional[int]=512							, _lowerCamelCase      :       Dict=2							, _lowerCamelCase      :       Optional[Any]=0.02							, _lowerCamelCase      :       List[Any]=1E-12							, _lowerCamelCase      :       Tuple=True							, _lowerCamelCase      :       Tuple=1							, _lowerCamelCase      :       Optional[Any]=0							, _lowerCamelCase      :       Optional[int]=2							, **_lowerCamelCase      :       List[str]							, ):





											"""simple docstring"""







											super().__init__(pad_token_id=_lowerCamelCase							, bos_token_id=_lowerCamelCase							, eos_token_id=_lowerCamelCase							, **_lowerCamelCase    )

											A_ :     Optional[Any] =							vocab_size
											A_ :     str =							max_position_embeddings
											A_ :     List[str] =							hidden_size
											A_ :     str =							num_hidden_layers
											A_ :     List[str] =							num_attention_heads
											A_ :     List[str] =							intermediate_size
											A_ :     int =							hidden_act
											A_ :     Tuple =							hidden_dropout_prob
											A_ :     Optional[Any] =							attention_probs_dropout_prob
											A_ :     Dict =							initializer_range
											A_ :     Optional[int] =							type_vocab_size
											A_ :     Any =							layer_norm_eps
											A_ :     Optional[int] =							use_cache



