
"""simple docstring"""






import unittest

from transformers import PegasusTokenizer, PegasusTokenizerFast
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, require_torch, slow
from transformers.utils import cached_property

from ...test_tokenization_common import TokenizerTesterMixin


__UpperCAmelCase       =  get_tests_dir('fixtures/test_sentencepiece_no_bos.model')




@require_sentencepiece
@require_tokenizers
class 			_SCREAMING_SNAKE_CASE					(			A__	, unittest.TestCase							):
			UpperCAmelCase_  :Tuple				 =						PegasusTokenizer
			UpperCAmelCase_  :List[Any]				 =						PegasusTokenizerFast
			UpperCAmelCase_  :Optional[int]				 =						True
			UpperCAmelCase_  :Union[str, Any]				 =						True






			def  __lowerCAmelCase (					self       )			->     Tuple:
						super().setUp()

						# We have a SentencePiece fixture for testing
						lowerCAmelCase_    :Optional[int]			     =  PegasusTokenizer(__A       )
						tokenizer.save_pretrained(self.tmpdirname       )






			@cached_property
			def  __lowerCAmelCase (					self       )			->     int:
						return PegasusTokenizer.from_pretrained("""google/pegasus-large"""       )






			def  __lowerCAmelCase (					self       ,   **__A       )			->     PegasusTokenizer:
						return PegasusTokenizer.from_pretrained(self.tmpdirname       ,   **__A       )






			def  __lowerCAmelCase (					self       ,   __A       )			->     List[Any]:
						return ("This is a test", "This is a test")






			def  __lowerCAmelCase (					self       )			->     int:
						lowerCAmelCase_    :Any			     =  """</s>"""
						lowerCAmelCase_    :Optional[int]			     =  1

						self.assertEqual(self.get_tokenizer()._convert_token_to_id(__A       )       ,   __A       )
						self.assertEqual(self.get_tokenizer()._convert_id_to_token(__A       )       ,   __A       )






			def  __lowerCAmelCase (					self       )			->     int:
						lowerCAmelCase_    :Any			     =  list(self.get_tokenizer().get_vocab().keys()       )

						self.assertEqual(vocab_keys[0]       ,   """<pad>"""       )
						self.assertEqual(vocab_keys[1]       ,   """</s>"""       )
						self.assertEqual(vocab_keys[-1]       ,   """v"""       )
						self.assertEqual(len(__A       )       ,   1103       )






			def  __lowerCAmelCase (					self       )			->     Optional[Any]:
						self.assertEqual(self.get_tokenizer().vocab_size       ,   1103       )






			def  __lowerCAmelCase (					self       )			->     Optional[Any]:
						lowerCAmelCase_    :Dict			     =  self.rust_tokenizer_class.from_pretrained(self.tmpdirname       )
						lowerCAmelCase_    :Optional[Any]			     =  self.tokenizer_class.from_pretrained(self.tmpdirname       )
						lowerCAmelCase_    :Optional[Any]			     =  (
						    """Let's see which <unk> is the better <unk_token_11> one <mask_1> It seems like this <mask_2> was important"""
						    """ </s> <pad> <pad> <pad>"""
						)
						lowerCAmelCase_    :List[str]			     =  rust_tokenizer([raw_input_str]       ,   return_tensors=__A       ,   add_special_tokens=__A       ).input_ids[0]
						lowerCAmelCase_    :List[Any]			     =  py_tokenizer([raw_input_str]       ,   return_tensors=__A       ,   add_special_tokens=__A       ).input_ids[0]
						self.assertListEqual(__A       ,   __A       )






			def  __lowerCAmelCase (					self       )			->     Optional[int]:
						lowerCAmelCase_    :Dict			     =  self._large_tokenizer
						# <mask_1> masks whole sentence while <mask_2> masks single word
						lowerCAmelCase_    :int			     =  """<mask_1> To ensure a <mask_2> flow of bank resolutions."""
						lowerCAmelCase_    :Any			     =  [2, 413, 615, 114, 3, 1971, 113, 1679, 1_0710, 107, 1]
						lowerCAmelCase_    :Dict			     =  tokenizer([raw_input_str]       ,   return_tensors=__A       ).input_ids[0]
						self.assertListEqual(__A       ,   __A       )






			def  __lowerCAmelCase (					self       )			->     str:
						lowerCAmelCase_    :int			     =  self._large_tokenizer
						# The tracebacks for the following asserts are **better** without messages or self.assertEqual
						assert tokenizer.vocab_size == 9_6103
						assert tokenizer.pad_token_id == 0
						assert tokenizer.eos_token_id == 1
						assert tokenizer.offset == 103
						assert tokenizer.unk_token_id == tokenizer.offset + 2 == 105
						assert tokenizer.unk_token == "<unk>"
						assert tokenizer.model_max_length == 1024
						lowerCAmelCase_    :Dict			     =  """To ensure a smooth flow of bank resolutions."""
						lowerCAmelCase_    :List[str]			     =  [413, 615, 114, 2291, 1971, 113, 1679, 1_0710, 107, 1]
						lowerCAmelCase_    :Union[str, Any]			     =  tokenizer([raw_input_str]       ,   return_tensors=__A       ).input_ids[0]
						self.assertListEqual(__A       ,   __A       )
						assert tokenizer.convert_ids_to_tokens([0, 1, 2, 3]       ) == ["<pad>", "</s>", "<mask_1>", "<mask_2>"]






			@require_torch
			def  __lowerCAmelCase (					self       )			->     Tuple:
						lowerCAmelCase_    :Dict			     =  ["""This is going to be way too long.""" * 150, """short example"""]
						lowerCAmelCase_    :int			     =  ["""not super long but more than 5 tokens""", """tiny"""]
						lowerCAmelCase_    :str			     =  self._large_tokenizer(__A       ,   padding=__A       ,   truncation=__A       ,   return_tensors="""pt"""       )
						lowerCAmelCase_    :Dict			     =  self._large_tokenizer(
						    text_target=__A       ,   max_length=5       ,   padding=__A       ,   truncation=__A       ,   return_tensors="""pt"""       )

						assert batch.input_ids.shape == (2, 1024)
						assert batch.attention_mask.shape == (2, 1024)
						assert targets["input_ids"].shape == (2, 5)
						assert len(__A       ) == 2  # input_ids, attention_mask.






			@slow
			def  __lowerCAmelCase (					self       )			->     List[Any]:
						# fmt: off
						lowerCAmelCase_    :Dict			     =  {"""input_ids""": [[3_8979, 143, 1_8485, 606, 130, 2_6669, 8_7686, 121, 5_4189, 1129, 111, 2_6669, 8_7686, 121, 9114, 1_4787, 121, 1_3249, 158, 592, 956, 121, 1_4621, 3_1576, 143, 6_2613, 108, 9688, 930, 4_3430, 1_1562, 6_2613, 304, 108, 1_1443, 897, 108, 9314, 1_7415, 6_3399, 108, 1_1443, 7614, 1_8316, 118, 4284, 7148, 1_2430, 143, 1400, 2_5703, 158, 111, 4284, 7148, 1_1772, 143, 2_1297, 1064, 158, 122, 204, 3506, 1754, 1133, 1_4787, 1581, 115, 3_3224, 4482, 111, 1355, 110, 2_9173, 317, 5_0833, 108, 2_0147, 9_4665, 111, 7_7198, 107, 1], [110, 6_2613, 117, 638, 112, 1133, 121, 2_0098, 1355, 7_9050, 1_3872, 135, 1596, 5_3541, 1352, 141, 1_3039, 5542, 124, 302, 518, 111, 268, 2956, 115, 149, 4427, 107, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [139, 1235, 2799, 1_8289, 1_7780, 204, 109, 9474, 1296, 107, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], """attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]}  # noqa: E501
						# fmt: on

						self.tokenizer_integration_test_util(
						    expected_encoding=__A       ,   model_name="""google/bigbird-pegasus-large-arxiv"""       ,   revision="""ba85d0851d708441f91440d509690f1ab6353415"""       ,   )




@require_sentencepiece
@require_tokenizers
class 			_SCREAMING_SNAKE_CASE					(			A__	, unittest.TestCase							):
			UpperCAmelCase_  :Optional[Any]				 =						PegasusTokenizer
			UpperCAmelCase_  :Any				 =						PegasusTokenizerFast
			UpperCAmelCase_  :Optional[Any]				 =						True
			UpperCAmelCase_  :List[Any]				 =						True






			def  __lowerCAmelCase (					self       )			->     List[str]:
						super().setUp()

						# We have a SentencePiece fixture for testing
						lowerCAmelCase_    :Any			     =  PegasusTokenizer(__A       ,   offset=0       ,   mask_token_sent=__A       ,   mask_token="""[MASK]"""       )
						tokenizer.save_pretrained(self.tmpdirname       )






			@cached_property
			def  __lowerCAmelCase (					self       )			->     int:
						return PegasusTokenizer.from_pretrained("""google/bigbird-pegasus-large-arxiv"""       )






			def  __lowerCAmelCase (					self       ,   **__A       )			->     PegasusTokenizer:
						return PegasusTokenizer.from_pretrained(self.tmpdirname       ,   **__A       )






			def  __lowerCAmelCase (					self       ,   __A       )			->     Tuple:
						return ("This is a test", "This is a test")






			def  __lowerCAmelCase (					self       )			->     Union[str, Any]:
						lowerCAmelCase_    :Any			     =  self.rust_tokenizer_class.from_pretrained(self.tmpdirname       )
						lowerCAmelCase_    :Optional[Any]			     =  self.tokenizer_class.from_pretrained(self.tmpdirname       )
						lowerCAmelCase_    :int			     =  (
						    """Let's see which <unk> is the better <unk_token> one [MASK] It seems like this [MASK] was important </s>"""
						    """ <pad> <pad> <pad>"""
						)
						lowerCAmelCase_    :int			     =  rust_tokenizer([raw_input_str]       ,   return_tensors=__A       ,   add_special_tokens=__A       ).input_ids[0]
						lowerCAmelCase_    :List[Any]			     =  py_tokenizer([raw_input_str]       ,   return_tensors=__A       ,   add_special_tokens=__A       ).input_ids[0]
						self.assertListEqual(__A       ,   __A       )






			@require_torch
			def  __lowerCAmelCase (					self       )			->     Optional[Any]:
						lowerCAmelCase_    :Optional[Any]			     =  ["""This is going to be way too long.""" * 1000, """short example"""]
						lowerCAmelCase_    :List[str]			     =  ["""not super long but more than 5 tokens""", """tiny"""]
						lowerCAmelCase_    :Any			     =  self._large_tokenizer(__A       ,   padding=__A       ,   truncation=__A       ,   return_tensors="""pt"""       )
						lowerCAmelCase_    :Any			     =  self._large_tokenizer(
						    text_target=__A       ,   max_length=5       ,   padding=__A       ,   truncation=__A       ,   return_tensors="""pt"""       )

						assert batch.input_ids.shape == (2, 4096)
						assert batch.attention_mask.shape == (2, 4096)
						assert targets["input_ids"].shape == (2, 5)
						assert len(__A       ) == 2  # input_ids, attention_mask.






			def  __lowerCAmelCase (					self       )			->     List[str]:
						lowerCAmelCase_    :Dict			     =  (
						    """This is an example string that is used to test the original TF implementation against the HF"""
						    """ implementation"""
						)

						lowerCAmelCase_    :Any			     =  self._large_tokenizer(__A       ).input_ids

						self.assertListEqual(
						    __A       ,   [182, 117, 142, 587, 4211, 120, 117, 263, 112, 804, 109, 856, 2_5016, 3137, 464, 109, 2_6955, 3137, 1]       ,   )








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					:      str    ,     a					:      Optional[Any]    ,     a					:      int=13    ,     a					:      str=7    ,     a					:      str=True    ,     a					:      List[str]=True    ,     a					:      Optional[Any]=True    ,     a					:      int=True    ,     a					:      List[Any]=99    ,     a					:      List[Any]=32    ,     a					:      Tuple=5    ,     a					:      Any=4    ,     a					:      Optional[int]=37    ,     a					:      Tuple="gelu"    ,     a					:      Any=0.1    ,     a					:      int=0.1    ,     a					:      List[Any]=128    ,     a					:      Union[str, Any]=32    ,     a					:      Union[str, Any]=16    ,     a					:      Dict=2    ,     a					:      List[Any]=0.0_2    ,     a					:      Optional[Any]=3    ,     a					:      List[Any]=4    ,     a					:      Optional[int]=None    ,     ):






							'''simple docstring'''







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






				def 							_lowerCamelCase		(						self					:      Dict   ):






							'''simple docstring'''







							lowerCAmelCase__      :      int         =		ids_tensor([self.batch_size, self.seq_length]    ,     self.vocab_size   )

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

							lowerCAmelCase__      :      Tuple         =		None
							if self.use_token_type_ids:
										lowerCAmelCase__      :      List[Any]         =		ids_tensor([self.batch_size, self.seq_length]    ,     self.type_vocab_size   )

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

							lowerCAmelCase__      :      Dict         =		self.get_config()

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






				def 							_lowerCamelCase		(						self					:      List[Any]   ):






							'''simple docstring'''







							return NezhaConfig(
							    vocab_size=self.vocab_size    ,     hidden_size=self.hidden_size    ,     num_hidden_layers=self.num_hidden_layers    ,     num_attention_heads=self.num_attention_heads    ,     intermediate_size=self.intermediate_size    ,     hidden_act=self.hidden_act    ,     hidden_dropout_prob=self.hidden_dropout_prob    ,     attention_probs_dropout_prob=self.attention_probs_dropout_prob    ,     max_position_embeddings=self.max_position_embeddings    ,     type_vocab_size=self.type_vocab_size    ,     is_decoder=a    ,     initializer_range=self.initializer_range    ,     )






				def 							_lowerCamelCase		(						self					:      Optional[Any]   ):






							'''simple docstring'''







							(
							    (
							    lowerCAmelCase__
							)      ,				(
							    lowerCAmelCase__
							)      ,				(
							    lowerCAmelCase__
							)      ,				(
							    lowerCAmelCase__
							)      ,				(
							    lowerCAmelCase__
							)      ,				(
							    lowerCAmelCase__
							)      ,				(
							    lowerCAmelCase__
							)      ,				
							)      :      List[Any]         =		self.prepare_config_and_inputs()

							lowerCAmelCase__      :      List[Any]         =		True
							lowerCAmelCase__      :      Tuple         =		floats_tensor([self.batch_size, self.seq_length, self.hidden_size]   )
							lowerCAmelCase__      :      Tuple         =		ids_tensor([self.batch_size, self.seq_length]    ,     vocab_size=2   )

							return (
							    config,
							    input_ids,
							    token_type_ids,
							    input_mask,
							    sequence_labels,
							    token_labels,
							    choice_labels,
							    encoder_hidden_states,
							    encoder_attention_mask,
							)






				def 							_lowerCamelCase		(						self					:      Optional[Any]    ,     a					:      Optional[int]    ,     a					:      Tuple    ,     a					:      Optional[int]    ,     a					:      List[Any]    ,     a					:      Tuple    ,     a					:      List[str]    ,     a					:      Any   ):






							'''simple docstring'''







							lowerCAmelCase__      :      List[str]         =		NezhaModel(config=a   )
							model.to(a   )
							model.eval()
							lowerCAmelCase__      :      Dict         =		model(a    ,     attention_mask=a    ,     token_type_ids=a   )
							lowerCAmelCase__      :      List[str]         =		model(a    ,     token_type_ids=a   )
							lowerCAmelCase__      :      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 							_lowerCamelCase		(						self					:      List[Any]    ,     a					:      Union[str, Any]    ,     a					:      Dict    ,     a					:      List[Any]    ,     a					:      Optional[Any]    ,     a					:      int    ,     a					:      Tuple    ,     a					:      List[Any]    ,     a					:      Tuple    ,     a					:      List[str]    ,     ):






							'''simple docstring'''







							lowerCAmelCase__      :      Union[str, Any]         =		True
							lowerCAmelCase__      :      Optional[int]         =		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__      :      Dict         =		model(
							    a    ,     attention_mask=a    ,     token_type_ids=a    ,     encoder_hidden_states=a    ,     )
							lowerCAmelCase__      :      List[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 							_lowerCamelCase		(						self					:      Tuple    ,     a					:      Optional[Any]    ,     a					:      List[Any]    ,     a					:      str    ,     a					:      List[str]    ,     a					:      Tuple    ,     a					:      List[Any]    ,     a					:      Optional[Any]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      List[Any]         =		NezhaForMaskedLM(config=a   )
							model.to(a   )
							model.eval()
							lowerCAmelCase__      :      Dict         =		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					:      List[Any]    ,     a					:      Optional[int]    ,     a					:      List[Any]    ,     a					:      int    ,     a					:      List[str]    ,     a					:      Union[str, Any]    ,     a					:      int    ,     a					:      Any   ):






							'''simple docstring'''







							lowerCAmelCase__      :      List[Any]         =		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 							_lowerCamelCase		(						self					:      int    ,     a					:      Optional[int]    ,     a					:      str    ,     a					:      List[str]    ,     a					:      int    ,     a					:      Dict    ,     a					:      Optional[Any]    ,     a					:      Optional[Any]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Tuple         =		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 							_lowerCamelCase		(						self					:      Union[str, Any]    ,     a					:      Dict    ,     a					:      List[str]    ,     a					:      Any    ,     a					:      Any    ,     a					:      Union[str, Any]    ,     a					:      Tuple    ,     a					:      List[Any]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Optional[Any]         =		NezhaForQuestionAnswering(config=a   )
							model.to(a   )
							model.eval()
							lowerCAmelCase__      :      Tuple         =		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					:      Tuple    ,     a					:      str    ,     a					:      Union[str, Any]    ,     a					:      Tuple    ,     a					:      Optional[Any]    ,     a					:      Dict    ,     a					:      str    ,     a					:      int   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Any         =		self.num_labels
							lowerCAmelCase__      :      Optional[Any]         =		NezhaForSequenceClassification(a   )
							model.to(a   )
							model.eval()
							lowerCAmelCase__      :      Tuple         =		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					:      List[str]    ,     a					:      Dict    ,     a					:      str    ,     a					:      Optional[Any]    ,     a					:      Optional[int]    ,     a					:      List[str]    ,     a					:      Dict    ,     a					:      str   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Dict         =		self.num_labels
							lowerCAmelCase__      :      str         =		NezhaForTokenClassification(config=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.seq_length, self.num_labels)   )






				def 							_lowerCamelCase		(						self					:      int    ,     a					:      Tuple    ,     a					:      List[Any]    ,     a					:      Tuple    ,     a					:      List[Any]    ,     a					:      Optional[int]    ,     a					:      Optional[int]    ,     a					:      Optional[Any]   ):






							'''simple docstring'''







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






							'''simple docstring'''







							lowerCAmelCase__      :      int         =		self.prepare_config_and_inputs()
							(
							    (
							    lowerCAmelCase__
							)      ,				(
							    lowerCAmelCase__
							)      ,				(
							    lowerCAmelCase__
							)      ,				(
							    lowerCAmelCase__
							)      ,				(
							    lowerCAmelCase__
							)      ,				(
							    lowerCAmelCase__
							)      ,				(
							    lowerCAmelCase__
							)      ,				
							)      :      Any         =		config_and_inputs
							lowerCAmelCase__      :      str         =		{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
							return config, inputs_dict







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






				def 							_lowerCamelCase		(						self					:      str    ,     a					:      Tuple    ,     a					:      int    ,     a					:      Dict=False   ):






							'''simple docstring'''







							lowerCAmelCase__      :      int         =		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__      :      Optional[Any]         =		torch.zeros(
													    self.model_tester.batch_size    ,     dtype=torch.long    ,     device=a   )
							return inputs_dict






				def 							_lowerCamelCase		(						self					:      Union[str, Any]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Optional[int]         =		NezhaModelTester(self   )
							lowerCAmelCase__      :      Optional[int]         =		ConfigTester(self    ,     config_class=a    ,     hidden_size=37   )






				def 							_lowerCamelCase		(						self					:      Union[str, Any]   ):






							'''simple docstring'''







							self.config_tester.run_common_tests()






				def 							_lowerCamelCase		(						self					:      Optional[int]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      List[str]         =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*a   )






				def 							_lowerCamelCase		(						self					:      List[str]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      int         =		self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_model_as_decoder(*a   )






				def 							_lowerCamelCase		(						self					:      Optional[Any]   ):






							'''simple docstring'''







							(
							    (
							    lowerCAmelCase__
							)      ,				(
							    lowerCAmelCase__
							)      ,				(
							    lowerCAmelCase__
							)      ,				(
							    lowerCAmelCase__
							)      ,				(
							    lowerCAmelCase__
							)      ,				(
							    lowerCAmelCase__
							)      ,				(
							    lowerCAmelCase__
							)      ,				(
							    lowerCAmelCase__
							)      ,				(
							    lowerCAmelCase__
							)      ,				
							)      :      str         =		self.model_tester.prepare_config_and_inputs_for_decoder()

							lowerCAmelCase__      :      str         =		None

							self.model_tester.create_and_check_model_as_decoder(
							    a    ,     a    ,     a    ,     a    ,     a    ,     a    ,     a    ,     a    ,     a    ,     )






				def 							_lowerCamelCase		(						self					:      int   ):






							'''simple docstring'''







							lowerCAmelCase__      :      int         =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*a   )






				def 							_lowerCamelCase		(						self					:      List[Any]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Union[str, Any]         =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_multiple_choice(*a   )






				def 							_lowerCamelCase		(						self					:      int   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Optional[int]         =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_next_sequence_prediction(*a   )






				def 							_lowerCamelCase		(						self					:      Union[str, Any]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      List[str]         =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_pretraining(*a   )






				def 							_lowerCamelCase		(						self					:      int   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Optional[int]         =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*a   )






				def 							_lowerCamelCase		(						self					:      Optional[Any]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      int         =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*a   )






				def 							_lowerCamelCase		(						self					:      List[Any]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Any         =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*a   )






				@slow
				def 							_lowerCamelCase		(						self					:      Union[str, Any]   ):






							'''simple docstring'''







							for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										lowerCAmelCase__      :      Optional[Any]         =		NezhaModel.from_pretrained(a   )
										self.assertIsNotNone(a   )






				@slow
				@require_torch_gpu
				def 							_lowerCamelCase		(						self					:      Optional[int]   ):






							'''simple docstring'''







							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__      :      Dict         =		True
										lowerCAmelCase__      :      Any         =		model_class(config=a   )

										lowerCAmelCase__      :      Union[str, Any]         =		self._prepare_for_class(a    ,     a   )
										lowerCAmelCase__      :      int         =		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__      :      Any         =		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      A__							(     unittest.TestCase	):






				@slow
				def 							_lowerCamelCase		(						self					:      Optional[int]   ):






							'''simple docstring'''







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






							'''simple docstring'''







							lowerCAmelCase__      :      Any         =		NezhaForMaskedLM.from_pretrained('sijunhe/nezha-cn-base'   )
							lowerCAmelCase__      :      Optional[int]         =		torch.tensor([[0, 1, 2, 3, 4, 5]]   )
							lowerCAmelCase__      :      Optional[int]         =		torch.tensor([[1, 1, 1, 1, 1, 1]]   )
							with torch.no_grad():
										lowerCAmelCase__      :      Optional[int]         =		model(a    ,     attention_mask=a   )[0]
							lowerCAmelCase__      :      int         =		torch.Size((1, 6, 21_128)   )
							self.assertEqual(output.shape    ,     a   )
							lowerCAmelCase__      :      List[Any]         =		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   )   )