



import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel







def       lowerCamelCase_  (    _a       :						List[str]   ,		_a       :						Optional[int]   ,		_a       :						Optional[Any]    ):



    '''simple docstring'''




    UpperCAmelCase_		, UpperCAmelCase_		, UpperCAmelCase_     :       List[Any]  		=					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_     :       Tuple  		=					pt_weights[F'''{prefix}.out_proj.weight''']
    UpperCAmelCase_     :       Any  		=					pt_weights[F'''{prefix}.out_proj.bias''']

    UpperCAmelCase_     :       Optional[int]  		=					q_proj
    UpperCAmelCase_     :       Dict  		=					q_proj_bias

    UpperCAmelCase_     :       List[Any]  		=					k_proj
    UpperCAmelCase_     :       Optional[int]  		=					k_proj_bias

    UpperCAmelCase_     :       Union[str, Any]  		=					v_proj
    UpperCAmelCase_     :       Optional[Any]  		=					v_proj_bias

    UpperCAmelCase_     :       Union[str, Any]  		=					out_proj_weights
    UpperCAmelCase_     :       Optional[int]  		=					out_proj_bias







def       lowerCamelCase_  (    _a       :						Dict   ,		_a       :						Optional[int]   ,		_a       :						Tuple    ):



    '''simple docstring'''




    copy_linear(hf_mlp.fca   ,		_a   ,		F'''{prefix}.c_fc'''    )
    copy_linear(hf_mlp.fca   ,		_a   ,		F'''{prefix}.c_proj'''    )







def       lowerCamelCase_  (    _a       :						Optional[Any]   ,		_a       :						List[Any]   ,		_a       :						Optional[int]    ):



    '''simple docstring'''




    UpperCAmelCase_     :       List[Any]  		=					pt_weights[F'''{prefix}.weight'''].data
    UpperCAmelCase_     :       Union[str, Any]  		=					pt_weights[F'''{prefix}.bias'''].data







def       lowerCamelCase_  (    _a       :						Union[str, Any]   ,		_a       :						Union[str, Any]   ,		_a       :						Optional[Any]    ):



    '''simple docstring'''




    copy_linear(hf_layer.layer_norma   ,		_a   ,		F'''{prefix}.ln_1'''    )
    copy_linear(hf_layer.layer_norma   ,		_a   ,		F'''{prefix}.ln_2'''    )

    # copy MLP
    copy_mlp(hf_layer.mlp   ,		_a   ,		F'''{prefix}.mlp'''    )

    # copy attn
    copy_attn_layer(hf_layer.self_attn   ,		_a   ,		F'''{prefix}.attn'''    )







def       lowerCamelCase_  (    _a       :						Any   ,		_a       :						Union[str, Any]   ,		_a       :						Union[str, Any]    ):



    '''simple docstring'''




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







def       lowerCamelCase_  (    _a       :						Tuple   ,		_a       :						Optional[int]    ):



    '''simple docstring'''




    UpperCAmelCase_     :       List[Any]  		=					pt_weights["""text_projection"""].data.T

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







def       lowerCamelCase_  (    _a       :						int   ,		_a       :						Tuple    ):



    '''simple docstring'''




    UpperCAmelCase_     :       Tuple  		=					pt_weights["""visual.proj"""].data.T

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

    # copy embeddings
    UpperCAmelCase_     :       str  		=					pt_weights["""visual.conv1.weight"""].data
    UpperCAmelCase_     :       List[str]  		=					pt_weights["""visual.class_embedding"""].data
    UpperCAmelCase_     :       Dict  		=					pt_weights["""visual.positional_embedding"""].data

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







