





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






				"""simple docstring"""







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




				def   UpperCamelCase__   (  self     :					List[Any]					):
								_a								= ids_tensor([self.batch_size, self.seq_length]					,				self.vocab_size					)

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

								_a								= None
								if self.use_token_type_ids:
												_a								= ids_tensor([self.batch_size, self.seq_length]					,				self.type_vocab_size					)

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

								_a								= self.get_config()

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




				def   UpperCamelCase__   (  self     :					List[str]					):
								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   UpperCamelCase__   (  self     :					List[str]					):
								(
								    (
								    _a
								)	,					(
								    _a
								)	,					(
								    _a
								)	,					(
								    _a
								)	,					(
								    _a
								)	,					(
								    _a
								)	,					(
								    _a
								)	,					
								)								= self.prepare_config_and_inputs()

								_a								= True
								_a								= floats_tensor([self.batch_size, self.seq_length, self.hidden_size]					)
								_a								= 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   UpperCamelCase__   (  self     :					Tuple					,				__a     :					Optional[Any]					,				__a     :					Tuple					,				__a     :					Tuple					,				__a     :					Union[str, Any]					,				__a     :					Tuple					,				__a     :					List[Any]					,				__a     :					str					):
								_a								= NezhaModel(config=__a					)
								model.to(__a					)
								model.eval()
								_a								= model(__a					,				attention_mask=__a					,				token_type_ids=__a					)
								_a								= model(__a					,				token_type_ids=__a					)
								_a								= 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   UpperCamelCase__   (  self     :					Optional[Any]					,				__a     :					Tuple					,				__a     :					List[Any]					,				__a     :					Any					,				__a     :					List[str]					,				__a     :					int					,				__a     :					Dict					,				__a     :					Optional[int]					,				__a     :					Dict					,				__a     :					int					,				):
								_a								= True
								_a								= NezhaModel(__a					)
								model.to(__a					)
								model.eval()
								_a								= model(
								    __a					,				attention_mask=__a					,				token_type_ids=__a					,				encoder_hidden_states=__a					,				encoder_attention_mask=__a					,				)
								_a								= model(
								    __a					,				attention_mask=__a					,				token_type_ids=__a					,				encoder_hidden_states=__a					,				)
								_a								= 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   UpperCamelCase__   (  self     :					Optional[Any]					,				__a     :					Optional[Any]					,				__a     :					Dict					,				__a     :					str					,				__a     :					Any					,				__a     :					Dict					,				__a     :					Optional[int]					,				__a     :					List[str]					):
								_a								= NezhaForMaskedLM(config=__a					)
								model.to(__a					)
								model.eval()
								_a								= 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   UpperCamelCase__   (  self     :					List[Any]					,				__a     :					Union[str, Any]					,				__a     :					str					,				__a     :					List[Any]					,				__a     :					Optional[int]					,				__a     :					Any					,				__a     :					Optional[int]					,				__a     :					Union[str, Any]					):
								_a								= NezhaForNextSentencePrediction(config=__a					)
								model.to(__a					)
								model.eval()
								_a								= model(
								    __a					,				attention_mask=__a					,				token_type_ids=__a					,				labels=__a					,				)
								self.parent.assertEqual(result.logits.shape					,				(self.batch_size, 2)					)




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




				def   UpperCamelCase__   (  self     :					str					,				__a     :					str					,				__a     :					int					,				__a     :					Optional[Any]					,				__a     :					Tuple					,				__a     :					Tuple					,				__a     :					str					,				__a     :					List[str]					):
								_a								= self.num_labels
								_a								= NezhaForTokenClassification(config=__a					)
								model.to(__a					)
								model.eval()
								_a								= 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   UpperCamelCase__   (  self     :					Union[str, Any]					,				__a     :					List[str]					,				__a     :					str					,				__a     :					Optional[int]					,				__a     :					int					,				__a     :					Any					,				__a     :					Dict					,				__a     :					Optional[int]					):
								_a								= self.num_choices
								_a								= NezhaForMultipleChoice(config=__a					)
								model.to(__a					)
								model.eval()
								_a								= input_ids.unsqueeze(1					).expand(-1					,				self.num_choices					,				-1					).contiguous()
								_a								= token_type_ids.unsqueeze(1					).expand(-1					,				self.num_choices					,				-1					).contiguous()
								_a								= input_mask.unsqueeze(1					).expand(-1					,				self.num_choices					,				-1					).contiguous()
								_a								= model(
								    __a					,				attention_mask=__a					,				token_type_ids=__a					,				labels=__a					,				)
								self.parent.assertEqual(result.logits.shape					,				(self.batch_size, self.num_choices)					)




				def   UpperCamelCase__   (  self     :					Any					):
								_a								= self.prepare_config_and_inputs()
								(
								    (
								    _a
								)	,					(
								    _a
								)	,					(
								    _a
								)	,					(
								    _a
								)	,					(
								    _a
								)	,					(
								    _a
								)	,					(
								    _a
								)	,					
								)								= config_and_inputs
								_a								= {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
								return config, inputs_dict


@require_torch
class 				__SCREAMING_SNAKE_CASE  (lowerCamelCase_       ,							lowerCamelCase_       ,							lowerCamelCase_       ,							unittest.TestCase      ):






				"""simple docstring"""







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




				def   UpperCamelCase__   (  self     :					Dict					,				__a     :					Union[str, Any]					,				__a     :					Any					,				__a     :					Optional[int]=False					):
								_a								= super()._prepare_for_class(__a					,				__a					,				return_labels=__a					)

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




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




				def   UpperCamelCase__   (  self     :					int					):
								self.config_tester.run_common_tests()




				def   UpperCamelCase__   (  self     :					Union[str, Any]					):
								_a								= self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*__a					)




				def   UpperCamelCase__   (  self     :					Union[str, Any]					):
								_a								= self.model_tester.prepare_config_and_inputs_for_decoder()
								self.model_tester.create_and_check_model_as_decoder(*__a					)




				def   UpperCamelCase__   (  self     :					str					):
								# This regression test was failing with PyTorch < 1.3
								(
								    (
								    _a
								)	,					(
								    _a
								)	,					(
								    _a
								)	,					(
								    _a
								)	,					(
								    _a
								)	,					(
								    _a
								)	,					(
								    _a
								)	,					(
								    _a
								)	,					(
								    _a
								)	,					
								)								= self.model_tester.prepare_config_and_inputs_for_decoder()

								_a								= None

								self.model_tester.create_and_check_model_as_decoder(
								    __a					,				__a					,				__a					,				__a					,				__a					,				__a					,				__a					,				__a					,				__a					,				)




				def   UpperCamelCase__   (  self     :					Optional[int]					):
								_a								= self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_lm(*__a					)




				def   UpperCamelCase__   (  self     :					Any					):
								_a								= self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_multiple_choice(*__a					)




				def   UpperCamelCase__   (  self     :					Tuple					):
								_a								= self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_next_sequence_prediction(*__a					)




				def   UpperCamelCase__   (  self     :					Tuple					):
								_a								= self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_pretraining(*__a					)




				def   UpperCamelCase__   (  self     :					Tuple					):
								_a								= self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*__a					)




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




				def   UpperCamelCase__   (  self     :					Dict					):
								_a								= self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_token_classification(*__a					)




				@slow
				def   UpperCamelCase__   (  self     :					int					):
								for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												_a								= NezhaModel.from_pretrained(__a					)
												self.assertIsNotNone(__a					)




				@slow
				@require_torch_gpu
				def   UpperCamelCase__   (  self     :					List[str]					):
								_a	,					_a								= 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								= True
												_a								= model_class(config=__a					)

												_a								= self._prepare_for_class(__a					,				__a					)
												_a								= 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								= 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 				__SCREAMING_SNAKE_CASE  (unittest.TestCase      ):






				"""simple docstring"""







				@slow
				def   UpperCamelCase__   (  self     :					List[Any]					):
								_a								= NezhaModel.from_pretrained("sijunhe/nezha-cn-base"					)
								_a								= torch.tensor([[0, 1, 2, 3, 4, 5]]					)
								_a								= torch.tensor([[0, 1, 1, 1, 1, 1]]					)
								with torch.no_grad():
												_a								= model(__a					,				attention_mask=__a					)[0]
								_a								= torch.Size((1, 6, 7_68)					)
								self.assertEqual(output.shape					,				__a					)
								_a								= torch.tensor([[[0.0685, 0.2441, 0.1102], [0.0600, 0.1906, 0.1349], [0.0221, 0.0819, 0.0586]]]					)

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




				@slow
				def   UpperCamelCase__   (  self     :					List[str]					):
								_a								= NezhaForMaskedLM.from_pretrained("sijunhe/nezha-cn-base"					)
								_a								= torch.tensor([[0, 1, 2, 3, 4, 5]]					)
								_a								= torch.tensor([[1, 1, 1, 1, 1, 1]]					)
								with torch.no_grad():
												_a								= model(__a					,				attention_mask=__a					)[0]
								_a								= torch.Size((1, 6, 2_11_28)					)
								self.assertEqual(output.shape					,				__a					)
								_a								= torch.tensor(
								    [[-2.7939, -1.7902, -2.2189], [-2.8585, -1.8908, -2.3723], [-2.6499, -1.7750, -2.2558]]					)

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







'''simple docstring'''







def  _lowerCamelCase				( lowercase						:      int						)	->	bool:
				_a								= n ** (1 / 3)
				return (val * val * val) == n


if __name__ == "__main__":
					print(perfect_cube(27))
					print(perfect_cube(4))
