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


   def __init__( self					:						Optional[Any] ,		__lowerCAmelCase					:						int ,		__lowerCAmelCase					:						List[Any]=13 ,		__lowerCAmelCase					:						int=7 ,		__lowerCAmelCase					:						Any=True ,		__lowerCAmelCase					:						Tuple=True ,		__lowerCAmelCase					:						Union[str, Any]=True ,		__lowerCAmelCase					:						Union[str, Any]=99 ,		__lowerCAmelCase					:						Any=32 ,		__lowerCAmelCase					:						int=5 ,		__lowerCAmelCase					:						Optional[Any]=4 ,		__lowerCAmelCase					:						List[str]=37 ,		__lowerCAmelCase					:						Dict="gelu" ,		__lowerCAmelCase					:						Optional[int]=0.1 ,		__lowerCAmelCase					:						Dict=0.1 ,		__lowerCAmelCase					:						Union[str, Any]=512 ,		__lowerCAmelCase					:						Tuple=16 ,		__lowerCAmelCase					:						Tuple=2 ,		__lowerCAmelCase					:						Optional[int]=0.02 ,		__lowerCAmelCase					:						str=3 ,		__lowerCAmelCase					:						List[str]=4 ,		__lowerCAmelCase					:						List[Any]=None ,		)   ->       Dict:
         lowercase_     			=   parent
         lowercase_     			=   batch_size
         lowercase_     			=   seq_length
         lowercase_     			=   is_training
         lowercase_     			=   use_input_mask
         lowercase_     			=   use_labels
         lowercase_     			=   vocab_size
         lowercase_     			=   hidden_size
         lowercase_     			=   num_hidden_layers
         lowercase_     			=   num_attention_heads
         lowercase_     			=   intermediate_size
         lowercase_     			=   hidden_act
         lowercase_     			=   hidden_dropout_prob
         lowercase_     			=   attention_probs_dropout_prob
         lowercase_     			=   max_position_embeddings
         lowercase_     			=   type_vocab_size
         lowercase_     			=   type_sequence_label_size
         lowercase_     			=   initializer_range
         lowercase_     			=   num_labels
         lowercase_     			=   num_choices
         lowercase_     			=   scope


   def __UpperCAmelCase							( self					:						int)   ->       Optional[int]:
         lowercase_     			=   ids_tensor([self.batch_size, self.seq_length] ,		self.vocab_size)

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

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

         lowercase_     			=   self.get_config()

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


   def __UpperCAmelCase							( self					:						int)   ->       str:
         lowercase_     			=   ids_tensor([self.batch_size, self.seq_length] ,		self.vocab_size)

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

         return config, input_ids, input_mask


   def __UpperCAmelCase							( self					:						Optional[Any])   ->       List[Any]:
         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] ,		__lowerCAmelCase					:						Tuple ,		__lowerCAmelCase					:						str ,		__lowerCAmelCase					:						Optional[int] ,		__lowerCAmelCase					:						Union[str, Any] ,		__lowerCAmelCase					:						int ,		__lowerCAmelCase					:						str)   ->       Dict:
         lowercase_     			=   ErnieMModel(config=__lowerCAmelCase)
         model.to(__lowerCAmelCase)
         model.eval()
         lowercase_     			=   model(__lowerCAmelCase ,		return_dict=__lowerCAmelCase)
         self.parent.assertEqual(result.last_hidden_state.shape ,		(self.batch_size, self.seq_length, self.hidden_size))


   def __UpperCAmelCase							( self					:						Any ,		__lowerCAmelCase					:						Dict ,		__lowerCAmelCase					:						Optional[int] ,		__lowerCAmelCase					:						Dict ,		__lowerCAmelCase					:						Union[str, Any] ,		__lowerCAmelCase					:						Optional[Any] ,		__lowerCAmelCase					:						List[Any])   ->       Union[str, Any]:
         lowercase_     			=   ErnieMForQuestionAnswering(config=__lowerCAmelCase)
         model.to(__lowerCAmelCase)
         model.eval()
         lowercase_     			=   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 __UpperCAmelCase							( self					:						Tuple ,		__lowerCAmelCase					:						Optional[Any] ,		__lowerCAmelCase					:						List[str] ,		__lowerCAmelCase					:						Dict ,		__lowerCAmelCase					:						Tuple ,		__lowerCAmelCase					:						Dict ,		__lowerCAmelCase					:						int)   ->       Optional[Any]:
         lowercase_     			=   ErnieMForInformationExtraction(config=__lowerCAmelCase)
         model.to(__lowerCAmelCase)
         model.eval()
         lowercase_     			=   torch.ones_like(__lowerCAmelCase ,		dtype=torch.floataa)
         lowercase_     			=   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 __UpperCAmelCase							( self					:						Dict ,		__lowerCAmelCase					:						Union[str, Any] ,		__lowerCAmelCase					:						List[Any] ,		__lowerCAmelCase					:						Dict ,		__lowerCAmelCase					:						Optional[Any] ,		__lowerCAmelCase					:						Tuple ,		__lowerCAmelCase					:						List[Any])   ->       str:
         lowercase_     			=   self.num_labels
         lowercase_     			=   ErnieMForSequenceClassification(__lowerCAmelCase)
         model.to(__lowerCAmelCase)
         model.eval()
         lowercase_     			=   model(__lowerCAmelCase ,		attention_mask=__lowerCAmelCase ,		labels=__lowerCAmelCase)
         self.parent.assertEqual(result.logits.shape ,		(self.batch_size, self.num_labels))


   def __UpperCAmelCase							( self					:						Union[str, Any] ,		__lowerCAmelCase					:						str ,		__lowerCAmelCase					:						List[str] ,		__lowerCAmelCase					:						Optional[Any] ,		__lowerCAmelCase					:						Tuple ,		__lowerCAmelCase					:						Optional[int] ,		__lowerCAmelCase					:						int)   ->       List[str]:
         lowercase_     			=   self.num_labels
         lowercase_     			=   ErnieMForTokenClassification(config=__lowerCAmelCase)
         model.to(__lowerCAmelCase)
         model.eval()
         input_ids.to(__lowerCAmelCase)
         input_mask.to(__lowerCAmelCase)
         token_labels.to(__lowerCAmelCase)

         lowercase_     			=   model(__lowerCAmelCase ,		attention_mask=__lowerCAmelCase ,		labels=__lowerCAmelCase)

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


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


   def __UpperCAmelCase							( self					:						Tuple)   ->       Dict:
         lowercase_     			=   self.prepare_config_and_inputs()
         (
             (
             lowercase_
         )			,				(
             lowercase_
         )			,				(
             lowercase_
         )			,				(
             lowercase_
         )			,				(
             lowercase_
         )			,				(
             lowercase_
         )			,				
         )     			=   config_and_inputs
         lowercase_     			=   {"input_ids": input_ids, "attention_mask": input_mask}
         return config, inputs_dict





