





import unittest

from transformers import FunnelConfig, FunnelTokenizer, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch

  from transformers import (
      MODEL_FOR_PRETRAINING_MAPPING,
      FunnelBaseModel,
      FunnelForMaskedLM,
      FunnelForMultipleChoice,
      FunnelForPreTraining,
      FunnelForQuestionAnswering,
      FunnelForSequenceClassification,
      FunnelForTokenClassification,
      FunnelModel,
  )



class 				__snake_case  :


    '''simple docstring'''







    def __init__(					self      ,							A_      ,							A_=13      ,							A_=7      ,							A_=True      ,							A_=True      ,							A_=True      ,							A_=True      ,							A_=99      ,							A_=[1, 1, 2]      ,							A_=1      ,							A_=32      ,							A_=4      ,							A_=8      ,							A_=37      ,							A_="gelu_new"      ,							A_=0.1      ,							A_=0.1      ,							A_=0.0      ,							A_=5_12      ,							A_=3      ,							A_=0.02      ,							A_=3      ,							A_=4      ,							A_=None      ,							A_=False      ,							):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= parent
        SCREAMING_SNAKE_CASE__       							= batch_size
        SCREAMING_SNAKE_CASE__       							= seq_length
        SCREAMING_SNAKE_CASE__       							= is_training
        SCREAMING_SNAKE_CASE__       							= use_input_mask
        SCREAMING_SNAKE_CASE__       							= use_token_type_ids
        SCREAMING_SNAKE_CASE__       							= use_labels
        SCREAMING_SNAKE_CASE__       							= vocab_size
        SCREAMING_SNAKE_CASE__       							= block_sizes
        SCREAMING_SNAKE_CASE__       							= num_decoder_layers
        SCREAMING_SNAKE_CASE__       							= d_model
        SCREAMING_SNAKE_CASE__       							= n_head
        SCREAMING_SNAKE_CASE__       							= d_head
        SCREAMING_SNAKE_CASE__       							= d_inner
        SCREAMING_SNAKE_CASE__       							= hidden_act
        SCREAMING_SNAKE_CASE__       							= hidden_dropout
        SCREAMING_SNAKE_CASE__       							= attention_dropout
        SCREAMING_SNAKE_CASE__       							= activation_dropout
        SCREAMING_SNAKE_CASE__       							= max_position_embeddings
        SCREAMING_SNAKE_CASE__       							= type_vocab_size
        SCREAMING_SNAKE_CASE__       							= 2
        SCREAMING_SNAKE_CASE__       							= num_labels
        SCREAMING_SNAKE_CASE__       							= num_choices
        SCREAMING_SNAKE_CASE__       							= scope
        SCREAMING_SNAKE_CASE__       							= initializer_std

        # Used in the tests to check the size of the first attention layer
        SCREAMING_SNAKE_CASE__       							= n_head
        # Used in the tests to check the size of the first hidden state
        SCREAMING_SNAKE_CASE__       							= self.d_model
        # Used in the tests to check the number of output hidden states/attentions
        SCREAMING_SNAKE_CASE__       							= sum(self.block_sizes    ) + (0 if base else self.num_decoder_layers)
        # FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with
        # the last hidden state of the first block (which is the first hidden state of the decoder).
        if not base:
            SCREAMING_SNAKE_CASE__       							= self.num_hidden_layers + 2



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= ids_tensor([self.batch_size, self.seq_length]      ,							self.vocab_size    )

        SCREAMING_SNAKE_CASE__       							= None
        if self.use_input_mask:
            SCREAMING_SNAKE_CASE__       							= ids_tensor([self.batch_size, self.seq_length]      ,							vocab_size=2    )

        SCREAMING_SNAKE_CASE__       							= None
        if self.use_token_type_ids:
            SCREAMING_SNAKE_CASE__       							= ids_tensor([self.batch_size, self.seq_length]      ,							self.type_vocab_size    )

        SCREAMING_SNAKE_CASE__       							= None
        SCREAMING_SNAKE_CASE__       							= None
        SCREAMING_SNAKE_CASE__       							= None
        if self.use_labels:
            SCREAMING_SNAKE_CASE__       							= ids_tensor([self.batch_size]      ,							self.type_sequence_label_size    )
            SCREAMING_SNAKE_CASE__       							= ids_tensor([self.batch_size, self.seq_length]      ,							self.num_labels    )
            SCREAMING_SNAKE_CASE__       							= ids_tensor([self.batch_size]      ,							self.num_choices    )
            SCREAMING_SNAKE_CASE__       							= ids_tensor([self.batch_size, self.seq_length]      ,							1    )

        SCREAMING_SNAKE_CASE__       							= self.get_config()

        return (
            config,
            input_ids,
            token_type_ids,
            input_mask,
            sequence_labels,
            token_labels,
            choice_labels,
            fake_token_labels,
        )



    def    lowercase_				(					self    ):





        '''simple docstring'''

        return FunnelConfig(
            vocab_size=self.vocab_size      ,							block_sizes=self.block_sizes      ,							num_decoder_layers=self.num_decoder_layers      ,							d_model=self.d_model      ,							n_head=self.n_head      ,							d_head=self.d_head      ,							d_inner=self.d_inner      ,							hidden_act=self.hidden_act      ,							hidden_dropout=self.hidden_dropout      ,							attention_dropout=self.attention_dropout      ,							activation_dropout=self.activation_dropout      ,							max_position_embeddings=self.max_position_embeddings      ,							type_vocab_size=self.type_vocab_size      ,							initializer_std=self.initializer_std      ,							)



    def    lowercase_				(					self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= FunnelModel(config=A_    )
        model.to(A_    )
        model.eval()
        SCREAMING_SNAKE_CASE__       							= model(A_      ,							attention_mask=A_      ,							token_type_ids=A_    )
        SCREAMING_SNAKE_CASE__       							= model(A_      ,							token_type_ids=A_    )
        SCREAMING_SNAKE_CASE__       							= model(A_    )
        self.parent.assertEqual(result.last_hidden_state.shape      ,							(self.batch_size, self.seq_length, self.d_model)    )

        SCREAMING_SNAKE_CASE__       							= False
        SCREAMING_SNAKE_CASE__       							= model(A_    )
        self.parent.assertEqual(result.last_hidden_state.shape      ,							(self.batch_size, self.seq_length, self.d_model)    )

        SCREAMING_SNAKE_CASE__       							= False
        SCREAMING_SNAKE_CASE__       							= model(A_    )
        self.parent.assertEqual(result.last_hidden_state.shape      ,							(self.batch_size, self.seq_length, self.d_model)    )



    def    lowercase_				(					self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= FunnelBaseModel(config=A_    )
        model.to(A_    )
        model.eval()
        SCREAMING_SNAKE_CASE__       							= model(A_      ,							attention_mask=A_      ,							token_type_ids=A_    )
        SCREAMING_SNAKE_CASE__       							= model(A_      ,							token_type_ids=A_    )
        SCREAMING_SNAKE_CASE__       							= model(A_    )
        self.parent.assertEqual(result.last_hidden_state.shape      ,							(self.batch_size, 2, self.d_model)    )

        SCREAMING_SNAKE_CASE__       							= False
        SCREAMING_SNAKE_CASE__       							= model(A_    )
        self.parent.assertEqual(result.last_hidden_state.shape      ,							(self.batch_size, 3, self.d_model)    )

        SCREAMING_SNAKE_CASE__       							= False
        SCREAMING_SNAKE_CASE__       							= model(A_    )
        self.parent.assertEqual(result.last_hidden_state.shape      ,							(self.batch_size, 2, self.d_model)    )



    def    lowercase_				(					self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.num_labels
        SCREAMING_SNAKE_CASE__       							= FunnelForPreTraining(config=A_    )
        model.to(A_    )
        model.eval()
        SCREAMING_SNAKE_CASE__       							= model(A_      ,							attention_mask=A_      ,							token_type_ids=A_      ,							labels=A_    )
        self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.seq_length)    )



    def    lowercase_				(					self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= FunnelForMaskedLM(config=A_    )
        model.to(A_    )
        model.eval()
        SCREAMING_SNAKE_CASE__       							= model(A_      ,							attention_mask=A_      ,							token_type_ids=A_      ,							labels=A_    )
        self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.seq_length, self.vocab_size)    )



    def    lowercase_				(					self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.num_labels
        SCREAMING_SNAKE_CASE__       							= FunnelForSequenceClassification(A_    )
        model.to(A_    )
        model.eval()
        SCREAMING_SNAKE_CASE__       							= model(A_      ,							attention_mask=A_      ,							token_type_ids=A_      ,							labels=A_    )
        self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.num_labels)    )



    def    lowercase_				(					self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.num_choices
        SCREAMING_SNAKE_CASE__       							= FunnelForMultipleChoice(config=A_    )
        model.to(A_    )
        model.eval()
        SCREAMING_SNAKE_CASE__       							= input_ids.unsqueeze(1    ).expand(-1      ,							self.num_choices      ,							-1    ).contiguous()
        SCREAMING_SNAKE_CASE__       							= token_type_ids.unsqueeze(1    ).expand(-1      ,							self.num_choices      ,							-1    ).contiguous()
        SCREAMING_SNAKE_CASE__       							= input_mask.unsqueeze(1    ).expand(-1      ,							self.num_choices      ,							-1    ).contiguous()
        SCREAMING_SNAKE_CASE__       							= model(
            A_      ,							attention_mask=A_      ,							token_type_ids=A_      ,							labels=A_      ,							)
        self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.num_choices)    )



    def    lowercase_				(					self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.num_labels
        SCREAMING_SNAKE_CASE__       							= FunnelForTokenClassification(config=A_    )
        model.to(A_    )
        model.eval()
        SCREAMING_SNAKE_CASE__       							= model(A_      ,							attention_mask=A_      ,							token_type_ids=A_      ,							labels=A_    )
        self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.seq_length, self.num_labels)    )



    def    lowercase_				(					self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= FunnelForQuestionAnswering(config=A_    )
        model.to(A_    )
        model.eval()
        SCREAMING_SNAKE_CASE__       							= model(
            A_      ,							attention_mask=A_      ,							token_type_ids=A_      ,							start_positions=A_      ,							end_positions=A_      ,							)
        self.parent.assertEqual(result.start_logits.shape      ,							(self.batch_size, self.seq_length)    )
        self.parent.assertEqual(result.end_logits.shape      ,							(self.batch_size, self.seq_length)    )




    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.prepare_config_and_inputs()
        (
            (
            SCREAMING_SNAKE_CASE__
        )							,		(
            SCREAMING_SNAKE_CASE__
        )							,		(
            SCREAMING_SNAKE_CASE__
        )							,		(
            SCREAMING_SNAKE_CASE__
        )							,		(
            SCREAMING_SNAKE_CASE__
        )							,		(
            SCREAMING_SNAKE_CASE__
        )							,		(
            SCREAMING_SNAKE_CASE__
        )							,		(
            SCREAMING_SNAKE_CASE__
        )							,		
        )       							= config_and_inputs
        SCREAMING_SNAKE_CASE__       							= {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
        return config, inputs_dict





@require_torch
class 				__snake_case  (     __SCREAMING_SNAKE_CASE ,    __SCREAMING_SNAKE_CASE ,    unittest.TestCase						):


    '''simple docstring'''







    lowerCamelCase__						:		str							=  False
    lowerCamelCase__						:		int							=  False
    lowerCamelCase__						:		Dict							=  (
        (
            FunnelModel,
            FunnelForMaskedLM,
            FunnelForPreTraining,
            FunnelForQuestionAnswering,
            FunnelForTokenClassification,
        )
        if is_torch_available()
        else ()
    )
    lowerCamelCase__						:		int							=  (
        {
            """feature-extraction""": (FunnelBaseModel, FunnelModel),
            """fill-mask""": FunnelForMaskedLM,
            """question-answering""": FunnelForQuestionAnswering,
            """text-classification""": FunnelForSequenceClassification,
            """token-classification""": FunnelForTokenClassification,
            """zero-shot""": FunnelForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )



    def    lowercase_				(					self      ,							A_      ,							A_      ,							A_=False    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= super()._prepare_for_class(A_      ,							A_      ,							return_labels=A_    )

        if return_labels:
            if model_class in get_values(A_    ):
                SCREAMING_SNAKE_CASE__       							= torch.zeros(
                    (self.model_tester.batch_size, self.model_tester.seq_length)      ,							dtype=torch.long      ,							device=A_    )
        return inputs_dict



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= FunnelModelTester(self    )
        SCREAMING_SNAKE_CASE__       							= ConfigTester(self      ,							config_class=A_    )



    def    lowercase_				(					self    ):





        '''simple docstring'''

        self.config_tester.run_common_tests()



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*A_    )



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_pretraining(*A_    )



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*A_    )



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*A_    )



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*A_    )




    def    lowercase_				(					self      ,							A_    ):





        '''simple docstring'''

        if hasattr(A_      ,							'''weight'''    ) and module.weight is not None:
            module.weight.data.fill_(3    )
        if hasattr(A_      ,							'''bias'''    ) and module.bias is not None:
            module.bias.data.fill_(3    )

        for param in ["r_w_bias", "r_r_bias", "r_kernel", "r_s_bias", "seg_embed"]:
            if hasattr(A_      ,							A_    ) and getattr(A_      ,							A_    ) is not None:
                SCREAMING_SNAKE_CASE__       							= getattr(A_      ,							A_    )
                weight.data.fill_(3    )





