





import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel





def    _lowercase    (  _UpperCAmelCase	,					_UpperCAmelCase	,					_UpperCAmelCase )  ->     int:
				lowerCamelCase       ,     lowerCamelCase       ,     lowerCamelCase		       =pt_weights[F"""{prefix}.in_proj_weight"""].chunk(3	,					dim=0 )
				lowerCamelCase       ,     lowerCamelCase       ,     lowerCamelCase		       =pt_weights[F"""{prefix}.in_proj_bias"""].chunk(3	,					dim=0 )

				lowerCamelCase		       =pt_weights[F"""{prefix}.out_proj.weight"""]
				lowerCamelCase		       =pt_weights[F"""{prefix}.out_proj.bias"""]

				lowerCamelCase		       =q_proj
				lowerCamelCase		       =q_proj_bias

				lowerCamelCase		       =k_proj
				lowerCamelCase		       =k_proj_bias

				lowerCamelCase		       =v_proj
				lowerCamelCase		       =v_proj_bias

				lowerCamelCase		       =out_proj_weights
				lowerCamelCase		       =out_proj_bias





def    _lowercase    (  _UpperCAmelCase	,					_UpperCAmelCase	,					_UpperCAmelCase )  ->     Tuple:
				copy_linear(hf_mlp.fca	,					_UpperCAmelCase	,					F"""{prefix}.c_fc""" )
				copy_linear(hf_mlp.fca	,					_UpperCAmelCase	,					F"""{prefix}.c_proj""" )





def    _lowercase    (  _UpperCAmelCase	,					_UpperCAmelCase	,					_UpperCAmelCase )  ->     Union[str, Any]:
				lowerCamelCase		       =pt_weights[F"""{prefix}.weight"""].data
				lowerCamelCase		       =pt_weights[F"""{prefix}.bias"""].data





