






from __future__ import annotations

import unittest

from transformers import FunnelConfig, is_tf_available
from transformers.testing_utils import require_tf

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 (
	    TFFunnelBaseModel,
	    TFFunnelForMaskedLM,
	    TFFunnelForMultipleChoice,
	    TFFunnelForPreTraining,
	    TFFunnelForQuestionAnswering,
	    TFFunnelForSequenceClassification,
	    TFFunnelForTokenClassification,
	    TFFunnelModel,
	)

class 						lowerCAmelCase   :

					'''simple docstring'''

					def __init__(     self :		Optional[int]						,   __a :		int						,   __a :		int=13						,   __a :		Union[str, Any]=7						,   __a :		int=True						,   __a :		List[str]=True						,   __a :		Tuple=True						,   __a :		str=True						,   __a :		str=99						,   __a :		Optional[int]=[1, 1, 2]						,   __a :		List[Any]=1						,   __a :		List[str]=32						,   __a :		Optional[Any]=4						,   __a :		Optional[Any]=8						,   __a :		Dict=37						,   __a :		Optional[int]="gelu_new"						,   __a :		int=0.1						,   __a :		List[Any]=0.1						,   __a :		Dict=0.0						,   __a :		List[Any]=512						,   __a :		Optional[Any]=3						,   __a :		Tuple=0.02						,   __a :		Any=3						,   __a :		Optional[Any]=4						,   __a :		int=None						,   __a :		Any=False						,   )	->     Any:


												"""simple docstring"""

												__lowercase					:							Dict			=   parent
												__lowercase					:							Any			=   batch_size
												__lowercase					:							Optional[Any]			=   seq_length
												__lowercase					:							Optional[Any]			=   is_training
												__lowercase					:							int			=   use_input_mask
												__lowercase					:							str			=   use_token_type_ids
												__lowercase					:							Tuple			=   use_labels
												__lowercase					:							Tuple			=   vocab_size
												__lowercase					:							Tuple			=   block_sizes
												__lowercase					:							Optional[Any]			=   num_decoder_layers
												__lowercase					:							int			=   d_model
												__lowercase					:							int			=   n_head
												__lowercase					:							Union[str, Any]			=   d_head
												__lowercase					:							Tuple			=   d_inner
												__lowercase					:							List[str]			=   hidden_act
												__lowercase					:							str			=   hidden_dropout
												__lowercase					:							List[Any]			=   attention_dropout
												__lowercase					:							int			=   activation_dropout
												__lowercase					:							Union[str, Any]			=   max_position_embeddings
												__lowercase					:							List[str]			=   type_vocab_size
												__lowercase					:							List[Any]			=   2
												__lowercase					:							Optional[int]			=   num_labels
												__lowercase					:							Tuple			=   num_choices
												__lowercase					:							str			=   scope
												__lowercase					:							List[Any]			=   initializer_std

												# Used in the tests to check the size of the first attention layer
												__lowercase					:							str			=   n_head
												# Used in the tests to check the size of the first hidden state
												__lowercase					:							List[str]			=   self.d_model
												# Used in the tests to check the number of output hidden states/attentions
												__lowercase					:							List[Any]			=   sum(self.block_sizes							) + (0 if base else self.num_decoder_layers)
												# FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with
												# the last hidden state of the first block (which is the first hidden state of the decoder).
												if not base:
																			__lowercase					:							Tuple			=   self.num_hidden_layers + 2



					def 					lowerCAmelCase (     self :		Tuple							)	->     Dict:


												"""simple docstring"""

												__lowercase					:							Optional[Any]			=   ids_tensor([self.batch_size, self.seq_length]						,   self.vocab_size							)

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

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

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

												__lowercase					:							Tuple			=   FunnelConfig(
												    vocab_size=self.vocab_size						,   block_sizes=self.block_sizes						,   num_decoder_layers=self.num_decoder_layers						,   d_model=self.d_model						,   n_head=self.n_head						,   d_head=self.d_head						,   d_inner=self.d_inner						,   hidden_act=self.hidden_act						,   hidden_dropout=self.hidden_dropout						,   attention_dropout=self.attention_dropout						,   activation_dropout=self.activation_dropout						,   max_position_embeddings=self.max_position_embeddings						,   type_vocab_size=self.type_vocab_size						,   initializer_std=self.initializer_std						,   )

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



					def 					lowerCAmelCase (     self :		List[str]						,   __a :		List[str]						,   __a :		Any						,   __a :		Dict						,   __a :		str						,   __a :		int						,   __a :		Any						,   __a :		int						,   )	->     List[Any]:


												"""simple docstring"""

												__lowercase					:							List[Any]			=   TFFunnelModel(config=__a							)
												__lowercase					:							Dict			=   {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
												__lowercase					:							int			=   model(__a							)

												__lowercase					:							Optional[Any]			=   [input_ids, input_mask]
												__lowercase					:							List[str]			=   model(__a							)

												__lowercase					:							str			=   model(__a							)
												self.parent.assertEqual(result.last_hidden_state.shape						,   (self.batch_size, self.seq_length, self.d_model)							)

												__lowercase					:							Optional[int]			=   False
												__lowercase					:							Optional[Any]			=   TFFunnelModel(config=__a							)
												__lowercase					:							int			=   model(__a							)
												self.parent.assertEqual(result.last_hidden_state.shape						,   (self.batch_size, self.seq_length, self.d_model)							)

												__lowercase					:							int			=   False
												__lowercase					:							Dict			=   TFFunnelModel(config=__a							)
												__lowercase					:							int			=   model(__a							)
												self.parent.assertEqual(result.last_hidden_state.shape						,   (self.batch_size, self.seq_length, self.d_model)							)



					def 					lowerCAmelCase (     self :		List[Any]						,   __a :		str						,   __a :		Tuple						,   __a :		Tuple						,   __a :		Union[str, Any]						,   __a :		List[str]						,   __a :		Tuple						,   __a :		Optional[Any]						,   )	->     str:


												"""simple docstring"""

												__lowercase					:							Any			=   TFFunnelBaseModel(config=__a							)
												__lowercase					:							int			=   {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
												__lowercase					:							Dict			=   model(__a							)

												__lowercase					:							Optional[Any]			=   [input_ids, input_mask]
												__lowercase					:							Tuple			=   model(__a							)

												__lowercase					:							int			=   model(__a							)
												self.parent.assertEqual(result.last_hidden_state.shape						,   (self.batch_size, 2, self.d_model)							)

												__lowercase					:							Union[str, Any]			=   False
												__lowercase					:							Dict			=   TFFunnelBaseModel(config=__a							)
												__lowercase					:							Union[str, Any]			=   model(__a							)
												self.parent.assertEqual(result.last_hidden_state.shape						,   (self.batch_size, 3, self.d_model)							)

												__lowercase					:							Optional[Any]			=   False
												__lowercase					:							Any			=   TFFunnelBaseModel(config=__a							)
												__lowercase					:							int			=   model(__a							)
												self.parent.assertEqual(result.last_hidden_state.shape						,   (self.batch_size, 2, self.d_model)							)



					def 					lowerCAmelCase (     self :		Dict						,   __a :		Optional[Any]						,   __a :		Dict						,   __a :		Tuple						,   __a :		str						,   __a :		List[str]						,   __a :		List[Any]						,   __a :		List[Any]						,   )	->     Optional[int]:


												"""simple docstring"""

												__lowercase					:							Union[str, Any]			=   TFFunnelForPreTraining(config=__a							)
												__lowercase					:							List[Any]			=   {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
												__lowercase					:							Union[str, Any]			=   model(__a							)
												self.parent.assertEqual(result.logits.shape						,   (self.batch_size, self.seq_length)							)



					def 					lowerCAmelCase (     self :		Any						,   __a :		Tuple						,   __a :		Tuple						,   __a :		List[Any]						,   __a :		Union[str, Any]						,   __a :		Optional[Any]						,   __a :		List[Any]						,   __a :		Any						,   )	->     int:


												"""simple docstring"""

												__lowercase					:							Any			=   TFFunnelForMaskedLM(config=__a							)
												__lowercase					:							List[Any]			=   {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
												__lowercase					:							Optional[int]			=   model(__a							)
												self.parent.assertEqual(result.logits.shape						,   (self.batch_size, self.seq_length, self.vocab_size)							)



					def 					lowerCAmelCase (     self :		Optional[int]						,   __a :		Tuple						,   __a :		Any						,   __a :		Dict						,   __a :		Union[str, Any]						,   __a :		List[str]						,   __a :		Any						,   __a :		Optional[Any]						,   )	->     List[str]:


												"""simple docstring"""

												__lowercase					:							List[str]			=   self.num_labels
												__lowercase					:							Optional[int]			=   TFFunnelForSequenceClassification(config=__a							)
												__lowercase					:							Union[str, Any]			=   {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
												__lowercase					:							Union[str, Any]			=   model(__a							)
												self.parent.assertEqual(result.logits.shape						,   (self.batch_size, self.num_labels)							)



					def 					lowerCAmelCase (     self :		Dict						,   __a :		Dict						,   __a :		Tuple						,   __a :		str						,   __a :		Optional[Any]						,   __a :		str						,   __a :		Any						,   __a :		List[str]						,   )	->     Dict:


												"""simple docstring"""

												__lowercase					:							Optional[int]			=   self.num_choices
												__lowercase					:							Union[str, Any]			=   TFFunnelForMultipleChoice(config=__a							)
												__lowercase					:							Optional[Any]			=   tf.tile(tf.expand_dims(__a						,   1							)						,   (1, self.num_choices, 1)							)
												__lowercase					:							int			=   tf.tile(tf.expand_dims(__a						,   1							)						,   (1, self.num_choices, 1)							)
												__lowercase					:							str			=   tf.tile(tf.expand_dims(__a						,   1							)						,   (1, self.num_choices, 1)							)
												__lowercase					:							Optional[int]			=   {
												    """input_ids""": multiple_choice_inputs_ids,
												    """attention_mask""": multiple_choice_input_mask,
												    """token_type_ids""": multiple_choice_token_type_ids,
												}
												__lowercase					:							int			=   model(__a							)
												self.parent.assertEqual(result.logits.shape						,   (self.batch_size, self.num_choices)							)



					def 					lowerCAmelCase (     self :		Dict						,   __a :		List[str]						,   __a :		List[str]						,   __a :		Any						,   __a :		Optional[int]						,   __a :		Dict						,   __a :		Optional[int]						,   __a :		Union[str, Any]						,   )	->     Optional[Any]:


												"""simple docstring"""

												__lowercase					:							int			=   self.num_labels
												__lowercase					:							int			=   TFFunnelForTokenClassification(config=__a							)
												__lowercase					:							Union[str, Any]			=   {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
												__lowercase					:							Tuple			=   model(__a							)
												self.parent.assertEqual(result.logits.shape						,   (self.batch_size, self.seq_length, self.num_labels)							)



					def 					lowerCAmelCase (     self :		int						,   __a :		Optional[Any]						,   __a :		Optional[Any]						,   __a :		Any						,   __a :		Any						,   __a :		Optional[Any]						,   __a :		Tuple						,   __a :		List[str]						,   )	->     List[Any]:


												"""simple docstring"""

												__lowercase					:							Optional[Any]			=   TFFunnelForQuestionAnswering(config=__a							)
												__lowercase					:							Tuple			=   {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
												__lowercase					:							Tuple			=   model(__a							)
												self.parent.assertEqual(result.start_logits.shape						,   (self.batch_size, self.seq_length)							)
												self.parent.assertEqual(result.end_logits.shape						,   (self.batch_size, self.seq_length)							)



					def 					lowerCAmelCase (     self :		Dict							)	->     Optional[int]:


												"""simple docstring"""

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







@require_tf
class 						lowerCAmelCase   (       __a					,				__a					,				unittest.TestCase	):

					'''simple docstring'''

					_A  :	Optional[Any]     =					(
					    (
					        TFFunnelModel,
					        TFFunnelForMaskedLM,
					        TFFunnelForPreTraining,
					        TFFunnelForQuestionAnswering,
					        TFFunnelForTokenClassification,
					    )
					    if is_tf_available()
					    else ()
					)
					_A  :	Any     =					(
					    {
					        '''feature-extraction''': (TFFunnelBaseModel, TFFunnelModel),
					        '''fill-mask''': TFFunnelForMaskedLM,
					        '''question-answering''': TFFunnelForQuestionAnswering,
					        '''text-classification''': TFFunnelForSequenceClassification,
					        '''token-classification''': TFFunnelForTokenClassification,
					        '''zero-shot''': TFFunnelForSequenceClassification,
					    }
					    if is_tf_available()
					    else {}
					)
					_A  :	Optional[Any]     =					False
					_A  :	int     =					False



					def 					lowerCAmelCase (     self :		Optional[Any]							)	->     Optional[int]:


												"""simple docstring"""

												__lowercase					:							int			=   TFFunnelModelTester(self							)
												__lowercase					:							Union[str, Any]			=   ConfigTester(self						,   config_class=__a							)



					def 					lowerCAmelCase (     self :		int							)	->     str:


												"""simple docstring"""

												self.config_tester.run_common_tests()



					def 					lowerCAmelCase (     self :		Optional[int]							)	->     List[Any]:


												"""simple docstring"""

												__lowercase					:							int			=   self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*__a							)



					def 					lowerCAmelCase (     self :		Dict							)	->     List[str]:


												"""simple docstring"""

												__lowercase					:							List[str]			=   self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_pretraining(*__a							)



					def 					lowerCAmelCase (     self :		str							)	->     int:


												"""simple docstring"""

												__lowercase					:							Optional[int]			=   self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_masked_lm(*__a							)



					def 					lowerCAmelCase (     self :		Tuple							)	->     Dict:


												"""simple docstring"""

												__lowercase					:							Union[str, Any]			=   self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_token_classification(*__a							)



					def 					lowerCAmelCase (     self :		Union[str, Any]							)	->     str:


												"""simple docstring"""

												__lowercase					:							List[Any]			=   self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_question_answering(*__a							)







@require_tf
class 						lowerCAmelCase   (       __a					,				unittest.TestCase	):

					'''simple docstring'''

					_A  :	Optional[int]     =					(
					    (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
					)
					_A  :	Dict     =					False
					_A  :	List[str]     =					False



					def 					lowerCAmelCase (     self :		Union[str, Any]							)	->     List[str]:


												"""simple docstring"""

												__lowercase					:							List[Any]			=   TFFunnelModelTester(self						,   base=__a							)
												__lowercase					:							Union[str, Any]			=   ConfigTester(self						,   config_class=__a							)



					def 					lowerCAmelCase (     self :		Dict							)	->     List[Any]:


												"""simple docstring"""

												self.config_tester.run_common_tests()



					def 					lowerCAmelCase (     self :		Optional[int]							)	->     List[Any]:


												"""simple docstring"""

												__lowercase					:							Optional[int]			=   self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_base_model(*__a							)



					def 					lowerCAmelCase (     self :		Optional[int]							)	->     Tuple:


												"""simple docstring"""

												__lowercase					:							List[Any]			=   self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_sequence_classification(*__a							)



					def 					lowerCAmelCase (     self :		Optional[int]							)	->     Optional[Any]:


												"""simple docstring"""

												__lowercase					:							Any			=   self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_multiple_choice(*__a							)







lowerCamelCase       :       Tuple        =				{'''a''': ['''c''', '''b'''], '''b''': ['''d''', '''e'''], '''c''': [], '''d''': [], '''e''': []}
lowerCamelCase       :       int        =				['''a''', '''b''', '''c''', '''d''', '''e''']
def 					snake_case_						(   lowerCAmelCase_				:  Optional[int]				,				lowerCAmelCase_				:  str				,				lowerCAmelCase_				:  List[Any]					):
							__lowercase					:							Dict			=   start
							# add current to visited
							visited.append(lowerCAmelCase_					)
							__lowercase					:							Dict			=   edges[current]
							for neighbor in neighbors:
														# if neighbor not in visited, visit
														if neighbor not in visited:
																					__lowercase					:							List[Any]			=   topological_sort(lowerCAmelCase_				,				lowerCAmelCase_				,				lowerCAmelCase_					)
    # if all neighbors visited add current to sort
							sort.append(lowerCAmelCase_					)
							# if all vertices haven't been visited select a new one to visit
							if len(lowerCAmelCase_					) != len(lowerCAmelCase_					):
														for vertice in vertices:
																					if vertice not in visited:
																												__lowercase					:							Tuple			=   topological_sort(lowerCAmelCase_				,				lowerCAmelCase_				,				lowerCAmelCase_					)
    # return sort
							return sort


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