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

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




      def 		snake_case_			(      self		:       List[str]				):
          __lowercase		:			Dict	    =       ids_tensor([self.batch_size, self.seq_length]				,       self.vocab_size				)

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

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

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

          __lowercase		:			Union[str, 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 		snake_case_			(      self		:       List[str]				,       _snake_case		:       Optional[int]				,       _snake_case		:       Optional[Any]				,       _snake_case		:       Any				,       _snake_case		:       Dict				,       _snake_case		:       List[Any]				,       _snake_case		:       Tuple				,       _snake_case		:       Tuple				,       ):
          __lowercase		:			Any	    =       TFFunnelModel(config=_snake_case				)
          __lowercase		:			Tuple	    =       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
          __lowercase		:			Tuple	    =       model(_snake_case				)

          __lowercase		:			List[str]	    =       [input_ids, input_mask]
          __lowercase		:			Tuple	    =       model(_snake_case				)

          __lowercase		:			List[Any]	    =       model(_snake_case				)
          self.parent.assertEqual(result.last_hidden_state.shape				,       (self.batch_size, self.seq_length, self.d_model)				)

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

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




      def 		snake_case_			(      self		:       Dict				,       _snake_case		:       Optional[int]				,       _snake_case		:       List[Any]				,       _snake_case		:       Optional[int]				,       _snake_case		:       Optional[int]				,       _snake_case		:       Any				,       _snake_case		:       List[Any]				,       _snake_case		:       Optional[int]				,       ):
          __lowercase		:			int	    =       TFFunnelBaseModel(config=_snake_case				)
          __lowercase		:			List[str]	    =       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
          __lowercase		:			Optional[int]	    =       model(_snake_case				)

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

          __lowercase		:			List[str]	    =       model(_snake_case				)
          self.parent.assertEqual(result.last_hidden_state.shape				,       (self.batch_size, 2, self.d_model)				)

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

          __lowercase		:			List[str]	    =       False
          __lowercase		:			Dict	    =       TFFunnelBaseModel(config=_snake_case				)
          __lowercase		:			List[str]	    =       model(_snake_case				)
          self.parent.assertEqual(result.last_hidden_state.shape				,       (self.batch_size, 2, self.d_model)				)




      def 		snake_case_			(      self		:       List[Any]				,       _snake_case		:       List[Any]				,       _snake_case		:       List[Any]				,       _snake_case		:       Dict				,       _snake_case		:       Dict				,       _snake_case		:       List[Any]				,       _snake_case		:       Tuple				,       _snake_case		:       int				,       ):
          __lowercase		:			Dict	    =       TFFunnelForPreTraining(config=_snake_case				)
          __lowercase		:			str	    =       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
          __lowercase		:			List[Any]	    =       model(_snake_case				)
          self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.seq_length)				)




      def 		snake_case_			(      self		:       Optional[Any]				,       _snake_case		:       Union[str, Any]				,       _snake_case		:       Union[str, Any]				,       _snake_case		:       List[Any]				,       _snake_case		:       Optional[int]				,       _snake_case		:       Any				,       _snake_case		:       List[str]				,       _snake_case		:       Union[str, Any]				,       ):
          __lowercase		:			Any	    =       TFFunnelForMaskedLM(config=_snake_case				)
          __lowercase		:			Optional[int]	    =       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
          __lowercase		:			Optional[int]	    =       model(_snake_case				)
          self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.seq_length, self.vocab_size)				)




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




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




      def 		snake_case_			(      self		:       List[str]				,       _snake_case		:       Tuple				,       _snake_case		:       Dict				,       _snake_case		:       Optional[Any]				,       _snake_case		:       Optional[int]				,       _snake_case		:       List[str]				,       _snake_case		:       str				,       _snake_case		:       Any				,       ):
          __lowercase		:			List[Any]	    =       self.num_labels
          __lowercase		:			Tuple	    =       TFFunnelForTokenClassification(config=_snake_case				)
          __lowercase		:			Optional[int]	    =       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
          __lowercase		:			Optional[int]	    =       model(_snake_case				)
          self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.seq_length, self.num_labels)				)




      def 		snake_case_			(      self		:       Union[str, Any]				,       _snake_case		:       str				,       _snake_case		:       Optional[int]				,       _snake_case		:       str				,       _snake_case		:       List[str]				,       _snake_case		:       List[Any]				,       _snake_case		:       Union[str, Any]				,       _snake_case		:       List[str]				,       ):
          __lowercase		:			Optional[Any]	    =       TFFunnelForQuestionAnswering(config=_snake_case				)
          __lowercase		:			List[str]	    =       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
          __lowercase		:			Union[str, Any]	    =       model(_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 		snake_case_			(      self		:       List[Any]				):
          __lowercase		:			Tuple	    =       self.prepare_config_and_inputs()
          (
              __lowercase      
          )		:			Any	    =       config_and_inputs
          __lowercase		:			int	    =       {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
          return config, inputs_dict






@require_tf
class 						__lowerCAmelCase	(	lowerCAmelCase_ , lowerCAmelCase_ , unittest.TestCase      ):



      """simple docstring"""







      A__	:			Dict        =    (
          (
              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__	:			Optional[Any]        =    False




      def 		snake_case_			(      self		:       Any				):
          __lowercase		:			Union[str, Any]	    =       TFFunnelModelTester(self				)
          __lowercase		:			Union[str, Any]	    =       ConfigTester(self				,       config_class=_snake_case				)




      def 		snake_case_			(      self		:       List[str]				):
          self.config_tester.run_common_tests()




      def 		snake_case_			(      self		:       List[Any]				):
          __lowercase		:			List[str]	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*_snake_case				)




      def 		snake_case_			(      self		:       Union[str, Any]				):
          __lowercase		:			List[str]	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_pretraining(*_snake_case				)




      def 		snake_case_			(      self		:       Dict				):
          __lowercase		:			Dict	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_lm(*_snake_case				)




      def 		snake_case_			(      self		:       Dict				):
          __lowercase		:			Dict	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_token_classification(*_snake_case				)




      def 		snake_case_			(      self		:       Any				):
          __lowercase		:			Optional[int]	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_question_answering(*_snake_case				)






@require_tf
class 						__lowerCAmelCase	(	lowerCAmelCase_ , unittest.TestCase      ):



      """simple docstring"""







      A__	:			List[Any]        =    (
          (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
      )
      A__	:			List[Any]        =    False
      A__	:			Any        =    False




      def 		snake_case_			(      self		:       List[str]				):
          __lowercase		:			Tuple	    =       TFFunnelModelTester(self				,       base=_snake_case				)
          __lowercase		:			str	    =       ConfigTester(self				,       config_class=_snake_case				)




      def 		snake_case_			(      self		:       Dict				):
          self.config_tester.run_common_tests()




      def 		snake_case_			(      self		:       Union[str, Any]				):
          __lowercase		:			Union[str, Any]	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_base_model(*_snake_case				)




      def 		snake_case_			(      self		:       Union[str, Any]				):
          __lowercase		:			int	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_sequence_classification(*_snake_case				)




      def 		snake_case_			(      self		:       int				):
          __lowercase		:			Union[str, Any]	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_multiple_choice(*_snake_case				)




from __future__ import annotations


def     UpperCAmelCase_  (						__lowerCAmelCase	) ->						int:
    if not nums:
        return 0
    __lowercase		:			List[Any]	    =       nums[0]
    __lowercase		:			Union[str, Any]	    =       0
    for num in nums[1:]:
        __lowercase    ,      __lowercase		:			Dict	    =       (
            max_excluding + num,
            max(__lowerCAmelCase	,					__lowerCAmelCase	),
        )
    return max(__lowerCAmelCase	,					__lowerCAmelCase	)


if __name__ == "__main__":
 import doctest

 doctest.testmod()


