



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


           '''simple docstring'''

           __a						       =				parent
           __a						       =				batch_size
           __a						       =				seq_length
           __a						       =				is_training
           __a						       =				use_input_mask
           __a						       =				use_token_type_ids
           __a						       =				use_labels
           __a						       =				vocab_size
           __a						       =				hidden_size
           __a						       =				num_hidden_layers
           __a						       =				num_attention_heads
           __a						       =				intermediate_size
           __a						       =				hidden_act
           __a						       =				hidden_dropout_prob
           __a						       =				attention_probs_dropout_prob
           __a						       =				max_position_embeddings
           __a						       =				type_vocab_size
           __a						       =				type_sequence_label_size
           __a						       =				initializer_range
           __a						       =				num_labels
           __a						       =				num_choices
           __a						       =				scope



       def 		_lowerCamelCase			(		self							:     int):


           '''simple docstring'''

           __a						       =				ids_tensor([self.batch_size, self.seq_length]     ,		self.vocab_size)

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

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

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

           __a						       =				self.get_config()

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



       def 		_lowerCamelCase			(		self							:     Optional[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=__SCREAMING_SNAKE_CASE     ,		initializer_range=self.initializer_range     ,		)



       def 		_lowerCamelCase			(		self							:     Tuple):


           '''simple docstring'''

           (
               (
               __a
           )       ,   (
               __a
           )       ,   (
               __a
           )       ,   (
               __a
           )       ,   (
               __a
           )       ,   (
               __a
           )       ,   (
               __a
           )       ,   
           )						       =				self.prepare_config_and_inputs()

           __a						       =				True
           __a						       =				floats_tensor([self.batch_size, self.seq_length, self.hidden_size])
           __a						       =				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							:     str     ,		__SCREAMING_SNAKE_CASE							:     Any     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]     ,		__SCREAMING_SNAKE_CASE							:     Optional[Any]     ,		__SCREAMING_SNAKE_CASE							:     str     ,		__SCREAMING_SNAKE_CASE							:     Any     ,		__SCREAMING_SNAKE_CASE							:     int     ,		__SCREAMING_SNAKE_CASE							:     int):


           '''simple docstring'''

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


           '''simple docstring'''

           __a						       =				True
           __a						       =				NezhaModel(__SCREAMING_SNAKE_CASE)
           model.to(__SCREAMING_SNAKE_CASE)
           model.eval()
           __a						       =				model(
               __SCREAMING_SNAKE_CASE     ,		attention_mask=__SCREAMING_SNAKE_CASE     ,		token_type_ids=__SCREAMING_SNAKE_CASE     ,		encoder_hidden_states=__SCREAMING_SNAKE_CASE     ,		encoder_attention_mask=__SCREAMING_SNAKE_CASE     ,		)
           __a						       =				model(
               __SCREAMING_SNAKE_CASE     ,		attention_mask=__SCREAMING_SNAKE_CASE     ,		token_type_ids=__SCREAMING_SNAKE_CASE     ,		encoder_hidden_states=__SCREAMING_SNAKE_CASE     ,		)
           __a						       =				model(__SCREAMING_SNAKE_CASE     ,		attention_mask=__SCREAMING_SNAKE_CASE     ,		token_type_ids=__SCREAMING_SNAKE_CASE)
           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							:     int     ,		__SCREAMING_SNAKE_CASE							:     int     ,		__SCREAMING_SNAKE_CASE							:     List[Any]     ,		__SCREAMING_SNAKE_CASE							:     Union[str, Any]     ,		__SCREAMING_SNAKE_CASE							:     Tuple     ,		__SCREAMING_SNAKE_CASE							:     Union[str, Any]     ,		__SCREAMING_SNAKE_CASE							:     Dict     ,		__SCREAMING_SNAKE_CASE							:     List[Any]):


           '''simple docstring'''

           __a						       =				NezhaForMaskedLM(config=__SCREAMING_SNAKE_CASE)
           model.to(__SCREAMING_SNAKE_CASE)
           model.eval()
           __a						       =				model(__SCREAMING_SNAKE_CASE     ,		attention_mask=__SCREAMING_SNAKE_CASE     ,		token_type_ids=__SCREAMING_SNAKE_CASE     ,		labels=__SCREAMING_SNAKE_CASE)
           self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.seq_length, self.vocab_size))



       def 		_lowerCamelCase			(		self							:     Optional[Any]     ,		__SCREAMING_SNAKE_CASE							:     Dict     ,		__SCREAMING_SNAKE_CASE							:     str     ,		__SCREAMING_SNAKE_CASE							:     str     ,		__SCREAMING_SNAKE_CASE							:     Tuple     ,		__SCREAMING_SNAKE_CASE							:     List[Any]     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]     ,		__SCREAMING_SNAKE_CASE							:     Union[str, Any]):


           '''simple docstring'''

           __a						       =				NezhaForNextSentencePrediction(config=__SCREAMING_SNAKE_CASE)
           model.to(__SCREAMING_SNAKE_CASE)
           model.eval()
           __a						       =				model(
               __SCREAMING_SNAKE_CASE     ,		attention_mask=__SCREAMING_SNAKE_CASE     ,		token_type_ids=__SCREAMING_SNAKE_CASE     ,		labels=__SCREAMING_SNAKE_CASE     ,		)
           self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, 2))



       def 		_lowerCamelCase			(		self							:     str     ,		__SCREAMING_SNAKE_CASE							:     Union[str, Any]     ,		__SCREAMING_SNAKE_CASE							:     Tuple     ,		__SCREAMING_SNAKE_CASE							:     int     ,		__SCREAMING_SNAKE_CASE							:     List[str]     ,		__SCREAMING_SNAKE_CASE							:     Any     ,		__SCREAMING_SNAKE_CASE							:     Union[str, Any]     ,		__SCREAMING_SNAKE_CASE							:     str):


           '''simple docstring'''

           __a						       =				NezhaForPreTraining(config=__SCREAMING_SNAKE_CASE)
           model.to(__SCREAMING_SNAKE_CASE)
           model.eval()
           __a						       =				model(
               __SCREAMING_SNAKE_CASE     ,		attention_mask=__SCREAMING_SNAKE_CASE     ,		token_type_ids=__SCREAMING_SNAKE_CASE     ,		labels=__SCREAMING_SNAKE_CASE     ,		next_sentence_label=__SCREAMING_SNAKE_CASE     ,		)
           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							:     Tuple     ,		__SCREAMING_SNAKE_CASE							:     str     ,		__SCREAMING_SNAKE_CASE							:     str     ,		__SCREAMING_SNAKE_CASE							:     List[Any]     ,		__SCREAMING_SNAKE_CASE							:     Optional[Any]     ,		__SCREAMING_SNAKE_CASE							:     str     ,		__SCREAMING_SNAKE_CASE							:     Any     ,		__SCREAMING_SNAKE_CASE							:     Union[str, Any]):


           '''simple docstring'''

           __a						       =				NezhaForQuestionAnswering(config=__SCREAMING_SNAKE_CASE)
           model.to(__SCREAMING_SNAKE_CASE)
           model.eval()
           __a						       =				model(
               __SCREAMING_SNAKE_CASE     ,		attention_mask=__SCREAMING_SNAKE_CASE     ,		token_type_ids=__SCREAMING_SNAKE_CASE     ,		start_positions=__SCREAMING_SNAKE_CASE     ,		end_positions=__SCREAMING_SNAKE_CASE     ,		)
           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							:     List[Any]     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]     ,		__SCREAMING_SNAKE_CASE							:     Optional[Any]     ,		__SCREAMING_SNAKE_CASE							:     Any     ,		__SCREAMING_SNAKE_CASE							:     Tuple     ,		__SCREAMING_SNAKE_CASE							:     Union[str, Any]     ,		__SCREAMING_SNAKE_CASE							:     Any     ,		__SCREAMING_SNAKE_CASE							:     Optional[Any]):


           '''simple docstring'''

           __a						       =				self.num_labels
           __a						       =				NezhaForSequenceClassification(__SCREAMING_SNAKE_CASE)
           model.to(__SCREAMING_SNAKE_CASE)
           model.eval()
           __a						       =				model(__SCREAMING_SNAKE_CASE     ,		attention_mask=__SCREAMING_SNAKE_CASE     ,		token_type_ids=__SCREAMING_SNAKE_CASE     ,		labels=__SCREAMING_SNAKE_CASE)
           self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.num_labels))



       def 		_lowerCamelCase			(		self							:     str     ,		__SCREAMING_SNAKE_CASE							:     Optional[Any]     ,		__SCREAMING_SNAKE_CASE							:     Union[str, Any]     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]     ,		__SCREAMING_SNAKE_CASE							:     Dict     ,		__SCREAMING_SNAKE_CASE							:     Optional[Any]     ,		__SCREAMING_SNAKE_CASE							:     str     ,		__SCREAMING_SNAKE_CASE							:     List[Any]):


           '''simple docstring'''

           __a						       =				self.num_labels
           __a						       =				NezhaForTokenClassification(config=__SCREAMING_SNAKE_CASE)
           model.to(__SCREAMING_SNAKE_CASE)
           model.eval()
           __a						       =				model(__SCREAMING_SNAKE_CASE     ,		attention_mask=__SCREAMING_SNAKE_CASE     ,		token_type_ids=__SCREAMING_SNAKE_CASE     ,		labels=__SCREAMING_SNAKE_CASE)
           self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.seq_length, self.num_labels))



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


           '''simple docstring'''

           __a						       =				self.num_choices
           __a						       =				NezhaForMultipleChoice(config=__SCREAMING_SNAKE_CASE)
           model.to(__SCREAMING_SNAKE_CASE)
           model.eval()
           __a						       =				input_ids.unsqueeze(1).expand(-1     ,		self.num_choices     ,		-1).contiguous()
           __a						       =				token_type_ids.unsqueeze(1).expand(-1     ,		self.num_choices     ,		-1).contiguous()
           __a						       =				input_mask.unsqueeze(1).expand(-1     ,		self.num_choices     ,		-1).contiguous()
           __a						       =				model(
               __SCREAMING_SNAKE_CASE     ,		attention_mask=__SCREAMING_SNAKE_CASE     ,		token_type_ids=__SCREAMING_SNAKE_CASE     ,		labels=__SCREAMING_SNAKE_CASE     ,		)
           self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.num_choices))





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


           '''simple docstring'''

           __a						       =				self.prepare_config_and_inputs()
           (
               (
               __a
           )       ,   (
               __a
           )       ,   (
               __a
           )       ,   (
               __a
           )       ,   (
               __a
           )       ,   (
               __a
           )       ,   (
               __a
           )       ,   
           )						       =				config_and_inputs
           __a						       =				{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
           return config, inputs_dict


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



       def 		_lowerCamelCase			(		self							:     Dict     ,		__SCREAMING_SNAKE_CASE							:     List[Any]     ,		__SCREAMING_SNAKE_CASE							:     Any     ,		__SCREAMING_SNAKE_CASE							:     List[str]=False):


           '''simple docstring'''

           __a						       =				super()._prepare_for_class(__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		return_labels=__SCREAMING_SNAKE_CASE)

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



       def 		_lowerCamelCase			(		self							:     List[Any]):


           '''simple docstring'''

           __a						       =				NezhaModelTester(self)
           __a						       =				ConfigTester(self     ,		config_class=__SCREAMING_SNAKE_CASE     ,		hidden_size=37)



       def 		_lowerCamelCase			(		self							:     List[str]):


           '''simple docstring'''

           self.config_tester.run_common_tests()



       def 		_lowerCamelCase			(		self							:     Optional[int]):


           '''simple docstring'''

           __a						       =				self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE)



       def 		_lowerCamelCase			(		self							:     Tuple):


           '''simple docstring'''

           __a						       =				self.model_tester.prepare_config_and_inputs_for_decoder()
           self.model_tester.create_and_check_model_as_decoder(*__SCREAMING_SNAKE_CASE)



       def 		_lowerCamelCase			(		self							:     int):


           '''simple docstring'''

           (
               (
               __a
           )       ,   (
               __a
           )       ,   (
               __a
           )       ,   (
               __a
           )       ,   (
               __a
           )       ,   (
               __a
           )       ,   (
               __a
           )       ,   (
               __a
           )       ,   (
               __a
           )       ,   
           )						       =				self.model_tester.prepare_config_and_inputs_for_decoder()

           __a						       =				None

           self.model_tester.create_and_check_model_as_decoder(
               __SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		)



       def 		_lowerCamelCase			(		self							:     List[Any]):


           '''simple docstring'''

           __a						       =				self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_lm(*__SCREAMING_SNAKE_CASE)



       def 		_lowerCamelCase			(		self							:     str):


           '''simple docstring'''

           __a						       =				self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_multiple_choice(*__SCREAMING_SNAKE_CASE)



       def 		_lowerCamelCase			(		self							:     Dict):


           '''simple docstring'''

           __a						       =				self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_next_sequence_prediction(*__SCREAMING_SNAKE_CASE)



       def 		_lowerCamelCase			(		self							:     Any):


           '''simple docstring'''

           __a						       =				self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_pretraining(*__SCREAMING_SNAKE_CASE)



       def 		_lowerCamelCase			(		self							:     str):


           '''simple docstring'''

           __a						       =				self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(*__SCREAMING_SNAKE_CASE)



       def 		_lowerCamelCase			(		self							:     List[str]):


           '''simple docstring'''

           __a						       =				self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_sequence_classification(*__SCREAMING_SNAKE_CASE)



       def 		_lowerCamelCase			(		self							:     List[str]):


           '''simple docstring'''

           __a						       =				self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(*__SCREAMING_SNAKE_CASE)



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


           '''simple docstring'''

           for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               __a						       =				NezhaModel.from_pretrained(__SCREAMING_SNAKE_CASE)
               self.assertIsNotNone(__SCREAMING_SNAKE_CASE)





       @slow
       @require_torch_gpu
       def 		_lowerCamelCase			(		self							:     str):


           '''simple docstring'''

           __a       ,   __a						       =				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						       =				True
               __a						       =				model_class(config=__SCREAMING_SNAKE_CASE)

               __a						       =				self._prepare_for_class(__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE)
               __a						       =				torch.jit.trace(
                   __SCREAMING_SNAKE_CASE     ,		(inputs_dict['''input_ids'''].to('''cpu'''), inputs_dict['''attention_mask'''].to('''cpu''')))

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