@torch.no_grad()
def       lowerCamelCase_  (    _a       :						Any   ,		_a       :						Dict   ,		_a       :						Optional[int]=None    ):



    '''simple docstring'''




    assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
    UpperCAmelCase_     :       Optional[int]  		=					ChineseCLIPConfig.from_pretrained(_a    )

    UpperCAmelCase_     :       str  		=					ChineseCLIPModel(_a    ).eval()

    UpperCAmelCase_     :       Optional[Any]  		=					torch.load(_a   ,		map_location="""cpu"""    )["""state_dict"""]
    UpperCAmelCase_     :       List[Any]  		=					{(name[7:] if name.startswith("""module."""    ) else name): value for name, value in pt_weights.items()}

    copy_text_model_and_projection(_a   ,		_a    )
    copy_vision_model_and_projection(_a   ,		_a    )
    UpperCAmelCase_     :       Optional[Any]  		=					pt_weights["""logit_scale"""].data

    hf_model.save_pretrained(_a    )


if __name__ == "__main__":
      UpperCamelCase_									=   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_									=   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			(  UpperCamelCase_				:					int  ,						UpperCamelCase_				:					int  ,						UpperCamelCase_				:					int  ,						UpperCamelCase_				:					int		)	->		int:
 return len_board * len_board_column * row + column




def 					a			(  UpperCamelCase_				:					list[list[str]]  ,						UpperCamelCase_				:					str  ,						UpperCamelCase_				:					int  ,						UpperCamelCase_				:					int  ,						UpperCamelCase_				:					int  ,						UpperCamelCase_				:					set[int]  ,						)	->		bool:

 if board[row][column] != word[word_index]:
  return False

 if word_index == len(UpperCamelCase_		) - 1:
  return True

 __A						:							List[str]      =						[(0, 1), (0, -1), (-1, 0), (1, 0)]
 __A						:							Any      =						len(UpperCamelCase_		)
 __A						:							Union[str, Any]      =						len(board[0]		)
 for direction in traverts_directions:
  __A						:							Any      =						row + direction[0]
  __A						:							List[Any]      =						column + direction[1]
  if not (0 <= next_i < len_board and 0 <= next_j < len_board_column):
   continue

  __A						:							Any      =						get_point_key(UpperCamelCase_  ,						UpperCamelCase_  ,						UpperCamelCase_  ,						UpperCamelCase_		)
  if key in visited_points_set:
   continue

  visited_points_set.add(UpperCamelCase_		)
  if exits_word(UpperCamelCase_  ,						UpperCamelCase_  ,						UpperCamelCase_  ,						UpperCamelCase_  ,						word_index + 1  ,						UpperCamelCase_		):
   return True

  visited_points_set.remove(UpperCamelCase_		)

 return False




def 					a			(  UpperCamelCase_				:					list[list[str]]  ,						UpperCamelCase_				:					str		)	->		bool:
 __A						:							Optional[Any]      =						(
     'The board should be a non empty matrix of single chars strings.'
 )

 __A						:							Dict      =						len(UpperCamelCase_		)
 if not isinstance(UpperCamelCase_  ,						UpperCamelCase_		) or len(UpperCamelCase_		) == 0:
  raise ValueError(UpperCamelCase_		)

 for row in board:
  if not isinstance(UpperCamelCase_  ,						UpperCamelCase_		) or len(UpperCamelCase_		) == 0:
   raise ValueError(UpperCamelCase_		)

  for item in row:
   if not isinstance(UpperCamelCase_  ,						UpperCamelCase_		) or len(UpperCamelCase_		) != 1:
    raise ValueError(UpperCamelCase_		)

    # Validate word
 if not isinstance(UpperCamelCase_  ,						UpperCamelCase_		) or len(UpperCamelCase_		) == 0:
  raise ValueError(
      'The word parameter should be a string of length greater than 0.'		)

 __A						:							List[Any]      =						len(board[0]		)
 for i in range(UpperCamelCase_		):
  for j in range(UpperCamelCase_		):
   if exits_word(
       UpperCamelCase_  ,						UpperCamelCase_  ,						UpperCamelCase_  ,						UpperCamelCase_  ,						0  ,						{get_point_key(UpperCamelCase_  ,						UpperCamelCase_  ,						UpperCamelCase_  ,						UpperCamelCase_		)}		):
    return True

 return False


if __name__ == "__main__":
   import doctest

   doctest.testmod()





