



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		,       _snake_case		,       _snake_case=13		,       _snake_case=7		,       _snake_case=True		,       _snake_case=True		,       _snake_case=True		,       _snake_case=99		,       _snake_case=32		,       _snake_case=5		,       _snake_case=4		,       _snake_case=37		,       _snake_case="gelu"		,       _snake_case=0.1		,       _snake_case=0.1		,       _snake_case=512		,       _snake_case=16		,       _snake_case=2		,       _snake_case=0.02		,       _snake_case=3		,       _snake_case=4		,       _snake_case=None		,       ):



									"""simple docstring"""





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




				def 							snake_case     (							self	):



									"""simple docstring"""





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

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

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

									_lowerCAmelCase      			=					self.get_config()

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




				def 							snake_case     (							self	):



									"""simple docstring"""





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

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

									return config, input_ids, input_mask




				def 							snake_case     (							self	):



									"""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 							snake_case     (							self		,       _snake_case		,       _snake_case		,       _snake_case		,       _snake_case		,       _snake_case		,       _snake_case	):



									"""simple docstring"""





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




				def 							snake_case     (							self		,       _snake_case		,       _snake_case		,       _snake_case		,       _snake_case		,       _snake_case		,       _snake_case	):



									"""simple docstring"""





									_lowerCAmelCase      			=					ErnieMForQuestionAnswering(config=_snake_case	)
									model.to(_snake_case	)
									model.eval()
									_lowerCAmelCase      			=					model(
									    _snake_case		,       attention_mask=_snake_case		,       start_positions=_snake_case		,       end_positions=_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		,       _snake_case		,       _snake_case		,       _snake_case		,       _snake_case		,       _snake_case		,       _snake_case	):



									"""simple docstring"""





									_lowerCAmelCase      			=					ErnieMForInformationExtraction(config=_snake_case	)
									model.to(_snake_case	)
									model.eval()
									_lowerCAmelCase      			=					torch.ones_like(_snake_case		,       dtype=torch.floataa	)
									_lowerCAmelCase      			=					model(
									    _snake_case		,       attention_mask=_snake_case		,       start_positions=_snake_case		,       end_positions=_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		,       _snake_case		,       _snake_case		,       _snake_case		,       _snake_case		,       _snake_case		,       _snake_case	):



									"""simple docstring"""





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




				def 							snake_case     (							self		,       _snake_case		,       _snake_case		,       _snake_case		,       _snake_case		,       _snake_case		,       _snake_case	):



									"""simple docstring"""





									_lowerCAmelCase      			=					self.num_labels
									_lowerCAmelCase      			=					ErnieMForTokenClassification(config=_snake_case	)
									model.to(_snake_case	)
									model.eval()
									input_ids.to(_snake_case	)
									input_mask.to(_snake_case	)
									token_labels.to(_snake_case	)

									_lowerCAmelCase      			=					model(_snake_case		,       attention_mask=_snake_case		,       labels=_snake_case	)

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




				def 							snake_case     (							self		,       _snake_case		,       _snake_case		,       _snake_case		,       _snake_case		,       _snake_case		,       _snake_case	):



									"""simple docstring"""





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




				def 							snake_case     (							self	):



									"""simple docstring"""





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





@require_torch
class       __lowerCAmelCase				(  lowerCamelCase__ ,		lowerCamelCase__ ,		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		,       _snake_case		,       _snake_case		,       _snake_case		,       _snake_case		,       _snake_case	):



									"""simple docstring"""





									if pipeline_test_casse_name == "QAPipelineTests":
														return True

									return False




				def 							snake_case     (							self	):



									"""simple docstring"""





									_lowerCAmelCase      			=					ErnieMModelTester(self	)
									_lowerCAmelCase      			=					ConfigTester(self		,       config_class=_snake_case		,       hidden_size=37	)




				def 							snake_case     (							self	):



									"""simple docstring"""





									self.config_tester.run_common_tests()




				def 							snake_case     (							self	):



									"""simple docstring"""





									_lowerCAmelCase      			=					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_snake_case	)




				def 							snake_case     (							self	):



									"""simple docstring"""





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




				def 							snake_case     (							self	):



									"""simple docstring"""





									_lowerCAmelCase      			=					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_multiple_choice(*_snake_case	)




				def 							snake_case     (							self	):



									"""simple docstring"""





									_lowerCAmelCase      			=					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*_snake_case	)




				def 							snake_case     (							self	):



									"""simple docstring"""





									_lowerCAmelCase      			=					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*_snake_case	)




				def 							snake_case     (							self	):



									"""simple docstring"""





									_lowerCAmelCase      			=					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_information_extraction(*_snake_case	)




				def 							snake_case     (							self	):



									"""simple docstring"""





									_lowerCAmelCase      			=					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*_snake_case	)




				@slow
				def 							snake_case     (							self	):



									"""simple docstring"""





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





@require_torch
class       __lowerCAmelCase				(  unittest.TestCase ):




				@slow
				def 							snake_case     (							self	):



									"""simple docstring"""





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

									# TODO Replace vocab size
									_lowerCAmelCase      			=					768

									_lowerCAmelCase      			=					torch.Size((1, 6, hidden_size)	)
									self.assertEqual(output.shape		,       _snake_case	)

									_lowerCAmelCase      			=					torch.tensor(
									    [[[-0.0012, 0.1245, -0.0214], [-0.0742, 0.0244, -0.0771], [-0.0333, 0.1164, -0.1554]]]	)

									self.assertTrue(torch.allclose(output[:, :3, :3]		,       _snake_case		,       atol=1e-3	)	)








# tests directory-specific settings - this file is run automatically
# by pytest before any tests are run

import sys
import warnings
from os.path import abspath, dirname, join


# allow having multiple repository checkouts and not needing to remember to rerun
# 'pip install -e .[dev]' when switching between checkouts and running tests.
A__			   = abspath(join(dirname(dirname(dirname(__file__))), """src"""))
sys.path.insert(1, git_repo_path)


# silence FutureWarning warnings in tests since often we can't act on them until
# they become normal warnings - i.e. the tests still need to test the current functionality
warnings.simplefilter(action="""ignore""", category=FutureWarning)


def 	_UpperCAmelCase       (   snake_case    ):





					"""simple docstring"""

					from transformers.testing_utils import pytest_addoption_shared

					pytest_addoption_shared(snake_case    )


def 	_UpperCAmelCase       (   snake_case    ):





					"""simple docstring"""

					from transformers.testing_utils import pytest_terminal_summary_main

					_lowerCAmelCase      			=					terminalreporter.config.getoption("""--make-reports"""    )
					if make_reports:
										pytest_terminal_summary_main(snake_case			,		id=snake_case    )



