
'''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     __snake_case		:
					def __init__(     self,    A,    A=13,    A=7,    A=True,    A=True,    A=True,    A=True,    A=99,    A=32,    A=5,    A=4,    A=37,    A="gelu",    A=0.1,    A=0.1,    A=128,    A=32,    A=16,    A=2,    A=0.02,    A=3,    A=4,    A=None,    ):

									"""simple docstring"""


									lowerCamelCase					:			Any									= parent
									lowerCamelCase					:			List[Any]									= batch_size
									lowerCamelCase					:			Tuple									= seq_length
									lowerCamelCase					:			Tuple									= is_training
									lowerCamelCase					:			int									= use_input_mask
									lowerCamelCase					:			int									= use_token_type_ids
									lowerCamelCase					:			Optional[Any]									= use_labels
									lowerCamelCase					:			Tuple									= vocab_size
									lowerCamelCase					:			Any									= hidden_size
									lowerCamelCase					:			int									= num_hidden_layers
									lowerCamelCase					:			str									= num_attention_heads
									lowerCamelCase					:			Any									= intermediate_size
									lowerCamelCase					:			Any									= hidden_act
									lowerCamelCase					:			Optional[Any]									= hidden_dropout_prob
									lowerCamelCase					:			Optional[Any]									= attention_probs_dropout_prob
									lowerCamelCase					:			Tuple									= max_position_embeddings
									lowerCamelCase					:			Union[str, Any]									= type_vocab_size
									lowerCamelCase					:			Any									= type_sequence_label_size
									lowerCamelCase					:			Tuple									= initializer_range
									lowerCamelCase					:			Optional[int]									= num_labels
									lowerCamelCase					:			List[str]									= num_choices
									lowerCamelCase					:			List[str]									= scope
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			str									= ids_tensor([self.batch_size, self.seq_length],    self.vocab_size      )

									lowerCamelCase					:			Any									= 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					:			str									= ids_tensor([self.batch_size, self.seq_length],    self.type_vocab_size      )

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

									lowerCamelCase					:			Any									= self.get_config()

									return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
					def     UpperCAmelCase_							(     self      ):

									"""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=_snake_case,    initializer_range=self.initializer_range,    )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									(
									    (
									    lowerCamelCase 
									)			, (
									    lowerCamelCase 
									)			, (
									    lowerCamelCase 
									)			, (
									    lowerCamelCase 
									)			, (
									    lowerCamelCase 
									)			, (
									    lowerCamelCase 
									)			, (
									    lowerCamelCase 
									)			,  
									)					:			Tuple									= self.prepare_config_and_inputs()

									lowerCamelCase					:			Tuple									= True
									lowerCamelCase					:			Optional[Any]									= floats_tensor([self.batch_size, self.seq_length, self.hidden_size]      )
									lowerCamelCase					:			str									= 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     UpperCAmelCase_							(     self,    A,    A,    A,    A,    A,    A,    A      ):

									"""simple docstring"""


									lowerCamelCase					:			Tuple									= NezhaModel(config=_snake_case      )
									model.to(_snake_case      )
									model.eval()
									lowerCamelCase					:			Optional[int]									= model(_snake_case,    attention_mask=_snake_case,    token_type_ids=_snake_case      )
									lowerCamelCase					:			str									= model(_snake_case,    token_type_ids=_snake_case      )
									lowerCamelCase					:			Tuple									= model(_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     UpperCAmelCase_							(     self,    A,    A,    A,    A,    A,    A,    A,    A,    A,    ):

									"""simple docstring"""


									lowerCamelCase					:			Tuple									= True
									lowerCamelCase					:			Optional[int]									= NezhaModel(_snake_case      )
									model.to(_snake_case      )
									model.eval()
									lowerCamelCase					:			Union[str, Any]									= model(
									    _snake_case,    attention_mask=_snake_case,    token_type_ids=_snake_case,    encoder_hidden_states=_snake_case,    encoder_attention_mask=_snake_case,    )
									lowerCamelCase					:			Tuple									= model(
									    _snake_case,    attention_mask=_snake_case,    token_type_ids=_snake_case,    encoder_hidden_states=_snake_case,    )
									lowerCamelCase					:			List[Any]									= model(_snake_case,    attention_mask=_snake_case,    token_type_ids=_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     UpperCAmelCase_							(     self,    A,    A,    A,    A,    A,    A,    A      ):

									"""simple docstring"""


									lowerCamelCase					:			Optional[int]									= NezhaForMaskedLM(config=_snake_case      )
									model.to(_snake_case      )
									model.eval()
									lowerCamelCase					:			int									= model(_snake_case,    attention_mask=_snake_case,    token_type_ids=_snake_case,    labels=_snake_case      )
									self.parent.assertEqual(result.logits.shape,    (self.batch_size, self.seq_length, self.vocab_size)      )
					def     UpperCAmelCase_							(     self,    A,    A,    A,    A,    A,    A,    A      ):

									"""simple docstring"""


									lowerCamelCase					:			int									= NezhaForNextSentencePrediction(config=_snake_case      )
									model.to(_snake_case      )
									model.eval()
									lowerCamelCase					:			int									= model(
									    _snake_case,    attention_mask=_snake_case,    token_type_ids=_snake_case,    labels=_snake_case,    )
									self.parent.assertEqual(result.logits.shape,    (self.batch_size, 2)      )
					def     UpperCAmelCase_							(     self,    A,    A,    A,    A,    A,    A,    A      ):

									"""simple docstring"""


									lowerCamelCase					:			Optional[Any]									= NezhaForPreTraining(config=_snake_case      )
									model.to(_snake_case      )
									model.eval()
									lowerCamelCase					:			int									= model(
									    _snake_case,    attention_mask=_snake_case,    token_type_ids=_snake_case,    labels=_snake_case,    next_sentence_label=_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     UpperCAmelCase_							(     self,    A,    A,    A,    A,    A,    A,    A      ):

									"""simple docstring"""


									lowerCamelCase					:			List[str]									= NezhaForQuestionAnswering(config=_snake_case      )
									model.to(_snake_case      )
									model.eval()
									lowerCamelCase					:			str									= model(
									    _snake_case,    attention_mask=_snake_case,    token_type_ids=_snake_case,    start_positions=_snake_case,    end_positions=_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     UpperCAmelCase_							(     self,    A,    A,    A,    A,    A,    A,    A      ):

									"""simple docstring"""


									lowerCamelCase					:			Tuple									= self.num_labels
									lowerCamelCase					:			Dict									= NezhaForSequenceClassification(_snake_case      )
									model.to(_snake_case      )
									model.eval()
									lowerCamelCase					:			str									= model(_snake_case,    attention_mask=_snake_case,    token_type_ids=_snake_case,    labels=_snake_case      )
									self.parent.assertEqual(result.logits.shape,    (self.batch_size, self.num_labels)      )
					def     UpperCAmelCase_							(     self,    A,    A,    A,    A,    A,    A,    A      ):

									"""simple docstring"""


									lowerCamelCase					:			Any									= self.num_labels
									lowerCamelCase					:			List[str]									= NezhaForTokenClassification(config=_snake_case      )
									model.to(_snake_case      )
									model.eval()
									lowerCamelCase					:			List[str]									= model(_snake_case,    attention_mask=_snake_case,    token_type_ids=_snake_case,    labels=_snake_case      )
									self.parent.assertEqual(result.logits.shape,    (self.batch_size, self.seq_length, self.num_labels)      )
					def     UpperCAmelCase_							(     self,    A,    A,    A,    A,    A,    A,    A      ):

									"""simple docstring"""


									lowerCamelCase					:			Optional[Any]									= self.num_choices
									lowerCamelCase					:			Union[str, Any]									= NezhaForMultipleChoice(config=_snake_case      )
									model.to(_snake_case      )
									model.eval()
									lowerCamelCase					:			Optional[int]									= input_ids.unsqueeze(1      ).expand(-1,    self.num_choices,    -1      ).contiguous()
									lowerCamelCase					:			List[str]									= token_type_ids.unsqueeze(1      ).expand(-1,    self.num_choices,    -1      ).contiguous()
									lowerCamelCase					:			Optional[int]									= input_mask.unsqueeze(1      ).expand(-1,    self.num_choices,    -1      ).contiguous()
									lowerCamelCase					:			Dict									= model(
									    _snake_case,    attention_mask=_snake_case,    token_type_ids=_snake_case,    labels=_snake_case,    )
									self.parent.assertEqual(result.logits.shape,    (self.batch_size, self.num_choices)      )




					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			Optional[Any]									= self.prepare_config_and_inputs()
									(
									    (
									    lowerCamelCase 
									)			, (
									    lowerCamelCase 
									)			, (
									    lowerCamelCase 
									)			, (
									    lowerCamelCase 
									)			, (
									    lowerCamelCase 
									)			, (
									    lowerCamelCase 
									)			, (
									    lowerCamelCase 
									)			,  
									)					:			str									= config_and_inputs
									lowerCamelCase					:			List[str]									= {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
									return config, inputs_dict





@require_torch
class     __snake_case		(   UpperCAmelCase_    ,   UpperCAmelCase_    ,   UpperCAmelCase_    ,   unittest.TestCase):
					_lowerCAmelCase			     =	(
					    (
					        NezhaModel,
					        NezhaForMaskedLM,
					        NezhaForMultipleChoice,
					        NezhaForNextSentencePrediction,
					        NezhaForPreTraining,
					        NezhaForQuestionAnswering,
					        NezhaForSequenceClassification,
					        NezhaForTokenClassification,
					    )
					    if is_torch_available()
					    else ()
					)
					_lowerCAmelCase			     =	(
					    {
					        "feature-extraction": NezhaModel,
					        "fill-mask": NezhaForMaskedLM,
					        "question-answering": NezhaForQuestionAnswering,
					        "text-classification": NezhaForSequenceClassification,
					        "token-classification": NezhaForTokenClassification,
					        "zero-shot": NezhaForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					_lowerCAmelCase			     =	True
					def     UpperCAmelCase_							(     self,    A,    A,    A=False      ):

									"""simple docstring"""


									lowerCamelCase					:			int									= super()._prepare_for_class(_snake_case,    _snake_case,    return_labels=_snake_case      )

									if return_labels:
													if model_class in get_values(_snake_case      ):
																	lowerCamelCase					:			List[Any]									= torch.zeros(
																	    (self.model_tester.batch_size, self.model_tester.seq_length),    dtype=torch.long,    device=_snake_case      )
																	lowerCamelCase					:			List[Any]									= torch.zeros(
																	    self.model_tester.batch_size,    dtype=torch.long,    device=_snake_case      )
									return inputs_dict
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			Dict									= NezhaModelTester(self      )
									lowerCamelCase					:			List[str]									= ConfigTester(self,    config_class=_snake_case,    hidden_size=37      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


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

									"""simple docstring"""


									lowerCamelCase					:			Tuple									= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_snake_case      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			Optional[int]									= self.model_tester.prepare_config_and_inputs_for_decoder()
									self.model_tester.create_and_check_model_as_decoder(*_snake_case      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


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

									lowerCamelCase					:			int									= None

									self.model_tester.create_and_check_model_as_decoder(
									    _snake_case,    _snake_case,    _snake_case,    _snake_case,    _snake_case,    _snake_case,    _snake_case,    _snake_case,    _snake_case,    )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			Dict									= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_lm(*_snake_case      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			Optional[Any]									= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_multiple_choice(*_snake_case      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			Optional[int]									= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_next_sequence_prediction(*_snake_case      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			Tuple									= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_pretraining(*_snake_case      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			Dict									= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*_snake_case      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			int									= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*_snake_case      )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			Union[str, Any]									= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*_snake_case      )
					@slow
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													lowerCamelCase					:			List[Any]									= NezhaModel.from_pretrained(_snake_case      )
													self.assertIsNotNone(_snake_case      )




					@slow
					@require_torch_gpu
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase			, lowerCamelCase					:			Optional[Any]									= 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					:			Optional[Any]									= True
													lowerCamelCase					:			List[str]									= model_class(config=_snake_case      )

													lowerCamelCase					:			Union[str, Any]									= self._prepare_for_class(_snake_case,    _snake_case      )
													lowerCamelCase					:			Union[str, Any]									= torch.jit.trace(
													    _snake_case,    (inputs_dict['input_ids'].to('cpu'      ), inputs_dict['attention_mask'].to('cpu'      ))      )

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





@require_torch
class     __snake_case		(   unittest.TestCase):
					@slow
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			int									= NezhaModel.from_pretrained('sijunhe/nezha-cn-base'      )
									lowerCamelCase					:			Union[str, Any]									= torch.tensor([[0, 1, 2, 3, 4, 5]]      )
									lowerCamelCase					:			Tuple									= torch.tensor([[0, 1, 1, 1, 1, 1]]      )
									with torch.no_grad():
													lowerCamelCase					:			List[str]									= model(_snake_case,    attention_mask=_snake_case      )[0]
									lowerCamelCase					:			Any									= torch.Size((1, 6, 768)      )
									self.assertEqual(output.shape,    _snake_case      )
									lowerCamelCase					:			str									= torch.tensor([[[0.0685, 0.2441, 0.1102], [0.0600, 0.1906, 0.1349], [0.0221, 0.0819, 0.0586]]]      )

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




					@slow
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			Optional[Any]									= NezhaForMaskedLM.from_pretrained('sijunhe/nezha-cn-base'      )
									lowerCamelCase					:			Union[str, Any]									= torch.tensor([[0, 1, 2, 3, 4, 5]]      )
									lowerCamelCase					:			Union[str, Any]									= torch.tensor([[1, 1, 1, 1, 1, 1]]      )
									with torch.no_grad():
													lowerCamelCase					:			Optional[Any]									= model(_snake_case,    attention_mask=_snake_case      )[0]
									lowerCamelCase					:			int									= torch.Size((1, 6, 2_1128)      )
									self.assertEqual(output.shape,    _snake_case      )
									lowerCamelCase					:			Any									= torch.tensor(
									    [[-2.7939, -1.7902, -2.2189], [-2.8585, -1.8908, -2.3723], [-2.6499, -1.7750, -2.2558]]      )

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


'''simple docstring'''






A			      =      {'a': ['c', 'b'], 'b': ['d', 'e'], 'c': [], 'd': [], 'e': []}
A			      =      ['a', 'b', 'c', 'd', 'e']





def    UpperCAmelCase							(  UpperCAmelCase__							:							Union[str, Any]       ,							UpperCAmelCase__							:							Any       ,							UpperCAmelCase__							:							List[Any]):
				lowerCamelCase					:			List[Any]									= start
				# add current to visited
				visited.append(UpperCAmelCase__)
				lowerCamelCase					:			List[str]									= edges[current]
				for neighbor in neighbors:
								# if neighbor not in visited, visit
								if neighbor not in visited:
												lowerCamelCase					:			Any									= topological_sort(UpperCAmelCase__       ,							UpperCAmelCase__       ,							UpperCAmelCase__)
    # if all neighbors visited add current to sort
				sort.append(UpperCAmelCase__)
				# if all vertices haven't been visited select a new one to visit
				if len(UpperCAmelCase__) != len(UpperCAmelCase__):
								for vertice in vertices:
												if vertice not in visited:
																lowerCamelCase					:			List[str]									= topological_sort(UpperCAmelCase__       ,							UpperCAmelCase__       ,							UpperCAmelCase__)
    # return sort
				return sort


if __name__ == "__main__":
					A			      =      topological_sort('a', [], [])
					print(sort)
