





"""simple docstring"""






import os
import tempfile
import unittest

from transformers import NezhaConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import (
		    MODEL_FOR_PRETRAINING_MAPPING,
		    NezhaForMaskedLM,
		    NezhaForMultipleChoice,
		    NezhaForNextSentencePrediction,
		    NezhaForPreTraining,
		    NezhaForQuestionAnswering,
		    NezhaForSequenceClassification,
		    NezhaForTokenClassification,
		    NezhaModel,
		)
		from transformers.models.nezha.modeling_nezha import NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST




class 				__lowercase			:







					'''simple docstring'''




					def __init__(						self	,			_UpperCAmelCase	,			_UpperCAmelCase=13	,			_UpperCAmelCase=7	,			_UpperCAmelCase=True	,			_UpperCAmelCase=True	,			_UpperCAmelCase=True	,			_UpperCAmelCase=True	,			_UpperCAmelCase=99	,			_UpperCAmelCase=32	,			_UpperCAmelCase=5	,			_UpperCAmelCase=4	,			_UpperCAmelCase=37	,			_UpperCAmelCase="gelu"	,			_UpperCAmelCase=0.1	,			_UpperCAmelCase=0.1	,			_UpperCAmelCase=128	,			_UpperCAmelCase=32	,			_UpperCAmelCase=16	,			_UpperCAmelCase=2	,			_UpperCAmelCase=0.0_2	,			_UpperCAmelCase=3	,			_UpperCAmelCase=4	,			_UpperCAmelCase=None	,			):
									__a :						List[Any]			       =  parent
									__a :						Union[str, Any]			       =  batch_size
									__a :						List[Any]			       =  seq_length
									__a :						Optional[Any]			       =  is_training
									__a :						str			       =  use_input_mask
									__a :						Any			       =  use_token_type_ids
									__a :						Tuple			       =  use_labels
									__a :						int			       =  vocab_size
									__a :						int			       =  hidden_size
									__a :						List[Any]			       =  num_hidden_layers
									__a :						Optional[int]			       =  num_attention_heads
									__a :						Tuple			       =  intermediate_size
									__a :						Union[str, Any]			       =  hidden_act
									__a :						Tuple			       =  hidden_dropout_prob
									__a :						Tuple			       =  attention_probs_dropout_prob
									__a :						Dict			       =  max_position_embeddings
									__a :						List[Any]			       =  type_vocab_size
									__a :						List[str]			       =  type_sequence_label_size
									__a :						Optional[Any]			       =  initializer_range
									__a :						Union[str, Any]			       =  num_labels
									__a :						Union[str, Any]			       =  num_choices
									__a :						Union[str, Any]			       =  scope







					def  _lowerCamelCase		(						self     ):
									__a :						List[str]			       =  ids_tensor([self.batch_size, self.seq_length]	,			self.vocab_size     )

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

									__a :						Optional[int]			       =  None
									if self.use_token_type_ids:
													__a :						Tuple			       =  ids_tensor([self.batch_size, self.seq_length]	,			self.type_vocab_size     )

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

									__a :						int			       =  self.get_config()

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







					def  _lowerCamelCase		(						self     ):
									return NezhaConfig(
									    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	,			is_decoder=_a	,			initializer_range=self.initializer_range	,			)







					def  _lowerCamelCase		(						self     ):
									(
									    __a		
									) :						List[Any]			       =  self.prepare_config_and_inputs()

									__a :						Tuple			       =  True
									__a :						Optional[Any]			       =  floats_tensor([self.batch_size, self.seq_length, self.hidden_size]     )
									__a :						Union[str, Any]			       =  ids_tensor([self.batch_size, self.seq_length]	,			vocab_size=2     )

									return (
									    config,
									    input_ids,
									    token_type_ids,
									    input_mask,
									    sequence_labels,
									    token_labels,
									    choice_labels,
									    encoder_hidden_states,
									    encoder_attention_mask,
									)







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									__a :						List[str]			       =  NezhaModel(config=_a     )
									model.to(_a     )
									model.eval()
									__a :						int			       =  model(_a	,			attention_mask=_a	,			token_type_ids=_a     )
									__a :						List[str]			       =  model(_a	,			token_type_ids=_a     )
									__a :						List[str]			       =  model(_a     )
									self.parent.assertEqual(result.last_hidden_state.shape	,			(self.batch_size, self.seq_length, self.hidden_size)     )
									self.parent.assertEqual(result.pooler_output.shape	,			(self.batch_size, self.hidden_size)     )







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			):
									__a :						List[str]			       =  True
									__a :						List[str]			       =  NezhaModel(_a     )
									model.to(_a     )
									model.eval()
									__a :						Tuple			       =  model(
									    _a	,			attention_mask=_a	,			token_type_ids=_a	,			encoder_hidden_states=_a	,			encoder_attention_mask=_a	,			)
									__a :						List[str]			       =  model(
									    _a	,			attention_mask=_a	,			token_type_ids=_a	,			encoder_hidden_states=_a	,			)
									__a :						str			       =  model(_a	,			attention_mask=_a	,			token_type_ids=_a     )
									self.parent.assertEqual(result.last_hidden_state.shape	,			(self.batch_size, self.seq_length, self.hidden_size)     )
									self.parent.assertEqual(result.pooler_output.shape	,			(self.batch_size, self.hidden_size)     )







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									__a :						Optional[Any]			       =  NezhaForMaskedLM(config=_a     )
									model.to(_a     )
									model.eval()
									__a :						Any			       =  model(_a	,			attention_mask=_a	,			token_type_ids=_a	,			labels=_a     )
									self.parent.assertEqual(result.logits.shape	,			(self.batch_size, self.seq_length, self.vocab_size)     )







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									__a :						str			       =  NezhaForNextSentencePrediction(config=_a     )
									model.to(_a     )
									model.eval()
									__a :						Tuple			       =  model(
									    _a	,			attention_mask=_a	,			token_type_ids=_a	,			labels=_a	,			)
									self.parent.assertEqual(result.logits.shape	,			(self.batch_size, 2)     )







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									__a :						List[Any]			       =  NezhaForPreTraining(config=_a     )
									model.to(_a     )
									model.eval()
									__a :						Union[str, Any]			       =  model(
									    _a	,			attention_mask=_a	,			token_type_ids=_a	,			labels=_a	,			next_sentence_label=_a	,			)
									self.parent.assertEqual(result.prediction_logits.shape	,			(self.batch_size, self.seq_length, self.vocab_size)     )
									self.parent.assertEqual(result.seq_relationship_logits.shape	,			(self.batch_size, 2)     )







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									__a :						Any			       =  NezhaForQuestionAnswering(config=_a     )
									model.to(_a     )
									model.eval()
									__a :						Optional[int]			       =  model(
									    _a	,			attention_mask=_a	,			token_type_ids=_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	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									__a :						Optional[Any]			       =  self.num_labels
									__a :						int			       =  NezhaForSequenceClassification(_a     )
									model.to(_a     )
									model.eval()
									__a :						Optional[int]			       =  model(_a	,			attention_mask=_a	,			token_type_ids=_a	,			labels=_a     )
									self.parent.assertEqual(result.logits.shape	,			(self.batch_size, self.num_labels)     )







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									__a :						str			       =  self.num_labels
									__a :						Dict			       =  NezhaForTokenClassification(config=_a     )
									model.to(_a     )
									model.eval()
									__a :						Dict			       =  model(_a	,			attention_mask=_a	,			token_type_ids=_a	,			labels=_a     )
									self.parent.assertEqual(result.logits.shape	,			(self.batch_size, self.seq_length, self.num_labels)     )







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									__a :						Any			       =  self.num_choices
									__a :						str			       =  NezhaForMultipleChoice(config=_a     )
									model.to(_a     )
									model.eval()
									__a :						List[Any]			       =  input_ids.unsqueeze(1     ).expand(-1	,			self.num_choices	,			-1     ).contiguous()
									__a :						Optional[Any]			       =  token_type_ids.unsqueeze(1     ).expand(-1	,			self.num_choices	,			-1     ).contiguous()
									__a :						str			       =  input_mask.unsqueeze(1     ).expand(-1	,			self.num_choices	,			-1     ).contiguous()
									__a :						Any			       =  model(
									    _a	,			attention_mask=_a	,			token_type_ids=_a	,			labels=_a	,			)
									self.parent.assertEqual(result.logits.shape	,			(self.batch_size, self.num_choices)     )







					def  _lowerCamelCase		(						self     ):
									__a :						int			       =  self.prepare_config_and_inputs()
									(
									    __a		
									) :						str			       =  config_and_inputs
									__a :						Union[str, Any]			       =  {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
									return config, inputs_dict






@require_torch
class 				__lowercase			(			UpperCamelCase_   ,      UpperCamelCase_   ,      UpperCamelCase_   ,      unittest.TestCase							):







					'''simple docstring'''




					__lowerCAmelCase  =  (
					    (
					        NezhaModel,
					        NezhaForMaskedLM,
					        NezhaForMultipleChoice,
					        NezhaForNextSentencePrediction,
					        NezhaForPreTraining,
					        NezhaForQuestionAnswering,
					        NezhaForSequenceClassification,
					        NezhaForTokenClassification,
					    )
					    if is_torch_available()
					    else ()
					)
					__lowerCAmelCase  =  (
					    {
					        """feature-extraction""": NezhaModel,
					        """fill-mask""": NezhaForMaskedLM,
					        """question-answering""": NezhaForQuestionAnswering,
					        """text-classification""": NezhaForSequenceClassification,
					        """token-classification""": NezhaForTokenClassification,
					        """zero-shot""": NezhaForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					__lowerCAmelCase  =  True







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase=False     ):
									__a :						Optional[int]			       =  super()._prepare_for_class(_a	,			_a	,			return_labels=_a     )

									if return_labels:
													if model_class in get_values(_a     ):
																	__a :						Tuple			       =  torch.zeros(
																	    (self.model_tester.batch_size, self.model_tester.seq_length)	,			dtype=torch.long	,			device=_a     )
																	__a :						Tuple			       =  torch.zeros(
																	    self.model_tester.batch_size	,			dtype=torch.long	,			device=_a     )
									return inputs_dict







					def  _lowerCamelCase		(						self     ):
									__a :						Tuple			       =  NezhaModelTester(self     )
									__a :						int			       =  ConfigTester(self	,			config_class=_a	,			hidden_size=37     )







					def  _lowerCamelCase		(						self     ):
									self.config_tester.run_common_tests()







					def  _lowerCamelCase		(						self     ):
									__a :						List[Any]			       =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_a     )







					def  _lowerCamelCase		(						self     ):
									__a :						Dict			       =  self.model_tester.prepare_config_and_inputs_for_decoder()
									self.model_tester.create_and_check_model_as_decoder(*_a     )







					def  _lowerCamelCase		(						self     ):
									(
									    __a		
									) :						Optional[int]			       =  self.model_tester.prepare_config_and_inputs_for_decoder()

									__a :						Any			       =  None

									self.model_tester.create_and_check_model_as_decoder(
									    _a	,			_a	,			_a	,			_a	,			_a	,			_a	,			_a	,			_a	,			_a	,			)







					def  _lowerCamelCase		(						self     ):
									__a :						str			       =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_lm(*_a     )







					def  _lowerCamelCase		(						self     ):
									__a :						List[str]			       =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_multiple_choice(*_a     )







					def  _lowerCamelCase		(						self     ):
									__a :						int			       =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_next_sequence_prediction(*_a     )







					def  _lowerCamelCase		(						self     ):
									__a :						List[Any]			       =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_pretraining(*_a     )







					def  _lowerCamelCase		(						self     ):
									__a :						Optional[Any]			       =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*_a     )







					def  _lowerCamelCase		(						self     ):
									__a :						Any			       =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*_a     )







					def  _lowerCamelCase		(						self     ):
									__a :						int			       =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*_a     )







					@slow
					def  _lowerCamelCase		(						self     ):


									for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__a :						Optional[Any]			       =  NezhaModel.from_pretrained(_a     )
													self.assertIsNotNone(_a     )







					@slow
					@require_torch_gpu
					def  _lowerCamelCase		(						self     ):
									__a :						str			       =  self.model_tester.prepare_config_and_inputs_for_common()
									for model_class in self.all_model_classes:
													# NezhaForMultipleChoice behaves incorrectly in JIT environments.
													if model_class == NezhaForMultipleChoice:
																	return

													__a :						Optional[int]			       =  True
													__a :						Optional[Any]			       =  model_class(config=_a     )

													__a :						Any			       =  self._prepare_for_class(_a	,			_a     )
													__a :						Union[str, Any]			       =  torch.jit.trace(
													    _a	,			(inputs_dict['''input_ids'''].to('''cpu'''     ), inputs_dict['''attention_mask'''].to('''cpu'''     ))     )

													with tempfile.TemporaryDirectory() as tmp:
																	torch.jit.save(_a	,			os.path.join(_a	,			'''bert.pt'''     )     )
																	__a :						Any			       =  torch.jit.load(os.path.join(_a	,			'''bert.pt'''     )	,			map_location=_a     )
																	loaded(inputs_dict['''input_ids'''].to(_a     )	,			inputs_dict['''attention_mask'''].to(_a     )     )






