

import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel





def  _snake_case		(snake_case_					:     Any    ,       snake_case_					:     Dict    ,       snake_case_					:     str  )							->		Union[str, Any]:
  __UpperCAmelCase,			__UpperCAmelCase,			__UpperCAmelCase	:  Tuple		=     pt_weights[F"{prefix}.in_proj_weight"].chunk(3    ,       dim=0  )
  __UpperCAmelCase,			__UpperCAmelCase,			__UpperCAmelCase	:  Optional[Any]		=     pt_weights[F"{prefix}.in_proj_bias"].chunk(3    ,       dim=0  )

  __UpperCAmelCase	:  Any		=     pt_weights[F"{prefix}.out_proj.weight"]
  __UpperCAmelCase	:  int		=     pt_weights[F"{prefix}.out_proj.bias"]

  __UpperCAmelCase	:  Dict		=     q_proj
  __UpperCAmelCase	:  Optional[Any]		=     q_proj_bias

  __UpperCAmelCase	:  List[Any]		=     k_proj
  __UpperCAmelCase	:  str		=     k_proj_bias

  __UpperCAmelCase	:  Dict		=     v_proj
  __UpperCAmelCase	:  Any		=     v_proj_bias

  __UpperCAmelCase	:  Tuple		=     out_proj_weights
  __UpperCAmelCase	:  Optional[int]		=     out_proj_bias





def  _snake_case		(snake_case_					:     Tuple    ,       snake_case_					:     List[str]    ,       snake_case_					:     Any  )							->		str:
  copy_linear(hf_mlp.fca    ,       snake_case_    ,       F"{prefix}.c_fc"  )
  copy_linear(hf_mlp.fca    ,       snake_case_    ,       F"{prefix}.c_proj"  )





def  _snake_case		(snake_case_					:     Any    ,       snake_case_					:     Union[str, Any]    ,       snake_case_					:     int  )							->		Tuple:
  __UpperCAmelCase	:  Optional[Any]		=     pt_weights[F"{prefix}.weight"].data
  __UpperCAmelCase	:  Tuple		=     pt_weights[F"{prefix}.bias"].data





def  _snake_case		(snake_case_					:     List[Any]    ,       snake_case_					:     Dict    ,       snake_case_					:     List[str]  )							->		Any:
  # copy layer norms
  copy_linear(hf_layer.layer_norma    ,       snake_case_    ,       F"{prefix}.ln_1"  )
  copy_linear(hf_layer.layer_norma    ,       snake_case_    ,       F"{prefix}.ln_2"  )

  # copy MLP
  copy_mlp(hf_layer.mlp    ,       snake_case_    ,       F"{prefix}.mlp"  )

  # copy attn
  copy_attn_layer(hf_layer.self_attn    ,       snake_case_    ,       F"{prefix}.attn"  )





def  _snake_case		(snake_case_					:     Tuple    ,       snake_case_					:     Dict    ,       snake_case_					:     Tuple  )							->		Optional[Any]:
  for layer_id, hf_layer in enumerate(snake_case_  ):
    copy_layer(snake_case_    ,       snake_case_    ,       F"{prefix}.{layer_id}"  )





def  _snake_case		(snake_case_					:     List[Any]    ,       snake_case_					:     Dict  )							->		str:
  # copy projection
  __UpperCAmelCase	:  str		=     pt_weights['text_projection'].data.T

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





def  _snake_case		(snake_case_					:     Optional[Any]    ,       snake_case_					:     int  )							->		int:
  # copy projection
  __UpperCAmelCase	:  Union[str, Any]		=     pt_weights['visual.proj'].data.T

  # copy layer norms
  copy_linear(hf_model.vision_model.pre_layrnorm    ,       snake_case_    ,       'visual.ln_pre'  )
  copy_linear(hf_model.vision_model.post_layernorm    ,       snake_case_    ,       'visual.ln_post'  )

  # copy embeddings
  __UpperCAmelCase	:  Dict		=     pt_weights['visual.conv1.weight'].data
  __UpperCAmelCase	:  int		=     pt_weights['visual.class_embedding'].data
  __UpperCAmelCase	:  Optional[int]		=     pt_weights['visual.positional_embedding'].data

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





@torch.no_grad()
def  _snake_case		(snake_case_					:     Dict    ,       snake_case_					:     Union[str, Any]    ,       snake_case_					:     List[str]=None  )							->		Optional[int]:
  assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
  __UpperCAmelCase	:  str		=     ChineseCLIPConfig.from_pretrained(snake_case_  )

  __UpperCAmelCase	:  List[str]		=     ChineseCLIPModel(snake_case_  ).eval()

  __UpperCAmelCase	:  Optional[int]		=     torch.load(snake_case_    ,       map_location='cpu'  )['state_dict']
  __UpperCAmelCase	:  str		=     {(name[7:] if name.startswith('module.'  ) else name): value for name, value in pt_weights.items()}

  copy_text_model_and_projection(snake_case_    ,       snake_case_  )
  copy_vision_model_and_projection(snake_case_    ,       snake_case_  )
  __UpperCAmelCase	:  int		=     pt_weights['logit_scale'].data

  hf_model.save_pretrained(snake_case_  )


if __name__ == "__main__":
      _SCREAMING_SNAKE_CASE							 =			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							 =			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

