



"""simple docstring"""


from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
				import tensorflow as tf

				from transformers import (
				    TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFXLMForMultipleChoice,
				    TFXLMForQuestionAnsweringSimple,
				    TFXLMForSequenceClassification,
				    TFXLMForTokenClassification,
				    TFXLMModel,
				    TFXLMWithLMHeadModel,
				    XLMConfig,
				)







class  A_     :



						"""simple docstring"""






						def __init__(   self					:Union[str, Any]  ,    lowercase_					:Union[str, Any]  ,    )							->     int:
												UpperCAmelCase			 =					parent
												UpperCAmelCase			 =					13
												UpperCAmelCase			 =					7
												UpperCAmelCase			 =					True
												UpperCAmelCase			 =					True
												UpperCAmelCase			 =					True
												UpperCAmelCase			 =					True
												UpperCAmelCase			 =					True
												UpperCAmelCase			 =					False
												UpperCAmelCase			 =					False
												UpperCAmelCase			 =					False
												UpperCAmelCase			 =					2
												UpperCAmelCase			 =					99
												UpperCAmelCase			 =					0
												UpperCAmelCase			 =					32
												UpperCAmelCase			 =					2
												UpperCAmelCase			 =					4
												UpperCAmelCase			 =					0.1
												UpperCAmelCase			 =					0.1
												UpperCAmelCase			 =					5_12
												UpperCAmelCase			 =					16
												UpperCAmelCase			 =					2
												UpperCAmelCase			 =					0.02
												UpperCAmelCase			 =					3
												UpperCAmelCase			 =					4
												UpperCAmelCase			 =					'last'
												UpperCAmelCase			 =					True
												UpperCAmelCase			 =					None
												UpperCAmelCase			 =					0






						def       UpperCAmelCase__	(   self					:str   )							->     str:
												UpperCAmelCase			 =					ids_tensor([self.batch_size, self.seq_length]  ,    self.vocab_size   )
												UpperCAmelCase			 =					random_attention_mask([self.batch_size, self.seq_length]  ,    dtype=tf.floataa   )

												UpperCAmelCase			 =					None
												if self.use_input_lengths:
																		UpperCAmelCase			 =					(
																		    ids_tensor([self.batch_size]  ,    vocab_size=2   ) + self.seq_length - 2
																		)  # small variation of seq_length

												UpperCAmelCase			 =					None
												if self.use_token_type_ids:
																		UpperCAmelCase			 =					ids_tensor([self.batch_size, self.seq_length]  ,    self.n_langs   )

												UpperCAmelCase			 =					None
												UpperCAmelCase			 =					None
												UpperCAmelCase			 =					None
												if self.use_labels:
																		UpperCAmelCase			 =					ids_tensor([self.batch_size]  ,    self.type_sequence_label_size   )
																		UpperCAmelCase			 =					ids_tensor([self.batch_size, self.seq_length]  ,    self.num_labels   )
																		UpperCAmelCase			 =					ids_tensor([self.batch_size]  ,    2  ,    dtype=tf.floataa   )
																		UpperCAmelCase			 =					ids_tensor([self.batch_size]  ,    self.num_choices   )

												UpperCAmelCase			 =					XLMConfig(
												    vocab_size=self.vocab_size  ,    n_special=self.n_special  ,    emb_dim=self.hidden_size  ,    n_layers=self.num_hidden_layers  ,    n_heads=self.num_attention_heads  ,    dropout=self.hidden_dropout_prob  ,    attention_dropout=self.attention_probs_dropout_prob  ,    gelu_activation=self.gelu_activation  ,    sinusoidal_embeddings=self.sinusoidal_embeddings  ,    asm=self.asm  ,    causal=self.causal  ,    n_langs=self.n_langs  ,    max_position_embeddings=self.max_position_embeddings  ,    initializer_range=self.initializer_range  ,    summary_type=self.summary_type  ,    use_proj=self.use_proj  ,    bos_token_id=self.bos_token_id  ,    )

												return (
												    config,
												    input_ids,
												    token_type_ids,
												    input_lengths,
												    sequence_labels,
												    token_labels,
												    is_impossible_labels,
												    choice_labels,
												    input_mask,
												)






						def       UpperCAmelCase__	(   self					:Optional[int]  ,    lowercase_					:Tuple  ,    lowercase_					:Optional[Any]  ,    lowercase_					:int  ,    lowercase_					:Tuple  ,    lowercase_					:str  ,    lowercase_					:int  ,    lowercase_					:Dict  ,    lowercase_					:Any  ,    lowercase_					:str  ,    )							->     str:
												UpperCAmelCase			 =					TFXLMModel(config=lowercase_   )
												UpperCAmelCase			 =					{'input_ids': input_ids, 'lengths': input_lengths, 'langs': token_type_ids}
												UpperCAmelCase			 =					model(lowercase_   )

												UpperCAmelCase			 =					[input_ids, input_mask]
												UpperCAmelCase			 =					model(lowercase_   )
												self.parent.assertEqual(result.last_hidden_state.shape  ,    (self.batch_size, self.seq_length, self.hidden_size)   )






						def       UpperCAmelCase__	(   self					:str  ,    lowercase_					:Optional[int]  ,    lowercase_					:int  ,    lowercase_					:Optional[int]  ,    lowercase_					:Optional[int]  ,    lowercase_					:Optional[Any]  ,    lowercase_					:Union[str, Any]  ,    lowercase_					:Union[str, Any]  ,    lowercase_					:Optional[Any]  ,    lowercase_					:int  ,    )							->     Optional[Any]:
												UpperCAmelCase			 =					TFXLMWithLMHeadModel(lowercase_   )

												UpperCAmelCase			 =					{'input_ids': input_ids, 'lengths': input_lengths, 'langs': token_type_ids}
												UpperCAmelCase			 =					model(lowercase_   )

												UpperCAmelCase			 =					outputs

												self.parent.assertEqual(result.logits.shape  ,    (self.batch_size, self.seq_length, self.vocab_size)   )






						def       UpperCAmelCase__	(   self					:Optional[int]  ,    lowercase_					:List[Any]  ,    lowercase_					:str  ,    lowercase_					:Union[str, Any]  ,    lowercase_					:List[Any]  ,    lowercase_					:Tuple  ,    lowercase_					:Optional[int]  ,    lowercase_					:int  ,    lowercase_					:Dict  ,    lowercase_					:Dict  ,    )							->     Dict:
												UpperCAmelCase			 =					TFXLMForQuestionAnsweringSimple(lowercase_   )

												UpperCAmelCase			 =					{'input_ids': input_ids, 'lengths': input_lengths}

												UpperCAmelCase			 =					model(lowercase_   )

												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       UpperCAmelCase__	(   self					:str  ,    lowercase_					:int  ,    lowercase_					:Any  ,    lowercase_					:List[Any]  ,    lowercase_					:List[str]  ,    lowercase_					:Any  ,    lowercase_					:Tuple  ,    lowercase_					:Optional[Any]  ,    lowercase_					:Union[str, Any]  ,    lowercase_					:Dict  ,    )							->     Dict:
												UpperCAmelCase			 =					TFXLMForSequenceClassification(lowercase_   )

												UpperCAmelCase			 =					{'input_ids': input_ids, 'lengths': input_lengths}

												UpperCAmelCase			 =					model(lowercase_   )

												self.parent.assertEqual(result.logits.shape  ,    (self.batch_size, self.type_sequence_label_size)   )






						def       UpperCAmelCase__	(   self					:List[str]  ,    lowercase_					:Tuple  ,    lowercase_					:str  ,    lowercase_					:int  ,    lowercase_					:List[str]  ,    lowercase_					:Any  ,    lowercase_					:Optional[int]  ,    lowercase_					:List[Any]  ,    lowercase_					:int  ,    lowercase_					:Union[str, Any]  ,    )							->     Union[str, Any]:
												UpperCAmelCase			 =					self.num_labels
												UpperCAmelCase			 =					TFXLMForTokenClassification(config=lowercase_   )
												UpperCAmelCase			 =					{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
												UpperCAmelCase			 =					model(lowercase_   )
												self.parent.assertEqual(result.logits.shape  ,    (self.batch_size, self.seq_length, self.num_labels)   )






						def       UpperCAmelCase__	(   self					:int  ,    lowercase_					:Optional[Any]  ,    lowercase_					:Tuple  ,    lowercase_					:str  ,    lowercase_					:Union[str, Any]  ,    lowercase_					:str  ,    lowercase_					:Optional[int]  ,    lowercase_					:Dict  ,    lowercase_					:Union[str, Any]  ,    lowercase_					:Dict  ,    )							->     Union[str, Any]:
												UpperCAmelCase			 =					self.num_choices
												UpperCAmelCase			 =					TFXLMForMultipleChoice(config=lowercase_   )
												UpperCAmelCase			 =					tf.tile(tf.expand_dims(lowercase_  ,    1   )  ,    (1, self.num_choices, 1)   )
												UpperCAmelCase			 =					tf.tile(tf.expand_dims(lowercase_  ,    1   )  ,    (1, self.num_choices, 1)   )
												UpperCAmelCase			 =					tf.tile(tf.expand_dims(lowercase_  ,    1   )  ,    (1, self.num_choices, 1)   )
												UpperCAmelCase			 =					{
												    'input_ids': multiple_choice_inputs_ids,
												    'attention_mask': multiple_choice_input_mask,
												    'token_type_ids': multiple_choice_token_type_ids,
												}
												UpperCAmelCase			 =					model(lowercase_   )
												self.parent.assertEqual(result.logits.shape  ,    (self.batch_size, self.num_choices)   )






						def       UpperCAmelCase__	(   self					:Dict   )							->     Dict:
												UpperCAmelCase			 =					self.prepare_config_and_inputs()
												(
												    (
												    UpperCAmelCase
												) ,       (
												    UpperCAmelCase
												) ,       (
												    UpperCAmelCase
												) ,       (
												    UpperCAmelCase
												) ,       (
												    UpperCAmelCase
												) ,       (
												    UpperCAmelCase
												) ,       (
												    UpperCAmelCase
												) ,       (
												    UpperCAmelCase
												) ,       (
												    UpperCAmelCase
												) ,       
												)			 =					config_and_inputs
												UpperCAmelCase			 =					{
												    'input_ids': input_ids,
												    'token_type_ids': token_type_ids,
												    'langs': token_type_ids,
												    'lengths': input_lengths,
												}
												return config, inputs_dict







@require_tf
class  A_     (	SCREAMING_SNAKE_CASE_      ,  SCREAMING_SNAKE_CASE_      ,  unittest.TestCase							):



						"""simple docstring"""
						__UpperCamelCase													=				(
						    (
						        TFXLMModel,
						        TFXLMWithLMHeadModel,
						        TFXLMForSequenceClassification,
						        TFXLMForQuestionAnsweringSimple,
						        TFXLMForTokenClassification,
						        TFXLMForMultipleChoice,
						    )
						    if is_tf_available()
						    else ()
						)
						__UpperCamelCase													=				(
						    (TFXLMWithLMHeadModel,) if is_tf_available() else ()
						)  # TODO (PVP): Check other models whether language generation is also applicable
						__UpperCamelCase													=				(
						    {
						        """feature-extraction""": TFXLMModel,
						        """fill-mask""": TFXLMWithLMHeadModel,
						        """question-answering""": TFXLMForQuestionAnsweringSimple,
						        """text-classification""": TFXLMForSequenceClassification,
						        """text-generation""": TFXLMWithLMHeadModel,
						        """token-classification""": TFXLMForTokenClassification,
						        """zero-shot""": TFXLMForSequenceClassification,
						    }
						    if is_tf_available()
						    else {}
						)
						__UpperCamelCase													=				False
						__UpperCamelCase													=				False






						def       UpperCAmelCase__	(   self					:int  ,    lowercase_					:List[str]  ,    lowercase_					:Optional[int]  ,    lowercase_					:List[str]  ,    lowercase_					:Optional[int]  ,    lowercase_					:Dict   )							->     str:
												if (
												    pipeline_test_casse_name == "QAPipelineTests"
												    and tokenizer_name is not None
												    and not tokenizer_name.endswith('Fast'   )
												):
																		# `QAPipelineTests` fails for a few models when the slower tokenizer are used.
																		# (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
																		# TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
																		return True

												return False






						def       UpperCAmelCase__	(   self					:Any   )							->     Optional[Any]:
												UpperCAmelCase			 =					TFXLMModelTester(self   )
												UpperCAmelCase			 =					ConfigTester(self  ,    config_class=lowercase_  ,    emb_dim=37   )






						def       UpperCAmelCase__	(   self					:Any   )							->     Tuple:
												self.config_tester.run_common_tests()






						def       UpperCAmelCase__	(   self					:Union[str, Any]   )							->     int:
												UpperCAmelCase			 =					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_xlm_model(*lowercase_   )






						def       UpperCAmelCase__	(   self					:str   )							->     int:
												UpperCAmelCase			 =					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_xlm_lm_head(*lowercase_   )






						def       UpperCAmelCase__	(   self					:Any   )							->     Optional[Any]:
												UpperCAmelCase			 =					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_xlm_qa(*lowercase_   )






						def       UpperCAmelCase__	(   self					:List[Any]   )							->     List[Any]:
												UpperCAmelCase			 =					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_xlm_sequence_classif(*lowercase_   )






						def       UpperCAmelCase__	(   self					:Any   )							->     List[Any]:
												UpperCAmelCase			 =					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_xlm_for_token_classification(*lowercase_   )






						def       UpperCAmelCase__	(   self					:str   )							->     List[Any]:
												UpperCAmelCase			 =					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_xlm_for_multiple_choice(*lowercase_   )






						@slow
						def       UpperCAmelCase__	(   self					:Any   )							->     List[Any]:
												for model_name in TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		UpperCAmelCase			 =					TFXLMModel.from_pretrained(lowercase_   )
																		self.assertIsNotNone(lowercase_   )







@require_tf
class  A_     (	unittest.TestCase							):



						"""simple docstring"""






						@slow
						def       UpperCAmelCase__	(   self					:Any   )							->     Any:
												UpperCAmelCase			 =					TFXLMWithLMHeadModel.from_pretrained('xlm-mlm-en-2048'   )
												UpperCAmelCase			 =					tf.convert_to_tensor([[14, 4_47]]  ,    dtype=tf.intaa   )  # the president
												UpperCAmelCase			 =					[
												    14,
												    4_47,
												    14,
												    4_47,
												    14,
												    4_47,
												    14,
												    4_47,
												    14,
												    4_47,
												    14,
												    4_47,
												    14,
												    4_47,
												    14,
												    4_47,
												    14,
												    4_47,
												    14,
												    4_47,
												]  # the president the president the president the president the president the president the president the president the president the president
												# TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference
												UpperCAmelCase			 =					model.generate(lowercase_  ,    do_sample=lowercase_   )
												self.assertListEqual(output_ids[0].numpy().tolist()  ,    lowercase_   )






import argparse

import torch

from transformers import ChineseCLIPConfig, ChineseCLIPModel


def SCREAMING_SNAKE_CASE					(  lowercase_:						List[str]		,    lowercase_:						Dict		,    lowercase_:						List[Any]					):
       _UpperCamelCase    ,   _UpperCamelCase    ,   _UpperCamelCase  :							List[Any]	    =	pt_weights[F"{prefix}.in_proj_weight"].chunk(3		,    dim=0					)
       _UpperCamelCase    ,   _UpperCamelCase    ,   _UpperCamelCase  :							Dict	    =	pt_weights[F"{prefix}.in_proj_bias"].chunk(3		,    dim=0					)

       _UpperCamelCase  :							List[str]	    =	pt_weights[F"{prefix}.out_proj.weight"]
       _UpperCamelCase  :							Any	    =	pt_weights[F"{prefix}.out_proj.bias"]

       _UpperCamelCase  :							List[Any]	    =	q_proj
       _UpperCamelCase  :							Union[str, Any]	    =	q_proj_bias

       _UpperCamelCase  :							str	    =	k_proj
       _UpperCamelCase  :							List[str]	    =	k_proj_bias

       _UpperCamelCase  :							Dict	    =	v_proj
       _UpperCamelCase  :							str	    =	v_proj_bias

       _UpperCamelCase  :							List[str]	    =	out_proj_weights
       _UpperCamelCase  :							Any	    =	out_proj_bias


def SCREAMING_SNAKE_CASE					(  lowercase_:						List[str]		,    lowercase_:						List[Any]		,    lowercase_:						Any					):
       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_:						Optional[Any]		,    lowercase_:						List[str]		,    lowercase_:						Any					):
       _UpperCamelCase  :							Dict	    =	pt_weights[F"{prefix}.weight"].data
       _UpperCamelCase  :							List[Any]	    =	pt_weights[F"{prefix}.bias"].data


