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:						Dict         =   parent
          snake_case:						Optional[int]         =   batch_size
          snake_case:						List[Any]         =   seq_length
          snake_case:						int         =   is_training
          snake_case:						str         =   use_input_mask
          snake_case:						int         =   use_labels
          snake_case:						int         =   vocab_size
          snake_case:						int         =   hidden_size
          snake_case:						str         =   num_hidden_layers
          snake_case:						List[Any]         =   num_attention_heads
          snake_case:						Tuple         =   intermediate_size
          snake_case:						Tuple         =   hidden_act
          snake_case:						List[str]         =   hidden_dropout_prob
          snake_case:						Optional[Any]         =   attention_probs_dropout_prob
          snake_case:						Optional[int]         =   max_position_embeddings
          snake_case:						int         =   type_vocab_size
          snake_case:						Union[str, Any]         =   type_sequence_label_size
          snake_case:						Optional[Any]         =   initializer_range
          snake_case:						Union[str, Any]         =   num_labels
          snake_case:						Dict         =   num_choices
          snake_case:						Any         =   scope






      def 			lowerCAmelCase_   (      self						)     ->				List[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:						str         =   random_attention_mask([self.batch_size, self.seq_length]						)

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

          snake_case:						Any         =   self.get_config()

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






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


          '''simple docstring'''







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

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

          return config, input_ids, input_mask






      def 			lowerCAmelCase_   (      self						)     ->				int:


          '''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						)     ->				Dict:


          '''simple docstring'''







          snake_case:						List[Any]         =   ErnieMModel(config=__lowerCamelCase						)
          model.to(__lowerCamelCase						)
          model.eval()
          snake_case:						int         =   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						)     ->				Tuple:


          '''simple docstring'''







          snake_case:						Optional[Any]         =   ErnieMForQuestionAnswering(config=__lowerCamelCase						)
          model.to(__lowerCamelCase						)
          model.eval()
          snake_case:						Optional[int]         =   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						)     ->				str:


          '''simple docstring'''







          snake_case:						Dict         =   ErnieMForInformationExtraction(config=__lowerCamelCase						)
          model.to(__lowerCamelCase						)
          model.eval()
          snake_case:						List[Any]         =   torch.ones_like(__lowerCamelCase  ,		dtype=torch.floataa						)
          snake_case:						Optional[int]         =   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						)     ->				Union[str, Any]:


          '''simple docstring'''







          snake_case:						Dict         =   self.num_labels
          snake_case:						List[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						)     ->				Optional[Any]:


          '''simple docstring'''







          snake_case:						Tuple         =   self.num_labels
          snake_case:						Any         =   ErnieMForTokenClassification(config=__lowerCamelCase						)
          model.to(__lowerCamelCase						)
          model.eval()
          input_ids.to(__lowerCamelCase						)
          input_mask.to(__lowerCamelCase						)
          token_labels.to(__lowerCamelCase						)

          snake_case:						List[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:						Optional[int]         =   self.num_choices
          snake_case:						Dict         =   ErnieMForMultipleChoice(config=__lowerCamelCase						)
          model.to(__lowerCamelCase						)
          model.eval()
          snake_case:						Dict         =   input_ids.unsqueeze(1						).expand(-1  ,		self.num_choices  ,		-1						).contiguous()
          snake_case:						Optional[Any]         =   input_mask.unsqueeze(1						).expand(-1  ,		self.num_choices  ,		-1						).contiguous()
          snake_case:						Optional[Any]         =   model(
              __lowerCamelCase  ,		attention_mask=__lowerCamelCase  ,		labels=__lowerCamelCase  ,		)
          self.parent.assertEqual(result.logits.shape  ,		(self.batch_size, self.num_choices)						)






      def 			lowerCAmelCase_   (      self						)     ->				Any:


          '''simple docstring'''







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





@require_torch
class 				lowerCamelCase			(							__snake_case				,     __snake_case				,     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						)     ->				Union[str, Any]:


          '''simple docstring'''







          if pipeline_test_casse_name == "QAPipelineTests":
              return True

          return False






      def 			lowerCAmelCase_   (      self						)     ->				str:


          '''simple docstring'''







          snake_case:						str         =   ErnieMModelTester(self						)
          snake_case:						Tuple         =   ConfigTester(self  ,		config_class=__lowerCamelCase  ,		hidden_size=37						)






      def 			lowerCAmelCase_   (      self						)     ->				Union[str, Any]:


          '''simple docstring'''







          self.config_tester.run_common_tests()






      def 			lowerCAmelCase_   (      self						)     ->				str:


          '''simple docstring'''







          snake_case:						List[Any]         =   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*__lowerCamelCase						)






      def 			lowerCAmelCase_   (      self						)     ->				Union[str, Any]:


          '''simple docstring'''







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






      def 			lowerCAmelCase_   (      self						)     ->				Dict:


          '''simple docstring'''







          snake_case:						int         =   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_multiple_choice(*__lowerCamelCase						)






      def 			lowerCAmelCase_   (      self						)     ->				Union[str, Any]:


          '''simple docstring'''







          snake_case:						Dict         =   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_question_answering(*__lowerCamelCase						)






      def 			lowerCAmelCase_   (      self						)     ->				str:


          '''simple docstring'''







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






      def 			lowerCAmelCase_   (      self						)     ->				Tuple:


          '''simple docstring'''







          snake_case:						Any         =   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_information_extraction(*__lowerCamelCase						)






      def 			lowerCAmelCase_   (      self						)     ->				Dict:


          '''simple docstring'''







          snake_case:						Optional[Any]         =   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_token_classification(*__lowerCamelCase						)






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


          '''simple docstring'''







          for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              snake_case:						List[Any]         =   ErnieMModel.from_pretrained(__lowerCamelCase						)
              self.assertIsNotNone(__lowerCamelCase						)





@require_torch
class 				lowerCamelCase			(							unittest.TestCase					):






      @slow
      def 			lowerCAmelCase_   (      self						)     ->				Union[str, Any]:


          '''simple docstring'''







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

          # TODO Replace vocab size
          snake_case:						Optional[int]         =   7_68

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

          snake_case:						Dict         =   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						)						)






import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils import AddedToken
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_mpnet import MPNetTokenizer


UpperCAmelCase__        =				logging.get_logger(__name__)

UpperCAmelCase__        =				{'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''}

UpperCAmelCase__        =				{
    '''vocab_file''': {
        '''microsoft/mpnet-base''': '''https://huggingface.co/microsoft/mpnet-base/resolve/main/vocab.txt''',
    },
    '''tokenizer_file''': {
        '''microsoft/mpnet-base''': '''https://huggingface.co/microsoft/mpnet-base/resolve/main/tokenizer.json''',
    },
}

