



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





     def __init__(							self			:				List[Any]  ,	UpperCamelCase			:				Any  ,	UpperCamelCase			:				List[str]=1_3  ,	UpperCamelCase			:				Union[str, Any]=7  ,	UpperCamelCase			:				Union[str, Any]=True  ,	UpperCamelCase			:				Optional[Any]=True  ,	UpperCamelCase			:				List[str]=True  ,	UpperCamelCase			:				Any=9_9  ,	UpperCamelCase			:				Dict=3_2  ,	UpperCamelCase			:				str=5  ,	UpperCamelCase			:				str=4  ,	UpperCamelCase			:				str=3_7  ,	UpperCamelCase			:				Optional[Any]="gelu"  ,	UpperCamelCase			:				Optional[Any]=0.1  ,	UpperCamelCase			:				Union[str, Any]=0.1  ,	UpperCamelCase			:				Tuple=5_1_2  ,	UpperCamelCase			:				List[Any]=1_6  ,	UpperCamelCase			:				int=2  ,	UpperCamelCase			:				List[str]=0.0_2  ,	UpperCamelCase			:				Tuple=3  ,	UpperCamelCase			:				List[str]=4  ,	UpperCamelCase			:				Tuple=None  ,	)->Any:
      __SCREAMING_SNAKE_CASE							:     Any							=						parent
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						batch_size
      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						seq_length
      __SCREAMING_SNAKE_CASE							:     Any							=						is_training
      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						use_input_mask
      __SCREAMING_SNAKE_CASE							:     List[str]							=						use_labels
      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						vocab_size
      __SCREAMING_SNAKE_CASE							:     List[Any]							=						hidden_size
      __SCREAMING_SNAKE_CASE							:     str							=						num_hidden_layers
      __SCREAMING_SNAKE_CASE							:     str							=						num_attention_heads
      __SCREAMING_SNAKE_CASE							:     List[Any]							=						intermediate_size
      __SCREAMING_SNAKE_CASE							:     Tuple							=						hidden_act
      __SCREAMING_SNAKE_CASE							:     Tuple							=						hidden_dropout_prob
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						attention_probs_dropout_prob
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						max_position_embeddings
      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						type_vocab_size
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						type_sequence_label_size
      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						initializer_range
      __SCREAMING_SNAKE_CASE							:     int							=						num_labels
      __SCREAMING_SNAKE_CASE							:     int							=						num_choices
      __SCREAMING_SNAKE_CASE							:     List[str]							=						scope





     def 						__snake_case     (							self			:				Optional[int]					)->Any:
      __SCREAMING_SNAKE_CASE							:     str							=						ids_tensor([self.batch_size, self.seq_length]  ,	self.vocab_size					)

      __SCREAMING_SNAKE_CASE							:     Tuple							=						None
      if self.use_input_mask:
       __SCREAMING_SNAKE_CASE							:     List[str]							=						random_attention_mask([self.batch_size, self.seq_length]					)

      __SCREAMING_SNAKE_CASE							:     List[Any]							=						None
      __SCREAMING_SNAKE_CASE							:     str							=						None
      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						None
      if self.use_labels:
       __SCREAMING_SNAKE_CASE							:     Dict							=						ids_tensor([self.batch_size]  ,	self.type_sequence_label_size					)
       __SCREAMING_SNAKE_CASE							:     Tuple							=						ids_tensor([self.batch_size, self.seq_length]  ,	self.num_labels					)
       __SCREAMING_SNAKE_CASE							:     Dict							=						ids_tensor([self.batch_size]  ,	self.num_choices					)

      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						self.get_config()

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





     def 						__snake_case     (							self			:				List[Any]					)->Dict:
      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						ids_tensor([self.batch_size, self.seq_length]  ,	self.vocab_size					)

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

      return config, input_ids, input_mask





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





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





     def 						__snake_case     (							self			:				Dict  ,	UpperCamelCase			:				int  ,	UpperCamelCase			:				Any  ,	UpperCamelCase			:				Dict  ,	UpperCamelCase			:				int  ,	UpperCamelCase			:				str  ,	UpperCamelCase			:				Any					)->Dict:
      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						self.num_labels
      __SCREAMING_SNAKE_CASE							:     str							=						ErnieMForTokenClassification(config=UpperCamelCase					)
      model.to(UpperCamelCase					)
      model.eval()
      input_ids.to(UpperCamelCase					)
      input_mask.to(UpperCamelCase					)
      token_labels.to(UpperCamelCase					)

      __SCREAMING_SNAKE_CASE							:     Tuple							=						model(UpperCamelCase  ,	attention_mask=UpperCamelCase  ,	labels=UpperCamelCase					)

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





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






     def 						__snake_case     (							self			:				Tuple					)->Any:
      __SCREAMING_SNAKE_CASE							:     Tuple							=						self.prepare_config_and_inputs()
      (
          __SCREAMING_SNAKE_CASE							
      )							:     Optional[int]							=						config_and_inputs
      __SCREAMING_SNAKE_CASE							:     Dict							=						{'''input_ids''': input_ids, '''attention_mask''': input_mask}
      return config, inputs_dict






