code: 
  
  
  
  
  """simple docstring"""
  
  import argparse
  
  import torch
  
  from transformers import ChineseCLIPConfig, ChineseCLIPModel
  
  
  
  def   UpperCamelCase__     (		lowercase__  :     int       ,     lowercase__  :     Union[str, Any]       ,     lowercase__  :     str   ):
  		snake_case		:						Union[str, Any]								=				pt_weights[F'''{prefix}.in_proj_weight'''].chunk(3       ,     dim=0   )
  		snake_case		:						Any								=				pt_weights[F'''{prefix}.in_proj_bias'''].chunk(3       ,     dim=0   )
  
  		snake_case		:						List[Any]								=				pt_weights[F'''{prefix}.out_proj.weight''']
  		snake_case		:						str								=				pt_weights[F'''{prefix}.out_proj.bias''']
  
  		snake_case		:						str								=				q_proj
  		snake_case		:						Optional[Any]								=				q_proj_bias
  
  		snake_case		:						List[Any]								=				k_proj
  		snake_case		:						str								=				k_proj_bias
  
  		snake_case		:						Tuple								=				v_proj
  		snake_case		:						List[Any]								=				v_proj_bias
  
  		snake_case		:						Dict								=				out_proj_weights
  		snake_case		:						Optional[int]								=				out_proj_bias
  
  
  
  def   UpperCamelCase__     (		lowercase__  :     Any       ,     lowercase__  :     List[str]       ,     lowercase__  :     Any   ):
  		copy_linear(hf_mlp.fca       ,     lowercase__       ,     F'''{prefix}.c_fc'''   )
  		copy_linear(hf_mlp.fca       ,     lowercase__       ,     F'''{prefix}.c_proj'''   )
  
  
  
  def   UpperCamelCase__     (		lowercase__  :     List[Any]       ,     lowercase__  :     Dict       ,     lowercase__  :     List[Any]   ):
  		snake_case		:						List[Any]								=				pt_weights[F'''{prefix}.weight'''].data
  		snake_case		:						Optional[int]								=				pt_weights[F'''{prefix}.bias'''].data
  
  
  
  def   UpperCamelCase__     (		lowercase__  :     Dict       ,     lowercase__  :     List[Any]       ,     lowercase__  :     List[Any]   ):
  		# copy layer norms
  		copy_linear(hf_layer.layer_norma       ,     lowercase__       ,     F'''{prefix}.ln_1'''   )
  		copy_linear(hf_layer.layer_norma       ,     lowercase__       ,     F'''{prefix}.ln_2'''   )
  
  		# copy MLP
  		copy_mlp(hf_layer.mlp       ,     lowercase__       ,     F'''{prefix}.mlp'''   )
  
  		# copy attn
  		copy_attn_layer(hf_layer.self_attn       ,     lowercase__       ,     F'''{prefix}.attn'''   )
  
  
  
  def   UpperCamelCase__     (		lowercase__  :     List[Any]       ,     lowercase__  :     Optional[int]       ,     lowercase__  :     Optional[int]   ):
  		for layer_id, hf_layer in enumerate(lowercase__   ):
  				copy_layer(lowercase__       ,     lowercase__       ,     F'''{prefix}.{layer_id}'''   )
  
  
  
  def   UpperCamelCase__     (		lowercase__  :     Tuple       ,     lowercase__  :     List[Any]   ):
  		# copy projection
  		snake_case		:						List[Any]								=				pt_weights["text_projection"].data.T
  
  		# copy text encoder
  		for name, param in hf_model.text_model.named_parameters():
  				snake_case		:						List[str]								=				pt_weights[F'''bert.{name}'''].data
  
  
  
  def   UpperCamelCase__     (		lowercase__  :     Dict       ,     lowercase__  :     Optional[Any]   ):
  		# copy projection
  		snake_case		:						Any								=				pt_weights["visual.proj"].data.T
  
  		# copy layer norms
  		copy_linear(hf_model.vision_model.pre_layrnorm       ,     lowercase__       ,     "visual.ln_pre"   )
  		copy_linear(hf_model.vision_model.post_layernorm       ,     lowercase__       ,     "visual.ln_post"   )
  
  		# copy embeddings
  		snake_case		:						Dict								=				pt_weights["visual.conv1.weight"].data
  		snake_case		:						Optional[int]								=				pt_weights["visual.class_embedding"].data
  		snake_case		:						Optional[Any]								=				pt_weights["visual.positional_embedding"].data
  
  		# copy encoder
  		copy_layers(hf_model.vision_model.encoder.layers       ,     lowercase__       ,     "visual.transformer.resblocks"   )
  
  
  
  @torch.no_grad()
  def   UpperCamelCase__     (		lowercase__  :     str       ,     lowercase__  :     Any       ,     lowercase__  :     Tuple=None   ):
  		assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
  		snake_case		:						Union[str, Any]								=				ChineseCLIPConfig.from_pretrained(lowercase__   )
  
  		snake_case		:						Any								=				ChineseCLIPModel(lowercase__   ).eval()
  
  		snake_case		:						int								=				torch.load(lowercase__       ,     map_location="cpu"   )["state_dict"]
  		snake_case		:						Dict								=				{(name[7:] if name.startswith("module."   ) else name): value for name, value in pt_weights.items()}
  
  		copy_text_model_and_projection(lowercase__       ,     lowercase__   )
  		copy_vision_model_and_projection(lowercase__       ,     lowercase__   )
  		snake_case		:						Dict								=				pt_weights["logit_scale"].data
  
  		hf_model.save_pretrained(lowercase__   )
  
  
  if __name__ == "__main__":
  							__A 		=			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 		=			parser.parse_args()
  
  							convert_chinese_clip_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
  							print("The conversion is finished!")
  
  
