






"""simple docstring"""



import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel




def     lowerCamelCase_					(	__A	:		List[str]      ,						__A	:		Any      ,						__A	:		Dict      ):
					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     lowerCamelCase_					(	__A	:		Tuple      ,						__A	:		Dict      ,						__A	:		Any      ):
					copy_linear(hf_mlp.fca      ,						__A      ,						f"{prefix}.c_fc"      )
					copy_linear(hf_mlp.fca      ,						__A      ,						f"{prefix}.c_proj"      )




def     lowerCamelCase_					(	__A	:		Optional[Any]      ,						__A	:		Dict      ,						__A	:		List[Any]      ):
					lowerCamelCase__					=pt_weights[f"{prefix}.weight"].data
					lowerCamelCase__					=pt_weights[f"{prefix}.bias"].data




def     lowerCamelCase_					(	__A	:		str      ,						__A	:		Any      ,						__A	:		str      ):
					# 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     lowerCamelCase_					(	__A	:		Optional[Any]      ,						__A	:		Union[str, Any]      ,						__A	:		int      ):
					for layer_id, hf_layer in enumerate(__A      ):
										copy_layer(__A      ,						__A      ,						f"{prefix}.{layer_id}"      )




def     lowerCamelCase_					(	__A	:		Optional[Any]      ,						__A	:		Optional[int]      ):
					# 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     lowerCamelCase_					(	__A	:		List[Any]      ,						__A	:		Union[str, Any]      ):
					# 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     lowerCamelCase_					(	__A	:		str      ,						__A	:		str      ,						__A	:		Optional[int]=None      ):
					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__":
						_snake_case							:   Tuple	  =       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							:   Dict	  =       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"""



import warnings

from ...utils import logging
from .image_processing_vit import ViTImageProcessor


_snake_case							:   Tuple	  =       logging.get_logger(__name__)




class 							__lowercase(							_UpperCamelCase	):






						def __init__(       self     ,    *_SCREAMING_SNAKE_CASE     ,    **_SCREAMING_SNAKE_CASE)      ->	None:




											"""simple docstring"""





											warnings.warn(
											    'The class ViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please'
											    ' use ViTImageProcessor instead.'     ,    _SCREAMING_SNAKE_CASE     ,    )
											super().__init__(*_SCREAMING_SNAKE_CASE     ,    **_SCREAMING_SNAKE_CASE)