


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:
       def __init__(   self, lowerCamelCase, lowerCamelCase=13, lowerCamelCase=7, lowerCamelCase=True, lowerCamelCase=True, lowerCamelCase=True, lowerCamelCase=True, lowerCamelCase=99, lowerCamelCase=[1, 1, 2], lowerCamelCase=1, lowerCamelCase=32, lowerCamelCase=4, lowerCamelCase=8, lowerCamelCase=37, lowerCamelCase="gelu_new", lowerCamelCase=0.1, lowerCamelCase=0.1, lowerCamelCase=0.0, lowerCamelCase=5_12, lowerCamelCase=3, lowerCamelCase=0.0_2, lowerCamelCase=3, lowerCamelCase=4, lowerCamelCase=None, lowerCamelCase=False, )			->  str:




           """simple docstring"""

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

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




           """simple docstring"""

           _lowercase			:				List[Any] 						=						ids_tensor([self.batch_size, self.seq_length], self.vocab_size)

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

           _lowercase			:				Dict 						=						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			:				List[Any] 						=						None
           _lowercase			:				Optional[Any] 						=						None
           if self.use_labels:
               _lowercase			:				Any 						=						ids_tensor([self.batch_size], self.type_sequence_label_size)
               _lowercase			:				Tuple 						=						ids_tensor([self.batch_size, self.seq_length], self.num_labels)
               _lowercase			:				Union[str, Any] 						=						ids_tensor([self.batch_size], self.num_choices)

           _lowercase			:				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      UpperCamelCase	(   self, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, )			->  int:




           """simple docstring"""

           _lowercase			:				List[Any] 						=						TFFunnelModel(config=lowerCamelCase)
           _lowercase			:				List[str] 						=						{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
           _lowercase			:				Tuple 						=						model(lowerCamelCase)

           _lowercase			:				Tuple 						=						[input_ids, input_mask]
           _lowercase			:				Tuple 						=						model(lowerCamelCase)

           _lowercase			:				List[str] 						=						model(lowerCamelCase)
           self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.d_model))

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

           _lowercase			:				Optional[int] 						=						False
           _lowercase			:				Any 						=						TFFunnelModel(config=lowerCamelCase)
           _lowercase			:				List[str] 						=						model(lowerCamelCase)
           self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.d_model))
       def      UpperCamelCase	(   self, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, )			->  str:




           """simple docstring"""

           _lowercase			:				Optional[int] 						=						TFFunnelBaseModel(config=lowerCamelCase)
           _lowercase			:				List[Any] 						=						{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
           _lowercase			:				Any 						=						model(lowerCamelCase)

           _lowercase			:				str 						=						[input_ids, input_mask]
           _lowercase			:				Optional[Any] 						=						model(lowerCamelCase)

           _lowercase			:				str 						=						model(lowerCamelCase)
           self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, 2, self.d_model))

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

           _lowercase			:				List[Any] 						=						False
           _lowercase			:				str 						=						TFFunnelBaseModel(config=lowerCamelCase)
           _lowercase			:				Union[str, Any] 						=						model(lowerCamelCase)
           self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, 2, self.d_model))
       def      UpperCamelCase	(   self, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, )			->  Tuple:




           """simple docstring"""

           _lowercase			:				Dict 						=						TFFunnelForPreTraining(config=lowerCamelCase)
           _lowercase			:				str 						=						{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
           _lowercase			:				List[Any] 						=						model(lowerCamelCase)
           self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length))
       def      UpperCamelCase	(   self, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, )			->  Dict:




           """simple docstring"""

           _lowercase			:				Tuple 						=						TFFunnelForMaskedLM(config=lowerCamelCase)
           _lowercase			:				List[str] 						=						{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
           _lowercase			:				List[str] 						=						model(lowerCamelCase)
           self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.vocab_size))
       def      UpperCamelCase	(   self, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, )			->  Tuple:




           """simple docstring"""

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




           """simple docstring"""

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




           """simple docstring"""

           _lowercase			:				Tuple 						=						self.num_labels
           _lowercase			:				Optional[int] 						=						TFFunnelForTokenClassification(config=lowerCamelCase)
           _lowercase			:				List[str] 						=						{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
           _lowercase			:				int 						=						model(lowerCamelCase)
           self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.num_labels))
       def      UpperCamelCase	(   self, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, )			->  Tuple:




           """simple docstring"""

           _lowercase			:				Dict 						=						TFFunnelForQuestionAnswering(config=lowerCamelCase)
           _lowercase			:				Union[str, Any] 						=						{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
           _lowercase			:				Any 						=						model(lowerCamelCase)
           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      UpperCamelCase	(   self)			->  Optional[Any]:




           """simple docstring"""

           _lowercase			:				List[str] 						=						self.prepare_config_and_inputs()
           (
               (
               _lowercase
           )						,							(
               _lowercase
           )						,							(
               _lowercase
           )						,							(
               _lowercase
           )						,							(
               _lowercase
           )						,							(
               _lowercase
           )						,							(
               _lowercase
           )						,							
           )			:				Dict 						=						config_and_inputs
           _lowercase			:				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  ):
       lowercase_					:      int       					=     (
           (
               TFFunnelModel,
               TFFunnelForMaskedLM,
               TFFunnelForPreTraining,
               TFFunnelForQuestionAnswering,
               TFFunnelForTokenClassification,
           )
           if is_tf_available()
           else ()
       )
       lowercase_					:      Optional[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 {}
       )
       lowercase_					:      Optional[Any]       					=     False
       lowercase_					:      Tuple       					=     False
       def      UpperCamelCase	(   self)			->  Union[str, Any]:




           """simple docstring"""

           _lowercase			:				Optional[int] 						=						TFFunnelModelTester(self)
           _lowercase			:				Any 						=						ConfigTester(self, config_class=lowerCamelCase)
       def      UpperCamelCase	(   self)			->  Optional[int]:




           """simple docstring"""

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




           """simple docstring"""

           _lowercase			:				str 						=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*lowerCamelCase)
       def      UpperCamelCase	(   self)			->  Optional[Any]:




           """simple docstring"""

           _lowercase			:				Any 						=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_pretraining(*lowerCamelCase)
       def      UpperCamelCase	(   self)			->  Any:




           """simple docstring"""

           _lowercase			:				Union[str, Any] 						=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase)
       def      UpperCamelCase	(   self)			->  Tuple:




           """simple docstring"""

           _lowercase			:				Dict 						=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(*lowerCamelCase)




       def      UpperCamelCase	(   self)			->  Optional[int]:




           """simple docstring"""

           _lowercase			:				Dict 						=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(*lowerCamelCase)






