



'''simple docstring'''




import os
import tempfile
from functools import partial
from unittest import TestCase
from unittest.mock import patch

import numpy as np
import pytest

from datasets.arrow_dataset import Dataset
from datasets.search import ElasticSearchIndex, FaissIndex, MissingIndex

from .utils import require_elasticsearch, require_faiss


lowerCamelCase							:Any       = pytest.mark.integration







@require_faiss
class      _lowerCAmelCase							(      snake_case_			):


	def    _a     (self							):
				A_     :						Optional[Any]     =					Dataset.from_dict({"""filename""": ["""my_name-train""" + """_""" + str(lowercase							) for x in np.arange(30							).tolist()]}							)
				return dset


	def    _a     (self							):
				import faiss

				A_     :						Dataset     =					self._create_dummy_dataset()
				A_     :						Optional[Any]     =					dset.map(
				    lambda lowercase	,				lowercase							: {"vecs": i * np.ones(5	,				dtype=np.floataa							)}	,				with_indices=lowercase	,				keep_in_memory=lowercase							)
				A_     :						List[Any]     =					dset.add_faiss_index("""vecs"""	,				batch_size=100	,				metric_type=faiss.METRIC_INNER_PRODUCT							)
				A_     :						str     =					dset.get_nearest_examples("""vecs"""	,				np.ones(5	,				dtype=np.floataa							)							)
				self.assertEqual(examples["""filename"""][0]	,				"""my_name-train_29"""							)
				dset.drop_index("""vecs"""							)


	def    _a     (self							):
				import faiss

				A_     :						Dataset     =					self._create_dummy_dataset()
				dset.add_faiss_index_from_external_arrays(
				    external_arrays=np.ones((30, 5)							) * np.arange(30							).reshape(-1	,				1							)	,				index_name="""vecs"""	,				batch_size=100	,				metric_type=faiss.METRIC_INNER_PRODUCT	,				)
				A_     :						Union[str, Any]     =					dset.get_nearest_examples("""vecs"""	,				np.ones(5	,				dtype=np.floataa							)							)
				self.assertEqual(examples["""filename"""][0]	,				"""my_name-train_29"""							)


	def    _a     (self							):
				import faiss

				A_     :						Dataset     =					self._create_dummy_dataset()
				dset.add_faiss_index_from_external_arrays(
				    external_arrays=np.ones((30, 5)							) * np.arange(30							).reshape(-1	,				1							)	,				index_name="""vecs"""	,				metric_type=faiss.METRIC_INNER_PRODUCT	,				)

				# Setting delete=False and unlinking manually is not pretty... but it is required on Windows to
				# ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue.
				# see https://bugs.python.org/issue14243 and
				# https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515
				with tempfile.NamedTemporaryFile(delete=lowercase							) as tmp_file:
							dset.save_faiss_index("""vecs"""	,				tmp_file.name							)
							dset.load_faiss_index("""vecs2"""	,				tmp_file.name							)
				os.unlink(tmp_file.name							)

				A_     :						Tuple     =					dset.get_nearest_examples("""vecs2"""	,				np.ones(5	,				dtype=np.floataa							)							)
				self.assertEqual(examples["""filename"""][0]	,				"""my_name-train_29"""							)


	def    _a     (self							):
				A_     :						Dataset     =					self._create_dummy_dataset()
				dset.add_faiss_index_from_external_arrays(
				    external_arrays=np.ones((30, 5)							) * np.arange(30							).reshape(-1	,				1							)	,				index_name="""vecs"""							)
				dset.drop_index("""vecs"""							)
				self.assertRaises(lowercase	,				partial(dset.get_nearest_examples	,				"""vecs2"""	,				np.ones(5	,				dtype=np.floataa							)							)							)


	def    _a     (self							):
				from elasticsearch import Elasticsearch

				A_     :						Dataset     =					self._create_dummy_dataset()
				with patch("""elasticsearch.Elasticsearch.search"""							) as mocked_search, patch(
				    """elasticsearch.client.IndicesClient.create"""							) as mocked_index_create, patch("""elasticsearch.helpers.streaming_bulk"""							) as mocked_bulk:
							A_     :						Any     =					{'acknowledged': True}
							mocked_bulk.return_value([(True, None)] * 30							)
							A_     :						Tuple     =					{'hits': {'hits': [{'_score': 1, '_id': 29}]}}
							A_     :						List[Any]     =					Elasticsearch()

							dset.add_elasticsearch_index("""filename"""	,				es_client=lowercase							)
							A_     :						int     =					dset.get_nearest_examples("""filename"""	,				"""my_name-train_29"""							)
							self.assertEqual(examples["""filename"""][0]	,				"""my_name-train_29"""							)







