





'''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        __a   :

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







													"""simple docstring"""
													UpperCAmelCase_      :   Any      =	parent
													UpperCAmelCase_      :   Optional[Any]      =	batch_size
													UpperCAmelCase_      :   Optional[int]      =	seq_length
													UpperCAmelCase_      :   Any      =	is_training
													UpperCAmelCase_      :   Optional[int]      =	use_input_mask
													UpperCAmelCase_      :   List[str]      =	use_labels
													UpperCAmelCase_      :   Optional[int]      =	vocab_size
													UpperCAmelCase_      :   List[Any]      =	hidden_size
													UpperCAmelCase_      :   str      =	num_hidden_layers
													UpperCAmelCase_      :   str      =	num_attention_heads
													UpperCAmelCase_      :   List[Any]      =	intermediate_size
													UpperCAmelCase_      :   Tuple      =	hidden_act
													UpperCAmelCase_      :   Tuple      =	hidden_dropout_prob
													UpperCAmelCase_      :   Optional[Any]      =	attention_probs_dropout_prob
													UpperCAmelCase_      :   Optional[Any]      =	max_position_embeddings
													UpperCAmelCase_      :   Optional[int]      =	type_vocab_size
													UpperCAmelCase_      :   Optional[Any]      =	type_sequence_label_size
													UpperCAmelCase_      :   Union[str, Any]      =	initializer_range
													UpperCAmelCase_      :   int      =	num_labels
													UpperCAmelCase_      :   int      =	num_choices
													UpperCAmelCase_      :   List[str]      =	scope

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







													"""simple docstring"""
													UpperCAmelCase_      :   str      =	ids_tensor([self.batch_size, self.seq_length]							,  self.vocab_size )

													UpperCAmelCase_      :   Tuple      =	None
													if self.use_input_mask:
																			UpperCAmelCase_      :   List[str]      =	random_attention_mask([self.batch_size, self.seq_length] )

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

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

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

							def      UpperCAmelCase__						(				self				:  List[Any] )       ->							Dict:







													"""simple docstring"""
													UpperCAmelCase_      :   Optional[int]      =	ids_tensor([self.batch_size, self.seq_length]							,  self.vocab_size )

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

													return config, input_ids, input_mask

							def      UpperCAmelCase__						(				self				:  Optional[int] )       ->							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      UpperCAmelCase__						(				self				:  Optional[int]							,  __magic_name__				:  Tuple							,  __magic_name__				:  Optional[int]							,  __magic_name__				:  int							,  __magic_name__				:  Dict							,  __magic_name__				:  Tuple							,  __magic_name__				:  Optional[Any] )       ->							Any:







													"""simple docstring"""
													UpperCAmelCase_      :   Tuple      =	ErnieMModel(config=__magic_name__ )
													model.to(__magic_name__ )
													model.eval()
													UpperCAmelCase_      :   Any      =	model(__magic_name__							,  return_dict=__magic_name__ )
													self.parent.assertEqual(result.last_hidden_state.shape							,  (self.batch_size, self.seq_length, self.hidden_size) )

							def      UpperCAmelCase__						(				self				:  Optional[int]							,  __magic_name__				:  int							,  __magic_name__				:  Optional[Any]							,  __magic_name__				:  str							,  __magic_name__				:  str							,  __magic_name__				:  List[str]							,  __magic_name__				:  Any )       ->							Union[str, Any]:







													"""simple docstring"""
													UpperCAmelCase_      :   int      =	ErnieMForQuestionAnswering(config=__magic_name__ )
													model.to(__magic_name__ )
													model.eval()
													UpperCAmelCase_      :   Optional[Any]      =	model(
													    __magic_name__							,  attention_mask=__magic_name__							,  start_positions=__magic_name__							,  end_positions=__magic_name__							,  )
													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      UpperCAmelCase__						(				self				:  str							,  __magic_name__				:  Any							,  __magic_name__				:  Tuple							,  __magic_name__				:  Union[str, Any]							,  __magic_name__				:  List[Any]							,  __magic_name__				:  str							,  __magic_name__				:  str )       ->							str:







													"""simple docstring"""
													UpperCAmelCase_      :   Optional[Any]      =	ErnieMForInformationExtraction(config=__magic_name__ )
													model.to(__magic_name__ )
													model.eval()
													UpperCAmelCase_      :   Optional[Any]      =	torch.ones_like(__magic_name__							,  dtype=torch.floataa )
													UpperCAmelCase_      :   List[Any]      =	model(
													    __magic_name__							,  attention_mask=__magic_name__							,  start_positions=__magic_name__							,  end_positions=__magic_name__							,  )
													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      UpperCAmelCase__						(				self				:  str							,  __magic_name__				:  Union[str, Any]							,  __magic_name__				:  Optional[Any]							,  __magic_name__				:  Tuple							,  __magic_name__				:  int							,  __magic_name__				:  Optional[int]							,  __magic_name__				:  str )       ->							List[str]:







													"""simple docstring"""
													UpperCAmelCase_      :   Optional[Any]      =	self.num_labels
													UpperCAmelCase_      :   int      =	ErnieMForSequenceClassification(__magic_name__ )
													model.to(__magic_name__ )
													model.eval()
													UpperCAmelCase_      :   Any      =	model(__magic_name__							,  attention_mask=__magic_name__							,  labels=__magic_name__ )
													self.parent.assertEqual(result.logits.shape							,  (self.batch_size, self.num_labels) )

							def      UpperCAmelCase__						(				self				:  Dict							,  __magic_name__				:  int							,  __magic_name__				:  Any							,  __magic_name__				:  Dict							,  __magic_name__				:  int							,  __magic_name__				:  str							,  __magic_name__				:  Any )       ->							Dict:







													"""simple docstring"""
													UpperCAmelCase_      :   Union[str, Any]      =	self.num_labels
													UpperCAmelCase_      :   str      =	ErnieMForTokenClassification(config=__magic_name__ )
													model.to(__magic_name__ )
													model.eval()
													input_ids.to(__magic_name__ )
													input_mask.to(__magic_name__ )
													token_labels.to(__magic_name__ )

													UpperCAmelCase_      :   Tuple      =	model(__magic_name__							,  attention_mask=__magic_name__							,  labels=__magic_name__ )

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

							def      UpperCAmelCase__						(				self				:  int							,  __magic_name__				:  Any							,  __magic_name__				:  Optional[Any]							,  __magic_name__				:  Optional[int]							,  __magic_name__				:  Tuple							,  __magic_name__				:  Any							,  __magic_name__				:  Optional[Any] )       ->							Optional[Any]:







													"""simple docstring"""
													UpperCAmelCase_      :   Optional[int]      =	self.num_choices
													UpperCAmelCase_      :   str      =	ErnieMForMultipleChoice(config=__magic_name__ )
													model.to(__magic_name__ )
													model.eval()
													UpperCAmelCase_      :   List[str]      =	input_ids.unsqueeze(1 ).expand(-1							,  self.num_choices							,  -1 ).contiguous()
													UpperCAmelCase_      :   Tuple      =	input_mask.unsqueeze(1 ).expand(-1							,  self.num_choices							,  -1 ).contiguous()
													UpperCAmelCase_      :   Tuple      =	model(
													    __magic_name__							,  attention_mask=__magic_name__							,  labels=__magic_name__							,  )
													self.parent.assertEqual(result.logits.shape							,  (self.batch_size, self.num_choices) )



							def      UpperCAmelCase__						(				self				:  Tuple )       ->							Any:







													"""simple docstring"""
													UpperCAmelCase_      :   Tuple      =	self.prepare_config_and_inputs()
													(
													    (
													    UpperCAmelCase_
													)    ,							(
													    UpperCAmelCase_
													)    ,							(
													    UpperCAmelCase_
													)    ,							(
													    UpperCAmelCase_
													)    ,							(
													    UpperCAmelCase_
													)    ,							(
													    UpperCAmelCase_
													)    ,							
													)      :   Optional[int]      =	config_and_inputs
													UpperCAmelCase_      :   Dict      =	{'''input_ids''': input_ids, '''attention_mask''': input_mask}
													return config, inputs_dict





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

							def      UpperCAmelCase__						(				self				:  str							,  __magic_name__				:  List[Any]							,  __magic_name__				:  int							,  __magic_name__				:  str							,  __magic_name__				:  Any							,  __magic_name__				:  Tuple )       ->							Tuple:







													"""simple docstring"""
													if pipeline_test_casse_name == "QAPipelineTests":
																			return True

													return False

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







													"""simple docstring"""
													UpperCAmelCase_      :   List[str]      =	ErnieMModelTester(self )
													UpperCAmelCase_      :   Tuple      =	ConfigTester(self							,  config_class=__magic_name__							,  hidden_size=37 )

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







													"""simple docstring"""
													self.config_tester.run_common_tests()

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







													"""simple docstring"""
													UpperCAmelCase_      :   Optional[Any]      =	self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*__magic_name__ )

							def      UpperCAmelCase__						(				self				:  int )       ->							Tuple:







													"""simple docstring"""
													UpperCAmelCase_      :   Union[str, Any]      =	self.model_tester.prepare_config_and_inputs()
													for type in ["absolute", "relative_key", "relative_key_query"]:
																			UpperCAmelCase_      :   List[str]      =	type
																			self.model_tester.create_and_check_model(*__magic_name__ )

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







													"""simple docstring"""
													UpperCAmelCase_      :   Any      =	self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_multiple_choice(*__magic_name__ )

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







													"""simple docstring"""
													UpperCAmelCase_      :   Any      =	self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_question_answering(*__magic_name__ )

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







													"""simple docstring"""
													UpperCAmelCase_      :   Optional[int]      =	self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_sequence_classification(*__magic_name__ )

							def      UpperCAmelCase__						(				self				:  Dict )       ->							List[str]:







													"""simple docstring"""
													UpperCAmelCase_      :   str      =	self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_information_extraction(*__magic_name__ )

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







													"""simple docstring"""
													UpperCAmelCase_      :   Union[str, Any]      =	self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_token_classification(*__magic_name__ )



							@slow
							def      UpperCAmelCase__						(				self				:  Any )       ->							Any:







													"""simple docstring"""
													for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			UpperCAmelCase_      :   Any      =	ErnieMModel.from_pretrained(__magic_name__ )
																			self.assertIsNotNone(__magic_name__ )





@require_torch
class        __a   (unittest.TestCase  ):

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







													"""simple docstring"""
													UpperCAmelCase_      :   List[Any]      =	ErnieMModel.from_pretrained('''susnato/ernie-m-base_pytorch''' )
													model.eval()
													UpperCAmelCase_      :   int      =	torch.tensor([[0, 1, 2, 3, 4, 5]] )
													UpperCAmelCase_      :   str      =	model(__magic_name__ )[0]

													# TODO Replace vocab size
													UpperCAmelCase_      :   Dict      =	7_68

													UpperCAmelCase_      :   Union[str, Any]      =	torch.Size((1, 6, hidden_size) )
													self.assertEqual(output.shape							,  __magic_name__ )

													UpperCAmelCase_      :   Optional[int]      =	torch.tensor(
													    [[[-0.0_0_1_2, 0.1_2_4_5, -0.0_2_1_4], [-0.0_7_4_2, 0.0_2_4_4, -0.0_7_7_1], [-0.0_3_3_3, 0.1_1_6_4, -0.1_5_5_4]]] )

													self.assertTrue(torch.allclose(output[:, :3, :3]							,  __magic_name__							,  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 	a_(		lowercase__	):



     """simple docstring"""



     __snake_case		:				Union[str, Any]  					='''speech-encoder-decoder'''
     __snake_case		:				List[Any]  					=True





     def __init__(		self					:	Any  ,  **lowerCAmelCase__					:	List[Any])							->	Tuple:

      """simple docstring"""
      super().__init__(**lowerCAmelCase__)
      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(lowerCAmelCase__  ,  **lowerCAmelCase__)
      SCREAMING_SNAKE_CASE				    =		AutoConfig.for_model(lowerCAmelCase__  ,  **lowerCAmelCase__)
      SCREAMING_SNAKE_CASE				    =		True





     @classmethod
     def     __UpperCamelCase						(		cls					:	str  ,  lowerCAmelCase__					:	PretrainedConfig  ,  lowerCAmelCase__					:	PretrainedConfig  ,  **lowerCAmelCase__					:	List[str])							->	PretrainedConfig:

      """simple docstring"""
      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()  ,  **lowerCAmelCase__)





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

      """simple docstring"""
      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




