





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




       def __init__(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__=13		,SCREAMING_SNAKE_CASE__=7		,SCREAMING_SNAKE_CASE__=True		,SCREAMING_SNAKE_CASE__=True		,SCREAMING_SNAKE_CASE__=True		,SCREAMING_SNAKE_CASE__=True		,SCREAMING_SNAKE_CASE__=99		,SCREAMING_SNAKE_CASE__=32		,SCREAMING_SNAKE_CASE__=5		,SCREAMING_SNAKE_CASE__=4		,SCREAMING_SNAKE_CASE__=37		,SCREAMING_SNAKE_CASE__="gelu"		,SCREAMING_SNAKE_CASE__=0.1		,SCREAMING_SNAKE_CASE__=0.1		,SCREAMING_SNAKE_CASE__=1_28		,SCREAMING_SNAKE_CASE__=32		,SCREAMING_SNAKE_CASE__=16		,SCREAMING_SNAKE_CASE__=2		,SCREAMING_SNAKE_CASE__=0.0_2		,SCREAMING_SNAKE_CASE__=3		,SCREAMING_SNAKE_CASE__=4		,SCREAMING_SNAKE_CASE__=None		,)      ->				Union[str, Any]:





             """simple docstring"""






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




       def    _UpperCamelCase					(						self							)      ->				Optional[int]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  ids_tensor([self.batch_size, self.seq_length]		,self.vocab_size							)

             __SCREAMING_SNAKE_CASE       :List[Any]		    =  None
             if self.use_input_mask:
                   __SCREAMING_SNAKE_CASE       :List[str]		    =  random_attention_mask([self.batch_size, self.seq_length]							)

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

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

             __SCREAMING_SNAKE_CASE       :Tuple		    =  self.get_config()

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




       def    _UpperCamelCase					(						self							)      ->				List[Any]:





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




       def    _UpperCamelCase					(						self							)      ->				int:





             """simple docstring"""






             (
                 (
                 __SCREAMING_SNAKE_CASE
             )    ,(
                 __SCREAMING_SNAKE_CASE
             )    ,(
                 __SCREAMING_SNAKE_CASE
             )    ,(
                 __SCREAMING_SNAKE_CASE
             )    ,(
                 __SCREAMING_SNAKE_CASE
             )    ,(
                 __SCREAMING_SNAKE_CASE
             )    ,(
                 __SCREAMING_SNAKE_CASE
             )    ,
             )       :str		    =  self.prepare_config_and_inputs()

             __SCREAMING_SNAKE_CASE       :int		    =  True
             __SCREAMING_SNAKE_CASE       :int		    =  floats_tensor([self.batch_size, self.seq_length, self.hidden_size]							)
             __SCREAMING_SNAKE_CASE       :Optional[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    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)      ->				int:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :str		    =  NezhaModel(config=SCREAMING_SNAKE_CASE__							)
             model.to(SCREAMING_SNAKE_CASE__							)
             model.eval()
             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  model(SCREAMING_SNAKE_CASE__		,attention_mask=SCREAMING_SNAKE_CASE__		,token_type_ids=SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE       :Tuple		    =  model(SCREAMING_SNAKE_CASE__		,token_type_ids=SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE       :List[str]		    =  model(SCREAMING_SNAKE_CASE__							)
             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		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,)      ->				Any:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  True
             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  NezhaModel(SCREAMING_SNAKE_CASE__							)
             model.to(SCREAMING_SNAKE_CASE__							)
             model.eval()
             __SCREAMING_SNAKE_CASE       :Dict		    =  model(
                 SCREAMING_SNAKE_CASE__		,attention_mask=SCREAMING_SNAKE_CASE__		,token_type_ids=SCREAMING_SNAKE_CASE__		,encoder_hidden_states=SCREAMING_SNAKE_CASE__		,encoder_attention_mask=SCREAMING_SNAKE_CASE__		,)
             __SCREAMING_SNAKE_CASE       :Dict		    =  model(
                 SCREAMING_SNAKE_CASE__		,attention_mask=SCREAMING_SNAKE_CASE__		,token_type_ids=SCREAMING_SNAKE_CASE__		,encoder_hidden_states=SCREAMING_SNAKE_CASE__		,)
             __SCREAMING_SNAKE_CASE       :List[Any]		    =  model(SCREAMING_SNAKE_CASE__		,attention_mask=SCREAMING_SNAKE_CASE__		,token_type_ids=SCREAMING_SNAKE_CASE__							)
             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		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)      ->				int:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :str		    =  NezhaForMaskedLM(config=SCREAMING_SNAKE_CASE__							)
             model.to(SCREAMING_SNAKE_CASE__							)
             model.eval()
             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  model(SCREAMING_SNAKE_CASE__		,attention_mask=SCREAMING_SNAKE_CASE__		,token_type_ids=SCREAMING_SNAKE_CASE__		,labels=SCREAMING_SNAKE_CASE__							)
             self.parent.assertEqual(result.logits.shape		,(self.batch_size, self.seq_length, self.vocab_size)							)




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)      ->				List[str]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  NezhaForNextSentencePrediction(config=SCREAMING_SNAKE_CASE__							)
             model.to(SCREAMING_SNAKE_CASE__							)
             model.eval()
             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  model(
                 SCREAMING_SNAKE_CASE__		,attention_mask=SCREAMING_SNAKE_CASE__		,token_type_ids=SCREAMING_SNAKE_CASE__		,labels=SCREAMING_SNAKE_CASE__		,)
             self.parent.assertEqual(result.logits.shape		,(self.batch_size, 2)							)




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)      ->				Optional[int]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :int		    =  NezhaForPreTraining(config=SCREAMING_SNAKE_CASE__							)
             model.to(SCREAMING_SNAKE_CASE__							)
             model.eval()
             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  model(
                 SCREAMING_SNAKE_CASE__		,attention_mask=SCREAMING_SNAKE_CASE__		,token_type_ids=SCREAMING_SNAKE_CASE__		,labels=SCREAMING_SNAKE_CASE__		,next_sentence_label=SCREAMING_SNAKE_CASE__		,)
             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		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)      ->				Optional[int]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Any		    =  NezhaForQuestionAnswering(config=SCREAMING_SNAKE_CASE__							)
             model.to(SCREAMING_SNAKE_CASE__							)
             model.eval()
             __SCREAMING_SNAKE_CASE       :int		    =  model(
                 SCREAMING_SNAKE_CASE__		,attention_mask=SCREAMING_SNAKE_CASE__		,token_type_ids=SCREAMING_SNAKE_CASE__		,start_positions=SCREAMING_SNAKE_CASE__		,end_positions=SCREAMING_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    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)      ->				Tuple:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  self.num_labels
             __SCREAMING_SNAKE_CASE       :str		    =  NezhaForSequenceClassification(SCREAMING_SNAKE_CASE__							)
             model.to(SCREAMING_SNAKE_CASE__							)
             model.eval()
             __SCREAMING_SNAKE_CASE       :List[Any]		    =  model(SCREAMING_SNAKE_CASE__		,attention_mask=SCREAMING_SNAKE_CASE__		,token_type_ids=SCREAMING_SNAKE_CASE__		,labels=SCREAMING_SNAKE_CASE__							)
             self.parent.assertEqual(result.logits.shape		,(self.batch_size, self.num_labels)							)




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)      ->				List[str]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  self.num_labels
             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  NezhaForTokenClassification(config=SCREAMING_SNAKE_CASE__							)
             model.to(SCREAMING_SNAKE_CASE__							)
             model.eval()
             __SCREAMING_SNAKE_CASE       :List[str]		    =  model(SCREAMING_SNAKE_CASE__		,attention_mask=SCREAMING_SNAKE_CASE__		,token_type_ids=SCREAMING_SNAKE_CASE__		,labels=SCREAMING_SNAKE_CASE__							)
             self.parent.assertEqual(result.logits.shape		,(self.batch_size, self.seq_length, self.num_labels)							)




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)      ->				Dict:





             """simple docstring"""






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




       def    _UpperCamelCase					(						self							)      ->				Dict:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Any		    =  self.prepare_config_and_inputs()
             (
                 (
                 __SCREAMING_SNAKE_CASE
             )    ,(
                 __SCREAMING_SNAKE_CASE
             )    ,(
                 __SCREAMING_SNAKE_CASE
             )    ,(
                 __SCREAMING_SNAKE_CASE
             )    ,(
                 __SCREAMING_SNAKE_CASE
             )    ,(
                 __SCREAMING_SNAKE_CASE
             )    ,(
                 __SCREAMING_SNAKE_CASE
             )    ,
             )       :int		    =  config_and_inputs
             __SCREAMING_SNAKE_CASE       :Tuple		    =  {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
             return config, inputs_dict






@require_torch
class  _SCREAMING_SNAKE_CASE(       A   ,     A   ,     A   ,     unittest.TestCase  ):
       SCREAMING_SNAKE_CASE_       :				List[Any]     						=  (
           (
               NezhaModel,
               NezhaForMaskedLM,
               NezhaForMultipleChoice,
               NezhaForNextSentencePrediction,
               NezhaForPreTraining,
               NezhaForQuestionAnswering,
               NezhaForSequenceClassification,
               NezhaForTokenClassification,
           )
           if is_torch_available()
           else ()
       )
       SCREAMING_SNAKE_CASE_       :				int     						=  (
           {
               '''feature-extraction''': NezhaModel,
               '''fill-mask''': NezhaForMaskedLM,
               '''question-answering''': NezhaForQuestionAnswering,
               '''text-classification''': NezhaForSequenceClassification,
               '''token-classification''': NezhaForTokenClassification,
               '''zero-shot''': NezhaForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       SCREAMING_SNAKE_CASE_       :				List[Any]     						=  True




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__=False							)      ->				Dict:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :List[Any]		    =  super()._prepare_for_class(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,return_labels=SCREAMING_SNAKE_CASE__							)

             if return_labels:
                   if model_class in get_values(SCREAMING_SNAKE_CASE__							):
                         __SCREAMING_SNAKE_CASE       :int		    =  torch.zeros(
                             (self.model_tester.batch_size, self.model_tester.seq_length)		,dtype=torch.long		,device=SCREAMING_SNAKE_CASE__							)
                         __SCREAMING_SNAKE_CASE       :Any		    =  torch.zeros(
                             self.model_tester.batch_size		,dtype=torch.long		,device=SCREAMING_SNAKE_CASE__							)
             return inputs_dict




       def    _UpperCamelCase					(						self							)      ->				Dict:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :str		    =  NezhaModelTester(self							)
             __SCREAMING_SNAKE_CASE       :List[str]		    =  ConfigTester(self		,config_class=SCREAMING_SNAKE_CASE__		,hidden_size=37							)




       def    _UpperCamelCase					(						self							)      ->				Dict:





             """simple docstring"""






             self.config_tester.run_common_tests()




       def    _UpperCamelCase					(						self							)      ->				Dict:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Any		    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__							)




       def    _UpperCamelCase					(						self							)      ->				Optional[Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :int		    =  self.model_tester.prepare_config_and_inputs_for_decoder()
             self.model_tester.create_and_check_model_as_decoder(*SCREAMING_SNAKE_CASE__							)




       def    _UpperCamelCase					(						self							)      ->				Optional[Any]:





             """simple docstring"""






             (
                 (
                 __SCREAMING_SNAKE_CASE
             )    ,(
                 __SCREAMING_SNAKE_CASE
             )    ,(
                 __SCREAMING_SNAKE_CASE
             )    ,(
                 __SCREAMING_SNAKE_CASE
             )    ,(
                 __SCREAMING_SNAKE_CASE
             )    ,(
                 __SCREAMING_SNAKE_CASE
             )    ,(
                 __SCREAMING_SNAKE_CASE
             )    ,(
                 __SCREAMING_SNAKE_CASE
             )    ,(
                 __SCREAMING_SNAKE_CASE
             )    ,
             )       :Optional[int]		    =  self.model_tester.prepare_config_and_inputs_for_decoder()

             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  None

             self.model_tester.create_and_check_model_as_decoder(
                 SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,)




       def    _UpperCamelCase					(						self							)      ->				List[str]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Tuple		    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_masked_lm(*SCREAMING_SNAKE_CASE__							)




       def    _UpperCamelCase					(						self							)      ->				Optional[Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :str		    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_multiple_choice(*SCREAMING_SNAKE_CASE__							)




       def    _UpperCamelCase					(						self							)      ->				List[Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :List[Any]		    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_next_sequence_prediction(*SCREAMING_SNAKE_CASE__							)




       def    _UpperCamelCase					(						self							)      ->				List[str]:





             """simple docstring"""






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




       def    _UpperCamelCase					(						self							)      ->				Union[str, Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_question_answering(*SCREAMING_SNAKE_CASE__							)




       def    _UpperCamelCase					(						self							)      ->				Tuple:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :int		    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_sequence_classification(*SCREAMING_SNAKE_CASE__							)




       def    _UpperCamelCase					(						self							)      ->				str:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :str		    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_token_classification(*SCREAMING_SNAKE_CASE__							)




       @slow
       def    _UpperCamelCase					(						self							)      ->				Union[str, Any]:





             """simple docstring"""






             for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   __SCREAMING_SNAKE_CASE       :str		    =  NezhaModel.from_pretrained(SCREAMING_SNAKE_CASE__							)
                   self.assertIsNotNone(SCREAMING_SNAKE_CASE__							)




       @slow
       @require_torch_gpu
       def    _UpperCamelCase					(						self							)      ->				Tuple:





             """simple docstring"""






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

                   __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  True
                   __SCREAMING_SNAKE_CASE       :Optional[int]		    =  model_class(config=SCREAMING_SNAKE_CASE__							)

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

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






@require_torch
class  _SCREAMING_SNAKE_CASE(       unittest.TestCase  ):




       @slow
       def    _UpperCamelCase					(						self							)      ->				List[str]:





             """simple docstring"""






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




       @slow
       def    _UpperCamelCase					(						self							)      ->				int:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Any		    =  NezhaForMaskedLM.from_pretrained('''sijunhe/nezha-cn-base'''							)
             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  torch.tensor([[0, 1, 2, 3, 4, 5]]							)
             __SCREAMING_SNAKE_CASE       :int		    =  torch.tensor([[1, 1, 1, 1, 1, 1]]							)
             with torch.no_grad():
                   __SCREAMING_SNAKE_CASE       :Optional[int]		    =  model(SCREAMING_SNAKE_CASE__		,attention_mask=SCREAMING_SNAKE_CASE__							)[0]
             __SCREAMING_SNAKE_CASE       :List[Any]		    =  torch.Size((1, 6, 2_11_28)							)
             self.assertEqual(output.shape		,SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE       :List[str]		    =  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]		,SCREAMING_SNAKE_CASE__		,atol=1E-4							)							)






"""simple docstring"""
import json
import os
from datetime import date
from pathlib import Path

from tabulate import DataRow, TableFormat, tabulate


lowerCamelCase_       =		TableFormat(
    lineabove=None,
    linebelowheader=None,
    linebetweenrows=None,
    linebelow=None,
    headerrow=DataRow("", "|", "|"),
    datarow=DataRow("", "|", "|"),
    padding=1,
    with_header_hide=None,
)


lowerCamelCase_       =		[]
lowerCamelCase_       =		[]

lowerCamelCase_       =		{"type": "section", "text": {"type": "plain_text", "text": "No failed tests! 🤗", "emoji": True}}

lowerCamelCase_       =		[
    {
        "type": "header",
        "text": {
            "type": "plain_text",
            "text": f'🤗 Accelerate nightly {os.environ.get("TEST_TYPE", "")} test results',
            "emoji": True,
        },
    }
]

lowerCamelCase_       =		0
for log in Path().glob("*.log"):
  lowerCamelCase_       =		0
  with open(log, "r") as f:
    for line in f:
      lowerCamelCase_       =		json.loads(line)
      if line.get("nodeid", "") != "":
        lowerCamelCase_       =		line["nodeid"]
        if line.get("duration", None) is not None:
          lowerCamelCase_       =		f'{line["duration"]:.4f}'
          if line.get("outcome", "") == "failed":
            section_num_failed += 1
            failed.append([test, duration, log.name.split("_")[0]])
            total_num_failed += 1
  group_info.append([str(log), section_num_failed, failed])
  lowerCamelCase_       =		[]
  log.unlink()

lowerCamelCase_       =		""
lowerCamelCase_       =		[]
if total_num_failed > 0:
  for name, num_failed, failed_tests in group_info:
    if num_failed > 0:
      if num_failed == 1:
        message += f"*{name[1:]}: {num_failed} failed test*\n"
      else:
        message += f"*{name[1:]}: {num_failed} failed tests*\n"
      lowerCamelCase_       =		[]
      lowerCamelCase_       =		{}
      for test in failed_tests:
        lowerCamelCase_       =		test[0].split("::")
        lowerCamelCase_       =		data[0].split("/")[-1]
        if data[0] not in filesafailed:
          lowerCamelCase_       =		[data[1:]]
        else:
          filesafailed[data[0]] += [data[1:]]
        failed_table.append(data)

      lowerCamelCase_       =		[test[0] for test in failed_table]
      lowerCamelCase_       =		list(set(files))
      # Count number of instances in failed_tests
      lowerCamelCase_       =		[]
      for file in individual_files:
        table.append([file, len(filesafailed[file])])

      lowerCamelCase_       =		tabulate(
          table,
          headers=["Test Location", "Num Failed"],
          tablefmt=hf_table_format,
          stralign="right",
      )
      message += f"\n```\n{failed_table}\n```"
      all_filesafailed.append(filesafailed)
  if len(message) > 3_0_0_0:
    lowerCamelCase_       =		"Too many failed tests, please see the full report in the Action results."
    lowerCamelCase_       =		len(err) + 1_0
    lowerCamelCase_       =		message[: 3_0_0_0 - offset] + f'\n...\n```\n{err}'
  print(f'### {message}')
else:
  lowerCamelCase_       =		"No failed tests! 🤗"
  print(f'## {message}')
  payload.append(no_error_payload)

if os.environ.get("TEST_TYPE", "") != "":
  from slack_sdk import WebClient

  lowerCamelCase_       =		WebClient(token=os.environ["SLACK_API_TOKEN"])
  if message != "No failed tests! 🤗":
    lowerCamelCase_       =		{
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": message,
        },
    }
    payload.append(md_report)
    lowerCamelCase_       =		{
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": "*For more details:*",
        },
        "accessory": {
            "type": "button",
            "text": {
                "type": "plain_text",
                "text": "Check Action results",
                "emoji": True,
            },
            "url": f'https://github.com/{os.environ["GITHUB_REPOSITORY"]}/actions/runs/{os.environ["GITHUB_RUN_ID"]}',
        },
    }
    payload.append(action_button)
    lowerCamelCase_       =		{
        "type": "context",
        "elements": [
            {
                "type": "plain_text",
                "text": f'Nightly {os.environ.get("TEST_TYPE")} test results for {date.today()}',
            }
        ],
    }
    payload.append(date_report)
  lowerCamelCase_       =		client.chat_postMessage(channel="#accelerate-ci-daily", text=message, blocks=payload)
  lowerCamelCase_       =		response.data["ts"]
  for failed_file in all_filesafailed:
    for test_location, test_failures in failed_file.items():
      # Keep only the first instance of the test name
      lowerCamelCase_       =		""
      for i, row in enumerate(test_failures):
        if row[0] != test_class:
          lowerCamelCase_       =		row[0]
        else:
          lowerCamelCase_       =		""

      lowerCamelCase_       =		{
          "type": "section",
          "text": {
              "type": "mrkdwn",
              "text": f'Test location: {test_location}\n```\n{tabulate(test_failures, headers=["Class", "Test"], tablefmt=hf_table_format, stralign="right")}\n```',
          },
      }

      client.chat_postMessage(
          channel="#accelerate-ci-daily",
          thread_ts=ts,
          blocks=[payload],
      )