



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




			'''simple docstring'''






			def __init__(  self				:					Union[str, Any]			,			a_				:					List[str]			,			a_				:					str=13			,			a_				:					str=7			,			a_				:					List[Any]=True			,			a_				:					List[str]=True			,			a_				:					Any=True			,			a_				:					Optional[Any]=99			,			a_				:					Dict=32			,			a_				:					List[Any]=5			,			a_				:					Any=4			,			a_				:					Tuple=37			,			a_				:					Optional[Any]="gelu"			,			a_				:					Union[str, Any]=0.1			,			a_				:					Tuple=0.1			,			a_				:					Optional[Any]=5_12			,			a_				:					str=16			,			a_				:					Optional[Any]=2			,			a_				:					List[str]=0.0_2			,			a_				:					Tuple=3			,			a_				:					List[str]=4			,			a_				:					Dict=None			,			):
					'''simple docstring'''





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




			def 					snake_case__	(  self				:					Union[str, Any]					):
					'''simple docstring'''





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

					__UpperCAmelCase       :	Any						=      None
					if self.use_input_mask:
							__UpperCAmelCase       :	Optional[Any]						=      random_attention_mask([self.batch_size, self.seq_length]					)

					__UpperCAmelCase       :	Optional[int]						=      None
					__UpperCAmelCase       :	Tuple						=      None
					__UpperCAmelCase       :	List[Any]						=      None
					if self.use_labels:
							__UpperCAmelCase       :	Any						=      ids_tensor([self.batch_size]			,			self.type_sequence_label_size					)
							__UpperCAmelCase       :	Dict						=      ids_tensor([self.batch_size, self.seq_length]			,			self.num_labels					)
							__UpperCAmelCase       :	str						=      ids_tensor([self.batch_size]			,			self.num_choices					)

					__UpperCAmelCase       :	Union[str, Any]						=      self.get_config()

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




			def 					snake_case__	(  self				:					List[str]					):
					'''simple docstring'''





					__UpperCAmelCase       :	int						=      ids_tensor([self.batch_size, self.seq_length]			,			self.vocab_size					)

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

					return config, input_ids, input_mask




			def 					snake_case__	(  self				:					Union[str, Any]					):
					'''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 					snake_case__	(  self				:					int			,			a_				:					Union[str, Any]			,			a_				:					Any			,			a_				:					Any			,			a_				:					int			,			a_				:					int			,			a_				:					Optional[int]					):
					'''simple docstring'''





					__UpperCAmelCase       :	Any						=      ErnieMModel(config=a_					)
					model.to(a_					)
					model.eval()
					__UpperCAmelCase       :	Tuple						=      model(a_			,			return_dict=a_					)
					self.parent.assertEqual(result.last_hidden_state.shape			,			(self.batch_size, self.seq_length, self.hidden_size)					)




			def 					snake_case__	(  self				:					str			,			a_				:					Union[str, Any]			,			a_				:					int			,			a_				:					int			,			a_				:					Optional[int]			,			a_				:					str			,			a_				:					int					):
					'''simple docstring'''





					__UpperCAmelCase       :	int						=      ErnieMForQuestionAnswering(config=a_					)
					model.to(a_					)
					model.eval()
					__UpperCAmelCase       :	Tuple						=      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 					snake_case__	(  self				:					List[str]			,			a_				:					Union[str, Any]			,			a_				:					List[Any]			,			a_				:					Optional[Any]			,			a_				:					Union[str, Any]			,			a_				:					Optional[Any]			,			a_				:					Dict					):
					'''simple docstring'''





					__UpperCAmelCase       :	Any						=      ErnieMForInformationExtraction(config=a_					)
					model.to(a_					)
					model.eval()
					__UpperCAmelCase       :	Optional[Any]						=      torch.ones_like(a_			,			dtype=torch.floataa					)
					__UpperCAmelCase       :	Any						=      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 					snake_case__	(  self				:					Union[str, Any]			,			a_				:					int			,			a_				:					List[str]			,			a_				:					Optional[Any]			,			a_				:					List[Any]			,			a_				:					List[str]			,			a_				:					Any					):
					'''simple docstring'''





					__UpperCAmelCase       :	int						=      self.num_labels
					__UpperCAmelCase       :	Optional[int]						=      ErnieMForSequenceClassification(a_					)
					model.to(a_					)
					model.eval()
					__UpperCAmelCase       :	Optional[int]						=      model(a_			,			attention_mask=a_			,			labels=a_					)
					self.parent.assertEqual(result.logits.shape			,			(self.batch_size, self.num_labels)					)




			def 					snake_case__	(  self				:					List[str]			,			a_				:					List[str]			,			a_				:					List[Any]			,			a_				:					List[str]			,			a_				:					int			,			a_				:					Optional[Any]			,			a_				:					Tuple					):
					'''simple docstring'''





					__UpperCAmelCase       :	Optional[Any]						=      self.num_labels
					__UpperCAmelCase       :	List[str]						=      ErnieMForTokenClassification(config=a_					)
					model.to(a_					)
					model.eval()
					input_ids.to(a_					)
					input_mask.to(a_					)
					token_labels.to(a_					)

					__UpperCAmelCase       :	Tuple						=      model(a_			,			attention_mask=a_			,			labels=a_					)

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




			def 					snake_case__	(  self				:					Optional[Any]			,			a_				:					int			,			a_				:					str			,			a_				:					Any			,			a_				:					List[str]			,			a_				:					Dict			,			a_				:					Optional[Any]					):
					'''simple docstring'''





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




			def 					snake_case__	(  self				:					List[str]					):
					'''simple docstring'''





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

@require_torch
class 						UpperCAmelCase__     (     __UpperCamelCase		,__UpperCamelCase		,unittest.TestCase  ):




			'''simple docstring'''






			UpperCamelCase     		=			(
			    (
			        ErnieMModel,
			        ErnieMForMultipleChoice,
			        ErnieMForQuestionAnswering,
			        ErnieMForSequenceClassification,
			        ErnieMForTokenClassification,
			    )
			    if is_torch_available()
			    else ()
			)
			UpperCamelCase     		=			()
			UpperCamelCase     		=			(
			    {
			        """feature-extraction""": ErnieMModel,
			        """question-answering""": ErnieMForQuestionAnswering,
			        """text-classification""": ErnieMForSequenceClassification,
			        """token-classification""": ErnieMForTokenClassification,
			        """zero-shot""": ErnieMForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)
			UpperCamelCase     		=			False




			def 					snake_case__	(  self				:					List[str]			,			a_				:					Any			,			a_				:					Optional[int]			,			a_				:					List[Any]			,			a_				:					Optional[int]			,			a_				:					List[Any]					):
					'''simple docstring'''





					if pipeline_test_casse_name == "QAPipelineTests":
							return True

					return False




			def 					snake_case__	(  self				:					Any					):
					'''simple docstring'''





					__UpperCAmelCase       :	Optional[int]						=      ErnieMModelTester(self					)
					__UpperCAmelCase       :	Optional[int]						=      ConfigTester(self			,			config_class=a_			,			hidden_size=37					)




			def 					snake_case__	(  self				:					Optional[int]					):
					'''simple docstring'''





					self.config_tester.run_common_tests()




			def 					snake_case__	(  self				:					List[str]					):
					'''simple docstring'''





					__UpperCAmelCase       :	Optional[int]						=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*a_					)




			def 					snake_case__	(  self				:					Dict					):
					'''simple docstring'''





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




			def 					snake_case__	(  self				:					Tuple					):
					'''simple docstring'''





					__UpperCAmelCase       :	str						=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_multiple_choice(*a_					)




			def 					snake_case__	(  self				:					List[str]					):
					'''simple docstring'''





					__UpperCAmelCase       :	Tuple						=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(*a_					)




			def 					snake_case__	(  self				:					List[Any]					):
					'''simple docstring'''





					__UpperCAmelCase       :	Optional[Any]						=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(*a_					)




			def 					snake_case__	(  self				:					int					):
					'''simple docstring'''





					__UpperCAmelCase       :	str						=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_information_extraction(*a_					)




			def 					snake_case__	(  self				:					Union[str, Any]					):
					'''simple docstring'''





					__UpperCAmelCase       :	Optional[int]						=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(*a_					)




			@slow
			def 					snake_case__	(  self				:					List[str]					):
					'''simple docstring'''





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

@require_torch
class 						UpperCAmelCase__     (     unittest.TestCase  ):




			'''simple docstring'''






			@slow
			def 					snake_case__	(  self				:					List[str]					):
					'''simple docstring'''





					__UpperCAmelCase       :	Optional[Any]						=      ErnieMModel.from_pretrained('''susnato/ernie-m-base_pytorch'''					)
					model.eval()
					__UpperCAmelCase       :	Any						=      torch.tensor([[0, 1, 2, 3, 4, 5]]					)
					__UpperCAmelCase       :	Union[str, Any]						=      model(a_					)[0]

					# TODO Replace vocab size
					__UpperCAmelCase       :	Dict						=      7_68

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

					__UpperCAmelCase       :	Optional[Any]						=      torch.tensor(
					    [[[-0.0_0_1_2, 0.1_2_4_5, -0.0_2_1_4], [-0.0_7_4_2, 0.0_2_4_4, -0.0_7_7_1], [-0.0_3_3_3, 0.1_1_6_4, -0.1_5_5_4]]]					)

					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()





