





'''simple docstring'''




import io
import os
import unicodedata
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


_A:					Optional[Any]							=				logging.get_logger(__name__)

_A:					Dict							=				"""▁"""

_A:					Optional[Any]							=				{"""vocab_file""": """vocab.txt""", """sentencepiece_model_ckpt""": """sentencepiece.bpe.model"""}

_A:					Tuple							=				{
    """sentencepiece_model_file""": """sentencepiece.bpe.model""",
    """vocab_file""": """vocab.txt""",
}

_A:					Any							=				{
    """vocab_file""": {
        """ernie-m-base""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt""",
        """ernie-m-large""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt""",
    },
    """sentencepiece_model_file""": {
        """ernie-m-base""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model""",
        """ernie-m-large""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model""",
    },
}

_A:					Union[str, Any]							=				{
    """ernie-m-base""": 514,
    """ernie-m-large""": 514,
}

_A:					Union[str, Any]							=				{
    """ernie-m-base""": {"""do_lower_case""": False},
    """ernie-m-large""": {"""do_lower_case""": False},
}






class UpperCAmelCase							(		UpperCAmelCase_							):
    _A :			List[str]						   =       ["input_ids"]

    _A :			str						   =       VOCAB_FILES_NAMES
    _A :			List[str]						   =       PRETRAINED_INIT_CONFIGURATION
    _A :			List[str]						   =       PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
    _A :			Optional[int]						   =       PRETRAINED_VOCAB_FILES_MAP
    _A :			str						   =       RESOURCE_FILES_NAMES



    def __init__(  self     ,			__A     ,			__A=None     ,			__A=False     ,			__A="utf8"     ,			__A="[UNK]"     ,			__A="[SEP]"     ,			__A="[PAD]"     ,			__A="[CLS]"     ,			__A="[MASK]"     ,			__A = None     ,			**__A     ,			):
     # Mask token behave like a normal word, i.e. include the space before it and
     # is included in the raw text, there should be a match in a non-normalized sentence.

     __UpperCAmelCase					=   {} if sp_model_kwargs is None else sp_model_kwargs
     super().__init__(
         do_lower_case=__A     ,			unk_token=__A     ,			sep_token=__A     ,			pad_token=__A     ,			cls_token=__A     ,			mask_token=__A     ,			vocab_file=__A     ,			encoding=__A     ,			sp_model_kwargs=self.sp_model_kwargs     ,			**__A     ,			)
     __UpperCAmelCase					=   do_lower_case
     __UpperCAmelCase					=   sentencepiece_model_ckpt
     __UpperCAmelCase					=   spm.SentencePieceProcessor(**self.sp_model_kwargs					)
     self.sp_model.Load(__A					)

     # to mimic paddlenlp.transformers.ernie_m.tokenizer.ErnieMTokenizer functioning
     if vocab_file is not None:
      __UpperCAmelCase					=   self.load_vocab(filepath=__A					)
     else:
      __UpperCAmelCase					=   {self.sp_model.id_to_piece(__A					): id for id in range(self.sp_model.get_piece_size()					)}
     __UpperCAmelCase					=   {v: k for k, v in self.vocab.items()}



    def 					__lowerCamelCase						(  self     ,			__A					):
     if text is None:
      return None

     __UpperCAmelCase					=   self.tokenize(__A					)
     __UpperCAmelCase					,    __UpperCAmelCase					=   '', []

     for i, ch in enumerate(__A					):
      if ch in self.SP_CHAR_MAPPING:
       __UpperCAmelCase					=   self.SP_CHAR_MAPPING.get(__A					)
      else:
       __UpperCAmelCase					=   unicodedata.normalize('NFKC'     ,			__A					)
      if self.is_whitespace(__A					):
       continue
      normalized_text += ch
      char_mapping.extend([i] * len(__A					)					)

     __UpperCAmelCase					,    __UpperCAmelCase					,    __UpperCAmelCase					=   normalized_text, [], 0

     if self.do_lower_case:
      __UpperCAmelCase					=   text.lower()

     for token in split_tokens:
      if token[:1] == "▁":
       __UpperCAmelCase					=   token[1:]
      __UpperCAmelCase					=   text[offset:].index(__A					) + offset
      __UpperCAmelCase					=   start + len(__A					)

      token_mapping.append((char_mapping[start], char_mapping[end - 1] + 1)					)
      __UpperCAmelCase					=   end
     return token_mapping



    @property
    def 					__lowerCamelCase						(  self					):
     return len(self.vocab					)



    def 					__lowerCamelCase						(  self					):
     return dict(self.vocab     ,			**self.added_tokens_encoder					)



    def __getstate__(  self					):
     __UpperCAmelCase					=   self.__dict__.copy()
     __UpperCAmelCase					=   None
     return state



    def __setstate__(  self     ,			__A					):
     __UpperCAmelCase					=   d

     # for backward compatibility
     if not hasattr(self     ,			'sp_model_kwargs'					):
      __UpperCAmelCase					=   {}

     __UpperCAmelCase					=   spm.SentencePieceProcessor(**self.sp_model_kwargs					)
     self.sp_model.Load(self.sentencepiece_model_ckpt					)



    def 					__lowerCamelCase						(  self     ,			__A					):
     return "".join((self.SP_CHAR_MAPPING.get(__A     ,			__A					) for c in text)					)



    def 					__lowerCamelCase						(  self     ,			__A     ,			__A=False     ,			__A=64     ,			__A=0.1					):

     if self.sp_model_kwargs.get('enable_sampling'					) is True:
      __UpperCAmelCase					=   True
     if self.sp_model_kwargs.get('alpha'					) is not None:
      __UpperCAmelCase					=   self.sp_model_kwargs.get('alpha'					)
     if self.sp_model_kwargs.get('nbest_size'					) is not None:
      __UpperCAmelCase					=   self.sp_model_kwargs.get('nbest_size'					)

     if not enable_sampling:
      __UpperCAmelCase					=   self.sp_model.EncodeAsPieces(__A					)
     else:
      __UpperCAmelCase					=   self.sp_model.SampleEncodeAsPieces(__A     ,			__A     ,			__A					)
     __UpperCAmelCase					=   []
     for pi, piece in enumerate(__A					):
      if piece == SPIECE_UNDERLINE:
       if not pieces[pi + 1].startswith(__A					) and pi != 0:
        new_pieces.append(__A					)
        continue
       else:
        continue
      __UpperCAmelCase					=   0
      for i, chunk in enumerate(__A					):
       if chunk == SPIECE_UNDERLINE:
        continue
       if self.is_ch_char(__A					) or self.is_punct(__A					):
        if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE:
         new_pieces.append(piece[lst_i:i]					)
        new_pieces.append(__A					)
        __UpperCAmelCase					=   i + 1
       elif chunk.isdigit() and i > 0 and not piece[i - 1].isdigit():
        if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE:
         new_pieces.append(piece[lst_i:i]					)
        __UpperCAmelCase					=   i
       elif not chunk.isdigit() and i > 0 and piece[i - 1].isdigit():
        if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE:
         new_pieces.append(piece[lst_i:i]					)
        __UpperCAmelCase					=   i
      if len(__A					) > lst_i:
       new_pieces.append(piece[lst_i:]					)
     return new_pieces



    def 					__lowerCamelCase						(  self     ,			__A					):
     __UpperCAmelCase					=   ''.join(__A					).replace(__A     ,			' '					).strip()
     return out_string



    def 					__lowerCamelCase						(  self     ,			__A					):
     __UpperCAmelCase					=   self.convert_ids_to_tokens(__A					)
     __UpperCAmelCase					=   ''.join(__A					).replace(__A     ,			' '					).strip()
     return out_string



    def 					__lowerCamelCase						(  self     ,			__A					):
     return self.vocab.get(__A     ,			self.vocab.get(self.unk_token					)					)



    def 					__lowerCamelCase						(  self     ,			__A					):
     return self.reverse_vocab.get(__A     ,			self.unk_token					)



    def 					__lowerCamelCase						(  self     ,			__A     ,			__A=None					):
     if token_ids_a is None:
      return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
     __UpperCAmelCase					=   [self.cls_token_id]
     __UpperCAmelCase					=   [self.sep_token_id]
     return _cls + token_ids_a + _sep + _sep + token_ids_a + _sep



    def 					__lowerCamelCase						(  self     ,			__A     ,			__A=None					):
     if offset_mapping_a is None:
      return [(0, 0)] + offset_mapping_a + [(0, 0)]

     return [(0, 0)] + offset_mapping_a + [(0, 0), (0, 0)] + offset_mapping_a + [(0, 0)]



    def 					__lowerCamelCase						(  self     ,			__A     ,			__A=None     ,			__A=False					):

     if already_has_special_tokens:
      if token_ids_a is not None:
       raise ValueError(
           'You should not supply a second sequence if the provided sequence of '
           'ids is already formatted with special tokens for the model.'					)
      return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a]

     if token_ids_a is not None:
      return [1] + ([0] * len(__A					)) + [1, 1] + ([0] * len(__A					)) + [1]
     return [1] + ([0] * len(__A					)) + [1]



    def 					__lowerCamelCase						(  self     ,			__A     ,			__A = None					):
     # called when `add_special_tokens` is True, so align with `build_inputs_with_special_tokens` method
     if token_ids_a is None:
      # [CLS] X [SEP]
      return (len(__A					) + 2) * [0]

     # [CLS] A [SEP] [SEP] B [SEP]
     return [0] * (len(__A					) + 1) + [1] * (len(__A					) + 3)



    def 					__lowerCamelCase						(  self     ,			__A					):
     if "\u4e00" <= char <= "\u9fff":
      return True
     return False



    def 					__lowerCamelCase						(  self     ,			__A					):
     if ("a" <= char <= "z") or ("A" <= char <= "Z"):
      return True
     return False



    def 					__lowerCamelCase						(  self     ,			__A					):
     if char in ",;:.?!~，；：。？！《》【】":
      return True
     return False



    def 					__lowerCamelCase						(  self     ,			__A					):
     if char == " " or char == "\t" or char == "\n" or char == "\r":
      return True
     if len(__A					) == 1:
      __UpperCAmelCase					=   unicodedata.category(__A					)
      if cat == "Zs":
       return True
     return False



    def 					__lowerCamelCase						(  self     ,			__A					):
     __UpperCAmelCase					=   {}
     with io.open(__A     ,			'r'     ,			encoding='utf-8'					) as f:
      for index, line in enumerate(__A					):
       __UpperCAmelCase					=   line.rstrip('\n'					)
       __UpperCAmelCase					=   int(__A					)

     return token_to_idx




    def 					__lowerCamelCase						(  self     ,			__A     ,			__A = None					):
     __UpperCAmelCase					=   0
     if os.path.isdir(__A					):
      __UpperCAmelCase					=   os.path.join(
          __A     ,			(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']					)
     else:
      __UpperCAmelCase					=   (filename_prefix + '-' if filename_prefix else '') + save_directory
     with open(__A     ,			'w'     ,			encoding='utf-8'					) as writer:
      for token, token_index in sorted(self.vocab.items()     ,			key=lambda __A					: kv[1]					):
       if index != token_index:
        logger.warning(
            f'Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive.'
            ' Please check that the vocabulary is not corrupted!'					)
        __UpperCAmelCase					=   token_index
       writer.write(token + '\n'					)
       index += 1

     __UpperCAmelCase					=   os.path.join(__A     ,			'sentencepiece.bpe.model'					)
     with open(__A     ,			'wb'					) as fi:
      __UpperCAmelCase					=   self.sp_model.serialized_model_proto()
      fi.write(__A					)

     return (vocab_file,)












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



    def __init__(  self     ,			__A     ,			__A=13     ,			__A=7     ,			__A=True     ,			__A=True     ,			__A=True     ,			__A=True     ,			__A=99     ,			__A=32     ,			__A=5     ,			__A=4     ,			__A=37     ,			__A="gelu"     ,			__A=0.1     ,			__A=0.1     ,			__A=128     ,			__A=32     ,			__A=16     ,			__A=2     ,			__A=0.0_2     ,			__A=3     ,			__A=4     ,			__A=None     ,			):
     __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 					__lowerCamelCase						(  self					):
     __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 					__lowerCamelCase						(  self					):
     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 					__lowerCamelCase						(  self					):
     (
         (
         __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 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   NezhaModel(config=__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   model(__A     ,			attention_mask=__A     ,			token_type_ids=__A					)
     __UpperCAmelCase					=   model(__A     ,			token_type_ids=__A					)
     __UpperCAmelCase					=   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 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			):
     __UpperCAmelCase					=   True
     __UpperCAmelCase					=   NezhaModel(__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   model(
         __A     ,			attention_mask=__A     ,			token_type_ids=__A     ,			encoder_hidden_states=__A     ,			encoder_attention_mask=__A     ,			)
     __UpperCAmelCase					=   model(
         __A     ,			attention_mask=__A     ,			token_type_ids=__A     ,			encoder_hidden_states=__A     ,			)
     __UpperCAmelCase					=   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 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   NezhaForMaskedLM(config=__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   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 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   NezhaForNextSentencePrediction(config=__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   model(
         __A     ,			attention_mask=__A     ,			token_type_ids=__A     ,			labels=__A     ,			)
     self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, 2)					)



    def 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   NezhaForPreTraining(config=__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   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 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   NezhaForQuestionAnswering(config=__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   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 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   self.num_labels
     __UpperCAmelCase					=   NezhaForSequenceClassification(__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   model(__A     ,			attention_mask=__A     ,			token_type_ids=__A     ,			labels=__A					)
     self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, self.num_labels)					)



    def 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   self.num_labels
     __UpperCAmelCase					=   NezhaForTokenClassification(config=__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   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 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   self.num_choices
     __UpperCAmelCase					=   NezhaForMultipleChoice(config=__A					)
     model.to(__A					)
     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(
         __A     ,			attention_mask=__A     ,			token_type_ids=__A     ,			labels=__A     ,			)
     self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, self.num_choices)					)




    def 					__lowerCamelCase						(  self					):
     __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 UpperCAmelCase							(		UpperCAmelCase_					,			UpperCAmelCase_					,			UpperCAmelCase_					,			unittest.TestCase							):
    _A :			List[str]						   =       (
        (
            NezhaModel,
            NezhaForMaskedLM,
            NezhaForMultipleChoice,
            NezhaForNextSentencePrediction,
            NezhaForPreTraining,
            NezhaForQuestionAnswering,
            NezhaForSequenceClassification,
            NezhaForTokenClassification,
        )
        if is_torch_available()
        else ()
    )
    _A :			str						   =       (
        {
            """feature-extraction""": NezhaModel,
            """fill-mask""": NezhaForMaskedLM,
            """question-answering""": NezhaForQuestionAnswering,
            """text-classification""": NezhaForSequenceClassification,
            """token-classification""": NezhaForTokenClassification,
            """zero-shot""": NezhaForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    _A :			Dict						   =       True



    def 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A=False					):
     __UpperCAmelCase					=   super()._prepare_for_class(__A     ,			__A     ,			return_labels=__A					)

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



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   NezhaModelTester(self					)
     __UpperCAmelCase					=   ConfigTester(self     ,			config_class=__A     ,			hidden_size=37					)



    def 					__lowerCamelCase						(  self					):
     self.config_tester.run_common_tests()



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*__A					)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs_for_decoder()
     self.model_tester.create_and_check_model_as_decoder(*__A					)



    def 					__lowerCamelCase						(  self					):
     # 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(
         __A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_lm(*__A					)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_multiple_choice(*__A					)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_next_sequence_prediction(*__A					)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_pretraining(*__A					)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*__A					)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*__A					)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*__A					)



    @slow
    def 					__lowerCamelCase						(  self					):
     for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
      __UpperCAmelCase					=   NezhaModel.from_pretrained(__A					)
      self.assertIsNotNone(__A					)




    @slow
    @require_torch_gpu
    def 					__lowerCamelCase						(  self					):
     __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=__A					)

      __UpperCAmelCase					=   self._prepare_for_class(__A     ,			__A					)
      __UpperCAmelCase					=   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'					)					)
       __UpperCAmelCase					=   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 UpperCAmelCase							(		unittest.TestCase							):



    @slow
    def 					__lowerCamelCase						(  self					):
     __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(__A     ,			attention_mask=__A					)[0]
     __UpperCAmelCase					=   torch.Size((1, 6, 768)					)
     self.assertEqual(output.shape     ,			__A					)
     __UpperCAmelCase					=   torch.tensor([[[0.0_6_8_5, 0.2_4_4_1, 0.1_1_0_2], [0.0_6_0_0, 0.1_9_0_6, 0.1_3_4_9], [0.0_2_2_1, 0.0_8_1_9, 0.0_5_8_6]]]					)

     self.assertTrue(torch.allclose(output[:, 1:4, 1:4]     ,			__A     ,			atol=1E-4					)					)




    @slow
    def 					__lowerCamelCase						(  self					):
     __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(__A     ,			attention_mask=__A					)[0]
     __UpperCAmelCase					=   torch.Size((1, 6, 21_128)					)
     self.assertEqual(output.shape     ,			__A					)
     __UpperCAmelCase					=   torch.tensor(
         [[-2.7_9_3_9, -1.7_9_0_2, -2.2_1_8_9], [-2.8_5_8_5, -1.8_9_0_8, -2.3_7_2_3], [-2.6_4_9_9, -1.7_7_5_0, -2.2_5_5_8]]					)

     self.assertTrue(torch.allclose(output[:, 1:4, 1:4]     ,			__A     ,			atol=1E-4					)					)