@require_faiss
class      _lowerCAmelCase							(      snake_case_			):


	def    _a     (self							):
				import faiss

				A_     :						Any     =					FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT							)

				# add vectors
				index.add_vectors(np.eye(5	,				dtype=np.floataa							)							)
				self.assertIsNotNone(index.faiss_index							)
				self.assertEqual(index.faiss_index.ntotal	,				5							)
				index.add_vectors(np.zeros((5, 5)	,				dtype=np.floataa							)							)
				self.assertEqual(index.faiss_index.ntotal	,				10							)

				# single query
				A_     :						List[str]     =					np.zeros(5	,				dtype=np.floataa							)
				A_     :						int     =					1
				A_     :						Union[str, Any]     =					index.search(lowercase							)
				self.assertRaises(lowercase	,				index.search	,				query.reshape(-1	,				1							)							)
				self.assertGreater(scores[0]	,				0							)
				self.assertEqual(indices[0]	,				1							)

				# batched queries
				A_     :						Any     =					np.eye(5	,				dtype=np.floataa							)[::-1]
				A_     :						Optional[Any]     =					index.search_batch(lowercase							)
				self.assertRaises(lowercase	,				index.search_batch	,				queries[0]							)
				A_     :						Optional[int]     =					[scores[0] for scores in total_scores]
				A_     :						Union[str, Any]     =					[indices[0] for indices in total_indices]
				self.assertGreater(np.min(lowercase							)	,				0							)
				self.assertListEqual([4, 3, 2, 1, 0]	,				lowercase							)


	def    _a     (self							):
				import faiss

				A_     :						Tuple     =					FaissIndex(string_factory="""Flat"""							)
				index.add_vectors(np.eye(5	,				dtype=np.floataa							)							)
				self.assertIsInstance(index.faiss_index	,				faiss.IndexFlat							)
				A_     :						int     =					FaissIndex(string_factory="""LSH"""							)
				index.add_vectors(np.eye(5	,				dtype=np.floataa							)							)
				self.assertIsInstance(index.faiss_index	,				faiss.IndexLSH							)
				with self.assertRaises(lowercase							):
							A_     :						Union[str, Any]     =					FaissIndex(string_factory="""Flat"""	,				custom_index=faiss.IndexFlat(5							)							)


	def    _a     (self							):
				import faiss

				A_     :						Any     =					faiss.IndexFlat(5							)
				A_     :						int     =					FaissIndex(custom_index=lowercase							)
				index.add_vectors(np.eye(5	,				dtype=np.floataa							)							)
				self.assertIsInstance(index.faiss_index	,				faiss.IndexFlat							)


	def    _a     (self							):
				import faiss

				A_     :						str     =					FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT							)
				index.add_vectors(np.eye(5	,				dtype=np.floataa							)							)

				# Setting delete=False and unlinking manually is not pretty... but it is required on Windows to
				# ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue.
				# see https://bugs.python.org/issue14243 and
				# https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515
				with tempfile.NamedTemporaryFile(delete=lowercase							) as tmp_file:
							index.save(tmp_file.name							)
							A_     :						str     =					FaissIndex.load(tmp_file.name							)
				os.unlink(tmp_file.name							)

				A_     :						int     =					np.zeros(5	,				dtype=np.floataa							)
				A_     :						Tuple     =					1
				A_     :						Any     =					index.search(lowercase							)
				self.assertGreater(scores[0]	,				0							)
				self.assertEqual(indices[0]	,				1							)






@require_faiss
def      a      (			lowerCamelCase__   ):
			'''simple docstring'''





			import faiss

			A_     :						List[Any]     =					FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT   )
			index.add_vectors(np.eye(5						,				dtype=np.floataa   )   )

			A_     :						List[str]     =					'index.faiss'
			A_     :						Tuple     =					f'mock://{index_name}'
			index.save(lowerCamelCase__						,				storage_options=mockfs.storage_options   )
			A_     :						List[str]     =					FaissIndex.load(lowerCamelCase__						,				storage_options=mockfs.storage_options   )

			A_     :						List[Any]     =					np.zeros(5						,				dtype=np.floataa   )
			A_     :						List[str]     =					1
			A_     :						int     =					index.search(lowerCamelCase__   )
			assert scores[0] > 0
			assert indices[0] == 1







