




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   :





     '''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=512       ,__a=16       ,__a=2       ,__a=0.02       ,__a=3       ,__a=4       ,__a=None       ,)			->							List[str]:
          snake_case :			Optional[int]					   = parent
          snake_case :			Dict					   = batch_size
          snake_case :			Optional[int]					   = seq_length
          snake_case :			Optional[Any]					   = is_training
          snake_case :			Dict					   = use_input_mask
          snake_case :			str					   = use_labels
          snake_case :			Optional[Any]					   = vocab_size
          snake_case :			Any					   = hidden_size
          snake_case :			Optional[Any]					   = num_hidden_layers
          snake_case :			Tuple					   = num_attention_heads
          snake_case :			str					   = intermediate_size
          snake_case :			Union[str, Any]					   = hidden_act
          snake_case :			Any					   = hidden_dropout_prob
          snake_case :			Optional[Any]					   = attention_probs_dropout_prob
          snake_case :			Union[str, Any]					   = max_position_embeddings
          snake_case :			Optional[int]					   = type_vocab_size
          snake_case :			Optional[int]					   = type_sequence_label_size
          snake_case :			int					   = initializer_range
          snake_case :			str					   = num_labels
          snake_case :			Union[str, Any]					   = num_choices
          snake_case :			Any					   = scope







     def  snake_case_			(							self       )			->							int:
          snake_case :			List[Any]					   = ids_tensor([self.batch_size, self.seq_length]       ,self.vocab_size       )

          snake_case :			Optional[Any]					   = None
          if self.use_input_mask:
               snake_case :			Dict					   = random_attention_mask([self.batch_size, self.seq_length]       )

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

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

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







     def  snake_case_			(							self       )			->							Union[str, Any]:
          snake_case :			Tuple					   = ids_tensor([self.batch_size, self.seq_length]       ,self.vocab_size       )

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

          return config, input_ids, input_mask







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







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







     def  snake_case_			(							self       ,__a       ,__a       ,__a       ,__a       ,__a       ,__a       )			->							Dict:
          snake_case :			int					   = self.num_labels
          snake_case :			Union[str, Any]					   = ErnieMForTokenClassification(config=SCREAMING_SNAKE_CASE_       )
          model.to(SCREAMING_SNAKE_CASE_       )
          model.eval()
          input_ids.to(SCREAMING_SNAKE_CASE_       )
          input_mask.to(SCREAMING_SNAKE_CASE_       )
          token_labels.to(SCREAMING_SNAKE_CASE_       )

          snake_case :			List[str]					   = model(SCREAMING_SNAKE_CASE_       ,attention_mask=SCREAMING_SNAKE_CASE_       ,labels=SCREAMING_SNAKE_CASE_       )

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







     def  snake_case_			(							self       ,__a       ,__a       ,__a       ,__a       ,__a       ,__a       )			->							str:
          snake_case :			int					   = self.num_choices
          snake_case :			Any					   = ErnieMForMultipleChoice(config=SCREAMING_SNAKE_CASE_       )
          model.to(SCREAMING_SNAKE_CASE_       )
          model.eval()
          snake_case :			List[Any]					   = input_ids.unsqueeze(1       ).expand(-1       ,self.num_choices       ,-1       ).contiguous()
          snake_case :			Any					   = input_mask.unsqueeze(1       ).expand(-1       ,self.num_choices       ,-1       ).contiguous()
          snake_case :			str					   = model(
              SCREAMING_SNAKE_CASE_       ,attention_mask=SCREAMING_SNAKE_CASE_       ,labels=SCREAMING_SNAKE_CASE_       ,)
          self.parent.assertEqual(result.logits.shape       ,(self.batch_size, self.num_choices)       )







     def  snake_case_			(							self       )			->							int:
          snake_case :			Optional[int]					   = self.prepare_config_and_inputs()
          (
              snake_case  
          ) :			Union[str, Any]					   = config_and_inputs
          snake_case :			Tuple					   = {'input_ids': input_ids, 'attention_mask': input_mask}
          return config, inputs_dict







@require_torch
class 				_a   (a__,		a__,		unittest.TestCase  ):





     '''simple docstring'''



     lowerCAmelCase_     :    Tuple									=				(
         (
             ErnieMModel,
             ErnieMForMultipleChoice,
             ErnieMForQuestionAnswering,
             ErnieMForSequenceClassification,
             ErnieMForTokenClassification,
         )
         if is_torch_available()
         else ()
     )
     lowerCAmelCase_     :    List[str]									=				()
     lowerCAmelCase_     :    int									=				(
         {
             "feature-extraction": ErnieMModel,
             "question-answering": ErnieMForQuestionAnswering,
             "text-classification": ErnieMForSequenceClassification,
             "token-classification": ErnieMForTokenClassification,
             "zero-shot": ErnieMForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     lowerCAmelCase_     :    Tuple									=				False







     def  snake_case_			(							self       ,__a       ,__a       ,__a       ,__a       ,__a       )			->							Optional[int]:
          if pipeline_test_casse_name == "QAPipelineTests":
               return True

          return False







     def  snake_case_			(							self       )			->							List[str]:
          snake_case :			List[str]					   = ErnieMModelTester(self       )
          snake_case :			int					   = ConfigTester(self       ,config_class=SCREAMING_SNAKE_CASE_       ,hidden_size=37       )







     def  snake_case_			(							self       )			->							int:
          self.config_tester.run_common_tests()







     def  snake_case_			(							self       )			->							Optional[int]:
          snake_case :			List[Any]					   = self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_       )







     def  snake_case_			(							self       )			->							Any:
          snake_case :			Optional[Any]					   = self.model_tester.prepare_config_and_inputs()
          for type in ["absolute", "relative_key", "relative_key_query"]:
               snake_case :			int					   = type
               self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_       )







     def  snake_case_			(							self       )			->							Any:
          snake_case :			Tuple					   = self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_multiple_choice(*SCREAMING_SNAKE_CASE_       )







     def  snake_case_			(							self       )			->							Any:
          snake_case :			Dict					   = self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_question_answering(*SCREAMING_SNAKE_CASE_       )







     def  snake_case_			(							self       )			->							List[str]:
          snake_case :			Dict					   = self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_sequence_classification(*SCREAMING_SNAKE_CASE_       )







     def  snake_case_			(							self       )			->							Optional[Any]:
          snake_case :			Optional[Any]					   = self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_information_extraction(*SCREAMING_SNAKE_CASE_       )







     def  snake_case_			(							self       )			->							Any:
          snake_case :			List[Any]					   = self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_token_classification(*SCREAMING_SNAKE_CASE_       )







     @slow
     def  snake_case_			(							self       )			->							List[str]:
          for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               snake_case :			List[Any]					   = ErnieMModel.from_pretrained(SCREAMING_SNAKE_CASE_       )
               self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )







@require_torch
class 				_a   (unittest.TestCase  ):





     '''simple docstring'''



     @slow
     def  snake_case_			(							self       )			->							Union[str, Any]:
          snake_case :			Optional[Any]					   = ErnieMModel.from_pretrained("""susnato/ernie-m-base_pytorch"""       )
          model.eval()
          snake_case :			Optional[Any]					   = torch.tensor([[0, 1, 2, 3, 4, 5]]       )
          snake_case :			Optional[Any]					   = model(SCREAMING_SNAKE_CASE_       )[0]

          # TODO Replace vocab size
          snake_case :			Dict					   = 768

          snake_case :			int					   = torch.Size((1, 6, hidden_size)       )
          self.assertEqual(output.shape       ,SCREAMING_SNAKE_CASE_       )

          snake_case :			Optional[Any]					   = 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]       ,SCREAMING_SNAKE_CASE_       ,atol=1E-3       )       )









'''simple docstring'''







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


lowercase			:      Any			=					{"""configuration_visual_bert""": ["""VISUAL_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """VisualBertConfig"""]}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      lowercase			:      Optional[int]			=					[
          """VISUAL_BERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
          """VisualBertForMultipleChoice""",
          """VisualBertForPreTraining""",
          """VisualBertForQuestionAnswering""",
          """VisualBertForRegionToPhraseAlignment""",
          """VisualBertForVisualReasoning""",
          """VisualBertLayer""",
          """VisualBertModel""",
          """VisualBertPreTrainedModel""",
      ]


if TYPE_CHECKING:
      from .configuration_visual_bert import VISUAL_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, VisualBertConfig

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_visual_bert import (
                VISUAL_BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
                VisualBertForMultipleChoice,
                VisualBertForPreTraining,
                VisualBertForQuestionAnswering,
                VisualBertForRegionToPhraseAlignment,
                VisualBertForVisualReasoning,
                VisualBertLayer,
                VisualBertModel,
                VisualBertPreTrainedModel,
            )


else:
      import sys

      lowercase			:      List[Any]			=					_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



