


import unittest

from transformers import ErnieMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

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


if is_torch_available():
			import torch

			from transformers import (
			    ErnieMForInformationExtraction,
			    ErnieMForMultipleChoice,
			    ErnieMForQuestionAnswering,
			    ErnieMForSequenceClassification,
			    ErnieMForTokenClassification,
			    ErnieMModel,
			)
			from transformers.models.ernie_m.modeling_ernie_m import ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST



class     snake_case__  :
				def __init__(  self     ,      _UpperCAmelCase     ,      _UpperCAmelCase=13     ,      _UpperCAmelCase=7     ,      _UpperCAmelCase=True     ,      _UpperCAmelCase=True     ,      _UpperCAmelCase=True     ,      _UpperCAmelCase=99     ,      _UpperCAmelCase=32     ,      _UpperCAmelCase=5     ,      _UpperCAmelCase=4     ,      _UpperCAmelCase=37     ,      _UpperCAmelCase="gelu"     ,      _UpperCAmelCase=0.1     ,      _UpperCAmelCase=0.1     ,      _UpperCAmelCase=512     ,      _UpperCAmelCase=16     ,      _UpperCAmelCase=2     ,      _UpperCAmelCase=0.02     ,      _UpperCAmelCase=3     ,      _UpperCAmelCase=4     ,      _UpperCAmelCase=None     ,      ):




										"""simple docstring"""




										__magic_name__				:							Tuple				=      parent
										__magic_name__				:							Any				=      batch_size
										__magic_name__				:							Dict				=      seq_length
										__magic_name__				:							List[str]				=      is_training
										__magic_name__				:							Any				=      use_input_mask
										__magic_name__				:							List[str]				=      use_labels
										__magic_name__				:							Union[str, Any]				=      vocab_size
										__magic_name__				:							Any				=      hidden_size
										__magic_name__				:							List[str]				=      num_hidden_layers
										__magic_name__				:							Tuple				=      num_attention_heads
										__magic_name__				:							Optional[Any]				=      intermediate_size
										__magic_name__				:							Optional[int]				=      hidden_act
										__magic_name__				:							List[Any]				=      hidden_dropout_prob
										__magic_name__				:							Dict				=      attention_probs_dropout_prob
										__magic_name__				:							str				=      max_position_embeddings
										__magic_name__				:							Tuple				=      type_vocab_size
										__magic_name__				:							List[Any]				=      type_sequence_label_size
										__magic_name__				:							Optional[int]				=      initializer_range
										__magic_name__				:							Union[str, Any]				=      num_labels
										__magic_name__				:							Any				=      num_choices
										__magic_name__				:							Optional[int]				=      scope
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							str				=      ids_tensor([self.batch_size, self.seq_length]     ,      self.vocab_size      )

										__magic_name__				:							List[str]				=      None
										if self.use_input_mask:
																__magic_name__				:							List[str]				=      random_attention_mask([self.batch_size, self.seq_length]      )

										__magic_name__				:							Tuple				=      None
										__magic_name__				:							str				=      None
										__magic_name__				:							Union[str, Any]				=      None
										if self.use_labels:
																__magic_name__				:							Any				=      ids_tensor([self.batch_size]     ,      self.type_sequence_label_size      )
																__magic_name__				:							Any				=      ids_tensor([self.batch_size, self.seq_length]     ,      self.num_labels      )
																__magic_name__				:							Union[str, Any]				=      ids_tensor([self.batch_size]     ,      self.num_choices      )

										__magic_name__				:							Tuple				=      self.get_config()

										return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							Any				=      ids_tensor([self.batch_size, self.seq_length]     ,      self.vocab_size      )

										__magic_name__				:							int				=      None
										if self.use_input_mask:
																__magic_name__				:							Dict				=      random_attention_mask([self.batch_size, self.seq_length]      )
										__magic_name__				:							List[Any]				=      self.get_config()

										return config, input_ids, input_mask
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										return ErnieMConfig(
										    vocab_size=self.vocab_size     ,      hidden_size=self.hidden_size     ,      num_hidden_layers=self.num_hidden_layers     ,      num_attention_heads=self.num_attention_heads     ,      intermediate_size=self.intermediate_size     ,      hidden_act=self.hidden_act     ,      hidden_dropout_prob=self.hidden_dropout_prob     ,      attention_probs_dropout_prob=self.attention_probs_dropout_prob     ,      max_position_embeddings=self.max_position_embeddings     ,      type_vocab_size=self.type_vocab_size     ,      initializer_range=self.initializer_range     ,      )
				def  UpperCamelCase_						(  self     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase      ):




										"""simple docstring"""




										__magic_name__				:							List[str]				=      ErnieMModel(config=_UpperCAmelCase      )
										model.to(_UpperCAmelCase      )
										model.eval()
										__magic_name__				:							Optional[Any]				=      model(_UpperCAmelCase     ,      return_dict=_UpperCAmelCase      )
										self.parent.assertEqual(result.last_hidden_state.shape     ,      (self.batch_size, self.seq_length, self.hidden_size)      )
				def  UpperCamelCase_						(  self     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase      ):




										"""simple docstring"""




										__magic_name__				:							Dict				=      ErnieMForQuestionAnswering(config=_UpperCAmelCase      )
										model.to(_UpperCAmelCase      )
										model.eval()
										__magic_name__				:							Optional[int]				=      model(
										    _UpperCAmelCase     ,      attention_mask=_UpperCAmelCase     ,      start_positions=_UpperCAmelCase     ,      end_positions=_UpperCAmelCase     ,      )
										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     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase      ):




										"""simple docstring"""




										__magic_name__				:							Tuple				=      ErnieMForInformationExtraction(config=_UpperCAmelCase      )
										model.to(_UpperCAmelCase      )
										model.eval()
										__magic_name__				:							Any				=      torch.ones_like(_UpperCAmelCase     ,      dtype=torch.floataa      )
										__magic_name__				:							Tuple				=      model(
										    _UpperCAmelCase     ,      attention_mask=_UpperCAmelCase     ,      start_positions=_UpperCAmelCase     ,      end_positions=_UpperCAmelCase     ,      )
										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     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase      ):




										"""simple docstring"""




										__magic_name__				:							str				=      self.num_labels
										__magic_name__				:							Optional[Any]				=      ErnieMForSequenceClassification(_UpperCAmelCase      )
										model.to(_UpperCAmelCase      )
										model.eval()
										__magic_name__				:							Any				=      model(_UpperCAmelCase     ,      attention_mask=_UpperCAmelCase     ,      labels=_UpperCAmelCase      )
										self.parent.assertEqual(result.logits.shape     ,      (self.batch_size, self.num_labels)      )
				def  UpperCamelCase_						(  self     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase      ):




										"""simple docstring"""




										__magic_name__				:							Optional[Any]				=      self.num_labels
										__magic_name__				:							Optional[Any]				=      ErnieMForTokenClassification(config=_UpperCAmelCase      )
										model.to(_UpperCAmelCase      )
										model.eval()
										input_ids.to(_UpperCAmelCase      )
										input_mask.to(_UpperCAmelCase      )
										token_labels.to(_UpperCAmelCase      )

										__magic_name__				:							Tuple				=      model(_UpperCAmelCase     ,      attention_mask=_UpperCAmelCase     ,      labels=_UpperCAmelCase      )

										self.parent.assertEqual(result.logits.shape     ,      (self.batch_size, self.seq_length, self.num_labels)      )
				def  UpperCamelCase_						(  self     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase      ):




										"""simple docstring"""




										__magic_name__				:							Union[str, Any]				=      self.num_choices
										__magic_name__				:							int				=      ErnieMForMultipleChoice(config=_UpperCAmelCase      )
										model.to(_UpperCAmelCase      )
										model.eval()
										__magic_name__				:							int				=      input_ids.unsqueeze(1      ).expand(-1     ,      self.num_choices     ,      -1      ).contiguous()
										__magic_name__				:							Any				=      input_mask.unsqueeze(1      ).expand(-1     ,      self.num_choices     ,      -1      ).contiguous()
										__magic_name__				:							List[Any]				=      model(
										    _UpperCAmelCase     ,      attention_mask=_UpperCAmelCase     ,      labels=_UpperCAmelCase     ,      )
										self.parent.assertEqual(result.logits.shape     ,      (self.batch_size, self.num_choices)      )





				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							Union[str, Any]				=      self.prepare_config_and_inputs()
										(
										    (
										    __magic_name__
										)		, (
										    __magic_name__
										)		, (
										    __magic_name__
										)		, (
										    __magic_name__
										)		, (
										    __magic_name__
										)		, (
										    __magic_name__
										)		, 
										)				:							List[str]				=      config_and_inputs
										__magic_name__				:							Union[str, Any]				=      {'input_ids': input_ids, 'attention_mask': input_mask}
										return config, inputs_dict






