



"""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=128   ,  _SCREAMING_SNAKE_CASE=32   ,  _SCREAMING_SNAKE_CASE=16   ,  _SCREAMING_SNAKE_CASE=2   ,  _SCREAMING_SNAKE_CASE=0.02   ,  _SCREAMING_SNAKE_CASE=3   ,  _SCREAMING_SNAKE_CASE=4   ,  _SCREAMING_SNAKE_CASE=None   ,  )						->   Dict:





    '''simple docstring'''




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

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





    '''simple docstring'''




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

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

    UpperCAmelCase			:      Optional[Any]			=			None
    if self.use_token_type_ids:
     UpperCAmelCase			:      Dict			=			ids_tensor([self.batch_size, self.seq_length]   ,  self.type_vocab_size		)

    UpperCAmelCase			:      List[Any]			=			None
    UpperCAmelCase			:      List[str]			=			None
    UpperCAmelCase			:      Tuple			=			None
    if self.use_labels:
     UpperCAmelCase			:      Dict			=			ids_tensor([self.batch_size]   ,  self.type_sequence_label_size		)
     UpperCAmelCase			:      Union[str, Any]			=			ids_tensor([self.batch_size, self.seq_length]   ,  self.num_labels		)
     UpperCAmelCase			:      str			=			ids_tensor([self.batch_size]   ,  self.num_choices		)

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

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

   def 			SCREAMING_SNAKE_CASE  (     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 			SCREAMING_SNAKE_CASE  (     self		)						->   List[Any]:





    '''simple docstring'''




    (
        (
        UpperCAmelCase
    )			,	(
        UpperCAmelCase
    )			,	(
        UpperCAmelCase
    )			,	(
        UpperCAmelCase
    )			,	(
        UpperCAmelCase
    )			,	(
        UpperCAmelCase
    )			,	(
        UpperCAmelCase
    )			,	
    )			:      Dict			=			self.prepare_config_and_inputs()

    UpperCAmelCase			:      Any			=			True
    UpperCAmelCase			:      Optional[int]			=			floats_tensor([self.batch_size, self.seq_length, self.hidden_size]		)
    UpperCAmelCase			:      Tuple			=			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 			SCREAMING_SNAKE_CASE  (     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'''




    UpperCAmelCase			:      Union[str, Any]			=			NezhaModel(config=_SCREAMING_SNAKE_CASE		)
    model.to(_SCREAMING_SNAKE_CASE		)
    model.eval()
    UpperCAmelCase			:      List[str]			=			model(_SCREAMING_SNAKE_CASE   ,  attention_mask=_SCREAMING_SNAKE_CASE   ,  token_type_ids=_SCREAMING_SNAKE_CASE		)
    UpperCAmelCase			:      Optional[Any]			=			model(_SCREAMING_SNAKE_CASE   ,  token_type_ids=_SCREAMING_SNAKE_CASE		)
    UpperCAmelCase			:      Dict			=			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 			SCREAMING_SNAKE_CASE  (     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'''




    UpperCAmelCase			:      Optional[int]			=			True
    UpperCAmelCase			:      List[str]			=			NezhaModel(_SCREAMING_SNAKE_CASE		)
    model.to(_SCREAMING_SNAKE_CASE		)
    model.eval()
    UpperCAmelCase			:      List[Any]			=			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   ,  )
    UpperCAmelCase			:      List[Any]			=			model(
        _SCREAMING_SNAKE_CASE   ,  attention_mask=_SCREAMING_SNAKE_CASE   ,  token_type_ids=_SCREAMING_SNAKE_CASE   ,  encoder_hidden_states=_SCREAMING_SNAKE_CASE   ,  )
    UpperCAmelCase			:      Tuple			=			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 			SCREAMING_SNAKE_CASE  (     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'''




    UpperCAmelCase			:      str			=			NezhaForMaskedLM(config=_SCREAMING_SNAKE_CASE		)
    model.to(_SCREAMING_SNAKE_CASE		)
    model.eval()
    UpperCAmelCase			:      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 			SCREAMING_SNAKE_CASE  (     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'''




    UpperCAmelCase			:      str			=			NezhaForNextSentencePrediction(config=_SCREAMING_SNAKE_CASE		)
    model.to(_SCREAMING_SNAKE_CASE		)
    model.eval()
    UpperCAmelCase			:      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, 2)		)

   def 			SCREAMING_SNAKE_CASE  (     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'''




    UpperCAmelCase			:      str			=			NezhaForPreTraining(config=_SCREAMING_SNAKE_CASE		)
    model.to(_SCREAMING_SNAKE_CASE		)
    model.eval()
    UpperCAmelCase			:      int			=			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 			SCREAMING_SNAKE_CASE  (     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'''




    UpperCAmelCase			:      List[Any]			=			NezhaForQuestionAnswering(config=_SCREAMING_SNAKE_CASE		)
    model.to(_SCREAMING_SNAKE_CASE		)
    model.eval()
    UpperCAmelCase			:      List[Any]			=			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 			SCREAMING_SNAKE_CASE  (     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'''




    UpperCAmelCase			:      List[Any]			=			self.num_labels
    UpperCAmelCase			:      List[str]			=			NezhaForSequenceClassification(_SCREAMING_SNAKE_CASE		)
    model.to(_SCREAMING_SNAKE_CASE		)
    model.eval()
    UpperCAmelCase			:      Tuple			=			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 			SCREAMING_SNAKE_CASE  (     self   ,  _SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)						->   Optional[Any]:





    '''simple docstring'''




    UpperCAmelCase			:      Dict			=			self.num_labels
    UpperCAmelCase			:      Any			=			NezhaForTokenClassification(config=_SCREAMING_SNAKE_CASE		)
    model.to(_SCREAMING_SNAKE_CASE		)
    model.eval()
    UpperCAmelCase			:      Optional[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.seq_length, self.num_labels)		)

   def 			SCREAMING_SNAKE_CASE  (     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'''




    UpperCAmelCase			:      int			=			self.num_choices
    UpperCAmelCase			:      Any			=			NezhaForMultipleChoice(config=_SCREAMING_SNAKE_CASE		)
    model.to(_SCREAMING_SNAKE_CASE		)
    model.eval()
    UpperCAmelCase			:      str			=			input_ids.unsqueeze(1		).expand(-1   ,  self.num_choices   ,  -1		).contiguous()
    UpperCAmelCase			:      str			=			token_type_ids.unsqueeze(1		).expand(-1   ,  self.num_choices   ,  -1		).contiguous()
    UpperCAmelCase			:      str			=			input_mask.unsqueeze(1		).expand(-1   ,  self.num_choices   ,  -1		).contiguous()
    UpperCAmelCase			:      Tuple			=			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 			SCREAMING_SNAKE_CASE  (     self		)						->   Tuple:





    '''simple docstring'''




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



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

   def 			SCREAMING_SNAKE_CASE  (     self   ,  _SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE=False		)						->   Dict:





    '''simple docstring'''




    UpperCAmelCase			:      Optional[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		):
      UpperCAmelCase			:      str			=			torch.zeros(
          (self.model_tester.batch_size, self.model_tester.seq_length)   ,  dtype=torch.long   ,  device=_SCREAMING_SNAKE_CASE		)
      UpperCAmelCase			:      Tuple			=			torch.zeros(
          self.model_tester.batch_size   ,  dtype=torch.long   ,  device=_SCREAMING_SNAKE_CASE		)
    return inputs_dict

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   Any:





    '''simple docstring'''




    UpperCAmelCase			:      str			=			NezhaModelTester(self		)
    UpperCAmelCase			:      Any			=			ConfigTester(self   ,  config_class=_SCREAMING_SNAKE_CASE   ,  hidden_size=37		)

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   str:





    '''simple docstring'''




    self.config_tester.run_common_tests()

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





    '''simple docstring'''




    UpperCAmelCase			:      Tuple			=			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE		)

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





    '''simple docstring'''




    UpperCAmelCase			:      List[Any]			=			self.model_tester.prepare_config_and_inputs_for_decoder()
    self.model_tester.create_and_check_model_as_decoder(*_SCREAMING_SNAKE_CASE		)

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   int:





    '''simple docstring'''




    (
        (
        UpperCAmelCase
    )			,	(
        UpperCAmelCase
    )			,	(
        UpperCAmelCase
    )			,	(
        UpperCAmelCase
    )			,	(
        UpperCAmelCase
    )			,	(
        UpperCAmelCase
    )			,	(
        UpperCAmelCase
    )			,	(
        UpperCAmelCase
    )			,	(
        UpperCAmelCase
    )			,	
    )			:      Union[str, Any]			=			self.model_tester.prepare_config_and_inputs_for_decoder()

    UpperCAmelCase			:      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 			SCREAMING_SNAKE_CASE  (     self		)						->   Union[str, Any]:





    '''simple docstring'''




    UpperCAmelCase			:      Optional[Any]			=			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_masked_lm(*_SCREAMING_SNAKE_CASE		)

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   Any:





    '''simple docstring'''




    UpperCAmelCase			:      Dict			=			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_multiple_choice(*_SCREAMING_SNAKE_CASE		)

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





    '''simple docstring'''




    UpperCAmelCase			:      Optional[int]			=			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_next_sequence_prediction(*_SCREAMING_SNAKE_CASE		)

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





    '''simple docstring'''




    UpperCAmelCase			:      Optional[Any]			=			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_pretraining(*_SCREAMING_SNAKE_CASE		)

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   Any:





    '''simple docstring'''




    UpperCAmelCase			:      Any			=			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_question_answering(*_SCREAMING_SNAKE_CASE		)

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





    '''simple docstring'''




    UpperCAmelCase			:      Optional[Any]			=			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_sequence_classification(*_SCREAMING_SNAKE_CASE		)

   def 			SCREAMING_SNAKE_CASE  (     self		)						->   str:





    '''simple docstring'''




    UpperCAmelCase			:      List[Any]			=			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_token_classification(*_SCREAMING_SNAKE_CASE		)

   @slow
   def 			SCREAMING_SNAKE_CASE  (     self		)						->   Dict:





    '''simple docstring'''




    for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     UpperCAmelCase			:      Dict			=			NezhaModel.from_pretrained(_SCREAMING_SNAKE_CASE		)
     self.assertIsNotNone(_SCREAMING_SNAKE_CASE		)




   @slow
   @require_torch_gpu
   def 			SCREAMING_SNAKE_CASE  (     self		)						->   Optional[int]:





    '''simple docstring'''




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

     UpperCAmelCase			:      Union[str, Any]			=			True
     UpperCAmelCase			:      List[Any]			=			model_class(config=_SCREAMING_SNAKE_CASE		)

     UpperCAmelCase			:      Dict			=			self._prepare_for_class(_SCREAMING_SNAKE_CASE   ,  _SCREAMING_SNAKE_CASE		)
     UpperCAmelCase			:      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"""		)		)
      UpperCAmelCase			:      Optional[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 			SCREAMING_SNAKE_CASE  (     self		)						->   Union[str, Any]:





    '''simple docstring'''




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




   @slow
   def 			SCREAMING_SNAKE_CASE  (     self		)						->   Dict:





    '''simple docstring'''




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





"""simple docstring"""
from __future__ import annotations

from decimal import Decimal

from numpy import array

def    _snake_case		(		UpperCamelCase		:	list[list[float]]		):
 UpperCAmelCase			:      str			=			Decimal

 # Check if the provided matrix has 2 rows and 2 columns
 # since this implementation only works for 2x2 matrices
 if len(UpperCamelCase		) == 2 and len(matrix[0]		) == 2 and len(matrix[1]		) == 2:
  # Calculate the determinant of the matrix
  UpperCAmelCase			:      Optional[int]			=			float(
      d(matrix[0][0]		) * d(matrix[1][1]		) - d(matrix[1][0]		) * d(matrix[0][1]		)		)
  if determinant == 0:
   raise ValueError("""This matrix has no inverse."""		)

  # Creates a copy of the matrix with swapped positions of the elements
  UpperCAmelCase			:      Optional[Any]			=			[[0.0, 0.0], [0.0, 0.0]]
  UpperCAmelCase			,	UpperCAmelCase			:      Any			=			matrix[1][1], matrix[0][0]
  UpperCAmelCase			,	UpperCAmelCase			:      Union[str, Any]			=			-matrix[1][0], -matrix[0][1]

  # Calculate the inverse of the matrix
  return [
      [(float(d(UpperCamelCase		)		) / determinant) or 0.0 for n in row] for row in swapped_matrix
  ]
 elif (
     len(UpperCamelCase		) == 3
     and len(matrix[0]		) == 3
     and len(matrix[1]		) == 3
     and len(matrix[2]		) == 3
 ):
  # Calculate the determinant of the matrix using Sarrus rule
  UpperCAmelCase			:      str			=			float(
      (
          (d(matrix[0][0]		) * d(matrix[1][1]		) * d(matrix[2][2]		))
          + (d(matrix[0][1]		) * d(matrix[1][2]		) * d(matrix[2][0]		))
          + (d(matrix[0][2]		) * d(matrix[1][0]		) * d(matrix[2][1]		))
      )
      - (
          (d(matrix[0][2]		) * d(matrix[1][1]		) * d(matrix[2][0]		))
          + (d(matrix[0][1]		) * d(matrix[1][0]		) * d(matrix[2][2]		))
          + (d(matrix[0][0]		) * d(matrix[1][2]		) * d(matrix[2][1]		))
      )		)
  if determinant == 0:
   raise ValueError("""This matrix has no inverse."""		)

  # Creating cofactor matrix
  UpperCAmelCase			:      Optional[int]			=			[
      [d(0.0		), d(0.0		), d(0.0		)],
      [d(0.0		), d(0.0		), d(0.0		)],
      [d(0.0		), d(0.0		), d(0.0		)],
  ]
  UpperCAmelCase			:      str			=			(d(matrix[1][1]		) * d(matrix[2][2]		)) - (
      d(matrix[1][2]		) * d(matrix[2][1]		)
  )
  UpperCAmelCase			:      List[str]			=			-(
      (d(matrix[1][0]		) * d(matrix[2][2]		)) - (d(matrix[1][2]		) * d(matrix[2][0]		))
  )
  UpperCAmelCase			:      Optional[int]			=			(d(matrix[1][0]		) * d(matrix[2][1]		)) - (
      d(matrix[1][1]		) * d(matrix[2][0]		)
  )
  UpperCAmelCase			:      Optional[int]			=			-(
      (d(matrix[0][1]		) * d(matrix[2][2]		)) - (d(matrix[0][2]		) * d(matrix[2][1]		))
  )
  UpperCAmelCase			:      str			=			(d(matrix[0][0]		) * d(matrix[2][2]		)) - (
      d(matrix[0][2]		) * d(matrix[2][0]		)
  )
  UpperCAmelCase			:      Union[str, Any]			=			-(
      (d(matrix[0][0]		) * d(matrix[2][1]		)) - (d(matrix[0][1]		) * d(matrix[2][0]		))
  )
  UpperCAmelCase			:      Dict			=			(d(matrix[0][1]		) * d(matrix[1][2]		)) - (
      d(matrix[0][2]		) * d(matrix[1][1]		)
  )
  UpperCAmelCase			:      Optional[Any]			=			-(
      (d(matrix[0][0]		) * d(matrix[1][2]		)) - (d(matrix[0][2]		) * d(matrix[1][0]		))
  )
  UpperCAmelCase			:      str			=			(d(matrix[0][0]		) * d(matrix[1][1]		)) - (
      d(matrix[0][1]		) * d(matrix[1][0]		)
  )

  # Transpose the cofactor matrix (Adjoint matrix)
  UpperCAmelCase			:      str			=			array(UpperCamelCase		)
  for i in range(3		):
   for j in range(3		):
    UpperCAmelCase			:      Any			=			cofactor_matrix[j][i]

        # Inverse of the matrix using the formula (1/determinant) * adjoint matrix
  UpperCAmelCase			:      Tuple			=			array(UpperCamelCase		)
  for i in range(3		):
   for j in range(3		):
    inverse_matrix[i][j] /= d(UpperCamelCase		)

        # Calculate the inverse of the matrix
  return [[float(d(UpperCamelCase		)		) or 0.0 for n in row] for row in inverse_matrix]
 raise ValueError("""Please provide a matrix of size 2x2 or 3x3."""		)
