
'''simple docstring'''





import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel





def        snake_case							(		UpperCAmelCase      ,  UpperCAmelCase      ,  UpperCAmelCase	)->			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        snake_case							(		UpperCAmelCase      ,  UpperCAmelCase      ,  UpperCAmelCase	)->			Any:







      """simple docstring"""
      copy_linear(hf_mlp.fca      ,  _SCREAMING_SNAKE_CASE      ,  f'{prefix}.c_fc'	)
      copy_linear(hf_mlp.fca      ,  _SCREAMING_SNAKE_CASE      ,  f'{prefix}.c_proj'	)





def        snake_case							(		UpperCAmelCase      ,  UpperCAmelCase      ,  UpperCAmelCase	)->			Optional[int]:







      """simple docstring"""
      __A       	=			pt_weights[f'{prefix}.weight'].data
      __A       	=			pt_weights[f'{prefix}.bias'].data





def        snake_case							(		UpperCAmelCase      ,  UpperCAmelCase      ,  UpperCAmelCase	)->			int:







      """simple docstring"""
      # copy layer norms
      copy_linear(hf_layer.layer_norma      ,  _SCREAMING_SNAKE_CASE      ,  f'{prefix}.ln_1'	)
      copy_linear(hf_layer.layer_norma      ,  _SCREAMING_SNAKE_CASE      ,  f'{prefix}.ln_2'	)

      # copy MLP
      copy_mlp(hf_layer.mlp      ,  _SCREAMING_SNAKE_CASE      ,  f'{prefix}.mlp'	)

      # copy attn
      copy_attn_layer(hf_layer.self_attn      ,  _SCREAMING_SNAKE_CASE      ,  f'{prefix}.attn'	)





def        snake_case							(		UpperCAmelCase      ,  UpperCAmelCase      ,  UpperCAmelCase	)->			List[str]:







      """simple docstring"""
      for layer_id, hf_layer in enumerate(_SCREAMING_SNAKE_CASE	):
            copy_layer(_SCREAMING_SNAKE_CASE      ,  _SCREAMING_SNAKE_CASE      ,  f'{prefix}.{layer_id}'	)





def        snake_case							(		UpperCAmelCase      ,  UpperCAmelCase	)->			Tuple:







      """simple docstring"""
      # copy projection
      __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        snake_case							(		UpperCAmelCase      ,  UpperCAmelCase	)->			str:







      """simple docstring"""
      # copy projection
      __A       	=			pt_weights['visual.proj'].data.T

      # copy layer norms
      copy_linear(hf_model.vision_model.pre_layrnorm      ,  _SCREAMING_SNAKE_CASE      ,  'visual.ln_pre'	)
      copy_linear(hf_model.vision_model.post_layernorm      ,  _SCREAMING_SNAKE_CASE      ,  '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      ,  _SCREAMING_SNAKE_CASE      ,  'visual.transformer.resblocks'	)





@torch.no_grad()
def        snake_case							(		UpperCAmelCase      ,  UpperCAmelCase      ,  UpperCAmelCase=None	)->			Union[str, Any]:







      """simple docstring"""
      assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
      __A       	=			ChineseCLIPConfig.from_pretrained(_SCREAMING_SNAKE_CASE	)

      __A       	=			ChineseCLIPModel(_SCREAMING_SNAKE_CASE	).eval()

      __A       	=			torch.load(_SCREAMING_SNAKE_CASE      ,  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(_SCREAMING_SNAKE_CASE      ,  _SCREAMING_SNAKE_CASE	)
      copy_vision_model_and_projection(_SCREAMING_SNAKE_CASE      ,  _SCREAMING_SNAKE_CASE	)
      __A       	=			pt_weights['logit_scale'].data

      hf_model.save_pretrained(_SCREAMING_SNAKE_CASE	)


if __name__ == "__main__":
 a__		:   Optional[int]            =       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__		:   Optional[int]            =       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        snake_case							(		UpperCAmelCase      ,  UpperCAmelCase      ,  UpperCAmelCase	)->			str:







      """simple docstring"""
      assert b != 0, "This cannot accept modulo that is == 0"
      if n == 0:
            return 1

      if n % 2 == 1:
            return (bin_exp_mod(UpperCAmelCase      ,  n - 1      ,  UpperCAmelCase	) * a) % b

      __A       	=			bin_exp_mod(UpperCAmelCase      ,  n / 2      ,  UpperCAmelCase	)
      return (r * r) % b


if __name__ == "__main__":
 try:
  a__		:   Tuple            =       int(input("Enter Base : ").strip())
  a__		:   List[str]            =       int(input("Enter Power : ").strip())
  a__		:   Optional[Any]            =       int(input("Enter Modulo : ").strip())
 except ValueError:
  print("Invalid literal for integer")

 print(bin_exp_mod(BASE, POWER, MODULO))