UpperCAmelCase__        =				{
    '''microsoft/mpnet-base''': 512,
}

UpperCAmelCase__        =				{
    '''microsoft/mpnet-base''': {'''do_lower_case''': True},
}







class  lowerCamelCase__  (			lowerCAmelCase):
     SCREAMING_SNAKE_CASE__      =     VOCAB_FILES_NAMES
     SCREAMING_SNAKE_CASE__      =     PRETRAINED_VOCAB_FILES_MAP
     SCREAMING_SNAKE_CASE__      =     PRETRAINED_INIT_CONFIGURATION
     SCREAMING_SNAKE_CASE__      =     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
     SCREAMING_SNAKE_CASE__      =     MPNetTokenizer
     SCREAMING_SNAKE_CASE__      =     ['''input_ids''', '''attention_mask''']





     def __init__(self						,	UpperCAmelCase=None						,	UpperCAmelCase=None						,	UpperCAmelCase=True						,	UpperCAmelCase="<s>"						,	UpperCAmelCase="</s>"						,	UpperCAmelCase="</s>"						,	UpperCAmelCase="<s>"						,	UpperCAmelCase="[UNK]"						,	UpperCAmelCase="<pad>"						,	UpperCAmelCase="<mask>"						,	UpperCAmelCase=True						,	UpperCAmelCase=None						,	**UpperCAmelCase						,	)	->  Optional[int]:
      super().__init__(
          UpperCAmelCase						,	tokenizer_file=UpperCAmelCase						,	do_lower_case=UpperCAmelCase						,	bos_token=UpperCAmelCase						,	eos_token=UpperCAmelCase						,	sep_token=UpperCAmelCase						,	cls_token=UpperCAmelCase						,	unk_token=UpperCAmelCase						,	pad_token=UpperCAmelCase						,	mask_token=UpperCAmelCase						,	tokenize_chinese_chars=UpperCAmelCase						,	strip_accents=UpperCAmelCase						,	**UpperCAmelCase						,	)

      _lowercase				 =json.loads(self.backend_tokenizer.normalizer.__getstate__()			)
      if (
          pre_tok_state.get('''lowercase'''						,	UpperCAmelCase			) != do_lower_case
          or pre_tok_state.get('''strip_accents'''						,	UpperCAmelCase			) != strip_accents
      ):
       _lowercase				 =getattr(UpperCAmelCase						,	pre_tok_state.pop('''type'''			)			)
       _lowercase				 =do_lower_case
       _lowercase				 =strip_accents
       _lowercase				 =pre_tok_class(**UpperCAmelCase			)

      _lowercase				 =do_lower_case





     @property
     def 	__A   (self			)	->  str:
      if self._mask_token is None:
       if self.verbose:
        logger.error('''Using mask_token, but it is not set yet.'''			)
       return None
      return str(self._mask_token			)





     @mask_token.setter
     def 	__A   (self						,	UpperCAmelCase			)	->  Optional[int]:
      _lowercase				 =AddedToken(UpperCAmelCase						,	lstrip=UpperCAmelCase						,	rstrip=UpperCAmelCase			) if isinstance(UpperCAmelCase						,	UpperCAmelCase			) else value
      _lowercase				 =value





     def 	__A   (self						,	UpperCAmelCase						,	UpperCAmelCase=None			)	->  List[str]:
      _lowercase				 =[self.bos_token_id] + token_ids_a + [self.eos_token_id]
      if token_ids_a is None:
       return output

      return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id]





     def 	__A   (self						,	UpperCAmelCase						,	UpperCAmelCase = None			)	->  List[int]:
      _lowercase				 =[self.sep_token_id]
      _lowercase				 =[self.cls_token_id]

      if token_ids_a is None:
       return len(cls + token_ids_a + sep			) * [0]
      return len(cls + token_ids_a + sep + sep + token_ids_a + sep			) * [0]





     def 	__A   (self						,	UpperCAmelCase						,	UpperCAmelCase = None			)	->  Tuple[str]:
      _lowercase				 =self._tokenizer.model.save(UpperCAmelCase						,	name=UpperCAmelCase			)
      return tuple(UpperCAmelCase			)

