


import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel

def       _a	(					SCREAMING_SNAKE_CASE						:					List[str]     ,			SCREAMING_SNAKE_CASE						:					str     ,			SCREAMING_SNAKE_CASE						:					Optional[int]  ):







 """simple docstring"""



 UpperCamelCase__							,					UpperCamelCase__							,					UpperCamelCase__       :     Any 							= pt_weights[F"{prefix}.in_proj_weight"].chunk(3     ,			dim=0  )
 UpperCamelCase__							,					UpperCamelCase__							,					UpperCamelCase__       :     int 							= pt_weights[F"{prefix}.in_proj_bias"].chunk(3     ,			dim=0  )

 UpperCamelCase__       :     str 							= pt_weights[F"{prefix}.out_proj.weight"]
 UpperCamelCase__       :     str 							= pt_weights[F"{prefix}.out_proj.bias"]

 UpperCamelCase__       :     Dict 							= q_proj
 UpperCamelCase__       :     int 							= q_proj_bias

 UpperCamelCase__       :     Optional[Any] 							= k_proj
 UpperCamelCase__       :     Tuple 							= k_proj_bias

 UpperCamelCase__       :     Any 							= v_proj
 UpperCamelCase__       :     List[str] 							= v_proj_bias

 UpperCamelCase__       :     str 							= out_proj_weights
 UpperCamelCase__       :     Optional[int] 							= out_proj_bias

def       _a	(					SCREAMING_SNAKE_CASE						:					Any     ,			SCREAMING_SNAKE_CASE						:					Optional[int]     ,			SCREAMING_SNAKE_CASE						:					str  ):







 """simple docstring"""



 copy_linear(hf_mlp.fca     ,			lowercase_     ,			F"{prefix}.c_fc"  )
 copy_linear(hf_mlp.fca     ,			lowercase_     ,			F"{prefix}.c_proj"  )

def       _a	(					SCREAMING_SNAKE_CASE						:					str     ,			SCREAMING_SNAKE_CASE						:					List[Any]     ,			SCREAMING_SNAKE_CASE						:					Any  ):







 """simple docstring"""



 UpperCamelCase__       :     List[Any] 							= pt_weights[F"{prefix}.weight"].data
 UpperCamelCase__       :     Optional[Any] 							= pt_weights[F"{prefix}.bias"].data

def       _a	(					SCREAMING_SNAKE_CASE						:					Tuple     ,			SCREAMING_SNAKE_CASE						:					Dict     ,			SCREAMING_SNAKE_CASE						:					Optional[Any]  ):







 """simple docstring"""



 copy_linear(hf_layer.layer_norma     ,			lowercase_     ,			F"{prefix}.ln_1"  )
 copy_linear(hf_layer.layer_norma     ,			lowercase_     ,			F"{prefix}.ln_2"  )

 # copy MLP
 copy_mlp(hf_layer.mlp     ,			lowercase_     ,			F"{prefix}.mlp"  )

 # copy attn
 copy_attn_layer(hf_layer.self_attn     ,			lowercase_     ,			F"{prefix}.attn"  )

def       _a	(					SCREAMING_SNAKE_CASE						:					Union[str, Any]     ,			SCREAMING_SNAKE_CASE						:					str     ,			SCREAMING_SNAKE_CASE						:					str  ):







 """simple docstring"""



 for layer_id, hf_layer in enumerate(lowercase_  ):
  copy_layer(lowercase_     ,			lowercase_     ,			F"{prefix}.{layer_id}"  )

def       _a	(					SCREAMING_SNAKE_CASE						:					List[str]     ,			SCREAMING_SNAKE_CASE						:					Any  ):







 """simple docstring"""



 UpperCamelCase__       :     List[Any] 							= pt_weights['''text_projection'''].data.T

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

