






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



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

    """simple docstring"""






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



 def     UpperCAmelCase     (						self : Any						):

    """simple docstring"""






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

    lowercase__    :						int       =			None
    if self.use_input_mask:
       lowercase__    :						Tuple       =			random_attention_mask([self.batch_size, self.seq_length]						)

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

    lowercase__    :						Union[str, Any]       =			self.get_config()

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



 def     UpperCAmelCase     (						self : Union[str, Any]						):

    """simple docstring"""






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

    lowercase__    :						Dict       =			None
    if self.use_input_mask:
       lowercase__    :						str       =			random_attention_mask([self.batch_size, self.seq_length]						)
    lowercase__    :						Optional[int]       =			self.get_config()

    return config, input_ids, input_mask



 def     UpperCAmelCase     (						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     UpperCAmelCase     (						self : Any			,		UpperCAmelCase__ : str			,		UpperCAmelCase__ : List[str]			,		UpperCAmelCase__ : Tuple			,		UpperCAmelCase__ : Union[str, Any]			,		UpperCAmelCase__ : List[Any]			,		UpperCAmelCase__ : List[Any]						):

    """simple docstring"""






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



 def     UpperCAmelCase     (						self : Tuple			,		UpperCAmelCase__ : Dict			,		UpperCAmelCase__ : List[Any]			,		UpperCAmelCase__ : Optional[Any]			,		UpperCAmelCase__ : Tuple			,		UpperCAmelCase__ : Dict			,		UpperCAmelCase__ : List[Any]						):

    """simple docstring"""






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

    """simple docstring"""






    lowercase__    :						List[str]       =			ErnieMForInformationExtraction(config=UpperCAmelCase__						)
    model.to(UpperCAmelCase__						)
    model.eval()
    lowercase__    :						Dict       =			torch.ones_like(UpperCAmelCase__			,		dtype=torch.floataa						)
    lowercase__    :						Any       =			model(
        UpperCAmelCase__			,		attention_mask=UpperCAmelCase__			,		start_positions=UpperCAmelCase__			,		end_positions=UpperCAmelCase__			,		)
    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 : Any			,		UpperCAmelCase__ : List[str]			,		UpperCAmelCase__ : List[str]			,		UpperCAmelCase__ : Dict			,		UpperCAmelCase__ : str			,		UpperCAmelCase__ : str			,		UpperCAmelCase__ : Optional[int]						):

    """simple docstring"""






    lowercase__    :						Union[str, Any]       =			self.num_labels
    lowercase__    :						Tuple       =			ErnieMForSequenceClassification(UpperCAmelCase__						)
    model.to(UpperCAmelCase__						)
    model.eval()
    lowercase__    :						List[str]       =			model(UpperCAmelCase__			,		attention_mask=UpperCAmelCase__			,		labels=UpperCAmelCase__						)
    self.parent.assertEqual(result.logits.shape			,		(self.batch_size, self.num_labels)						)



 def     UpperCAmelCase     (						self : str			,		UpperCAmelCase__ : Dict			,		UpperCAmelCase__ : str			,		UpperCAmelCase__ : int			,		UpperCAmelCase__ : List[str]			,		UpperCAmelCase__ : Tuple			,		UpperCAmelCase__ : List[str]						):

    """simple docstring"""






    lowercase__    :						Optional[int]       =			self.num_labels
    lowercase__    :						Any       =			ErnieMForTokenClassification(config=UpperCAmelCase__						)
    model.to(UpperCAmelCase__						)
    model.eval()
    input_ids.to(UpperCAmelCase__						)
    input_mask.to(UpperCAmelCase__						)
    token_labels.to(UpperCAmelCase__						)

    lowercase__    :						List[Any]       =			model(UpperCAmelCase__			,		attention_mask=UpperCAmelCase__			,		labels=UpperCAmelCase__						)

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



 def     UpperCAmelCase     (						self : List[Any]			,		UpperCAmelCase__ : Any			,		UpperCAmelCase__ : List[Any]			,		UpperCAmelCase__ : Tuple			,		UpperCAmelCase__ : Union[str, Any]			,		UpperCAmelCase__ : Tuple			,		UpperCAmelCase__ : List[Any]						):

    """simple docstring"""






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







 def     UpperCAmelCase     (						self : Dict						):

    """simple docstring"""






    lowercase__    :						Tuple       =			self.prepare_config_and_inputs()
    (
        (
        lowercase__
    )  ,       (
        lowercase__
    )  ,       (
        lowercase__
    )  ,       (
        lowercase__
    )  ,       (
        lowercase__
    )  ,       (
        lowercase__
    )  ,       
    )    :						Any       =			config_and_inputs
    lowercase__    :						int       =			{"""input_ids""": input_ids, """attention_mask""": input_mask}
    return config, inputs_dict
