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   lowerCamelCase					:
  """simple docstring"""







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







  def    __A     (							self :       List[Any]   )  ->	Tuple:
        SCREAMING_SNAKE_CASE_          =  ids_tensor([self.batch_size, self.seq_length]  ,     self.vocab_size   )

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

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

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

        SCREAMING_SNAKE_CASE_          =  self.get_config()

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







  def    __A     (							self :       Optional[Any]   )  ->	Union[str, Any]:
        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=__magic_name__  ,     initializer_range=self.initializer_range  ,     )







  def    __A     (							self :       Optional[Any]   )  ->	Dict:
        (
            (
            SCREAMING_SNAKE_CASE_
        )     , (
            SCREAMING_SNAKE_CASE_
        )     , (
            SCREAMING_SNAKE_CASE_
        )     , (
            SCREAMING_SNAKE_CASE_
        )     , (
            SCREAMING_SNAKE_CASE_
        )     , (
            SCREAMING_SNAKE_CASE_
        )     , (
            SCREAMING_SNAKE_CASE_
        )     , 
        )          =  self.prepare_config_and_inputs()

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







  def    __A     (							self :       List[Any]  ,     __magic_name__ :       Union[str, Any]  ,     __magic_name__ :       Dict  ,     __magic_name__ :       Tuple  ,     __magic_name__ :       Union[str, Any]  ,     __magic_name__ :       str  ,     __magic_name__ :       Tuple  ,     __magic_name__ :       Union[str, Any]   )  ->	List[Any]:
        SCREAMING_SNAKE_CASE_          =  NezhaForNextSentencePrediction(config=__magic_name__   )
        model.to(__magic_name__   )
        model.eval()
        SCREAMING_SNAKE_CASE_          =  model(
            __magic_name__  ,     attention_mask=__magic_name__  ,     token_type_ids=__magic_name__  ,     labels=__magic_name__  ,     )
        self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, 2)   )







  def    __A     (							self :       Optional[Any]  ,     __magic_name__ :       Dict  ,     __magic_name__ :       str  ,     __magic_name__ :       Optional[int]  ,     __magic_name__ :       List[str]  ,     __magic_name__ :       Dict  ,     __magic_name__ :       Union[str, Any]  ,     __magic_name__ :       Dict   )  ->	Any:
        SCREAMING_SNAKE_CASE_          =  NezhaForPreTraining(config=__magic_name__   )
        model.to(__magic_name__   )
        model.eval()
        SCREAMING_SNAKE_CASE_          =  model(
            __magic_name__  ,     attention_mask=__magic_name__  ,     token_type_ids=__magic_name__  ,     labels=__magic_name__  ,     next_sentence_label=__magic_name__  ,     )
        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    __A     (							self :       Tuple  ,     __magic_name__ :       Dict  ,     __magic_name__ :       List[str]  ,     __magic_name__ :       List[Any]  ,     __magic_name__ :       Optional[int]  ,     __magic_name__ :       Dict  ,     __magic_name__ :       List[Any]  ,     __magic_name__ :       List[Any]   )  ->	Dict:
        SCREAMING_SNAKE_CASE_          =  NezhaForQuestionAnswering(config=__magic_name__   )
        model.to(__magic_name__   )
        model.eval()
        SCREAMING_SNAKE_CASE_          =  model(
            __magic_name__  ,     attention_mask=__magic_name__  ,     token_type_ids=__magic_name__  ,     start_positions=__magic_name__  ,     end_positions=__magic_name__  ,     )
        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    __A     (							self :       Tuple  ,     __magic_name__ :       List[str]  ,     __magic_name__ :       Optional[int]  ,     __magic_name__ :       str  ,     __magic_name__ :       Dict  ,     __magic_name__ :       List[str]  ,     __magic_name__ :       Union[str, Any]  ,     __magic_name__ :       List[Any]   )  ->	Optional[Any]:
        SCREAMING_SNAKE_CASE_          =  self.num_labels
        SCREAMING_SNAKE_CASE_          =  NezhaForSequenceClassification(__magic_name__   )
        model.to(__magic_name__   )
        model.eval()
        SCREAMING_SNAKE_CASE_          =  model(__magic_name__  ,     attention_mask=__magic_name__  ,     token_type_ids=__magic_name__  ,     labels=__magic_name__   )
        self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.num_labels)   )







  def    __A     (							self :       Any  ,     __magic_name__ :       List[str]  ,     __magic_name__ :       int  ,     __magic_name__ :       List[Any]  ,     __magic_name__ :       List[Any]  ,     __magic_name__ :       Union[str, Any]  ,     __magic_name__ :       Union[str, Any]  ,     __magic_name__ :       Tuple   )  ->	Dict:
        SCREAMING_SNAKE_CASE_          =  self.num_labels
        SCREAMING_SNAKE_CASE_          =  NezhaForTokenClassification(config=__magic_name__   )
        model.to(__magic_name__   )
        model.eval()
        SCREAMING_SNAKE_CASE_          =  model(__magic_name__  ,     attention_mask=__magic_name__  ,     token_type_ids=__magic_name__  ,     labels=__magic_name__   )
        self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.seq_length, self.num_labels)   )







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







  def    __A     (							self :       str   )  ->	Tuple:
        SCREAMING_SNAKE_CASE_          =  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_
        )     , 
        )          =  config_and_inputs
        SCREAMING_SNAKE_CASE_          =  {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
        return config, inputs_dict



@require_torch
class   lowerCamelCase					(SCREAMING_SNAKE_CASE__  ,      SCREAMING_SNAKE_CASE__  ,      SCREAMING_SNAKE_CASE__  ,      unittest.TestCase					):
  """simple docstring"""







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







  def    __A     (							self :       int  ,     __magic_name__ :       List[str]  ,     __magic_name__ :       Dict  ,     __magic_name__ :       Tuple=False   )  ->	str:
        SCREAMING_SNAKE_CASE_          =  super()._prepare_for_class(__magic_name__  ,     __magic_name__  ,     return_labels=__magic_name__   )

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







  def    __A     (							self :       str   )  ->	Union[str, Any]:
        SCREAMING_SNAKE_CASE_          =  NezhaModelTester(self   )
        SCREAMING_SNAKE_CASE_          =  ConfigTester(self  ,     config_class=__magic_name__  ,     hidden_size=37   )







  def    __A     (							self :       Union[str, Any]   )  ->	Any:
        self.config_tester.run_common_tests()







  def    __A     (							self :       int   )  ->	Any:
        SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__magic_name__   )







  def    __A     (							self :       List[str]   )  ->	Optional[Any]:
        SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs_for_decoder()
        self.model_tester.create_and_check_model_as_decoder(*__magic_name__   )







  def    __A     (							self :       str   )  ->	Optional[Any]:
        # This regression test was failing with PyTorch < 1.3
        (
            (
            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_
        )     , 
        )          =  self.model_tester.prepare_config_and_inputs_for_decoder()

        SCREAMING_SNAKE_CASE_          =  None

        self.model_tester.create_and_check_model_as_decoder(
            __magic_name__  ,     __magic_name__  ,     __magic_name__  ,     __magic_name__  ,     __magic_name__  ,     __magic_name__  ,     __magic_name__  ,     __magic_name__  ,     __magic_name__  ,     )







  def    __A     (							self :       Tuple   )  ->	Dict:
        SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*__magic_name__   )







  def    __A     (							self :       Optional[Any]   )  ->	Optional[int]:
        SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_multiple_choice(*__magic_name__   )







  def    __A     (							self :       str   )  ->	List[str]:
        SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_next_sequence_prediction(*__magic_name__   )







  def    __A     (							self :       Union[str, Any]   )  ->	str:
        SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_pretraining(*__magic_name__   )







  def    __A     (							self :       Union[str, Any]   )  ->	int:
        SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*__magic_name__   )







  def    __A     (							self :       str   )  ->	Any:
        SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*__magic_name__   )







  def    __A     (							self :       Dict   )  ->	Tuple:
        SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*__magic_name__   )







  @slow
  def    __A     (							self :       Union[str, Any]   )  ->	str:
        for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              SCREAMING_SNAKE_CASE_          =  NezhaModel.from_pretrained(__magic_name__   )
              self.assertIsNotNone(__magic_name__   )







  @slow
  @require_torch_gpu
  def    __A     (							self :       List[Any]   )  ->	Tuple:
        SCREAMING_SNAKE_CASE_     , SCREAMING_SNAKE_CASE_          =  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_          =  True
              SCREAMING_SNAKE_CASE_          =  model_class(config=__magic_name__   )

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

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