@require_tf
class _lowerCamelCase(		_a,					unittest.TestCase  ):
       lowercase_					:      int       					=     (
           (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
       )
       lowercase_					:      Dict       					=     False
       lowercase_					:      List[Any]       					=     False
       def      UpperCamelCase	(   self)			->  List[Any]:




           """simple docstring"""

           _lowercase			:				Tuple 						=						TFFunnelModelTester(self, base=lowerCamelCase)
           _lowercase			:				List[str] 						=						ConfigTester(self, config_class=lowerCamelCase)
       def      UpperCamelCase	(   self)			->  List[Any]:




           """simple docstring"""

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




           """simple docstring"""

           _lowercase			:				List[Any] 						=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_base_model(*lowerCamelCase)
       def      UpperCamelCase	(   self)			->  Any:




           """simple docstring"""

           _lowercase			:				Optional[Any] 						=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase)




       def      UpperCamelCase	(   self)			->  List[str]:




           """simple docstring"""

           _lowercase			:				List[Any] 						=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_multiple_choice(*lowerCamelCase)








"""simple docstring"""






from typing import Any







def 	_snake_case     (							lowercase__				,       lowercase__				,       lowercase__				,       lowercase__				,       lowercase__				,       ):
				_validation(
				    lowercase__				,       lowercase__				,       lowercase__				,       lowercase__				,       lowercase__				,       )
				# Creates data structures and fill initial step
				_lowerCamelCase	:     dict       =       {}
				_lowerCamelCase	:     dict       =       {}
				for state in states_space:
								_lowerCamelCase	:     Optional[Any]       =       observations_space[0]
								_lowerCamelCase	:     int       =       (
								    initial_probabilities[state] * emission_probabilities[state][observation]
								)
								_lowerCamelCase	:     List[Any]       =       None

				# Fills the data structure with the probabilities of
				# different transitions and pointers to previous states
				for o in range(1				,       len(lowercase__						)						):
								_lowerCamelCase	:     List[str]       =       observations_space[o]
								_lowerCamelCase	:     str       =       observations_space[o - 1]
								for state in states_space:
												# Calculates the argmax for probability function
												_lowerCamelCase	:     Dict       =       ''
												_lowerCamelCase	:     int       =       -1
												for k_state in states_space:
																_lowerCamelCase	:     Any       =       (
																    probabilities[(k_state, prior_observation)]
																    * transition_probabilities[k_state][state]
																    * emission_probabilities[state][observation]
																)
																if probability > max_probability:
																				_lowerCamelCase	:     Dict       =       probability
																				_lowerCamelCase	:     int       =       k_state

            # Update probabilities and pointers dicts
												_lowerCamelCase	:     List[str]       =       (
												    probabilities[(arg_max, prior_observation)]
												    * transition_probabilities[arg_max][state]
												    * emission_probabilities[state][observation]
												)

												_lowerCamelCase	:     Union[str, Any]       =       arg_max

    # The final observation
				_lowerCamelCase	:     Tuple       =       observations_space[len(lowercase__						) - 1]

				# argmax for given final observation
				_lowerCamelCase	:     Optional[int]       =       ''
				_lowerCamelCase	:     str       =       -1
				for k_state in states_space:
								_lowerCamelCase	:     Optional[int]       =       probabilities[(k_state, final_observation)]
								if probability > max_probability:
												_lowerCamelCase	:     int       =       probability
												_lowerCamelCase	:     Optional[int]       =       k_state
				_lowerCamelCase	:     Any       =       arg_max

				# Process pointers backwards
				_lowerCamelCase	:     Optional[Any]       =       last_state
				_lowerCamelCase	:     Union[str, Any]       =       []
				for o in range(len(lowercase__						) - 1				,       -1				,       -1						):
								result.append(lowercase__						)
								_lowerCamelCase	:     Dict       =       pointers[previous, observations_space[o]]
				result.reverse()

				return result







