




import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel




def        __SCREAMING_SNAKE_CASE (	lowercase_       ,  lowercase_       ,  lowercase_   )							->						Optional[Any]:

    """simple docstring"""


    __UpperCamelCase		,   __UpperCamelCase		,   __UpperCamelCase			=    pt_weights[F"{prefix}.in_proj_weight"].chunk(3       ,  dim=0   )
    __UpperCamelCase		,   __UpperCamelCase		,   __UpperCamelCase			=    pt_weights[F"{prefix}.in_proj_bias"].chunk(3       ,  dim=0   )

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

    __UpperCamelCase			=    q_proj
    __UpperCamelCase			=    q_proj_bias

    __UpperCamelCase			=    k_proj
    __UpperCamelCase			=    k_proj_bias

    __UpperCamelCase			=    v_proj
    __UpperCamelCase			=    v_proj_bias

    __UpperCamelCase			=    out_proj_weights
    __UpperCamelCase			=    out_proj_bias




def        __SCREAMING_SNAKE_CASE (	lowercase_       ,  lowercase_       ,  lowercase_   )							->						Optional[Any]:

    """simple docstring"""


    copy_linear(hf_mlp.fca       ,  lowercase_       ,  F"{prefix}.c_fc"   )
    copy_linear(hf_mlp.fca       ,  lowercase_       ,  F"{prefix}.c_proj"   )




def        __SCREAMING_SNAKE_CASE (	lowercase_       ,  lowercase_       ,  lowercase_   )							->						Dict:

    """simple docstring"""


    __UpperCamelCase			=    pt_weights[F"{prefix}.weight"].data
    __UpperCamelCase			=    pt_weights[F"{prefix}.bias"].data




def        __SCREAMING_SNAKE_CASE (	lowercase_       ,  lowercase_       ,  lowercase_   )							->						Optional[Any]:

    """simple docstring"""


    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        __SCREAMING_SNAKE_CASE (	lowercase_       ,  lowercase_       ,  lowercase_   )							->						List[Any]:

    """simple docstring"""


    for layer_id, hf_layer in enumerate(lowercase_   ):
        copy_layer(lowercase_       ,  lowercase_       ,  F"{prefix}.{layer_id}"   )




def        __SCREAMING_SNAKE_CASE (	lowercase_       ,  lowercase_   )							->						Optional[int]:

    """simple docstring"""


    __UpperCamelCase			=    pt_weights['''text_projection'''].data.T

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




def        __SCREAMING_SNAKE_CASE (	lowercase_       ,  lowercase_   )							->						Union[str, Any]:

    """simple docstring"""


    __UpperCamelCase			=    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
    __UpperCamelCase			=    pt_weights['''visual.conv1.weight'''].data
    __UpperCamelCase			=    pt_weights['''visual.class_embedding'''].data
    __UpperCamelCase			=    pt_weights['''visual.positional_embedding'''].data

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




