



'''simple docstring'''





_UpperCamelCase											=		65521

def 			a_	(				_lowerCAmelCase			) -> int:
  __lowerCamelCase			:			Any       			=					1
  __lowerCamelCase			:			Any       			=					0
  for plain_chr in plain_text:
    __lowerCamelCase			:			str       			=					(a + ord(_lowerCAmelCase			)) % MOD_ADLER
    __lowerCamelCase			:			Union[str, Any]       			=					(b + a) % MOD_ADLER
  return (b << 16) | a











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




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




  def 		_lowercase	(					self :      Optional[int]  )   ->   Dict:
    __lowerCamelCase			:			Tuple       			=					ids_tensor([self.batch_size, self.seq_length]     ,      self.vocab_size  )

    __lowerCamelCase			:			Optional[int]       			=					None
    if self.use_input_mask:
      __lowerCamelCase			:			Optional[int]       			=					random_attention_mask([self.batch_size, self.seq_length]  )

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

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

    __lowerCamelCase			:			List[Any]       			=					self.get_config()

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




  def 		_lowercase	(					self :      List[str]  )   ->   int:
    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=_a     ,      initializer_range=self.initializer_range     ,      )




  def 		_lowercase	(					self :      Tuple  )   ->   Optional[Any]:
    (
        (
        __lowerCamelCase
    )			,(
        __lowerCamelCase
    )			,(
        __lowerCamelCase
    )			,(
        __lowerCamelCase
    )			,(
        __lowerCamelCase
    )			,(
        __lowerCamelCase
    )			,(
        __lowerCamelCase
    )			,
    )			:			List[Any]       			=					self.prepare_config_and_inputs()

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




  def 		_lowercase	(					self :      int     ,      _a :      Tuple     ,      _a :      List[Any]     ,      _a :      Any     ,      _a :      Optional[Any]     ,      _a :      Dict     ,      _a :      Dict     ,      _a :      List[Any]  )   ->   str:
    __lowerCamelCase			:			str       			=					NezhaForNextSentencePrediction(config=_a  )
    model.to(_a  )
    model.eval()
    __lowerCamelCase			:			str       			=					model(
        _a     ,      attention_mask=_a     ,      token_type_ids=_a     ,      labels=_a     ,      )
    self.parent.assertEqual(result.logits.shape     ,      (self.batch_size, 2)  )




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




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




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




  def 		_lowercase	(					self :      List[str]  )   ->   Any:
    __lowerCamelCase			:			Optional[Any]       			=					self.prepare_config_and_inputs()
    (
        (
        __lowerCamelCase
    )			,(
        __lowerCamelCase
    )			,(
        __lowerCamelCase
    )			,(
        __lowerCamelCase
    )			,(
        __lowerCamelCase
    )			,(
        __lowerCamelCase
    )			,(
        __lowerCamelCase
    )			,
    )			:			Optional[int]       			=					config_and_inputs
    __lowerCamelCase			:			List[Any]       			=					{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
    return config, inputs_dict






@require_torch
class    lowerCamelCase_       (							SCREAMING_SNAKE_CASE_  ,  SCREAMING_SNAKE_CASE_  ,  SCREAMING_SNAKE_CASE_  ,  unittest.TestCase	):
  """simple docstring"""




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




  def 		_lowercase	(					self :      List[str]     ,      _a :      Union[str, Any]     ,      _a :      Any     ,      _a :      List[Any]=False  )   ->   Optional[Any]:
    __lowerCamelCase			:			List[str]       			=					super()._prepare_for_class(_a     ,      _a     ,      return_labels=_a  )

    if return_labels:
      if model_class in get_values(_a  ):
        __lowerCamelCase			:			Tuple       			=					torch.zeros(
            (self.model_tester.batch_size, self.model_tester.seq_length)     ,      dtype=torch.long     ,      device=_a  )
        __lowerCamelCase			:			List[Any]       			=					torch.zeros(
            self.model_tester.batch_size     ,      dtype=torch.long     ,      device=_a  )
    return inputs_dict




  def 		_lowercase	(					self :      Optional[int]  )   ->   Any:
    __lowerCamelCase			:			Dict       			=					NezhaModelTester(self  )
    __lowerCamelCase			:			int       			=					ConfigTester(self     ,      config_class=_a     ,      hidden_size=37  )




  def 		_lowercase	(					self :      Optional[Any]  )   ->   List[Any]:
    self.config_tester.run_common_tests()




  def 		_lowercase	(					self :      int  )   ->   List[str]:
    __lowerCamelCase			:			str       			=					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*_a  )




  def 		_lowercase	(					self :      Optional[Any]  )   ->   Union[str, Any]:
    __lowerCamelCase			:			List[str]       			=					self.model_tester.prepare_config_and_inputs_for_decoder()
    self.model_tester.create_and_check_model_as_decoder(*_a  )




  def 		_lowercase	(					self :      str  )   ->   Any:
    # This regression test was failing with PyTorch < 1.3
    (
        (
        __lowerCamelCase
    )			,(
        __lowerCamelCase
    )			,(
        __lowerCamelCase
    )			,(
        __lowerCamelCase
    )			,(
        __lowerCamelCase
    )			,(
        __lowerCamelCase
    )			,(
        __lowerCamelCase
    )			,(
        __lowerCamelCase
    )			,(
        __lowerCamelCase
    )			,
    )			:			str       			=					self.model_tester.prepare_config_and_inputs_for_decoder()

    __lowerCamelCase			:			Dict       			=					None

    self.model_tester.create_and_check_model_as_decoder(
        _a     ,      _a     ,      _a     ,      _a     ,      _a     ,      _a     ,      _a     ,      _a     ,      _a     ,      )




  def 		_lowercase	(					self :      int  )   ->   Tuple:
    __lowerCamelCase			:			Any       			=					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_masked_lm(*_a  )




  def 		_lowercase	(					self :      str  )   ->   str:
    __lowerCamelCase			:			str       			=					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_multiple_choice(*_a  )




  def 		_lowercase	(					self :      List[str]  )   ->   int:
    __lowerCamelCase			:			int       			=					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_next_sequence_prediction(*_a  )




  def 		_lowercase	(					self :      Any  )   ->   Any:
    __lowerCamelCase			:			Union[str, Any]       			=					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_pretraining(*_a  )




  def 		_lowercase	(					self :      Union[str, Any]  )   ->   Optional[int]:
    __lowerCamelCase			:			Dict       			=					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_question_answering(*_a  )




  def 		_lowercase	(					self :      Optional[int]  )   ->   str:
    __lowerCamelCase			:			Optional[Any]       			=					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_sequence_classification(*_a  )




  def 		_lowercase	(					self :      Dict  )   ->   Any:
    __lowerCamelCase			:			str       			=					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_token_classification(*_a  )




  @slow
  def 		_lowercase	(					self :      Optional[Any]  )   ->   Union[str, Any]:
    for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
      __lowerCamelCase			:			List[str]       			=					NezhaModel.from_pretrained(_a  )
      self.assertIsNotNone(_a  )




  @slow
  @require_torch_gpu
  def 		_lowercase	(					self :      List[Any]  )   ->   str:
    __lowerCamelCase			,__lowerCamelCase			:			Optional[int]       			=					self.model_tester.prepare_config_and_inputs_for_common()
    for model_class in self.all_model_classes:
      # NezhaForMultipleChoice behaves incorrectly in JIT environments.
      if model_class == NezhaForMultipleChoice:
        return

      __lowerCamelCase			:			List[str]       			=					True
      __lowerCamelCase			:			Dict       			=					model_class(config=_a  )

      __lowerCamelCase			:			List[str]       			=					self._prepare_for_class(_a     ,      _a  )
      __lowerCamelCase			:			Dict       			=					torch.jit.trace(
          _a     ,      (inputs_dict['input_ids'].to('cpu'  ), inputs_dict['attention_mask'].to('cpu'  ))  )

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






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




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




  @slow
  def 		_lowercase	(					self :      Dict  )   ->   Dict:
    __lowerCamelCase			:			Dict       			=					NezhaForMaskedLM.from_pretrained('sijunhe/nezha-cn-base'  )
    __lowerCamelCase			:			Optional[Any]       			=					torch.tensor([[0, 1, 2, 3, 4, 5]]  )
    __lowerCamelCase			:			List[str]       			=					torch.tensor([[1, 1, 1, 1, 1, 1]]  )
    with torch.no_grad():
      __lowerCamelCase			:			Union[str, Any]       			=					model(_a     ,      attention_mask=_a  )[0]
    __lowerCamelCase			:			Optional[Any]       			=					torch.Size((1, 6, 2_1128)  )
    self.assertEqual(output.shape     ,      _a  )
    __lowerCamelCase			:			Optional[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]     ,      _a     ,      atol=1e-4  )  )