@require_torch
class 				__snake_case  (     __SCREAMING_SNAKE_CASE ,    unittest.TestCase						):


    '''simple docstring'''







    lowerCamelCase__						:		Optional[int]							=  False
    lowerCamelCase__						:		Any							=  False
    lowerCamelCase__						:		List[str]							=  (
        (FunnelBaseModel, FunnelForMultipleChoice, FunnelForSequenceClassification) if is_torch_available() else ()
    )



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= FunnelModelTester(self      ,							base=A_    )
        SCREAMING_SNAKE_CASE__       							= ConfigTester(self      ,							config_class=A_    )



    def    lowercase_				(					self    ):





        '''simple docstring'''

        self.config_tester.run_common_tests()



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_base_model(*A_    )



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*A_    )



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_multiple_choice(*A_    )



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__       							= self.model_tester.prepare_config_and_inputs_for_common()
        SCREAMING_SNAKE_CASE__       							= True

        for model_class in self.all_model_classes:
            if model_class.__name__ == "FunnelBaseModel":
                continue
            SCREAMING_SNAKE_CASE__       							= model_class(A_    )
            model.to(A_    )
            model.train()
            SCREAMING_SNAKE_CASE__       							= self._prepare_for_class(A_      ,							A_      ,							return_labels=A_    )
            SCREAMING_SNAKE_CASE__       							= model(**A_    ).loss
            loss.backward()




    def    lowercase_				(					self      ,							A_    ):





        '''simple docstring'''

        if hasattr(A_      ,							'''weight'''    ) and module.weight is not None:
            module.weight.data.fill_(3    )
        if hasattr(A_      ,							'''bias'''    ) and module.bias is not None:
            module.bias.data.fill_(3    )

        for param in ["r_w_bias", "r_r_bias", "r_kernel", "r_s_bias", "seg_embed"]:
            if hasattr(A_      ,							A_    ) and getattr(A_      ,							A_    ) is not None:
                SCREAMING_SNAKE_CASE__       							= getattr(A_      ,							A_    )
                weight.data.fill_(3    )