@require_elasticsearch
class      _lowerCAmelCase							(      snake_case_			):


	def    _a     (self							):
				from elasticsearch import Elasticsearch

				with patch("""elasticsearch.Elasticsearch.search"""							) as mocked_search, patch(
				    """elasticsearch.client.IndicesClient.create"""							) as mocked_index_create, patch("""elasticsearch.helpers.streaming_bulk"""							) as mocked_bulk:
							A_     :						str     =					Elasticsearch()
							A_     :						List[str]     =					{'acknowledged': True}
							A_     :						Optional[Any]     =					ElasticSearchIndex(es_client=lowercase							)
							mocked_bulk.return_value([(True, None)] * 3							)
							index.add_documents(["""foo""", """bar""", """foobar"""]							)

							# single query
							A_     :						int     =					'foo'
							A_     :						int     =					{'hits': {'hits': [{'_score': 1, '_id': 0}]}}
							A_     :						Union[str, Any]     =					index.search(lowercase							)
							self.assertEqual(scores[0]	,				1							)
							self.assertEqual(indices[0]	,				0							)

							# single query with timeout
							A_     :						List[Any]     =					'foo'
							A_     :						Dict     =					{'hits': {'hits': [{'_score': 1, '_id': 0}]}}
							A_     :						Union[str, Any]     =					index.search(lowercase	,				request_timeout=30							)
							self.assertEqual(scores[0]	,				1							)
							self.assertEqual(indices[0]	,				0							)

							# batched queries
							A_     :						List[Any]     =					['foo', 'bar', 'foobar']
							A_     :						Optional[int]     =					{'hits': {'hits': [{'_score': 1, '_id': 1}]}}
							A_     :						Union[str, Any]     =					index.search_batch(lowercase							)
							A_     :						Optional[Any]     =					[scores[0] for scores in total_scores]
							A_     :						str     =					[indices[0] for indices in total_indices]
							self.assertGreater(np.min(lowercase							)	,				0							)
							self.assertListEqual([1, 1, 1]	,				lowercase							)

							# batched queries with timeout
							A_     :						Any     =					['foo', 'bar', 'foobar']
							A_     :						Tuple     =					{'hits': {'hits': [{'_score': 1, '_id': 1}]}}
							A_     :						Optional[int]     =					index.search_batch(lowercase	,				request_timeout=30							)
							A_     :						Optional[Any]     =					[scores[0] for scores in total_scores]
							A_     :						Optional[Any]     =					[indices[0] for indices in total_indices]
							self.assertGreater(np.min(lowercase							)	,				0							)
							self.assertListEqual([1, 1, 1]	,				lowercase							)




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


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


	def    _a     (self							):
				A_     :						List[str]     =					ids_tensor([self.batch_size, self.seq_length]	,				self.vocab_size							)

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

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

				A_     :						Dict     =					None
				A_     :						Any     =					None
				A_     :						List[Any]     =					None
				if self.use_labels:
							A_     :						int     =					ids_tensor([self.batch_size]	,				self.type_sequence_label_size							)
							A_     :						int     =					ids_tensor([self.batch_size, self.seq_length]	,				self.num_labels							)
							A_     :						int     =					ids_tensor([self.batch_size]	,				self.num_choices							)

				A_     :						Any     =					self.get_config()

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


	def    _a     (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=lowercase	,				initializer_range=self.initializer_range	,				)


	def    _a     (self							):
				(
				    (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   
				)     :						Union[str, Any]     =					self.prepare_config_and_inputs()

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


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase							):
				A_     :						Tuple     =					NezhaForNextSentencePrediction(config=lowercase							)
				model.to(lowercase							)
				model.eval()
				A_     :						Union[str, Any]     =					model(
				    lowercase	,				attention_mask=lowercase	,				token_type_ids=lowercase	,				labels=lowercase	,				)
				self.parent.assertEqual(result.logits.shape	,				(self.batch_size, 2)							)


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


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase							):
				A_     :						List[str]     =					self.num_labels
				A_     :						Optional[int]     =					NezhaForTokenClassification(config=lowercase							)
				model.to(lowercase							)
				model.eval()
				A_     :						int     =					model(lowercase	,				attention_mask=lowercase	,				token_type_ids=lowercase	,				labels=lowercase							)
				self.parent.assertEqual(result.logits.shape	,				(self.batch_size, self.seq_length, self.num_labels)							)


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


	def    _a     (self							):
				A_     :						Tuple     =					self.prepare_config_and_inputs()
				(
				    (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   
				)     :						int     =					config_and_inputs
				A_     :						Any     =					{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
				return config, inputs_dict







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


	def    _a     (self	,				lowercase	,				lowercase	,				lowercase=False							):
				A_     :						Optional[Any]     =					super()._prepare_for_class(lowercase	,				lowercase	,				return_labels=lowercase							)

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


	def    _a     (self							):
				A_     :						Optional[int]     =					NezhaModelTester(self							)
				A_     :						Any     =					ConfigTester(self	,				config_class=lowercase	,				hidden_size=37							)


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


	def    _a     (self							):
				A_     :						Any     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*lowercase							)


	def    _a     (self							):
				A_     :						str     =					self.model_tester.prepare_config_and_inputs_for_decoder()
				self.model_tester.create_and_check_model_as_decoder(*lowercase							)


	def    _a     (self							):
				# This regression test was failing with PyTorch < 1.3
				(
				    (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   (
				    A_
				),   
				)     :						List[Any]     =					self.model_tester.prepare_config_and_inputs_for_decoder()

				A_     :						str     =					None

				self.model_tester.create_and_check_model_as_decoder(
				    lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				lowercase	,				)


	def    _a     (self							):
				A_     :						str     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_masked_lm(*lowercase							)


	def    _a     (self							):
				A_     :						Any     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_multiple_choice(*lowercase							)


	def    _a     (self							):
				A_     :						Union[str, Any]     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_next_sequence_prediction(*lowercase							)


	def    _a     (self							):
				A_     :						Any     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_pretraining(*lowercase							)


	def    _a     (self							):
				A_     :						Tuple     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_question_answering(*lowercase							)


	def    _a     (self							):
				A_     :						List[str]     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_sequence_classification(*lowercase							)


	def    _a     (self							):
				A_     :						Any     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_token_classification(*lowercase							)


	@slow
	def    _a     (self							):
				for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							A_     :						Any     =					NezhaModel.from_pretrained(lowercase							)
							self.assertIsNotNone(lowercase							)


	@slow
	@require_torch_gpu
	def    _a     (self							):
				A_,   A_     :						Tuple     =					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

							A_     :						Optional[int]     =					True
							A_     :						str     =					model_class(config=lowercase							)

							A_     :						str     =					self._prepare_for_class(lowercase	,				lowercase							)
							A_     :						Tuple     =					torch.jit.trace(
							    lowercase	,				(inputs_dict["""input_ids"""].to("""cpu"""							), inputs_dict["""attention_mask"""].to("""cpu"""							))							)

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







@require_torch
class      _lowerCAmelCase							(      unittest.TestCase			):


	@slow
	def    _a     (self							):
				A_     :						Dict     =					NezhaModel.from_pretrained("""sijunhe/nezha-cn-base"""							)
				A_     :						List[Any]     =					torch.tensor([[0, 1, 2, 3, 4, 5]]							)
				A_     :						List[str]     =					torch.tensor([[0, 1, 1, 1, 1, 1]]							)
				with torch.no_grad():
							A_     :						Optional[int]     =					model(lowercase	,				attention_mask=lowercase							)[0]
				A_     :						Optional[int]     =					torch.Size((1, 6, 768)							)
				self.assertEqual(output.shape	,				lowercase							)
				A_     :						List[Any]     =					torch.tensor([[[0.06_85, 0.24_41, 0.11_02], [0.06_00, 0.19_06, 0.13_49], [0.02_21, 0.08_19, 0.05_86]]]							)

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


	@slow
	def    _a     (self							):
				A_     :						str     =					NezhaForMaskedLM.from_pretrained("""sijunhe/nezha-cn-base"""							)
				A_     :						Union[str, Any]     =					torch.tensor([[0, 1, 2, 3, 4, 5]]							)
				A_     :						str     =					torch.tensor([[1, 1, 1, 1, 1, 1]]							)
				with torch.no_grad():
							A_     :						Tuple     =					model(lowercase	,				attention_mask=lowercase							)[0]
				A_     :						str     =					torch.Size((1, 6, 21128)							)
				self.assertEqual(output.shape	,				lowercase							)
				A_     :						List[Any]     =					torch.tensor(
				    [[-2.79_39, -1.79_02, -2.21_89], [-2.85_85, -1.89_08, -2.37_23], [-2.64_99, -1.77_50, -2.25_58]]							)

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