
'''simple docstring'''





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






 def __init__(      self	:Optional[int]						,					_A	:Tuple						,					_A	:str=13						,					_A	:List[Any]=7						,					_A	:str=True						,					_A	:str=True						,					_A	:Tuple=True						,					_A	:Tuple=99						,					_A	:List[Any]=32						,					_A	:Dict=5						,					_A	:str=4						,					_A	:str=37						,					_A	:Optional[int]="gelu"						,					_A	:Dict=0.1						,					_A	:Tuple=0.1						,					_A	:Any=512						,					_A	:Dict=16						,					_A	:List[Any]=2						,					_A	:Any=0.02						,					_A	:Optional[int]=3						,					_A	:Dict=4						,					_A	:Optional[Any]=None						,					)					->      Any:







       '''simple docstring'''



       __A													=      parent
       __A													=      batch_size
       __A													=      seq_length
       __A													=      is_training
       __A													=      use_input_mask
       __A													=      use_labels
       __A													=      vocab_size
       __A													=      hidden_size
       __A													=      num_hidden_layers
       __A													=      num_attention_heads
       __A													=      intermediate_size
       __A													=      hidden_act
       __A													=      hidden_dropout_prob
       __A													=      attention_probs_dropout_prob
       __A													=      max_position_embeddings
       __A													=      type_vocab_size
       __A													=      type_sequence_label_size
       __A													=      initializer_range
       __A													=      num_labels
       __A													=      num_choices
       __A													=      scope






 def 						lowercase_    (      self	:List[str]      )					->      Optional[int]:







       '''simple docstring'''



       __A													=      ids_tensor([self.batch_size, self.seq_length]						,					self.vocab_size      )

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

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

       __A													=      self.get_config()

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






 def 						lowercase_    (      self	:int      )					->      str:







       '''simple docstring'''



       __A													=      ids_tensor([self.batch_size, self.seq_length]						,					self.vocab_size      )

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

       return config, input_ids, input_mask






 def 						lowercase_    (      self	:Dict      )					->      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 						lowercase_    (      self	:str						,					_A	:List[Any]						,					_A	:Tuple						,					_A	:Tuple						,					_A	:Dict						,					_A	:List[str]						,					_A	:Union[str, Any]      )					->      int:







       '''simple docstring'''



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






 def 						lowercase_    (      self	:int						,					_A	:str						,					_A	:List[Any]						,					_A	:Dict						,					_A	:str						,					_A	:int						,					_A	:List[Any]      )					->      Tuple:







       '''simple docstring'''



       __A													=      ErnieMForQuestionAnswering(config=_A      )
       model.to(_A      )
       model.eval()
       __A													=      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 						lowercase_    (      self	:Union[str, Any]						,					_A	:str						,					_A	:Optional[int]						,					_A	:int						,					_A	:Union[str, Any]						,					_A	:List[Any]						,					_A	:List[str]      )					->      List[Any]:







       '''simple docstring'''



       __A													=      ErnieMForInformationExtraction(config=_A      )
       model.to(_A      )
       model.eval()
       __A													=      torch.ones_like(_A						,					dtype=torch.floataa      )
       __A													=      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 						lowercase_    (      self	:Tuple						,					_A	:Tuple						,					_A	:Dict						,					_A	:List[Any]						,					_A	:str						,					_A	:List[str]						,					_A	:str      )					->      Tuple:







       '''simple docstring'''



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






 def 						lowercase_    (      self	:Any						,					_A	:int						,					_A	:Dict						,					_A	:Dict						,					_A	:Union[str, Any]						,					_A	:Dict						,					_A	:Optional[int]      )					->      Optional[Any]:







       '''simple docstring'''



       __A													=      self.num_labels
       __A													=      ErnieMForTokenClassification(config=_A      )
       model.to(_A      )
       model.eval()
       input_ids.to(_A      )
       input_mask.to(_A      )
       token_labels.to(_A      )

       __A													=      model(_A						,					attention_mask=_A						,					labels=_A      )

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






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







       '''simple docstring'''



       __A													=      self.num_choices
       __A													=      ErnieMForMultipleChoice(config=_A      )
       model.to(_A      )
       model.eval()
       __A													=      input_ids.unsqueeze(1      ).expand(-1						,					self.num_choices						,					-1      ).contiguous()
       __A													=      input_mask.unsqueeze(1      ).expand(-1						,					self.num_choices						,					-1      ).contiguous()
       __A													=      model(
           _A						,					attention_mask=_A						,					labels=_A						,					)
       self.parent.assertEqual(result.logits.shape						,					(self.batch_size, self.num_choices)      )






 def 						lowercase_    (      self	:Tuple      )					->      Dict:







       '''simple docstring'''



       __A													=      self.prepare_config_and_inputs()
       (
           (
           __A
       )						,   (
           __A
       )						,   (
           __A
       )						,   (
           __A
       )						,   (
           __A
       )						,   (
           __A
       )						,   
       )													=      config_and_inputs
       __A													=      {'input_ids': input_ids, 'attention_mask': input_mask}
       return config, inputs_dict


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






 def 						lowercase_    (      self	:str						,					_A	:Optional[int]						,					_A	:int						,					_A	:int						,					_A	:Tuple						,					_A	:Union[str, Any]      )					->      List[Any]:







       '''simple docstring'''



       if pipeline_test_casse_name == "QAPipelineTests":
             return True

       return False






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







       '''simple docstring'''



       __A													=      ErnieMModelTester(self      )
       __A													=      ConfigTester(self						,					config_class=_A						,					hidden_size=37      )






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







       '''simple docstring'''



       self.config_tester.run_common_tests()






 def 						lowercase_    (      self	:int      )					->      Tuple:







       '''simple docstring'''



       __A													=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_A      )






 def 						lowercase_    (      self	:int      )					->      List[str]:







       '''simple docstring'''



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






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







       '''simple docstring'''



       __A													=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_multiple_choice(*_A      )






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







       '''simple docstring'''



       __A													=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*_A      )






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







       '''simple docstring'''



       __A													=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*_A      )






 def 						lowercase_    (      self	:Any      )					->      Dict:







       '''simple docstring'''



       __A													=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_information_extraction(*_A      )






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







       '''simple docstring'''



       __A													=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(*_A      )






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







       '''simple docstring'''



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


