






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






	def __init__(		self					,UpperCamelCase					,UpperCamelCase=13					,UpperCamelCase=7					,UpperCamelCase=True					,UpperCamelCase=True					,UpperCamelCase=True					,UpperCamelCase=True					,UpperCamelCase=99					,UpperCamelCase=[1, 1, 2]					,UpperCamelCase=1					,UpperCamelCase=32					,UpperCamelCase=4					,UpperCamelCase=8					,UpperCamelCase=37					,UpperCamelCase="gelu_new"					,UpperCamelCase=0.1					,UpperCamelCase=0.1					,UpperCamelCase=0.0					,UpperCamelCase=512					,UpperCamelCase=3					,UpperCamelCase=0.02					,UpperCamelCase=3					,UpperCamelCase=4					,UpperCamelCase=None					,UpperCamelCase=False					,)						->    List[Any]:
					snake_case__      :Optional[Any]         =   parent
					snake_case__      :List[str]         =   batch_size
					snake_case__      :Optional[int]         =   seq_length
					snake_case__      :int         =   is_training
					snake_case__      :Union[str, Any]         =   use_input_mask
					snake_case__      :Any         =   use_token_type_ids
					snake_case__      :int         =   use_labels
					snake_case__      :List[Any]         =   vocab_size
					snake_case__      :str         =   block_sizes
					snake_case__      :Union[str, Any]         =   num_decoder_layers
					snake_case__      :str         =   d_model
					snake_case__      :str         =   n_head
					snake_case__      :Union[str, Any]         =   d_head
					snake_case__      :List[Any]         =   d_inner
					snake_case__      :Any         =   hidden_act
					snake_case__      :Tuple         =   hidden_dropout
					snake_case__      :List[Any]         =   attention_dropout
					snake_case__      :Any         =   activation_dropout
					snake_case__      :Optional[int]         =   max_position_embeddings
					snake_case__      :List[str]         =   type_vocab_size
					snake_case__      :Tuple         =   2
					snake_case__      :Any         =   num_labels
					snake_case__      :str         =   num_choices
					snake_case__      :Union[str, Any]         =   scope
					snake_case__      :str         =   initializer_std

					# Used in the tests to check the size of the first attention layer
					snake_case__      :List[str]         =   n_head
					# Used in the tests to check the size of the first hidden state
					snake_case__      :Optional[int]         =   self.d_model
					# Used in the tests to check the number of output hidden states/attentions
					snake_case__      :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:
									snake_case__      :Any         =   self.num_hidden_layers + 2






	def    lowerCAmelCase_							(		self       )						->    str:
					snake_case__      :Dict         =   ids_tensor([self.batch_size, self.seq_length]					,self.vocab_size       )

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

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

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

					snake_case__      :List[Any]         =   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					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,)						->    List[Any]:
					snake_case__      :Any         =   TFFunnelModel(config=UpperCamelCase       )
					snake_case__      :List[str]         =   {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
					snake_case__      :Any         =   model(UpperCamelCase       )

					snake_case__      :Optional[Any]         =   [input_ids, input_mask]
					snake_case__      :Optional[Any]         =   model(UpperCamelCase       )

					snake_case__      :Optional[Any]         =   model(UpperCamelCase       )
					self.parent.assertEqual(result.last_hidden_state.shape					,(self.batch_size, self.seq_length, self.d_model)       )

					snake_case__      :Dict         =   False
					snake_case__      :Optional[Any]         =   TFFunnelModel(config=UpperCamelCase       )
					snake_case__      :str         =   model(UpperCamelCase       )
					self.parent.assertEqual(result.last_hidden_state.shape					,(self.batch_size, self.seq_length, self.d_model)       )

					snake_case__      :List[Any]         =   False
					snake_case__      :Optional[Any]         =   TFFunnelModel(config=UpperCamelCase       )
					snake_case__      :Union[str, Any]         =   model(UpperCamelCase       )
					self.parent.assertEqual(result.last_hidden_state.shape					,(self.batch_size, self.seq_length, self.d_model)       )






	def    lowerCAmelCase_							(		self					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,)						->    Tuple:
					snake_case__      :List[Any]         =   TFFunnelBaseModel(config=UpperCamelCase       )
					snake_case__      :Tuple         =   {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
					snake_case__      :Optional[int]         =   model(UpperCamelCase       )

					snake_case__      :List[str]         =   [input_ids, input_mask]
					snake_case__      :Optional[Any]         =   model(UpperCamelCase       )

					snake_case__      :Any         =   model(UpperCamelCase       )
					self.parent.assertEqual(result.last_hidden_state.shape					,(self.batch_size, 2, self.d_model)       )

					snake_case__      :Optional[Any]         =   False
					snake_case__      :Optional[int]         =   TFFunnelBaseModel(config=UpperCamelCase       )
					snake_case__      :List[Any]         =   model(UpperCamelCase       )
					self.parent.assertEqual(result.last_hidden_state.shape					,(self.batch_size, 3, self.d_model)       )

					snake_case__      :Dict         =   False
					snake_case__      :List[Any]         =   TFFunnelBaseModel(config=UpperCamelCase       )
					snake_case__      :Dict         =   model(UpperCamelCase       )
					self.parent.assertEqual(result.last_hidden_state.shape					,(self.batch_size, 2, self.d_model)       )






	def    lowerCAmelCase_							(		self					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,)						->    List[Any]:
					snake_case__      :Tuple         =   TFFunnelForPreTraining(config=UpperCamelCase       )
					snake_case__      :int         =   {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
					snake_case__      :Optional[int]         =   model(UpperCamelCase       )
					self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.seq_length)       )






	def    lowerCAmelCase_							(		self					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,)						->    Any:
					snake_case__      :List[Any]         =   TFFunnelForMaskedLM(config=UpperCamelCase       )
					snake_case__      :List[Any]         =   {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
					snake_case__      :Optional[int]         =   model(UpperCamelCase       )
					self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.seq_length, self.vocab_size)       )






	def    lowerCAmelCase_							(		self					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,)						->    int:
					snake_case__      :Union[str, Any]         =   self.num_labels
					snake_case__      :Tuple         =   TFFunnelForSequenceClassification(config=UpperCamelCase       )
					snake_case__      :Dict         =   {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
					snake_case__      :List[Any]         =   model(UpperCamelCase       )
					self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.num_labels)       )






	def    lowerCAmelCase_							(		self					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,)						->    List[str]:
					snake_case__      :Dict         =   self.num_choices
					snake_case__      :List[str]         =   TFFunnelForMultipleChoice(config=UpperCamelCase       )
					snake_case__      :Tuple         =   tf.tile(tf.expand_dims(UpperCamelCase					,1       )					,(1, self.num_choices, 1)       )
					snake_case__      :str         =   tf.tile(tf.expand_dims(UpperCamelCase					,1       )					,(1, self.num_choices, 1)       )
					snake_case__      :Dict         =   tf.tile(tf.expand_dims(UpperCamelCase					,1       )					,(1, self.num_choices, 1)       )
					snake_case__      :Tuple         =   {
					    "input_ids": multiple_choice_inputs_ids,
					    "attention_mask": multiple_choice_input_mask,
					    "token_type_ids": multiple_choice_token_type_ids,
					}
					snake_case__      :Optional[int]         =   model(UpperCamelCase       )
					self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.num_choices)       )






	def    lowerCAmelCase_							(		self					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,)						->    int:
					snake_case__      :Any         =   self.num_labels
					snake_case__      :Tuple         =   TFFunnelForTokenClassification(config=UpperCamelCase       )
					snake_case__      :int         =   {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
					snake_case__      :str         =   model(UpperCamelCase       )
					self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.seq_length, self.num_labels)       )






	def    lowerCAmelCase_							(		self					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,)						->    Union[str, Any]:
					snake_case__      :List[str]         =   TFFunnelForQuestionAnswering(config=UpperCamelCase       )
					snake_case__      :Any         =   {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
					snake_case__      :Union[str, Any]         =   model(UpperCamelCase       )
					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       )						->    Optional[Any]:
					snake_case__      :str         =   self.prepare_config_and_inputs()
					(
					    (
					    snake_case__
					)    ,						(
					    snake_case__
					)    ,						(
					    snake_case__
					)    ,						(
					    snake_case__
					)    ,						(
					    snake_case__
					)    ,						(
					    snake_case__
					)    ,						(
					    snake_case__
					)    ,						
					)      :str         =   config_and_inputs
					snake_case__      :Optional[int]         =   {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
					return config, inputs_dict




@require_tf
class 					_snake_case						(			_A       ,    _A       ,    unittest.TestCase ):
	_A          =   (
	    (
	        TFFunnelModel,
	        TFFunnelForMaskedLM,
	        TFFunnelForPreTraining,
	        TFFunnelForQuestionAnswering,
	        TFFunnelForTokenClassification,
	    )
	    if is_tf_available()
	    else ()
	)
	_A          =   (
	    {
	        '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          =   False
	_A          =   False






	def    lowerCAmelCase_							(		self       )						->    int:
					snake_case__      :List[Any]         =   TFFunnelModelTester(self       )
					snake_case__      :Optional[Any]         =   ConfigTester(self					,config_class=UpperCamelCase       )






	def    lowerCAmelCase_							(		self       )						->    Tuple:
					self.config_tester.run_common_tests()






	def    lowerCAmelCase_							(		self       )						->    List[Any]:
					snake_case__      :Dict         =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*UpperCamelCase       )






	def    lowerCAmelCase_							(		self       )						->    List[Any]:
					snake_case__      :str         =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_pretraining(*UpperCamelCase       )






	def    lowerCAmelCase_							(		self       )						->    Optional[Any]:
					snake_case__      :Tuple         =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_lm(*UpperCamelCase       )






	def    lowerCAmelCase_							(		self       )						->    List[Any]:
					snake_case__      :List[Any]         =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(*UpperCamelCase       )






	def    lowerCAmelCase_							(		self       )						->    Dict:
					snake_case__      :Tuple         =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(*UpperCamelCase       )




@require_tf
class 					_snake_case						(			_A       ,    unittest.TestCase ):
	_A          =   (
	    (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
	)
	_A          =   False
	_A          =   False






	def    lowerCAmelCase_							(		self       )						->    Union[str, Any]:
					snake_case__      :List[str]         =   TFFunnelModelTester(self					,base=UpperCamelCase       )
					snake_case__      :str         =   ConfigTester(self					,config_class=UpperCamelCase       )






	def    lowerCAmelCase_							(		self       )						->    str:
					self.config_tester.run_common_tests()






	def    lowerCAmelCase_							(		self       )						->    str:
					snake_case__      :List[str]         =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_base_model(*UpperCamelCase       )






	def    lowerCAmelCase_							(		self       )						->    Union[str, Any]:
					snake_case__      :str         =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase       )






	def    lowerCAmelCase_							(		self       )						->    List[str]:
					snake_case__      :Any         =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_multiple_choice(*UpperCamelCase       )







from __future__ import annotations





def 					lowercase_ (    __snake_case     :							list      )     -> float:





				'''simple docstring'''


				if not nums:
								raise ValueError("List is empty"      )
				return sum(__snake_case      ) / len(__snake_case      )


if __name__ == "__main__":
	import doctest

	doctest.testmod()