def       _a	(					SCREAMING_SNAKE_CASE						:					int     ,			SCREAMING_SNAKE_CASE						:					Union[str, Any]  ):







 """simple docstring"""



 UpperCamelCase__       :     str 							= pt_weights['''visual.proj'''].data.T

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

 # copy embeddings
 UpperCamelCase__       :     Union[str, Any] 							= pt_weights['''visual.conv1.weight'''].data
 UpperCamelCase__       :     List[str] 							= pt_weights['''visual.class_embedding'''].data
 UpperCamelCase__       :     Any 							= pt_weights['''visual.positional_embedding'''].data

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

@torch.no_grad()
def       _a	(					SCREAMING_SNAKE_CASE						:					Union[str, Any]     ,			SCREAMING_SNAKE_CASE						:					List[Any]     ,			SCREAMING_SNAKE_CASE						:					str=None  ):







 """simple docstring"""



 assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
 UpperCamelCase__       :     Optional[int] 							= ChineseCLIPConfig.from_pretrained(lowercase_  )

 UpperCamelCase__       :     Union[str, Any] 							= ChineseCLIPModel(lowercase_  ).eval()

 UpperCamelCase__       :     Tuple 							= torch.load(lowercase_     ,			map_location='''cpu'''  )['''state_dict''']
 UpperCamelCase__       :     List[str] 							= {(name[7:] if name.startswith('''module.'''  ) else name): value for name, value in pt_weights.items()}

 copy_text_model_and_projection(lowercase_     ,			lowercase_  )
 copy_vision_model_and_projection(lowercase_     ,			lowercase_  )
 UpperCamelCase__       :     Tuple 							= pt_weights['''logit_scale'''].data

 hf_model.save_pretrained(lowercase_  )


if __name__ == "__main__":
       __UpperCamelCase							:							Union[str, Any]    							=					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!")








class 			_lowerCamelCase    :

      """simple docstring"""

      def __init__(  self       :			Optional[Any]			,							snake_case       :			int					):
          __UpperCamelCase			=    data
          __UpperCamelCase			=    42
          __UpperCamelCase			=    42







def        __SCREAMING_SNAKE_CASE (	lowercase_   )							->						None:

    """simple docstring"""


    __UpperCamelCase			=    0
    __UpperCamelCase			=    x




def        __SCREAMING_SNAKE_CASE (	lowercase_       ,  lowercase_   )							->						None:

    """simple docstring"""


    __UpperCamelCase		,   __UpperCamelCase			=    find_set(lowercase_   ), find_set(lowercase_   )
    if x == y:
        return

    elif x.rank > y.rank:
        __UpperCamelCase			=    x
    else:
        __UpperCamelCase			=    y
        if x.rank == y.rank:
            y.rank += 1




def        __SCREAMING_SNAKE_CASE (	lowercase_   )							->						Node:

    """simple docstring"""


    if x != x.parent:
        __UpperCamelCase			=    find_set(x.parent   )
    return x.parent




def        __SCREAMING_SNAKE_CASE (	lowercase_   )							->						set:

    """simple docstring"""


    __UpperCamelCase			=    ({0, 1, 2}, {3, 4, 5})
    for s in sets:
        if node.data in s:
            return s
    __UpperCamelCase			=    F"{node.data} is not in {sets}"
    raise ValueError(lowercase_   )




def        __SCREAMING_SNAKE_CASE (	)							->						None:

    """simple docstring"""


    __UpperCamelCase			=    [Node(lowercase_   ) for i in range(6   )]
    for v in vertex:
        make_set(lowercase_   )

    union_set(vertex[0]       ,  vertex[1]   )
    union_set(vertex[1]       ,  vertex[2]   )
    union_set(vertex[3]       ,  vertex[4]   )
    union_set(vertex[3]       ,  vertex[5]   )

    for nodea in vertex:
        for nodea in vertex:
            if find_python_set(lowercase_   ).isdisjoint(find_python_set(lowercase_   )   ):
                assert find_set(lowercase_   ) != find_set(lowercase_   )
            else:
                assert find_set(lowercase_   ) == find_set(lowercase_   )


if __name__ == "__main__":
 test_disjoint_set()






