




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						,	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.0_2						,	A=3						,	A=4						,	A=None						,	)						->					List[str]:



          '''simple docstring'''



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







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



          '''simple docstring'''



          a											=   ids_tensor([self.batch_size, self.seq_length]						,	self.vocab_size				)

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

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

          a											=   self.get_config()

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







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



          '''simple docstring'''



          a											=   ids_tensor([self.batch_size, self.seq_length]						,	self.vocab_size				)

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

          return config, input_ids, input_mask







     def     lowerCAmelCase_       (			self				)						->					List[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     lowerCAmelCase_       (			self						,	A						,	A						,	A						,	A						,	A						,	A				)						->					str:



          '''simple docstring'''



          a											=   ErnieMModel(config=A				)
          model.to(A				)
          model.eval()
          a											=   model(A						,	return_dict=A				)
          self.parent.assertEqual(result.last_hidden_state.shape						,	(self.batch_size, self.seq_length, self.hidden_size)				)







     def     lowerCAmelCase_       (			self						,	A						,	A						,	A						,	A						,	A						,	A				)						->					List[str]:



          '''simple docstring'''



          a											=   ErnieMForQuestionAnswering(config=A				)
          model.to(A				)
          model.eval()
          a											=   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     lowerCAmelCase_       (			self						,	A						,	A						,	A						,	A						,	A						,	A				)						->					List[str]:



          '''simple docstring'''



          a											=   ErnieMForInformationExtraction(config=A				)
          model.to(A				)
          model.eval()
          a											=   torch.ones_like(A						,	dtype=torch.floataa				)
          a											=   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     lowerCAmelCase_       (			self						,	A						,	A						,	A						,	A						,	A						,	A				)						->					Tuple:



          '''simple docstring'''



          a											=   self.num_labels
          a											=   ErnieMForSequenceClassification(A				)
          model.to(A				)
          model.eval()
          a											=   model(A						,	attention_mask=A						,	labels=A				)
          self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.num_labels)				)







     def     lowerCAmelCase_       (			self						,	A						,	A						,	A						,	A						,	A						,	A				)						->					List[Any]:



          '''simple docstring'''



          a											=   self.num_labels
          a											=   ErnieMForTokenClassification(config=A				)
          model.to(A				)
          model.eval()
          input_ids.to(A				)
          input_mask.to(A				)
          token_labels.to(A				)

          a											=   model(A						,	attention_mask=A						,	labels=A				)

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







     def     lowerCAmelCase_       (			self						,	A						,	A						,	A						,	A						,	A						,	A				)						->					Tuple:



          '''simple docstring'''



          a											=   self.num_choices
          a											=   ErnieMForMultipleChoice(config=A				)
          model.to(A				)
          model.eval()
          a											=   input_ids.unsqueeze(1				).expand(-1						,	self.num_choices						,	-1				).contiguous()
          a											=   input_mask.unsqueeze(1				).expand(-1						,	self.num_choices						,	-1				).contiguous()
          a											=   model(
              A						,	attention_mask=A						,	labels=A						,	)
          self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.num_choices)				)







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



          '''simple docstring'''



          a											=   self.prepare_config_and_inputs()
          (
              (
              a
          )						,   (
              a
          )						,   (
              a
          )						,   (
              a
          )						,   (
              a
          )						,   (
              a
          )						,   
          )											=   config_and_inputs
          a											=   {"input_ids": input_ids, "attention_mask": input_mask}
          return config, inputs_dict







@require_torch
class     a__ (      UpperCamelCase__     ,      UpperCamelCase__     ,      unittest.TestCase   ):
     a	:    Optional[Any]	      =			(
         (
             ErnieMModel,
             ErnieMForMultipleChoice,
             ErnieMForQuestionAnswering,
             ErnieMForSequenceClassification,
             ErnieMForTokenClassification,
         )
         if is_torch_available()
         else ()
     )
     a	:    Optional[Any]	      =			()
     a	:    Union[str, Any]	      =			(
         {
             """feature-extraction""": ErnieMModel,
             """question-answering""": ErnieMForQuestionAnswering,
             """text-classification""": ErnieMForSequenceClassification,
             """token-classification""": ErnieMForTokenClassification,
             """zero-shot""": ErnieMForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     a	:    Dict	      =			False







     def     lowerCAmelCase_       (			self						,	A						,	A						,	A						,	A						,	A				)						->					Optional[Any]:



          '''simple docstring'''



          if pipeline_test_casse_name == "QAPipelineTests":
               return True

          return False







     def     lowerCAmelCase_       (			self				)						->					Tuple:



          '''simple docstring'''



          a											=   ErnieMModelTester(self				)
          a											=   ConfigTester(self						,	config_class=A						,	hidden_size=37				)







     def     lowerCAmelCase_       (			self				)						->					Tuple:



          '''simple docstring'''



          self.config_tester.run_common_tests()







     def     lowerCAmelCase_       (			self				)						->					int:



          '''simple docstring'''



          a											=   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*A				)







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



          '''simple docstring'''



          a											=   self.model_tester.prepare_config_and_inputs()
          for type in ["absolute", "relative_key", "relative_key_query"]:
               a											=   type
               self.model_tester.create_and_check_model(*A				)







     def     lowerCAmelCase_       (			self				)						->					Tuple:



          '''simple docstring'''



          a											=   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_multiple_choice(*A				)







     def     lowerCAmelCase_       (			self				)						->					int:



          '''simple docstring'''



          a											=   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_question_answering(*A				)







     def     lowerCAmelCase_       (			self				)						->					Any:



          '''simple docstring'''



          a											=   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_sequence_classification(*A				)







     def     lowerCAmelCase_       (			self				)						->					int:



          '''simple docstring'''



          a											=   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_information_extraction(*A				)







     def     lowerCAmelCase_       (			self				)						->					int:



          '''simple docstring'''



          a											=   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_token_classification(*A				)







     @slow
     def     lowerCAmelCase_       (			self				)						->					Tuple:



          '''simple docstring'''



          for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               a											=   ErnieMModel.from_pretrained(A				)
               self.assertIsNotNone(A				)







@require_torch
class     a__ (      unittest.TestCase   ):







     @slow
     def     lowerCAmelCase_       (			self				)						->					List[Any]:



          '''simple docstring'''



          a											=   ErnieMModel.from_pretrained("susnato/ernie-m-base_pytorch"				)
          model.eval()
          a											=   torch.tensor([[0, 1, 2, 3, 4, 5]]				)
          a											=   model(A				)[0]

          # TODO Replace vocab size
          a											=   768

          a											=   torch.Size((1, 6, hidden_size)				)
          self.assertEqual(output.shape						,	A				)

          a											=   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]						,	A						,	atol=1e-3				)				)







from typing import TYPE_CHECKING

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


lowercase__   :    str 					=      {
    "configuration_rwkv": ["RWKV_PRETRAINED_CONFIG_ARCHIVE_MAP", "RwkvConfig", "RwkvOnnxConfig"],
}

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   lowercase__   :    int 					=      [
       "RWKV_PRETRAINED_MODEL_ARCHIVE_LIST",
       "RwkvForCausalLM",
       "RwkvModel",
       "RwkvPreTrainedModel",
   ]


if TYPE_CHECKING:
   from .configuration_rwkv import RWKV_PRETRAINED_CONFIG_ARCHIVE_MAP, RwkvConfig, RwkvOnnxConfig

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_rwkv import (
          RWKV_PRETRAINED_MODEL_ARCHIVE_LIST,
          RwkvForCausalLM,
          RwkvModel,
          RwkvPreTrainedModel,
      )
else:
   import sys

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

