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






       def __init__(  self :							Dict       ,      UpperCAmelCase_ :							List[Any]       ,      UpperCAmelCase_ :							List[str]=13       ,      UpperCAmelCase_ :							int=7       ,      UpperCAmelCase_ :							List[Any]=True       ,      UpperCAmelCase_ :							List[Any]=True       ,      UpperCAmelCase_ :							Any=True       ,      UpperCAmelCase_ :							str=True       ,      UpperCAmelCase_ :							Dict=99       ,      UpperCAmelCase_ :							List[Any]=32       ,      UpperCAmelCase_ :							Any=5       ,      UpperCAmelCase_ :							List[Any]=4       ,      UpperCAmelCase_ :							Tuple=37       ,      UpperCAmelCase_ :							int="gelu"       ,      UpperCAmelCase_ :							Any=0.1       ,      UpperCAmelCase_ :							int=0.1       ,      UpperCAmelCase_ :							str=128       ,      UpperCAmelCase_ :							Tuple=32       ,      UpperCAmelCase_ :							Tuple=16       ,      UpperCAmelCase_ :							int=2       ,      UpperCAmelCase_ :							Optional[int]=0.02       ,      UpperCAmelCase_ :							Any=3       ,      UpperCAmelCase_ :							Union[str, Any]=4       ,      UpperCAmelCase_ :							List[Any]=None       ,      )->  Optional[Any]:







              """simple docstring"""
              UpperCamelCase					 =		parent
              UpperCamelCase					 =		batch_size
              UpperCamelCase					 =		seq_length
              UpperCamelCase					 =		is_training
              UpperCamelCase					 =		use_input_mask
              UpperCamelCase					 =		use_token_type_ids
              UpperCamelCase					 =		use_labels
              UpperCamelCase					 =		vocab_size
              UpperCamelCase					 =		hidden_size
              UpperCamelCase					 =		num_hidden_layers
              UpperCamelCase					 =		num_attention_heads
              UpperCamelCase					 =		intermediate_size
              UpperCamelCase					 =		hidden_act
              UpperCamelCase					 =		hidden_dropout_prob
              UpperCamelCase					 =		attention_probs_dropout_prob
              UpperCamelCase					 =		max_position_embeddings
              UpperCamelCase					 =		type_vocab_size
              UpperCamelCase					 =		type_sequence_label_size
              UpperCamelCase					 =		initializer_range
              UpperCamelCase					 =		num_labels
              UpperCamelCase					 =		num_choices
              UpperCamelCase					 =		scope






       def 	_SCREAMING_SNAKE_CASE       (  self :							Optional[Any]			)->  Dict:







              """simple docstring"""
              UpperCamelCase					 =		ids_tensor([self.batch_size, self.seq_length]       ,      self.vocab_size			)

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

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

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

              UpperCamelCase					 =		self.get_config()

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






       def 	_SCREAMING_SNAKE_CASE       (  self :							Optional[int]			)->  str:







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






       def 	_SCREAMING_SNAKE_CASE       (  self :							Optional[Any]			)->  List[str]:







              """simple docstring"""
              (
                  (
                  UpperCamelCase
              ) ,       (
                  UpperCamelCase
              ) ,       (
                  UpperCamelCase
              ) ,       (
                  UpperCamelCase
              ) ,       (
                  UpperCamelCase
              ) ,       (
                  UpperCamelCase
              ) ,       (
                  UpperCamelCase
              ) ,       
              )					 =		self.prepare_config_and_inputs()

              UpperCamelCase					 =		True
              UpperCamelCase					 =		floats_tensor([self.batch_size, self.seq_length, self.hidden_size]			)
              UpperCamelCase					 =		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 :							Dict       ,      UpperCAmelCase_ :							int       ,      UpperCAmelCase_ :							Dict       ,      UpperCAmelCase_ :							Optional[int]       ,      UpperCAmelCase_ :							List[str]       ,      UpperCAmelCase_ :							int       ,      UpperCAmelCase_ :							Optional[Any]       ,      UpperCAmelCase_ :							Any			)->  Any:







              """simple docstring"""
              UpperCamelCase					 =		NezhaModel(config=UpperCAmelCase_			)
              model.to(UpperCAmelCase_			)
              model.eval()
              UpperCamelCase					 =		model(UpperCAmelCase_       ,      attention_mask=UpperCAmelCase_       ,      token_type_ids=UpperCAmelCase_			)
              UpperCamelCase					 =		model(UpperCAmelCase_       ,      token_type_ids=UpperCAmelCase_			)
              UpperCamelCase					 =		model(UpperCAmelCase_			)
              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 :							int       ,      UpperCAmelCase_ :							Dict       ,      UpperCAmelCase_ :							Union[str, Any]       ,      UpperCAmelCase_ :							Optional[int]       ,      UpperCAmelCase_ :							Any       ,      UpperCAmelCase_ :							int       ,      UpperCAmelCase_ :							List[Any]       ,      UpperCAmelCase_ :							int       ,      UpperCAmelCase_ :							List[str]       ,      UpperCAmelCase_ :							str       ,      )->  Union[str, Any]:







              """simple docstring"""
              UpperCamelCase					 =		True
              UpperCamelCase					 =		NezhaModel(UpperCAmelCase_			)
              model.to(UpperCAmelCase_			)
              model.eval()
              UpperCamelCase					 =		model(
                  UpperCAmelCase_       ,      attention_mask=UpperCAmelCase_       ,      token_type_ids=UpperCAmelCase_       ,      encoder_hidden_states=UpperCAmelCase_       ,      encoder_attention_mask=UpperCAmelCase_       ,      )
              UpperCamelCase					 =		model(
                  UpperCAmelCase_       ,      attention_mask=UpperCAmelCase_       ,      token_type_ids=UpperCAmelCase_       ,      encoder_hidden_states=UpperCAmelCase_       ,      )
              UpperCamelCase					 =		model(UpperCAmelCase_       ,      attention_mask=UpperCAmelCase_       ,      token_type_ids=UpperCAmelCase_			)
              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 :							List[Any]       ,      UpperCAmelCase_ :							str       ,      UpperCAmelCase_ :							List[Any]       ,      UpperCAmelCase_ :							Union[str, Any]       ,      UpperCAmelCase_ :							List[Any]       ,      UpperCAmelCase_ :							Optional[int]       ,      UpperCAmelCase_ :							str       ,      UpperCAmelCase_ :							Tuple			)->  Tuple:







              """simple docstring"""
              UpperCamelCase					 =		NezhaForMaskedLM(config=UpperCAmelCase_			)
              model.to(UpperCAmelCase_			)
              model.eval()
              UpperCamelCase					 =		model(UpperCAmelCase_       ,      attention_mask=UpperCAmelCase_       ,      token_type_ids=UpperCAmelCase_       ,      labels=UpperCAmelCase_			)
              self.parent.assertEqual(result.logits.shape       ,      (self.batch_size, self.seq_length, self.vocab_size)			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							Optional[Any]       ,      UpperCAmelCase_ :							List[str]       ,      UpperCAmelCase_ :							Optional[int]       ,      UpperCAmelCase_ :							Tuple       ,      UpperCAmelCase_ :							Dict       ,      UpperCAmelCase_ :							str       ,      UpperCAmelCase_ :							int       ,      UpperCAmelCase_ :							Optional[int]			)->  Union[str, Any]:







              """simple docstring"""
              UpperCamelCase					 =		NezhaForNextSentencePrediction(config=UpperCAmelCase_			)
              model.to(UpperCAmelCase_			)
              model.eval()
              UpperCamelCase					 =		model(
                  UpperCAmelCase_       ,      attention_mask=UpperCAmelCase_       ,      token_type_ids=UpperCAmelCase_       ,      labels=UpperCAmelCase_       ,      )
              self.parent.assertEqual(result.logits.shape       ,      (self.batch_size, 2)			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							List[Any]       ,      UpperCAmelCase_ :							Union[str, Any]       ,      UpperCAmelCase_ :							str       ,      UpperCAmelCase_ :							Optional[Any]       ,      UpperCAmelCase_ :							Any       ,      UpperCAmelCase_ :							List[Any]       ,      UpperCAmelCase_ :							Union[str, Any]       ,      UpperCAmelCase_ :							Dict			)->  Optional[int]:







              """simple docstring"""
              UpperCamelCase					 =		NezhaForPreTraining(config=UpperCAmelCase_			)
              model.to(UpperCAmelCase_			)
              model.eval()
              UpperCamelCase					 =		model(
                  UpperCAmelCase_       ,      attention_mask=UpperCAmelCase_       ,      token_type_ids=UpperCAmelCase_       ,      labels=UpperCAmelCase_       ,      next_sentence_label=UpperCAmelCase_       ,      )
              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 :							str       ,      UpperCAmelCase_ :							List[Any]       ,      UpperCAmelCase_ :							int       ,      UpperCAmelCase_ :							Any       ,      UpperCAmelCase_ :							Optional[int]       ,      UpperCAmelCase_ :							List[str]       ,      UpperCAmelCase_ :							List[str]       ,      UpperCAmelCase_ :							Tuple			)->  List[Any]:







              """simple docstring"""
              UpperCamelCase					 =		NezhaForQuestionAnswering(config=UpperCAmelCase_			)
              model.to(UpperCAmelCase_			)
              model.eval()
              UpperCamelCase					 =		model(
                  UpperCAmelCase_       ,      attention_mask=UpperCAmelCase_       ,      token_type_ids=UpperCAmelCase_       ,      start_positions=UpperCAmelCase_       ,      end_positions=UpperCAmelCase_       ,      )
              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 :							List[str]       ,      UpperCAmelCase_ :							List[Any]       ,      UpperCAmelCase_ :							List[str]       ,      UpperCAmelCase_ :							Optional[int]       ,      UpperCAmelCase_ :							List[str]       ,      UpperCAmelCase_ :							Any       ,      UpperCAmelCase_ :							List[str]       ,      UpperCAmelCase_ :							Dict			)->  Tuple:







              """simple docstring"""
              UpperCamelCase					 =		self.num_labels
              UpperCamelCase					 =		NezhaForSequenceClassification(UpperCAmelCase_			)
              model.to(UpperCAmelCase_			)
              model.eval()
              UpperCamelCase					 =		model(UpperCAmelCase_       ,      attention_mask=UpperCAmelCase_       ,      token_type_ids=UpperCAmelCase_       ,      labels=UpperCAmelCase_			)
              self.parent.assertEqual(result.logits.shape       ,      (self.batch_size, self.num_labels)			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							List[str]       ,      UpperCAmelCase_ :							Tuple       ,      UpperCAmelCase_ :							Optional[Any]       ,      UpperCAmelCase_ :							Optional[Any]       ,      UpperCAmelCase_ :							int       ,      UpperCAmelCase_ :							Any       ,      UpperCAmelCase_ :							Any       ,      UpperCAmelCase_ :							Optional[Any]			)->  Tuple:







              """simple docstring"""
              UpperCamelCase					 =		self.num_labels
              UpperCamelCase					 =		NezhaForTokenClassification(config=UpperCAmelCase_			)
              model.to(UpperCAmelCase_			)
              model.eval()
              UpperCamelCase					 =		model(UpperCAmelCase_       ,      attention_mask=UpperCAmelCase_       ,      token_type_ids=UpperCAmelCase_       ,      labels=UpperCAmelCase_			)
              self.parent.assertEqual(result.logits.shape       ,      (self.batch_size, self.seq_length, self.num_labels)			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							Union[str, Any]       ,      UpperCAmelCase_ :							Dict       ,      UpperCAmelCase_ :							Any       ,      UpperCAmelCase_ :							Optional[int]       ,      UpperCAmelCase_ :							Any       ,      UpperCAmelCase_ :							Dict       ,      UpperCAmelCase_ :							Optional[Any]       ,      UpperCAmelCase_ :							str			)->  Dict:







              """simple docstring"""
              UpperCamelCase					 =		self.num_choices
              UpperCamelCase					 =		NezhaForMultipleChoice(config=UpperCAmelCase_			)
              model.to(UpperCAmelCase_			)
              model.eval()
              UpperCamelCase					 =		input_ids.unsqueeze(1			).expand(-1       ,      self.num_choices       ,      -1			).contiguous()
              UpperCamelCase					 =		token_type_ids.unsqueeze(1			).expand(-1       ,      self.num_choices       ,      -1			).contiguous()
              UpperCamelCase					 =		input_mask.unsqueeze(1			).expand(-1       ,      self.num_choices       ,      -1			).contiguous()
              UpperCamelCase					 =		model(
                  UpperCAmelCase_       ,      attention_mask=UpperCAmelCase_       ,      token_type_ids=UpperCAmelCase_       ,      labels=UpperCAmelCase_       ,      )
              self.parent.assertEqual(result.logits.shape       ,      (self.batch_size, self.num_choices)			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							int			)->  Tuple:







              """simple docstring"""
              UpperCamelCase					 =		self.prepare_config_and_inputs()
              (
                  (
                  UpperCamelCase
              ) ,       (
                  UpperCamelCase
              ) ,       (
                  UpperCamelCase
              ) ,       (
                  UpperCamelCase
              ) ,       (
                  UpperCamelCase
              ) ,       (
                  UpperCamelCase
              ) ,       (
                  UpperCamelCase
              ) ,       
              )					 =		config_and_inputs
              UpperCamelCase					 =		{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
              return config, inputs_dict





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






       def 	_SCREAMING_SNAKE_CASE       (  self :							str       ,      UpperCAmelCase_ :							Optional[int]       ,      UpperCAmelCase_ :							List[str]       ,      UpperCAmelCase_ :							List[str]=False			)->  str:







              """simple docstring"""
              UpperCamelCase					 =		super()._prepare_for_class(UpperCAmelCase_       ,      UpperCAmelCase_       ,      return_labels=UpperCAmelCase_			)

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






       def 	_SCREAMING_SNAKE_CASE       (  self :							Optional[Any]			)->  int:







              """simple docstring"""
              UpperCamelCase					 =		NezhaModelTester(self			)
              UpperCamelCase					 =		ConfigTester(self       ,      config_class=UpperCAmelCase_       ,      hidden_size=37			)






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







              """simple docstring"""
              self.config_tester.run_common_tests()






       def 	_SCREAMING_SNAKE_CASE       (  self :							int			)->  Any:







              """simple docstring"""
              UpperCamelCase					 =		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_model(*UpperCAmelCase_			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							str			)->  Tuple:







              """simple docstring"""
              UpperCamelCase					 =		self.model_tester.prepare_config_and_inputs_for_decoder()
              self.model_tester.create_and_check_model_as_decoder(*UpperCAmelCase_			)






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







              """simple docstring"""
              # This regression test was failing with PyTorch < 1.3
              (
                  (
                  UpperCamelCase
              ) ,       (
                  UpperCamelCase
              ) ,       (
                  UpperCamelCase
              ) ,       (
                  UpperCamelCase
              ) ,       (
                  UpperCamelCase
              ) ,       (
                  UpperCamelCase
              ) ,       (
                  UpperCamelCase
              ) ,       (
                  UpperCamelCase
              ) ,       (
                  UpperCamelCase
              ) ,       
              )					 =		self.model_tester.prepare_config_and_inputs_for_decoder()

              UpperCamelCase					 =		None

              self.model_tester.create_and_check_model_as_decoder(
                  UpperCAmelCase_       ,      UpperCAmelCase_       ,      UpperCAmelCase_       ,      UpperCAmelCase_       ,      UpperCAmelCase_       ,      UpperCAmelCase_       ,      UpperCAmelCase_       ,      UpperCAmelCase_       ,      UpperCAmelCase_       ,      )






       def 	_SCREAMING_SNAKE_CASE       (  self :							int			)->  int:







              """simple docstring"""
              UpperCamelCase					 =		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_masked_lm(*UpperCAmelCase_			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							Optional[int]			)->  List[Any]:







              """simple docstring"""
              UpperCamelCase					 =		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_multiple_choice(*UpperCAmelCase_			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							Dict			)->  List[str]:







              """simple docstring"""
              UpperCamelCase					 =		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_next_sequence_prediction(*UpperCAmelCase_			)






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







              """simple docstring"""
              UpperCamelCase					 =		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_pretraining(*UpperCAmelCase_			)






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







              """simple docstring"""
              UpperCamelCase					 =		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_question_answering(*UpperCAmelCase_			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							List[Any]			)->  Dict:







              """simple docstring"""
              UpperCamelCase					 =		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_sequence_classification(*UpperCAmelCase_			)






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







              """simple docstring"""
              UpperCamelCase					 =		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_token_classification(*UpperCAmelCase_			)






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







              """simple docstring"""
              for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                     UpperCamelCase					 =		NezhaModel.from_pretrained(UpperCAmelCase_			)
                     self.assertIsNotNone(UpperCAmelCase_			)






       @slow
       @require_torch_gpu
       def 	_SCREAMING_SNAKE_CASE       (  self :							List[str]			)->  Union[str, Any]:







              """simple docstring"""
              UpperCamelCase ,       UpperCamelCase					 =		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					 =		True
                     UpperCamelCase					 =		model_class(config=UpperCAmelCase_			)

                     UpperCamelCase					 =		self._prepare_for_class(UpperCAmelCase_       ,      UpperCAmelCase_			)
                     UpperCamelCase					 =		torch.jit.trace(
                         UpperCAmelCase_       ,      (inputs_dict["input_ids"].to("cpu"			), inputs_dict["attention_mask"].to("cpu"			))			)

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





@require_torch
class   __a  (      unittest.TestCase	):






       @slow
       def 	_SCREAMING_SNAKE_CASE       (  self :							Dict			)->  Optional[Any]:







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






       @slow
       def 	_SCREAMING_SNAKE_CASE       (  self :							Tuple			)->  Tuple:







              """simple docstring"""
              UpperCamelCase					 =		NezhaForMaskedLM.from_pretrained("sijunhe/nezha-cn-base"			)
              UpperCamelCase					 =		torch.tensor([[0, 1, 2, 3, 4, 5]]			)
              UpperCamelCase					 =		torch.tensor([[1, 1, 1, 1, 1, 1]]			)
              with torch.no_grad():
                     UpperCamelCase					 =		model(UpperCAmelCase_       ,      attention_mask=UpperCAmelCase_			)[0]
              UpperCamelCase					 =		torch.Size((1, 6, 21_128)			)
              self.assertEqual(output.shape       ,      UpperCAmelCase_			)
              UpperCamelCase					 =		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]       ,      UpperCAmelCase_       ,      atol=1e-4			)			)







"""simple docstring"""


def 	lowerCamelCase__			(							UpperCAmelCase_				)->							int:







       """simple docstring"""
       if a < 0:
              raise ValueError("Input value must be a positive integer"				)
       elif isinstance(UpperCAmelCase_		,      UpperCAmelCase_				):
              raise TypeError("Input value must be a 'int' type"				)
       return bin(UpperCAmelCase_				).count("1"				)


if __name__ == "__main__":
      import doctest

      doctest.testmod()





