



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






			def __init__(   self  :							Union[str, Any]      ,       SCREAMING_SNAKE_CASE  :							List[Any]      ,       SCREAMING_SNAKE_CASE  :							Dict=13      ,       SCREAMING_SNAKE_CASE  :							List[str]=7      ,       SCREAMING_SNAKE_CASE  :							Optional[int]=True      ,       SCREAMING_SNAKE_CASE  :							Any=True      ,       SCREAMING_SNAKE_CASE  :							Optional[int]=True      ,       SCREAMING_SNAKE_CASE  :							str=99      ,       SCREAMING_SNAKE_CASE  :							int=32      ,       SCREAMING_SNAKE_CASE  :							Any=5      ,       SCREAMING_SNAKE_CASE  :							Optional[Any]=4      ,       SCREAMING_SNAKE_CASE  :							Optional[Any]=37      ,       SCREAMING_SNAKE_CASE  :							List[Any]="gelu"      ,       SCREAMING_SNAKE_CASE  :							List[Any]=0.1      ,       SCREAMING_SNAKE_CASE  :							Any=0.1      ,       SCREAMING_SNAKE_CASE  :							Tuple=512      ,       SCREAMING_SNAKE_CASE  :							List[str]=16      ,       SCREAMING_SNAKE_CASE  :							Any=2      ,       SCREAMING_SNAKE_CASE  :							Dict=0.02      ,       SCREAMING_SNAKE_CASE  :							str=3      ,       SCREAMING_SNAKE_CASE  :							Any=4      ,       SCREAMING_SNAKE_CASE  :							Any=None      ,       ):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:int										=      parent
					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      batch_size
					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      seq_length
					SCREAMING_SNAKE_CASE_						:int										=      is_training
					SCREAMING_SNAKE_CASE_						:Any										=      use_input_mask
					SCREAMING_SNAKE_CASE_						:Optional[int]										=      use_labels
					SCREAMING_SNAKE_CASE_						:List[str]										=      vocab_size
					SCREAMING_SNAKE_CASE_						:str										=      hidden_size
					SCREAMING_SNAKE_CASE_						:List[Any]										=      num_hidden_layers
					SCREAMING_SNAKE_CASE_						:Any										=      num_attention_heads
					SCREAMING_SNAKE_CASE_						:Dict										=      intermediate_size
					SCREAMING_SNAKE_CASE_						:Optional[int]										=      hidden_act
					SCREAMING_SNAKE_CASE_						:Tuple										=      hidden_dropout_prob
					SCREAMING_SNAKE_CASE_						:Dict										=      attention_probs_dropout_prob
					SCREAMING_SNAKE_CASE_						:List[str]										=      max_position_embeddings
					SCREAMING_SNAKE_CASE_						:Dict										=      type_vocab_size
					SCREAMING_SNAKE_CASE_						:Dict										=      type_sequence_label_size
					SCREAMING_SNAKE_CASE_						:List[Any]										=      initializer_range
					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      num_labels
					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      num_choices
					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      scope






			def 				_lowercase	(   self  :							int	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Dict										=      ids_tensor([self.batch_size, self.seq_length]      ,       self.vocab_size	)

					SCREAMING_SNAKE_CASE_						:str										=      None
					if self.use_input_mask:
							SCREAMING_SNAKE_CASE_						:Any										=      random_attention_mask([self.batch_size, self.seq_length]	)

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

					SCREAMING_SNAKE_CASE_						:Dict										=      self.get_config()

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






			def 				_lowercase	(   self  :							Optional[int]	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Any										=      ids_tensor([self.batch_size, self.seq_length]      ,       self.vocab_size	)

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

					return config, input_ids, input_mask






			def 				_lowercase	(   self  :							Optional[int]	):




					"""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 				_lowercase	(   self  :							Union[str, Any]      ,       SCREAMING_SNAKE_CASE  :							Dict      ,       SCREAMING_SNAKE_CASE  :							Any      ,       SCREAMING_SNAKE_CASE  :							Any      ,       SCREAMING_SNAKE_CASE  :							Tuple      ,       SCREAMING_SNAKE_CASE  :							int      ,       SCREAMING_SNAKE_CASE  :							Optional[int]	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:str										=      ErnieMModel(config=SCREAMING_SNAKE_CASE	)
					model.to(SCREAMING_SNAKE_CASE	)
					model.eval()
					SCREAMING_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 				_lowercase	(   self  :							Union[str, Any]      ,       SCREAMING_SNAKE_CASE  :							Optional[Any]      ,       SCREAMING_SNAKE_CASE  :							Tuple      ,       SCREAMING_SNAKE_CASE  :							int      ,       SCREAMING_SNAKE_CASE  :							Tuple      ,       SCREAMING_SNAKE_CASE  :							Any      ,       SCREAMING_SNAKE_CASE  :							Dict	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Tuple										=      ErnieMForQuestionAnswering(config=SCREAMING_SNAKE_CASE	)
					model.to(SCREAMING_SNAKE_CASE	)
					model.eval()
					SCREAMING_SNAKE_CASE_						:Any										=      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 				_lowercase	(   self  :							Optional[Any]      ,       SCREAMING_SNAKE_CASE  :							Optional[Any]      ,       SCREAMING_SNAKE_CASE  :							int      ,       SCREAMING_SNAKE_CASE  :							int      ,       SCREAMING_SNAKE_CASE  :							str      ,       SCREAMING_SNAKE_CASE  :							Optional[Any]      ,       SCREAMING_SNAKE_CASE  :							Dict	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      ErnieMForInformationExtraction(config=SCREAMING_SNAKE_CASE	)
					model.to(SCREAMING_SNAKE_CASE	)
					model.eval()
					SCREAMING_SNAKE_CASE_						:Dict										=      torch.ones_like(SCREAMING_SNAKE_CASE      ,       dtype=torch.floataa	)
					SCREAMING_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 				_lowercase	(   self  :							str      ,       SCREAMING_SNAKE_CASE  :							Dict      ,       SCREAMING_SNAKE_CASE  :							List[Any]      ,       SCREAMING_SNAKE_CASE  :							Optional[Any]      ,       SCREAMING_SNAKE_CASE  :							Any      ,       SCREAMING_SNAKE_CASE  :							Any      ,       SCREAMING_SNAKE_CASE  :							str	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Optional[int]										=      self.num_labels
					SCREAMING_SNAKE_CASE_						:Any										=      ErnieMForSequenceClassification(SCREAMING_SNAKE_CASE	)
					model.to(SCREAMING_SNAKE_CASE	)
					model.eval()
					SCREAMING_SNAKE_CASE_						:Dict										=      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 				_lowercase	(   self  :							Optional[int]      ,       SCREAMING_SNAKE_CASE  :							Optional[Any]      ,       SCREAMING_SNAKE_CASE  :							Union[str, Any]      ,       SCREAMING_SNAKE_CASE  :							Dict      ,       SCREAMING_SNAKE_CASE  :							Any      ,       SCREAMING_SNAKE_CASE  :							Any      ,       SCREAMING_SNAKE_CASE  :							Optional[int]	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:int										=      self.num_labels
					SCREAMING_SNAKE_CASE_						: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	)

					SCREAMING_SNAKE_CASE_						:Any										=      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 				_lowercase	(   self  :							Union[str, Any]      ,       SCREAMING_SNAKE_CASE  :							List[Any]      ,       SCREAMING_SNAKE_CASE  :							List[str]      ,       SCREAMING_SNAKE_CASE  :							Any      ,       SCREAMING_SNAKE_CASE  :							str      ,       SCREAMING_SNAKE_CASE  :							int      ,       SCREAMING_SNAKE_CASE  :							str	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      self.num_choices
					SCREAMING_SNAKE_CASE_						:str										=      ErnieMForMultipleChoice(config=SCREAMING_SNAKE_CASE	)
					model.to(SCREAMING_SNAKE_CASE	)
					model.eval()
					SCREAMING_SNAKE_CASE_						:int										=      input_ids.unsqueeze(1	).expand(-1      ,       self.num_choices      ,       -1	).contiguous()
					SCREAMING_SNAKE_CASE_						:Dict										=      input_mask.unsqueeze(1	).expand(-1      ,       self.num_choices      ,       -1	).contiguous()
					SCREAMING_SNAKE_CASE_						: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_choices)	)






			def 				_lowercase	(   self  :							List[Any]	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Any										=      self.prepare_config_and_inputs()
					(
					    (
					    SCREAMING_SNAKE_CASE_
					)						,		(
					    SCREAMING_SNAKE_CASE_
					)						,		(
					    SCREAMING_SNAKE_CASE_
					)						,		(
					    SCREAMING_SNAKE_CASE_
					)						,		(
					    SCREAMING_SNAKE_CASE_
					)						,		(
					    SCREAMING_SNAKE_CASE_
					)						,		
					)						:Tuple										=      config_and_inputs
					SCREAMING_SNAKE_CASE_						:List[str]										=      {'input_ids': input_ids, 'attention_mask': input_mask}
					return config, inputs_dict




@require_torch
class 				__lowerCAmelCase(							lowerCAmelCase__		,			lowerCAmelCase__		,			unittest.TestCase      ):
			__snake_case  :		Dict		=      (
			    (
			        ErnieMModel,
			        ErnieMForMultipleChoice,
			        ErnieMForQuestionAnswering,
			        ErnieMForSequenceClassification,
			        ErnieMForTokenClassification,
			    )
			    if is_torch_available()
			    else ()
			)
			__snake_case  :		Union[str, Any]		=      ()
			__snake_case  :		List[str]		=      (
			    {
			        'feature-extraction': ErnieMModel,
			        'question-answering': ErnieMForQuestionAnswering,
			        'text-classification': ErnieMForSequenceClassification,
			        'token-classification': ErnieMForTokenClassification,
			        'zero-shot': ErnieMForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)
			__snake_case  :		Union[str, Any]		=      False






			def 				_lowercase	(   self  :							Optional[Any]      ,       SCREAMING_SNAKE_CASE  :							Tuple      ,       SCREAMING_SNAKE_CASE  :							Optional[int]      ,       SCREAMING_SNAKE_CASE  :							Union[str, Any]      ,       SCREAMING_SNAKE_CASE  :							List[str]      ,       SCREAMING_SNAKE_CASE  :							int	):




					"""simple docstring"""







					if pipeline_test_casse_name == "QAPipelineTests":
							return True

					return False






			def 				_lowercase	(   self  :							Union[str, Any]	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      ErnieMModelTester(self	)
					SCREAMING_SNAKE_CASE_						:Any										=      ConfigTester(self      ,       config_class=SCREAMING_SNAKE_CASE      ,       hidden_size=37	)






			def 				_lowercase	(   self  :							List[str]	):




					"""simple docstring"""







					self.config_tester.run_common_tests()






			def 				_lowercase	(   self  :							List[Any]	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:str										=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE	)






			def 				_lowercase	(   self  :							List[str]	):




					"""simple docstring"""







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






			def 				_lowercase	(   self  :							Any	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Dict										=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_multiple_choice(*SCREAMING_SNAKE_CASE	)






			def 				_lowercase	(   self  :							List[Any]	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Any										=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(*SCREAMING_SNAKE_CASE	)






			def 				_lowercase	(   self  :							Dict	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(*SCREAMING_SNAKE_CASE	)






			def 				_lowercase	(   self  :							Optional[Any]	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_information_extraction(*SCREAMING_SNAKE_CASE	)






			def 				_lowercase	(   self  :							List[Any]	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Tuple										=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(*SCREAMING_SNAKE_CASE	)






			@slow
			def 				_lowercase	(   self  :							Dict	):




					"""simple docstring"""







					for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      ErnieMModel.from_pretrained(SCREAMING_SNAKE_CASE	)
							self.assertIsNotNone(SCREAMING_SNAKE_CASE	)




@require_torch
class 				__lowerCAmelCase(							unittest.TestCase      ):






			@slow
			def 				_lowercase	(   self  :							int	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Dict										=      ErnieMModel.from_pretrained('susnato/ernie-m-base_pytorch'	)
					model.eval()
					SCREAMING_SNAKE_CASE_						:Optional[int]										=      torch.tensor([[0, 1, 2, 3, 4, 5]]	)
					SCREAMING_SNAKE_CASE_						:List[str]										=      model(SCREAMING_SNAKE_CASE	)[0]

					# TODO Replace vocab size
					SCREAMING_SNAKE_CASE_						:Optional[int]										=      768

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

					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      torch.tensor(
					    [[[-0.00_12, 0.12_45, -0.02_14], [-0.07_42, 0.02_44, -0.07_71], [-0.03_33, 0.11_64, -0.15_54]]]	)

					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, is_vision_available


SCREAMING_SNAKE_CASE__     : Dict				=   {"""configuration_detr""": ["""DETR_PRETRAINED_CONFIG_ARCHIVE_MAP""", """DetrConfig""", """DetrOnnxConfig"""]}

try:
							if not is_vision_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							SCREAMING_SNAKE_CASE__     : List[Any]				=   ["""DetrFeatureExtractor"""]
							SCREAMING_SNAKE_CASE__     : str				=   ["""DetrImageProcessor"""]

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							SCREAMING_SNAKE_CASE__     : Optional[Any]				=   [
							    """DETR_PRETRAINED_MODEL_ARCHIVE_LIST""",
							    """DetrForObjectDetection""",
							    """DetrForSegmentation""",
							    """DetrModel""",
							    """DetrPreTrainedModel""",
							]


if TYPE_CHECKING:
							from .configuration_detr import DETR_PRETRAINED_CONFIG_ARCHIVE_MAP, DetrConfig, DetrOnnxConfig

							try:
														if not is_vision_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .feature_extraction_detr import DetrFeatureExtractor
														from .image_processing_detr import DetrImageProcessor

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_detr import (
														    DETR_PRETRAINED_MODEL_ARCHIVE_LIST,
														    DetrForObjectDetection,
														    DetrForSegmentation,
														    DetrModel,
														    DetrPreTrainedModel,
														)

else:
							import sys

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