"""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 						__A							:






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

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






     def     __A  (		self  ):
       _lowerCAmelCase						:       Union[str, Any]  			=     ids_tensor([self.batch_size, self.seq_length]				,     self.vocab_size  )

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

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

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

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

       _lowerCAmelCase						:       Tuple  			=     [input_ids, input_mask]
       _lowerCAmelCase						:       List[str]  			=     model(a__  )

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

       _lowerCAmelCase						:       str  			=     False
       _lowerCAmelCase						:       List[str]  			=     TFFunnelModel(config=a__  )
       _lowerCAmelCase						:       Optional[int]  			=     model(a__  )
       self.parent.assertEqual(result.last_hidden_state.shape				,     (self.batch_size, self.seq_length, self.d_model)  )

       _lowerCAmelCase						:       int  			=     False
       _lowerCAmelCase						:       Tuple  			=     TFFunnelModel(config=a__  )
       _lowerCAmelCase						:       int  			=     model(a__  )
       self.parent.assertEqual(result.last_hidden_state.shape				,     (self.batch_size, self.seq_length, self.d_model)  )






     def     __A  (		self				,     a__				,     a__				,     a__				,     a__				,     a__				,     a__				,     a__				,     ):
       _lowerCAmelCase						:       Optional[Any]  			=     TFFunnelBaseModel(config=a__  )
       _lowerCAmelCase						:       Dict  			=     {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
       _lowerCAmelCase						:       Any  			=     model(a__  )

       _lowerCAmelCase						:       Optional[int]  			=     [input_ids, input_mask]
       _lowerCAmelCase						:       Any  			=     model(a__  )

       _lowerCAmelCase						:       Optional[int]  			=     model(a__  )
       self.parent.assertEqual(result.last_hidden_state.shape				,     (self.batch_size, 2, self.d_model)  )

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

       _lowerCAmelCase						:       Any  			=     False
       _lowerCAmelCase						:       Optional[Any]  			=     TFFunnelBaseModel(config=a__  )
       _lowerCAmelCase						:       Dict  			=     model(a__  )
       self.parent.assertEqual(result.last_hidden_state.shape				,     (self.batch_size, 2, self.d_model)  )






     def     __A  (		self				,     a__				,     a__				,     a__				,     a__				,     a__				,     a__				,     a__				,     ):
       _lowerCAmelCase						:       Union[str, Any]  			=     TFFunnelForPreTraining(config=a__  )
       _lowerCAmelCase						:       Union[str, Any]  			=     {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
       _lowerCAmelCase						:       Optional[int]  			=     model(a__  )
       self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length)  )






     def     __A  (		self				,     a__				,     a__				,     a__				,     a__				,     a__				,     a__				,     a__				,     ):
       _lowerCAmelCase						:       Any  			=     TFFunnelForMaskedLM(config=a__  )
       _lowerCAmelCase						:       List[str]  			=     {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
       _lowerCAmelCase						:       List[str]  			=     model(a__  )
       self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length, self.vocab_size)  )






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






     def     __A  (		self				,     a__				,     a__				,     a__				,     a__				,     a__				,     a__				,     a__				,     ):
       _lowerCAmelCase						:       List[str]  			=     self.num_choices
       _lowerCAmelCase						:       Dict  			=     TFFunnelForMultipleChoice(config=a__  )
       _lowerCAmelCase						:       Any  			=     tf.tile(tf.expand_dims(a__				,     1  )				,     (1, self.num_choices, 1)  )
       _lowerCAmelCase						:       int  			=     tf.tile(tf.expand_dims(a__				,     1  )				,     (1, self.num_choices, 1)  )
       _lowerCAmelCase						:       str  			=     tf.tile(tf.expand_dims(a__				,     1  )				,     (1, self.num_choices, 1)  )
       _lowerCAmelCase						:       List[str]  			=     {
           """input_ids""": multiple_choice_inputs_ids,
           """attention_mask""": multiple_choice_input_mask,
           """token_type_ids""": multiple_choice_token_type_ids,
       }
       _lowerCAmelCase						:       List[str]  			=     model(a__  )
       self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.num_choices)  )






     def     __A  (		self				,     a__				,     a__				,     a__				,     a__				,     a__				,     a__				,     a__				,     ):
       _lowerCAmelCase						:       Tuple  			=     self.num_labels
       _lowerCAmelCase						:       List[Any]  			=     TFFunnelForTokenClassification(config=a__  )
       _lowerCAmelCase						:       Optional[Any]  			=     {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
       _lowerCAmelCase						:       Dict  			=     model(a__  )
       self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length, self.num_labels)  )






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







@require_tf
class 						__A							( SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_    ,	unittest.TestCase		):
     _UpperCamelCase							:			Dict							=		(
         (
             TFFunnelModel,
             TFFunnelForMaskedLM,
             TFFunnelForPreTraining,
             TFFunnelForQuestionAnswering,
             TFFunnelForTokenClassification,
         )
         if is_tf_available()
         else ()
     )
     _UpperCamelCase							:			int							=		(
         {
             "feature-extraction": (TFFunnelBaseModel, TFFunnelModel),
             "fill-mask": TFFunnelForMaskedLM,
             "question-answering": TFFunnelForQuestionAnswering,
             "text-classification": TFFunnelForSequenceClassification,
             "token-classification": TFFunnelForTokenClassification,
             "zero-shot": TFFunnelForSequenceClassification,
         }
         if is_tf_available()
         else {}
     )
     _UpperCamelCase							:			List[str]							=		False
     _UpperCamelCase							:			List[str]							=		False






     def     __A  (		self  ):
       _lowerCAmelCase						:       Dict  			=     TFFunnelModelTester(self  )
       _lowerCAmelCase						:       Union[str, Any]  			=     ConfigTester(self				,     config_class=a__  )






     def     __A  (		self  ):
       self.config_tester.run_common_tests()






     def     __A  (		self  ):
       _lowerCAmelCase						:       List[Any]  			=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*a__  )






     def     __A  (		self  ):
       _lowerCAmelCase						:       Any  			=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_pretraining(*a__  )






     def     __A  (		self  ):
       _lowerCAmelCase						:       List[Any]  			=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_lm(*a__  )






     def     __A  (		self  ):
       _lowerCAmelCase						:       int  			=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(*a__  )






     def     __A  (		self  ):
       _lowerCAmelCase						:       List[str]  			=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*a__  )







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






     def     __A  (		self  ):
       _lowerCAmelCase						:       Tuple  			=     TFFunnelModelTester(self				,     base=a__  )
       _lowerCAmelCase						:       List[Any]  			=     ConfigTester(self				,     config_class=a__  )






     def     __A  (		self  ):
       self.config_tester.run_common_tests()






     def     __A  (		self  ):
       _lowerCAmelCase						:       Optional[int]  			=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_base_model(*a__  )






     def     __A  (		self  ):
       _lowerCAmelCase						:       Union[str, Any]  			=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*a__  )






     def     __A  (		self  ):
       _lowerCAmelCase						:       Optional[Any]  			=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_multiple_choice(*a__  )












"""simple docstring"""
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxSeqaSeqConfigWithPast
from ...utils import logging


lowerCamelCase_					=      logging.get_logger(__name__)

lowerCamelCase_					=      {
    '''t5-small''': '''https://huggingface.co/t5-small/resolve/main/config.json''',
    '''t5-base''': '''https://huggingface.co/t5-base/resolve/main/config.json''',
    '''t5-large''': '''https://huggingface.co/t5-large/resolve/main/config.json''',
    '''t5-3b''': '''https://huggingface.co/t5-3b/resolve/main/config.json''',
    '''t5-11b''': '''https://huggingface.co/t5-11b/resolve/main/config.json''',
}







class 			UpperCamelCase_       (__A     ):
	__magic_name__	      =				'''t5'''
	__magic_name__	      =				['''past_key_values''']
	__magic_name__	      =				{'''hidden_size''': '''d_model''', '''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers'''}





	def __init__(      self					: str	,	lowerCAmelCase_					: List[Any]=32_128	,	lowerCAmelCase_					: Tuple=512	,	lowerCAmelCase_					: Optional[int]=64	,	lowerCAmelCase_					: List[str]=2_048	,	lowerCAmelCase_					: Tuple=6	,	lowerCAmelCase_					: Dict=None	,	lowerCAmelCase_					: str=8	,	lowerCAmelCase_					: Optional[int]=32	,	lowerCAmelCase_					: Dict=128	,	lowerCAmelCase_					: Optional[Any]=0.1	,	lowerCAmelCase_					: str=1e-6	,	lowerCAmelCase_					: Dict=1.0	,	lowerCAmelCase_					: str="relu"	,	lowerCAmelCase_					: List[Any]=True	,	lowerCAmelCase_					: List[str]=True	,	lowerCAmelCase_					: Any=0	,	lowerCAmelCase_					: Tuple=1	,	**lowerCAmelCase_					: Optional[int]	,	)						->			int:
				UpperCAmelCase_     : int			     =					vocab_size
				UpperCAmelCase_     : Optional[Any]			     =					d_model
				UpperCAmelCase_     : str			     =					d_kv
				UpperCAmelCase_     : Any			     =					d_ff
				UpperCAmelCase_     : int			     =					num_layers
				UpperCAmelCase_     : Union[str, Any]			     =					(
				    num_decoder_layers if num_decoder_layers is not None else self.num_layers
				)  # default = symmetry
				UpperCAmelCase_     : Optional[Any]			     =					num_heads
				UpperCAmelCase_     : Any			     =					relative_attention_num_buckets
				UpperCAmelCase_     : Optional[Any]			     =					relative_attention_max_distance
				UpperCAmelCase_     : Optional[Any]			     =					dropout_rate
				UpperCAmelCase_     : Tuple			     =					layer_norm_epsilon
				UpperCAmelCase_     : int			     =					initializer_factor
				UpperCAmelCase_     : int			     =					feed_forward_proj
				UpperCAmelCase_     : str			     =					use_cache

				UpperCAmelCase_     : Tuple			     =					self.feed_forward_proj.split("-"						)
				UpperCAmelCase_     : List[Any]			     =					act_info[-1]
				UpperCAmelCase_     : Optional[int]			     =					act_info[0] == "gated"

				if len(lowerCAmelCase_						) > 1 and act_info[0] != "gated" or len(lowerCAmelCase_						) > 2:
							raise ValueError(
							    f"""`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer."""
							    "Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. "
							    "'gated-gelu' or 'relu'"						)

				# for backwards compatibility
				if feed_forward_proj == "gated-gelu":
							UpperCAmelCase_     : int			     =					"gelu_new"

				super().__init__(
				    pad_token_id=lowerCAmelCase_	,	eos_token_id=lowerCAmelCase_	,	is_encoder_decoder=lowerCAmelCase_	,	**lowerCAmelCase_	,	)







class 			UpperCamelCase_       (__A     ):





	@property
	def      _SCREAMING_SNAKE_CASE      (      self					: Dict						)						->			Mapping[str, Mapping[int, str]]:
				UpperCAmelCase_     : Any			     =					{
				    "input_ids": {0: "batch", 1: "encoder_sequence"},
				    "attention_mask": {0: "batch", 1: "encoder_sequence"},
				}
				if self.use_past:
							UpperCAmelCase_     : List[Any]			     =					"past_encoder_sequence + sequence"
							UpperCAmelCase_     : Union[str, Any]			     =					{0: "batch"}
							UpperCAmelCase_     : Optional[Any]			     =					{0: "batch", 1: "past_decoder_sequence + sequence"}
				else:
							UpperCAmelCase_     : List[Any]			     =					{0: "batch", 1: "decoder_sequence"}
							UpperCAmelCase_     : Tuple			     =					{0: "batch", 1: "decoder_sequence"}

				if self.use_past:
							self.fill_with_past_key_values_(lowerCAmelCase_	,	direction="inputs"						)

				return common_inputs





	@property
	def      _SCREAMING_SNAKE_CASE      (      self					: Any						)						->			int:
				return 13



