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





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





		def  _A							(							self      :		str			):
							lowerCAmelCase						:   Optional[Any]							=       ids_tensor([self.batch_size, self.seq_length]  ,					self.vocab_size			)

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

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

							lowerCAmelCase						:   Any							=       self.get_config()

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





		def  _A							(							self      :		Any			):
							lowerCAmelCase						:   Tuple							=       ids_tensor([self.batch_size, self.seq_length]  ,					self.vocab_size			)

							lowerCAmelCase						:   str							=       None
							if self.use_input_mask:
												lowerCAmelCase						:   Dict							=       random_attention_mask([self.batch_size, self.seq_length]			)
							lowerCAmelCase						:   int							=       self.get_config()

							return config, input_ids, input_mask





		def  _A							(							self      :		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  _A							(							self      :		List[Any]  ,					lowerCamelCase__      :		Tuple  ,					lowerCamelCase__      :		Optional[Any]  ,					lowerCamelCase__      :		List[str]  ,					lowerCamelCase__      :		Dict  ,					lowerCamelCase__      :		str  ,					lowerCamelCase__      :		str			):
							lowerCAmelCase						:   List[Any]							=       ErnieMModel(config=UpperCamelCase__			)
							model.to(UpperCamelCase__			)
							model.eval()
							lowerCAmelCase						:   Optional[int]							=       model(UpperCamelCase__  ,					return_dict=UpperCamelCase__			)
							self.parent.assertEqual(result.last_hidden_state.shape  ,					(self.batch_size, self.seq_length, self.hidden_size)			)





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





		def  _A							(							self      :		Any  ,					lowerCamelCase__      :		Union[str, Any]  ,					lowerCamelCase__      :		Tuple  ,					lowerCamelCase__      :		str  ,					lowerCamelCase__      :		List[str]  ,					lowerCamelCase__      :		str  ,					lowerCamelCase__      :		List[Any]			):
							lowerCAmelCase						:   List[Any]							=       self.num_labels
							lowerCAmelCase						:   str							=       ErnieMForTokenClassification(config=UpperCamelCase__			)
							model.to(UpperCamelCase__			)
							model.eval()
							input_ids.to(UpperCamelCase__			)
							input_mask.to(UpperCamelCase__			)
							token_labels.to(UpperCamelCase__			)

							lowerCAmelCase						:   Dict							=       model(UpperCamelCase__  ,					attention_mask=UpperCamelCase__  ,					labels=UpperCamelCase__			)

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





		def  _A							(							self      :		Union[str, Any]  ,					lowerCamelCase__      :		Optional[Any]  ,					lowerCamelCase__      :		List[str]  ,					lowerCamelCase__      :		List[Any]  ,					lowerCamelCase__      :		int  ,					lowerCamelCase__      :		Tuple  ,					lowerCamelCase__      :		List[str]			):
							lowerCAmelCase						:   Tuple							=       self.num_choices
							lowerCAmelCase						:   Optional[Any]							=       ErnieMForMultipleChoice(config=UpperCamelCase__			)
							model.to(UpperCamelCase__			)
							model.eval()
							lowerCAmelCase						:   Optional[Any]							=       input_ids.unsqueeze(1			).expand(-1  ,					self.num_choices  ,					-1			).contiguous()
							lowerCAmelCase						:   Optional[int]							=       input_mask.unsqueeze(1			).expand(-1  ,					self.num_choices  ,					-1			).contiguous()
							lowerCAmelCase						:   int							=       model(
							    UpperCamelCase__  ,					attention_mask=UpperCamelCase__  ,					labels=UpperCamelCase__  ,					)
							self.parent.assertEqual(result.logits.shape  ,					(self.batch_size, self.num_choices)			)





		def  _A							(							self      :		Tuple			):
							lowerCAmelCase						:   Optional[int]							=       self.prepare_config_and_inputs()
							(
							    lowerCAmelCase				
							)						:   int							=       config_and_inputs
							lowerCAmelCase						:   str							=       {"input_ids": input_ids, "attention_mask": input_mask}
							return config, inputs_dict



@require_torch
class 						__magic_name__				(  lowerCAmelCase__,			lowerCAmelCase__,			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  _A							(							self      :		Tuple  ,					lowerCamelCase__      :		Tuple  ,					lowerCamelCase__      :		Optional[int]  ,					lowerCamelCase__      :		Dict  ,					lowerCamelCase__      :		List[str]  ,					lowerCamelCase__      :		str			):
							if pipeline_test_casse_name == "QAPipelineTests":
												return True

							return False





		def  _A							(							self      :		Dict			):
							lowerCAmelCase						:   Tuple							=       ErnieMModelTester(self			)
							lowerCAmelCase						:   List[Any]							=       ConfigTester(self  ,					config_class=UpperCamelCase__  ,					hidden_size=3_7			)





		def  _A							(							self      :		List[str]			):
							self.config_tester.run_common_tests()





		def  _A							(							self      :		Optional[Any]			):
							lowerCAmelCase						:   Optional[Any]							=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*UpperCamelCase__			)





		def  _A							(							self      :		Optional[Any]			):
							lowerCAmelCase						:   List[Any]							=       self.model_tester.prepare_config_and_inputs()
							for type in ["absolute", "relative_key", "relative_key_query"]:
												lowerCAmelCase						:   int							=       type
												self.model_tester.create_and_check_model(*UpperCamelCase__			)





		def  _A							(							self      :		List[Any]			):
							lowerCAmelCase						:   int							=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_multiple_choice(*UpperCamelCase__			)





		def  _A							(							self      :		Union[str, Any]			):
							lowerCAmelCase						:   Optional[Any]							=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*UpperCamelCase__			)





		def  _A							(							self      :		Union[str, Any]			):
							lowerCAmelCase						:   Tuple							=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase__			)





		def  _A							(							self      :		Optional[int]			):
							lowerCAmelCase						:   Union[str, Any]							=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_information_extraction(*UpperCamelCase__			)





		def  _A							(							self      :		Tuple			):
							lowerCAmelCase						:   Any							=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*UpperCamelCase__			)





		@slow
		def  _A							(							self      :		Optional[int]			):
							for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												lowerCAmelCase						:   Union[str, Any]							=       ErnieMModel.from_pretrained(UpperCamelCase__			)
												self.assertIsNotNone(UpperCamelCase__			)



