





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		:       Dict		,lowerCamelCase__		:       Any		,lowerCamelCase__		:       Optional[Any]=13		,lowerCamelCase__		:       Optional[int]=7		,lowerCamelCase__		:       Any=True		,lowerCamelCase__		:       Any=True		,lowerCamelCase__		:       Any=True		,lowerCamelCase__		:       Optional[int]=99		,lowerCamelCase__		:       int=32		,lowerCamelCase__		:       Union[str, Any]=5		,lowerCamelCase__		:       Union[str, Any]=4		,lowerCamelCase__		:       Optional[Any]=37		,lowerCamelCase__		:       Dict="gelu"		,lowerCamelCase__		:       Any=0.1		,lowerCamelCase__		:       Union[str, Any]=0.1		,lowerCamelCase__		:       Dict=512		,lowerCamelCase__		:       Dict=16		,lowerCamelCase__		:       List[str]=2		,lowerCamelCase__		:       List[Any]=0.02		,lowerCamelCase__		:       List[Any]=3		,lowerCamelCase__		:       Optional[Any]=4		,lowerCamelCase__		:       Dict=None		,)	->						Optional[Any]:




           '''simple docstring'''


           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 							SCREAMING_SNAKE_CASE__    (				self		:       str			)	->						List[str]:




           '''simple docstring'''


           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 							SCREAMING_SNAKE_CASE__    (				self		:       int			)	->						Optional[Any]:




           '''simple docstring'''


           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 							SCREAMING_SNAKE_CASE__    (				self		:       Union[str, Any]			)	->						Any:




           '''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 							SCREAMING_SNAKE_CASE__    (				self		:       Any		,lowerCamelCase__		:       List[str]		,lowerCamelCase__		:       Any		,lowerCamelCase__		:       List[str]		,lowerCamelCase__		:       Optional[int]		,lowerCamelCase__		:       str		,lowerCamelCase__		:       Any			)	->						Any:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		ErnieMModel(config=lowerCamelCase__			)
           model.to(lowerCamelCase__			)
           model.eval()
           SCREAMING_SNAKE_CASE						=		model(lowerCamelCase__		,return_dict=lowerCamelCase__			)
           self.parent.assertEqual(result.last_hidden_state.shape		,(self.batch_size, self.seq_length, self.hidden_size)			)






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Dict		,lowerCamelCase__		:       Any		,lowerCamelCase__		:       Union[str, Any]		,lowerCamelCase__		:       List[Any]		,lowerCamelCase__		:       Any		,lowerCamelCase__		:       str		,lowerCamelCase__		:       Tuple			)	->						List[str]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		ErnieMForQuestionAnswering(config=lowerCamelCase__			)
           model.to(lowerCamelCase__			)
           model.eval()
           SCREAMING_SNAKE_CASE						=		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 							SCREAMING_SNAKE_CASE__    (				self		:       int		,lowerCamelCase__		:       Any		,lowerCamelCase__		:       Any		,lowerCamelCase__		:       Optional[int]		,lowerCamelCase__		:       List[Any]		,lowerCamelCase__		:       Optional[Any]		,lowerCamelCase__		:       Dict			)	->						int:




           '''simple docstring'''


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




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		self.num_labels
           SCREAMING_SNAKE_CASE						=		ErnieMForSequenceClassification(lowerCamelCase__			)
           model.to(lowerCamelCase__			)
           model.eval()
           SCREAMING_SNAKE_CASE						=		model(lowerCamelCase__		,attention_mask=lowerCamelCase__		,labels=lowerCamelCase__			)
           self.parent.assertEqual(result.logits.shape		,(self.batch_size, self.num_labels)			)






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Optional[Any]		,lowerCamelCase__		:       Union[str, Any]		,lowerCamelCase__		:       Dict		,lowerCamelCase__		:       int		,lowerCamelCase__		:       Optional[int]		,lowerCamelCase__		:       List[Any]		,lowerCamelCase__		:       List[str]			)	->						int:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		self.num_labels
           SCREAMING_SNAKE_CASE						=		ErnieMForTokenClassification(config=lowerCamelCase__			)
           model.to(lowerCamelCase__			)
           model.eval()
           input_ids.to(lowerCamelCase__			)
           input_mask.to(lowerCamelCase__			)
           token_labels.to(lowerCamelCase__			)

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

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






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Any		,lowerCamelCase__		:       Dict		,lowerCamelCase__		:       int		,lowerCamelCase__		:       Any		,lowerCamelCase__		:       Any		,lowerCamelCase__		:       Optional[int]		,lowerCamelCase__		:       Dict			)	->						Any:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		self.num_choices
           SCREAMING_SNAKE_CASE						=		ErnieMForMultipleChoice(config=lowerCamelCase__			)
           model.to(lowerCamelCase__			)
           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(
               lowerCamelCase__		,attention_mask=lowerCamelCase__		,labels=lowerCamelCase__		,)
           self.parent.assertEqual(result.logits.shape		,(self.batch_size, self.num_choices)			)






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Optional[Any]			)	->						Optional[int]:




           '''simple docstring'''


           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__					( lowerCAmelCase_				, lowerCAmelCase_				, unittest.TestCase  ):



     '''simple docstring'''

     __snake_case       :      Dict						      =     (
         (
             ErnieMModel,
             ErnieMForMultipleChoice,
             ErnieMForQuestionAnswering,
             ErnieMForSequenceClassification,
             ErnieMForTokenClassification,
         )
         if is_torch_available()
         else ()
     )
     __snake_case       :      Any						      =     ()
     __snake_case       :      Dict						      =     (
         {
             "feature-extraction": ErnieMModel,
             "question-answering": ErnieMForQuestionAnswering,
             "text-classification": ErnieMForSequenceClassification,
             "token-classification": ErnieMForTokenClassification,
             "zero-shot": ErnieMForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     __snake_case       :      int						      =     False






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Union[str, Any]		,lowerCamelCase__		:       Union[str, Any]		,lowerCamelCase__		:       Dict		,lowerCamelCase__		:       List[Any]		,lowerCamelCase__		:       Tuple		,lowerCamelCase__		:       List[Any]			)	->						Tuple:




           '''simple docstring'''


           if pipeline_test_casse_name == "QAPipelineTests":
                 return True

           return False






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




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		ErnieMModelTester(self			)
           SCREAMING_SNAKE_CASE						=		ConfigTester(self		,config_class=lowerCamelCase__		,hidden_size=37			)






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Tuple			)	->						Optional[Any]:




           '''simple docstring'''


           self.config_tester.run_common_tests()






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Dict			)	->						Any:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*lowerCamelCase__			)






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




           '''simple docstring'''


           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(*lowerCamelCase__			)






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




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_multiple_choice(*lowerCamelCase__			)






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Any			)	->						Optional[int]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(*lowerCamelCase__			)






     def 							SCREAMING_SNAKE_CASE__    (				self		:       List[str]			)	->						Optional[Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase__			)






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Any			)	->						Dict:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_information_extraction(*lowerCamelCase__			)






     def 							SCREAMING_SNAKE_CASE__    (				self		:       int			)	->						Optional[Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(*lowerCamelCase__			)






     @slow
     def 							SCREAMING_SNAKE_CASE__    (				self		:       Optional[int]			)	->						Optional[int]:




           '''simple docstring'''


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

@require_torch
class 						UpperCamelCase__					( unittest.TestCase  ):



     '''simple docstring'''

     @slow
     def 							SCREAMING_SNAKE_CASE__    (				self		:       Dict			)	->						Optional[Any]:




           '''simple docstring'''


           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(lowerCamelCase__			)[0]

           # TODO Replace vocab size
           SCREAMING_SNAKE_CASE						=		768

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

           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]		,lowerCamelCase__		,atol=1e-3			)			)









import os
from string import ascii_letters

SCREAMING_SNAKE_CASE_		  =			ascii_letters + """ \t\n"""



def   __lowercase  ( )					->  dict[str, None]:


      '''simple docstring'''


      SCREAMING_SNAKE_CASE						=		os.path.split(os.path.realpath(_SCREAMING_SNAKE_CASE      )      )
      SCREAMING_SNAKE_CASE						=		{}
      with open(path[0] + """/dictionary.txt"""      ) as dictionary_file:
            for word in dictionary_file.read().split("""\n"""      ):
                  SCREAMING_SNAKE_CASE						=		None
      return english_words


SCREAMING_SNAKE_CASE_		  =			load_dictionary()



def   __lowercase  ( _SCREAMING_SNAKE_CASE      )					->  float:


      '''simple docstring'''


      SCREAMING_SNAKE_CASE						=		message.upper()
      SCREAMING_SNAKE_CASE						=		remove_non_letters(_SCREAMING_SNAKE_CASE      )
      SCREAMING_SNAKE_CASE						=		message.split()
      SCREAMING_SNAKE_CASE						=		len([word for word in possible_words if word in ENGLISH_WORDS]      )
      return float(_SCREAMING_SNAKE_CASE      ) / len(_SCREAMING_SNAKE_CASE      )



def   __lowercase  ( _SCREAMING_SNAKE_CASE      )					->  str:


      '''simple docstring'''


      return "".join(symbol for symbol in message if symbol in LETTERS_AND_SPACE      )



def   __lowercase  ( _SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE = 20					,		_SCREAMING_SNAKE_CASE = 85      )					->  bool:


      '''simple docstring'''


      SCREAMING_SNAKE_CASE						=		get_english_count(_SCREAMING_SNAKE_CASE      ) * 1_00 >= word_percentage
      SCREAMING_SNAKE_CASE						=		len(remove_non_letters(_SCREAMING_SNAKE_CASE      )      )
      SCREAMING_SNAKE_CASE						=		(float(_SCREAMING_SNAKE_CASE      ) / len(_SCREAMING_SNAKE_CASE      )) * 1_00
      SCREAMING_SNAKE_CASE						=		message_letters_percentage >= letter_percentage
      return words_match and letters_match


if __name__ == "__main__":
      import doctest

      doctest.testmod()


