


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


      def __init__(							self							,						SCREAMING_SNAKE_CASE__							,						SCREAMING_SNAKE_CASE__=13							,						SCREAMING_SNAKE_CASE__=7							,						SCREAMING_SNAKE_CASE__=True							,						SCREAMING_SNAKE_CASE__=True							,						SCREAMING_SNAKE_CASE__=True							,						SCREAMING_SNAKE_CASE__=99							,						SCREAMING_SNAKE_CASE__=32							,						SCREAMING_SNAKE_CASE__=5							,						SCREAMING_SNAKE_CASE__=4							,						SCREAMING_SNAKE_CASE__=37							,						SCREAMING_SNAKE_CASE__="gelu"							,						SCREAMING_SNAKE_CASE__=0.1							,						SCREAMING_SNAKE_CASE__=0.1							,						SCREAMING_SNAKE_CASE__=512							,						SCREAMING_SNAKE_CASE__=16							,						SCREAMING_SNAKE_CASE__=2							,						SCREAMING_SNAKE_CASE__=0.02							,						SCREAMING_SNAKE_CASE__=3							,						SCREAMING_SNAKE_CASE__=4							,						SCREAMING_SNAKE_CASE__=None							,						):
            lowercase			:   Tuple					   =		parent
            lowercase			:   List[str]					   =		batch_size
            lowercase			:   Tuple					   =		seq_length
            lowercase			:   int					   =		is_training
            lowercase			:   Dict					   =		use_input_mask
            lowercase			:   Any					   =		use_labels
            lowercase			:   Dict					   =		vocab_size
            lowercase			:   Optional[int]					   =		hidden_size
            lowercase			:   List[Any]					   =		num_hidden_layers
            lowercase			:   List[Any]					   =		num_attention_heads
            lowercase			:   List[Any]					   =		intermediate_size
            lowercase			:   Union[str, Any]					   =		hidden_act
            lowercase			:   List[str]					   =		hidden_dropout_prob
            lowercase			:   Tuple					   =		attention_probs_dropout_prob
            lowercase			:   Tuple					   =		max_position_embeddings
            lowercase			:   Any					   =		type_vocab_size
            lowercase			:   Optional[Any]					   =		type_sequence_label_size
            lowercase			:   Tuple					   =		initializer_range
            lowercase			:   Tuple					   =		num_labels
            lowercase			:   str					   =		num_choices
            lowercase			:   Union[str, Any]					   =		scope


      def        __lowerCamelCase			(							self		):
            lowercase			:   Dict					   =		ids_tensor([self.batch_size, self.seq_length]							,						self.vocab_size		)

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

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

            lowercase			:   Optional[Any]					   =		self.get_config()

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


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

            lowercase			:   Any					   =		None
            if self.use_input_mask:
                  lowercase			:   Tuple					   =		random_attention_mask([self.batch_size, self.seq_length]		)
            lowercase			:   Dict					   =		self.get_config()

            return config, input_ids, input_mask


      def        __lowerCamelCase			(							self		):
            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        __lowerCamelCase			(							self							,						SCREAMING_SNAKE_CASE__							,						SCREAMING_SNAKE_CASE__							,						SCREAMING_SNAKE_CASE__							,						SCREAMING_SNAKE_CASE__							,						SCREAMING_SNAKE_CASE__							,						SCREAMING_SNAKE_CASE__		):
            lowercase			:   Any					   =		ErnieMModel(config=SCREAMING_SNAKE_CASE__		)
            model.to(SCREAMING_SNAKE_CASE__		)
            model.eval()
            lowercase			:   str					   =		model(SCREAMING_SNAKE_CASE__							,						return_dict=SCREAMING_SNAKE_CASE__		)
            self.parent.assertEqual(result.last_hidden_state.shape							,						(self.batch_size, self.seq_length, self.hidden_size)		)


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


      def        __lowerCamelCase			(							self							,						SCREAMING_SNAKE_CASE__							,						SCREAMING_SNAKE_CASE__							,						SCREAMING_SNAKE_CASE__							,						SCREAMING_SNAKE_CASE__							,						SCREAMING_SNAKE_CASE__							,						SCREAMING_SNAKE_CASE__		):
            lowercase			:   List[Any]					   =		self.num_labels
            lowercase			:   List[str]					   =		ErnieMForTokenClassification(config=SCREAMING_SNAKE_CASE__		)
            model.to(SCREAMING_SNAKE_CASE__		)
            model.eval()
            input_ids.to(SCREAMING_SNAKE_CASE__		)
            input_mask.to(SCREAMING_SNAKE_CASE__		)
            token_labels.to(SCREAMING_SNAKE_CASE__		)

            lowercase			:   Any					   =		model(SCREAMING_SNAKE_CASE__							,						attention_mask=SCREAMING_SNAKE_CASE__							,						labels=SCREAMING_SNAKE_CASE__		)

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


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






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







