


'''simple docstring'''

from collections import OrderedDict
from typing import Mapping

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


__lowercase:		List[str]    						=  logging.get_logger(__name__)

__lowercase:		List[Any]    						=  {
    "uclanlp/plbart-base": "https://huggingface.co/uclanlp/plbart-base/resolve/main/config.json",
    # See all PLBART models at https://huggingface.co/models?filter=plbart
}







class 			UpperCAmelCase			(							SCREAMING_SNAKE_CASE__):
 _lowerCamelCase			:       Optional[int]            =       'plbart'
 _lowerCamelCase			:       Union[str, Any]            =       ['past_key_values']
 _lowerCamelCase			:       Tuple            =       {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}

 def __init__(			self			:						Optional[Any],       a_			:						Union[str, Any]=5_0005,       a_			:						int=1024,       a_			:						str=6,       a_			:						Union[str, Any]=3072,       a_			:						Optional[int]=12,       a_			:						Optional[Any]=6,       a_			:						Dict=3072,       a_			:						Dict=12,       a_			:						int=0.0,       a_			:						List[Any]=0.0,       a_			:						Optional[Any]=True,       a_			:						List[str]=True,       a_			:						Tuple="gelu",       a_			:						Any=768,       a_			:						Dict=0.1,       a_			:						List[Any]=0.1,       a_			:						Union[str, Any]=0.0,       a_			:						Optional[Any]=0.02,       a_			:						Union[str, Any]=0.0,       a_			:						int=True,       a_			:						Dict=1,       a_			:						Optional[int]=0,       a_			:						Optional[int]=2,       a_			:						Any=2,       **a_			:						Optional[Any],       ):





    """simple docstring"""



    UpperCamelCase__			    =    vocab_size
    UpperCamelCase__			    =    max_position_embeddings
    UpperCamelCase__			    =    d_model
    UpperCamelCase__			    =    encoder_ffn_dim
    UpperCamelCase__			    =    encoder_layers
    UpperCamelCase__			    =    encoder_attention_heads
    UpperCamelCase__			    =    decoder_ffn_dim
    UpperCamelCase__			    =    decoder_layers
    UpperCamelCase__			    =    decoder_attention_heads
    UpperCamelCase__			    =    dropout
    UpperCamelCase__			    =    attention_dropout
    UpperCamelCase__			    =    activation_dropout
    UpperCamelCase__			    =    activation_function
    UpperCamelCase__			    =    init_std
    UpperCamelCase__			    =    encoder_layerdrop
    UpperCamelCase__			    =    decoder_layerdrop
    UpperCamelCase__			    =    classifier_dropout
    UpperCamelCase__			    =    use_cache
    UpperCamelCase__			    =    encoder_layers
    UpperCamelCase__			    =    scale_embedding  # scale factor will be sqrt(d_model) if True
    super().__init__(
        pad_token_id=a_,       bos_token_id=a_,       eos_token_id=a_,       is_encoder_decoder=a_,       forced_eos_token_id=a_,       **a_,       )







class 			UpperCAmelCase			(							SCREAMING_SNAKE_CASE__):

 @property
 def 		lowercase_    (			self			:						int       ):





    """simple docstring"""



    return OrderedDict(
        [
            ("input_ids", {0: "batch", 1: "sequence"}),
            ("attention_mask", {0: "batch", 1: "sequence"}),
        ]       )

 @property
 def 		lowercase_    (			self			:						Tuple       ):





    """simple docstring"""



    if self.use_past:
       return OrderedDict(
           [
               ("last_hidden_state", {0: "batch", 1: "sequence"}),
               ("past_keys", {0: "batch", 2: "sequence"}),
               ("encoder_last_hidden_state", {0: "batch", 1: "sequence"}),
           ]       )
    else:
       return OrderedDict(
           [
               ("last_hidden_state", {0: "batch", 1: "sequence"}),
               ("encoder_last_hidden_state", {0: "batch", 1: "sequence"}),
           ]       )



'''simple docstring'''

import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel



