


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

   '''simple docstring'''




   def __init__(		self						,				__A						,				__A=13						,				__A=7						,				__A=True						,				__A=True						,				__A=True						,				__A=True						,				__A=99						,				__A=32						,				__A=5						,				__A=4						,				__A=37						,				__A="gelu"						,				__A=0.1						,				__A=0.1						,				__A=128						,				__A=32						,				__A=16						,				__A=2						,				__A=0.02						,				__A=3						,				__A=4						,				__A=None						,				):






     """simple docstring"""





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






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					int    =				ids_tensor([self.batch_size, self.seq_length]						,				self.vocab_size )

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

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

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

     lowerCamelCase :					Optional[int]    =				self.get_config()

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






   def _snake_case    (		self ):






     """simple docstring"""





     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=lowerCAmelCase__						,				initializer_range=self.initializer_range						,				)






   def _snake_case    (		self ):






     """simple docstring"""





     (
         (
         lowerCamelCase     
     )    ,	(
         lowerCamelCase     
     )    ,	(
         lowerCamelCase     
     )    ,	(
         lowerCamelCase     
     )    ,	(
         lowerCamelCase     
     )    ,	(
         lowerCamelCase     
     )    ,	(
         lowerCamelCase     
     )    ,	     
     ) :					int    =				self.prepare_config_and_inputs()

     lowerCamelCase :					str    =				True
     lowerCamelCase :					Dict    =				floats_tensor([self.batch_size, self.seq_length, self.hidden_size] )
     lowerCamelCase :					int    =				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 _snake_case    (		self						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A ):






     """simple docstring"""





     lowerCamelCase :					List[Any]    =				NezhaModel(config=lowerCAmelCase__ )
     model.to(lowerCAmelCase__ )
     model.eval()
     lowerCamelCase :					Union[str, Any]    =				model(lowerCAmelCase__						,				attention_mask=lowerCAmelCase__						,				token_type_ids=lowerCAmelCase__ )
     lowerCamelCase :					int    =				model(lowerCAmelCase__						,				token_type_ids=lowerCAmelCase__ )
     lowerCamelCase :					List[Any]    =				model(lowerCAmelCase__ )
     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 _snake_case    (		self						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				):






     """simple docstring"""





     lowerCamelCase :					Optional[int]    =				True
     lowerCamelCase :					List[Any]    =				NezhaModel(lowerCAmelCase__ )
     model.to(lowerCAmelCase__ )
     model.eval()
     lowerCamelCase :					List[Any]    =				model(
         lowerCAmelCase__						,				attention_mask=lowerCAmelCase__						,				token_type_ids=lowerCAmelCase__						,				encoder_hidden_states=lowerCAmelCase__						,				encoder_attention_mask=lowerCAmelCase__						,				)
     lowerCamelCase :					Dict    =				model(
         lowerCAmelCase__						,				attention_mask=lowerCAmelCase__						,				token_type_ids=lowerCAmelCase__						,				encoder_hidden_states=lowerCAmelCase__						,				)
     lowerCamelCase :					int    =				model(lowerCAmelCase__						,				attention_mask=lowerCAmelCase__						,				token_type_ids=lowerCAmelCase__ )
     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 _snake_case    (		self						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A ):






     """simple docstring"""





     lowerCamelCase :					str    =				NezhaForMaskedLM(config=lowerCAmelCase__ )
     model.to(lowerCAmelCase__ )
     model.eval()
     lowerCamelCase :					List[Any]    =				model(lowerCAmelCase__						,				attention_mask=lowerCAmelCase__						,				token_type_ids=lowerCAmelCase__						,				labels=lowerCAmelCase__ )
     self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.seq_length, self.vocab_size) )






   def _snake_case    (		self						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A ):






     """simple docstring"""





     lowerCamelCase :					List[str]    =				NezhaForNextSentencePrediction(config=lowerCAmelCase__ )
     model.to(lowerCAmelCase__ )
     model.eval()
     lowerCamelCase :					Tuple    =				model(
         lowerCAmelCase__						,				attention_mask=lowerCAmelCase__						,				token_type_ids=lowerCAmelCase__						,				labels=lowerCAmelCase__						,				)
     self.parent.assertEqual(result.logits.shape						,				(self.batch_size, 2) )






   def _snake_case    (		self						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A ):






     """simple docstring"""





     lowerCamelCase :					Tuple    =				NezhaForPreTraining(config=lowerCAmelCase__ )
     model.to(lowerCAmelCase__ )
     model.eval()
     lowerCamelCase :					Optional[int]    =				model(
         lowerCAmelCase__						,				attention_mask=lowerCAmelCase__						,				token_type_ids=lowerCAmelCase__						,				labels=lowerCAmelCase__						,				next_sentence_label=lowerCAmelCase__						,				)
     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 _snake_case    (		self						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A ):






     """simple docstring"""





     lowerCamelCase :					Dict    =				NezhaForQuestionAnswering(config=lowerCAmelCase__ )
     model.to(lowerCAmelCase__ )
     model.eval()
     lowerCamelCase :					Union[str, Any]    =				model(
         lowerCAmelCase__						,				attention_mask=lowerCAmelCase__						,				token_type_ids=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 _snake_case    (		self						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A ):






     """simple docstring"""





     lowerCamelCase :					Any    =				self.num_labels
     lowerCamelCase :					Dict    =				NezhaForSequenceClassification(lowerCAmelCase__ )
     model.to(lowerCAmelCase__ )
     model.eval()
     lowerCamelCase :					Any    =				model(lowerCAmelCase__						,				attention_mask=lowerCAmelCase__						,				token_type_ids=lowerCAmelCase__						,				labels=lowerCAmelCase__ )
     self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.num_labels) )






   def _snake_case    (		self						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A ):






     """simple docstring"""





     lowerCamelCase :					List[Any]    =				self.num_labels
     lowerCamelCase :					Tuple    =				NezhaForTokenClassification(config=lowerCAmelCase__ )
     model.to(lowerCAmelCase__ )
     model.eval()
     lowerCamelCase :					List[Any]    =				model(lowerCAmelCase__						,				attention_mask=lowerCAmelCase__						,				token_type_ids=lowerCAmelCase__						,				labels=lowerCAmelCase__ )
     self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.seq_length, self.num_labels) )






   def _snake_case    (		self						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A ):






     """simple docstring"""





     lowerCamelCase :					Union[str, Any]    =				self.num_choices
     lowerCamelCase :					Any    =				NezhaForMultipleChoice(config=lowerCAmelCase__ )
     model.to(lowerCAmelCase__ )
     model.eval()
     lowerCamelCase :					int    =				input_ids.unsqueeze(1 ).expand(-1						,				self.num_choices						,				-1 ).contiguous()
     lowerCamelCase :					Tuple    =				token_type_ids.unsqueeze(1 ).expand(-1						,				self.num_choices						,				-1 ).contiguous()
     lowerCamelCase :					List[str]    =				input_mask.unsqueeze(1 ).expand(-1						,				self.num_choices						,				-1 ).contiguous()
     lowerCamelCase :					List[Any]    =				model(
         lowerCAmelCase__						,				attention_mask=lowerCAmelCase__						,				token_type_ids=lowerCAmelCase__						,				labels=lowerCAmelCase__						,				)
     self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.num_choices) )






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Dict    =				self.prepare_config_and_inputs()
     (
         (
         lowerCamelCase     
     )    ,	(
         lowerCamelCase     
     )    ,	(
         lowerCamelCase     
     )    ,	(
         lowerCamelCase     
     )    ,	(
         lowerCamelCase     
     )    ,	(
         lowerCamelCase     
     )    ,	(
         lowerCamelCase     
     )    ,	     
     ) :					List[str]    =				config_and_inputs
     lowerCamelCase :					List[Any]    =				{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
     return config, inputs_dict



@require_torch
class 	UpperCAmelCase_					(					_UpperCAmelCase		, _UpperCAmelCase		, _UpperCAmelCase		, unittest.TestCase    ):

   '''simple docstring'''




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






   def _snake_case    (		self						,				__A						,				__A						,				__A=False ):






     """simple docstring"""





     lowerCamelCase :					List[str]    =				super()._prepare_for_class(lowerCAmelCase__						,				lowerCAmelCase__						,				return_labels=lowerCAmelCase__ )

     if return_labels:
       if model_class in get_values(lowerCAmelCase__ ):
         lowerCamelCase :					Any    =				torch.zeros(
             (self.model_tester.batch_size, self.model_tester.seq_length)						,				dtype=torch.long						,				device=lowerCAmelCase__ )
         lowerCamelCase :					Optional[int]    =				torch.zeros(
             self.model_tester.batch_size						,				dtype=torch.long						,				device=lowerCAmelCase__ )
     return inputs_dict






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					List[Any]    =				NezhaModelTester(self )
     lowerCamelCase :					Any    =				ConfigTester(self						,				config_class=lowerCAmelCase__						,				hidden_size=37 )






   def _snake_case    (		self ):






     """simple docstring"""





     self.config_tester.run_common_tests()






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Optional[Any]    =				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowerCAmelCase__ )






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					List[str]    =				self.model_tester.prepare_config_and_inputs_for_decoder()
     self.model_tester.create_and_check_model_as_decoder(*lowerCAmelCase__ )






   def _snake_case    (		self ):






     """simple docstring"""





     (
         (
         lowerCamelCase     
     )    ,	(
         lowerCamelCase     
     )    ,	(
         lowerCamelCase     
     )    ,	(
         lowerCamelCase     
     )    ,	(
         lowerCamelCase     
     )    ,	(
         lowerCamelCase     
     )    ,	(
         lowerCamelCase     
     )    ,	(
         lowerCamelCase     
     )    ,	(
         lowerCamelCase     
     )    ,	     
     ) :					Dict    =				self.model_tester.prepare_config_and_inputs_for_decoder()

     lowerCamelCase :					Dict    =				None

     self.model_tester.create_and_check_model_as_decoder(
         lowerCAmelCase__						,				lowerCAmelCase__						,				lowerCAmelCase__						,				lowerCAmelCase__						,				lowerCAmelCase__						,				lowerCAmelCase__						,				lowerCAmelCase__						,				lowerCAmelCase__						,				lowerCAmelCase__						,				)






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Any    =				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase__ )






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Dict    =				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_multiple_choice(*lowerCAmelCase__ )






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					List[str]    =				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_next_sequence_prediction(*lowerCAmelCase__ )






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Union[str, Any]    =				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_pretraining(*lowerCAmelCase__ )






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Union[str, Any]    =				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase__ )






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					int    =				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase__ )






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Union[str, Any]    =				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase__ )






   @slow
   def _snake_case    (		self ):






     """simple docstring"""





     for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       lowerCamelCase :					int    =				NezhaModel.from_pretrained(lowerCAmelCase__ )
       self.assertIsNotNone(lowerCAmelCase__ )






   @slow
   @require_torch_gpu
   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase    ,	lowerCamelCase :					Dict    =				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

       lowerCamelCase :					Any    =				True
       lowerCamelCase :					str    =				model_class(config=lowerCAmelCase__ )

       lowerCamelCase :					Optional[int]    =				self._prepare_for_class(lowerCAmelCase__						,				lowerCAmelCase__ )
       lowerCamelCase :					List[Any]    =				torch.jit.trace(
           lowerCAmelCase__						,				(inputs_dict["input_ids"].to("cpu" ), inputs_dict["attention_mask"].to("cpu" )) )

       with tempfile.TemporaryDirectory() as tmp:
         torch.jit.save(lowerCAmelCase__						,				os.path.join(lowerCAmelCase__						,				"bert.pt" ) )
         lowerCamelCase :					Tuple    =				torch.jit.load(os.path.join(lowerCAmelCase__						,				"bert.pt" )						,				map_location=lowerCAmelCase__ )
         loaded(inputs_dict["input_ids"].to(lowerCAmelCase__ )						,				inputs_dict["attention_mask"].to(lowerCAmelCase__ ) )