@torch.no_grad()
def        __SCREAMING_SNAKE_CASE (	lowercase_       ,  lowercase_       ,  lowercase_=None   )							->						List[Any]:

    """simple docstring"""


    assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
    __UpperCamelCase			=    ChineseCLIPConfig.from_pretrained(lowercase_   )

    __UpperCamelCase			=    ChineseCLIPModel(lowercase_   ).eval()

    __UpperCamelCase			=    torch.load(lowercase_       ,  map_location='''cpu'''   )['''state_dict''']
    __UpperCamelCase			=    {(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_   )
    __UpperCamelCase			=    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!")












import json
import os
import shutil
import tempfile
import unittest

from transformers import is_speech_available, is_torch_available
from transformers.models.speechta import SpeechTaTokenizer
from transformers.testing_utils import get_tests_dir, require_torch
from transformers.utils import FEATURE_EXTRACTOR_NAME


if is_speech_available() and is_torch_available():
 from transformers import SpeechTaFeatureExtractor, SpeechTaProcessor

 from .test_feature_extraction_speechta import floats_list


a_ 		=  get_tests_dir("fixtures/test_sentencepiece_bpe_char.model")

@require_torch
class 			_lowerCamelCase    (      unittest.TestCase ):

      """simple docstring"""

      def    snake_case						(  self       :			int					):
          __UpperCamelCase			=    tempfile.mkdtemp()

          __UpperCamelCase			=    SpeechTaTokenizer(snake_case					)
          tokenizer.save_pretrained(self.tmpdirname					)

          __UpperCamelCase			=    {
              '''feature_size''': 1,
              '''padding_value''': 0.0,
              '''sampling_rate''': 16000,
              '''do_normalize''': False,
              '''num_mel_bins''': 80,
              '''hop_length''': 16,
              '''win_length''': 64,
              '''win_function''': '''hann_window''',
              '''fmin''': 80,
              '''fmax''': 7600,
              '''mel_floor''': 1E-10,
              '''reduction_factor''': 2,
              '''return_attention_mask''': True,
          }

          __UpperCamelCase			=    os.path.join(self.tmpdirname			,							snake_case					)
          with open(self.feature_extraction_file			,							'''w'''			,							encoding='''utf-8'''					) as fp:
              fp.write(json.dumps(snake_case					) + '''\n'''					)







      def    snake_case						(  self       :			Union[str, Any]			,							**snake_case       :			List[Any]					):
          return SpeechTaTokenizer.from_pretrained(self.tmpdirname			,							**snake_case					)







      def    snake_case						(  self       :			List[Any]			,							**snake_case       :			List[str]					):
          return SpeechTaFeatureExtractor.from_pretrained(self.tmpdirname			,							**snake_case					)







      def    snake_case						(  self       :			Union[str, Any]					):
          shutil.rmtree(self.tmpdirname					)







      def    snake_case						(  self       :			List[str]					):
          __UpperCamelCase			=    self.get_tokenizer()
          __UpperCamelCase			=    self.get_feature_extractor()

          __UpperCamelCase			=    SpeechTaProcessor(tokenizer=snake_case			,							feature_extractor=snake_case					)

          processor.save_pretrained(self.tmpdirname					)
          __UpperCamelCase			=    SpeechTaProcessor.from_pretrained(self.tmpdirname					)

          self.assertEqual(processor.tokenizer.get_vocab()			,							tokenizer.get_vocab()					)
          self.assertIsInstance(processor.tokenizer			,							snake_case					)

          self.assertEqual(processor.feature_extractor.to_json_string()			,							feature_extractor.to_json_string()					)
          self.assertIsInstance(processor.feature_extractor			,							snake_case					)







      def    snake_case						(  self       :			Tuple					):
          __UpperCamelCase			=    SpeechTaProcessor(tokenizer=self.get_tokenizer()			,							feature_extractor=self.get_feature_extractor()					)
          processor.save_pretrained(self.tmpdirname					)

          __UpperCamelCase			=    self.get_tokenizer(bos_token='''(BOS)'''			,							eos_token='''(EOS)'''					)
          __UpperCamelCase			=    self.get_feature_extractor(do_normalize=snake_case			,							padding_value=1.0					)

          __UpperCamelCase			=    SpeechTaProcessor.from_pretrained(
              self.tmpdirname			,							bos_token='''(BOS)'''			,							eos_token='''(EOS)'''			,							do_normalize=snake_case			,							padding_value=1.0					)

          self.assertEqual(processor.tokenizer.get_vocab()			,							tokenizer_add_kwargs.get_vocab()					)
          self.assertIsInstance(processor.tokenizer			,							snake_case					)

          self.assertEqual(processor.feature_extractor.to_json_string()			,							feature_extractor_add_kwargs.to_json_string()					)
          self.assertIsInstance(processor.feature_extractor			,							snake_case					)







      def    snake_case						(  self       :			List[str]					):
          __UpperCamelCase			=    self.get_feature_extractor()
          __UpperCamelCase			=    self.get_tokenizer()

          __UpperCamelCase			=    SpeechTaProcessor(tokenizer=snake_case			,							feature_extractor=snake_case					)

          __UpperCamelCase			=    floats_list((3, 1000)					)

          __UpperCamelCase			=    feature_extractor(audio=snake_case			,							return_tensors='''np'''					)
          __UpperCamelCase			=    processor(audio=snake_case			,							return_tensors='''np'''					)

          for key in input_feat_extract.keys():
              self.assertAlmostEqual(input_feat_extract[key].sum()			,							input_processor[key].sum()			,							delta=1E-2					)







      def    snake_case						(  self       :			List[Any]					):
          __UpperCamelCase			=    self.get_feature_extractor()
          __UpperCamelCase			=    self.get_tokenizer()

          __UpperCamelCase			=    SpeechTaProcessor(tokenizer=snake_case			,							feature_extractor=snake_case					)

          __UpperCamelCase			=    floats_list((3, 1000)					)

          __UpperCamelCase			=    feature_extractor(audio_target=snake_case			,							return_tensors='''np'''					)
          __UpperCamelCase			=    processor(audio_target=snake_case			,							return_tensors='''np'''					)

          for key in input_feat_extract.keys():
              self.assertAlmostEqual(input_feat_extract[key].sum()			,							input_processor[key].sum()			,							delta=1E-2					)







      def    snake_case						(  self       :			Union[str, Any]					):
          __UpperCamelCase			=    self.get_feature_extractor()
          __UpperCamelCase			=    self.get_tokenizer()

          __UpperCamelCase			=    SpeechTaProcessor(tokenizer=snake_case			,							feature_extractor=snake_case					)

          __UpperCamelCase			=    '''This is a test string'''

          __UpperCamelCase			=    processor(text=snake_case					)
          __UpperCamelCase			=    tokenizer(snake_case					)

          for key in encoded_tok.keys():
              self.assertListEqual(encoded_tok[key]			,							encoded_processor[key]					)







      def    snake_case						(  self       :			str					):
          __UpperCamelCase			=    self.get_feature_extractor()
          __UpperCamelCase			=    self.get_tokenizer()

          __UpperCamelCase			=    SpeechTaProcessor(tokenizer=snake_case			,							feature_extractor=snake_case					)

          __UpperCamelCase			=    '''This is a test string'''

          __UpperCamelCase			=    processor(text_target=snake_case					)
          __UpperCamelCase			=    tokenizer(snake_case					)

          for key in encoded_tok.keys():
              self.assertListEqual(encoded_tok[key]			,							encoded_processor[key]					)







      def    snake_case						(  self       :			str					):
          __UpperCamelCase			=    self.get_feature_extractor()
          __UpperCamelCase			=    self.get_tokenizer()

          __UpperCamelCase			=    SpeechTaProcessor(tokenizer=snake_case			,							feature_extractor=snake_case					)

          __UpperCamelCase			=    [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

          __UpperCamelCase			=    processor.batch_decode(snake_case					)
          __UpperCamelCase			=    tokenizer.batch_decode(snake_case					)

          self.assertListEqual(snake_case			,							snake_case					)







      def    snake_case						(  self       :			Optional[int]					):
          __UpperCamelCase			=    self.get_feature_extractor()
          __UpperCamelCase			=    self.get_tokenizer()

          __UpperCamelCase			=    SpeechTaProcessor(tokenizer=snake_case			,							feature_extractor=snake_case					)

          self.assertListEqual(
              processor.model_input_names			,							feature_extractor.model_input_names			,							msg='''`processor` and `feature_extractor` model input names do not match'''			,							)