@require_torch
class    _SCREAMING_SNAKE_CASE						(UpperCamelCase      ,     UpperCamelCase      ,     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 						__snake_case     (							self			:				str  ,	UpperCamelCase			:				List[Any]  ,	UpperCamelCase			:				int  ,	UpperCamelCase			:				str  ,	UpperCamelCase			:				Any  ,	UpperCamelCase			:				Tuple					)->Tuple:
      if pipeline_test_casse_name == "QAPipelineTests":
       return True

      return False





     def 						__snake_case     (							self			:				List[str]					)->Union[str, Any]:
      __SCREAMING_SNAKE_CASE							:     List[str]							=						ErnieMModelTester(self					)
      __SCREAMING_SNAKE_CASE							:     Tuple							=						ConfigTester(self  ,	config_class=UpperCamelCase  ,	hidden_size=3_7					)





     def 						__snake_case     (							self			:				List[str]					)->Any:
      self.config_tester.run_common_tests()





     def 						__snake_case     (							self			:				List[str]					)->Any:
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*UpperCamelCase					)





     def 						__snake_case     (							self			:				int					)->Tuple:
      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						self.model_tester.prepare_config_and_inputs()
      for type in ["absolute", "relative_key", "relative_key_query"]:
       __SCREAMING_SNAKE_CASE							:     List[str]							=						type
       self.model_tester.create_and_check_model(*UpperCamelCase					)





     def 						__snake_case     (							self			:				List[Any]					)->List[Any]:
      __SCREAMING_SNAKE_CASE							:     Any							=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_multiple_choice(*UpperCamelCase					)





     def 						__snake_case     (							self			:				List[str]					)->int:
      __SCREAMING_SNAKE_CASE							:     Any							=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_question_answering(*UpperCamelCase					)





     def 						__snake_case     (							self			:				List[str]					)->Optional[Any]:
      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase					)





     def 						__snake_case     (							self			:				Dict					)->List[str]:
      __SCREAMING_SNAKE_CASE							:     str							=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_information_extraction(*UpperCamelCase					)





     def 						__snake_case     (							self			:				Union[str, Any]					)->Union[str, Any]:
      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_token_classification(*UpperCamelCase					)






     @slow
     def 						__snake_case     (							self			:				Any					)->Any:
      for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __SCREAMING_SNAKE_CASE							:     Any							=						ErnieMModel.from_pretrained(UpperCamelCase					)
       self.assertIsNotNone(UpperCamelCase					)






@require_torch
class    _SCREAMING_SNAKE_CASE						(unittest.TestCase			):





     @slow
     def 						__snake_case     (							self			:				str					)->Union[str, Any]:
      __SCREAMING_SNAKE_CASE							:     List[Any]							=						ErnieMModel.from_pretrained("susnato/ernie-m-base_pytorch"					)
      model.eval()
      __SCREAMING_SNAKE_CASE							:     int							=						torch.tensor([[0, 1, 2, 3, 4, 5]]					)
      __SCREAMING_SNAKE_CASE							:     str							=						model(UpperCamelCase					)[0]

      # TODO Replace vocab size
      __SCREAMING_SNAKE_CASE							:     Dict							=						7_6_8

      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						torch.Size((1, 6, hidden_size)					)
      self.assertEqual(output.shape  ,	UpperCamelCase					)

      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						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]  ,	UpperCamelCase  ,	atol=1E-3					)					)













'''simple docstring'''







from typing import TYPE_CHECKING

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


snake_case_  :							Any            =	{
    "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_  :							str            =	[
					    "DPRContextEncoderTokenizerFast",
					    "DPRQuestionEncoderTokenizerFast",
					    "DPRReaderTokenizerFast",
					]

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					snake_case_  :							Optional[int]            =	[
					    "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_  :							Dict            =	[
					    "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_  :							List[str]            =	_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)