@require_torch
class 				__SCREAMING_SNAKE_CASE   (			A__  ,						A__  ,						unittest.TestCase   ):
      A				:							Tuple    							=     (
          (
              ErnieMModel,
              ErnieMForMultipleChoice,
              ErnieMForQuestionAnswering,
              ErnieMForSequenceClassification,
              ErnieMForTokenClassification,
          )
          if is_torch_available()
          else ()
      )
      A				:							Dict    							=     ()
      A				:							Optional[Any]    							=     (
          {
              'feature-extraction': ErnieMModel,
              'question-answering': ErnieMForQuestionAnswering,
              'text-classification': ErnieMForSequenceClassification,
              'token-classification': ErnieMForTokenClassification,
              'zero-shot': ErnieMForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      A				:							List[str]    							=     False


      def        __lowerCamelCase			(							self							,						SCREAMING_SNAKE_CASE__							,						SCREAMING_SNAKE_CASE__							,						SCREAMING_SNAKE_CASE__							,						SCREAMING_SNAKE_CASE__							,						SCREAMING_SNAKE_CASE__		):
            if pipeline_test_casse_name == "QAPipelineTests":
                  return True

            return False


      def        __lowerCamelCase			(							self		):
            lowercase			:   List[Any]					   =		ErnieMModelTester(self		)
            lowercase			:   Optional[Any]					   =		ConfigTester(self							,						config_class=SCREAMING_SNAKE_CASE__							,						hidden_size=37		)


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


      def        __lowerCamelCase			(							self		):
            lowercase			:   List[str]					   =		self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__		)


      def        __lowerCamelCase			(							self		):
            lowercase			:   Any					   =		self.model_tester.prepare_config_and_inputs()
            for type in ["absolute", "relative_key", "relative_key_query"]:
                  lowercase			:   str					   =		type
                  self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__		)


      def        __lowerCamelCase			(							self		):
            lowercase			:   int					   =		self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_multiple_choice(*SCREAMING_SNAKE_CASE__		)


      def        __lowerCamelCase			(							self		):
            lowercase			:   Any					   =		self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_question_answering(*SCREAMING_SNAKE_CASE__		)


      def        __lowerCamelCase			(							self		):
            lowercase			:   List[Any]					   =		self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_sequence_classification(*SCREAMING_SNAKE_CASE__		)


      def        __lowerCamelCase			(							self		):
            lowercase			:   int					   =		self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_information_extraction(*SCREAMING_SNAKE_CASE__		)


      def        __lowerCamelCase			(							self		):
            lowercase			:   str					   =		self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_token_classification(*SCREAMING_SNAKE_CASE__		)






      @slow
      def        __lowerCamelCase			(							self		):
            for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  lowercase			:   Optional[Any]					   =		ErnieMModel.from_pretrained(SCREAMING_SNAKE_CASE__		)
                  self.assertIsNotNone(SCREAMING_SNAKE_CASE__		)