@require_torch
class 			UpperCamelCase__	(     unittest.TestCase):






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







       '''simple docstring'''



       __A													=      ErnieMModel.from_pretrained('susnato/ernie-m-base_pytorch'      )
       model.eval()
       __A													=      torch.tensor([[0, 1, 2, 3, 4, 5]]      )
       __A													=      model(_A      )[0]

       # TODO Replace vocab size
       __A													=      768

       __A													=      torch.Size((1, 6, hidden_size)      )
       self.assertEqual(output.shape						,					_A      )

       __A													=      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]						,					_A						,					atol=1E-3      )      )








import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto.configuration_auto import AutoConfig


__UpperCAmelCase   						=     logging.get_logger(__name__)





class 				UpperCamelCase__		(	__SCREAMING_SNAKE_CASE	):






       """simple docstring"""


       UpperCAmelCase_				       ="speech-encoder-decoder"
       UpperCAmelCase_				       =True



       def __init__(							self ,     **_A	)				->				Dict:
          super().__init__(**_A	)
          if "encoder" not in kwargs or "decoder" not in kwargs:
             raise ValueError(
                 F'''A configuraton of type {self.model_type} cannot be instantiated because not both `encoder` and'''
                 F''' `decoder` sub-configurations are passed, but only {kwargs}'''	)

          SCREAMING_SNAKE_CASE_	=      kwargs.pop('''encoder'''	)
          SCREAMING_SNAKE_CASE_	=      encoder_config.pop('''model_type'''	)
          SCREAMING_SNAKE_CASE_	=      kwargs.pop('''decoder'''	)
          SCREAMING_SNAKE_CASE_	=      decoder_config.pop('''model_type'''	)

          SCREAMING_SNAKE_CASE_	=      AutoConfig.for_model(_A ,     **_A	)
          SCREAMING_SNAKE_CASE_	=      AutoConfig.for_model(_A ,     **_A	)
          SCREAMING_SNAKE_CASE_	=      True



       @classmethod
       def     _UpperCamelCase	(							cls ,     _A ,     _A ,     **_A	)				->				PretrainedConfig:
          logger.info('''Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config'''	)
          SCREAMING_SNAKE_CASE_	=      True
          SCREAMING_SNAKE_CASE_	=      True

          return cls(encoder=encoder_config.to_dict() ,     decoder=decoder_config.to_dict() ,     **_A	)




       def     _UpperCamelCase	(							self	)				->				Union[str, Any]:
          SCREAMING_SNAKE_CASE_	=      copy.deepcopy(self.__dict__	)
          SCREAMING_SNAKE_CASE_	=      self.encoder.to_dict()
          SCREAMING_SNAKE_CASE_	=      self.decoder.to_dict()
          SCREAMING_SNAKE_CASE_	=      self.__class__.model_type
          return output