@require_torch
class   lowerCamelCase					(unittest.TestCase					):
  """simple docstring"""







  @slow
  def    __A     (							self :       Tuple   )  ->	Any:
        SCREAMING_SNAKE_CASE_          =  NezhaModel.from_pretrained("sijunhe/nezha-cn-base"   )
        SCREAMING_SNAKE_CASE_          =  torch.tensor([[0, 1, 2, 3, 4, 5]]   )
        SCREAMING_SNAKE_CASE_          =  torch.tensor([[0, 1, 1, 1, 1, 1]]   )
        with torch.no_grad():
              SCREAMING_SNAKE_CASE_          =  model(__magic_name__  ,     attention_mask=__magic_name__   )[0]
        SCREAMING_SNAKE_CASE_          =  torch.Size((1, 6, 768)   )
        self.assertEqual(output.shape  ,     __magic_name__   )
        SCREAMING_SNAKE_CASE_          =  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]  ,     __magic_name__  ,     atol=1e-4   )   )







  @slow
  def    __A     (							self :       List[str]   )  ->	Optional[int]:
        SCREAMING_SNAKE_CASE_          =  NezhaForMaskedLM.from_pretrained("sijunhe/nezha-cn-base"   )
        SCREAMING_SNAKE_CASE_          =  torch.tensor([[0, 1, 2, 3, 4, 5]]   )
        SCREAMING_SNAKE_CASE_          =  torch.tensor([[1, 1, 1, 1, 1, 1]]   )
        with torch.no_grad():
              SCREAMING_SNAKE_CASE_          =  model(__magic_name__  ,     attention_mask=__magic_name__   )[0]
        SCREAMING_SNAKE_CASE_          =  torch.Size((1, 6, 21_128)   )
        self.assertEqual(output.shape  ,     __magic_name__   )
        SCREAMING_SNAKE_CASE_          =  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]  ,     __magic_name__  ,     atol=1e-4   )   )