@require_torch
class 				__SCREAMING_SNAKE_CASE   (			unittest.TestCase   ):


      @slow
      def        __lowerCamelCase			(							self		):
            lowercase			:   Optional[int]					   =		ErnieMModel.from_pretrained('''susnato/ernie-m-base_pytorch'''		)
            model.eval()
            lowercase			:   int					   =		torch.tensor([[0, 1, 2, 3, 4, 5]]		)
            lowercase			:   int					   =		model(SCREAMING_SNAKE_CASE__		)[0]

            # TODO Replace vocab size
            lowercase			:   Tuple					   =		768

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

            lowercase			:   List[Any]					   =		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]							,						SCREAMING_SNAKE_CASE__							,						atol=1E-3		)		)





import numpy as np




def 			__lowercase	(						_UpperCamelCase,    _UpperCamelCase     )    ->Dict:







      """simple docstring"""

      lowercase			:   Optional[Any]					   =		np.array(_UpperCamelCase     )
      lowercase			:   Dict					   =		np.array(_UpperCamelCase     )

      lowercase			:   List[Any]					   =		abs(predict - actual     )
      lowercase			:   int					   =		difference.mean()

      return score




def 			__lowercase	(						_UpperCamelCase,    _UpperCamelCase     )    ->List[Any]:







      """simple docstring"""

      lowercase			:   Tuple					   =		np.array(_UpperCamelCase     )
      lowercase			:   str					   =		np.array(_UpperCamelCase     )

      lowercase			:   List[str]					   =		predict - actual
      lowercase			:   Tuple					   =		np.square(_UpperCamelCase     )

      lowercase			:   Union[str, Any]					   =		square_diff.mean()
      return score




def 			__lowercase	(						_UpperCamelCase,    _UpperCamelCase     )    ->str:







      """simple docstring"""

      lowercase			:   Union[str, Any]					   =		np.array(_UpperCamelCase     )
      lowercase			:   Optional[Any]					   =		np.array(_UpperCamelCase     )

      lowercase			:   Tuple					   =		predict - actual
      lowercase			:   Dict					   =		np.square(_UpperCamelCase     )
      lowercase			:   Union[str, Any]					   =		square_diff.mean()
      lowercase			:   Union[str, Any]					   =		np.sqrt(_UpperCamelCase     )
      return score




def 			__lowercase	(						_UpperCamelCase,    _UpperCamelCase     )    ->List[Any]:







      """simple docstring"""

      lowercase			:   List[str]					   =		np.array(_UpperCamelCase     )
      lowercase			:   List[Any]					   =		np.array(_UpperCamelCase     )

      lowercase			:   Dict					   =		np.log(predict + 1     )
      lowercase			:   List[str]					   =		np.log(actual + 1     )

      lowercase			:   int					   =		log_predict - log_actual
      lowercase			:   Tuple					   =		np.square(_UpperCamelCase     )
      lowercase			:   List[Any]					   =		square_diff.mean()

      lowercase			:   int					   =		np.sqrt(_UpperCamelCase     )

      return score




def 			__lowercase	(						_UpperCamelCase,    _UpperCamelCase     )    ->List[str]:







      """simple docstring"""

      lowercase			:   List[Any]					   =		np.array(_UpperCamelCase     )
      lowercase			:   Optional[int]					   =		np.array(_UpperCamelCase     )

      lowercase			:   Optional[int]					   =		predict - actual
      lowercase			:   Optional[int]					   =		np.sum(_UpperCamelCase     ) / len(_UpperCamelCase     )
      lowercase			:   Dict					   =		np.sum(_UpperCamelCase     ) / len(_UpperCamelCase     )
      # print(numerator, denumerator)
      lowercase			:   int					   =		float(_UpperCamelCase     ) / denumerator * 100

      return score




def 			__lowercase	(						_UpperCamelCase,    _UpperCamelCase     )    ->List[Any]:







      """simple docstring"""

      return np.mean(np.array(_UpperCamelCase     ) == np.array(_UpperCamelCase     )     )