@require_torch
class 	UpperCAmelCase_					(					unittest.TestCase    ):

   '''simple docstring'''




   @slow
   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					str    =				NezhaModel.from_pretrained("sijunhe/nezha-cn-base" )
     lowerCamelCase :					Optional[Any]    =				torch.tensor([[0, 1, 2, 3, 4, 5]] )
     lowerCamelCase :					Any    =				torch.tensor([[0, 1, 1, 1, 1, 1]] )
     with torch.no_grad():
       lowerCamelCase :					Optional[Any]    =				model(lowerCAmelCase__						,				attention_mask=lowerCAmelCase__ )[0]
     lowerCamelCase :					Any    =				torch.Size((1, 6, 768) )
     self.assertEqual(output.shape						,				lowerCAmelCase__ )
     lowerCamelCase :					List[Any]    =				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]						,				lowerCAmelCase__						,				atol=1e-4 ) )






   @slow
   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					int    =				NezhaForMaskedLM.from_pretrained("sijunhe/nezha-cn-base" )
     lowerCamelCase :					int    =				torch.tensor([[0, 1, 2, 3, 4, 5]] )
     lowerCamelCase :					Tuple    =				torch.tensor([[1, 1, 1, 1, 1, 1]] )
     with torch.no_grad():
       lowerCamelCase :					List[str]    =				model(lowerCAmelCase__						,				attention_mask=lowerCAmelCase__ )[0]
     lowerCamelCase :					Tuple    =				torch.Size((1, 6, 2_1128) )
     self.assertEqual(output.shape						,				lowerCAmelCase__ )
     lowerCamelCase :					int    =				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]						,				lowerCAmelCase__						,				atol=1e-4 ) )




import requests





def 				UpperCAmelCase						(    lowercase							,			lowercase							):







				"""simple docstring"""



				__lowercase 			=							{'''Content-Type''': '''application/json'''}
				__lowercase 			=							requests.post(lowercase							,			json={'''text''': message_body}							,			headers=lowercase							)
				if response.status_code != 200:
								__lowercase 			=							(
								    '''Request to slack returned an error '''
								    F"{response.status_code}, the response is:\n{response.text}"
								)
								raise ValueError(lowercase							)


if __name__ == "__main__":
		# Set the slack url to the one provided by Slack when you create the webhook at
		# https://my.slack.com/services/new/incoming-webhook/
		send_slack_message("""<YOUR MESSAGE BODY>""", """<SLACK CHANNEL URL>""")