@require_torch
class 							__snake_case(       __UpperCamelCase  ,							__UpperCamelCase  ,							unittest.TestCase   ):
 lowercase			:							Any		   =		(
     (
         ErnieMModel,
         ErnieMForMultipleChoice,
         ErnieMForQuestionAnswering,
         ErnieMForSequenceClassification,
         ErnieMForTokenClassification,
     )
     if is_torch_available()
     else ()
 )
 lowercase			:							Optional[int]		   =		()
 lowercase			:							int		   =		(
     {
         '''feature-extraction''': ErnieMModel,
         '''question-answering''': ErnieMForQuestionAnswering,
         '''text-classification''': ErnieMForSequenceClassification,
         '''token-classification''': ErnieMForTokenClassification,
         '''zero-shot''': ErnieMForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 lowercase			:							Dict		   =		False



 def     UpperCAmelCase     (						self : Any			,		UpperCAmelCase__ : Dict			,		UpperCAmelCase__ : Optional[int]			,		UpperCAmelCase__ : int			,		UpperCAmelCase__ : List[Any]			,		UpperCAmelCase__ : List[Any]						):

    """simple docstring"""






    if pipeline_test_casse_name == "QAPipelineTests":
       return True

    return False



 def     UpperCAmelCase     (						self : Any						):

    """simple docstring"""






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



 def     UpperCAmelCase     (						self : Optional[Any]						):

    """simple docstring"""






    self.config_tester.run_common_tests()



 def     UpperCAmelCase     (						self : str						):

    """simple docstring"""






    lowercase__    :						int       =			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*UpperCAmelCase__						)



 def     UpperCAmelCase     (						self : List[Any]						):

    """simple docstring"""






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



 def     UpperCAmelCase     (						self : List[Any]						):

    """simple docstring"""






    lowercase__    :						int       =			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_multiple_choice(*UpperCAmelCase__						)



 def     UpperCAmelCase     (						self : int						):

    """simple docstring"""






    lowercase__    :						Optional[int]       =			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_question_answering(*UpperCAmelCase__						)



 def     UpperCAmelCase     (						self : List[str]						):

    """simple docstring"""






    lowercase__    :						Optional[int]       =			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_sequence_classification(*UpperCAmelCase__						)



 def     UpperCAmelCase     (						self : str						):

    """simple docstring"""






    lowercase__    :						int       =			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_information_extraction(*UpperCAmelCase__						)



 def     UpperCAmelCase     (						self : List[str]						):

    """simple docstring"""






    lowercase__    :						Tuple       =			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_token_classification(*UpperCAmelCase__						)







 @slow
 def     UpperCAmelCase     (						self : Union[str, Any]						):

    """simple docstring"""






    for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       lowercase__    :						Union[str, Any]       =			ErnieMModel.from_pretrained(UpperCAmelCase__						)
       self.assertIsNotNone(UpperCAmelCase__						)
@require_torch
class 							__snake_case(       unittest.TestCase   ):



 @slow
 def     UpperCAmelCase     (						self : Any						):

    """simple docstring"""






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

    # TODO Replace vocab size
    lowercase__    :						List[Any]       =			768

    lowercase__    :						Tuple       =			torch.Size((1, 6, hidden_size)						)
    self.assertEqual(output.shape			,		UpperCAmelCase__						)

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










def    a__    (	A       ):
   return int(A       ) if x - int(A       ) >= 0 else int(A       ) - 1


if __name__ == "__main__":
 import doctest

 doctest.testmod()