def 	_snake_case     (							lowercase__				,       lowercase__				,       lowercase__				,       lowercase__				,       lowercase__				,       ):
				_validate_not_empty(
				    lowercase__				,       lowercase__				,       lowercase__				,       lowercase__				,       lowercase__				,       )
				_validate_lists(lowercase__				,       lowercase__						)
				_validate_dicts(
				    lowercase__				,       lowercase__				,       lowercase__						)







def 	_snake_case     (							lowercase__				,       lowercase__				,       lowercase__				,       lowercase__				,       lowercase__				,       ):
				if not all(
				    [
				        observations_space,
				        states_space,
				        initial_probabilities,
				        transition_probabilities,
				        emission_probabilities,
				    ]						):
								raise ValueError('There\'s an empty parameter'						)







def 	_snake_case     (							lowercase__				,       lowercase__						):
				_validate_list(lowercase__				,       'observations_space'						)
				_validate_list(lowercase__				,       'states_space'						)







def 	_snake_case     (							lowercase__				,       lowercase__						):
				if not isinstance(_object				,       lowercase__						):
								_lowerCamelCase	:     Tuple       =       f'''{var_name} must be a list'''
								raise ValueError(lowercase__						)
				else:
								for x in _object:
												if not isinstance(lowercase__				,       lowercase__						):
																_lowerCamelCase	:     int       =       f'''{var_name} must be a list of strings'''
																raise ValueError(lowercase__						)







def 	_snake_case     (							lowercase__				,       lowercase__				,       lowercase__				,       ):
				_validate_dict(lowercase__				,       'initial_probabilities'				,       lowercase__						)
				_validate_nested_dict(lowercase__				,       'transition_probabilities'						)
				_validate_nested_dict(lowercase__				,       'emission_probabilities'						)







def 	_snake_case     (							lowercase__				,       lowercase__						):
				_validate_dict(_object				,       lowercase__				,       lowercase__						)
				for x in _object.values():
								_validate_dict(lowercase__				,       lowercase__				,       lowercase__				,       lowercase__						)







def 	_snake_case     (							lowercase__				,       lowercase__				,       lowercase__				,       lowercase__ = False						):
				if not isinstance(_object				,       lowercase__						):
								_lowerCamelCase	:     Tuple       =       f'''{var_name} must be a dict'''
								raise ValueError(lowercase__						)
				if not all(isinstance(lowercase__				,       lowercase__						) for x in _object						):
								_lowerCamelCase	:     Optional[int]       =       f'''{var_name} all keys must be strings'''
								raise ValueError(lowercase__						)
				if not all(isinstance(lowercase__				,       lowercase__						) for x in _object.values()						):
								_lowerCamelCase	:     Optional[int]       =       'nested dictionary ' if nested else ''
								_lowerCamelCase	:     Optional[Any]       =       f'''{var_name} {nested_text}all values must be {value_type.__name__}'''
								raise ValueError(lowercase__						)


if __name__ == "__main__":
			from doctest import testmod

			testmod()