





'''simple docstring'''






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




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

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




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

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

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

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

            _lowerCAmelCase					=				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 							lowercase__       (     self     :					List[str]     ,     __snake_case     :					Optional[int]     ,     __snake_case     :					Optional[Any]     ,     __snake_case     :					Optional[Any]     ,     __snake_case     :					Union[str, Any]     ,     __snake_case     :					Union[str, Any]     ,     __snake_case     :					List[str]     ,     __snake_case     :					int     ,     )							-> str:
            _lowerCAmelCase					=				TFFunnelModel(config=_snake_case    )
            _lowerCAmelCase					=				{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
            _lowerCAmelCase					=				model(_snake_case    )

            _lowerCAmelCase					=				[input_ids, input_mask]
            _lowerCAmelCase					=				model(_snake_case    )

            _lowerCAmelCase					=				model(_snake_case    )
            self.parent.assertEqual(result.last_hidden_state.shape     ,     (self.batch_size, self.seq_length, self.d_model)    )

            _lowerCAmelCase					=				False
            _lowerCAmelCase					=				TFFunnelModel(config=_snake_case    )
            _lowerCAmelCase					=				model(_snake_case    )
            self.parent.assertEqual(result.last_hidden_state.shape     ,     (self.batch_size, self.seq_length, self.d_model)    )

            _lowerCAmelCase					=				False
            _lowerCAmelCase					=				TFFunnelModel(config=_snake_case    )
            _lowerCAmelCase					=				model(_snake_case    )
            self.parent.assertEqual(result.last_hidden_state.shape     ,     (self.batch_size, self.seq_length, self.d_model)    )




      def 							lowercase__       (     self     :					int     ,     __snake_case     :					Optional[Any]     ,     __snake_case     :					Optional[int]     ,     __snake_case     :					Any     ,     __snake_case     :					List[str]     ,     __snake_case     :					List[Any]     ,     __snake_case     :					str     ,     __snake_case     :					Any     ,     )							-> int:
            _lowerCAmelCase					=				TFFunnelBaseModel(config=_snake_case    )
            _lowerCAmelCase					=				{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
            _lowerCAmelCase					=				model(_snake_case    )

            _lowerCAmelCase					=				[input_ids, input_mask]
            _lowerCAmelCase					=				model(_snake_case    )

            _lowerCAmelCase					=				model(_snake_case    )
            self.parent.assertEqual(result.last_hidden_state.shape     ,     (self.batch_size, 2, self.d_model)    )

            _lowerCAmelCase					=				False
            _lowerCAmelCase					=				TFFunnelBaseModel(config=_snake_case    )
            _lowerCAmelCase					=				model(_snake_case    )
            self.parent.assertEqual(result.last_hidden_state.shape     ,     (self.batch_size, 3, self.d_model)    )

            _lowerCAmelCase					=				False
            _lowerCAmelCase					=				TFFunnelBaseModel(config=_snake_case    )
            _lowerCAmelCase					=				model(_snake_case    )
            self.parent.assertEqual(result.last_hidden_state.shape     ,     (self.batch_size, 2, self.d_model)    )




      def 							lowercase__       (     self     :					Dict     ,     __snake_case     :					List[str]     ,     __snake_case     :					List[Any]     ,     __snake_case     :					List[Any]     ,     __snake_case     :					int     ,     __snake_case     :					List[str]     ,     __snake_case     :					int     ,     __snake_case     :					str     ,     )							-> Union[str, Any]:
            _lowerCAmelCase					=				TFFunnelForPreTraining(config=_snake_case    )
            _lowerCAmelCase					=				{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
            _lowerCAmelCase					=				model(_snake_case    )
            self.parent.assertEqual(result.logits.shape     ,     (self.batch_size, self.seq_length)    )




      def 							lowercase__       (     self     :					Union[str, Any]     ,     __snake_case     :					List[Any]     ,     __snake_case     :					List[Any]     ,     __snake_case     :					List[Any]     ,     __snake_case     :					Tuple     ,     __snake_case     :					Any     ,     __snake_case     :					List[Any]     ,     __snake_case     :					str     ,     )							-> Optional[Any]:
            _lowerCAmelCase					=				TFFunnelForMaskedLM(config=_snake_case    )
            _lowerCAmelCase					=				{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
            _lowerCAmelCase					=				model(_snake_case    )
            self.parent.assertEqual(result.logits.shape     ,     (self.batch_size, self.seq_length, self.vocab_size)    )




      def 							lowercase__       (     self     :					str     ,     __snake_case     :					Optional[Any]     ,     __snake_case     :					Dict     ,     __snake_case     :					str     ,     __snake_case     :					str     ,     __snake_case     :					Optional[int]     ,     __snake_case     :					Dict     ,     __snake_case     :					Optional[int]     ,     )							-> str:
            _lowerCAmelCase					=				self.num_labels
            _lowerCAmelCase					=				TFFunnelForSequenceClassification(config=_snake_case    )
            _lowerCAmelCase					=				{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
            _lowerCAmelCase					=				model(_snake_case    )
            self.parent.assertEqual(result.logits.shape     ,     (self.batch_size, self.num_labels)    )




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




      def 							lowercase__       (     self     :					int     ,     __snake_case     :					int     ,     __snake_case     :					Tuple     ,     __snake_case     :					Dict     ,     __snake_case     :					Any     ,     __snake_case     :					Any     ,     __snake_case     :					Union[str, Any]     ,     __snake_case     :					int     ,     )							-> Optional[int]:
            _lowerCAmelCase					=				self.num_labels
            _lowerCAmelCase					=				TFFunnelForTokenClassification(config=_snake_case    )
            _lowerCAmelCase					=				{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
            _lowerCAmelCase					=				model(_snake_case    )
            self.parent.assertEqual(result.logits.shape     ,     (self.batch_size, self.seq_length, self.num_labels)    )




      def 							lowercase__       (     self     :					Tuple     ,     __snake_case     :					Dict     ,     __snake_case     :					Union[str, Any]     ,     __snake_case     :					List[str]     ,     __snake_case     :					List[str]     ,     __snake_case     :					List[Any]     ,     __snake_case     :					Optional[int]     ,     __snake_case     :					Tuple     ,     )							-> Tuple:
            _lowerCAmelCase					=				TFFunnelForQuestionAnswering(config=_snake_case    )
            _lowerCAmelCase					=				{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
            _lowerCAmelCase					=				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 							lowercase__       (     self     :					List[Any]    )							-> int:
            _lowerCAmelCase					=				self.prepare_config_and_inputs()
            (
                (
                _lowerCAmelCase     
            )	,				(
                _lowerCAmelCase     
            )	,				(
                _lowerCAmelCase     
            )	,				(
                _lowerCAmelCase     
            )	,				(
                _lowerCAmelCase     
            )	,				(
                _lowerCAmelCase     
            )	,				(
                _lowerCAmelCase     
            )	,				     
            )					=				config_and_inputs
            _lowerCAmelCase					=				{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
            return config, inputs_dict




@require_tf
class 					UpperCAmelCase	(     _lowercase				,						_lowercase				,						unittest.TestCase  ):
      _lowercase:     int			=  (
          (
              TFFunnelModel,
              TFFunnelForMaskedLM,
              TFFunnelForPreTraining,
              TFFunnelForQuestionAnswering,
              TFFunnelForTokenClassification,
          )
          if is_tf_available()
          else ()
      )
      _lowercase:     Optional[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 {}
      )
      _lowercase:     int			=  False
      _lowercase:     Union[str, Any]			=  False




      def 							lowercase__       (     self     :					str    )							-> Dict:
            _lowerCAmelCase					=				TFFunnelModelTester(self    )
            _lowerCAmelCase					=				ConfigTester(self     ,     config_class=_snake_case    )




      def 							lowercase__       (     self     :					Dict    )							-> Optional[int]:
            self.config_tester.run_common_tests()




      def 							lowercase__       (     self     :					Tuple    )							-> List[Any]:
            _lowerCAmelCase					=				self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*_snake_case    )




      def 							lowercase__       (     self     :					Union[str, Any]    )							-> Dict:
            _lowerCAmelCase					=				self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_pretraining(*_snake_case    )




      def 							lowercase__       (     self     :					int    )							-> Optional[int]:
            _lowerCAmelCase					=				self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_masked_lm(*_snake_case    )




      def 							lowercase__       (     self     :					Tuple    )							-> Union[str, Any]:
            _lowerCAmelCase					=				self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_token_classification(*_snake_case    )




      def 							lowercase__       (     self     :					Union[str, Any]    )							-> Optional[int]:
            _lowerCAmelCase					=				self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_question_answering(*_snake_case    )




@require_tf
class 					UpperCAmelCase	(     _lowercase				,						unittest.TestCase  ):
      _lowercase:     Optional[Any]			=  (
          (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
      )
      _lowercase:     List[Any]			=  False
      _lowercase:     List[str]			=  False




      def 							lowercase__       (     self     :					int    )							-> int:
            _lowerCAmelCase					=				TFFunnelModelTester(self     ,     base=_snake_case    )
            _lowerCAmelCase					=				ConfigTester(self     ,     config_class=_snake_case    )




      def 							lowercase__       (     self     :					Dict    )							-> List[str]:
            self.config_tester.run_common_tests()




      def 							lowercase__       (     self     :					List[str]    )							-> Union[str, Any]:
            _lowerCAmelCase					=				self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_base_model(*_snake_case    )




      def 							lowercase__       (     self     :					Any    )							-> int:
            _lowerCAmelCase					=				self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_sequence_classification(*_snake_case    )




      def 							lowercase__       (     self     :					List[Any]    )							-> str:
            _lowerCAmelCase					=				self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_multiple_choice(*_snake_case    )








"""simple docstring"""





from typing import TYPE_CHECKING

from ..utils import _LazyModule


UpperCamelCase_       :    Dict						      =			{
    '''config''': [
        '''EXTERNAL_DATA_FORMAT_SIZE_LIMIT''',
        '''OnnxConfig''',
        '''OnnxConfigWithPast''',
        '''OnnxSeq2SeqConfigWithPast''',
        '''PatchingSpec''',
    ],
    '''convert''': ['''export''', '''validate_model_outputs'''],
    '''features''': ['''FeaturesManager'''],
    '''utils''': ['''ParameterFormat''', '''compute_serialized_parameters_size'''],
}


if TYPE_CHECKING:
		from .config import (
		    EXTERNAL_DATA_FORMAT_SIZE_LIMIT,
		    OnnxConfig,
		    OnnxConfigWithPast,
		    OnnxSeqaSeqConfigWithPast,
		    PatchingSpec,
		)
		from .convert import export, validate_model_outputs
		from .features import FeaturesManager
		from .utils import ParameterFormat, compute_serialized_parameters_size

else:
		import sys

		UpperCamelCase_       :    List[Any]						      =			_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)