@require_torch
@require_sentencepiece
@require_tokenizers
class 				__snake_case  (     unittest.TestCase						):


    '''simple docstring'''







    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= 13
        SCREAMING_SNAKE_CASE__       							= 7
        SCREAMING_SNAKE_CASE__       							= torch.arange(0      ,							batch_size * sequence_length    ).long().reshape(A_      ,							A_    )
        SCREAMING_SNAKE_CASE__       							= [0, 1, 2, 3, 4, 5, 6, 4, 1, 3, 5, 0, 1]
        SCREAMING_SNAKE_CASE__       							= torch.tensor([[2] + [0] * a + [1] * (sequence_length - a - 1) for a in lengths]    )

        SCREAMING_SNAKE_CASE__       							= FunnelModel.from_pretrained('''sgugger/funnel-random-tiny'''    )
        SCREAMING_SNAKE_CASE__       							= model(A_      ,							token_type_ids=A_    )[0].abs()

        SCREAMING_SNAKE_CASE__       							= torch.tensor(2344.8352    )
        SCREAMING_SNAKE_CASE__       							= torch.tensor(0.8052    )
        self.assertTrue(torch.allclose(output.sum()      ,							A_      ,							atol=1E-4    )    )
        self.assertTrue(torch.allclose(output.mean()      ,							A_      ,							atol=1E-4    )    )

        SCREAMING_SNAKE_CASE__       							= torch.tensor([[1] * 7, [1] * 4 + [0] * 3] * 6 + [[0, 1, 1, 0, 0, 1, 1]]    )
        SCREAMING_SNAKE_CASE__       							= model(A_      ,							attention_mask=A_      ,							token_type_ids=A_    )[0].abs()

        SCREAMING_SNAKE_CASE__       							= torch.tensor(2343.8425    )
        SCREAMING_SNAKE_CASE__       							= torch.tensor(0.8049    )
        self.assertTrue(torch.allclose(output.sum()      ,							A_      ,							atol=1E-4    )    )
        self.assertTrue(torch.allclose(output.mean()      ,							A_      ,							atol=1E-4    )    )




    @slow
    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= FunnelTokenizer.from_pretrained('''huggingface/funnel-small'''    )
        SCREAMING_SNAKE_CASE__       							= FunnelModel.from_pretrained('''huggingface/funnel-small'''    )
        SCREAMING_SNAKE_CASE__       							= tokenizer('''Hello! I am the Funnel Transformer model.'''      ,							return_tensors='''pt'''    )
        SCREAMING_SNAKE_CASE__       							= model(**A_    )[0]

        SCREAMING_SNAKE_CASE__       							= torch.tensor(235.7246    )
        SCREAMING_SNAKE_CASE__       							= torch.tensor(0.0256    )
        self.assertTrue(torch.allclose(output.sum()      ,							A_      ,							atol=1E-4    )    )
        self.assertTrue(torch.allclose(output.mean()      ,							A_      ,							atol=1E-4    )    )