@require_torch
class     _A       (	unittest.TestCase    ):



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


           '''simple docstring'''

           __a						       =				NezhaModel.from_pretrained('''sijunhe/nezha-cn-base''')
           __a						       =				torch.tensor([[0, 1, 2, 3, 4, 5]])
           __a						       =				torch.tensor([[0, 1, 1, 1, 1, 1]])
           with torch.no_grad():
               __a						       =				model(__SCREAMING_SNAKE_CASE     ,		attention_mask=__SCREAMING_SNAKE_CASE)[0]
           __a						       =				torch.Size((1, 6, 768))
           self.assertEqual(output.shape     ,		__SCREAMING_SNAKE_CASE)
           __a						       =				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]     ,		__SCREAMING_SNAKE_CASE     ,		atol=1E-4))





       @slow
       def 		_lowerCamelCase			(		self							:     str):


           '''simple docstring'''

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










from __future__ import annotations

import random

# Maximum size of the population.  Bigger could be faster but is more memory expensive.
__snake_case						:Optional[int]      		=     200
# Number of elements selected in every generation of evolution. The selection takes
# place from best to worst of that generation and must be smaller than N_POPULATION.
__snake_case						:List[str]      		=     50
# Probability that an element of a generation can mutate, changing one of its genes.
# This will guarantee that all genes will be used during evolution.
__snake_case						:List[Any]      		=     0.4
# Just a seed to improve randomness required by the algorithm.
random.seed(random.randint(0, 1000))





