


"""simple docstring"""






import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel




def        A_					(					UpperCAmelCase__       ,   UpperCAmelCase__       ,   UpperCAmelCase__				)     ->     Optional[int]:
   a	, a	, a   :	Any						=    pt_weights[F'{prefix}.in_proj_weight'].chunk(3       ,   dim=0				)
   a	, a	, a   :	int						=    pt_weights[F'{prefix}.in_proj_bias'].chunk(3       ,   dim=0				)

   a   :	Optional[int]						=    pt_weights[F'{prefix}.out_proj.weight']
   a   :	Union[str, Any]						=    pt_weights[F'{prefix}.out_proj.bias']

   a   :	int						=    q_proj
   a   :	Any						=    q_proj_bias

   a   :	Optional[int]						=    k_proj
   a   :	List[str]						=    k_proj_bias

   a   :	str						=    v_proj
   a   :	Union[str, Any]						=    v_proj_bias

   a   :	Optional[int]						=    out_proj_weights
   a   :	int						=    out_proj_bias




def        A_					(					UpperCAmelCase__       ,   UpperCAmelCase__       ,   UpperCAmelCase__				)     ->     int:
   copy_linear(hf_mlp.fca       ,   lowerCamelCase__       ,   F'{prefix}.c_fc'				)
   copy_linear(hf_mlp.fca       ,   lowerCamelCase__       ,   F'{prefix}.c_proj'				)




def        A_					(					UpperCAmelCase__       ,   UpperCAmelCase__       ,   UpperCAmelCase__				)     ->     Union[str, Any]:
   a   :	Optional[Any]						=    pt_weights[F'{prefix}.weight'].data
   a   :	Optional[int]						=    pt_weights[F'{prefix}.bias'].data




def        A_					(					UpperCAmelCase__       ,   UpperCAmelCase__       ,   UpperCAmelCase__				)     ->     str:
   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        A_					(					UpperCAmelCase__       ,   UpperCAmelCase__       ,   UpperCAmelCase__				)     ->     str:




   for layer_id, hf_layer in enumerate(lowerCamelCase__				):
      copy_layer(lowerCamelCase__       ,   lowerCamelCase__       ,   F'{prefix}.{layer_id}'				)




def        A_					(					UpperCAmelCase__       ,   UpperCAmelCase__				)     ->     Optional[int]:
   a   :	Optional[int]						=    pt_weights['text_projection'].data.T

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




def        A_					(					UpperCAmelCase__       ,   UpperCAmelCase__				)     ->     int:
   a   :	List[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
   a   :	str						=    pt_weights['visual.conv1.weight'].data
   a   :	Tuple						=    pt_weights['visual.class_embedding'].data
   a   :	int						=    pt_weights['visual.positional_embedding'].data

   # copy encoder
   copy_layers(hf_model.vision_model.encoder.layers       ,   lowerCamelCase__       ,   'visual.transformer.resblocks'				)




@torch.no_grad()
def        A_					(					UpperCAmelCase__       ,   UpperCAmelCase__       ,   UpperCAmelCase__=None				)     ->     int:
   assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
   a   :	str						=    ChineseCLIPConfig.from_pretrained(lowerCamelCase__				)

   a   :	List[Any]						=    ChineseCLIPModel(lowerCamelCase__				).eval()

   a   :	Any						=    torch.load(lowerCamelCase__       ,   map_location='cpu'				)['state_dict']
   a   :	Tuple						=    {(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   :	Tuple						=    pt_weights['logit_scale'].data

   hf_model.save_pretrained(lowerCamelCase__				)


if __name__ == "__main__":
 SCREAMING_SNAKE_CASE__				:  Dict						= 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."
 )
 SCREAMING_SNAKE_CASE__				:  Tuple						= parser.parse_args()

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






def 					__lowercase						(    lowerCamelCase__       )					->int:

  '''simple docstring'''




  if not isinstance(lowerCamelCase__			,					lowerCamelCase__       ):
    raise ValueError("double_factorial() only accepts integral values"       )
  if n < 0:
    raise ValueError("double_factorial() not defined for negative values"       )
  return 1 if n <= 1 else n * double_factorial(n - 2       )


if __name__ == "__main__":
     import doctest

     doctest.testmod()