from collections.abc import Iterable
from dataclasses import dataclass
from typing import Any



class  _a  :






							"""simple docstring"""

							snake_case_							=    ("_front", "_back", "_len")



							@dataclass
							class  _a  :






														"""simple docstring"""

														snake_case_							=    None
														snake_case_							=    None
														snake_case_							=    None







							class  _a  :






														"""simple docstring"""

														snake_case_							=    ("_cur",)

														def __init__(   self					:  Tuple				,						a					:  Deque._Node | None     )						->None:
																				SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       cur

														def __iter__(   self					:  Dict     )						->Deque._Iterator:
																				return self






														def   A_	(   self					:  int     )						->Any:
																				if self._cur is None:
																										# finished iterating
																										raise StopIteration
																				SCREAMING_SNAKE_CASE__  :					Tuple =       self._cur.val
																				SCREAMING_SNAKE_CASE__  :					int =       self._cur.next_node

																				return val







							def __init__(   self					:  str				,						a					:  Iterable[Any] | None = None     )						->None:
													SCREAMING_SNAKE_CASE__  :					Any =       None
													SCREAMING_SNAKE_CASE__  :					Any =       None
													SCREAMING_SNAKE_CASE__  :					int =       0

													if iterable is not None:
																			# append every value to the deque
																			for val in iterable:
																									self.append(a     )

							def   A_	(   self					:  Tuple				,						a					:  Any     )						->None:
													SCREAMING_SNAKE_CASE__  :					Optional[Any] =       self._Node(a				,						a				,						a     )
													if self.is_empty():
																			# front = back
																			SCREAMING_SNAKE_CASE__  :					int =       node
																			SCREAMING_SNAKE_CASE__  :					Dict =       1
													else:
																			# connect nodes
																			SCREAMING_SNAKE_CASE__  :					Optional[int] =       node
																			SCREAMING_SNAKE_CASE__  :					Any =       self._back
																			SCREAMING_SNAKE_CASE__  :					Tuple =       node  # assign new back to the new node

																			self._len += 1

																			# make sure there were no errors
																			assert not self.is_empty(), "Error on appending value."

							def   A_	(   self					:  List[str]				,						a					:  Any     )						->None:
													SCREAMING_SNAKE_CASE__  :					List[Any] =       self._Node(a				,						a				,						a     )
													if self.is_empty():
																			# front = back
																			SCREAMING_SNAKE_CASE__  :					str =       node
																			SCREAMING_SNAKE_CASE__  :					Dict =       1
													else:
																			# connect nodes
																			SCREAMING_SNAKE_CASE__  :					Any =       self._front
																			SCREAMING_SNAKE_CASE__  :					Tuple =       node
																			SCREAMING_SNAKE_CASE__  :					int =       node  # assign new front to the new node

																			self._len += 1

																			# make sure there were no errors
																			assert not self.is_empty(), "Error on appending value."

							def   A_	(   self					:  Dict				,						a					:  Iterable[Any]     )						->None:
													for val in iterable:
																			self.append(a     )

							def   A_	(   self					:  Tuple				,						a					:  Iterable[Any]     )						->None:
													for val in iterable:
																			self.appendleft(a     )

							def   A_	(   self					:  Any     )						->Any:
													assert not self.is_empty(), "Deque is empty."

													SCREAMING_SNAKE_CASE__  :					List[Any] =       self._back
													SCREAMING_SNAKE_CASE__  :					Optional[int] =       self._back.prev_node  # set new back
													# drop the last node - python will deallocate memory automatically
													SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       None

													self._len -= 1

													return topop.val

							def   A_	(   self					:  Any     )						->Any:
													assert not self.is_empty(), "Deque is empty."

													SCREAMING_SNAKE_CASE__  :					Any =       self._front
													SCREAMING_SNAKE_CASE__  :					Dict =       self._front.next_node  # set new front and drop the first node
													SCREAMING_SNAKE_CASE__  :					Dict =       None

													self._len -= 1

													return topop.val

							def   A_	(   self					:  str     )						->bool:
													return self._front is None

							def __len__(   self					:  List[str]     )						->int:
													return self._len

							def __eq__(   self					:  List[Any]				,						a					:  object     )						->bool:

													if not isinstance(a				,						a     ):
																			return NotImplemented

													SCREAMING_SNAKE_CASE__  :					List[Any] =       self._front
													SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       other._front

													# if the length of the dequeues are not the same, they are not equal
													if len(self     ) != len(a     ):
																			return False

													while me is not None and oth is not None:
																			# compare every value
																			if me.val != oth.val:
																									return False
																			SCREAMING_SNAKE_CASE__  :					Any =       me.next_node
																			SCREAMING_SNAKE_CASE__  :					Any =       oth.next_node

													return True

							def __iter__(   self					:  Any     )						->Deque._Iterator:
													return Deque._Iterator(self._front     )






							def __repr__(   self					:  Any     )						->str:
													SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       []
													SCREAMING_SNAKE_CASE__  :					int =       self._front
													while aux is not None:
																			# append the values in a list to display
																			values_list.append(aux.val     )
																			SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       aux.next_node

													return f"""[{', '.join(repr(a     ) for val in values_list     )}]"""


if __name__ == "__main__":
				import doctest

				doctest.testmod()