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






      def __init__(      self  ,		__lowerCamelCase  ,		__lowerCamelCase=13  ,		__lowerCamelCase=7  ,		__lowerCamelCase=True  ,		__lowerCamelCase=True  ,		__lowerCamelCase=True  ,		__lowerCamelCase=99  ,		__lowerCamelCase=32  ,		__lowerCamelCase=5  ,		__lowerCamelCase=4  ,		__lowerCamelCase=37  ,		__lowerCamelCase="gelu"  ,		__lowerCamelCase=0.1  ,		__lowerCamelCase=0.1  ,		__lowerCamelCase=5_12  ,		__lowerCamelCase=16  ,		__lowerCamelCase=2  ,		__lowerCamelCase=0.02  ,		__lowerCamelCase=3  ,		__lowerCamelCase=4  ,		__lowerCamelCase=None  ,		)     ->				List[Any]:


          '''simple docstring'''







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






      def 			lowerCAmelCase_   (      self						)     ->				str:


          '''simple docstring'''







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

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

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

          snake_case:						int         =   self.get_config()

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






      def 			lowerCAmelCase_   (      self						)     ->				Dict:


          '''simple docstring'''







          snake_case:						Tuple         =   ids_tensor([self.batch_size, self.seq_length]  ,		self.vocab_size						)

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

          return config, input_ids, input_mask






      def 			lowerCAmelCase_   (      self						)     ->				Tuple:


          '''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 			lowerCAmelCase_   (      self  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase						)     ->				str:


          '''simple docstring'''







          snake_case:						str         =   ErnieMModel(config=lowerCAmelCase__						)
          model.to(lowerCAmelCase__						)
          model.eval()
          snake_case:						List[Any]         =   model(lowerCAmelCase__  ,		return_dict=lowerCAmelCase__						)
          self.parent.assertEqual(result.last_hidden_state.shape  ,		(self.batch_size, self.seq_length, self.hidden_size)						)






      def 			lowerCAmelCase_   (      self  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase						)     ->				Optional[Any]:


          '''simple docstring'''







          snake_case:						Optional[Any]         =   ErnieMForQuestionAnswering(config=lowerCAmelCase__						)
          model.to(lowerCAmelCase__						)
          model.eval()
          snake_case:						List[str]         =   model(
              lowerCAmelCase__  ,		attention_mask=lowerCAmelCase__  ,		start_positions=lowerCAmelCase__  ,		end_positions=lowerCAmelCase__  ,		)
          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  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase						)     ->				List[Any]:


          '''simple docstring'''







          snake_case:						Dict         =   ErnieMForInformationExtraction(config=lowerCAmelCase__						)
          model.to(lowerCAmelCase__						)
          model.eval()
          snake_case:						Any         =   torch.ones_like(lowerCAmelCase__  ,		dtype=torch.floataa						)
          snake_case:						Tuple         =   model(
              lowerCAmelCase__  ,		attention_mask=lowerCAmelCase__  ,		start_positions=lowerCAmelCase__  ,		end_positions=lowerCAmelCase__  ,		)
          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  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase						)     ->				Tuple:


          '''simple docstring'''







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






      def 			lowerCAmelCase_   (      self  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase						)     ->				List[Any]:


          '''simple docstring'''







          snake_case:						Union[str, Any]         =   self.num_labels
          snake_case:						Dict         =   ErnieMForTokenClassification(config=lowerCAmelCase__						)
          model.to(lowerCAmelCase__						)
          model.eval()
          input_ids.to(lowerCAmelCase__						)
          input_mask.to(lowerCAmelCase__						)
          token_labels.to(lowerCAmelCase__						)

          snake_case:						Optional[Any]         =   model(lowerCAmelCase__  ,		attention_mask=lowerCAmelCase__  ,		labels=lowerCAmelCase__						)

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






      def 			lowerCAmelCase_   (      self  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase						)     ->				Optional[int]:


          '''simple docstring'''







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






      def 			lowerCAmelCase_   (      self						)     ->				Optional[int]:


          '''simple docstring'''







          snake_case:						int         =   self.prepare_config_and_inputs()
          (
              snake_case		
          ):						Optional[Any]         =   config_and_inputs
          snake_case:						int         =   {'input_ids': input_ids, 'attention_mask': input_mask}
          return config, inputs_dict





@require_torch
class 				lowerCamelCase			(							__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 			lowerCAmelCase_   (      self  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase  ,		__lowerCamelCase						)     ->				int:


          '''simple docstring'''







          if pipeline_test_casse_name == "QAPipelineTests":
              return True

          return False






      def 			lowerCAmelCase_   (      self						)     ->				List[Any]:


          '''simple docstring'''







          snake_case:						Any         =   ErnieMModelTester(self						)
          snake_case:						List[str]         =   ConfigTester(self  ,		config_class=lowerCAmelCase__  ,		hidden_size=37						)






      def 			lowerCAmelCase_   (      self						)     ->				Tuple:


          '''simple docstring'''







          self.config_tester.run_common_tests()






      def 			lowerCAmelCase_   (      self						)     ->				str:


          '''simple docstring'''







          snake_case:						Union[str, Any]         =   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*lowerCAmelCase__						)






      def 			lowerCAmelCase_   (      self						)     ->				Tuple:


          '''simple docstring'''







          snake_case:						int         =   self.model_tester.prepare_config_and_inputs()
          for type in ["absolute", "relative_key", "relative_key_query"]:
              snake_case:						str         =   type
              self.model_tester.create_and_check_model(*lowerCAmelCase__						)






      def 			lowerCAmelCase_   (      self						)     ->				Tuple:


          '''simple docstring'''







          snake_case:						Optional[Any]         =   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_multiple_choice(*lowerCAmelCase__						)






      def 			lowerCAmelCase_   (      self						)     ->				Dict:


          '''simple docstring'''







          snake_case:						Tuple         =   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase__						)






      def 			lowerCAmelCase_   (      self						)     ->				Optional[Any]:


          '''simple docstring'''







          snake_case:						Any         =   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase__						)






      def 			lowerCAmelCase_   (      self						)     ->				List[str]:


          '''simple docstring'''







          snake_case:						Optional[int]         =   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_information_extraction(*lowerCAmelCase__						)






      def 			lowerCAmelCase_   (      self						)     ->				Optional[Any]:


          '''simple docstring'''







          snake_case:						Optional[int]         =   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase__						)






      @slow
      def 			lowerCAmelCase_   (      self						)     ->				Any:


          '''simple docstring'''







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





@require_torch
class 				lowerCamelCase			(							unittest.TestCase					):






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


          '''simple docstring'''







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

          # TODO Replace vocab size
          snake_case:						Union[str, Any]         =   7_68

          snake_case:						Any         =   torch.Size((1, 6, hidden_size)						)
          self.assertEqual(output.shape  ,		lowerCAmelCase__						)

          snake_case:						str         =   torch.tensor(
              [[[-0.0_012, 0.1_245, -0.0_214], [-0.0_742, 0.0_244, -0.0_771], [-0.0_333, 0.1_164, -0.1_554]]]						)

          self.assertTrue(torch.allclose(output[:, :3, :3]  ,		lowerCAmelCase__  ,		atol=1e-3						)						)






from typing import TYPE_CHECKING

from ...utils import _LazyModule


__lowerCAmelCase									=				{'''tokenization_phobert''': ['''PhobertTokenizer''']}


if TYPE_CHECKING:
 from .tokenization_phobert import PhobertTokenizer

else:
 import sys

 __lowerCAmelCase									=				_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)


