'''simple docstring'''





import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel






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







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

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

     a :				List[Any]				=      q_proj
     a :				str				=      q_proj_bias

     a :				Dict				=      k_proj
     a :				Union[str, Any]				=      k_proj_bias

     a :				Optional[int]				=      v_proj
     a :				Dict				=      v_proj_bias

     a :				List[str]				=      out_proj_weights
     a :				Union[str, Any]				=      out_proj_bias






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







     """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 SCREAMING_SNAKE_CASE__   (							snake_case    :							Any							,     snake_case    :							Union[str, Any]							,     snake_case    :							Dict   )					->							Tuple:







     """simple docstring"""
     a :				Dict				=      pt_weights[F"""{prefix}.weight"""].data
     a :				List[str]				=      pt_weights[F"""{prefix}.bias"""].data






def SCREAMING_SNAKE_CASE__   (							snake_case    :							Union[str, Any]							,     snake_case    :							Optional[Any]							,     snake_case    :							List[Any]   )					->							Any:







     """simple docstring"""
     # 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 SCREAMING_SNAKE_CASE__   (							snake_case    :							Optional[Any]							,     snake_case    :							str							,     snake_case    :							Any   )					->							List[str]:







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






def SCREAMING_SNAKE_CASE__   (							snake_case    :							Optional[Any]							,     snake_case    :							Optional[Any]   )					->							Tuple:







     """simple docstring"""
     # copy projection
     a :				List[str]				=      pt_weights['text_projection'].data.T

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






def SCREAMING_SNAKE_CASE__   (							snake_case    :							Optional[int]							,     snake_case    :							Any   )					->							List[Any]:







     """simple docstring"""
     # copy projection
     a :				Union[str, Any]				=      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 :				List[str]				=      pt_weights['visual.conv1.weight'].data
     a :				str				=      pt_weights['visual.class_embedding'].data
     a :				Optional[Any]				=      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 SCREAMING_SNAKE_CASE__   (							snake_case    :							Optional[Any]							,     snake_case    :							List[Any]							,     snake_case    :							Optional[int]=None   )					->							Optional[int]:







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

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

     a :				Optional[Any]				=      torch.load(snake_case							,     map_location='cpu'   )['state_dict']
     a :				Union[str, 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   )
     a :				str				=      pt_weights['logit_scale'].data

     hf_model.save_pretrained(snake_case   )


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    :			Any						     =  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"""


def SCREAMING_SNAKE_CASE_   (       )->		Tuple:
       _lowerCamelCase    	=					1

       while True:
              if (
                  sorted(str(snake_case		)		)
                  == sorted(str(2 * i		)		)
                  == sorted(str(3 * i		)		)
                  == sorted(str(4 * i		)		)
                  == sorted(str(5 * i		)		)
                  == sorted(str(6 * i		)		)
              ):
                     return i

              i += 1


if __name__ == "__main__":
      print(solution())






