





'''simple docstring'''







import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel

def 		SCREAMING_SNAKE_CASE      (					lowerCAmelCase__     :	str       ,  lowerCAmelCase__     :	str       ,  lowerCAmelCase__     :	Optional[int])			->						Optional[Any]:

     '''simple docstring'''




     _lowercase							:      Optional[int]  						=							pt_weights[F'''{prefix}.in_proj_weight'''].chunk(3       ,  dim=0)
     _lowercase							:      List[str]  						=							pt_weights[F'''{prefix}.in_proj_bias'''].chunk(3       ,  dim=0)

     _lowercase							:      Union[str, Any]  						=							pt_weights[F'''{prefix}.out_proj.weight''']
     _lowercase							:      str  						=							pt_weights[F'''{prefix}.out_proj.bias''']

     _lowercase							:      Dict  						=							q_proj
     _lowercase							:      Optional[int]  						=							q_proj_bias

     _lowercase							:      List[Any]  						=							k_proj
     _lowercase							:      Union[str, Any]  						=							k_proj_bias

     _lowercase							:      Union[str, Any]  						=							v_proj
     _lowercase							:      Optional[Any]  						=							v_proj_bias

     _lowercase							:      Optional[int]  						=							out_proj_weights
     _lowercase							:      Tuple  						=							out_proj_bias

def 		SCREAMING_SNAKE_CASE      (					lowerCAmelCase__     :	Tuple       ,  lowerCAmelCase__     :	Tuple       ,  lowerCAmelCase__     :	Any)			->						Dict:

     '''simple docstring'''




     copy_linear(hf_mlp.fca       ,  _SCREAMING_SNAKE_CASE       ,  F'''{prefix}.c_fc''')
     copy_linear(hf_mlp.fca       ,  _SCREAMING_SNAKE_CASE       ,  F'''{prefix}.c_proj''')

def 		SCREAMING_SNAKE_CASE      (					lowerCAmelCase__     :	Tuple       ,  lowerCAmelCase__     :	List[str]       ,  lowerCAmelCase__     :	Any)			->						Optional[Any]:

     '''simple docstring'''




     _lowercase							:      Tuple  						=							pt_weights[F'''{prefix}.weight'''].data
     _lowercase							:      int  						=							pt_weights[F'''{prefix}.bias'''].data

def 		SCREAMING_SNAKE_CASE      (					lowerCAmelCase__     :	Union[str, Any]       ,  lowerCAmelCase__     :	List[str]       ,  lowerCAmelCase__     :	Union[str, Any])			->						Optional[Any]:

     '''simple docstring'''




     copy_linear(hf_layer.layer_norma       ,  _SCREAMING_SNAKE_CASE       ,  F'''{prefix}.ln_1''')
     copy_linear(hf_layer.layer_norma       ,  _SCREAMING_SNAKE_CASE       ,  F'''{prefix}.ln_2''')

     # copy MLP
     copy_mlp(hf_layer.mlp       ,  _SCREAMING_SNAKE_CASE       ,  F'''{prefix}.mlp''')

     # copy attn
     copy_attn_layer(hf_layer.self_attn       ,  _SCREAMING_SNAKE_CASE       ,  F'''{prefix}.attn''')

def 		SCREAMING_SNAKE_CASE      (					lowerCAmelCase__     :	Dict       ,  lowerCAmelCase__     :	Dict       ,  lowerCAmelCase__     :	Optional[int])			->						Any:

     '''simple docstring'''




     for layer_id, hf_layer in enumerate(_SCREAMING_SNAKE_CASE):
          copy_layer(_SCREAMING_SNAKE_CASE       ,  _SCREAMING_SNAKE_CASE       ,  F'''{prefix}.{layer_id}''')

def 		SCREAMING_SNAKE_CASE      (					lowerCAmelCase__     :	Union[str, Any]       ,  lowerCAmelCase__     :	List[str])			->						List[str]:

     '''simple docstring'''




     _lowercase							:      Optional[Any]  						=							pt_weights['text_projection'].data.T

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

def 		SCREAMING_SNAKE_CASE      (					lowerCAmelCase__     :	Any       ,  lowerCAmelCase__     :	Optional[Any])			->						Any:

     '''simple docstring'''




     _lowercase							:      Any  						=							pt_weights['visual.proj'].data.T

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

     # copy embeddings
     _lowercase							:      int  						=							pt_weights['visual.conv1.weight'].data
     _lowercase							:      Dict  						=							pt_weights['visual.class_embedding'].data
     _lowercase							:      List[Any]  						=							pt_weights['visual.positional_embedding'].data

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

@torch.no_grad()
def 		SCREAMING_SNAKE_CASE      (					lowerCAmelCase__     :	Union[str, Any]       ,  lowerCAmelCase__     :	Tuple       ,  lowerCAmelCase__     :	List[Any]=None)			->						List[Any]:

     '''simple docstring'''




     assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
     _lowercase							:      int  						=							ChineseCLIPConfig.from_pretrained(_SCREAMING_SNAKE_CASE)

     _lowercase							:      int  						=							ChineseCLIPModel(_SCREAMING_SNAKE_CASE).eval()

     _lowercase							:      Any  						=							torch.load(_SCREAMING_SNAKE_CASE       ,  map_location='cpu')['state_dict']
     _lowercase							:      Union[str, Any]  						=							{(name[7:] if name.startswith('module.') else name): value for name, value in pt_weights.items()}

     copy_text_model_and_projection(_SCREAMING_SNAKE_CASE       ,  _SCREAMING_SNAKE_CASE)
     copy_vision_model_and_projection(_SCREAMING_SNAKE_CASE       ,  _SCREAMING_SNAKE_CASE)
     _lowercase							:      Any  						=							pt_weights['logit_scale'].data

     hf_model.save_pretrained(_SCREAMING_SNAKE_CASE)


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'''







import warnings

from ...utils import logging
from .image_processing_mobilenet_va import MobileNetVaImageProcessor


A						    =							logging.get_logger(__name__)



class 							__SCREAMING_SNAKE_CASE			(       lowerCAmelCase__      ):
      '''simple docstring'''



      def __init__(							self    : Tuple   ,*UpperCamelCase    : str   ,**UpperCamelCase    : List[Any]  )		->  None:
           warnings.warn(
               'The class MobileNetV2FeatureExtractor is deprecated and will be removed in version 5 of Transformers.'
               ' Please use MobileNetV2ImageProcessor instead.'   ,UpperCamelCase   ,)
           super().__init__(*UpperCamelCase   ,**UpperCamelCase  )