



"""simple docstring"""




import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel


def 	lowercase__  (      _UpperCAmelCase						,   _UpperCAmelCase						,   _UpperCAmelCase					)  ->      Any:






      '''simple docstring'''


      lowercase	:   Tuple											= pt_weights[f'''{prefix}.in_proj_weight'''].chunk(3						,   dim=0					)
      lowercase	:   str											= pt_weights[f'''{prefix}.in_proj_bias'''].chunk(3						,   dim=0					)

      lowercase	:   List[str]											= pt_weights[f'''{prefix}.out_proj.weight''']
      lowercase	:   Tuple											= pt_weights[f'''{prefix}.out_proj.bias''']

      lowercase	:   str											= q_proj
      lowercase	:   Any											= q_proj_bias

      lowercase	:   str											= k_proj
      lowercase	:   Dict											= k_proj_bias

      lowercase	:   Optional[Any]											= v_proj
      lowercase	:   List[Any]											= v_proj_bias

      lowercase	:   Union[str, Any]											= out_proj_weights
      lowercase	:   Dict											= out_proj_bias


def 	lowercase__  (      _UpperCAmelCase						,   _UpperCAmelCase						,   _UpperCAmelCase					)  ->      Any:






      '''simple docstring'''


      copy_linear(hf_mlp.fca						,   lowerCAmelCase_						,   f'''{prefix}.c_fc'''					)
      copy_linear(hf_mlp.fca						,   lowerCAmelCase_						,   f'''{prefix}.c_proj'''					)


def 	lowercase__  (      _UpperCAmelCase						,   _UpperCAmelCase						,   _UpperCAmelCase					)  ->      Union[str, Any]:






      '''simple docstring'''


      lowercase	:   List[str]											= pt_weights[f'''{prefix}.weight'''].data
      lowercase	:   Any											= pt_weights[f'''{prefix}.bias'''].data


def 	lowercase__  (      _UpperCAmelCase						,   _UpperCAmelCase						,   _UpperCAmelCase					)  ->      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 	lowercase__  (      _UpperCAmelCase						,   _UpperCAmelCase						,   _UpperCAmelCase					)  ->      Optional[int]:






      '''simple docstring'''


      for layer_id, hf_layer in enumerate(lowerCAmelCase_					):
            copy_layer(lowerCAmelCase_						,   lowerCAmelCase_						,   f'''{prefix}.{layer_id}'''					)


def 	lowercase__  (      _UpperCAmelCase						,   _UpperCAmelCase					)  ->      int:






      '''simple docstring'''


      lowercase	:   Optional[int]											= pt_weights['''text_projection'''].data.T

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


def 	lowercase__  (      _UpperCAmelCase						,   _UpperCAmelCase					)  ->      int:






      '''simple docstring'''


      lowercase	:   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
      lowercase	:   Any											= pt_weights['''visual.conv1.weight'''].data
      lowercase	:   List[Any]											= pt_weights['''visual.class_embedding'''].data
      lowercase	:   str											= pt_weights['''visual.positional_embedding'''].data

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


@torch.no_grad()
def 	lowercase__  (      _UpperCAmelCase						,   _UpperCAmelCase						,   _UpperCAmelCase=None					)  ->      Optional[Any]:






      '''simple docstring'''


      assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
      lowercase	:   List[str]											= ChineseCLIPConfig.from_pretrained(lowerCAmelCase_					)

      lowercase	:   int											= ChineseCLIPModel(lowerCAmelCase_					).eval()

      lowercase	:   int											= torch.load(lowerCAmelCase_						,   map_location='cpu'					)['''state_dict''']
      lowercase	:   List[str]											= {(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_					)
      lowercase	:   str											= pt_weights['''logit_scale'''].data

      hf_model.save_pretrained(lowerCAmelCase_					)


if __name__ == "__main__":
     _UpperCamelCase:					List[str]     				=	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.'
     )
     _UpperCamelCase:					List[Any]     				=	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  _a     (				lowerCAmelCase_ = 4_000_000						):





      """simple docstring"""



      _snake_case		:						Optional[int]         =  1
      _snake_case		:						Tuple         =  2
      _snake_case		:						Optional[Any]         =  0
      while j <= n:
            if j % 2 == 0:
                  total += j
            _snake_case  ,				_snake_case		:						str         =  j, i + j

      return total


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