style_context: 
  
  
  
  
  """simple docstring"""
  
  import os
  import re
  import unicodedata
  from shutil import copyfile
  from typing import Any, Dict, List, Optional, Tuple
  
  import sentencepiece as spm
  
  from ...tokenization_utils import AddedToken, PreTrainedTokenizer
  from ...utils import logging
  
  
  __A 		=			logging.get_logger(__name__)
  __A 		=			{"vocab_file": "spiece.model"}
  
  __A 		=			{
      "vocab_file": {
          "google/fnet-base": "https://huggingface.co/google/fnet-base/resolve/main/spiece.model",
          "google/fnet-large": "https://huggingface.co/google/fnet-large/resolve/main/spiece.model",
      },
  }
  
  __A 		=			{
      "google/fnet-base": 512,
      "google/fnet-large": 512,
  }
  
  __A 		=			"▁"
  class 			lowerCamelCase__    (  lowerCamelCase_			):
  			a__ :      Dict								=     VOCAB_FILES_NAMES
  			a__ :      Tuple								=     PRETRAINED_VOCAB_FILES_MAP
  			a__ :      int								=     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
  			a__ :      List[Any]								=     ["""input_ids""", """token_type_ids"""]
  
  
  			def __init__(     self				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE=False				, SCREAMING_SNAKE_CASE=True				, SCREAMING_SNAKE_CASE=True				, SCREAMING_SNAKE_CASE="<unk>"				, SCREAMING_SNAKE_CASE="[SEP]"				, SCREAMING_SNAKE_CASE="<pad>"				, SCREAMING_SNAKE_CASE="[CLS]"				, SCREAMING_SNAKE_CASE="[MASK]"				, SCREAMING_SNAKE_CASE = None				, **SCREAMING_SNAKE_CASE				, ):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						Dict								=				(
  					    AddedToken(SCREAMING_SNAKE_CASE				, lstrip=SCREAMING_SNAKE_CASE				, rstrip=SCREAMING_SNAKE_CASE				, normalized=SCREAMING_SNAKE_CASE							)
  					    if isinstance(SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE							)
  					    else mask_token
  					)
  
  					snake_case		:						Optional[int]								=				{} if sp_model_kwargs is None else sp_model_kwargs
  
  					super().__init__(
  					    do_lower_case=SCREAMING_SNAKE_CASE				, remove_space=SCREAMING_SNAKE_CASE				, keep_accents=SCREAMING_SNAKE_CASE				, unk_token=SCREAMING_SNAKE_CASE				, sep_token=SCREAMING_SNAKE_CASE				, pad_token=SCREAMING_SNAKE_CASE				, cls_token=SCREAMING_SNAKE_CASE				, mask_token=SCREAMING_SNAKE_CASE				, sp_model_kwargs=self.sp_model_kwargs				, **SCREAMING_SNAKE_CASE				, )
  
  					snake_case		:						List[str]								=				do_lower_case
  					snake_case		:						Any								=				remove_space
  					snake_case		:						Union[str, Any]								=				keep_accents
  					snake_case		:						Optional[int]								=				vocab_file
  
  					snake_case		:						Dict								=				spm.SentencePieceProcessor(**self.sp_model_kwargs							)
  					self.sp_model.Load(SCREAMING_SNAKE_CASE							)
  
  
  			@property
  			def 							lowerCamelCase_				(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					return len(self.sp_model							)
  
  
  			def 							lowerCamelCase_				(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						Optional[int]								=				{self.convert_ids_to_tokens(SCREAMING_SNAKE_CASE							): i for i in range(self.vocab_size							)}
  					vocab.update(self.added_tokens_encoder							)
  					return vocab
  
  
  			def __getstate__(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						Tuple								=				self.__dict__.copy()
  					snake_case		:						List[Any]								=				None
  					return state
  
  
  			def __setstate__(     self				, SCREAMING_SNAKE_CASE							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						Union[str, Any]								=				d
  
  					# for backward compatibility
  					if not hasattr(self				, "sp_model_kwargs"							):
  							snake_case		:						List[Any]								=				{}
  
  					snake_case		:						Tuple								=				spm.SentencePieceProcessor(**self.sp_model_kwargs							)
  					self.sp_model.Load(self.vocab_file							)
  
  
  			def 							lowerCamelCase_				(     self				, SCREAMING_SNAKE_CASE							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					if self.remove_space:
  							snake_case		:						int								=				" ".join(inputs.strip().split()							)
  					else:
  							snake_case		:						Union[str, Any]								=				inputs
  					snake_case		:						Union[str, Any]								=				outputs.replace("``"				, "\""							).replace("''"				, "\""							)
  
  					if not self.keep_accents:
  							snake_case		:						List[Any]								=				unicodedata.normalize("NFKD"				, SCREAMING_SNAKE_CASE							)
  							snake_case		:						int								=				"".join([c for c in outputs if not unicodedata.combining(SCREAMING_SNAKE_CASE							)]							)
  					if self.do_lower_case:
  							snake_case		:						Union[str, Any]								=				outputs.lower()
  
  					return outputs
  
  
  			def 							lowerCamelCase_				(     self				, SCREAMING_SNAKE_CASE							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						List[str]								=				self.preprocess_text(SCREAMING_SNAKE_CASE							)
  					snake_case		:						List[str]								=				self.sp_model.encode(SCREAMING_SNAKE_CASE				, out_type=SCREAMING_SNAKE_CASE							)
  					snake_case		:						Optional[Any]								=				[]
  					for piece in pieces:
  							if len(SCREAMING_SNAKE_CASE							) > 1 and piece[-1] == str(","							) and piece[-2].isdigit():
  									snake_case		:						Union[str, Any]								=				self.sp_model.EncodeAsPieces(piece[:-1].replace(SCREAMING_SNAKE_CASE				, ""							)							)
  									if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE:
  											if len(cur_pieces[0]							) == 1:
  													snake_case		:						List[Any]								=				cur_pieces[1:]
  											else:
  													snake_case		:						Dict								=				cur_pieces[0][1:]
  									cur_pieces.append(piece[-1]							)
  									new_pieces.extend(SCREAMING_SNAKE_CASE							)
  							else:
  									new_pieces.append(SCREAMING_SNAKE_CASE							)
  
  					return new_pieces
  
  
  			def 							lowerCamelCase_				(     self				, SCREAMING_SNAKE_CASE							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					return self.sp_model.PieceToId(SCREAMING_SNAKE_CASE							)
  
  
  			def 							lowerCamelCase_				(     self				, SCREAMING_SNAKE_CASE							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					return self.sp_model.IdToPiece(SCREAMING_SNAKE_CASE							)
  
  
  			def 							lowerCamelCase_				(     self				, SCREAMING_SNAKE_CASE							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						Dict								=				[]
  					snake_case		:						Any								=				""
  					snake_case		:						Dict								=				False
  					for token in tokens:
  							# make sure that special tokens are not decoded using sentencepiece model
  							if token in self.all_special_tokens:
  									if not prev_is_special:
  											out_string += " "
  									out_string += self.sp_model.decode(SCREAMING_SNAKE_CASE							) + token
  									snake_case		:						Optional[int]								=				True
  									snake_case		:						Union[str, Any]								=				[]
  							else:
  									current_sub_tokens.append(SCREAMING_SNAKE_CASE							)
  									snake_case		:						Tuple								=				False
  					out_string += self.sp_model.decode(SCREAMING_SNAKE_CASE							)
  					return out_string.strip()
  
  
  			def 							lowerCamelCase_				(     self				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE = False				, SCREAMING_SNAKE_CASE = None				, SCREAMING_SNAKE_CASE = True				, **SCREAMING_SNAKE_CASE				, ):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						int								=				kwargs.pop("use_source_tokenizer"				, SCREAMING_SNAKE_CASE							)
  
  					snake_case		:						Tuple								=				self.convert_ids_to_tokens(SCREAMING_SNAKE_CASE				, skip_special_tokens=SCREAMING_SNAKE_CASE							)
  
  					# To avoid mixing byte-level and unicode for byte-level BPT
  					# we need to build string separately for added tokens and byte-level tokens
  					# cf. https://github.com/huggingface/transformers/issues/1133
  					snake_case		:						Any								=				[]
  					snake_case		:						Union[str, Any]								=				[]
  					for token in filtered_tokens:
  							if skip_special_tokens and token in self.all_special_ids:
  									continue
  							if token in self.added_tokens_encoder:
  									if current_sub_text:
  											sub_texts.append(self.convert_tokens_to_string(SCREAMING_SNAKE_CASE							)							)
  											snake_case		:						Optional[Any]								=				[]
  									sub_texts.append(SCREAMING_SNAKE_CASE							)
  							else:
  									current_sub_text.append(SCREAMING_SNAKE_CASE							)
  					if current_sub_text:
  							sub_texts.append(self.convert_tokens_to_string(SCREAMING_SNAKE_CASE							)							)
  
  					# Mimic the behavior of the Rust tokenizer:
  					# No space after <unk>
  					if spaces_between_special_tokens:
  							snake_case		:						Any								=				re.sub(r"(<unk>) "				, r"\1"				, " ".join(SCREAMING_SNAKE_CASE							)							)
  					else:
  							snake_case		:						Optional[int]								=				"".join(SCREAMING_SNAKE_CASE							)
  
  					snake_case		:						Optional[int]								=				(
  					    clean_up_tokenization_spaces
  					    if clean_up_tokenization_spaces is not None
  					    else self.clean_up_tokenization_spaces
  					)
  					if clean_up_tokenization_spaces:
  							snake_case		:						List[Any]								=				self.clean_up_tokenization(SCREAMING_SNAKE_CASE							)
  							return clean_text
  					else:
  							return text
  
  
  			def 							lowerCamelCase_				(     self				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE = None							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						Tuple								=				[self.sep_token_id]
  					snake_case		:						List[str]								=				[self.cls_token_id]
  					if token_ids_a is None:
  							return cls + token_ids_a + sep
  					return cls + token_ids_a + sep + token_ids_a + sep
  
  
  			def 							lowerCamelCase_				(     self				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE = None				, SCREAMING_SNAKE_CASE = False							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					if already_has_special_tokens:
  							return super().get_special_tokens_mask(
  							    token_ids_a=SCREAMING_SNAKE_CASE				, token_ids_a=SCREAMING_SNAKE_CASE				, already_has_special_tokens=SCREAMING_SNAKE_CASE							)
  
  					if token_ids_a is not None:
  							return [1] + ([0] * len(SCREAMING_SNAKE_CASE							)) + [1] + ([0] * len(SCREAMING_SNAKE_CASE							)) + [1]
  					return [1] + ([0] * len(SCREAMING_SNAKE_CASE							)) + [1]
  
  
  			def 							lowerCamelCase_				(     self				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE = None							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						Dict								=				[self.sep_token_id]
  					snake_case		:						Optional[int]								=				[self.cls_token_id]
  
  					if token_ids_a is None:
  							return len(cls + token_ids_a + sep							) * [0]
  					return len(cls + token_ids_a + sep							) * [0] + len(token_ids_a + sep							) * [1]
  
  
  
  			def 							lowerCamelCase_				(     self				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE = None							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					if not os.path.isdir(SCREAMING_SNAKE_CASE							):
  							logger.error(F'''Vocabulary path ({save_directory}) should be a directory'''							)
  							return
  					snake_case		:						Tuple								=				os.path.join(
  					    SCREAMING_SNAKE_CASE				, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]							)
  
  					if os.path.abspath(self.vocab_file							) != os.path.abspath(SCREAMING_SNAKE_CASE							) and os.path.isfile(self.vocab_file							):
  							copyfile(self.vocab_file				, SCREAMING_SNAKE_CASE							)
  					elif not os.path.isfile(self.vocab_file							):
  							with open(SCREAMING_SNAKE_CASE				, "wb"							) as fi:
  									snake_case		:						List[str]								=				self.sp_model.serialized_model_proto()
  									fi.write(SCREAMING_SNAKE_CASE							)
  
  					return (out_vocab_file,)
  
  