




'''simple docstring'''


from __future__ import annotations

import os
import tempfile
import unittest

from transformers import ConvBertConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
	import tensorflow as tf

	from transformers import (
	    TFConvBertForMaskedLM,
	    TFConvBertForMultipleChoice,
	    TFConvBertForQuestionAnswering,
	    TFConvBertForSequenceClassification,
	    TFConvBertForTokenClassification,
	    TFConvBertModel,
	)
class 						a_				:




	def __init__(       self     :  Union[str, Any]       ,		__lowerCAmelCase     :  Optional[Any]       ,		__lowerCAmelCase     :  str=1_3       ,		__lowerCAmelCase     :  str=7       ,		__lowerCAmelCase     :  Dict=True       ,		__lowerCAmelCase     :  Union[str, Any]=True       ,		__lowerCAmelCase     :  int=True       ,		__lowerCAmelCase     :  Optional[Any]=True       ,		__lowerCAmelCase     :  int=9_9       ,		__lowerCAmelCase     :  List[Any]=3_2       ,		__lowerCAmelCase     :  int=2       ,		__lowerCAmelCase     :  Dict=4       ,		__lowerCAmelCase     :  Dict=3_7       ,		__lowerCAmelCase     :  Optional[int]="gelu"       ,		__lowerCAmelCase     :  Any=0.1       ,		__lowerCAmelCase     :  Any=0.1       ,		__lowerCAmelCase     :  Optional[int]=5_1_2       ,		__lowerCAmelCase     :  str=1_6       ,		__lowerCAmelCase     :  Optional[int]=2       ,		__lowerCAmelCase     :  Tuple=0.02       ,		__lowerCAmelCase     :  Tuple=3       ,		__lowerCAmelCase     :  Union[str, Any]=4       ,		__lowerCAmelCase     :  List[str]=None       ,		):
							__snake_case						 =   parent
							__snake_case						 =   1_3
							__snake_case						 =   7
							__snake_case						 =   True
							__snake_case						 =   True
							__snake_case						 =   True
							__snake_case						 =   True
							__snake_case						 =   9_9
							__snake_case						 =   3_8_4
							__snake_case						 =   2
							__snake_case						 =   4
							__snake_case						 =   3_7
							__snake_case						 =   'gelu'
							__snake_case						 =   0.1
							__snake_case						 =   0.1
							__snake_case						 =   5_1_2
							__snake_case						 =   1_6
							__snake_case						 =   2
							__snake_case						 =   0.02
							__snake_case						 =   3
							__snake_case						 =   4
							__snake_case						 =   1_2_8
							__snake_case						 =   2
							__snake_case						 =   9
							__snake_case						 =   1
							__snake_case						 =   None




	def   lowercase__   (       self     :  Optional[Any]      ):
							__snake_case						 =   ids_tensor([self.batch_size, self.seq_length]       ,		self.vocab_size      )

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

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

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

							__snake_case						 =   ConvBertConfig(
							    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       ,		initializer_range=self.initializer_range       ,		return_dict=__lowerCAmelCase       ,		)

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




	def   lowercase__   (       self     :  Optional[int]       ,		__lowerCAmelCase     :  str       ,		__lowerCAmelCase     :  str       ,		__lowerCAmelCase     :  int       ,		__lowerCAmelCase     :  Dict       ,		__lowerCAmelCase     :  Any       ,		__lowerCAmelCase     :  Dict       ,		__lowerCAmelCase     :  Any      ):
							__snake_case						 =   TFConvBertModel(config=__lowerCAmelCase      )
							__snake_case						 =   {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

							__snake_case						 =   [input_ids, input_mask]
							__snake_case						 =   model(__lowerCAmelCase      )

							__snake_case						 =   model(__lowerCAmelCase      )

							self.parent.assertEqual(result.last_hidden_state.shape       ,		(self.batch_size, self.seq_length, self.hidden_size)      )




	def   lowercase__   (       self     :  Tuple       ,		__lowerCAmelCase     :  int       ,		__lowerCAmelCase     :  int       ,		__lowerCAmelCase     :  str       ,		__lowerCAmelCase     :  Tuple       ,		__lowerCAmelCase     :  int       ,		__lowerCAmelCase     :  Union[str, Any]       ,		__lowerCAmelCase     :  str      ):
							__snake_case						 =   TFConvBertForMaskedLM(config=__lowerCAmelCase      )
							__snake_case						 =   {
							    'input_ids': input_ids,
							    'attention_mask': input_mask,
							    'token_type_ids': token_type_ids,
							}
							__snake_case						 =   model(__lowerCAmelCase      )
							self.parent.assertEqual(result.logits.shape       ,		(self.batch_size, self.seq_length, self.vocab_size)      )




	def   lowercase__   (       self     :  str       ,		__lowerCAmelCase     :  Dict       ,		__lowerCAmelCase     :  Optional[int]       ,		__lowerCAmelCase     :  Dict       ,		__lowerCAmelCase     :  str       ,		__lowerCAmelCase     :  Tuple       ,		__lowerCAmelCase     :  List[Any]       ,		__lowerCAmelCase     :  List[Any]      ):
							__snake_case						 =   self.num_labels
							__snake_case						 =   TFConvBertForSequenceClassification(config=__lowerCAmelCase      )
							__snake_case						 =   {
							    'input_ids': input_ids,
							    'attention_mask': input_mask,
							    'token_type_ids': token_type_ids,
							}

							__snake_case						 =   model(__lowerCAmelCase      )
							self.parent.assertEqual(result.logits.shape       ,		(self.batch_size, self.num_labels)      )




	def   lowercase__   (       self     :  List[str]       ,		__lowerCAmelCase     :  str       ,		__lowerCAmelCase     :  List[str]       ,		__lowerCAmelCase     :  Any       ,		__lowerCAmelCase     :  Union[str, Any]       ,		__lowerCAmelCase     :  List[str]       ,		__lowerCAmelCase     :  Any       ,		__lowerCAmelCase     :  str      ):
							__snake_case						 =   self.num_choices
							__snake_case						 =   TFConvBertForMultipleChoice(config=__lowerCAmelCase      )
							__snake_case						 =   tf.tile(tf.expand_dims(__lowerCAmelCase       ,		1      )       ,		(1, self.num_choices, 1)      )
							__snake_case						 =   tf.tile(tf.expand_dims(__lowerCAmelCase       ,		1      )       ,		(1, self.num_choices, 1)      )
							__snake_case						 =   tf.tile(tf.expand_dims(__lowerCAmelCase       ,		1      )       ,		(1, self.num_choices, 1)      )
							__snake_case						 =   {
							    'input_ids': multiple_choice_inputs_ids,
							    'attention_mask': multiple_choice_input_mask,
							    'token_type_ids': multiple_choice_token_type_ids,
							}
							__snake_case						 =   model(__lowerCAmelCase      )
							self.parent.assertEqual(result.logits.shape       ,		(self.batch_size, self.num_choices)      )




	def   lowercase__   (       self     :  Optional[Any]       ,		__lowerCAmelCase     :  Tuple       ,		__lowerCAmelCase     :  int       ,		__lowerCAmelCase     :  int       ,		__lowerCAmelCase     :  List[Any]       ,		__lowerCAmelCase     :  List[Any]       ,		__lowerCAmelCase     :  int       ,		__lowerCAmelCase     :  List[str]      ):
							__snake_case						 =   self.num_labels
							__snake_case						 =   TFConvBertForTokenClassification(config=__lowerCAmelCase      )
							__snake_case						 =   {
							    'input_ids': input_ids,
							    'attention_mask': input_mask,
							    'token_type_ids': token_type_ids,
							}
							__snake_case						 =   model(__lowerCAmelCase      )
							self.parent.assertEqual(result.logits.shape       ,		(self.batch_size, self.seq_length, self.num_labels)      )




	def   lowercase__   (       self     :  List[str]       ,		__lowerCAmelCase     :  str       ,		__lowerCAmelCase     :  Any       ,		__lowerCAmelCase     :  Dict       ,		__lowerCAmelCase     :  Any       ,		__lowerCAmelCase     :  Any       ,		__lowerCAmelCase     :  Optional[Any]       ,		__lowerCAmelCase     :  Optional[Any]      ):
							__snake_case						 =   TFConvBertForQuestionAnswering(config=__lowerCAmelCase      )
							__snake_case						 =   {
							    'input_ids': input_ids,
							    'attention_mask': input_mask,
							    'token_type_ids': token_type_ids,
							}

							__snake_case						 =   model(__lowerCAmelCase      )
							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   lowercase__   (       self     :  Union[str, Any]      ):
							__snake_case						 =   self.prepare_config_and_inputs()
							(
							    (
							    __snake_case
							) ,       (
							    __snake_case
							) ,       (
							    __snake_case
							) ,       (
							    __snake_case
							) ,       (
							    __snake_case
							) ,       (
							    __snake_case
							) ,       (
							    __snake_case
							) ,       
							)						 =   config_and_inputs
							__snake_case						 =   {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
							return config, inputs_dict





@require_tf
class 						a_				(			UpperCAmelCase__     ,						UpperCAmelCase__     ,						unittest.TestCase						):
	lowercase_       :     List[Any]			  =      (
	    (
	        TFConvBertModel,
	        TFConvBertForMaskedLM,
	        TFConvBertForQuestionAnswering,
	        TFConvBertForSequenceClassification,
	        TFConvBertForTokenClassification,
	        TFConvBertForMultipleChoice,
	    )
	    if is_tf_available()
	    else ()
	)
	lowercase_       :     Dict			  =      (
	    {
	        '''feature-extraction''': TFConvBertModel,
	        '''fill-mask''': TFConvBertForMaskedLM,
	        '''question-answering''': TFConvBertForQuestionAnswering,
	        '''text-classification''': TFConvBertForSequenceClassification,
	        '''token-classification''': TFConvBertForTokenClassification,
	        '''zero-shot''': TFConvBertForSequenceClassification,
	    }
	    if is_tf_available()
	    else {}
	)
	lowercase_       :     str			  =      False
	lowercase_       :     List[Any]			  =      False
	lowercase_       :     Any			  =      False




	def   lowercase__   (       self     :  Union[str, Any]      ):
							__snake_case						 =   TFConvBertModelTester(self      )
							__snake_case						 =   ConfigTester(self       ,		config_class=__lowerCAmelCase       ,		hidden_size=3_7      )




	def   lowercase__   (       self     :  List[Any]      ):
							self.config_tester.run_common_tests()




	def   lowercase__   (       self     :  Optional[Any]      ):
							__snake_case						 =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__lowerCAmelCase      )




	def   lowercase__   (       self     :  str      ):
							__snake_case						 =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*__lowerCAmelCase      )




	def   lowercase__   (       self     :  List[Any]      ):
							__snake_case						 =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_multiple_choice(*__lowerCAmelCase      )




	def   lowercase__   (       self     :  str      ):
							__snake_case						 =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*__lowerCAmelCase      )




	def   lowercase__   (       self     :  Optional[int]      ):
							__snake_case						 =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*__lowerCAmelCase      )




	def   lowercase__   (       self     :  List[str]      ):
							__snake_case						 =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*__lowerCAmelCase      )




	@slow
	def   lowercase__   (       self     :  Dict      ):
							__snake_case ,       __snake_case						 =   self.model_tester.prepare_config_and_inputs_for_common()
							__snake_case						 =   True
							__snake_case						 =   True

							if hasattr(__lowerCAmelCase       ,		'use_cache'      ):
													__snake_case						 =   True

							__snake_case						 =   getattr(self.model_tester       ,		'encoder_seq_length'       ,		self.model_tester.seq_length      )
							__snake_case						 =   getattr(self.model_tester       ,		'key_length'       ,		__lowerCAmelCase      )

							for model_class in self.all_model_classes:
													__snake_case						 =   self._prepare_for_class(__lowerCAmelCase       ,		__lowerCAmelCase      )
													__snake_case						 =   model_class(__lowerCAmelCase      )
													__snake_case						 =   len(model(__lowerCAmelCase      )      )

													with tempfile.TemporaryDirectory() as tmpdirname:
																			model.save_pretrained(__lowerCAmelCase       ,		saved_model=__lowerCAmelCase      )
																			__snake_case						 =   os.path.join(__lowerCAmelCase       ,		'saved_model'       ,		'1'      )
																			__snake_case						 =   tf.keras.models.load_model(__lowerCAmelCase      )
																			__snake_case						 =   model(__lowerCAmelCase      )

																			if self.is_encoder_decoder:
																									__snake_case						 =   outputs['encoder_hidden_states']
																									__snake_case						 =   outputs['encoder_attentions']
																			else:
																									__snake_case						 =   outputs['hidden_states']
																									__snake_case						 =   outputs['attentions']

																			self.assertEqual(len(__lowerCAmelCase      )       ,		__lowerCAmelCase      )

																			__snake_case						 =   getattr(
																			    self.model_tester       ,		'expected_num_hidden_layers'       ,		self.model_tester.num_hidden_layers + 1      )

																			self.assertEqual(len(__lowerCAmelCase      )       ,		__lowerCAmelCase      )
																			self.assertListEqual(
																			    list(output_hidden_states[0].shape[-2:]      )       ,		[self.model_tester.seq_length, self.model_tester.hidden_size]       ,		)

																			self.assertEqual(len(__lowerCAmelCase      )       ,		self.model_tester.num_hidden_layers      )
																			self.assertListEqual(
																			    list(output_attentions[0].shape[-3:]      )       ,		[self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]       ,		)




	@slow
	def   lowercase__   (       self     :  int      ):
							__snake_case						 =   TFConvBertModel.from_pretrained('YituTech/conv-bert-base'      )
							self.assertIsNotNone(__lowerCAmelCase      )




	def   lowercase__   (       self     :  Tuple      ):
							__snake_case ,       __snake_case						 =   self.model_tester.prepare_config_and_inputs_for_common()
							__snake_case						 =   True
							__snake_case						 =   getattr(self.model_tester       ,		'decoder_seq_length'       ,		self.model_tester.seq_length      )
							__snake_case						 =   getattr(self.model_tester       ,		'encoder_seq_length'       ,		self.model_tester.seq_length      )
							__snake_case						 =   getattr(self.model_tester       ,		'key_length'       ,		__lowerCAmelCase      )
							__snake_case						 =   getattr(self.model_tester       ,		'key_length'       ,		__lowerCAmelCase      )

							def check_decoder_attentions_output(__lowerCAmelCase     :  Dict      ):
													__snake_case						 =   len(__lowerCAmelCase      )
													self.assertEqual(out_len % 2       ,		0      )
													__snake_case						 =   outputs.decoder_attentions
													self.assertEqual(len(__lowerCAmelCase      )       ,		self.model_tester.num_hidden_layers      )
													self.assertListEqual(
													    list(decoder_attentions[0].shape[-3:]      )       ,		[self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length]       ,		)

							def check_encoder_attentions_output(__lowerCAmelCase     :  Any      ):
													__snake_case						 =   [
													    t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
													]
													self.assertEqual(len(__lowerCAmelCase      )       ,		self.model_tester.num_hidden_layers      )
													self.assertListEqual(
													    list(attentions[0].shape[-3:]      )       ,		[self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]       ,		)

							for model_class in self.all_model_classes:
													__snake_case						 =   True
													__snake_case						 =   False
													__snake_case						 =   model_class(__lowerCAmelCase      )
													__snake_case						 =   model(self._prepare_for_class(__lowerCAmelCase       ,		__lowerCAmelCase      )      )
													__snake_case						 =   len(__lowerCAmelCase      )
													self.assertEqual(config.output_hidden_states       ,		__lowerCAmelCase      )
													check_encoder_attentions_output(__lowerCAmelCase      )

													if self.is_encoder_decoder:
																			__snake_case						 =   model_class(__lowerCAmelCase      )
																			__snake_case						 =   model(self._prepare_for_class(__lowerCAmelCase       ,		__lowerCAmelCase      )      )
																			self.assertEqual(config.output_hidden_states       ,		__lowerCAmelCase      )
																			check_decoder_attentions_output(__lowerCAmelCase      )

													# Check that output attentions can also be changed via the config
													del inputs_dict["output_attentions"]
													__snake_case						 =   True
													__snake_case						 =   model_class(__lowerCAmelCase      )
													__snake_case						 =   model(self._prepare_for_class(__lowerCAmelCase       ,		__lowerCAmelCase      )      )
													self.assertEqual(config.output_hidden_states       ,		__lowerCAmelCase      )
													check_encoder_attentions_output(__lowerCAmelCase      )

													# Check attention is always last and order is fine
													__snake_case						 =   True
													__snake_case						 =   True
													__snake_case						 =   model_class(__lowerCAmelCase      )
													__snake_case						 =   model(self._prepare_for_class(__lowerCAmelCase       ,		__lowerCAmelCase      )      )

													self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1)       ,		len(__lowerCAmelCase      )      )
													self.assertEqual(model.config.output_hidden_states       ,		__lowerCAmelCase      )
													check_encoder_attentions_output(__lowerCAmelCase      )