def    _lowercase    (  _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    _lowercase    (  _UpperCAmelCase	,					_UpperCAmelCase	,					_UpperCAmelCase )  ->     List[Any]:
				for layer_id, hf_layer in enumerate(_UpperCAmelCase ):
								copy_layer(_UpperCAmelCase	,					_UpperCAmelCase	,					F"""{prefix}.{layer_id}""" )





def    _lowercase    (  _UpperCAmelCase	,					_UpperCAmelCase )  ->     Dict:
				# copy projection
				lowerCamelCase		       =pt_weights["""text_projection"""].data.T

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





def    _lowercase    (  _UpperCAmelCase	,					_UpperCAmelCase )  ->     Optional[int]:
				# copy projection
				lowerCamelCase		       =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
				lowerCamelCase		       =pt_weights["""visual.conv1.weight"""].data
				lowerCamelCase		       =pt_weights["""visual.class_embedding"""].data
				lowerCamelCase		       =pt_weights["""visual.positional_embedding"""].data

				# copy encoder
				copy_layers(hf_model.vision_model.encoder.layers	,					_UpperCAmelCase	,					"""visual.transformer.resblocks""" )





@torch.no_grad()
def    _lowercase    (  _UpperCAmelCase	,					_UpperCAmelCase	,					_UpperCAmelCase=None )  ->     str:
				assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
				lowerCamelCase		       =ChineseCLIPConfig.from_pretrained(_UpperCAmelCase )

				lowerCamelCase		       =ChineseCLIPModel(_UpperCAmelCase ).eval()

				lowerCamelCase		       =torch.load(_UpperCAmelCase	,					map_location="""cpu""" )["""state_dict"""]
				lowerCamelCase		       ={(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 )
				lowerCamelCase		       =pt_weights["""logit_scale"""].data

				hf_model.save_pretrained(_UpperCAmelCase )


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

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











import argparse
import os
import re


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_dummies.py
__SCREAMING_SNAKE_CASE			:      str      			="""src/transformers"""

# Matches is_xxx_available()
__SCREAMING_SNAKE_CASE			:      Union[str, Any]      			=re.compile(R"""is\_([a-z_]*)_available()""")
# Matches from xxx import bla
__SCREAMING_SNAKE_CASE			:      Dict      			=re.compile(R"""\s+from\s+\S*\s+import\s+([^\(\s].*)\n""")
__SCREAMING_SNAKE_CASE			:      List[str]      			=re.compile(R"""^\s+if\s+not\s+\(?is\_[a-z_]*\_available\(\)""")


__SCREAMING_SNAKE_CASE			:      Tuple      			="""
{0} = None
"""

__SCREAMING_SNAKE_CASE			:      Dict      			="""
class {0}(metaclass=DummyObject):
    _backends = {1}

    def __init__(self, *args, **kwargs):
        requires_backends(self, {1})
"""


__SCREAMING_SNAKE_CASE			:      Optional[Any]      			="""
def {0}(*args, **kwargs):
    requires_backends({0}, {1})
"""

def 	lowerCAmelCase_	(     lowerCamelCase:				Dict  )    ->	str:


      '''simple docstring'''




      if _re_test_backend.search(lowerCamelCase  ) is None:
            return None
      lowerCamelCase__					     = [b[0] for b in _re_backend.findall(lowerCamelCase  )]
      backends.sort()
      return "_and_".join(lowerCamelCase  )

def 	lowerCAmelCase_	(     )    ->	Any:


      '''simple docstring'''




      with open(os.path.join(lowerCamelCase   , '__init__.py'  )   , 'r'   , encoding='utf-8'   , newline='\n'  ) as f:
            lowerCamelCase__					     = f.readlines()

      # Get to the point we do the actual imports for type checking
      lowerCamelCase__					     = 0
      while not lines[line_index].startswith('if TYPE_CHECKING'  ):
            line_index += 1

      lowerCamelCase__					     = {}
      # Go through the end of the file
      while line_index < len(lowerCamelCase  ):
            # If the line is an if is_backend_available, we grab all objects associated.
            lowerCamelCase__					     = find_backend(lines[line_index]  )
            if backend is not None:
                  while not lines[line_index].startswith('    else:'  ):
                        line_index += 1
                  line_index += 1

                  lowerCamelCase__					     = []
                  # Until we unindent, add backend objects to the list
                  while len(lines[line_index]  ) <= 1 or lines[line_index].startswith(' ' * 8  ):
                        lowerCamelCase__					     = lines[line_index]
                        lowerCamelCase__					     = _re_single_line_import.search(lowerCamelCase  )
                        if single_line_import_search is not None:
                              objects.extend(single_line_import_search.groups()[0].split(', '  )  )
                        elif line.startswith(' ' * 12  ):
                              objects.append(line[12:-2]  )
                        line_index += 1

                  lowerCamelCase__					     = objects
            else:
                  line_index += 1

      return backend_specific_objects

def 	lowerCAmelCase_	(     lowerCamelCase:				int   , lowerCamelCase:				Tuple  )    ->	Optional[Any]:


      '''simple docstring'''




      if name.isupper():
            return DUMMY_CONSTANT.format(lowerCamelCase  )
      elif name.islower():
            return DUMMY_FUNCTION.format(lowerCamelCase   , lowerCamelCase  )
      else:
            return DUMMY_CLASS.format(lowerCamelCase   , lowerCamelCase  )

def 	lowerCAmelCase_	(     lowerCamelCase:				Tuple=None  )    ->	int:


      '''simple docstring'''




      if backend_specific_objects is None:
            lowerCamelCase__					     = read_init()
      # For special correspondence backend to module name as used in the function requires_modulename
      lowerCamelCase__					     = {}

      for backend, objects in backend_specific_objects.items():
            lowerCamelCase__					     = '[' + ', '.join(f"""\"{b}\"""" for b in backend.split('_and_'  )  ) + ']'
            lowerCamelCase__					     = '# This file is autogenerated by the command `make fix-copies`, do not edit.\n'
            dummy_file += "from ..utils import DummyObject, requires_backends\n\n"
            dummy_file += "\n".join([create_dummy_object(lowerCamelCase   , lowerCamelCase  ) for o in objects]  )
            lowerCamelCase__					     = dummy_file

      return dummy_files

def 	lowerCAmelCase_	(     lowerCamelCase:				List[Any]=False  )    ->	Union[str, Any]:


      '''simple docstring'''




      lowerCamelCase__					     = create_dummy_files()
      # For special correspondence backend to shortcut as used in utils/dummy_xxx_objects.py
      lowerCamelCase__					     = {'torch': 'pt'}

      # Locate actual dummy modules and read their content.
      lowerCamelCase__					     = os.path.join(lowerCamelCase   , 'utils'  )
      lowerCamelCase__					     = {
          backend: os.path.join(lowerCamelCase   , f"""dummy_{short_names.get(lowerCamelCase   , lowerCamelCase  )}_objects.py"""  )
          for backend in dummy_files.keys()
      }

      lowerCamelCase__					     = {}
      for backend, file_path in dummy_file_paths.items():
            if os.path.isfile(lowerCamelCase  ):
                  with open(lowerCamelCase   , 'r'   , encoding='utf-8'   , newline='\n'  ) as f:
                        lowerCamelCase__					     = f.read()
            else:
                  lowerCamelCase__					     = ''

      for backend in dummy_files.keys():
            if dummy_files[backend] != actual_dummies[backend]:
                  if overwrite:
                        print(
                            f"""Updating transformers.utils.dummy_{short_names.get(lowerCamelCase   , lowerCamelCase  )}_objects.py as the main """
                            '__init__ has new objects.'  )
                        with open(dummy_file_paths[backend]   , 'w'   , encoding='utf-8'   , newline='\n'  ) as f:
                              f.write(dummy_files[backend]  )
                  else:
                        raise ValueError(
                            'The main __init__ has objects that are not present in '
                            f"""transformers.utils.dummy_{short_names.get(lowerCamelCase   , lowerCamelCase  )}_objects.py. Run `make fix-copies` """
                            'to fix this.'  )


if __name__ == "__main__":
      __SCREAMING_SNAKE_CASE			:      str      			=argparse.ArgumentParser()
      parser.add_argument("""--fix_and_overwrite""", action="""store_true""", help="""Whether to fix inconsistencies.""")
      __SCREAMING_SNAKE_CASE			:      Union[str, Any]      			=parser.parse_args()

      check_dummies(args.fix_and_overwrite)