@require_torch
class 				lowercase    (    __lowerCamelCase			,					__lowerCamelCase			,					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 __UpperCAmelCase							( self					:						Any ,		__lowerCAmelCase					:						Any ,		__lowerCAmelCase					:						Optional[int] ,		__lowerCAmelCase					:						Tuple ,		__lowerCAmelCase					:						Tuple ,		__lowerCAmelCase					:						Any)   ->       Dict:
         if pipeline_test_casse_name == "QAPipelineTests":
               return True

         return False


   def __UpperCAmelCase							( self					:						Optional[int])   ->       List[str]:
         lowercase_     			=   ErnieMModelTester(self)
         lowercase_     			=   ConfigTester(self ,		config_class=__lowerCAmelCase ,		hidden_size=37)


   def __UpperCAmelCase							( self					:						Optional[Any])   ->       Optional[int]:
         self.config_tester.run_common_tests()


   def __UpperCAmelCase							( self					:						Union[str, Any])   ->       List[Any]:
         lowercase_     			=   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__lowerCAmelCase)


   def __UpperCAmelCase							( self					:						str)   ->       Union[str, Any]:
         lowercase_     			=   self.model_tester.prepare_config_and_inputs()
         for type in ["absolute", "relative_key", "relative_key_query"]:
               lowercase_     			=   type
               self.model_tester.create_and_check_model(*__lowerCAmelCase)


   def __UpperCAmelCase							( self					:						Union[str, Any])   ->       Tuple:
         lowercase_     			=   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_multiple_choice(*__lowerCAmelCase)


   def __UpperCAmelCase							( self					:						int)   ->       Optional[int]:
         lowercase_     			=   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*__lowerCAmelCase)


   def __UpperCAmelCase							( self					:						List[str])   ->       List[str]:
         lowercase_     			=   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*__lowerCAmelCase)


   def __UpperCAmelCase							( self					:						str)   ->       Dict:
         lowercase_     			=   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_information_extraction(*__lowerCAmelCase)


   def __UpperCAmelCase							( self					:						Optional[int])   ->       Dict:
         lowercase_     			=   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*__lowerCAmelCase)


   @slow
   def __UpperCAmelCase							( self					:						int)   ->       Union[str, Any]:
         for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               lowercase_     			=   ErnieMModel.from_pretrained(__lowerCAmelCase)
               self.assertIsNotNone(__lowerCAmelCase)





@require_torch
class 				lowercase    (    unittest.TestCase			):


   @slow
   def __UpperCAmelCase							( self					:						int)   ->       str:
         lowercase_     			=   ErnieMModel.from_pretrained("susnato/ernie-m-base_pytorch")
         model.eval()
         lowercase_     			=   torch.tensor([[0, 1, 2, 3, 4, 5]])
         lowercase_     			=   model(__lowerCAmelCase)[0]

         # TODO Replace vocab size
         lowercase_     			=   768

         lowercase_     			=   torch.Size((1, 6, hidden_size))
         self.assertEqual(output.shape ,		__lowerCAmelCase)

         lowercase_     			=   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))







'''simple docstring'''

import unittest
from dataclasses import dataclass
from typing import Optional

