






import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel






def     lowerCamelCase__				(   __lowerCamelCase      :					Optional[Any]  ,		__lowerCamelCase      :					List[str]  ,		__lowerCamelCase      :					str		):
  __UpperCAmelCase				,			__UpperCAmelCase				,			__UpperCAmelCase							:     List[Any]									=  pt_weights[f"""{prefix}.in_proj_weight"""].chunk(3  ,		dim=0		)
  __UpperCAmelCase				,			__UpperCAmelCase				,			__UpperCAmelCase							:     List[Any]									=  pt_weights[f"""{prefix}.in_proj_bias"""].chunk(3  ,		dim=0		)

  __UpperCAmelCase							:     Optional[int]									=  pt_weights[f"""{prefix}.out_proj.weight"""]
  __UpperCAmelCase							:     Dict									=  pt_weights[f"""{prefix}.out_proj.bias"""]

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

  __UpperCAmelCase							:     Any									=  k_proj
  __UpperCAmelCase							:     Dict									=  k_proj_bias

  __UpperCAmelCase							:     Dict									=  v_proj
  __UpperCAmelCase							:     List[Any]									=  v_proj_bias

  __UpperCAmelCase							:     Dict									=  out_proj_weights
  __UpperCAmelCase							:     Any									=  out_proj_bias






def     lowerCamelCase__				(   __lowerCamelCase      :					Tuple  ,		__lowerCamelCase      :					Tuple  ,		__lowerCamelCase      :					Tuple		):
  copy_linear(hf_mlp.fca  ,		__lowerCamelCase  ,		f"""{prefix}.c_fc"""		)
  copy_linear(hf_mlp.fca  ,		__lowerCamelCase  ,		f"""{prefix}.c_proj"""		)






def     lowerCamelCase__				(   __lowerCamelCase      :					Optional[int]  ,		__lowerCamelCase      :					List[str]  ,		__lowerCamelCase      :					int		):
  __UpperCAmelCase							:     List[str]									=  pt_weights[f"""{prefix}.weight"""].data
  __UpperCAmelCase							:     List[str]									=  pt_weights[f"""{prefix}.bias"""].data






def     lowerCamelCase__				(   __lowerCamelCase      :					str  ,		__lowerCamelCase      :					Any  ,		__lowerCamelCase      :					int		):
  # copy layer norms
  copy_linear(hf_layer.layer_norma  ,		__lowerCamelCase  ,		f"""{prefix}.ln_1"""		)
  copy_linear(hf_layer.layer_norma  ,		__lowerCamelCase  ,		f"""{prefix}.ln_2"""		)

  # copy MLP
  copy_mlp(hf_layer.mlp  ,		__lowerCamelCase  ,		f"""{prefix}.mlp"""		)

  # copy attn
  copy_attn_layer(hf_layer.self_attn  ,		__lowerCamelCase  ,		f"""{prefix}.attn"""		)






def     lowerCamelCase__				(   __lowerCamelCase      :					str  ,		__lowerCamelCase      :					List[str]  ,		__lowerCamelCase      :					Tuple		):
  for layer_id, hf_layer in enumerate(__lowerCamelCase		):
    copy_layer(__lowerCamelCase  ,		__lowerCamelCase  ,		f"""{prefix}.{layer_id}"""		)






def     lowerCamelCase__				(   __lowerCamelCase      :					str  ,		__lowerCamelCase      :					List[Any]		):
  # copy projection
  __UpperCAmelCase							:     List[str]									=  pt_weights["""text_projection"""].data.T

  # copy text encoder
  for name, param in hf_model.text_model.named_parameters():
    __UpperCAmelCase							:     int									=  pt_weights[f"""bert.{name}"""].data






def     lowerCamelCase__				(   __lowerCamelCase      :					Optional[Any]  ,		__lowerCamelCase      :					Union[str, Any]		):
  # copy projection
  __UpperCAmelCase							:     Optional[Any]									=  pt_weights["""visual.proj"""].data.T

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

  # copy embeddings
  __UpperCAmelCase							:     Dict									=  pt_weights["""visual.conv1.weight"""].data
  __UpperCAmelCase							:     Optional[int]									=  pt_weights["""visual.class_embedding"""].data
  __UpperCAmelCase							:     Optional[Any]									=  pt_weights["""visual.positional_embedding"""].data

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






@torch.no_grad()
def     lowerCamelCase__				(   __lowerCamelCase      :					Tuple  ,		__lowerCamelCase      :					Tuple  ,		__lowerCamelCase      :					Union[str, Any]=None		):
  assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
  __UpperCAmelCase							:     Any									=  ChineseCLIPConfig.from_pretrained(__lowerCamelCase		)

  __UpperCAmelCase							:     Union[str, Any]									=  ChineseCLIPModel(__lowerCamelCase		).eval()

  __UpperCAmelCase							:     Union[str, Any]									=  torch.load(__lowerCamelCase  ,		map_location="""cpu"""		)["""state_dict"""]
  __UpperCAmelCase							:     List[Any]									=  {(name[7:] if name.startswith("""module."""		) else name): value for name, value in pt_weights.items()}

  copy_text_model_and_projection(__lowerCamelCase  ,		__lowerCamelCase		)
  copy_vision_model_and_projection(__lowerCamelCase  ,		__lowerCamelCase		)
  __UpperCAmelCase							:     Tuple									=  pt_weights["""logit_scale"""].data

  hf_model.save_pretrained(__lowerCamelCase		)


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

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










def     lowerCamelCase__				(   ):

  for a in range(300		):
    for b in range(a + 1  ,		400		):
      for c in range(b + 1  ,		500		):
        if (a + b + c) == 1000 and (a**2) + (b**2) == (c**2):
          return a * b * c

  return -1






def     lowerCamelCase__				(   ):

  for a in range(300		):
    for b in range(400		):
      __UpperCAmelCase							:     Optional[Any]									=  1000 - a - b
      if a < b < c and (a**2) + (b**2) == (c**2):
        return a * b * c

  return -1






def     lowerCamelCase__				(   ):
  import timeit

  print(
      timeit.timeit("""solution()"""  ,		setup="""from __main__ import solution"""  ,		number=1000		)		)
  print(
      timeit.timeit(
          """solution_fast()"""  ,		setup="""from __main__ import solution_fast"""  ,		number=1000		)		)


if __name__ == "__main__":
       print(f"""{solution() = }""")


