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					,					A_					,					A_=13					,					A_=7					,					A_=True					,					A_=True					,					A_=True					,					A_=99					,					A_=32					,					A_=5					,					A_=4					,					A_=37					,					A_="gelu"					,					A_=0.1					,					A_=0.1					,					A_=5_12					,					A_=16					,					A_=2					,					A_=0.02					,					A_=3					,					A_=4					,					A_=None					,					)			->					str:






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



     def 		UpperCamelCase_    (       self    )			->					Dict:






           """simple docstring"""
           _lowerCamelCase           =					ids_tensor([self.batch_size, self.seq_length]					,					self.vocab_size    )

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

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

           _lowerCamelCase           =					self.get_config()

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



     def 		UpperCamelCase_    (       self    )			->					Optional[int]:






           """simple docstring"""
           _lowerCamelCase           =					ids_tensor([self.batch_size, self.seq_length]					,					self.vocab_size    )

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

           return config, input_ids, input_mask



     def 		UpperCamelCase_    (       self    )			->					str:






           """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					,					A_					,					A_					,					A_					,					A_					,					A_					,					A_    )			->					Dict:






           """simple docstring"""
           _lowerCamelCase           =					ErnieMModel(config=A_    )
           model.to(A_    )
           model.eval()
           _lowerCamelCase           =					model(A_					,					return_dict=A_    )
           self.parent.assertEqual(result.last_hidden_state.shape					,					(self.batch_size, self.seq_length, self.hidden_size)    )



     def 		UpperCamelCase_    (       self					,					A_					,					A_					,					A_					,					A_					,					A_					,					A_    )			->					Any:






           """simple docstring"""
           _lowerCamelCase           =					ErnieMForQuestionAnswering(config=A_    )
           model.to(A_    )
           model.eval()
           _lowerCamelCase           =					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 		UpperCamelCase_    (       self					,					A_					,					A_					,					A_					,					A_					,					A_					,					A_    )			->					List[str]:






           """simple docstring"""
           _lowerCamelCase           =					ErnieMForInformationExtraction(config=A_    )
           model.to(A_    )
           model.eval()
           _lowerCamelCase           =					torch.ones_like(A_					,					dtype=torch.floataa    )
           _lowerCamelCase           =					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 		UpperCamelCase_    (       self					,					A_					,					A_					,					A_					,					A_					,					A_					,					A_    )			->					Optional[Any]:






           """simple docstring"""
           _lowerCamelCase           =					self.num_labels
           _lowerCamelCase           =					ErnieMForSequenceClassification(A_    )
           model.to(A_    )
           model.eval()
           _lowerCamelCase           =					model(A_					,					attention_mask=A_					,					labels=A_    )
           self.parent.assertEqual(result.logits.shape					,					(self.batch_size, self.num_labels)    )



     def 		UpperCamelCase_    (       self					,					A_					,					A_					,					A_					,					A_					,					A_					,					A_    )			->					int:






           """simple docstring"""
           _lowerCamelCase           =					self.num_labels
           _lowerCamelCase           =					ErnieMForTokenClassification(config=A_    )
           model.to(A_    )
           model.eval()
           input_ids.to(A_    )
           input_mask.to(A_    )
           token_labels.to(A_    )

           _lowerCamelCase           =					model(A_					,					attention_mask=A_					,					labels=A_    )

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



     def 		UpperCamelCase_    (       self					,					A_					,					A_					,					A_					,					A_					,					A_					,					A_    )			->					Tuple:






           """simple docstring"""
           _lowerCamelCase           =					self.num_choices
           _lowerCamelCase           =					ErnieMForMultipleChoice(config=A_    )
           model.to(A_    )
           model.eval()
           _lowerCamelCase           =					input_ids.unsqueeze(1    ).expand(-1					,					self.num_choices					,					-1    ).contiguous()
           _lowerCamelCase           =					input_mask.unsqueeze(1    ).expand(-1					,					self.num_choices					,					-1    ).contiguous()
           _lowerCamelCase           =					model(
               A_					,					attention_mask=A_					,					labels=A_					,					)
           self.parent.assertEqual(result.logits.shape					,					(self.batch_size, self.num_choices)    )





     def 		UpperCamelCase_    (       self    )			->					Tuple:






           """simple docstring"""
           _lowerCamelCase           =					self.prepare_config_and_inputs()
           (
               (
               _lowerCamelCase
           )     ,       (
               _lowerCamelCase
           )     ,       (
               _lowerCamelCase
           )     ,       (
               _lowerCamelCase
           )     ,       (
               _lowerCamelCase
           )     ,       (
               _lowerCamelCase
           )     ,       
           )           =					config_and_inputs
           _lowerCamelCase           =					{'''input_ids''': input_ids, '''attention_mask''': input_mask}
           return config, inputs_dict





@require_torch
class 			UpperCamelCase					(			__lowercase ,	__lowercase ,	unittest.TestCase			):

     '''simple docstring'''

     A_					    =	(
         (
             ErnieMModel,
             ErnieMForMultipleChoice,
             ErnieMForQuestionAnswering,
             ErnieMForSequenceClassification,
             ErnieMForTokenClassification,
         )
         if is_torch_available()
         else ()
     )
     A_					    =	()
     A_					    =	(
         {
             'feature-extraction': ErnieMModel,
             'question-answering': ErnieMForQuestionAnswering,
             'text-classification': ErnieMForSequenceClassification,
             'token-classification': ErnieMForTokenClassification,
             'zero-shot': ErnieMForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     A_					    =	False



     def 		UpperCamelCase_    (       self					,					A_					,					A_					,					A_					,					A_					,					A_    )			->					Optional[int]:






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

           return False



     def 		UpperCamelCase_    (       self    )			->					str:






           """simple docstring"""
           _lowerCamelCase           =					ErnieMModelTester(self    )
           _lowerCamelCase           =					ConfigTester(self					,					config_class=A_					,					hidden_size=37    )



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






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



     def 		UpperCamelCase_    (       self    )			->					int:






           """simple docstring"""
           _lowerCamelCase           =					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*A_    )



     def 		UpperCamelCase_    (       self    )			->					int:






           """simple docstring"""
           _lowerCamelCase           =					self.model_tester.prepare_config_and_inputs()
           for type in ["absolute", "relative_key", "relative_key_query"]:
                 _lowerCamelCase           =					type
                 self.model_tester.create_and_check_model(*A_    )



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






           """simple docstring"""
           _lowerCamelCase           =					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_multiple_choice(*A_    )



     def 		UpperCamelCase_    (       self    )			->					str:






           """simple docstring"""
           _lowerCamelCase           =					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(*A_    )



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






           """simple docstring"""
           _lowerCamelCase           =					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_sequence_classification(*A_    )



     def 		UpperCamelCase_    (       self    )			->					int:






           """simple docstring"""
           _lowerCamelCase           =					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_information_extraction(*A_    )



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






           """simple docstring"""
           _lowerCamelCase           =					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(*A_    )





     @slow
     def 		UpperCamelCase_    (       self    )			->					int:






           """simple docstring"""
           for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 _lowerCamelCase           =					ErnieMModel.from_pretrained(A_    )
                 self.assertIsNotNone(A_    )





@require_torch
class 			UpperCamelCase					(			unittest.TestCase			):

     '''simple docstring'''

     @slow
     def 		UpperCamelCase_    (       self    )			->					int:






           """simple docstring"""
           _lowerCamelCase           =					ErnieMModel.from_pretrained('''susnato/ernie-m-base_pytorch'''    )
           model.eval()
           _lowerCamelCase           =					torch.tensor([[0, 1, 2, 3, 4, 5]]    )
           _lowerCamelCase           =					model(A_    )[0]

           # TODO Replace vocab size
           _lowerCamelCase           =					7_68

           _lowerCamelCase           =					torch.Size((1, 6, hidden_size)    )
           self.assertEqual(output.shape					,					A_    )

           _lowerCamelCase           =					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]					,					A_					,					atol=1E-3    )    )
from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


snake_case__						      =			{
    'configuration_dpr': ['DPR_PRETRAINED_CONFIG_ARCHIVE_MAP', 'DPRConfig'],
    'tokenization_dpr': [
        'DPRContextEncoderTokenizer',
        'DPRQuestionEncoderTokenizer',
        'DPRReaderOutput',
        'DPRReaderTokenizer',
    ],
}


try:
     if not is_tokenizers_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     snake_case__						      =			[
         'DPRContextEncoderTokenizerFast',
         'DPRQuestionEncoderTokenizerFast',
         'DPRReaderTokenizerFast',
     ]

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     snake_case__						      =			[
         'DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST',
         'DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST',
         'DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST',
         'DPRContextEncoder',
         'DPRPretrainedContextEncoder',
         'DPRPreTrainedModel',
         'DPRPretrainedQuestionEncoder',
         'DPRPretrainedReader',
         'DPRQuestionEncoder',
         'DPRReader',
     ]

try:
     if not is_tf_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     snake_case__						      =			[
         'TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST',
         'TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST',
         'TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST',
         'TFDPRContextEncoder',
         'TFDPRPretrainedContextEncoder',
         'TFDPRPretrainedQuestionEncoder',
         'TFDPRPretrainedReader',
         'TFDPRQuestionEncoder',
         'TFDPRReader',
     ]


if TYPE_CHECKING:
     from .configuration_dpr import DPR_PRETRAINED_CONFIG_ARCHIVE_MAP, DPRConfig
     from .tokenization_dpr import (
         DPRContextEncoderTokenizer,
         DPRQuestionEncoderTokenizer,
         DPRReaderOutput,
         DPRReaderTokenizer,
     )

     try:
          if not is_tokenizers_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .tokenization_dpr_fast import (
              DPRContextEncoderTokenizerFast,
              DPRQuestionEncoderTokenizerFast,
              DPRReaderTokenizerFast,
          )

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_dpr import (
              DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
              DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
              DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
              DPRContextEncoder,
              DPRPretrainedContextEncoder,
              DPRPreTrainedModel,
              DPRPretrainedQuestionEncoder,
              DPRPretrainedReader,
              DPRQuestionEncoder,
              DPRReader,
          )

     try:
          if not is_tf_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_tf_dpr import (
              TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
              TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
              TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
              TFDPRContextEncoder,
              TFDPRPretrainedContextEncoder,
              TFDPRPretrainedQuestionEncoder,
              TFDPRPretrainedReader,
              TFDPRQuestionEncoder,
              TFDPRReader,
          )

else:
     import sys

     snake_case__						      =			_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)