




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							:						Any							,  a							:						List[Any]							,  a							:						Union[str, Any]=13							,  a							:						int=7							,  a							:						List[Any]=True							,  a							:						List[Any]=True							,  a							:						Any=True							,  a							:						List[str]=99							,  a							:						str=32							,  a							:						Optional[int]=5							,  a							:						str=4							,  a							:						int=37							,  a							:						Optional[int]="gelu"							,  a							:						Optional[int]=0.1							,  a							:						Dict=0.1							,  a							:						List[Any]=512							,  a							:						Optional[int]=16							,  a							:						int=2							,  a							:						Any=0.02							,  a							:						Optional[int]=3							,  a							:						str=4							,  a							:						Optional[Any]=None							,  )      ->       Optional[int]:
										SCREAMING_SNAKE_CASE     =       parent
										SCREAMING_SNAKE_CASE     =       batch_size
										SCREAMING_SNAKE_CASE     =       seq_length
										SCREAMING_SNAKE_CASE     =       is_training
										SCREAMING_SNAKE_CASE     =       use_input_mask
										SCREAMING_SNAKE_CASE     =       use_labels
										SCREAMING_SNAKE_CASE     =       vocab_size
										SCREAMING_SNAKE_CASE     =       hidden_size
										SCREAMING_SNAKE_CASE     =       num_hidden_layers
										SCREAMING_SNAKE_CASE     =       num_attention_heads
										SCREAMING_SNAKE_CASE     =       intermediate_size
										SCREAMING_SNAKE_CASE     =       hidden_act
										SCREAMING_SNAKE_CASE     =       hidden_dropout_prob
										SCREAMING_SNAKE_CASE     =       attention_probs_dropout_prob
										SCREAMING_SNAKE_CASE     =       max_position_embeddings
										SCREAMING_SNAKE_CASE     =       type_vocab_size
										SCREAMING_SNAKE_CASE     =       type_sequence_label_size
										SCREAMING_SNAKE_CASE     =       initializer_range
										SCREAMING_SNAKE_CASE     =       num_labels
										SCREAMING_SNAKE_CASE     =       num_choices
										SCREAMING_SNAKE_CASE     =       scope





							def  _UpperCAmelCase  (       self							:						Optional[int]      )      ->       Dict:
										SCREAMING_SNAKE_CASE     =       ids_tensor([self.batch_size, self.seq_length]							,  self.vocab_size      )

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

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

										SCREAMING_SNAKE_CASE     =       self.get_config()

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





							def  _UpperCAmelCase  (       self							:						Optional[Any]      )      ->       int:
										SCREAMING_SNAKE_CASE     =       ids_tensor([self.batch_size, self.seq_length]							,  self.vocab_size      )

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

										return config, input_ids, input_mask





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





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





							def  _UpperCAmelCase  (       self							:						Union[str, Any]							,  a							:						List[str]							,  a							:						Optional[int]							,  a							:						int							,  a							:						List[str]							,  a							:						Optional[int]							,  a							:						int      )      ->       List[Any]:
										SCREAMING_SNAKE_CASE     =       self.num_labels
										SCREAMING_SNAKE_CASE     =       ErnieMForTokenClassification(config=a      )
										model.to(a      )
										model.eval()
										input_ids.to(a      )
										input_mask.to(a      )
										token_labels.to(a      )

										SCREAMING_SNAKE_CASE     =       model(a							,  attention_mask=a							,  labels=a      )

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





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







							def  _UpperCAmelCase  (       self							:						Union[str, Any]      )      ->       Optional[Any]:
										SCREAMING_SNAKE_CASE     =       self.prepare_config_and_inputs()
										(
										    (
										    SCREAMING_SNAKE_CASE  
										)  ,	(
										    SCREAMING_SNAKE_CASE  
										)  ,	(
										    SCREAMING_SNAKE_CASE  
										)  ,	(
										    SCREAMING_SNAKE_CASE  
										)  ,	(
										    SCREAMING_SNAKE_CASE  
										)  ,	(
										    SCREAMING_SNAKE_CASE  
										)  ,	  
										)     =       config_and_inputs
										SCREAMING_SNAKE_CASE     =       {"""input_ids""": input_ids, """attention_mask""": input_mask}
										return config, inputs_dict





@require_torch
class       UpperCAmelCase_   ( SCREAMING_SNAKE_CASE__		,   SCREAMING_SNAKE_CASE__		,   unittest.TestCase							):




							'''simple docstring'''



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





							def  _UpperCAmelCase  (       self							:						str							,  a							:						Tuple							,  a							:						Union[str, Any]							,  a							:						List[str]							,  a							:						Dict							,  a							:						int      )      ->       Union[str, Any]:







										if pipeline_test_casse_name == "QAPipelineTests":
													return True

										return False





							def  _UpperCAmelCase  (       self							:						Dict      )      ->       int:
										SCREAMING_SNAKE_CASE     =       ErnieMModelTester(self      )
										SCREAMING_SNAKE_CASE     =       ConfigTester(self							,  config_class=a							,  hidden_size=37      )





							def  _UpperCAmelCase  (       self							:						Union[str, Any]      )      ->       Optional[int]:
										self.config_tester.run_common_tests()





							def  _UpperCAmelCase  (       self							:						Optional[int]      )      ->       Optional[Any]:
										SCREAMING_SNAKE_CASE     =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*a      )





							def  _UpperCAmelCase  (       self							:						Optional[int]      )      ->       List[Any]:
										SCREAMING_SNAKE_CASE     =       self.model_tester.prepare_config_and_inputs()
										for type in ["absolute", "relative_key", "relative_key_query"]:
													SCREAMING_SNAKE_CASE     =       type
													self.model_tester.create_and_check_model(*a      )





							def  _UpperCAmelCase  (       self							:						str      )      ->       List[Any]:
										SCREAMING_SNAKE_CASE     =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_multiple_choice(*a      )





							def  _UpperCAmelCase  (       self							:						List[Any]      )      ->       Optional[int]:
										SCREAMING_SNAKE_CASE     =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(*a      )





							def  _UpperCAmelCase  (       self							:						Any      )      ->       Union[str, Any]:
										SCREAMING_SNAKE_CASE     =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_sequence_classification(*a      )





							def  _UpperCAmelCase  (       self							:						List[Any]      )      ->       Dict:
										SCREAMING_SNAKE_CASE     =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_information_extraction(*a      )





							def  _UpperCAmelCase  (       self							:						str      )      ->       List[str]:
										SCREAMING_SNAKE_CASE     =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*a      )







							@slow
							def  _UpperCAmelCase  (       self							:						Any      )      ->       List[str]:







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





@require_torch
class       UpperCAmelCase_   ( unittest.TestCase							):




							'''simple docstring'''



							@slow
							def  _UpperCAmelCase  (       self							:						Tuple      )      ->       str:
										SCREAMING_SNAKE_CASE     =       ErnieMModel.from_pretrained("""susnato/ernie-m-base_pytorch"""      )
										model.eval()
										SCREAMING_SNAKE_CASE     =       torch.tensor([[0, 1, 2, 3, 4, 5]]      )
										SCREAMING_SNAKE_CASE     =       model(a      )[0]

										# TODO Replace vocab size
										SCREAMING_SNAKE_CASE     =       768

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

										SCREAMING_SNAKE_CASE     =       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      )      )













from math import pi




def   _SCREAMING_SNAKE_CASE      (	snake_case_		:						float						):
     return degree / (180 / pi)


if __name__ == "__main__":
    from doctest import testmod

    testmod()