from transformers.utils import ModelOutput





@dataclass
class 				lowercase    (    __lowerCamelCase			):
   lowerCamelCase_            =42
   lowerCamelCase_            =None
   lowerCamelCase_            =None





class 				lowercase    (    unittest.TestCase			):


   def __UpperCAmelCase							( self					:						Tuple)   ->       Dict:
         lowercase_     			=   ModelOutputTest(a=30)
         self.assertEqual(x.a ,		30)
         self.assertIsNone(x.b)
         self.assertIsNone(x.c)
         with self.assertRaises(__lowerCAmelCase):
               lowercase_     			=   x.d


   def __UpperCAmelCase							( self					:						List[str])   ->       Union[str, Any]:
         lowercase_     			=   ModelOutputTest(a=30 ,		b=10)
         self.assertEqual(x[0] ,		30)
         self.assertEqual(x[1] ,		10)
         self.assertEqual(x[:2] ,		(30, 10))
         self.assertEqual(x[:] ,		(30, 10))

         lowercase_     			=   ModelOutputTest(a=30 ,		c=10)
         self.assertEqual(x[0] ,		30)
         self.assertEqual(x[1] ,		10)
         self.assertEqual(x[:2] ,		(30, 10))
         self.assertEqual(x[:] ,		(30, 10))


   def __UpperCAmelCase							( self					:						Union[str, Any])   ->       List[str]:
         lowercase_     			=   ModelOutputTest(a=30 ,		b=10)
         self.assertEqual(x["a"] ,		30)
         self.assertEqual(x["b"] ,		10)
         with self.assertRaises(__lowerCAmelCase):
               lowercase_     			=   x["c"]

         lowercase_     			=   ModelOutputTest(a=30 ,		c=10)
         self.assertEqual(x["a"] ,		30)
         self.assertEqual(x["c"] ,		10)
         with self.assertRaises(__lowerCAmelCase):
               lowercase_     			=   x["b"]


   def __UpperCAmelCase							( self					:						List[str])   ->       List[str]:
         lowercase_     			=   ModelOutputTest(a=30)
         self.assertEqual(list(x.keys()) ,		["a"])
         self.assertEqual(list(x.values()) ,		[30])
         self.assertEqual(list(x.items()) ,		[("a", 30)])
         self.assertEqual(list(__lowerCAmelCase) ,		["a"])

         lowercase_     			=   ModelOutputTest(a=30 ,		b=10)
         self.assertEqual(list(x.keys()) ,		["a", "b"])
         self.assertEqual(list(x.values()) ,		[30, 10])
         self.assertEqual(list(x.items()) ,		[("a", 30), ("b", 10)])
         self.assertEqual(list(__lowerCAmelCase) ,		["a", "b"])

         lowercase_     			=   ModelOutputTest(a=30 ,		c=10)
         self.assertEqual(list(x.keys()) ,		["a", "c"])
         self.assertEqual(list(x.values()) ,		[30, 10])
         self.assertEqual(list(x.items()) ,		[("a", 30), ("c", 10)])
         self.assertEqual(list(__lowerCAmelCase) ,		["a", "c"])

         with self.assertRaises(__lowerCAmelCase):
               lowercase_     			=   x.update({"d": 20})
         with self.assertRaises(__lowerCAmelCase):
               del x["a"]
         with self.assertRaises(__lowerCAmelCase):
               lowercase_     			=   x.pop("a")
         with self.assertRaises(__lowerCAmelCase):
               lowercase_     			=   x.setdefault("d" ,		32)


   def __UpperCAmelCase							( self					:						List[Any])   ->       Optional[int]:
         lowercase_     			=   ModelOutputTest(a=30)
         lowercase_     			=   10
         self.assertEqual(x.a ,		10)
         self.assertEqual(x["a"] ,		10)


   def __UpperCAmelCase							( self					:						List[Any])   ->       Any:
         lowercase_     			=   ModelOutputTest(a=30)
         lowercase_     			=   10
         self.assertEqual(x.a ,		10)
         self.assertEqual(x["a"] ,		10)


   def __UpperCAmelCase							( self					:						Union[str, Any])   ->       List[Any]:
         lowercase_     			=   ModelOutputTest({"a": 30, "b": 10})
         self.assertEqual(list(x.keys()) ,		["a", "b"])
         self.assertEqual(x.a ,		30)
         self.assertEqual(x.b ,		10)


   def __UpperCAmelCase							( self					:						List[str])   ->       str:
         lowercase_     			=   ModelOutputTest([("a", 30), ("b", 10)])
         self.assertEqual(list(x.keys()) ,		["a", "b"])
         self.assertEqual(x.a ,		30)
         self.assertEqual(x.b ,		10)

         with self.assertRaises(__lowerCAmelCase):
               lowercase_     			=   ModelOutputTest([("a", 30), (10, 10)])

         lowercase_     			=   ModelOutputTest(a=(30, 30))
         self.assertEqual(list(x.keys()) ,		["a"])
         self.assertEqual(x.a ,		(30, 30))