def SCREAMING_SNAKE_CASE					(  lowercase_:						Any		,    lowercase_:						Optional[int]		,    lowercase_:						Union[str, 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 SCREAMING_SNAKE_CASE					(  lowercase_:						str		,    lowercase_:						str		,    lowercase_:						int					):
       for layer_id, hf_layer in enumerate(lowercase_					):
              copy_layer(lowercase_		,    lowercase_		,    F"{prefix}.{layer_id}"					)


def SCREAMING_SNAKE_CASE					(  lowercase_:						Union[str, Any]		,    lowercase_:						List[Any]					):
       # copy projection
       _UpperCamelCase  :							List[str]	    =	pt_weights['text_projection'].data.T

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


def SCREAMING_SNAKE_CASE					(  lowercase_:						str		,    lowercase_:						Any					):
       # copy projection
       _UpperCamelCase  :							List[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
       _UpperCamelCase  :							List[Any]	    =	pt_weights['visual.conv1.weight'].data
       _UpperCamelCase  :							List[Any]	    =	pt_weights['visual.class_embedding'].data
       _UpperCamelCase  :							int	    =	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_:						Optional[Any]		,    lowercase_:						Optional[int]		,    lowercase_:						List[Any]=None					):
       assert config_path is not None, "Please specify the ChineseCLIP model config of the corresponding model size."
       _UpperCamelCase  :							Optional[Any]	    =	ChineseCLIPConfig.from_pretrained(lowercase_					)

       _UpperCamelCase  :							List[Any]	    =	ChineseCLIPModel(lowercase_					).eval()

       _UpperCamelCase  :							List[Any]	    =	torch.load(lowercase_		,    map_location='cpu'					)['state_dict']
       _UpperCamelCase  :							Union[str, Any]	    =	{(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  :							List[Any]	    =	pt_weights['logit_scale'].data

       hf_model.save_pretrained(lowercase_					)


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!''')