@require_torch
class     snake_case__  (      _A						,       _A						,       unittest.TestCase  ):
				__lowerCAmelCase		=      (
				    (
				        ErnieMModel,
				        ErnieMForMultipleChoice,
				        ErnieMForQuestionAnswering,
				        ErnieMForSequenceClassification,
				        ErnieMForTokenClassification,
				    )
				    if is_torch_available()
				    else ()
				)
				__lowerCAmelCase		=      ()
				__lowerCAmelCase		=      (
				    {
				        '''feature-extraction''': ErnieMModel,
				        '''question-answering''': ErnieMForQuestionAnswering,
				        '''text-classification''': ErnieMForSequenceClassification,
				        '''token-classification''': ErnieMForTokenClassification,
				        '''zero-shot''': ErnieMForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)
				__lowerCAmelCase		=      False
				def  UpperCamelCase_						(  self     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase      ):




										"""simple docstring"""




										if pipeline_test_casse_name == "QAPipelineTests":
																return True

										return False
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							List[str]				=      ErnieMModelTester(self      )
										__magic_name__				:							Dict				=      ConfigTester(self     ,      config_class=_UpperCAmelCase     ,      hidden_size=37      )
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										self.config_tester.run_common_tests()
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							Optional[int]				=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*_UpperCAmelCase      )
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							int				=      self.model_tester.prepare_config_and_inputs()
										for type in ["absolute", "relative_key", "relative_key_query"]:
																__magic_name__				:							Optional[Any]				=      type
																self.model_tester.create_and_check_model(*_UpperCAmelCase      )
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							Union[str, Any]				=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_multiple_choice(*_UpperCAmelCase      )
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							Any				=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(*_UpperCAmelCase      )
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							List[str]				=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_sequence_classification(*_UpperCAmelCase      )
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							Any				=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_information_extraction(*_UpperCAmelCase      )
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							str				=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*_UpperCAmelCase      )





				@slow
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__magic_name__				:							Optional[Any]				=      ErnieMModel.from_pretrained(_UpperCAmelCase      )
																self.assertIsNotNone(_UpperCAmelCase      )






