

import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel




def      UpperCAmelCase__							(     _A :					Any      , _A :					List[Any]      , _A :					Optional[Any]				):





  '''simple docstring'''







  a__    =pt_weights[F"""{prefix}.in_proj_weight"""].chunk(3      , dim=0				)
  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      UpperCAmelCase__							(     _A :					Optional[Any]      , _A :					Any      , _A :					Any				):





  '''simple docstring'''







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




def      UpperCAmelCase__							(     _A :					int      , _A :					Union[str, Any]      , _A :					List[str]				):





  '''simple docstring'''







  a__    =pt_weights[F"""{prefix}.weight"""].data
  a__    =pt_weights[F"""{prefix}.bias"""].data




def      UpperCAmelCase__							(     _A :					str      , _A :					Optional[Any]      , _A :					List[Any]				):





  '''simple docstring'''







  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      UpperCAmelCase__							(     _A :					Union[str, Any]      , _A :					List[Any]      , _A :					List[Any]				):





  '''simple docstring'''







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




def      UpperCAmelCase__							(     _A :					str      , _A :					Dict				):





  '''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      UpperCAmelCase__							(     _A :					Any      , _A :					int				):





  '''simple docstring'''







  a__    =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
  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      , _lowerCAmelCase      , '''visual.transformer.resblocks'''				)




@torch.no_grad()
def      UpperCAmelCase__							(     _A :					Tuple      , _A :					Optional[int]      , _A :					Tuple=None				):





  '''simple docstring'''







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

  a__    =ChineseCLIPModel(_lowerCAmelCase				).eval()

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

  hf_model.save_pretrained(_lowerCAmelCase				)


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'''

from __future__ import annotations


def        UpperCAmelCase__   (				_lowerCAmelCase    :			list[int | float])    ->		int | float:



						"""simple docstring"""

						if len(_lowerCAmelCase) == 0:
												raise ValueError("find_max() arg is an empty sequence")
						snake_case :       Any				=    nums[0]
						for x in nums:
												if x > max_num:
																		snake_case :       int				=    x
						return max_num


if __name__ == "__main__":
						import doctest

						doctest.testmod(verbose=True)