import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel


def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_  )							->	str:
    SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__       							= pt_weights[f'''{prefix}.in_proj_weight'''].chunk(3   ,       dim=0  )
    SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__       							= pt_weights[f'''{prefix}.in_proj_bias'''].chunk(3   ,       dim=0  )

    SCREAMING_SNAKE_CASE__       							= pt_weights[f'''{prefix}.out_proj.weight''']
    SCREAMING_SNAKE_CASE__       							= pt_weights[f'''{prefix}.out_proj.bias''']

    SCREAMING_SNAKE_CASE__       							= q_proj
    SCREAMING_SNAKE_CASE__       							= q_proj_bias

    SCREAMING_SNAKE_CASE__       							= k_proj
    SCREAMING_SNAKE_CASE__       							= k_proj_bias

    SCREAMING_SNAKE_CASE__       							= v_proj
    SCREAMING_SNAKE_CASE__       							= v_proj_bias

    SCREAMING_SNAKE_CASE__       							= out_proj_weights
    SCREAMING_SNAKE_CASE__       							= out_proj_bias


def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_  )							->	Dict:
    copy_linear(hf_mlp.fca   ,       lowerCAmelCase_   ,       f'''{prefix}.c_fc'''  )
    copy_linear(hf_mlp.fca   ,       lowerCAmelCase_   ,       f'''{prefix}.c_proj'''  )