def      __snake_case	(			_UpperCAmelCase   , _UpperCAmelCase				):
    __a						       =				len([g for position, g in enumerate(_UpperCAmelCase				) if g == main_target[position]]				)
    return (item, float(_UpperCAmelCase				))





def      __snake_case	(			_UpperCAmelCase   , _UpperCAmelCase				):
    __a						       =				random.randint(0   , len(_UpperCAmelCase				) - 1				)
    __a						       =				parent_a[:random_slice] + parent_a[random_slice:]
    __a						       =				parent_a[:random_slice] + parent_a[random_slice:]
    return (child_a, child_a)





def      __snake_case	(			_UpperCAmelCase   , _UpperCAmelCase				):
    __a						       =				list(_UpperCAmelCase				)
    if random.uniform(0   , 1				) < MUTATION_PROBABILITY:
        __a						       =				random.choice(_UpperCAmelCase				)
    return "".join(_UpperCAmelCase				)





def      __snake_case	(			_UpperCAmelCase   , _UpperCAmelCase   , _UpperCAmelCase   , ):
    __a						       =				[]
    # Generate more children proportionally to the fitness score.
    __a						       =				int(parent_a[1] * 100				) + 1
    __a						       =				10 if child_n >= 10 else child_n
    for _ in range(_UpperCAmelCase				):
        __a						       =				population_score[random.randint(0   , _UpperCAmelCase				)][0]

        __a       ,   __a						       =				crossover(parent_a[0]   , _UpperCAmelCase				)
        # Append new string to the population list.
        pop.append(mutate(_UpperCAmelCase   , _UpperCAmelCase				)				)
        pop.append(mutate(_UpperCAmelCase   , _UpperCAmelCase				)				)
    return pop





