




import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel





def 							lowerCamelCase    (							UpperCAmelCase_	:      List[Any]   ,       UpperCAmelCase_	:      int   ,       UpperCAmelCase_	:      Optional[int]      )->			List[str]:

       """simple docstring"""




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

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

       a			   =q_proj
       a			   =q_proj_bias

       a			   =k_proj
       a			   =k_proj_bias

       a			   =v_proj
       a			   =v_proj_bias

       a			   =out_proj_weights
       a			   =out_proj_bias





def 							lowerCamelCase    (							UpperCAmelCase_	:      Any   ,       UpperCAmelCase_	:      Dict   ,       UpperCAmelCase_	:      Optional[int]      )->			Union[str, Any]:

       """simple docstring"""




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





def 							lowerCamelCase    (							UpperCAmelCase_	:      List[Any]   ,       UpperCAmelCase_	:      Dict   ,       UpperCAmelCase_	:      List[Any]      )->			Optional[int]:

       """simple docstring"""




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





def 							lowerCamelCase    (							UpperCAmelCase_	:      Union[str, Any]   ,       UpperCAmelCase_	:      str   ,       UpperCAmelCase_	:      Optional[Any]      )->			Any:

       """simple docstring"""




       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 							lowerCamelCase    (							UpperCAmelCase_	:      Union[str, Any]   ,       UpperCAmelCase_	:      Tuple   ,       UpperCAmelCase_	:      Optional[int]      )->			Tuple:

       """simple docstring"""




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





def 							lowerCamelCase    (							UpperCAmelCase_	:      Union[str, Any]   ,       UpperCAmelCase_	:      Union[str, Any]      )->			List[Any]:

       """simple docstring"""




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

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





def 							lowerCamelCase    (							UpperCAmelCase_	:      Optional[Any]   ,       UpperCAmelCase_	:      int      )->			Optional[Any]:

       """simple docstring"""




       a			   =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
       a			   =pt_weights["""visual.conv1.weight"""].data
       a			   =pt_weights["""visual.class_embedding"""].data
       a			   =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 							lowerCamelCase    (							UpperCAmelCase_	:      str   ,       UpperCAmelCase_	:      int   ,       UpperCAmelCase_	:      List[Any]=None      )->			Dict:

       """simple docstring"""




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

       a			   =ChineseCLIPModel(_snake_case      ).eval()

       a			   =torch.load(_snake_case   ,       map_location="""cpu"""      )["""state_dict"""]
       a			   ={(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      )
       a			   =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!''')

"""simple docstring"""


import warnings

from ...utils import logging
from .image_processing_convnext import ConvNextImageProcessor


__UpperCamelCase       :    str         =		logging.get_logger(__name__)
class   UpperCAmelCase_	(				lowercase__   ):
    def __init__(	self			:       List[Any]   ,						*_lowercase			:       Optional[Any]   ,						**_lowercase			:       str		)						->    None:
       warnings.warn(
           "The class ConvNextFeatureExtractor is deprecated and will be removed in version 5 of Transformers."
           " Please use ConvNextImageProcessor instead."   ,						_lowercase   ,						)
       super().__init__(*_lowercase   ,						**_lowercase		)