@require_torch
class 				__lowercase			(			unittest.TestCase							):







					'''simple docstring'''




					@slow
					def  _lowerCamelCase		(						self     ):
									__a :						str			       =  NezhaModel.from_pretrained('''sijunhe/nezha-cn-base'''     )
									__a :						Union[str, Any]			       =  torch.tensor([[0, 1, 2, 3, 4, 5]]     )
									__a :						str			       =  torch.tensor([[0, 1, 1, 1, 1, 1]]     )
									with torch.no_grad():
													__a :						Dict			       =  model(_a	,			attention_mask=_a     )[0]
									__a :						str			       =  torch.Size((1, 6, 768)     )
									self.assertEqual(output.shape	,			_a     )
									__a :						int			       =  torch.tensor([[[0.0_6_8_5, 0.2_4_4_1, 0.1_1_0_2], [0.0_6_0_0, 0.1_9_0_6, 0.1_3_4_9], [0.0_2_2_1, 0.0_8_1_9, 0.0_5_8_6]]]     )

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







					@slow
					def  _lowerCamelCase		(						self     ):
									__a :						List[str]			       =  NezhaForMaskedLM.from_pretrained('''sijunhe/nezha-cn-base'''     )
									__a :						Union[str, Any]			       =  torch.tensor([[0, 1, 2, 3, 4, 5]]     )
									__a :						List[str]			       =  torch.tensor([[1, 1, 1, 1, 1, 1]]     )
									with torch.no_grad():
													__a :						Dict			       =  model(_a	,			attention_mask=_a     )[0]
									__a :						List[str]			       =  torch.Size((1, 6, 21128)     )
									self.assertEqual(output.shape	,			_a     )
									__a :						Union[str, Any]			       =  torch.tensor(
									    [[-2.7_9_3_9, -1.7_9_0_2, -2.2_1_8_9], [-2.8_5_8_5, -1.8_9_0_8, -2.3_7_2_3], [-2.6_4_9_9, -1.7_7_5_0, -2.2_5_5_8]]     )

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






"""simple docstring"""






# 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     __A      (			a_						:Tuple)		->		Dict:
				from transformers.testing_utils import pytest_addoption_shared

				pytest_addoption_shared(a_)






def     __A      (			a_						:Any)		->		int:
				from transformers.testing_utils import pytest_terminal_summary_main

				__a :						str			       =  terminalreporter.config.getoption('''--make-reports''')
				if make_reports:
								pytest_terminal_summary_main(a_   ,       id=a_)