




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 							__lowercase   :



     def __init__(  self   :							Union[str, Any]      ,		lowercase__   :							List[str]      ,		lowercase__   :							str=1_3      ,		lowercase__   :							str=7      ,		lowercase__   :							List[Any]=True      ,		lowercase__   :							List[str]=True      ,		lowercase__   :							Any=True      ,		lowercase__   :							Optional[Any]=9_9      ,		lowercase__   :							Dict=3_2      ,		lowercase__   :							List[Any]=5      ,		lowercase__   :							Any=4      ,		lowercase__   :							Tuple=3_7      ,		lowercase__   :							Optional[Any]="gelu"      ,		lowercase__   :							Union[str, Any]=0.1      ,		lowercase__   :							Tuple=0.1      ,		lowercase__   :							Optional[Any]=5_1_2      ,		lowercase__   :							str=1_6      ,		lowercase__   :							Optional[Any]=2      ,		lowercase__   :							List[str]=0.02      ,		lowercase__   :							Tuple=3      ,		lowercase__   :							List[str]=4      ,		lowercase__   :							Dict=None      ,		):
       a_		    =  parent
       a_		    =  batch_size
       a_		    =  seq_length
       a_		    =  is_training
       a_		    =  use_input_mask
       a_		    =  use_labels
       a_		    =  vocab_size
       a_		    =  hidden_size
       a_		    =  num_hidden_layers
       a_		    =  num_attention_heads
       a_		    =  intermediate_size
       a_		    =  hidden_act
       a_		    =  hidden_dropout_prob
       a_		    =  attention_probs_dropout_prob
       a_		    =  max_position_embeddings
       a_		    =  type_vocab_size
       a_		    =  type_sequence_label_size
       a_		    =  initializer_range
       a_		    =  num_labels
       a_		    =  num_choices
       a_		    =  scope



     def 				__magic_name__		(  self   :							Union[str, Any]       ):
       a_		    =  ids_tensor([self.batch_size, self.seq_length]      ,		self.vocab_size       )

       a_		    =  None
       if self.use_input_mask:
         a_		    =  random_attention_mask([self.batch_size, self.seq_length]       )

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

       a_		    =  self.get_config()

       return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels



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

       a_		    =  None
       if self.use_input_mask:
         a_		    =  random_attention_mask([self.batch_size, self.seq_length]       )
       a_		    =  self.get_config()

       return config, input_ids, input_mask



     def 				__magic_name__		(  self   :							Union[str, Any]       ):
       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 				__magic_name__		(  self   :							int      ,		lowercase__   :							Union[str, Any]      ,		lowercase__   :							Any      ,		lowercase__   :							Any      ,		lowercase__   :							int      ,		lowercase__   :							int      ,		lowercase__   :							Optional[int]       ):
       a_		    =  ErnieMModel(config=a_       )
       model.to(a_       )
       model.eval()
       a_		    =  model(a_      ,		return_dict=a_       )
       self.parent.assertEqual(result.last_hidden_state.shape      ,		(self.batch_size, self.seq_length, self.hidden_size)       )



     def 				__magic_name__		(  self   :							str      ,		lowercase__   :							Union[str, Any]      ,		lowercase__   :							int      ,		lowercase__   :							int      ,		lowercase__   :							Optional[int]      ,		lowercase__   :							str      ,		lowercase__   :							int       ):
       a_		    =  ErnieMForQuestionAnswering(config=a_       )
       model.to(a_       )
       model.eval()
       a_		    =  model(
           a_      ,		attention_mask=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 				__magic_name__		(  self   :							List[str]      ,		lowercase__   :							Union[str, Any]      ,		lowercase__   :							List[Any]      ,		lowercase__   :							Optional[Any]      ,		lowercase__   :							Union[str, Any]      ,		lowercase__   :							Optional[Any]      ,		lowercase__   :							Dict       ):
       a_		    =  ErnieMForInformationExtraction(config=a_       )
       model.to(a_       )
       model.eval()
       a_		    =  torch.ones_like(a_      ,		dtype=torch.floataa       )
       a_		    =  model(
           a_      ,		attention_mask=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 				__magic_name__		(  self   :							Union[str, Any]      ,		lowercase__   :							int      ,		lowercase__   :							List[str]      ,		lowercase__   :							Optional[Any]      ,		lowercase__   :							List[Any]      ,		lowercase__   :							List[str]      ,		lowercase__   :							Any       ):
       a_		    =  self.num_labels
       a_		    =  ErnieMForSequenceClassification(a_       )
       model.to(a_       )
       model.eval()
       a_		    =  model(a_      ,		attention_mask=a_      ,		labels=a_       )
       self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.num_labels)       )



     def 				__magic_name__		(  self   :							List[str]      ,		lowercase__   :							List[str]      ,		lowercase__   :							List[Any]      ,		lowercase__   :							List[str]      ,		lowercase__   :							int      ,		lowercase__   :							Optional[Any]      ,		lowercase__   :							Tuple       ):
       a_		    =  self.num_labels
       a_		    =  ErnieMForTokenClassification(config=a_       )
       model.to(a_       )
       model.eval()
       input_ids.to(a_       )
       input_mask.to(a_       )
       token_labels.to(a_       )

       a_		    =  model(a_      ,		attention_mask=a_      ,		labels=a_       )

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



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



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



