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





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





		def       _A							(							self    :	Optional[Any]				):
							lowerCAmelCase		:					List[Any]  	=     ids_tensor([self.batch_size, self.seq_length]						,  self.vocab_size				)

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

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

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

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





		def       _A							(							self    :	str				):
							lowerCAmelCase		:					Optional[Any]  	=     ids_tensor([self.batch_size, self.seq_length]						,  self.vocab_size				)

							lowerCAmelCase		:					List[str]  	=     None
							if self.use_input_mask:
												lowerCAmelCase		:					Dict  	=     random_attention_mask([self.batch_size, self.seq_length]				)
							lowerCAmelCase		:					Dict  	=     self.get_config()

							return config, input_ids, input_mask





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





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





		def       _A							(							self    :	Dict						,  lowerCamelCase__    :	Tuple						,  lowerCamelCase__    :	Optional[Any]						,  lowerCamelCase__    :	int						,  lowerCamelCase__    :	List[Any]						,  lowerCamelCase__    :	Optional[Any]						,  lowerCamelCase__    :	Union[str, Any]				):
							lowerCAmelCase		:					List[str]  	=     self.num_labels
							lowerCAmelCase		:					int  	=     ErnieMForTokenClassification(config=lowerCamelCase__				)
							model.to(lowerCamelCase__				)
							model.eval()
							input_ids.to(lowerCamelCase__				)
							input_mask.to(lowerCamelCase__				)
							token_labels.to(lowerCamelCase__				)

							lowerCAmelCase		:					Optional[Any]  	=     model(lowerCamelCase__						,  attention_mask=lowerCamelCase__						,  labels=lowerCamelCase__				)

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





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





		def       _A							(							self    :	int				):
							lowerCAmelCase		:					Tuple  	=     self.prepare_config_and_inputs()
							(
							    lowerCAmelCase							
							)		:					Tuple  	=     config_and_inputs
							lowerCAmelCase		:					str  	=     {'''input_ids''': input_ids, '''attention_mask''': input_mask}
							return config, inputs_dict



@require_torch
class 		__magic_name__   (						snake_case,				snake_case,				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       _A							(							self    :	int						,  lowerCamelCase__    :	str						,  lowerCamelCase__    :	str						,  lowerCamelCase__    :	int						,  lowerCamelCase__    :	Optional[int]						,  lowerCamelCase__    :	Tuple				):
							if pipeline_test_casse_name == "QAPipelineTests":
												return True

							return False





		def       _A							(							self    :	List[str]				):
							lowerCAmelCase		:					str  	=     ErnieMModelTester(self				)
							lowerCAmelCase		:					Union[str, Any]  	=     ConfigTester(self						,  config_class=lowerCamelCase__						,  hidden_size=3_7				)





		def       _A							(							self    :	Union[str, Any]				):
							self.config_tester.run_common_tests()





		def       _A							(							self    :	Union[str, Any]				):
							lowerCAmelCase		:					str  	=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowerCamelCase__				)





		def       _A							(							self    :	str				):
							lowerCAmelCase		:					Union[str, Any]  	=     self.model_tester.prepare_config_and_inputs()
							for type in ["absolute", "relative_key", "relative_key_query"]:
												lowerCAmelCase		:					Optional[Any]  	=     type
												self.model_tester.create_and_check_model(*lowerCamelCase__				)





		def       _A							(							self    :	Union[str, Any]				):
							lowerCAmelCase		:					Optional[int]  	=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_multiple_choice(*lowerCamelCase__				)





		def       _A							(							self    :	Optional[Any]				):
							lowerCAmelCase		:					Dict  	=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*lowerCamelCase__				)





		def       _A							(							self    :	Any				):
							lowerCAmelCase		:					Optional[int]  	=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase__				)





		def       _A							(							self    :	str				):
							lowerCAmelCase		:					int  	=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_information_extraction(*lowerCamelCase__				)





		def       _A							(							self    :	int				):
							lowerCAmelCase		:					Tuple  	=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*lowerCamelCase__				)





		@slow
		def       _A							(							self    :	List[Any]				):
							for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												lowerCAmelCase		:					int  	=     ErnieMModel.from_pretrained(lowerCamelCase__				)
												self.assertIsNotNone(lowerCamelCase__				)



@require_torch
class 		__magic_name__   (						unittest.TestCase    ):





		@slow
		def       _A							(							self    :	List[str]				):
							lowerCAmelCase		:					Optional[Any]  	=     ErnieMModel.from_pretrained('''susnato/ernie-m-base_pytorch'''				)
							model.eval()
							lowerCAmelCase		:					List[str]  	=     torch.tensor([[0, 1, 2, 3, 4, 5]]				)
							lowerCAmelCase		:					Union[str, Any]  	=     model(lowerCamelCase__				)[0]

							# TODO Replace vocab size
							lowerCAmelCase		:					Optional[Any]  	=     7_6_8

							lowerCAmelCase		:					str  	=     torch.Size((1, 6, hidden_size)				)
							self.assertEqual(output.shape						,  lowerCamelCase__				)

							lowerCAmelCase		:					List[str]  	=     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]						,  lowerCamelCase__						,  atol=1E-3				)				)


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


__SCREAMING_SNAKE_CASE			:   List[str]         =     {'configuration_vilt': ['VILT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ViltConfig']}

try:
		if not is_vision_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__SCREAMING_SNAKE_CASE			:   Any         =     ['ViltFeatureExtractor']
		__SCREAMING_SNAKE_CASE			:   Optional[int]         =     ['ViltImageProcessor']
		__SCREAMING_SNAKE_CASE			:   str         =     ['ViltProcessor']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__SCREAMING_SNAKE_CASE			:   Dict         =     [
		    'VILT_PRETRAINED_MODEL_ARCHIVE_LIST',
		    'ViltForImageAndTextRetrieval',
		    'ViltForImagesAndTextClassification',
		    'ViltForTokenClassification',
		    'ViltForMaskedLM',
		    'ViltForQuestionAnswering',
		    'ViltLayer',
		    'ViltModel',
		    'ViltPreTrainedModel',
		]


if TYPE_CHECKING:
		from .configuration_vilt import VILT_PRETRAINED_CONFIG_ARCHIVE_MAP, ViltConfig

		try:
				if not is_vision_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .feature_extraction_vilt import ViltFeatureExtractor
				from .image_processing_vilt import ViltImageProcessor
				from .processing_vilt import ViltProcessor

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_vilt import (
				    VILT_PRETRAINED_MODEL_ARCHIVE_LIST,
				    ViltForImageAndTextRetrieval,
				    ViltForImagesAndTextClassification,
				    ViltForMaskedLM,
				    ViltForQuestionAnswering,
				    ViltForTokenClassification,
				    ViltLayer,
				    ViltModel,
				    ViltPreTrainedModel,
				)


else:
		import sys

		__SCREAMING_SNAKE_CASE			:   Optional[Any]         =     _LazyModule(__name__, globals()['__file__'], _import_structure)
