


import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel




def   snake_case_	(    snake_case     ,				snake_case     ,				snake_case	)    ->							int:
       lowercase__     ,		lowercase__     ,		lowercase__:  List[str]         =  pt_weights[f'{prefix}.in_proj_weight'].chunk(3     ,				dim=0	)
       lowercase__     ,		lowercase__     ,		lowercase__:  Union[str, Any]         =  pt_weights[f'{prefix}.in_proj_bias'].chunk(3     ,				dim=0	)

       lowercase__:  List[Any]         =  pt_weights[f'{prefix}.out_proj.weight']
       lowercase__:  int         =  pt_weights[f'{prefix}.out_proj.bias']

       lowercase__:  int         =  q_proj
       lowercase__:  List[Any]         =  q_proj_bias

       lowercase__:  Optional[Any]         =  k_proj
       lowercase__:  Optional[Any]         =  k_proj_bias

       lowercase__:  int         =  v_proj
       lowercase__:  str         =  v_proj_bias

       lowercase__:  Tuple         =  out_proj_weights
       lowercase__:  str         =  out_proj_bias




def   snake_case_	(    snake_case     ,				snake_case     ,				snake_case	)    ->							str:
       copy_linear(hf_mlp.fca     ,				snake_case     ,				f'{prefix}.c_fc'	)
       copy_linear(hf_mlp.fca     ,				snake_case     ,				f'{prefix}.c_proj'	)




def   snake_case_	(    snake_case     ,				snake_case     ,				snake_case	)    ->							List[Any]:
       lowercase__:  Optional[Any]         =  pt_weights[f'{prefix}.weight'].data
       lowercase__:  Dict         =  pt_weights[f'{prefix}.bias'].data




def   snake_case_	(    snake_case     ,				snake_case     ,				snake_case	)    ->							int:
       # copy layer norms
       copy_linear(hf_layer.layer_norma     ,				snake_case     ,				f'{prefix}.ln_1'	)
       copy_linear(hf_layer.layer_norma     ,				snake_case     ,				f'{prefix}.ln_2'	)

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

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




def   snake_case_	(    snake_case     ,				snake_case     ,				snake_case	)    ->							Tuple:
       for layer_id, hf_layer in enumerate(snake_case	):
              copy_layer(snake_case     ,				snake_case     ,				f'{prefix}.{layer_id}'	)




def   snake_case_	(    snake_case     ,				snake_case	)    ->							List[Any]:
       # copy projection
       lowercase__:  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   snake_case_	(    snake_case     ,				snake_case	)    ->							Optional[Any]:
       # copy projection
       lowercase__:  List[str]         =  pt_weights['visual.proj'].data.T

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

       # copy embeddings
       lowercase__:  Tuple         =  pt_weights['visual.conv1.weight'].data
       lowercase__:  Optional[int]         =  pt_weights['visual.class_embedding'].data
       lowercase__:  Optional[int]         =  pt_weights['visual.positional_embedding'].data

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




@torch.no_grad()
def   snake_case_	(    snake_case     ,				snake_case     ,				snake_case=None	)    ->							int:
       assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
       lowercase__:  str         =  ChineseCLIPConfig.from_pretrained(snake_case	)

       lowercase__:  Optional[Any]         =  ChineseCLIPModel(snake_case	).eval()

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

       copy_text_model_and_projection(snake_case     ,				snake_case	)
       copy_vision_model_and_projection(snake_case     ,				snake_case	)
       lowercase__:  Optional[int]         =  pt_weights['logit_scale'].data

       hf_model.save_pretrained(snake_case	)


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

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






from typing import TYPE_CHECKING

from ...utils import _LazyModule


__lowerCAmelCase									=				{'''tokenization_phobert''': ['''PhobertTokenizer''']}


if TYPE_CHECKING:
 from .tokenization_phobert import PhobertTokenizer

else:
 import sys

 __lowerCAmelCase									=				_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)