@require_torch
class     snake_case__  (      unittest.TestCase  ):
				@slow
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							Dict				=      ErnieMModel.from_pretrained('susnato/ernie-m-base_pytorch'      )
										model.eval()
										__magic_name__				:							int				=      torch.tensor([[0, 1, 2, 3, 4, 5]]      )
										__magic_name__				:							List[str]				=      model(_UpperCAmelCase      )[0]

										# TODO Replace vocab size
										__magic_name__				:							List[Any]				=      768

										__magic_name__				:							int				=      torch.Size((1, 6, hidden_size)      )
										self.assertEqual(output.shape     ,      _UpperCAmelCase      )

										__magic_name__				:							int				=      torch.tensor(
										    [[[-0.0012, 0.1245, -0.0214], [-0.0742, 0.0244, -0.0771], [-0.0333, 0.1164, -0.1554]]]      )

										self.assertTrue(torch.allclose(output[:, :3, :3]     ,      _UpperCAmelCase     ,      atol=1E-3      )      )






import argparse

from . import (
    BertConfig,
    BertGenerationConfig,
    BertGenerationDecoder,
    BertGenerationEncoder,
    load_tf_weights_in_bert_generation,
    logging,
)


logging.set_verbosity_info()
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :Optional[int]      ,						lowercase__  :List[Any]      ,						lowercase__  :str      ,						lowercase__  :List[str]      ,						lowercase__  :List[Any]				)       ->  List[Any]:
						# Initialise PyTorch model
						__magic_name__				:							Any				=      BertConfig.from_pretrained(
						    'bert-large-cased'      ,						vocab_size=lowercase__      ,						max_position_embeddings=5_1_2      ,						is_decoder=lowercase__      ,						add_cross_attention=lowercase__      ,						)
						__magic_name__				:							Optional[int]				=      bert_config.to_dict()
						del bert_config_dict["type_vocab_size"]
						__magic_name__				:							Optional[int]				=      BertGenerationConfig(**lowercase__				)
						if is_encoder:
												__magic_name__				:							Dict				=      BertGenerationEncoder(lowercase__				)
						else:
												__magic_name__				:							str				=      BertGenerationDecoder(lowercase__				)
						print(f"Building PyTorch model from configuration: {config}"				)

						# Load weights from tf checkpoint
						load_tf_weights_in_bert_generation(
						    lowercase__      ,						lowercase__      ,						model_class='bert'      ,						is_encoder_named_decoder=lowercase__      ,						is_encoder=lowercase__      ,						)

						# Save pytorch-model
						print(f"Save PyTorch model and config to {pytorch_dump_path}"				)
						model.save_pretrained(lowercase__				)


if __name__ == "__main__":
			snake_case__     							= argparse.ArgumentParser()
			# Required parameters
			parser.add_argument(
			    '--tf_hub_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.'
			)
			parser.add_argument(
			    '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.'
			)
			parser.add_argument(
			    '--is_encoder_named_decoder',
			    action='store_true',
			    help='If decoder has to be renamed to encoder in PyTorch model.',
			)
			parser.add_argument('--is_encoder', action='store_true', help='If model is an encoder.')
			parser.add_argument('--vocab_size', default=5_0358, type=int, help='Vocab size of model')
			snake_case__     							= parser.parse_args()
			convert_tf_checkpoint_to_pytorch(
			    args.tf_hub_path,
			    args.pytorch_dump_path,
			    args.is_encoder_named_decoder,
			    args.vocab_size,
			    is_encoder=args.is_encoder,
			)