@require_torch
class 						__magic_name__				(  unittest.TestCase      ):





		@slow
		def  _A							(							self      :		Tuple			):
							lowerCAmelCase						:   Tuple							=       ErnieMModel.from_pretrained('''susnato/ernie-m-base_pytorch'''			)
							model.eval()
							lowerCAmelCase						:   Optional[Any]							=       torch.tensor([[0, 1, 2, 3, 4, 5]]			)
							lowerCAmelCase						:   Dict							=       model(UpperCamelCase__			)[0]

							# TODO Replace vocab size
							lowerCAmelCase						:   Optional[int]							=       7_6_8

							lowerCAmelCase						:   Optional[Any]							=       torch.Size((1, 6, hidden_size)			)
							self.assertEqual(output.shape  ,					UpperCamelCase__			)

							lowerCAmelCase						:   Dict							=       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]  ,					UpperCamelCase__  ,					atol=1E-3			)			)


def       UpperCAmelCase__							(    __magic_name__  :       Union[str, Any]							,					__magic_name__  :       int					):


					'''simple docstring'''

					sequence.append(__magic_name__					)

					lowerCAmelCase						:   Tuple							=       0
					while sequence[index] != target:
										index += 1

					sequence.pop()

					if index == len(__magic_name__					):
										return None

					return index


if __name__ == "__main__":
		__SCREAMING_SNAKE_CASE     :				Dict             =       input('Enter numbers separated by comma:\n').strip()
		__SCREAMING_SNAKE_CASE     :				int             =       [int(item) for item in user_input.split(',')]

		__SCREAMING_SNAKE_CASE     :				str             =       input('Enter a single number to be found in the list:\n')
		__SCREAMING_SNAKE_CASE     :				Tuple             =       int(target_input)
		__SCREAMING_SNAKE_CASE     :				Union[str, Any]             =       sentinel_linear_search(sequence, target)
		if result is not None:
				print(f"""{target} found at positions: {result}""")
		else:
				print('Not found')