def 							SCREAMING_SNAKE_CASE__(		_UpperCamelCase   :					int     ,		_UpperCamelCase   :					List[str]     ,		_UpperCamelCase   :					List[str] )		->       List[str]:





   '''simple docstring'''







   UpperCamelCase__    ,		UpperCamelCase__    ,		UpperCamelCase__			    =    pt_weights[F'{prefix}.in_proj_weight'].chunk(3     ,		dim=0 )
   UpperCamelCase__    ,		UpperCamelCase__    ,		UpperCamelCase__			    =    pt_weights[F'{prefix}.in_proj_bias'].chunk(3     ,		dim=0 )

   UpperCamelCase__			    =    pt_weights[F'{prefix}.out_proj.weight']
   UpperCamelCase__			    =    pt_weights[F'{prefix}.out_proj.bias']

   UpperCamelCase__			    =    q_proj
   UpperCamelCase__			    =    q_proj_bias

   UpperCamelCase__			    =    k_proj
   UpperCamelCase__			    =    k_proj_bias

   UpperCamelCase__			    =    v_proj
   UpperCamelCase__			    =    v_proj_bias

   UpperCamelCase__			    =    out_proj_weights
   UpperCamelCase__			    =    out_proj_bias



def 							SCREAMING_SNAKE_CASE__(		_UpperCamelCase   :					Union[str, Any]     ,		_UpperCamelCase   :					List[str]     ,		_UpperCamelCase   :					Dict )		->       Tuple:





   '''simple docstring'''







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



def 							SCREAMING_SNAKE_CASE__(		_UpperCamelCase   :					Tuple     ,		_UpperCamelCase   :					str     ,		_UpperCamelCase   :					Optional[int] )		->       Any:





   '''simple docstring'''







   UpperCamelCase__			    =    pt_weights[F'{prefix}.weight'].data
   UpperCamelCase__			    =    pt_weights[F'{prefix}.bias'].data



def 							SCREAMING_SNAKE_CASE__(		_UpperCamelCase   :					int     ,		_UpperCamelCase   :					List[Any]     ,		_UpperCamelCase   :					Any )		->       Union[str, Any]:





   '''simple docstring'''







   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 							SCREAMING_SNAKE_CASE__(		_UpperCamelCase   :					Tuple     ,		_UpperCamelCase   :					Optional[int]     ,		_UpperCamelCase   :					Optional[int] )		->       List[Any]:





   '''simple docstring'''







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



def 							SCREAMING_SNAKE_CASE__(		_UpperCamelCase   :					Tuple     ,		_UpperCamelCase   :					Dict )		->       Tuple:





   '''simple docstring'''







   UpperCamelCase__			    =    pt_weights["text_projection"].data.T

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



def 							SCREAMING_SNAKE_CASE__(		_UpperCamelCase   :					List[str]     ,		_UpperCamelCase   :					Dict )		->       List[Any]:





   '''simple docstring'''







   UpperCamelCase__			    =    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
   UpperCamelCase__			    =    pt_weights["visual.conv1.weight"].data
   UpperCamelCase__			    =    pt_weights["visual.class_embedding"].data
   UpperCamelCase__			    =    pt_weights["visual.positional_embedding"].data

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



@torch.no_grad()
def 							SCREAMING_SNAKE_CASE__(		_UpperCamelCase   :					int     ,		_UpperCamelCase   :					Union[str, Any]     ,		_UpperCamelCase   :					Any=None )		->       List[str]:





   '''simple docstring'''







   assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
   UpperCamelCase__			    =    ChineseCLIPConfig.from_pretrained(_UpperCamelCase )

   UpperCamelCase__			    =    ChineseCLIPModel(_UpperCamelCase ).eval()

   UpperCamelCase__			    =    torch.load(_UpperCamelCase     ,		map_location="cpu" )["state_dict"]
   UpperCamelCase__			    =    {(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 )
   UpperCamelCase__			    =    pt_weights["logit_scale"].data

   hf_model.save_pretrained(_UpperCamelCase )


if __name__ == "__main__":
       __lowercase:		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."
       )
       __lowercase:		Optional[int]    						=  parser.parse_args()

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