@require_tf
class 						a_				(			unittest.TestCase						):




	@slow
	def   lowercase__   (       self     :  Union[str, Any]      ):
							__snake_case						 =   TFConvBertModel.from_pretrained('YituTech/conv-bert-base'      )
							__snake_case						 =   tf.constant([[0, 1, 2, 3, 4, 5]]      )
							__snake_case						 =   model(__lowerCAmelCase      )[0]

							__snake_case						 =   [1, 6, 7_6_8]
							self.assertEqual(output.shape       ,		__lowerCAmelCase      )

							__snake_case						 =   tf.constant(
							    [
							        [
							            [-0.03475493, -0.4686034, -0.30638832],
							            [0.22637248, -0.26988646, -0.7423424],
							            [0.10324868, -0.45013508, -0.58280784],
							        ]
							    ]      )
							tf.debugging.assert_near(output[:, :3, :3]       ,		__lowerCAmelCase       ,		atol=1E-4      )







'''simple docstring'''


def 						lowerCamelCase__   (				a							):
						__snake_case						 =   [0] * len(a							)
						__snake_case						 =   []
						__snake_case						 =   []
						__snake_case						 =   0

						for values in graph.values():
												for i in values:
																		indegree[i] += 1

						for i in range(len(a							)							):
												if indegree[i] == 0:
																		queue.append(a							)

						while queue:
												__snake_case						 =   queue.pop(0							)
												cnt += 1
												topo.append(a							)
												for x in graph[vertex]:
																		indegree[x] -= 1
																		if indegree[x] == 0:
																								queue.append(a							)

						if cnt != len(a							):
												print('Cycle exists'							)
						else:
												print(a							)


# Adjacency List of Graph
_lowercase  =			{0: [1, 2], 1: [3], 2: [3], 3: [4, 5], 4: [], 5: []}






topological_sort(graph)

