





import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel





def    _lowercase    (  _UpperCAmelCase	,					_UpperCAmelCase	,					_UpperCAmelCase )  ->     int:
				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    _lowercase    (  _UpperCAmelCase	,					_UpperCAmelCase	,					_UpperCAmelCase )  ->     Tuple:
				copy_linear(hf_mlp.fca	,					_UpperCAmelCase	,					F"""{prefix}.c_fc""" )
				copy_linear(hf_mlp.fca	,					_UpperCAmelCase	,					F"""{prefix}.c_proj""" )





def    _lowercase    (  _UpperCAmelCase	,					_UpperCAmelCase	,					_UpperCAmelCase )  ->     Union[str, Any]:
				lowerCamelCase		       =pt_weights[F"""{prefix}.weight"""].data
				lowerCamelCase		       =pt_weights[F"""{prefix}.bias"""].data





def    _lowercase    (  _UpperCAmelCase	,					_UpperCAmelCase	,					_UpperCAmelCase )  ->     List[Any]:
				# copy layer norms
				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    _lowercase    (  _UpperCAmelCase	,					_UpperCAmelCase	,					_UpperCAmelCase )  ->     List[Any]:
				for layer_id, hf_layer in enumerate(_UpperCAmelCase ):
								copy_layer(_UpperCAmelCase	,					_UpperCAmelCase	,					F"""{prefix}.{layer_id}""" )





def    _lowercase    (  _UpperCAmelCase	,					_UpperCAmelCase )  ->     Dict:
				# 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    _lowercase    (  _UpperCAmelCase	,					_UpperCAmelCase )  ->     Optional[int]:
				# copy projection
				lowerCamelCase		       =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
				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	,					_UpperCAmelCase	,					"""visual.transformer.resblocks""" )





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

				lowerCamelCase		       =ChineseCLIPModel(_UpperCAmelCase ).eval()

				lowerCamelCase		       =torch.load(_UpperCAmelCase	,					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(_UpperCAmelCase	,					_UpperCAmelCase )
				copy_vision_model_and_projection(_UpperCAmelCase	,					_UpperCAmelCase )
				lowerCamelCase		       =pt_weights["""logit_scale"""].data

				hf_model.save_pretrained(_UpperCAmelCase )


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

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











from __future__ import annotations

from enum import Enum, unique
from typing import TypeVar

# Create a generic variable that can be 'Enum', or any subclass.
_lowerCamelCase			 =						TypeVar('T', bound='Enum')



@unique
class 			a     (   _A			):

     '''simple docstring'''





     lowerCAmelCase				:       Optional[Any]   		=  80
     lowerCAmelCase				:       List[str]   		=  70
     lowerCAmelCase				:       Optional[Any]   		=  60
     lowerCAmelCase				:       Optional[Any]   		=  50
     lowerCAmelCase				:       List[Any]   		=  40
     lowerCAmelCase				:       Dict   		=  30
     lowerCAmelCase				:       List[Any]   		=  20
     lowerCAmelCase				:       List[Any]   		=  10



@unique
class 			a     (   _A			):

     '''simple docstring'''





     lowerCAmelCase				:       Dict   		=  24
     lowerCAmelCase				:       Union[str, Any]   		=  21
     lowerCAmelCase				:       Tuple   		=  18
     lowerCAmelCase				:       str   		=  15
     lowerCAmelCase				:       Dict   		=  12
     lowerCAmelCase				:       Dict   		=  9
     lowerCAmelCase				:       Tuple   		=  6
     lowerCAmelCase				:       Any   		=  3
     lowerCAmelCase				:       List[str]   		=  2
     lowerCAmelCase				:       Any   		=  1
     lowerCAmelCase				:       str   		=  -1
     lowerCAmelCase				:       Any   		=  -2
     lowerCAmelCase				:       List[str]   		=  -3
     lowerCAmelCase				:       Dict   		=  -6
     lowerCAmelCase				:       int   		=  -9
     lowerCAmelCase				:       Optional[Any]   		=  -12
     lowerCAmelCase				:       List[str]   		=  -15
     lowerCAmelCase				:       Any   		=  -18
     lowerCAmelCase				:       int   		=  -21
     lowerCAmelCase				:       Union[str, Any]   		=  -24
     @classmethod
     def   lowerCamelCase_   (	cls   :			type[T]				):
         return {unit.name: unit.value for unit in cls if unit.value > 0}




     @classmethod
     def   lowerCamelCase_   (	cls   :			type[T]				):
         return {unit.name: unit.value for unit in cls if unit.value < 0}




def 					SCREAMING_SNAKE_CASE	(       __UpperCamelCase			:						float      )  ->							str:
    UpperCAmelCase_							 =     SIUnit.get_positive() if value > 0 else SIUnit.get_negative()
    for name_prefix, value_prefix in prefixes.items():
        UpperCAmelCase_							 =     value / (10**value_prefix)
        if numerical_part > 1:
            return f'{numerical_part!s} {name_prefix}'
    return str(__UpperCamelCase      )




def 					SCREAMING_SNAKE_CASE	(       __UpperCamelCase			:						float      )  ->							str:
    for prefix in BinaryUnit:
        UpperCAmelCase_							 =     value / (2**prefix.value)
        if numerical_part > 1:
            return f'{numerical_part!s} {prefix.name}'
    return str(__UpperCamelCase      )


if __name__ == "__main__":
 import doctest

 doctest.testmod()