@require_torch
class 							__lowercase   (			__UpperCamelCase	,		__UpperCamelCase	,		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 				__magic_name__		(  self   :							List[str]      ,		lowercase__   :							Any      ,		lowercase__   :							Optional[int]      ,		lowercase__   :							List[Any]      ,		lowercase__   :							Optional[int]      ,		lowercase__   :							List[Any]       ):





       if pipeline_test_casse_name == "QAPipelineTests":
         return True

       return False



     def 				__magic_name__		(  self   :							Any       ):
       a_		    =  ErnieMModelTester(self       )
       a_		    =  ConfigTester(self      ,		config_class=a_      ,		hidden_size=3_7       )



     def 				__magic_name__		(  self   :							Optional[int]       ):
       self.config_tester.run_common_tests()



     def 				__magic_name__		(  self   :							List[str]       ):
       a_		    =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*a_       )



     def 				__magic_name__		(  self   :							Dict       ):
       a_		    =  self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
         a_		    =  type
         self.model_tester.create_and_check_model(*a_       )



     def 				__magic_name__		(  self   :							Tuple       ):
       a_		    =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_multiple_choice(*a_       )



     def 				__magic_name__		(  self   :							List[str]       ):
       a_		    =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*a_       )



     def 				__magic_name__		(  self   :							List[Any]       ):
       a_		    =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*a_       )



     def 				__magic_name__		(  self   :							int       ):
       a_		    =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_information_extraction(*a_       )



     def 				__magic_name__		(  self   :							Union[str, Any]       ):
       a_		    =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(*a_       )



     @slow
     def 				__magic_name__		(  self   :							List[str]       ):





       for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         a_		    =  ErnieMModel.from_pretrained(a_       )
         self.assertIsNotNone(a_       )



@require_torch
class 							__lowercase   (			unittest.TestCase    ):



     @slow
     def 				__magic_name__		(  self   :							List[str]       ):
       a_		    =  ErnieMModel.from_pretrained('''susnato/ernie-m-base_pytorch'''       )
       model.eval()
       a_		    =  torch.tensor([[0, 1, 2, 3, 4, 5]]       )
       a_		    =  model(a_       )[0]

       # TODO Replace vocab size
       a_		    =  7_6_8

       a_		    =  torch.Size((1, 6, hidden_size)       )
       self.assertEqual(output.shape      ,		a_       )

       a_		    =  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]      ,		a_      ,		atol=1e-3       )       )





class 						UpperCAmelCase__     :




			'''simple docstring'''






			def __init__(  self				:					List[Any]			,			a_				:					int					):
					'''simple docstring'''





					__UpperCAmelCase       :	List[Any]						=      data
					__UpperCAmelCase       :	BinaryTreeNode | None						=      None
					__UpperCAmelCase       :	BinaryTreeNode | None						=      None

def 			a		( _UpperCAmelCase				:   BinaryTreeNode | None					,  _UpperCAmelCase				:   int		):







		'''simple docstring'''





		if node is None:
				__UpperCAmelCase       :	List[Any]						=      BinaryTreeNode(_UpperCAmelCase		)
				return node

		# binary search tree is not empty,
		# so we will insert it into the tree
		# if new_value is less than value of data in node,
		#  add it to left subtree and proceed recursively
		if new_value < node.data:
				__UpperCAmelCase       :	int						=      insert(node.left_child					,  _UpperCAmelCase		)
		else:
				# if new_value is greater than value of data in node,
				#  add it to right subtree and proceed recursively
				__UpperCAmelCase       :	str						=      insert(node.right_child					,  _UpperCAmelCase		)
		return node

def 			a		( _UpperCAmelCase				:   None | BinaryTreeNode		):  # if node is None,return







		'''simple docstring'''





		if node:
				__UpperCAmelCase       :	Optional[int]						=      inorder(node.left_child		)
				__UpperCAmelCase       :	int						=      [*inorder_array, node.data]
				__UpperCAmelCase       :	Optional[Any]						=      inorder_array + inorder(node.right_child		)
		else:
				__UpperCAmelCase       :	List[Any]						=      []
		return inorder_array

def 			a		( ):







		'''simple docstring'''





		__UpperCAmelCase       :	Any						=      insert(_UpperCAmelCase					,  15		)
		insert(_UpperCAmelCase					,  10		)
		insert(_UpperCAmelCase					,  25		)
		insert(_UpperCAmelCase					,  6		)
		insert(_UpperCAmelCase					,  14		)
		insert(_UpperCAmelCase					,  20		)
		insert(_UpperCAmelCase					,  60		)
		return root

def 			a		( ):







		'''simple docstring'''





		__UpperCAmelCase       :	List[Any]						=      make_tree()
		print('''Printing values of binary search tree in Inorder Traversal.'''		)
		inorder(_UpperCAmelCase		)


if __name__ == "__main__":
				import doctest

				doctest.testmod()
				main()





