




import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel

def       SCREAMING_SNAKE_CASE							(     __UpperCamelCase	,      __UpperCamelCase	,      __UpperCamelCase)    ->  str:
     a						,   a						,   a											=   pt_weights[f'''{prefix}.in_proj_weight'''].chunk(3	,      dim=0)
     a						,   a						,   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       SCREAMING_SNAKE_CASE							(     __UpperCamelCase	,      __UpperCamelCase	,      __UpperCamelCase)    ->  List[Any]:
     copy_linear(hf_mlp.fca	,      __UpperCamelCase	,      f'''{prefix}.c_fc''')
     copy_linear(hf_mlp.fca	,      __UpperCamelCase	,      f'''{prefix}.c_proj''')

def       SCREAMING_SNAKE_CASE							(     __UpperCamelCase	,      __UpperCamelCase	,      __UpperCamelCase)    ->  Any:
     a											=   pt_weights[f'''{prefix}.weight'''].data
     a											=   pt_weights[f'''{prefix}.bias'''].data

def       SCREAMING_SNAKE_CASE							(     __UpperCamelCase	,      __UpperCamelCase	,      __UpperCamelCase)    ->  List[Any]:
     # copy layer norms
     copy_linear(hf_layer.layer_norma	,      __UpperCamelCase	,      f'''{prefix}.ln_1''')
     copy_linear(hf_layer.layer_norma	,      __UpperCamelCase	,      f'''{prefix}.ln_2''')

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

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

def       SCREAMING_SNAKE_CASE							(     __UpperCamelCase	,      __UpperCamelCase	,      __UpperCamelCase)    ->  Optional[int]:
     for layer_id, hf_layer in enumerate(__UpperCamelCase):
          copy_layer(__UpperCamelCase	,      __UpperCamelCase	,      f'''{prefix}.{layer_id}''')

def       SCREAMING_SNAKE_CASE							(     __UpperCamelCase	,      __UpperCamelCase)    ->  List[str]:
     # 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       SCREAMING_SNAKE_CASE							(     __UpperCamelCase	,      __UpperCamelCase)    ->  int:
     # copy projection
     a											=   pt_weights["visual.proj"].data.T

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

@torch.no_grad()
def       SCREAMING_SNAKE_CASE							(     __UpperCamelCase	,      __UpperCamelCase	,      __UpperCamelCase=None)    ->  Optional[int]:
     assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
     a											=   ChineseCLIPConfig.from_pretrained(__UpperCamelCase)

     a											=   ChineseCLIPModel(__UpperCamelCase).eval()

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

     hf_model.save_pretrained(__UpperCamelCase)


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

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







from __future__ import annotations

def       SCREAMING_SNAKE_CASE							(     __UpperCamelCase)    ->  int | None:
     for r in range(1	,      __UpperCamelCase):
          a											=   []
          for x in range(n - 1):
               a											=   pow(__UpperCamelCase	,      __UpperCamelCase	,      __UpperCamelCase)
               if val in li:
                    break
               li.append(__UpperCamelCase)
          else:
               return r
     return None


if __name__ == "__main__":
   lowercase__   :    Tuple 					=      int(input("Enter a prime number q: "))
   lowercase__   :    List[Any] 					=      find_primitive(q)
   if a is None:
      print(F'Cannot find the primitive for the value: {a!r}')
   else:
      lowercase__   :    Any 					=      int(input("Enter private key of A: "))
      lowercase__   :    Optional[int] 					=      pow(a, a_private, q)
      lowercase__   :    Optional[Any] 					=      int(input("Enter private key of B: "))
      lowercase__   :    Any 					=      pow(a, b_private, q)

      lowercase__   :    Optional[Any] 					=      pow(b_public, a_private, q)
      lowercase__   :    List[str] 					=      pow(a_public, b_private, q)

      print("The key value generated by A is: ", a_secret)
      print("The key value generated by B is: ", b_secret)