import os
import unittest

from huggingface_hub.utils import are_progress_bars_disabled

import transformers.models.bart.tokenization_bart
from transformers import logging
from transformers.testing_utils import CaptureLogger, mockenv, mockenv_context
from transformers.utils.logging import disable_progress_bar, enable_progress_bar


class   lowerCamelCase					(unittest.TestCase					):
  """simple docstring"""







  def    __A     (							self :       List[Any]   )  ->	Optional[int]:
        SCREAMING_SNAKE_CASE_          =  logging.get_logger()

        # the current default level is logging.WARNING
        SCREAMING_SNAKE_CASE_          =  logging.get_verbosity()

        logging.set_verbosity_error()
        self.assertEqual(logger.getEffectiveLevel()  ,     logging.get_verbosity()   )

        logging.set_verbosity_warning()
        self.assertEqual(logger.getEffectiveLevel()  ,     logging.get_verbosity()   )

        logging.set_verbosity_info()
        self.assertEqual(logger.getEffectiveLevel()  ,     logging.get_verbosity()   )

        logging.set_verbosity_debug()
        self.assertEqual(logger.getEffectiveLevel()  ,     logging.get_verbosity()   )

        # restore to the original level
        logging.set_verbosity(__magic_name__   )







  def    __A     (							self :       Union[str, Any]   )  ->	Tuple:
        SCREAMING_SNAKE_CASE_          =  logging.get_verbosity()

        SCREAMING_SNAKE_CASE_          =  logging.get_logger("transformers.models.bart.tokenization_bart"   )
        SCREAMING_SNAKE_CASE_          =  "Testing 1, 2, 3"

        # should be able to log warnings (if default settings weren't overridden by `pytest --log-level-all`)
        if level_origin <= logging.WARNING:
              with CaptureLogger(__magic_name__   ) as cl:
                    logger.warning(__magic_name__   )
              self.assertEqual(cl.out  ,     msg + "\n"   )

        # this is setting the level for all of `transformers.*` loggers
        logging.set_verbosity_error()

        # should not be able to log warnings
        with CaptureLogger(__magic_name__   ) as cl:
              logger.warning(__magic_name__   )
        self.assertEqual(cl.out  ,     ""   )

        # should be able to log warnings again
        logging.set_verbosity_warning()
        with CaptureLogger(__magic_name__   ) as cl:
              logger.warning(__magic_name__   )
        self.assertEqual(cl.out  ,     msg + "\n"   )

        # restore to the original level
        logging.set_verbosity(__magic_name__   )







  @mockenv(TRANSFORMERS_VERBOSITY="error"   )
  def    __A     (							self :       int   )  ->	Optional[Any]:
        # reset for the env var to take effect, next time some logger call is made
        transformers.utils.logging._reset_library_root_logger()
        # this action activates the env var
        SCREAMING_SNAKE_CASE_          =  logging.get_logger("transformers.models.bart.tokenization_bart"   )

        SCREAMING_SNAKE_CASE_          =  os.getenv("TRANSFORMERS_VERBOSITY"  ,     __magic_name__   )
        SCREAMING_SNAKE_CASE_          =  logging.log_levels[env_level_str]

        SCREAMING_SNAKE_CASE_          =  logging.get_verbosity()
        self.assertEqual(
            __magic_name__  ,     __magic_name__  ,     F'''TRANSFORMERS_VERBOSITY={env_level_str}/{env_level}, but internal verbosity is {current_level}'''  ,     )

        # restore to the original level
        SCREAMING_SNAKE_CASE_          =  ""
        transformers.utils.logging._reset_library_root_logger()







  @mockenv(TRANSFORMERS_VERBOSITY="super-error"   )
  def    __A     (							self :       Dict   )  ->	List[Any]:
        # reset for the env var to take effect, next time some logger call is made
        transformers.utils.logging._reset_library_root_logger()
        SCREAMING_SNAKE_CASE_          =  logging.logging.getLogger()
        with CaptureLogger(__magic_name__   ) as cl:
              # this action activates the env var
              logging.get_logger("transformers.models.bart.tokenization_bart"   )
        self.assertIn("Unknown option TRANSFORMERS_VERBOSITY=super-error"  ,     cl.out   )

        # no need to restore as nothing was changed







  def    __A     (							self :       Dict   )  ->	Optional[int]:
        # testing `logger.warning_advice()`
        transformers.utils.logging._reset_library_root_logger()

        SCREAMING_SNAKE_CASE_          =  logging.get_logger("transformers.models.bart.tokenization_bart"   )
        SCREAMING_SNAKE_CASE_          =  "Testing 1, 2, 3"

        with mockenv_context(TRANSFORMERS_NO_ADVISORY_WARNINGS="1"   ):
              # nothing should be logged as env var disables this method
              with CaptureLogger(__magic_name__   ) as cl:
                    logger.warning_advice(__magic_name__   )
              self.assertEqual(cl.out  ,     ""   )

        with mockenv_context(TRANSFORMERS_NO_ADVISORY_WARNINGS=""   ):
              # should log normally as TRANSFORMERS_NO_ADVISORY_WARNINGS is unset
              with CaptureLogger(__magic_name__   ) as cl:
                    logger.warning_advice(__magic_name__   )
              self.assertEqual(cl.out  ,     msg + "\n"   )



def      a__ (		):
      disable_progress_bar()
      assert are_progress_bars_disabled()

      enable_progress_bar()
      assert not are_progress_bars_disabled()