def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_  )							->	str:
    SCREAMING_SNAKE_CASE__       							= pt_weights[f'''{prefix}.weight'''].data
    SCREAMING_SNAKE_CASE__       							= pt_weights[f'''{prefix}.bias'''].data


def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_  )							->	Dict:
    # copy layer norms
    copy_linear(hf_layer.layer_norma   ,       lowerCAmelCase_   ,       f'''{prefix}.ln_1'''  )
    copy_linear(hf_layer.layer_norma   ,       lowerCAmelCase_   ,       f'''{prefix}.ln_2'''  )

    # copy MLP
    copy_mlp(hf_layer.mlp   ,       lowerCAmelCase_   ,       f'''{prefix}.mlp'''  )

    # copy attn
    copy_attn_layer(hf_layer.self_attn   ,       lowerCAmelCase_   ,       f'''{prefix}.attn'''  )


def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_  )							->	Optional[int]:
    for layer_id, hf_layer in enumerate(lowerCAmelCase_  ):
        copy_layer(lowerCAmelCase_   ,       lowerCAmelCase_   ,       f'''{prefix}.{layer_id}'''  )


def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_  )							->	Any:
    # copy projection
    SCREAMING_SNAKE_CASE__       							= pt_weights['''text_projection'''].data.T

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


def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_  )							->	List[str]:
    # copy projection
    SCREAMING_SNAKE_CASE__       							= pt_weights['''visual.proj'''].data.T

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

    # copy embeddings
    SCREAMING_SNAKE_CASE__       							= pt_weights['''visual.conv1.weight'''].data
    SCREAMING_SNAKE_CASE__       							= pt_weights['''visual.class_embedding'''].data
    SCREAMING_SNAKE_CASE__       							= pt_weights['''visual.positional_embedding'''].data

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


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

    SCREAMING_SNAKE_CASE__       							= ChineseCLIPModel(lowerCAmelCase_  ).eval()

    SCREAMING_SNAKE_CASE__       							= torch.load(lowerCAmelCase_   ,       map_location='''cpu'''  )['''state_dict''']
    SCREAMING_SNAKE_CASE__       							= {(name[7:] if name.startswith('''module.'''  ) else name): value for name, value in pt_weights.items()}

    copy_text_model_and_projection(lowerCAmelCase_   ,       lowerCAmelCase_  )
    copy_vision_model_and_projection(lowerCAmelCase_   ,       lowerCAmelCase_  )
    SCREAMING_SNAKE_CASE__       							= pt_weights['''logit_scale'''].data

    hf_model.save_pretrained(lowerCAmelCase_  )


if __name__ == "__main__":
  _A				:							Union[str, Any]							= 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				:							Union[str, Any]							= parser.parse_args()

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