def      __snake_case	(			_UpperCAmelCase   , _UpperCAmelCase   , _UpperCAmelCase = True				):

    # Verify if N_POPULATION is bigger than N_SELECTED
    if N_POPULATION < N_SELECTED:
        __a						       =				f'{N_POPULATION} must be bigger than {N_SELECTED}'
        raise ValueError(_UpperCAmelCase				)
    # Verify that the target contains no genes besides the ones inside genes variable.
    __a						       =				sorted({c for c in target if c not in genes}				)
    if not_in_genes_list:
        __a						       =				f'{not_in_genes_list} is not in genes list, evolution cannot converge'
        raise ValueError(_UpperCAmelCase				)

    # Generate random starting population.
    __a						       =				[]
    for _ in range(_UpperCAmelCase				):
        population.append(''''''.join([random.choice(_UpperCAmelCase				) for i in range(len(_UpperCAmelCase				)				)]				)				)

    # Just some logs to know what the algorithms is doing.
    __a       ,   __a						       =				0, 0

    # This loop will end when we find a perfect match for our target.
    while True:
        generation += 1
        total_population += len(_UpperCAmelCase				)

        # Random population created. Now it's time to evaluate.

        # Adding a bit of concurrency can make everything faster,
        #
        # import concurrent.futures
        # population_score: list[tuple[str, float]] = []
        # with concurrent.futures.ThreadPoolExecutor(
        #                                   max_workers=NUM_WORKERS) as executor:
        #     futures = {executor.submit(evaluate, item) for item in population}
        #     concurrent.futures.wait(futures)
        #     population_score = [item.result() for item in futures]
        #
        # but with a simple algorithm like this, it will probably be slower.
        # We just need to call evaluate for every item inside the population.
        __a						       =				[evaluate(_UpperCAmelCase   , _UpperCAmelCase				) for item in population]

        # Check if there is a matching evolution.
        __a						       =				sorted(_UpperCAmelCase   , key=lambda _UpperCAmelCase				: x[1]   , reverse=_UpperCAmelCase				)
        if population_score[0][0] == target:
            return (generation, total_population, population_score[0][0])

        # Print the best result every 10 generation.
        # Just to know that the algorithm is working.
        if debug and generation % 10 == 0:
            print(
                f'\nGeneration: {generation}'
                f'\nTotal Population:{total_population}'
                f'\nBest score: {population_score[0][1]}'
                f'\nBest string: {population_score[0][0]}'				)

        # Flush the old population, keeping some of the best evolutions.
        # Keeping this avoid regression of evolution.
        __a						       =				population[: int(N_POPULATION / 3				)]
        population.clear()
        population.extend(_UpperCAmelCase				)
        # Normalize population score to be between 0 and 1.
        __a						       =				[
            (item, score / len(_UpperCAmelCase				)) for item, score in population_score
        ]

        # This is selection
        for i in range(_UpperCAmelCase				):
            population.extend(select(population_score[int(_UpperCAmelCase				)]   , _UpperCAmelCase   , _UpperCAmelCase				)				)
            # Check if the population has already reached the maximum value and if so,
            # break the cycle.  If this check is disabled, the algorithm will take
            # forever to compute large strings, but will also calculate small strings in
            # a far fewer generations.
            if len(_UpperCAmelCase				) > N_POPULATION:
                break


if __name__ == "__main__":
 __snake_case						:Optional[int]      		=     (
     '''This is a genetic algorithm to evaluate, combine, evolve, and mutate a string!'''
 )
 __snake_case						:List[Any]      		=     list(
     ''' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm'''
     '''nopqrstuvwxyz.,;!?+-*#@^\'èéòà€ù=)(&%$£/\\'''
 )
 __snake_case		,__snake_case		,__snake_case						:Dict      		=     basic(target_str, genes_list)
 print(
     f'\nGeneration: {generation}\nTotal Population: {population}\nTarget: {target}'
 )





